blob: af512927c607518ad40f3e8107b6dbcf8b2a8919 [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 Heo11ebea52012-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 Heoe22bee72010-06-29 10:07:14 +020061 WORKER_REBIND = 1 << 5, /* mom is home, come back */
Tejun Heofb0e7be2010-06-29 10:07:15 +020062 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
Tejun Heof3421792010-07-02 10:03:51 +020063 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
Tejun Heoe22bee72010-06-29 10:07:14 +020064
Tejun Heo403c8212012-07-17 12:39:27 -070065 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_REBIND | WORKER_UNBOUND |
66 WORKER_CPU_INTENSIVE,
Tejun Heodb7bccf2010-06-29 10:07:12 +020067
68 /* gcwq->trustee_state */
69 TRUSTEE_START = 0, /* start */
70 TRUSTEE_IN_CHARGE = 1, /* trustee in charge of gcwq */
71 TRUSTEE_BUTCHER = 2, /* butcher workers */
72 TRUSTEE_RELEASE = 3, /* release workers */
73 TRUSTEE_DONE = 4, /* trustee is done */
Tejun Heoc8e55f32010-06-29 10:07:12 +020074
Tejun Heo32704762012-07-13 22:16:45 -070075 NR_WORKER_POOLS = 2, /* # worker pools per gcwq */
Tejun Heo4ce62e92012-07-13 22:16:44 -070076
Tejun Heoc8e55f32010-06-29 10:07:12 +020077 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
78 BUSY_WORKER_HASH_SIZE = 1 << BUSY_WORKER_HASH_ORDER,
79 BUSY_WORKER_HASH_MASK = BUSY_WORKER_HASH_SIZE - 1,
Tejun Heodb7bccf2010-06-29 10:07:12 +020080
Tejun Heoe22bee72010-06-29 10:07:14 +020081 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
82 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
83
Tejun Heo3233cdb2011-02-16 18:10:19 +010084 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
85 /* call for help after 10ms
86 (min two ticks) */
Tejun Heoe22bee72010-06-29 10:07:14 +020087 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
88 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heodb7bccf2010-06-29 10:07:12 +020089 TRUSTEE_COOLDOWN = HZ / 10, /* for trustee draining */
Tejun Heoe22bee72010-06-29 10:07:14 +020090
91 /*
92 * Rescue workers are used only on emergencies and shared by
93 * all cpus. Give -20.
94 */
95 RESCUER_NICE_LEVEL = -20,
Tejun Heo32704762012-07-13 22:16:45 -070096 HIGHPRI_NICE_LEVEL = -20,
Tejun Heoc8e55f32010-06-29 10:07:12 +020097};
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99/*
Tejun Heo4690c4a2010-06-29 10:07:10 +0200100 * Structure fields follow one of the following exclusion rules.
101 *
Tejun Heoe41e7042010-08-24 14:22:47 +0200102 * I: Modifiable by initialization/destruction paths and read-only for
103 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200104 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200105 * P: Preemption protected. Disabling preemption is enough and should
106 * only be modified and accessed from the local cpu.
107 *
Tejun Heo8b03ae32010-06-29 10:07:12 +0200108 * L: gcwq->lock protected. Access with gcwq->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200109 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200110 * X: During normal operation, modification requires gcwq->lock and
111 * should be done only from local cpu. Either disabling preemption
112 * on local cpu or grabbing gcwq->lock is enough for read access.
Tejun Heof3421792010-07-02 10:03:51 +0200113 * If GCWQ_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200114 *
Tejun Heo73f53c42010-06-29 10:07:11 +0200115 * F: wq->flush_mutex protected.
116 *
Tejun Heo4690c4a2010-06-29 10:07:10 +0200117 * W: workqueue_lock protected.
118 */
119
Tejun Heo8b03ae32010-06-29 10:07:12 +0200120struct global_cwq;
Tejun Heobd7bdd42012-07-12 14:46:37 -0700121struct worker_pool;
Tejun Heoc34056a2010-06-29 10:07:11 +0200122
Tejun Heoe22bee72010-06-29 10:07:14 +0200123/*
124 * The poor guys doing the actual heavy lifting. All on-duty workers
125 * are either serving the manager role, on idle list or on busy hash.
126 */
Tejun Heoc34056a2010-06-29 10:07:11 +0200127struct worker {
Tejun Heoc8e55f32010-06-29 10:07:12 +0200128 /* on idle list while idle, on busy hash table while busy */
129 union {
130 struct list_head entry; /* L: while idle */
131 struct hlist_node hentry; /* L: while busy */
132 };
133
Tejun Heoc34056a2010-06-29 10:07:11 +0200134 struct work_struct *current_work; /* L: work being processed */
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200135 struct cpu_workqueue_struct *current_cwq; /* L: current_work's cwq */
Tejun Heoaffee4b2010-06-29 10:07:12 +0200136 struct list_head scheduled; /* L: scheduled works */
Tejun Heoc34056a2010-06-29 10:07:11 +0200137 struct task_struct *task; /* I: worker task */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700138 struct worker_pool *pool; /* I: the associated pool */
Tejun Heoe22bee72010-06-29 10:07:14 +0200139 /* 64 bytes boundary on 64bit, 32 on 32bit */
140 unsigned long last_active; /* L: last active timestamp */
141 unsigned int flags; /* X: flags */
Tejun Heoc34056a2010-06-29 10:07:11 +0200142 int id; /* I: worker id */
Tejun Heoe22bee72010-06-29 10:07:14 +0200143 struct work_struct rebind_work; /* L: rebind worker to cpu */
Tejun Heoc34056a2010-06-29 10:07:11 +0200144};
145
Tejun Heobd7bdd42012-07-12 14:46:37 -0700146struct worker_pool {
147 struct global_cwq *gcwq; /* I: the owning gcwq */
Tejun Heo11ebea52012-07-12 14:46:37 -0700148 unsigned int flags; /* X: flags */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700149
150 struct list_head worklist; /* L: list of pending works */
151 int nr_workers; /* L: total number of workers */
152 int nr_idle; /* L: currently idle ones */
153
154 struct list_head idle_list; /* X: list of idle workers */
155 struct timer_list idle_timer; /* L: worker idle timeout */
156 struct timer_list mayday_timer; /* L: SOS timer for workers */
157
158 struct ida worker_ida; /* L: for worker IDs */
159 struct worker *first_idle; /* L: first idle worker */
160};
161
Tejun Heo4690c4a2010-06-29 10:07:10 +0200162/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200163 * Global per-cpu workqueue. There's one and only one for each cpu
164 * and all works are queued and processed here regardless of their
165 * target workqueues.
Tejun Heo8b03ae32010-06-29 10:07:12 +0200166 */
167struct global_cwq {
168 spinlock_t lock; /* the gcwq lock */
169 unsigned int cpu; /* I: the associated cpu */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200170 unsigned int flags; /* L: GCWQ_* flags */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200171
Tejun Heobd7bdd42012-07-12 14:46:37 -0700172 /* workers are chained either in busy_hash or pool idle_list */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200173 struct hlist_head busy_hash[BUSY_WORKER_HASH_SIZE];
174 /* L: hash of busy workers */
175
Tejun Heo32704762012-07-13 22:16:45 -0700176 struct worker_pool pools[2]; /* normal and highpri pools */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200177
178 struct task_struct *trustee; /* L: for gcwq shutdown */
179 unsigned int trustee_state; /* L: trustee state */
180 wait_queue_head_t trustee_wait; /* trustee wait */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200181} ____cacheline_aligned_in_smp;
182
183/*
Tejun Heo502ca9d2010-06-29 10:07:13 +0200184 * The per-CPU workqueue. The lower WORK_STRUCT_FLAG_BITS of
Tejun Heo0f900042010-06-29 10:07:11 +0200185 * work_struct->data are used for flags and thus cwqs need to be
186 * aligned at two's power of the number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 */
188struct cpu_workqueue_struct {
Tejun Heobd7bdd42012-07-12 14:46:37 -0700189 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200190 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200191 int work_color; /* L: current color */
192 int flush_color; /* L: flushing color */
193 int nr_in_flight[WORK_NR_COLORS];
194 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200195 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200196 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200197 struct list_head delayed_works; /* L: delayed works */
Tejun Heo0f900042010-06-29 10:07:11 +0200198};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200201 * Structure used to wait for workqueue flush.
202 */
203struct wq_flusher {
204 struct list_head list; /* F: list of flushers */
205 int flush_color; /* F: flush color waiting for */
206 struct completion done; /* flush completion */
207};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Tejun Heo73f53c42010-06-29 10:07:11 +0200209/*
Tejun Heof2e005a2010-07-20 15:59:09 +0200210 * All cpumasks are assumed to be always set on UP and thus can't be
211 * used to determine whether there's something to be done.
212 */
213#ifdef CONFIG_SMP
214typedef cpumask_var_t mayday_mask_t;
215#define mayday_test_and_set_cpu(cpu, mask) \
216 cpumask_test_and_set_cpu((cpu), (mask))
217#define mayday_clear_cpu(cpu, mask) cpumask_clear_cpu((cpu), (mask))
218#define for_each_mayday_cpu(cpu, mask) for_each_cpu((cpu), (mask))
Tejun Heo9c375472010-08-31 11:18:34 +0200219#define alloc_mayday_mask(maskp, gfp) zalloc_cpumask_var((maskp), (gfp))
Tejun Heof2e005a2010-07-20 15:59:09 +0200220#define free_mayday_mask(mask) free_cpumask_var((mask))
221#else
222typedef unsigned long mayday_mask_t;
223#define mayday_test_and_set_cpu(cpu, mask) test_and_set_bit(0, &(mask))
224#define mayday_clear_cpu(cpu, mask) clear_bit(0, &(mask))
225#define for_each_mayday_cpu(cpu, mask) if ((cpu) = 0, (mask))
226#define alloc_mayday_mask(maskp, gfp) true
227#define free_mayday_mask(mask) do { } while (0)
228#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230/*
231 * The externally visible workqueue abstraction is an array of
232 * per-CPU workqueues:
233 */
234struct workqueue_struct {
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200235 unsigned int flags; /* W: WQ_* flags */
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200236 union {
237 struct cpu_workqueue_struct __percpu *pcpu;
238 struct cpu_workqueue_struct *single;
239 unsigned long v;
240 } cpu_wq; /* I: cwq's */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200241 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200242
243 struct mutex flush_mutex; /* protects wq flushing */
244 int work_color; /* F: current work color */
245 int flush_color; /* F: current flush color */
246 atomic_t nr_cwqs_to_flush; /* flush in progress */
247 struct wq_flusher *first_flusher; /* F: first flusher */
248 struct list_head flusher_queue; /* F: flush waiters */
249 struct list_head flusher_overflow; /* F: flush overflow list */
250
Tejun Heof2e005a2010-07-20 15:59:09 +0200251 mayday_mask_t mayday_mask; /* cpus requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200252 struct worker *rescuer; /* I: rescue worker */
253
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200254 int nr_drainers; /* W: drain in progress */
Tejun Heodcd989c2010-06-29 10:07:14 +0200255 int saved_max_active; /* W: saved cwq max_active */
Johannes Berg4e6045f2007-10-18 23:39:55 -0700256#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200257 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700258#endif
Tejun Heob196be82012-01-10 15:11:35 -0800259 char name[]; /* I: workqueue name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260};
261
Tejun Heod320c032010-06-29 10:07:14 +0200262struct workqueue_struct *system_wq __read_mostly;
263struct workqueue_struct *system_long_wq __read_mostly;
264struct workqueue_struct *system_nrt_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200265struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100266struct workqueue_struct *system_freezable_wq __read_mostly;
Alan Stern62d3c542012-03-02 10:51:00 +0100267struct workqueue_struct *system_nrt_freezable_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200268EXPORT_SYMBOL_GPL(system_wq);
269EXPORT_SYMBOL_GPL(system_long_wq);
270EXPORT_SYMBOL_GPL(system_nrt_wq);
Tejun Heof3421792010-07-02 10:03:51 +0200271EXPORT_SYMBOL_GPL(system_unbound_wq);
Tejun Heo24d51ad2011-02-21 09:52:50 +0100272EXPORT_SYMBOL_GPL(system_freezable_wq);
Alan Stern62d3c542012-03-02 10:51:00 +0100273EXPORT_SYMBOL_GPL(system_nrt_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200274
Tejun Heo97bd2342010-10-05 10:41:14 +0200275#define CREATE_TRACE_POINTS
276#include <trace/events/workqueue.h>
277
Tejun Heo4ce62e92012-07-13 22:16:44 -0700278#define for_each_worker_pool(pool, gcwq) \
Tejun Heo32704762012-07-13 22:16:45 -0700279 for ((pool) = &(gcwq)->pools[0]; \
280 (pool) < &(gcwq)->pools[NR_WORKER_POOLS]; (pool)++)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700281
Tejun Heodb7bccf2010-06-29 10:07:12 +0200282#define for_each_busy_worker(worker, i, pos, gcwq) \
283 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \
284 hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry)
285
Tejun Heof3421792010-07-02 10:03:51 +0200286static inline int __next_gcwq_cpu(int cpu, const struct cpumask *mask,
287 unsigned int sw)
288{
289 if (cpu < nr_cpu_ids) {
290 if (sw & 1) {
291 cpu = cpumask_next(cpu, mask);
292 if (cpu < nr_cpu_ids)
293 return cpu;
294 }
295 if (sw & 2)
296 return WORK_CPU_UNBOUND;
297 }
298 return WORK_CPU_NONE;
299}
300
301static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
302 struct workqueue_struct *wq)
303{
304 return __next_gcwq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2);
305}
306
Tejun Heo09884952010-08-01 11:50:12 +0200307/*
308 * CPU iterators
309 *
310 * An extra gcwq is defined for an invalid cpu number
311 * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any
312 * specific CPU. The following iterators are similar to
313 * for_each_*_cpu() iterators but also considers the unbound gcwq.
314 *
315 * for_each_gcwq_cpu() : possible CPUs + WORK_CPU_UNBOUND
316 * for_each_online_gcwq_cpu() : online CPUs + WORK_CPU_UNBOUND
317 * for_each_cwq_cpu() : possible CPUs for bound workqueues,
318 * WORK_CPU_UNBOUND for unbound workqueues
319 */
Tejun Heof3421792010-07-02 10:03:51 +0200320#define for_each_gcwq_cpu(cpu) \
321 for ((cpu) = __next_gcwq_cpu(-1, cpu_possible_mask, 3); \
322 (cpu) < WORK_CPU_NONE; \
323 (cpu) = __next_gcwq_cpu((cpu), cpu_possible_mask, 3))
324
325#define for_each_online_gcwq_cpu(cpu) \
326 for ((cpu) = __next_gcwq_cpu(-1, cpu_online_mask, 3); \
327 (cpu) < WORK_CPU_NONE; \
328 (cpu) = __next_gcwq_cpu((cpu), cpu_online_mask, 3))
329
330#define for_each_cwq_cpu(cpu, wq) \
331 for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, (wq)); \
332 (cpu) < WORK_CPU_NONE; \
333 (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq)))
334
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900335#ifdef CONFIG_DEBUG_OBJECTS_WORK
336
337static struct debug_obj_descr work_debug_descr;
338
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100339static void *work_debug_hint(void *addr)
340{
341 return ((struct work_struct *) addr)->func;
342}
343
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900344/*
345 * fixup_init is called when:
346 * - an active object is initialized
347 */
348static int work_fixup_init(void *addr, enum debug_obj_state state)
349{
350 struct work_struct *work = addr;
351
352 switch (state) {
353 case ODEBUG_STATE_ACTIVE:
354 cancel_work_sync(work);
355 debug_object_init(work, &work_debug_descr);
356 return 1;
357 default:
358 return 0;
359 }
360}
361
362/*
363 * fixup_activate is called when:
364 * - an active object is activated
365 * - an unknown object is activated (might be a statically initialized object)
366 */
367static int work_fixup_activate(void *addr, enum debug_obj_state state)
368{
369 struct work_struct *work = addr;
370
371 switch (state) {
372
373 case ODEBUG_STATE_NOTAVAILABLE:
374 /*
375 * This is not really a fixup. The work struct was
376 * statically initialized. We just make sure that it
377 * is tracked in the object tracker.
378 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200379 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900380 debug_object_init(work, &work_debug_descr);
381 debug_object_activate(work, &work_debug_descr);
382 return 0;
383 }
384 WARN_ON_ONCE(1);
385 return 0;
386
387 case ODEBUG_STATE_ACTIVE:
388 WARN_ON(1);
389
390 default:
391 return 0;
392 }
393}
394
395/*
396 * fixup_free is called when:
397 * - an active object is freed
398 */
399static int work_fixup_free(void *addr, enum debug_obj_state state)
400{
401 struct work_struct *work = addr;
402
403 switch (state) {
404 case ODEBUG_STATE_ACTIVE:
405 cancel_work_sync(work);
406 debug_object_free(work, &work_debug_descr);
407 return 1;
408 default:
409 return 0;
410 }
411}
412
413static struct debug_obj_descr work_debug_descr = {
414 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100415 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900416 .fixup_init = work_fixup_init,
417 .fixup_activate = work_fixup_activate,
418 .fixup_free = work_fixup_free,
419};
420
421static inline void debug_work_activate(struct work_struct *work)
422{
423 debug_object_activate(work, &work_debug_descr);
424}
425
426static inline void debug_work_deactivate(struct work_struct *work)
427{
428 debug_object_deactivate(work, &work_debug_descr);
429}
430
431void __init_work(struct work_struct *work, int onstack)
432{
433 if (onstack)
434 debug_object_init_on_stack(work, &work_debug_descr);
435 else
436 debug_object_init(work, &work_debug_descr);
437}
438EXPORT_SYMBOL_GPL(__init_work);
439
440void destroy_work_on_stack(struct work_struct *work)
441{
442 debug_object_free(work, &work_debug_descr);
443}
444EXPORT_SYMBOL_GPL(destroy_work_on_stack);
445
446#else
447static inline void debug_work_activate(struct work_struct *work) { }
448static inline void debug_work_deactivate(struct work_struct *work) { }
449#endif
450
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100451/* Serializes the accesses to the list of workqueues. */
452static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453static LIST_HEAD(workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200454static bool workqueue_freezing; /* W: have wqs started freezing? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Oleg Nesterov14441962007-05-23 13:57:57 -0700456/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200457 * The almighty global cpu workqueues. nr_running is the only field
458 * which is expected to be used frequently by other cpus via
459 * try_to_wake_up(). Put it in a separate cacheline.
Oleg Nesterov14441962007-05-23 13:57:57 -0700460 */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200461static DEFINE_PER_CPU(struct global_cwq, global_cwq);
Tejun Heo4ce62e92012-07-13 22:16:44 -0700462static DEFINE_PER_CPU_SHARED_ALIGNED(atomic_t, pool_nr_running[NR_WORKER_POOLS]);
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800463
Tejun Heof3421792010-07-02 10:03:51 +0200464/*
465 * Global cpu workqueue and nr_running counter for unbound gcwq. The
466 * gcwq is always online, has GCWQ_DISASSOCIATED set, and all its
467 * workers have WORKER_UNBOUND set.
468 */
469static struct global_cwq unbound_global_cwq;
Tejun Heo4ce62e92012-07-13 22:16:44 -0700470static atomic_t unbound_pool_nr_running[NR_WORKER_POOLS] = {
471 [0 ... NR_WORKER_POOLS - 1] = ATOMIC_INIT(0), /* always 0 */
472};
Tejun Heof3421792010-07-02 10:03:51 +0200473
Tejun Heoc34056a2010-06-29 10:07:11 +0200474static int worker_thread(void *__worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Tejun Heo32704762012-07-13 22:16:45 -0700476static int worker_pool_pri(struct worker_pool *pool)
477{
478 return pool - pool->gcwq->pools;
479}
480
Tejun Heo8b03ae32010-06-29 10:07:12 +0200481static struct global_cwq *get_gcwq(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Tejun Heof3421792010-07-02 10:03:51 +0200483 if (cpu != WORK_CPU_UNBOUND)
484 return &per_cpu(global_cwq, cpu);
485 else
486 return &unbound_global_cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
Tejun Heo63d95a92012-07-12 14:46:37 -0700489static atomic_t *get_pool_nr_running(struct worker_pool *pool)
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700490{
Tejun Heo63d95a92012-07-12 14:46:37 -0700491 int cpu = pool->gcwq->cpu;
Tejun Heo32704762012-07-13 22:16:45 -0700492 int idx = worker_pool_pri(pool);
Tejun Heo63d95a92012-07-12 14:46:37 -0700493
Tejun Heof3421792010-07-02 10:03:51 +0200494 if (cpu != WORK_CPU_UNBOUND)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700495 return &per_cpu(pool_nr_running, cpu)[idx];
Tejun Heof3421792010-07-02 10:03:51 +0200496 else
Tejun Heo4ce62e92012-07-13 22:16:44 -0700497 return &unbound_pool_nr_running[idx];
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700498}
499
Tejun Heo4690c4a2010-06-29 10:07:10 +0200500static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
501 struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700502{
Tejun Heof3421792010-07-02 10:03:51 +0200503 if (!(wq->flags & WQ_UNBOUND)) {
Lai Jiangshane06ffa12012-03-09 18:03:20 +0800504 if (likely(cpu < nr_cpu_ids))
Tejun Heof3421792010-07-02 10:03:51 +0200505 return per_cpu_ptr(wq->cpu_wq.pcpu, cpu);
Tejun Heof3421792010-07-02 10:03:51 +0200506 } else if (likely(cpu == WORK_CPU_UNBOUND))
507 return wq->cpu_wq.single;
508 return NULL;
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700509}
510
Tejun Heo73f53c42010-06-29 10:07:11 +0200511static unsigned int work_color_to_flags(int color)
512{
513 return color << WORK_STRUCT_COLOR_SHIFT;
514}
515
516static int get_work_color(struct work_struct *work)
517{
518 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
519 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
520}
521
522static int work_next_color(int color)
523{
524 return (color + 1) % WORK_NR_COLORS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
David Howells4594bf12006-12-07 11:33:26 +0000527/*
Tejun Heoe1201532010-07-22 14:14:25 +0200528 * A work's data points to the cwq with WORK_STRUCT_CWQ set while the
529 * work is on queue. Once execution starts, WORK_STRUCT_CWQ is
530 * cleared and the work data contains the cpu number it was last on.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200531 *
532 * set_work_{cwq|cpu}() and clear_work_data() can be used to set the
533 * cwq, cpu or clear work->data. These functions should only be
534 * called while the work is owned - ie. while the PENDING bit is set.
535 *
536 * get_work_[g]cwq() can be used to obtain the gcwq or cwq
537 * corresponding to a work. gcwq is available once the work has been
538 * queued anywhere after initialization. cwq is available only from
539 * queueing until execution starts.
David Howells4594bf12006-12-07 11:33:26 +0000540 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200541static inline void set_work_data(struct work_struct *work, unsigned long data,
542 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000543{
David Howells4594bf12006-12-07 11:33:26 +0000544 BUG_ON(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200545 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000546}
David Howells365970a2006-11-22 14:54:49 +0000547
Tejun Heo7a22ad72010-06-29 10:07:13 +0200548static void set_work_cwq(struct work_struct *work,
549 struct cpu_workqueue_struct *cwq,
550 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200551{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200552 set_work_data(work, (unsigned long)cwq,
Tejun Heoe1201532010-07-22 14:14:25 +0200553 WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200554}
555
Tejun Heo7a22ad72010-06-29 10:07:13 +0200556static void set_work_cpu(struct work_struct *work, unsigned int cpu)
David Howells365970a2006-11-22 14:54:49 +0000557{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200558 set_work_data(work, cpu << WORK_STRUCT_FLAG_BITS, WORK_STRUCT_PENDING);
559}
560
561static void clear_work_data(struct work_struct *work)
562{
563 set_work_data(work, WORK_STRUCT_NO_CPU, 0);
564}
565
Tejun Heo7a22ad72010-06-29 10:07:13 +0200566static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work)
567{
Tejun Heoe1201532010-07-22 14:14:25 +0200568 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200569
Tejun Heoe1201532010-07-22 14:14:25 +0200570 if (data & WORK_STRUCT_CWQ)
571 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
572 else
573 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200574}
575
576static struct global_cwq *get_work_gcwq(struct work_struct *work)
577{
Tejun Heoe1201532010-07-22 14:14:25 +0200578 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200579 unsigned int cpu;
580
Tejun Heoe1201532010-07-22 14:14:25 +0200581 if (data & WORK_STRUCT_CWQ)
582 return ((struct cpu_workqueue_struct *)
Tejun Heobd7bdd42012-07-12 14:46:37 -0700583 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->gcwq;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200584
585 cpu = data >> WORK_STRUCT_FLAG_BITS;
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200586 if (cpu == WORK_CPU_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200587 return NULL;
588
Tejun Heof3421792010-07-02 10:03:51 +0200589 BUG_ON(cpu >= nr_cpu_ids && cpu != WORK_CPU_UNBOUND);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200590 return get_gcwq(cpu);
David Howells365970a2006-11-22 14:54:49 +0000591}
592
593/*
Tejun Heo32704762012-07-13 22:16:45 -0700594 * Policy functions. These define the policies on how the global worker
595 * pools are managed. Unless noted otherwise, these functions assume that
596 * they're being called with gcwq->lock held.
David Howells365970a2006-11-22 14:54:49 +0000597 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200598
Tejun Heo63d95a92012-07-12 14:46:37 -0700599static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000600{
Tejun Heo32704762012-07-13 22:16:45 -0700601 return !atomic_read(get_pool_nr_running(pool));
David Howells365970a2006-11-22 14:54:49 +0000602}
603
Tejun Heoe22bee72010-06-29 10:07:14 +0200604/*
605 * Need to wake up a worker? Called from anything but currently
606 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700607 *
608 * Note that, because unbound workers never contribute to nr_running, this
609 * function will always return %true for unbound gcwq as long as the
610 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200611 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700612static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000613{
Tejun Heo63d95a92012-07-12 14:46:37 -0700614 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000615}
616
Tejun Heoe22bee72010-06-29 10:07:14 +0200617/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700618static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200619{
Tejun Heo63d95a92012-07-12 14:46:37 -0700620 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200621}
622
623/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700624static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200625{
Tejun Heo63d95a92012-07-12 14:46:37 -0700626 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200627
Tejun Heo32704762012-07-13 22:16:45 -0700628 return !list_empty(&pool->worklist) && atomic_read(nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200629}
630
631/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700632static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200633{
Tejun Heo63d95a92012-07-12 14:46:37 -0700634 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200635}
636
637/* Do I need to be the manager? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700638static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200639{
Tejun Heo63d95a92012-07-12 14:46:37 -0700640 return need_to_create_worker(pool) ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700641 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200642}
643
644/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700645static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200646{
Tejun Heo11ebea52012-07-12 14:46:37 -0700647 bool managing = pool->flags & POOL_MANAGING_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -0700648 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
649 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200650
651 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
652}
653
654/*
655 * Wake up functions.
656 */
657
Tejun Heo7e116292010-06-29 10:07:13 +0200658/* Return the first worker. Safe with preemption disabled */
Tejun Heo63d95a92012-07-12 14:46:37 -0700659static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200660{
Tejun Heo63d95a92012-07-12 14:46:37 -0700661 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200662 return NULL;
663
Tejun Heo63d95a92012-07-12 14:46:37 -0700664 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200665}
666
667/**
668 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700669 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200670 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700671 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200672 *
673 * CONTEXT:
674 * spin_lock_irq(gcwq->lock).
675 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700676static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200677{
Tejun Heo63d95a92012-07-12 14:46:37 -0700678 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200679
680 if (likely(worker))
681 wake_up_process(worker->task);
682}
683
Tejun Heo4690c4a2010-06-29 10:07:10 +0200684/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200685 * wq_worker_waking_up - a worker is waking up
686 * @task: task waking up
687 * @cpu: CPU @task is waking up to
688 *
689 * This function is called during try_to_wake_up() when a worker is
690 * being awoken.
691 *
692 * CONTEXT:
693 * spin_lock_irq(rq->lock)
694 */
695void wq_worker_waking_up(struct task_struct *task, unsigned int cpu)
696{
697 struct worker *worker = kthread_data(task);
698
Steven Rostedt2d646722010-12-03 23:12:33 -0500699 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heo63d95a92012-07-12 14:46:37 -0700700 atomic_inc(get_pool_nr_running(worker->pool));
Tejun Heoe22bee72010-06-29 10:07:14 +0200701}
702
703/**
704 * wq_worker_sleeping - a worker is going to sleep
705 * @task: task going to sleep
706 * @cpu: CPU in question, must be the current CPU number
707 *
708 * This function is called during schedule() when a busy worker is
709 * going to sleep. Worker on the same cpu can be woken up by
710 * returning pointer to its task.
711 *
712 * CONTEXT:
713 * spin_lock_irq(rq->lock)
714 *
715 * RETURNS:
716 * Worker task on @cpu to wake up, %NULL if none.
717 */
718struct task_struct *wq_worker_sleeping(struct task_struct *task,
719 unsigned int cpu)
720{
721 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heobd7bdd42012-07-12 14:46:37 -0700722 struct worker_pool *pool = worker->pool;
Tejun Heo63d95a92012-07-12 14:46:37 -0700723 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200724
Steven Rostedt2d646722010-12-03 23:12:33 -0500725 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200726 return NULL;
727
728 /* this can only happen on the local cpu */
729 BUG_ON(cpu != raw_smp_processor_id());
730
731 /*
732 * The counterpart of the following dec_and_test, implied mb,
733 * worklist not empty test sequence is in insert_work().
734 * Please read comment there.
735 *
736 * NOT_RUNNING is clear. This means that trustee is not in
737 * charge and we're running on the local cpu w/ rq lock held
738 * and preemption disabled, which in turn means that none else
739 * could be manipulating idle_list, so dereferencing idle_list
740 * without gcwq lock is safe.
741 */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700742 if (atomic_dec_and_test(nr_running) && !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700743 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200744 return to_wakeup ? to_wakeup->task : NULL;
745}
746
747/**
748 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200749 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200750 * @flags: flags to set
751 * @wakeup: wakeup an idle worker if necessary
752 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200753 * Set @flags in @worker->flags and adjust nr_running accordingly. If
754 * nr_running becomes zero and @wakeup is %true, an idle worker is
755 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200756 *
Tejun Heocb444762010-07-02 10:03:50 +0200757 * CONTEXT:
758 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200759 */
760static inline void worker_set_flags(struct worker *worker, unsigned int flags,
761 bool wakeup)
762{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700763 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200764
Tejun Heocb444762010-07-02 10:03:50 +0200765 WARN_ON_ONCE(worker->task != current);
766
Tejun Heoe22bee72010-06-29 10:07:14 +0200767 /*
768 * If transitioning into NOT_RUNNING, adjust nr_running and
769 * wake up an idle worker as necessary if requested by
770 * @wakeup.
771 */
772 if ((flags & WORKER_NOT_RUNNING) &&
773 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heo63d95a92012-07-12 14:46:37 -0700774 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200775
776 if (wakeup) {
777 if (atomic_dec_and_test(nr_running) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -0700778 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700779 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200780 } else
781 atomic_dec(nr_running);
782 }
783
Tejun Heod302f012010-06-29 10:07:13 +0200784 worker->flags |= flags;
785}
786
787/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200788 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200789 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200790 * @flags: flags to clear
791 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200792 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200793 *
Tejun Heocb444762010-07-02 10:03:50 +0200794 * CONTEXT:
795 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200796 */
797static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
798{
Tejun Heo63d95a92012-07-12 14:46:37 -0700799 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200800 unsigned int oflags = worker->flags;
801
Tejun Heocb444762010-07-02 10:03:50 +0200802 WARN_ON_ONCE(worker->task != current);
803
Tejun Heod302f012010-06-29 10:07:13 +0200804 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200805
Tejun Heo42c025f2011-01-11 15:58:49 +0100806 /*
807 * If transitioning out of NOT_RUNNING, increment nr_running. Note
808 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
809 * of multiple flags, not a single flag.
810 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200811 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
812 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heo63d95a92012-07-12 14:46:37 -0700813 atomic_inc(get_pool_nr_running(pool));
Tejun Heod302f012010-06-29 10:07:13 +0200814}
815
816/**
Tejun Heoc8e55f32010-06-29 10:07:12 +0200817 * busy_worker_head - return the busy hash head for a work
818 * @gcwq: gcwq of interest
819 * @work: work to be hashed
820 *
821 * Return hash head of @gcwq for @work.
822 *
823 * CONTEXT:
824 * spin_lock_irq(gcwq->lock).
825 *
826 * RETURNS:
827 * Pointer to the hash head.
828 */
829static struct hlist_head *busy_worker_head(struct global_cwq *gcwq,
830 struct work_struct *work)
831{
832 const int base_shift = ilog2(sizeof(struct work_struct));
833 unsigned long v = (unsigned long)work;
834
835 /* simple shift and fold hash, do we need something better? */
836 v >>= base_shift;
837 v += v >> BUSY_WORKER_HASH_ORDER;
838 v &= BUSY_WORKER_HASH_MASK;
839
840 return &gcwq->busy_hash[v];
841}
842
843/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200844 * __find_worker_executing_work - find worker which is executing a work
845 * @gcwq: gcwq of interest
846 * @bwh: hash head as returned by busy_worker_head()
847 * @work: work to find worker for
848 *
849 * Find a worker which is executing @work on @gcwq. @bwh should be
850 * the hash head obtained by calling busy_worker_head() with the same
851 * work.
852 *
853 * CONTEXT:
854 * spin_lock_irq(gcwq->lock).
855 *
856 * RETURNS:
857 * Pointer to worker which is executing @work if found, NULL
858 * otherwise.
859 */
860static struct worker *__find_worker_executing_work(struct global_cwq *gcwq,
861 struct hlist_head *bwh,
862 struct work_struct *work)
863{
864 struct worker *worker;
865 struct hlist_node *tmp;
866
867 hlist_for_each_entry(worker, tmp, bwh, hentry)
868 if (worker->current_work == work)
869 return worker;
870 return NULL;
871}
872
873/**
874 * find_worker_executing_work - find worker which is executing a work
875 * @gcwq: gcwq of interest
876 * @work: work to find worker for
877 *
878 * Find a worker which is executing @work on @gcwq. This function is
879 * identical to __find_worker_executing_work() except that this
880 * function calculates @bwh itself.
881 *
882 * CONTEXT:
883 * spin_lock_irq(gcwq->lock).
884 *
885 * RETURNS:
886 * Pointer to worker which is executing @work if found, NULL
887 * otherwise.
888 */
889static struct worker *find_worker_executing_work(struct global_cwq *gcwq,
890 struct work_struct *work)
891{
892 return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work),
893 work);
894}
895
896/**
Tejun Heo7e116292010-06-29 10:07:13 +0200897 * insert_work - insert a work into gcwq
Tejun Heo4690c4a2010-06-29 10:07:10 +0200898 * @cwq: cwq @work belongs to
899 * @work: work to insert
900 * @head: insertion point
901 * @extra_flags: extra WORK_STRUCT_* flags to set
902 *
Tejun Heo7e116292010-06-29 10:07:13 +0200903 * Insert @work which belongs to @cwq into @gcwq after @head.
904 * @extra_flags is or'd to work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200905 *
906 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +0200907 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +0200908 */
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700909static void insert_work(struct cpu_workqueue_struct *cwq,
Tejun Heo4690c4a2010-06-29 10:07:10 +0200910 struct work_struct *work, struct list_head *head,
911 unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700912{
Tejun Heo63d95a92012-07-12 14:46:37 -0700913 struct worker_pool *pool = cwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100914
Tejun Heo4690c4a2010-06-29 10:07:10 +0200915 /* we own @work, set data and link */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200916 set_work_cwq(work, cwq, extra_flags);
Tejun Heo4690c4a2010-06-29 10:07:10 +0200917
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700918 /*
919 * Ensure that we get the right work->data if we see the
920 * result of list_add() below, see try_to_grab_pending().
921 */
922 smp_wmb();
Tejun Heo4690c4a2010-06-29 10:07:10 +0200923
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700924 list_add_tail(&work->entry, head);
Tejun Heoe22bee72010-06-29 10:07:14 +0200925
926 /*
927 * Ensure either worker_sched_deactivated() sees the above
928 * list_add_tail() or we see zero nr_running to avoid workers
929 * lying around lazily while there are works to be processed.
930 */
931 smp_mb();
932
Tejun Heo63d95a92012-07-12 14:46:37 -0700933 if (__need_more_worker(pool))
934 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700935}
936
Tejun Heoc8efcc22010-12-20 19:32:04 +0100937/*
938 * Test whether @work is being queued from another work executing on the
939 * same workqueue. This is rather expensive and should only be used from
940 * cold paths.
941 */
942static bool is_chained_work(struct workqueue_struct *wq)
943{
944 unsigned long flags;
945 unsigned int cpu;
946
947 for_each_gcwq_cpu(cpu) {
948 struct global_cwq *gcwq = get_gcwq(cpu);
949 struct worker *worker;
950 struct hlist_node *pos;
951 int i;
952
953 spin_lock_irqsave(&gcwq->lock, flags);
954 for_each_busy_worker(worker, i, pos, gcwq) {
955 if (worker->task != current)
956 continue;
957 spin_unlock_irqrestore(&gcwq->lock, flags);
958 /*
959 * I'm @worker, no locking necessary. See if @work
960 * is headed to the same workqueue.
961 */
962 return worker->current_cwq->wq == wq;
963 }
964 spin_unlock_irqrestore(&gcwq->lock, flags);
965 }
966 return false;
967}
968
Tejun Heo4690c4a2010-06-29 10:07:10 +0200969static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 struct work_struct *work)
971{
Tejun Heo502ca9d2010-06-29 10:07:13 +0200972 struct global_cwq *gcwq;
973 struct cpu_workqueue_struct *cwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200974 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +0200975 unsigned int work_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 unsigned long flags;
977
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900978 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200979
Tejun Heoc8efcc22010-12-20 19:32:04 +0100980 /* if dying, only works from the same workqueue are allowed */
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200981 if (unlikely(wq->flags & WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +0100982 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +0200983 return;
984
Tejun Heoc7fc77f2010-07-02 10:03:51 +0200985 /* determine gcwq to use */
986 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +0200987 struct global_cwq *last_gcwq;
988
Tejun Heoc7fc77f2010-07-02 10:03:51 +0200989 if (unlikely(cpu == WORK_CPU_UNBOUND))
990 cpu = raw_smp_processor_id();
991
Tejun Heo18aa9ef2010-06-29 10:07:13 +0200992 /*
993 * It's multi cpu. If @wq is non-reentrant and @work
994 * was previously on a different cpu, it might still
995 * be running there, in which case the work needs to
996 * be queued on that cpu to guarantee non-reentrance.
997 */
Tejun Heo502ca9d2010-06-29 10:07:13 +0200998 gcwq = get_gcwq(cpu);
Tejun Heo18aa9ef2010-06-29 10:07:13 +0200999 if (wq->flags & WQ_NON_REENTRANT &&
1000 (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) {
1001 struct worker *worker;
1002
1003 spin_lock_irqsave(&last_gcwq->lock, flags);
1004
1005 worker = find_worker_executing_work(last_gcwq, work);
1006
1007 if (worker && worker->current_cwq->wq == wq)
1008 gcwq = last_gcwq;
1009 else {
1010 /* meh... not running there, queue here */
1011 spin_unlock_irqrestore(&last_gcwq->lock, flags);
1012 spin_lock_irqsave(&gcwq->lock, flags);
1013 }
1014 } else
1015 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heof3421792010-07-02 10:03:51 +02001016 } else {
1017 gcwq = get_gcwq(WORK_CPU_UNBOUND);
1018 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001019 }
1020
1021 /* gcwq determined, get cwq and queue */
1022 cwq = get_cwq(gcwq->cpu, wq);
Tejun Heocdadf002010-10-05 10:49:55 +02001023 trace_workqueue_queue_work(cpu, cwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001024
Dan Carpenterf5b25522012-04-13 22:06:58 +03001025 if (WARN_ON(!list_empty(&work->entry))) {
1026 spin_unlock_irqrestore(&gcwq->lock, flags);
1027 return;
1028 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001029
Tejun Heo73f53c42010-06-29 10:07:11 +02001030 cwq->nr_in_flight[cwq->work_color]++;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001031 work_flags = work_color_to_flags(cwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001032
1033 if (likely(cwq->nr_active < cwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001034 trace_workqueue_activate_work(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001035 cwq->nr_active++;
Tejun Heo32704762012-07-13 22:16:45 -07001036 worklist = &cwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001037 } else {
1038 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001039 worklist = &cwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001040 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001041
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001042 insert_work(cwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001043
Tejun Heo8b03ae32010-06-29 10:07:12 +02001044 spin_unlock_irqrestore(&gcwq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
1046
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001047/**
1048 * queue_work - queue work on a workqueue
1049 * @wq: workqueue to use
1050 * @work: work to queue
1051 *
Alan Stern057647f2006-10-28 10:38:58 -07001052 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 *
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07001054 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1055 * it can be processed by another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001057int queue_work(struct workqueue_struct *wq, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001059 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001061 ret = queue_work_on(get_cpu(), wq, work);
1062 put_cpu();
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return ret;
1065}
Dave Jonesae90dd52006-06-30 01:40:45 -04001066EXPORT_SYMBOL_GPL(queue_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Zhang Ruic1a220e2008-07-23 21:28:39 -07001068/**
1069 * queue_work_on - queue work on specific cpu
1070 * @cpu: CPU number to execute work on
1071 * @wq: workqueue to use
1072 * @work: work to queue
1073 *
1074 * Returns 0 if @work was already on a queue, non-zero otherwise.
1075 *
1076 * We queue the work to a specific CPU, the caller must ensure it
1077 * can't go away.
1078 */
1079int
1080queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
1081{
1082 int ret = 0;
1083
Tejun Heo22df02b2010-06-29 10:07:10 +02001084 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001085 __queue_work(cpu, wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001086 ret = 1;
1087 }
1088 return ret;
1089}
1090EXPORT_SYMBOL_GPL(queue_work_on);
1091
Li Zefan6d141c32008-02-08 04:21:09 -08001092static void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
David Howells52bad642006-11-22 14:54:01 +00001094 struct delayed_work *dwork = (struct delayed_work *)__data;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001095 struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Tejun Heo4690c4a2010-06-29 10:07:10 +02001097 __queue_work(smp_processor_id(), cwq->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098}
1099
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001100/**
1101 * queue_delayed_work - queue work on a workqueue after delay
1102 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001103 * @dwork: delayable work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001104 * @delay: number of jiffies to wait before queueing
1105 *
Alan Stern057647f2006-10-28 10:38:58 -07001106 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001107 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001108int queue_delayed_work(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001109 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
David Howells52bad642006-11-22 14:54:01 +00001111 if (delay == 0)
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001112 return queue_work(wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001114 return queue_delayed_work_on(-1, wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115}
Dave Jonesae90dd52006-06-30 01:40:45 -04001116EXPORT_SYMBOL_GPL(queue_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001118/**
1119 * queue_delayed_work_on - queue work on specific CPU after delay
1120 * @cpu: CPU number to execute work on
1121 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001122 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001123 * @delay: number of jiffies to wait before queueing
1124 *
Alan Stern057647f2006-10-28 10:38:58 -07001125 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001126 */
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001127int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001128 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001129{
1130 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +00001131 struct timer_list *timer = &dwork->timer;
1132 struct work_struct *work = &dwork->work;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001133
Tejun Heo22df02b2010-06-29 10:07:10 +02001134 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001135 unsigned int lcpu;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001136
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001137 BUG_ON(timer_pending(timer));
1138 BUG_ON(!list_empty(&work->entry));
1139
Andrew Liu8a3e77c2008-05-01 04:35:14 -07001140 timer_stats_timer_set_start_info(&dwork->timer);
1141
Tejun Heo7a22ad72010-06-29 10:07:13 +02001142 /*
1143 * This stores cwq for the moment, for the timer_fn.
1144 * Note that the work's gcwq is preserved to allow
1145 * reentrance detection for delayed works.
1146 */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001147 if (!(wq->flags & WQ_UNBOUND)) {
1148 struct global_cwq *gcwq = get_work_gcwq(work);
1149
1150 if (gcwq && gcwq->cpu != WORK_CPU_UNBOUND)
1151 lcpu = gcwq->cpu;
1152 else
1153 lcpu = raw_smp_processor_id();
1154 } else
1155 lcpu = WORK_CPU_UNBOUND;
1156
Tejun Heo7a22ad72010-06-29 10:07:13 +02001157 set_work_cwq(work, get_cwq(lcpu, wq), 0);
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001158
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001159 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +00001160 timer->data = (unsigned long)dwork;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001161 timer->function = delayed_work_timer_fn;
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001162
1163 if (unlikely(cpu >= 0))
1164 add_timer_on(timer, cpu);
1165 else
1166 add_timer(timer);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001167 ret = 1;
1168 }
1169 return ret;
1170}
Dave Jonesae90dd52006-06-30 01:40:45 -04001171EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Tejun Heoc8e55f32010-06-29 10:07:12 +02001173/**
1174 * worker_enter_idle - enter idle state
1175 * @worker: worker which is entering idle state
1176 *
1177 * @worker is entering idle state. Update stats and idle timer if
1178 * necessary.
1179 *
1180 * LOCKING:
1181 * spin_lock_irq(gcwq->lock).
1182 */
1183static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001185 struct worker_pool *pool = worker->pool;
1186 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Tejun Heoc8e55f32010-06-29 10:07:12 +02001188 BUG_ON(worker->flags & WORKER_IDLE);
1189 BUG_ON(!list_empty(&worker->entry) &&
1190 (worker->hentry.next || worker->hentry.pprev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Tejun Heocb444762010-07-02 10:03:50 +02001192 /* can't use worker_set_flags(), also called from start_worker() */
1193 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001194 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001195 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001196
Tejun Heoc8e55f32010-06-29 10:07:12 +02001197 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001198 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001199
Tejun Heo403c8212012-07-17 12:39:27 -07001200 if (likely(gcwq->trustee_state != TRUSTEE_DONE)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001201 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
Tejun Heobd7bdd42012-07-12 14:46:37 -07001202 mod_timer(&pool->idle_timer,
Tejun Heoe22bee72010-06-29 10:07:14 +02001203 jiffies + IDLE_WORKER_TIMEOUT);
1204 } else
Tejun Heodb7bccf2010-06-29 10:07:12 +02001205 wake_up_all(&gcwq->trustee_wait);
Tejun Heocb444762010-07-02 10:03:50 +02001206
Tejun Heo544ecf32012-05-14 15:04:50 -07001207 /*
1208 * Sanity check nr_running. Because trustee releases gcwq->lock
Tejun Heo403c8212012-07-17 12:39:27 -07001209 * between setting %WORKER_UNBOUND and zapping nr_running, the
Tejun Heo544ecf32012-05-14 15:04:50 -07001210 * warning may trigger spuriously. Check iff trustee is idle.
1211 */
1212 WARN_ON_ONCE(gcwq->trustee_state == TRUSTEE_DONE &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001213 pool->nr_workers == pool->nr_idle &&
Tejun Heo63d95a92012-07-12 14:46:37 -07001214 atomic_read(get_pool_nr_running(pool)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215}
1216
Tejun Heoc8e55f32010-06-29 10:07:12 +02001217/**
1218 * worker_leave_idle - leave idle state
1219 * @worker: worker which is leaving idle state
1220 *
1221 * @worker is leaving idle state. Update stats.
1222 *
1223 * LOCKING:
1224 * spin_lock_irq(gcwq->lock).
1225 */
1226static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001228 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Tejun Heoc8e55f32010-06-29 10:07:12 +02001230 BUG_ON(!(worker->flags & WORKER_IDLE));
Tejun Heod302f012010-06-29 10:07:13 +02001231 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001232 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001233 list_del_init(&worker->entry);
1234}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Tejun Heoe22bee72010-06-29 10:07:14 +02001236/**
1237 * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq
1238 * @worker: self
1239 *
1240 * Works which are scheduled while the cpu is online must at least be
1241 * scheduled to a worker which is bound to the cpu so that if they are
1242 * flushed from cpu callbacks while cpu is going down, they are
1243 * guaranteed to execute on the cpu.
1244 *
1245 * This function is to be used by rogue workers and rescuers to bind
1246 * themselves to the target cpu and may race with cpu going down or
1247 * coming online. kthread_bind() can't be used because it may put the
1248 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1249 * verbatim as it's best effort and blocking and gcwq may be
1250 * [dis]associated in the meantime.
1251 *
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001252 * This function tries set_cpus_allowed() and locks gcwq and verifies the
1253 * binding against %GCWQ_DISASSOCIATED which is set during
1254 * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1255 * enters idle state or fetches works without dropping lock, it can
1256 * guarantee the scheduling requirement described in the first paragraph.
Tejun Heoe22bee72010-06-29 10:07:14 +02001257 *
1258 * CONTEXT:
1259 * Might sleep. Called without any lock but returns with gcwq->lock
1260 * held.
1261 *
1262 * RETURNS:
1263 * %true if the associated gcwq is online (@worker is successfully
1264 * bound), %false if offline.
1265 */
1266static bool worker_maybe_bind_and_lock(struct worker *worker)
Namhyung Kim972fa1c2010-08-22 23:19:43 +09001267__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001268{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001269 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001270 struct task_struct *task = worker->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Tejun Heoe22bee72010-06-29 10:07:14 +02001272 while (true) {
1273 /*
1274 * The following call may fail, succeed or succeed
1275 * without actually migrating the task to the cpu if
1276 * it races with cpu hotunplug operation. Verify
1277 * against GCWQ_DISASSOCIATED.
1278 */
Tejun Heof3421792010-07-02 10:03:51 +02001279 if (!(gcwq->flags & GCWQ_DISASSOCIATED))
1280 set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu));
Oleg Nesterov85f41862007-05-09 02:34:20 -07001281
Tejun Heoe22bee72010-06-29 10:07:14 +02001282 spin_lock_irq(&gcwq->lock);
1283 if (gcwq->flags & GCWQ_DISASSOCIATED)
1284 return false;
1285 if (task_cpu(task) == gcwq->cpu &&
1286 cpumask_equal(&current->cpus_allowed,
1287 get_cpu_mask(gcwq->cpu)))
1288 return true;
1289 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001290
Tejun Heo5035b202011-04-29 18:08:37 +02001291 /*
1292 * We've raced with CPU hot[un]plug. Give it a breather
1293 * and retry migration. cond_resched() is required here;
1294 * otherwise, we might deadlock against cpu_stop trying to
1295 * bring down the CPU on non-preemptive kernel.
1296 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001297 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001298 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001299 }
1300}
1301
1302/*
Tejun Heo403c8212012-07-17 12:39:27 -07001303 * Function for worker->rebind_work used to rebind unbound busy workers to
1304 * the associated cpu which is coming back online. This is scheduled by
1305 * cpu up but can race with other cpu hotplug operations and may be
1306 * executed twice without intervening cpu down.
Tejun Heoe22bee72010-06-29 10:07:14 +02001307 */
1308static void worker_rebind_fn(struct work_struct *work)
1309{
1310 struct worker *worker = container_of(work, struct worker, rebind_work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001311 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001312
1313 if (worker_maybe_bind_and_lock(worker))
1314 worker_clr_flags(worker, WORKER_REBIND);
1315
1316 spin_unlock_irq(&gcwq->lock);
1317}
1318
Tejun Heoc34056a2010-06-29 10:07:11 +02001319static struct worker *alloc_worker(void)
1320{
1321 struct worker *worker;
1322
1323 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001324 if (worker) {
1325 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001326 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heoe22bee72010-06-29 10:07:14 +02001327 INIT_WORK(&worker->rebind_work, worker_rebind_fn);
1328 /* on creation a worker is in !idle && prep state */
1329 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001330 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001331 return worker;
1332}
1333
1334/**
1335 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001336 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001337 * @bind: whether to set affinity to @cpu or not
1338 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001339 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001340 * can be started by calling start_worker() or destroyed using
1341 * destroy_worker().
1342 *
1343 * CONTEXT:
1344 * Might sleep. Does GFP_KERNEL allocations.
1345 *
1346 * RETURNS:
1347 * Pointer to the newly created worker.
1348 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001349static struct worker *create_worker(struct worker_pool *pool, bool bind)
Tejun Heoc34056a2010-06-29 10:07:11 +02001350{
Tejun Heo63d95a92012-07-12 14:46:37 -07001351 struct global_cwq *gcwq = pool->gcwq;
Tejun Heof3421792010-07-02 10:03:51 +02001352 bool on_unbound_cpu = gcwq->cpu == WORK_CPU_UNBOUND;
Tejun Heo32704762012-07-13 22:16:45 -07001353 const char *pri = worker_pool_pri(pool) ? "H" : "";
Tejun Heoc34056a2010-06-29 10:07:11 +02001354 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001355 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001356
Tejun Heo8b03ae32010-06-29 10:07:12 +02001357 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001358 while (ida_get_new(&pool->worker_ida, &id)) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001359 spin_unlock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001360 if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001361 goto fail;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001362 spin_lock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001363 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02001364 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001365
1366 worker = alloc_worker();
1367 if (!worker)
1368 goto fail;
1369
Tejun Heobd7bdd42012-07-12 14:46:37 -07001370 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001371 worker->id = id;
1372
Tejun Heof3421792010-07-02 10:03:51 +02001373 if (!on_unbound_cpu)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001374 worker->task = kthread_create_on_node(worker_thread,
Tejun Heo32704762012-07-13 22:16:45 -07001375 worker, cpu_to_node(gcwq->cpu),
1376 "kworker/%u:%d%s", gcwq->cpu, id, pri);
Tejun Heof3421792010-07-02 10:03:51 +02001377 else
1378 worker->task = kthread_create(worker_thread, worker,
Tejun Heo32704762012-07-13 22:16:45 -07001379 "kworker/u:%d%s", id, pri);
Tejun Heoc34056a2010-06-29 10:07:11 +02001380 if (IS_ERR(worker->task))
1381 goto fail;
1382
Tejun Heo32704762012-07-13 22:16:45 -07001383 if (worker_pool_pri(pool))
1384 set_user_nice(worker->task, HIGHPRI_NICE_LEVEL);
1385
Tejun Heodb7bccf2010-06-29 10:07:12 +02001386 /*
Tejun Heo403c8212012-07-17 12:39:27 -07001387 * An unbound worker will become a regular one if CPU comes online
1388 * later on. Make sure every worker has PF_THREAD_BOUND set.
Tejun Heodb7bccf2010-06-29 10:07:12 +02001389 */
Tejun Heof3421792010-07-02 10:03:51 +02001390 if (bind && !on_unbound_cpu)
Tejun Heo8b03ae32010-06-29 10:07:12 +02001391 kthread_bind(worker->task, gcwq->cpu);
Tejun Heof3421792010-07-02 10:03:51 +02001392 else {
Tejun Heodb7bccf2010-06-29 10:07:12 +02001393 worker->task->flags |= PF_THREAD_BOUND;
Tejun Heof3421792010-07-02 10:03:51 +02001394 if (on_unbound_cpu)
1395 worker->flags |= WORKER_UNBOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001397
Tejun Heoc34056a2010-06-29 10:07:11 +02001398 return worker;
1399fail:
1400 if (id >= 0) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001401 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001402 ida_remove(&pool->worker_ida, id);
Tejun Heo8b03ae32010-06-29 10:07:12 +02001403 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001404 }
1405 kfree(worker);
1406 return NULL;
1407}
1408
1409/**
1410 * start_worker - start a newly created worker
1411 * @worker: worker to start
1412 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001413 * Make the gcwq aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001414 *
1415 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001416 * spin_lock_irq(gcwq->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001417 */
1418static void start_worker(struct worker *worker)
1419{
Tejun Heocb444762010-07-02 10:03:50 +02001420 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001421 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001422 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001423 wake_up_process(worker->task);
1424}
1425
1426/**
1427 * destroy_worker - destroy a workqueue worker
1428 * @worker: worker to be destroyed
1429 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001430 * Destroy @worker and adjust @gcwq stats accordingly.
1431 *
1432 * CONTEXT:
1433 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001434 */
1435static void destroy_worker(struct worker *worker)
1436{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001437 struct worker_pool *pool = worker->pool;
1438 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001439 int id = worker->id;
1440
1441 /* sanity check frenzy */
1442 BUG_ON(worker->current_work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001443 BUG_ON(!list_empty(&worker->scheduled));
Tejun Heoc34056a2010-06-29 10:07:11 +02001444
Tejun Heoc8e55f32010-06-29 10:07:12 +02001445 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001446 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001447 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001448 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001449
1450 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001451 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001452
1453 spin_unlock_irq(&gcwq->lock);
1454
Tejun Heoc34056a2010-06-29 10:07:11 +02001455 kthread_stop(worker->task);
1456 kfree(worker);
1457
Tejun Heo8b03ae32010-06-29 10:07:12 +02001458 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001459 ida_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001460}
1461
Tejun Heo63d95a92012-07-12 14:46:37 -07001462static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001463{
Tejun Heo63d95a92012-07-12 14:46:37 -07001464 struct worker_pool *pool = (void *)__pool;
1465 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001466
1467 spin_lock_irq(&gcwq->lock);
1468
Tejun Heo63d95a92012-07-12 14:46:37 -07001469 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001470 struct worker *worker;
1471 unsigned long expires;
1472
1473 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001474 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001475 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1476
1477 if (time_before(jiffies, expires))
Tejun Heo63d95a92012-07-12 14:46:37 -07001478 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001479 else {
1480 /* it's been idle for too long, wake up manager */
Tejun Heo11ebea52012-07-12 14:46:37 -07001481 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -07001482 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001483 }
1484 }
1485
1486 spin_unlock_irq(&gcwq->lock);
1487}
1488
1489static bool send_mayday(struct work_struct *work)
1490{
1491 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1492 struct workqueue_struct *wq = cwq->wq;
Tejun Heof3421792010-07-02 10:03:51 +02001493 unsigned int cpu;
Tejun Heoe22bee72010-06-29 10:07:14 +02001494
1495 if (!(wq->flags & WQ_RESCUER))
1496 return false;
1497
1498 /* mayday mayday mayday */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001499 cpu = cwq->pool->gcwq->cpu;
Tejun Heof3421792010-07-02 10:03:51 +02001500 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1501 if (cpu == WORK_CPU_UNBOUND)
1502 cpu = 0;
Tejun Heof2e005a2010-07-20 15:59:09 +02001503 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001504 wake_up_process(wq->rescuer->task);
1505 return true;
1506}
1507
Tejun Heo63d95a92012-07-12 14:46:37 -07001508static void gcwq_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001509{
Tejun Heo63d95a92012-07-12 14:46:37 -07001510 struct worker_pool *pool = (void *)__pool;
1511 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001512 struct work_struct *work;
1513
1514 spin_lock_irq(&gcwq->lock);
1515
Tejun Heo63d95a92012-07-12 14:46:37 -07001516 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001517 /*
1518 * We've been trying to create a new worker but
1519 * haven't been successful. We might be hitting an
1520 * allocation deadlock. Send distress signals to
1521 * rescuers.
1522 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001523 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001524 send_mayday(work);
1525 }
1526
1527 spin_unlock_irq(&gcwq->lock);
1528
Tejun Heo63d95a92012-07-12 14:46:37 -07001529 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001530}
1531
1532/**
1533 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001534 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001535 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001536 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001537 * have at least one idle worker on return from this function. If
1538 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001539 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001540 * possible allocation deadlock.
1541 *
1542 * On return, need_to_create_worker() is guaranteed to be false and
1543 * may_start_working() true.
1544 *
1545 * LOCKING:
1546 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1547 * multiple times. Does GFP_KERNEL allocations. Called only from
1548 * manager.
1549 *
1550 * RETURNS:
1551 * false if no action was taken and gcwq->lock stayed locked, true
1552 * otherwise.
1553 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001554static bool maybe_create_worker(struct worker_pool *pool)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001555__releases(&gcwq->lock)
1556__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001557{
Tejun Heo63d95a92012-07-12 14:46:37 -07001558 struct global_cwq *gcwq = pool->gcwq;
1559
1560 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001561 return false;
1562restart:
Tejun Heo9f9c2362010-07-14 11:31:20 +02001563 spin_unlock_irq(&gcwq->lock);
1564
Tejun Heoe22bee72010-06-29 10:07:14 +02001565 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07001566 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001567
1568 while (true) {
1569 struct worker *worker;
1570
Tejun Heo63d95a92012-07-12 14:46:37 -07001571 worker = create_worker(pool, true);
Tejun Heoe22bee72010-06-29 10:07:14 +02001572 if (worker) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001573 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001574 spin_lock_irq(&gcwq->lock);
1575 start_worker(worker);
Tejun Heo63d95a92012-07-12 14:46:37 -07001576 BUG_ON(need_to_create_worker(pool));
Tejun Heoe22bee72010-06-29 10:07:14 +02001577 return true;
1578 }
1579
Tejun Heo63d95a92012-07-12 14:46:37 -07001580 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001581 break;
1582
Tejun Heoe22bee72010-06-29 10:07:14 +02001583 __set_current_state(TASK_INTERRUPTIBLE);
1584 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001585
Tejun Heo63d95a92012-07-12 14:46:37 -07001586 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001587 break;
1588 }
1589
Tejun Heo63d95a92012-07-12 14:46:37 -07001590 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001591 spin_lock_irq(&gcwq->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07001592 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001593 goto restart;
1594 return true;
1595}
1596
1597/**
1598 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo63d95a92012-07-12 14:46:37 -07001599 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001600 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001601 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02001602 * IDLE_WORKER_TIMEOUT.
1603 *
1604 * LOCKING:
1605 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1606 * multiple times. Called only from manager.
1607 *
1608 * RETURNS:
1609 * false if no action was taken and gcwq->lock stayed locked, true
1610 * otherwise.
1611 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001612static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001613{
1614 bool ret = false;
1615
Tejun Heo63d95a92012-07-12 14:46:37 -07001616 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001617 struct worker *worker;
1618 unsigned long expires;
1619
Tejun Heo63d95a92012-07-12 14:46:37 -07001620 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001621 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1622
1623 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001624 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001625 break;
1626 }
1627
1628 destroy_worker(worker);
1629 ret = true;
1630 }
1631
1632 return ret;
1633}
1634
1635/**
1636 * manage_workers - manage worker pool
1637 * @worker: self
1638 *
1639 * Assume the manager role and manage gcwq worker pool @worker belongs
1640 * to. At any given time, there can be only zero or one manager per
1641 * gcwq. The exclusion is handled automatically by this function.
1642 *
1643 * The caller can safely start processing works on false return. On
1644 * true return, it's guaranteed that need_to_create_worker() is false
1645 * and may_start_working() is true.
1646 *
1647 * CONTEXT:
1648 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1649 * multiple times. Does GFP_KERNEL allocations.
1650 *
1651 * RETURNS:
1652 * false if no action was taken and gcwq->lock stayed locked, true if
1653 * some action was taken.
1654 */
1655static bool manage_workers(struct worker *worker)
1656{
Tejun Heo63d95a92012-07-12 14:46:37 -07001657 struct worker_pool *pool = worker->pool;
1658 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001659 bool ret = false;
1660
Tejun Heo11ebea52012-07-12 14:46:37 -07001661 if (pool->flags & POOL_MANAGING_WORKERS)
Tejun Heoe22bee72010-06-29 10:07:14 +02001662 return ret;
1663
Tejun Heo11ebea52012-07-12 14:46:37 -07001664 pool->flags &= ~POOL_MANAGE_WORKERS;
1665 pool->flags |= POOL_MANAGING_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02001666
1667 /*
1668 * Destroy and then create so that may_start_working() is true
1669 * on return.
1670 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001671 ret |= maybe_destroy_workers(pool);
1672 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001673
Tejun Heo11ebea52012-07-12 14:46:37 -07001674 pool->flags &= ~POOL_MANAGING_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02001675
1676 /*
1677 * The trustee might be waiting to take over the manager
1678 * position, tell it we're done.
1679 */
1680 if (unlikely(gcwq->trustee))
1681 wake_up_all(&gcwq->trustee_wait);
1682
1683 return ret;
1684}
1685
Tejun Heoa62428c2010-06-29 10:07:10 +02001686/**
Tejun Heoaffee4b2010-06-29 10:07:12 +02001687 * move_linked_works - move linked works to a list
1688 * @work: start of series of works to be scheduled
1689 * @head: target list to append @work to
1690 * @nextp: out paramter for nested worklist walking
1691 *
1692 * Schedule linked works starting from @work to @head. Work series to
1693 * be scheduled starts at @work and includes any consecutive work with
1694 * WORK_STRUCT_LINKED set in its predecessor.
1695 *
1696 * If @nextp is not NULL, it's updated to point to the next work of
1697 * the last scheduled work. This allows move_linked_works() to be
1698 * nested inside outer list_for_each_entry_safe().
1699 *
1700 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001701 * spin_lock_irq(gcwq->lock).
Tejun Heoaffee4b2010-06-29 10:07:12 +02001702 */
1703static void move_linked_works(struct work_struct *work, struct list_head *head,
1704 struct work_struct **nextp)
1705{
1706 struct work_struct *n;
1707
1708 /*
1709 * Linked worklist will always end before the end of the list,
1710 * use NULL for list head.
1711 */
1712 list_for_each_entry_safe_from(work, n, NULL, entry) {
1713 list_move_tail(&work->entry, head);
1714 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1715 break;
1716 }
1717
1718 /*
1719 * If we're already inside safe list traversal and have moved
1720 * multiple works to the scheduled queue, the next position
1721 * needs to be updated.
1722 */
1723 if (nextp)
1724 *nextp = n;
1725}
1726
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001727static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
1728{
1729 struct work_struct *work = list_first_entry(&cwq->delayed_works,
1730 struct work_struct, entry);
1731
Tejun Heocdadf002010-10-05 10:49:55 +02001732 trace_workqueue_activate_work(work);
Tejun Heo32704762012-07-13 22:16:45 -07001733 move_linked_works(work, &cwq->pool->worklist, NULL);
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001734 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001735 cwq->nr_active++;
1736}
1737
Tejun Heoaffee4b2010-06-29 10:07:12 +02001738/**
Tejun Heo73f53c42010-06-29 10:07:11 +02001739 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
1740 * @cwq: cwq of interest
1741 * @color: color of work which left the queue
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001742 * @delayed: for a delayed work
Tejun Heo73f53c42010-06-29 10:07:11 +02001743 *
1744 * A work either has completed or is removed from pending queue,
1745 * decrement nr_in_flight of its cwq and handle workqueue flushing.
1746 *
1747 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001748 * spin_lock_irq(gcwq->lock).
Tejun Heo73f53c42010-06-29 10:07:11 +02001749 */
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001750static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color,
1751 bool delayed)
Tejun Heo73f53c42010-06-29 10:07:11 +02001752{
1753 /* ignore uncolored works */
1754 if (color == WORK_NO_COLOR)
1755 return;
1756
1757 cwq->nr_in_flight[color]--;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001758
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001759 if (!delayed) {
1760 cwq->nr_active--;
1761 if (!list_empty(&cwq->delayed_works)) {
1762 /* one down, submit a delayed one */
1763 if (cwq->nr_active < cwq->max_active)
1764 cwq_activate_first_delayed(cwq);
1765 }
Tejun Heo502ca9d2010-06-29 10:07:13 +02001766 }
Tejun Heo73f53c42010-06-29 10:07:11 +02001767
1768 /* is flush in progress and are we at the flushing tip? */
1769 if (likely(cwq->flush_color != color))
1770 return;
1771
1772 /* are there still in-flight works? */
1773 if (cwq->nr_in_flight[color])
1774 return;
1775
1776 /* this cwq is done, clear flush_color */
1777 cwq->flush_color = -1;
1778
1779 /*
1780 * If this was the last cwq, wake up the first flusher. It
1781 * will handle the rest.
1782 */
1783 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
1784 complete(&cwq->wq->first_flusher->done);
1785}
1786
1787/**
Tejun Heoa62428c2010-06-29 10:07:10 +02001788 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02001789 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02001790 * @work: work to process
1791 *
1792 * Process @work. This function contains all the logics necessary to
1793 * process a single work including synchronization against and
1794 * interaction with other workers on the same cpu, queueing and
1795 * flushing. As long as context requirement is met, any worker can
1796 * call this function to process a work.
1797 *
1798 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001799 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02001800 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001801static void process_one_work(struct worker *worker, struct work_struct *work)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001802__releases(&gcwq->lock)
1803__acquires(&gcwq->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02001804{
Tejun Heo7e116292010-06-29 10:07:13 +02001805 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001806 struct worker_pool *pool = worker->pool;
1807 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001808 struct hlist_head *bwh = busy_worker_head(gcwq, work);
Tejun Heofb0e7be2010-06-29 10:07:15 +02001809 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heoa62428c2010-06-29 10:07:10 +02001810 work_func_t f = work->func;
Tejun Heo73f53c42010-06-29 10:07:11 +02001811 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02001812 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02001813#ifdef CONFIG_LOCKDEP
1814 /*
1815 * It is permissible to free the struct work_struct from
1816 * inside the function that is called from it, this we need to
1817 * take into account for lockdep too. To avoid bogus "held
1818 * lock freed" warnings as well as problems when looking into
1819 * work->lockdep_map, make a copy and use that here.
1820 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07001821 struct lockdep_map lockdep_map;
1822
1823 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02001824#endif
Tejun Heo7e116292010-06-29 10:07:13 +02001825 /*
1826 * A single work shouldn't be executed concurrently by
1827 * multiple workers on a single cpu. Check whether anyone is
1828 * already processing the work. If so, defer the work to the
1829 * currently executing one.
1830 */
1831 collision = __find_worker_executing_work(gcwq, bwh, work);
1832 if (unlikely(collision)) {
1833 move_linked_works(work, &collision->scheduled, NULL);
1834 return;
1835 }
1836
Tejun Heoa62428c2010-06-29 10:07:10 +02001837 /* claim and process */
Tejun Heoa62428c2010-06-29 10:07:10 +02001838 debug_work_deactivate(work);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001839 hlist_add_head(&worker->hentry, bwh);
Tejun Heoc34056a2010-06-29 10:07:11 +02001840 worker->current_work = work;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001841 worker->current_cwq = cwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02001842 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02001843
Tejun Heo7a22ad72010-06-29 10:07:13 +02001844 /* record the current cpu number in the work data and dequeue */
1845 set_work_cpu(work, gcwq->cpu);
Tejun Heoa62428c2010-06-29 10:07:10 +02001846 list_del_init(&work->entry);
1847
Tejun Heo649027d2010-06-29 10:07:14 +02001848 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02001849 * CPU intensive works don't participate in concurrency
1850 * management. They're the scheduler's responsibility.
1851 */
1852 if (unlikely(cpu_intensive))
1853 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
1854
Tejun Heo974271c2012-07-12 14:46:37 -07001855 /*
1856 * Unbound gcwq isn't concurrency managed and work items should be
1857 * executed ASAP. Wake up another worker if necessary.
1858 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001859 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
1860 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07001861
Tejun Heo8b03ae32010-06-29 10:07:12 +02001862 spin_unlock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001863
Tejun Heoa62428c2010-06-29 10:07:10 +02001864 work_clear_pending(work);
Tejun Heoe1594892011-01-09 23:32:15 +01001865 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02001866 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001867 trace_workqueue_execute_start(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02001868 f(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001869 /*
1870 * While we must be careful to not use "work" after this, the trace
1871 * point will only record its address.
1872 */
1873 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02001874 lock_map_release(&lockdep_map);
1875 lock_map_release(&cwq->wq->lockdep_map);
1876
1877 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
1878 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
1879 "%s/0x%08x/%d\n",
1880 current->comm, preempt_count(), task_pid_nr(current));
1881 printk(KERN_ERR " last function: ");
1882 print_symbol("%s\n", (unsigned long)f);
1883 debug_show_held_locks(current);
1884 dump_stack();
1885 }
1886
Tejun Heo8b03ae32010-06-29 10:07:12 +02001887 spin_lock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001888
Tejun Heofb0e7be2010-06-29 10:07:15 +02001889 /* clear cpu intensive status */
1890 if (unlikely(cpu_intensive))
1891 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
1892
Tejun Heoa62428c2010-06-29 10:07:10 +02001893 /* we're done with it, release */
Tejun Heoc8e55f32010-06-29 10:07:12 +02001894 hlist_del_init(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001895 worker->current_work = NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001896 worker->current_cwq = NULL;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001897 cwq_dec_nr_in_flight(cwq, work_color, false);
Tejun Heoa62428c2010-06-29 10:07:10 +02001898}
1899
Tejun Heoaffee4b2010-06-29 10:07:12 +02001900/**
1901 * process_scheduled_works - process scheduled works
1902 * @worker: self
1903 *
1904 * Process all scheduled works. Please note that the scheduled list
1905 * may change while processing a work, so this function repeatedly
1906 * fetches a work from the top and executes it.
1907 *
1908 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001909 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02001910 * multiple times.
1911 */
1912static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
Tejun Heoaffee4b2010-06-29 10:07:12 +02001914 while (!list_empty(&worker->scheduled)) {
1915 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001917 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919}
1920
Tejun Heo4690c4a2010-06-29 10:07:10 +02001921/**
1922 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02001923 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02001924 *
Tejun Heoe22bee72010-06-29 10:07:14 +02001925 * The gcwq worker thread function. There's a single dynamic pool of
1926 * these per each cpu. These workers process all works regardless of
1927 * their specific target workqueue. The only exception is works which
1928 * belong to workqueues with a rescuer which will be explained in
1929 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02001930 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001931static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
Tejun Heoc34056a2010-06-29 10:07:11 +02001933 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001934 struct worker_pool *pool = worker->pool;
1935 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
Tejun Heoe22bee72010-06-29 10:07:14 +02001937 /* tell the scheduler that this is a workqueue worker */
1938 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001939woke_up:
Tejun Heoc8e55f32010-06-29 10:07:12 +02001940 spin_lock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Tejun Heoc8e55f32010-06-29 10:07:12 +02001942 /* DIE can be set only while we're idle, checking here is enough */
1943 if (worker->flags & WORKER_DIE) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001944 spin_unlock_irq(&gcwq->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001945 worker->task->flags &= ~PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001946 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 }
1948
Tejun Heoc8e55f32010-06-29 10:07:12 +02001949 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001950recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02001951 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07001952 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001953 goto sleep;
1954
1955 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07001956 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02001957 goto recheck;
1958
Tejun Heoc8e55f32010-06-29 10:07:12 +02001959 /*
1960 * ->scheduled list can only be filled while a worker is
1961 * preparing to process a work or actually processing it.
1962 * Make sure nobody diddled with it while I was sleeping.
1963 */
1964 BUG_ON(!list_empty(&worker->scheduled));
1965
Tejun Heoe22bee72010-06-29 10:07:14 +02001966 /*
1967 * When control reaches this point, we're guaranteed to have
1968 * at least one idle worker or that someone else has already
1969 * assumed the manager role.
1970 */
1971 worker_clr_flags(worker, WORKER_PREP);
1972
1973 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02001974 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07001975 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02001976 struct work_struct, entry);
1977
1978 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
1979 /* optimization path, not strictly necessary */
1980 process_one_work(worker, work);
1981 if (unlikely(!list_empty(&worker->scheduled)))
1982 process_scheduled_works(worker);
1983 } else {
1984 move_linked_works(work, &worker->scheduled, NULL);
1985 process_scheduled_works(worker);
1986 }
Tejun Heo63d95a92012-07-12 14:46:37 -07001987 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02001988
Tejun Heoe22bee72010-06-29 10:07:14 +02001989 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02001990sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07001991 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02001992 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02001993
Tejun Heoc8e55f32010-06-29 10:07:12 +02001994 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02001995 * gcwq->lock is held and there's no work to process and no
1996 * need to manage, sleep. Workers are woken up only while
1997 * holding gcwq->lock or from local cpu, so setting the
1998 * current state before releasing gcwq->lock is enough to
1999 * prevent losing any event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002000 */
2001 worker_enter_idle(worker);
2002 __set_current_state(TASK_INTERRUPTIBLE);
2003 spin_unlock_irq(&gcwq->lock);
2004 schedule();
2005 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006}
2007
Tejun Heoe22bee72010-06-29 10:07:14 +02002008/**
2009 * rescuer_thread - the rescuer thread function
2010 * @__wq: the associated workqueue
2011 *
2012 * Workqueue rescuer thread function. There's one rescuer for each
2013 * workqueue which has WQ_RESCUER set.
2014 *
2015 * Regular work processing on a gcwq may block trying to create a new
2016 * worker which uses GFP_KERNEL allocation which has slight chance of
2017 * developing into deadlock if some works currently on the same queue
2018 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2019 * the problem rescuer solves.
2020 *
2021 * When such condition is possible, the gcwq summons rescuers of all
2022 * workqueues which have works queued on the gcwq and let them process
2023 * those works so that forward progress can be guaranteed.
2024 *
2025 * This should happen rarely.
2026 */
2027static int rescuer_thread(void *__wq)
2028{
2029 struct workqueue_struct *wq = __wq;
2030 struct worker *rescuer = wq->rescuer;
2031 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heof3421792010-07-02 10:03:51 +02002032 bool is_unbound = wq->flags & WQ_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02002033 unsigned int cpu;
2034
2035 set_user_nice(current, RESCUER_NICE_LEVEL);
2036repeat:
2037 set_current_state(TASK_INTERRUPTIBLE);
2038
2039 if (kthread_should_stop())
2040 return 0;
2041
Tejun Heof3421792010-07-02 10:03:51 +02002042 /*
2043 * See whether any cpu is asking for help. Unbounded
2044 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
2045 */
Tejun Heof2e005a2010-07-20 15:59:09 +02002046 for_each_mayday_cpu(cpu, wq->mayday_mask) {
Tejun Heof3421792010-07-02 10:03:51 +02002047 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2048 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002049 struct worker_pool *pool = cwq->pool;
2050 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002051 struct work_struct *work, *n;
2052
2053 __set_current_state(TASK_RUNNING);
Tejun Heof2e005a2010-07-20 15:59:09 +02002054 mayday_clear_cpu(cpu, wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002055
2056 /* migrate to the target cpu if possible */
Tejun Heobd7bdd42012-07-12 14:46:37 -07002057 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002058 worker_maybe_bind_and_lock(rescuer);
2059
2060 /*
2061 * Slurp in all works issued via this workqueue and
2062 * process'em.
2063 */
2064 BUG_ON(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002065 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02002066 if (get_work_cwq(work) == cwq)
2067 move_linked_works(work, scheduled, &n);
2068
2069 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002070
2071 /*
2072 * Leave this gcwq. If keep_working() is %true, notify a
2073 * regular worker; otherwise, we end up with 0 concurrency
2074 * and stalling the execution.
2075 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002076 if (keep_working(pool))
2077 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002078
Tejun Heoe22bee72010-06-29 10:07:14 +02002079 spin_unlock_irq(&gcwq->lock);
2080 }
2081
2082 schedule();
2083 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084}
2085
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002086struct wq_barrier {
2087 struct work_struct work;
2088 struct completion done;
2089};
2090
2091static void wq_barrier_func(struct work_struct *work)
2092{
2093 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2094 complete(&barr->done);
2095}
2096
Tejun Heo4690c4a2010-06-29 10:07:10 +02002097/**
2098 * insert_wq_barrier - insert a barrier work
2099 * @cwq: cwq to insert barrier into
2100 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002101 * @target: target work to attach @barr to
2102 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002103 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002104 * @barr is linked to @target such that @barr is completed only after
2105 * @target finishes execution. Please note that the ordering
2106 * guarantee is observed only with respect to @target and on the local
2107 * cpu.
2108 *
2109 * Currently, a queued barrier can't be canceled. This is because
2110 * try_to_grab_pending() can't determine whether the work to be
2111 * grabbed is at the head of the queue and thus can't clear LINKED
2112 * flag of the previous work while there must be a valid next work
2113 * after a work with LINKED flag set.
2114 *
2115 * Note that when @worker is non-NULL, @target may be modified
2116 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002117 *
2118 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002119 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002120 */
Oleg Nesterov83c22522007-05-09 02:33:54 -07002121static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002122 struct wq_barrier *barr,
2123 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002124{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002125 struct list_head *head;
2126 unsigned int linked = 0;
2127
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002128 /*
Tejun Heo8b03ae32010-06-29 10:07:12 +02002129 * debugobject calls are safe here even with gcwq->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002130 * as we know for sure that this will not trigger any of the
2131 * checks and call back into the fixup functions where we
2132 * might deadlock.
2133 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002134 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002135 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002136 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002137
Tejun Heoaffee4b2010-06-29 10:07:12 +02002138 /*
2139 * If @target is currently being executed, schedule the
2140 * barrier to the worker; otherwise, put it after @target.
2141 */
2142 if (worker)
2143 head = worker->scheduled.next;
2144 else {
2145 unsigned long *bits = work_data_bits(target);
2146
2147 head = target->entry.next;
2148 /* there can already be other linked works, inherit and set */
2149 linked = *bits & WORK_STRUCT_LINKED;
2150 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2151 }
2152
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002153 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002154 insert_work(cwq, &barr->work, head,
2155 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002156}
2157
Tejun Heo73f53c42010-06-29 10:07:11 +02002158/**
2159 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2160 * @wq: workqueue being flushed
2161 * @flush_color: new flush color, < 0 for no-op
2162 * @work_color: new work color, < 0 for no-op
2163 *
2164 * Prepare cwqs for workqueue flushing.
2165 *
2166 * If @flush_color is non-negative, flush_color on all cwqs should be
2167 * -1. If no cwq has in-flight commands at the specified color, all
2168 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2169 * has in flight commands, its cwq->flush_color is set to
2170 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2171 * wakeup logic is armed and %true is returned.
2172 *
2173 * The caller should have initialized @wq->first_flusher prior to
2174 * calling this function with non-negative @flush_color. If
2175 * @flush_color is negative, no flush color update is done and %false
2176 * is returned.
2177 *
2178 * If @work_color is non-negative, all cwqs should have the same
2179 * work_color which is previous to @work_color and all will be
2180 * advanced to @work_color.
2181 *
2182 * CONTEXT:
2183 * mutex_lock(wq->flush_mutex).
2184 *
2185 * RETURNS:
2186 * %true if @flush_color >= 0 and there's something to flush. %false
2187 * otherwise.
2188 */
2189static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2190 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191{
Tejun Heo73f53c42010-06-29 10:07:11 +02002192 bool wait = false;
2193 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -07002194
Tejun Heo73f53c42010-06-29 10:07:11 +02002195 if (flush_color >= 0) {
2196 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2197 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002198 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002199
Tejun Heof3421792010-07-02 10:03:51 +02002200 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002201 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002202 struct global_cwq *gcwq = cwq->pool->gcwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002203
Tejun Heo8b03ae32010-06-29 10:07:12 +02002204 spin_lock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002205
2206 if (flush_color >= 0) {
2207 BUG_ON(cwq->flush_color != -1);
2208
2209 if (cwq->nr_in_flight[flush_color]) {
2210 cwq->flush_color = flush_color;
2211 atomic_inc(&wq->nr_cwqs_to_flush);
2212 wait = true;
2213 }
2214 }
2215
2216 if (work_color >= 0) {
2217 BUG_ON(work_color != work_next_color(cwq->work_color));
2218 cwq->work_color = work_color;
2219 }
2220
Tejun Heo8b03ae32010-06-29 10:07:12 +02002221 spin_unlock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002222 }
2223
2224 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2225 complete(&wq->first_flusher->done);
2226
2227 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228}
2229
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002230/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002232 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 *
2234 * Forces execution of the workqueue and blocks until its completion.
2235 * This is typically used in driver shutdown handlers.
2236 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002237 * We sleep until all works which were queued on entry have been handled,
2238 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002240void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241{
Tejun Heo73f53c42010-06-29 10:07:11 +02002242 struct wq_flusher this_flusher = {
2243 .list = LIST_HEAD_INIT(this_flusher.list),
2244 .flush_color = -1,
2245 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2246 };
2247 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002248
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002249 lock_map_acquire(&wq->lockdep_map);
2250 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002251
2252 mutex_lock(&wq->flush_mutex);
2253
2254 /*
2255 * Start-to-wait phase
2256 */
2257 next_color = work_next_color(wq->work_color);
2258
2259 if (next_color != wq->flush_color) {
2260 /*
2261 * Color space is not full. The current work_color
2262 * becomes our flush_color and work_color is advanced
2263 * by one.
2264 */
2265 BUG_ON(!list_empty(&wq->flusher_overflow));
2266 this_flusher.flush_color = wq->work_color;
2267 wq->work_color = next_color;
2268
2269 if (!wq->first_flusher) {
2270 /* no flush in progress, become the first flusher */
2271 BUG_ON(wq->flush_color != this_flusher.flush_color);
2272
2273 wq->first_flusher = &this_flusher;
2274
2275 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2276 wq->work_color)) {
2277 /* nothing to flush, done */
2278 wq->flush_color = next_color;
2279 wq->first_flusher = NULL;
2280 goto out_unlock;
2281 }
2282 } else {
2283 /* wait in queue */
2284 BUG_ON(wq->flush_color == this_flusher.flush_color);
2285 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2286 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2287 }
2288 } else {
2289 /*
2290 * Oops, color space is full, wait on overflow queue.
2291 * The next flush completion will assign us
2292 * flush_color and transfer to flusher_queue.
2293 */
2294 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2295 }
2296
2297 mutex_unlock(&wq->flush_mutex);
2298
2299 wait_for_completion(&this_flusher.done);
2300
2301 /*
2302 * Wake-up-and-cascade phase
2303 *
2304 * First flushers are responsible for cascading flushes and
2305 * handling overflow. Non-first flushers can simply return.
2306 */
2307 if (wq->first_flusher != &this_flusher)
2308 return;
2309
2310 mutex_lock(&wq->flush_mutex);
2311
Tejun Heo4ce48b32010-07-02 10:03:51 +02002312 /* we might have raced, check again with mutex held */
2313 if (wq->first_flusher != &this_flusher)
2314 goto out_unlock;
2315
Tejun Heo73f53c42010-06-29 10:07:11 +02002316 wq->first_flusher = NULL;
2317
2318 BUG_ON(!list_empty(&this_flusher.list));
2319 BUG_ON(wq->flush_color != this_flusher.flush_color);
2320
2321 while (true) {
2322 struct wq_flusher *next, *tmp;
2323
2324 /* complete all the flushers sharing the current flush color */
2325 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2326 if (next->flush_color != wq->flush_color)
2327 break;
2328 list_del_init(&next->list);
2329 complete(&next->done);
2330 }
2331
2332 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2333 wq->flush_color != work_next_color(wq->work_color));
2334
2335 /* this flush_color is finished, advance by one */
2336 wq->flush_color = work_next_color(wq->flush_color);
2337
2338 /* one color has been freed, handle overflow queue */
2339 if (!list_empty(&wq->flusher_overflow)) {
2340 /*
2341 * Assign the same color to all overflowed
2342 * flushers, advance work_color and append to
2343 * flusher_queue. This is the start-to-wait
2344 * phase for these overflowed flushers.
2345 */
2346 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2347 tmp->flush_color = wq->work_color;
2348
2349 wq->work_color = work_next_color(wq->work_color);
2350
2351 list_splice_tail_init(&wq->flusher_overflow,
2352 &wq->flusher_queue);
2353 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2354 }
2355
2356 if (list_empty(&wq->flusher_queue)) {
2357 BUG_ON(wq->flush_color != wq->work_color);
2358 break;
2359 }
2360
2361 /*
2362 * Need to flush more colors. Make the next flusher
2363 * the new first flusher and arm cwqs.
2364 */
2365 BUG_ON(wq->flush_color == wq->work_color);
2366 BUG_ON(wq->flush_color != next->flush_color);
2367
2368 list_del_init(&next->list);
2369 wq->first_flusher = next;
2370
2371 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2372 break;
2373
2374 /*
2375 * Meh... this color is already done, clear first
2376 * flusher and repeat cascading.
2377 */
2378 wq->first_flusher = NULL;
2379 }
2380
2381out_unlock:
2382 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383}
Dave Jonesae90dd52006-06-30 01:40:45 -04002384EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002386/**
2387 * drain_workqueue - drain a workqueue
2388 * @wq: workqueue to drain
2389 *
2390 * Wait until the workqueue becomes empty. While draining is in progress,
2391 * only chain queueing is allowed. IOW, only currently pending or running
2392 * work items on @wq can queue further work items on it. @wq is flushed
2393 * repeatedly until it becomes empty. The number of flushing is detemined
2394 * by the depth of chaining and should be relatively short. Whine if it
2395 * takes too long.
2396 */
2397void drain_workqueue(struct workqueue_struct *wq)
2398{
2399 unsigned int flush_cnt = 0;
2400 unsigned int cpu;
2401
2402 /*
2403 * __queue_work() needs to test whether there are drainers, is much
2404 * hotter than drain_workqueue() and already looks at @wq->flags.
2405 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2406 */
2407 spin_lock(&workqueue_lock);
2408 if (!wq->nr_drainers++)
2409 wq->flags |= WQ_DRAINING;
2410 spin_unlock(&workqueue_lock);
2411reflush:
2412 flush_workqueue(wq);
2413
2414 for_each_cwq_cpu(cpu, wq) {
2415 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002416 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002417
Tejun Heobd7bdd42012-07-12 14:46:37 -07002418 spin_lock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002419 drained = !cwq->nr_active && list_empty(&cwq->delayed_works);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002420 spin_unlock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002421
2422 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002423 continue;
2424
2425 if (++flush_cnt == 10 ||
2426 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
2427 pr_warning("workqueue %s: flush on destruction isn't complete after %u tries\n",
2428 wq->name, flush_cnt);
2429 goto reflush;
2430 }
2431
2432 spin_lock(&workqueue_lock);
2433 if (!--wq->nr_drainers)
2434 wq->flags &= ~WQ_DRAINING;
2435 spin_unlock(&workqueue_lock);
2436}
2437EXPORT_SYMBOL_GPL(drain_workqueue);
2438
Tejun Heobaf59022010-09-16 10:42:16 +02002439static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
2440 bool wait_executing)
2441{
2442 struct worker *worker = NULL;
2443 struct global_cwq *gcwq;
2444 struct cpu_workqueue_struct *cwq;
2445
2446 might_sleep();
2447 gcwq = get_work_gcwq(work);
2448 if (!gcwq)
2449 return false;
2450
2451 spin_lock_irq(&gcwq->lock);
2452 if (!list_empty(&work->entry)) {
2453 /*
2454 * See the comment near try_to_grab_pending()->smp_rmb().
2455 * If it was re-queued to a different gcwq under us, we
2456 * are not going to wait.
2457 */
2458 smp_rmb();
2459 cwq = get_work_cwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002460 if (unlikely(!cwq || gcwq != cwq->pool->gcwq))
Tejun Heobaf59022010-09-16 10:42:16 +02002461 goto already_gone;
2462 } else if (wait_executing) {
2463 worker = find_worker_executing_work(gcwq, work);
2464 if (!worker)
2465 goto already_gone;
2466 cwq = worker->current_cwq;
2467 } else
2468 goto already_gone;
2469
2470 insert_wq_barrier(cwq, barr, work, worker);
2471 spin_unlock_irq(&gcwq->lock);
2472
Tejun Heoe1594892011-01-09 23:32:15 +01002473 /*
2474 * If @max_active is 1 or rescuer is in use, flushing another work
2475 * item on the same workqueue may lead to deadlock. Make sure the
2476 * flusher is not running on the same workqueue by verifying write
2477 * access.
2478 */
2479 if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER)
2480 lock_map_acquire(&cwq->wq->lockdep_map);
2481 else
2482 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heobaf59022010-09-16 10:42:16 +02002483 lock_map_release(&cwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002484
Tejun Heobaf59022010-09-16 10:42:16 +02002485 return true;
2486already_gone:
2487 spin_unlock_irq(&gcwq->lock);
2488 return false;
2489}
2490
Oleg Nesterovdb700892008-07-25 01:47:49 -07002491/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002492 * flush_work - wait for a work to finish executing the last queueing instance
2493 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002494 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002495 * Wait until @work has finished execution. This function considers
2496 * only the last queueing instance of @work. If @work has been
2497 * enqueued across different CPUs on a non-reentrant workqueue or on
2498 * multiple workqueues, @work might still be executing on return on
2499 * some of the CPUs from earlier queueing.
Oleg Nesterova67da702008-07-25 01:47:52 -07002500 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002501 * If @work was queued only on a non-reentrant, ordered or unbound
2502 * workqueue, @work is guaranteed to be idle on return if it hasn't
2503 * been requeued since flush started.
2504 *
2505 * RETURNS:
2506 * %true if flush_work() waited for the work to finish execution,
2507 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002508 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002509bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002510{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002511 struct wq_barrier barr;
2512
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002513 lock_map_acquire(&work->lockdep_map);
2514 lock_map_release(&work->lockdep_map);
2515
Tejun Heobaf59022010-09-16 10:42:16 +02002516 if (start_flush_work(work, &barr, true)) {
2517 wait_for_completion(&barr.done);
2518 destroy_work_on_stack(&barr.work);
2519 return true;
2520 } else
2521 return false;
Oleg Nesterovdb700892008-07-25 01:47:49 -07002522}
2523EXPORT_SYMBOL_GPL(flush_work);
2524
Tejun Heo401a8d02010-09-16 10:36:00 +02002525static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work)
2526{
2527 struct wq_barrier barr;
2528 struct worker *worker;
2529
2530 spin_lock_irq(&gcwq->lock);
2531
2532 worker = find_worker_executing_work(gcwq, work);
2533 if (unlikely(worker))
2534 insert_wq_barrier(worker->current_cwq, &barr, work, worker);
2535
2536 spin_unlock_irq(&gcwq->lock);
2537
2538 if (unlikely(worker)) {
2539 wait_for_completion(&barr.done);
2540 destroy_work_on_stack(&barr.work);
2541 return true;
2542 } else
2543 return false;
2544}
2545
2546static bool wait_on_work(struct work_struct *work)
2547{
2548 bool ret = false;
2549 int cpu;
2550
2551 might_sleep();
2552
2553 lock_map_acquire(&work->lockdep_map);
2554 lock_map_release(&work->lockdep_map);
2555
2556 for_each_gcwq_cpu(cpu)
2557 ret |= wait_on_cpu_work(get_gcwq(cpu), work);
2558 return ret;
2559}
2560
Tejun Heo09383492010-09-16 10:48:29 +02002561/**
2562 * flush_work_sync - wait until a work has finished execution
2563 * @work: the work to flush
2564 *
2565 * Wait until @work has finished execution. On return, it's
2566 * guaranteed that all queueing instances of @work which happened
2567 * before this function is called are finished. In other words, if
2568 * @work hasn't been requeued since this function was called, @work is
2569 * guaranteed to be idle on return.
2570 *
2571 * RETURNS:
2572 * %true if flush_work_sync() waited for the work to finish execution,
2573 * %false if it was already idle.
2574 */
2575bool flush_work_sync(struct work_struct *work)
2576{
2577 struct wq_barrier barr;
2578 bool pending, waited;
2579
2580 /* we'll wait for executions separately, queue barr only if pending */
2581 pending = start_flush_work(work, &barr, false);
2582
2583 /* wait for executions to finish */
2584 waited = wait_on_work(work);
2585
2586 /* wait for the pending one */
2587 if (pending) {
2588 wait_for_completion(&barr.done);
2589 destroy_work_on_stack(&barr.work);
2590 }
2591
2592 return pending || waited;
2593}
2594EXPORT_SYMBOL_GPL(flush_work_sync);
2595
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002596/*
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002597 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002598 * so this work can't be re-armed in any way.
2599 */
2600static int try_to_grab_pending(struct work_struct *work)
2601{
Tejun Heo8b03ae32010-06-29 10:07:12 +02002602 struct global_cwq *gcwq;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002603 int ret = -1;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002604
Tejun Heo22df02b2010-06-29 10:07:10 +02002605 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002606 return 0;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002607
2608 /*
2609 * The queueing is in progress, or it is already queued. Try to
2610 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
2611 */
Tejun Heo7a22ad72010-06-29 10:07:13 +02002612 gcwq = get_work_gcwq(work);
2613 if (!gcwq)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002614 return ret;
2615
Tejun Heo8b03ae32010-06-29 10:07:12 +02002616 spin_lock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002617 if (!list_empty(&work->entry)) {
2618 /*
Tejun Heo7a22ad72010-06-29 10:07:13 +02002619 * This work is queued, but perhaps we locked the wrong gcwq.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002620 * In that case we must see the new value after rmb(), see
2621 * insert_work()->wmb().
2622 */
2623 smp_rmb();
Tejun Heo7a22ad72010-06-29 10:07:13 +02002624 if (gcwq == get_work_gcwq(work)) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002625 debug_work_deactivate(work);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002626 list_del_init(&work->entry);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002627 cwq_dec_nr_in_flight(get_work_cwq(work),
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02002628 get_work_color(work),
2629 *work_data_bits(work) & WORK_STRUCT_DELAYED);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002630 ret = 1;
2631 }
2632 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02002633 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002634
2635 return ret;
2636}
2637
Tejun Heo401a8d02010-09-16 10:36:00 +02002638static bool __cancel_work_timer(struct work_struct *work,
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002639 struct timer_list* timer)
2640{
2641 int ret;
2642
2643 do {
2644 ret = (timer && likely(del_timer(timer)));
2645 if (!ret)
2646 ret = try_to_grab_pending(work);
2647 wait_on_work(work);
2648 } while (unlikely(ret < 0));
2649
Tejun Heo7a22ad72010-06-29 10:07:13 +02002650 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002651 return ret;
2652}
2653
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002654/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002655 * cancel_work_sync - cancel a work and wait for it to finish
2656 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002657 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002658 * Cancel @work and wait for its execution to finish. This function
2659 * can be used even if the work re-queues itself or migrates to
2660 * another workqueue. On return from this function, @work is
2661 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002662 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002663 * cancel_work_sync(&delayed_work->work) must not be used for
2664 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002665 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002666 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002667 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002668 *
2669 * RETURNS:
2670 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002671 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002672bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002673{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002674 return __cancel_work_timer(work, NULL);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002675}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002676EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002677
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002678/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002679 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2680 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002681 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002682 * Delayed timer is cancelled and the pending work is queued for
2683 * immediate execution. Like flush_work(), this function only
2684 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002685 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002686 * RETURNS:
2687 * %true if flush_work() waited for the work to finish execution,
2688 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002689 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002690bool flush_delayed_work(struct delayed_work *dwork)
2691{
2692 if (del_timer_sync(&dwork->timer))
2693 __queue_work(raw_smp_processor_id(),
2694 get_work_cwq(&dwork->work)->wq, &dwork->work);
2695 return flush_work(&dwork->work);
2696}
2697EXPORT_SYMBOL(flush_delayed_work);
2698
2699/**
Tejun Heo09383492010-09-16 10:48:29 +02002700 * flush_delayed_work_sync - wait for a dwork to finish
2701 * @dwork: the delayed work to flush
2702 *
2703 * Delayed timer is cancelled and the pending work is queued for
2704 * execution immediately. Other than timer handling, its behavior
2705 * is identical to flush_work_sync().
2706 *
2707 * RETURNS:
2708 * %true if flush_work_sync() waited for the work to finish execution,
2709 * %false if it was already idle.
2710 */
2711bool flush_delayed_work_sync(struct delayed_work *dwork)
2712{
2713 if (del_timer_sync(&dwork->timer))
2714 __queue_work(raw_smp_processor_id(),
2715 get_work_cwq(&dwork->work)->wq, &dwork->work);
2716 return flush_work_sync(&dwork->work);
2717}
2718EXPORT_SYMBOL(flush_delayed_work_sync);
2719
2720/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002721 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2722 * @dwork: the delayed work cancel
2723 *
2724 * This is cancel_work_sync() for delayed works.
2725 *
2726 * RETURNS:
2727 * %true if @dwork was pending, %false otherwise.
2728 */
2729bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002730{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002731 return __cancel_work_timer(&dwork->work, &dwork->timer);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002732}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002733EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002735/**
2736 * schedule_work - put work task in global workqueue
2737 * @work: job to be done
2738 *
Bart Van Assche5b0f437d2009-07-30 19:00:53 +02002739 * Returns zero if @work was already on the kernel-global workqueue and
2740 * non-zero otherwise.
2741 *
2742 * This puts a job in the kernel-global workqueue if it was not already
2743 * queued and leaves it in the same position on the kernel-global
2744 * workqueue otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002745 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002746int schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747{
Tejun Heod320c032010-06-29 10:07:14 +02002748 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749}
Dave Jonesae90dd52006-06-30 01:40:45 -04002750EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
Zhang Ruic1a220e2008-07-23 21:28:39 -07002752/*
2753 * schedule_work_on - put work task on a specific cpu
2754 * @cpu: cpu to put the work task on
2755 * @work: job to be done
2756 *
2757 * This puts a job on a specific cpu
2758 */
2759int schedule_work_on(int cpu, struct work_struct *work)
2760{
Tejun Heod320c032010-06-29 10:07:14 +02002761 return queue_work_on(cpu, system_wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07002762}
2763EXPORT_SYMBOL(schedule_work_on);
2764
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002765/**
2766 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +00002767 * @dwork: job to be done
2768 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002769 *
2770 * After waiting for a given time this puts a job in the kernel-global
2771 * workqueue.
2772 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002773int schedule_delayed_work(struct delayed_work *dwork,
Ingo Molnar82f67cd2007-02-16 01:28:13 -08002774 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775{
Tejun Heod320c032010-06-29 10:07:14 +02002776 return queue_delayed_work(system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777}
Dave Jonesae90dd52006-06-30 01:40:45 -04002778EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002780/**
2781 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
2782 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +00002783 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002784 * @delay: number of jiffies to wait
2785 *
2786 * After waiting for a given time this puts a job in the kernel-global
2787 * workqueue on the specified CPU.
2788 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +00002790 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791{
Tejun Heod320c032010-06-29 10:07:14 +02002792 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793}
Dave Jonesae90dd52006-06-30 01:40:45 -04002794EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
Andrew Mortonb6136772006-06-25 05:47:49 -07002796/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002797 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002798 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002799 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002800 * schedule_on_each_cpu() executes @func on each online CPU using the
2801 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002802 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002803 *
2804 * RETURNS:
2805 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002806 */
David Howells65f27f32006-11-22 14:55:48 +00002807int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002808{
2809 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002810 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002811
Andrew Mortonb6136772006-06-25 05:47:49 -07002812 works = alloc_percpu(struct work_struct);
2813 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002814 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002815
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002816 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002817
Christoph Lameter15316ba2006-01-08 01:00:43 -08002818 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002819 struct work_struct *work = per_cpu_ptr(works, cpu);
2820
2821 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002822 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002823 }
Tejun Heo93981802009-11-17 14:06:20 -08002824
2825 for_each_online_cpu(cpu)
2826 flush_work(per_cpu_ptr(works, cpu));
2827
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002828 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002829 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002830 return 0;
2831}
2832
Alan Sterneef6a7d2010-02-12 17:39:21 +09002833/**
2834 * flush_scheduled_work - ensure that any scheduled work has run to completion.
2835 *
2836 * Forces execution of the kernel-global workqueue and blocks until its
2837 * completion.
2838 *
2839 * Think twice before calling this function! It's very easy to get into
2840 * trouble if you don't take great care. Either of the following situations
2841 * will lead to deadlock:
2842 *
2843 * One of the work items currently on the workqueue needs to acquire
2844 * a lock held by your code or its caller.
2845 *
2846 * Your code is running in the context of a work routine.
2847 *
2848 * They will be detected by lockdep when they occur, but the first might not
2849 * occur very often. It depends on what work items are on the workqueue and
2850 * what locks they need, which you have no control over.
2851 *
2852 * In most situations flushing the entire workqueue is overkill; you merely
2853 * need to know that a particular work item isn't queued and isn't running.
2854 * In such cases you should use cancel_delayed_work_sync() or
2855 * cancel_work_sync() instead.
2856 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857void flush_scheduled_work(void)
2858{
Tejun Heod320c032010-06-29 10:07:14 +02002859 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860}
Dave Jonesae90dd52006-06-30 01:40:45 -04002861EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
2863/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06002864 * execute_in_process_context - reliably execute the routine with user context
2865 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06002866 * @ew: guaranteed storage for the execute work structure (must
2867 * be available when the work executes)
2868 *
2869 * Executes the function immediately if process context is available,
2870 * otherwise schedules the function for delayed execution.
2871 *
2872 * Returns: 0 - function was executed
2873 * 1 - function was scheduled for execution
2874 */
David Howells65f27f32006-11-22 14:55:48 +00002875int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06002876{
2877 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00002878 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002879 return 0;
2880 }
2881
David Howells65f27f32006-11-22 14:55:48 +00002882 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002883 schedule_work(&ew->work);
2884
2885 return 1;
2886}
2887EXPORT_SYMBOL_GPL(execute_in_process_context);
2888
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889int keventd_up(void)
2890{
Tejun Heod320c032010-06-29 10:07:14 +02002891 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892}
2893
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002894static int alloc_cwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895{
Oleg Nesterov3af244332007-05-09 02:34:09 -07002896 /*
Tejun Heo0f900042010-06-29 10:07:11 +02002897 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
2898 * Make sure that the alignment isn't lower than that of
2899 * unsigned long long.
Oleg Nesterov3af244332007-05-09 02:34:09 -07002900 */
Tejun Heo0f900042010-06-29 10:07:11 +02002901 const size_t size = sizeof(struct cpu_workqueue_struct);
2902 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
2903 __alignof__(unsigned long long));
Oleg Nesterov3af244332007-05-09 02:34:09 -07002904
Lai Jiangshane06ffa12012-03-09 18:03:20 +08002905 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02002906 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
Tejun Heo931ac772010-07-20 11:07:48 +02002907 else {
Tejun Heof3421792010-07-02 10:03:51 +02002908 void *ptr;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01002909
Tejun Heof3421792010-07-02 10:03:51 +02002910 /*
2911 * Allocate enough room to align cwq and put an extra
2912 * pointer at the end pointing back to the originally
2913 * allocated pointer which will be used for free.
2914 */
2915 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
2916 if (ptr) {
2917 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
2918 *(void **)(wq->cpu_wq.single + 1) = ptr;
2919 }
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002920 }
Tejun Heof3421792010-07-02 10:03:51 +02002921
Tejun Heo0415b00d12011-03-24 18:50:09 +01002922 /* just in case, make sure it's actually aligned */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002923 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
2924 return wq->cpu_wq.v ? 0 : -ENOMEM;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002925}
2926
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002927static void free_cwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002928{
Lai Jiangshane06ffa12012-03-09 18:03:20 +08002929 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02002930 free_percpu(wq->cpu_wq.pcpu);
2931 else if (wq->cpu_wq.single) {
2932 /* the pointer to free is stored right after the cwq */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002933 kfree(*(void **)(wq->cpu_wq.single + 1));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002934 }
2935}
2936
Tejun Heof3421792010-07-02 10:03:51 +02002937static int wq_clamp_max_active(int max_active, unsigned int flags,
2938 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002939{
Tejun Heof3421792010-07-02 10:03:51 +02002940 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
2941
2942 if (max_active < 1 || max_active > lim)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002943 printk(KERN_WARNING "workqueue: max_active %d requested for %s "
2944 "is out of range, clamping between %d and %d\n",
Tejun Heof3421792010-07-02 10:03:51 +02002945 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002946
Tejun Heof3421792010-07-02 10:03:51 +02002947 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002948}
2949
Tejun Heob196be82012-01-10 15:11:35 -08002950struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02002951 unsigned int flags,
2952 int max_active,
2953 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08002954 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07002955{
Tejun Heob196be82012-01-10 15:11:35 -08002956 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002957 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02002958 unsigned int cpu;
Tejun Heob196be82012-01-10 15:11:35 -08002959 size_t namelen;
2960
2961 /* determine namelen, allocate wq and format name */
2962 va_start(args, lock_name);
2963 va_copy(args1, args);
2964 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
2965
2966 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
2967 if (!wq)
2968 goto err;
2969
2970 vsnprintf(wq->name, namelen, fmt, args1);
2971 va_end(args);
2972 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002973
Tejun Heof3421792010-07-02 10:03:51 +02002974 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02002975 * Workqueues which may be used during memory reclaim should
2976 * have a rescuer to guarantee forward progress.
2977 */
2978 if (flags & WQ_MEM_RECLAIM)
2979 flags |= WQ_RESCUER;
2980
Tejun Heod320c032010-06-29 10:07:14 +02002981 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08002982 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002983
Tejun Heob196be82012-01-10 15:11:35 -08002984 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02002985 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002986 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02002987 mutex_init(&wq->flush_mutex);
2988 atomic_set(&wq->nr_cwqs_to_flush, 0);
2989 INIT_LIST_HEAD(&wq->flusher_queue);
2990 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002991
Johannes Bergeb13ba82008-01-16 09:51:58 +01002992 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07002993 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002994
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002995 if (alloc_cwqs(wq) < 0)
2996 goto err;
2997
Tejun Heof3421792010-07-02 10:03:51 +02002998 for_each_cwq_cpu(cpu, wq) {
Tejun Heo15376632010-06-29 10:07:11 +02002999 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003000 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo32704762012-07-13 22:16:45 -07003001 int pool_idx = (bool)(flags & WQ_HIGHPRI);
Tejun Heo15376632010-06-29 10:07:11 +02003002
Tejun Heo0f900042010-06-29 10:07:11 +02003003 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heo32704762012-07-13 22:16:45 -07003004 cwq->pool = &gcwq->pools[pool_idx];
Tejun Heoc34056a2010-06-29 10:07:11 +02003005 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02003006 cwq->flush_color = -1;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003007 cwq->max_active = max_active;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003008 INIT_LIST_HEAD(&cwq->delayed_works);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003009 }
3010
Tejun Heoe22bee72010-06-29 10:07:14 +02003011 if (flags & WQ_RESCUER) {
3012 struct worker *rescuer;
3013
Tejun Heof2e005a2010-07-20 15:59:09 +02003014 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
Tejun Heoe22bee72010-06-29 10:07:14 +02003015 goto err;
3016
3017 wq->rescuer = rescuer = alloc_worker();
3018 if (!rescuer)
3019 goto err;
3020
Tejun Heob196be82012-01-10 15:11:35 -08003021 rescuer->task = kthread_create(rescuer_thread, wq, "%s",
3022 wq->name);
Tejun Heoe22bee72010-06-29 10:07:14 +02003023 if (IS_ERR(rescuer->task))
3024 goto err;
3025
Tejun Heoe22bee72010-06-29 10:07:14 +02003026 rescuer->task->flags |= PF_THREAD_BOUND;
3027 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003028 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003029
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003030 /*
3031 * workqueue_lock protects global freeze state and workqueues
3032 * list. Grab it, set max_active accordingly and add the new
3033 * workqueue to workqueues list.
3034 */
Tejun Heo15376632010-06-29 10:07:11 +02003035 spin_lock(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003036
Tejun Heo58a69cb2011-02-16 09:25:31 +01003037 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heof3421792010-07-02 10:03:51 +02003038 for_each_cwq_cpu(cpu, wq)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003039 get_cwq(cpu, wq)->max_active = 0;
3040
Tejun Heo15376632010-06-29 10:07:11 +02003041 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003042
Tejun Heo15376632010-06-29 10:07:11 +02003043 spin_unlock(&workqueue_lock);
3044
Oleg Nesterov3af244332007-05-09 02:34:09 -07003045 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02003046err:
3047 if (wq) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003048 free_cwqs(wq);
Tejun Heof2e005a2010-07-20 15:59:09 +02003049 free_mayday_mask(wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02003050 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003051 kfree(wq);
3052 }
3053 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003054}
Tejun Heod320c032010-06-29 10:07:14 +02003055EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003056
3057/**
3058 * destroy_workqueue - safely terminate a workqueue
3059 * @wq: target workqueue
3060 *
3061 * Safely destroy a workqueue. All work currently pending will be done first.
3062 */
3063void destroy_workqueue(struct workqueue_struct *wq)
3064{
Tejun Heoc8e55f32010-06-29 10:07:12 +02003065 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003066
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003067 /* drain it before proceeding with destruction */
3068 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003069
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003070 /*
3071 * wq list is used to freeze wq, remove from list after
3072 * flushing is complete in case freeze races us.
3073 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003074 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07003075 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003076 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003077
Tejun Heoe22bee72010-06-29 10:07:14 +02003078 /* sanity check */
Tejun Heof3421792010-07-02 10:03:51 +02003079 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02003080 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3081 int i;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003082
Tejun Heo73f53c42010-06-29 10:07:11 +02003083 for (i = 0; i < WORK_NR_COLORS; i++)
3084 BUG_ON(cwq->nr_in_flight[i]);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003085 BUG_ON(cwq->nr_active);
3086 BUG_ON(!list_empty(&cwq->delayed_works));
Tejun Heo73f53c42010-06-29 10:07:11 +02003087 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003088
Tejun Heoe22bee72010-06-29 10:07:14 +02003089 if (wq->flags & WQ_RESCUER) {
3090 kthread_stop(wq->rescuer->task);
Tejun Heof2e005a2010-07-20 15:59:09 +02003091 free_mayday_mask(wq->mayday_mask);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003092 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003093 }
3094
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003095 free_cwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003096 kfree(wq);
3097}
3098EXPORT_SYMBOL_GPL(destroy_workqueue);
3099
Tejun Heodcd989c2010-06-29 10:07:14 +02003100/**
3101 * workqueue_set_max_active - adjust max_active of a workqueue
3102 * @wq: target workqueue
3103 * @max_active: new max_active value.
3104 *
3105 * Set max_active of @wq to @max_active.
3106 *
3107 * CONTEXT:
3108 * Don't call from IRQ context.
3109 */
3110void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3111{
3112 unsigned int cpu;
3113
Tejun Heof3421792010-07-02 10:03:51 +02003114 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003115
3116 spin_lock(&workqueue_lock);
3117
3118 wq->saved_max_active = max_active;
3119
Tejun Heof3421792010-07-02 10:03:51 +02003120 for_each_cwq_cpu(cpu, wq) {
Tejun Heodcd989c2010-06-29 10:07:14 +02003121 struct global_cwq *gcwq = get_gcwq(cpu);
3122
3123 spin_lock_irq(&gcwq->lock);
3124
Tejun Heo58a69cb2011-02-16 09:25:31 +01003125 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heodcd989c2010-06-29 10:07:14 +02003126 !(gcwq->flags & GCWQ_FREEZING))
3127 get_cwq(gcwq->cpu, wq)->max_active = max_active;
3128
3129 spin_unlock_irq(&gcwq->lock);
3130 }
3131
3132 spin_unlock(&workqueue_lock);
3133}
3134EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3135
3136/**
3137 * workqueue_congested - test whether a workqueue is congested
3138 * @cpu: CPU in question
3139 * @wq: target workqueue
3140 *
3141 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3142 * no synchronization around this function and the test result is
3143 * unreliable and only useful as advisory hints or for debugging.
3144 *
3145 * RETURNS:
3146 * %true if congested, %false otherwise.
3147 */
3148bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3149{
3150 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3151
3152 return !list_empty(&cwq->delayed_works);
3153}
3154EXPORT_SYMBOL_GPL(workqueue_congested);
3155
3156/**
3157 * work_cpu - return the last known associated cpu for @work
3158 * @work: the work of interest
3159 *
3160 * RETURNS:
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003161 * CPU number if @work was ever queued. WORK_CPU_NONE otherwise.
Tejun Heodcd989c2010-06-29 10:07:14 +02003162 */
3163unsigned int work_cpu(struct work_struct *work)
3164{
3165 struct global_cwq *gcwq = get_work_gcwq(work);
3166
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003167 return gcwq ? gcwq->cpu : WORK_CPU_NONE;
Tejun Heodcd989c2010-06-29 10:07:14 +02003168}
3169EXPORT_SYMBOL_GPL(work_cpu);
3170
3171/**
3172 * work_busy - test whether a work is currently pending or running
3173 * @work: the work to be tested
3174 *
3175 * Test whether @work is currently pending or running. There is no
3176 * synchronization around this function and the test result is
3177 * unreliable and only useful as advisory hints or for debugging.
3178 * Especially for reentrant wqs, the pending state might hide the
3179 * running state.
3180 *
3181 * RETURNS:
3182 * OR'd bitmask of WORK_BUSY_* bits.
3183 */
3184unsigned int work_busy(struct work_struct *work)
3185{
3186 struct global_cwq *gcwq = get_work_gcwq(work);
3187 unsigned long flags;
3188 unsigned int ret = 0;
3189
3190 if (!gcwq)
3191 return false;
3192
3193 spin_lock_irqsave(&gcwq->lock, flags);
3194
3195 if (work_pending(work))
3196 ret |= WORK_BUSY_PENDING;
3197 if (find_worker_executing_work(gcwq, work))
3198 ret |= WORK_BUSY_RUNNING;
3199
3200 spin_unlock_irqrestore(&gcwq->lock, flags);
3201
3202 return ret;
3203}
3204EXPORT_SYMBOL_GPL(work_busy);
3205
Tejun Heodb7bccf2010-06-29 10:07:12 +02003206/*
3207 * CPU hotplug.
3208 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003209 * There are two challenges in supporting CPU hotplug. Firstly, there
3210 * are a lot of assumptions on strong associations among work, cwq and
3211 * gcwq which make migrating pending and scheduled works very
3212 * difficult to implement without impacting hot paths. Secondly,
3213 * gcwqs serve mix of short, long and very long running works making
3214 * blocked draining impractical.
3215 *
Tejun Heo403c8212012-07-17 12:39:27 -07003216 * This is solved by allowing a gcwq to be detached from CPU, running it
3217 * with unbound workers and allowing it to be reattached later if the cpu
3218 * comes back online. A separate thread is created to govern a gcwq in
3219 * such state and is called the trustee of the gcwq.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003220 *
3221 * Trustee states and their descriptions.
3222 *
3223 * START Command state used on startup. On CPU_DOWN_PREPARE, a
3224 * new trustee is started with this state.
3225 *
3226 * IN_CHARGE Once started, trustee will enter this state after
Tejun Heoe22bee72010-06-29 10:07:14 +02003227 * assuming the manager role and making all existing
3228 * workers rogue. DOWN_PREPARE waits for trustee to
3229 * enter this state. After reaching IN_CHARGE, trustee
3230 * tries to execute the pending worklist until it's empty
3231 * and the state is set to BUTCHER, or the state is set
3232 * to RELEASE.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003233 *
3234 * BUTCHER Command state which is set by the cpu callback after
3235 * the cpu has went down. Once this state is set trustee
3236 * knows that there will be no new works on the worklist
3237 * and once the worklist is empty it can proceed to
3238 * killing idle workers.
3239 *
3240 * RELEASE Command state which is set by the cpu callback if the
3241 * cpu down has been canceled or it has come online
3242 * again. After recognizing this state, trustee stops
Tejun Heoe22bee72010-06-29 10:07:14 +02003243 * trying to drain or butcher and clears ROGUE, rebinds
3244 * all remaining workers back to the cpu and releases
3245 * manager role.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003246 *
3247 * DONE Trustee will enter this state after BUTCHER or RELEASE
3248 * is complete.
3249 *
3250 * trustee CPU draining
3251 * took over down complete
3252 * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE
3253 * | | ^
3254 * | CPU is back online v return workers |
3255 * ----------------> RELEASE --------------
3256 */
3257
3258/**
3259 * trustee_wait_event_timeout - timed event wait for trustee
3260 * @cond: condition to wait for
3261 * @timeout: timeout in jiffies
3262 *
3263 * wait_event_timeout() for trustee to use. Handles locking and
3264 * checks for RELEASE request.
3265 *
3266 * CONTEXT:
3267 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3268 * multiple times. To be used by trustee.
3269 *
3270 * RETURNS:
3271 * Positive indicating left time if @cond is satisfied, 0 if timed
3272 * out, -1 if canceled.
3273 */
3274#define trustee_wait_event_timeout(cond, timeout) ({ \
3275 long __ret = (timeout); \
3276 while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) && \
3277 __ret) { \
3278 spin_unlock_irq(&gcwq->lock); \
3279 __wait_event_timeout(gcwq->trustee_wait, (cond) || \
3280 (gcwq->trustee_state == TRUSTEE_RELEASE), \
3281 __ret); \
3282 spin_lock_irq(&gcwq->lock); \
3283 } \
3284 gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret); \
3285})
3286
3287/**
3288 * trustee_wait_event - event wait for trustee
3289 * @cond: condition to wait for
3290 *
3291 * wait_event() for trustee to use. Automatically handles locking and
3292 * checks for CANCEL request.
3293 *
3294 * CONTEXT:
3295 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3296 * multiple times. To be used by trustee.
3297 *
3298 * RETURNS:
3299 * 0 if @cond is satisfied, -1 if canceled.
3300 */
3301#define trustee_wait_event(cond) ({ \
3302 long __ret1; \
3303 __ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\
3304 __ret1 < 0 ? -1 : 0; \
3305})
3306
Tejun Heo4ce62e92012-07-13 22:16:44 -07003307static bool gcwq_is_managing_workers(struct global_cwq *gcwq)
3308{
3309 struct worker_pool *pool;
3310
3311 for_each_worker_pool(pool, gcwq)
3312 if (pool->flags & POOL_MANAGING_WORKERS)
3313 return true;
3314 return false;
3315}
3316
3317static bool gcwq_has_idle_workers(struct global_cwq *gcwq)
3318{
3319 struct worker_pool *pool;
3320
3321 for_each_worker_pool(pool, gcwq)
3322 if (!list_empty(&pool->idle_list))
3323 return true;
3324 return false;
3325}
3326
Tejun Heodb7bccf2010-06-29 10:07:12 +02003327static int __cpuinit trustee_thread(void *__gcwq)
3328{
3329 struct global_cwq *gcwq = __gcwq;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003330 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003331 struct worker *worker;
Tejun Heoe22bee72010-06-29 10:07:14 +02003332 struct work_struct *work;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003333 struct hlist_node *pos;
Tejun Heoe22bee72010-06-29 10:07:14 +02003334 long rc;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003335 int i;
3336
3337 BUG_ON(gcwq->cpu != smp_processor_id());
3338
3339 spin_lock_irq(&gcwq->lock);
3340 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003341 * Claim the manager position and make all workers rogue.
3342 * Trustee must be bound to the target cpu and can't be
3343 * cancelled.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003344 */
3345 BUG_ON(gcwq->cpu != smp_processor_id());
Tejun Heo4ce62e92012-07-13 22:16:44 -07003346 rc = trustee_wait_event(!gcwq_is_managing_workers(gcwq));
Tejun Heoe22bee72010-06-29 10:07:14 +02003347 BUG_ON(rc < 0);
3348
Tejun Heof2d5a0e2012-07-17 12:39:26 -07003349 /*
3350 * We've claimed all manager positions. Make all workers unbound
3351 * and set DISASSOCIATED. Before this, all workers except for the
3352 * ones which are still executing works from before the last CPU
3353 * down must be on the cpu. After this, they may become diasporas.
3354 */
Tejun Heo4ce62e92012-07-13 22:16:44 -07003355 for_each_worker_pool(pool, gcwq) {
3356 pool->flags |= POOL_MANAGING_WORKERS;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003357
Tejun Heo4ce62e92012-07-13 22:16:44 -07003358 list_for_each_entry(worker, &pool->idle_list, entry)
Tejun Heo403c8212012-07-17 12:39:27 -07003359 worker->flags |= WORKER_UNBOUND;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003360 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003361
3362 for_each_busy_worker(worker, i, pos, gcwq)
Tejun Heo403c8212012-07-17 12:39:27 -07003363 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003364
Tejun Heof2d5a0e2012-07-17 12:39:26 -07003365 gcwq->flags |= GCWQ_DISASSOCIATED;
3366
Tejun Heodb7bccf2010-06-29 10:07:12 +02003367 /*
Tejun Heo403c8212012-07-17 12:39:27 -07003368 * Call schedule() so that we cross rq->lock and thus can guarantee
3369 * sched callbacks see the unbound flag. This is necessary as
3370 * scheduler callbacks may be invoked from other cpus.
Tejun Heoe22bee72010-06-29 10:07:14 +02003371 */
3372 spin_unlock_irq(&gcwq->lock);
3373 schedule();
3374 spin_lock_irq(&gcwq->lock);
3375
3376 /*
Tejun Heocb444762010-07-02 10:03:50 +02003377 * Sched callbacks are disabled now. Zap nr_running. After
3378 * this, nr_running stays zero and need_more_worker() and
3379 * keep_working() are always true as long as the worklist is
3380 * not empty.
Tejun Heoe22bee72010-06-29 10:07:14 +02003381 */
Tejun Heo4ce62e92012-07-13 22:16:44 -07003382 for_each_worker_pool(pool, gcwq)
3383 atomic_set(get_pool_nr_running(pool), 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003384
3385 spin_unlock_irq(&gcwq->lock);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003386 for_each_worker_pool(pool, gcwq)
3387 del_timer_sync(&pool->idle_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003388 spin_lock_irq(&gcwq->lock);
3389
3390 /*
Tejun Heodb7bccf2010-06-29 10:07:12 +02003391 * We're now in charge. Notify and proceed to drain. We need
3392 * to keep the gcwq running during the whole CPU down
3393 * procedure as other cpu hotunplug callbacks may need to
3394 * flush currently running tasks.
3395 */
3396 gcwq->trustee_state = TRUSTEE_IN_CHARGE;
3397 wake_up_all(&gcwq->trustee_wait);
3398
3399 /*
3400 * The original cpu is in the process of dying and may go away
3401 * anytime now. When that happens, we and all workers would
Tejun Heoe22bee72010-06-29 10:07:14 +02003402 * be migrated to other cpus. Try draining any left work. We
3403 * want to get it over with ASAP - spam rescuers, wake up as
3404 * many idlers as necessary and create new ones till the
3405 * worklist is empty. Note that if the gcwq is frozen, there
Tejun Heo58a69cb2011-02-16 09:25:31 +01003406 * may be frozen works in freezable cwqs. Don't declare
Tejun Heoe22bee72010-06-29 10:07:14 +02003407 * completion while frozen.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003408 */
Tejun Heo4ce62e92012-07-13 22:16:44 -07003409 while (true) {
3410 bool busy = false;
Tejun Heoe22bee72010-06-29 10:07:14 +02003411
Tejun Heo4ce62e92012-07-13 22:16:44 -07003412 for_each_worker_pool(pool, gcwq)
3413 busy |= pool->nr_workers != pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +02003414
Tejun Heo4ce62e92012-07-13 22:16:44 -07003415 if (!busy && !(gcwq->flags & GCWQ_FREEZING) &&
3416 gcwq->trustee_state != TRUSTEE_IN_CHARGE)
3417 break;
Tejun Heoe22bee72010-06-29 10:07:14 +02003418
Tejun Heo4ce62e92012-07-13 22:16:44 -07003419 for_each_worker_pool(pool, gcwq) {
3420 int nr_works = 0;
3421
3422 list_for_each_entry(work, &pool->worklist, entry) {
3423 send_mayday(work);
3424 nr_works++;
3425 }
3426
3427 list_for_each_entry(worker, &pool->idle_list, entry) {
3428 if (!nr_works--)
3429 break;
3430 wake_up_process(worker->task);
3431 }
3432
3433 if (need_to_create_worker(pool)) {
3434 spin_unlock_irq(&gcwq->lock);
3435 worker = create_worker(pool, false);
3436 spin_lock_irq(&gcwq->lock);
3437 if (worker) {
Tejun Heo403c8212012-07-17 12:39:27 -07003438 worker->flags |= WORKER_UNBOUND;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003439 start_worker(worker);
3440 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003441 }
3442 }
3443
Tejun Heodb7bccf2010-06-29 10:07:12 +02003444 /* give a breather */
3445 if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN) < 0)
3446 break;
3447 }
3448
Tejun Heoe22bee72010-06-29 10:07:14 +02003449 /*
3450 * Either all works have been scheduled and cpu is down, or
3451 * cpu down has already been canceled. Wait for and butcher
3452 * all workers till we're canceled.
3453 */
3454 do {
Tejun Heo4ce62e92012-07-13 22:16:44 -07003455 rc = trustee_wait_event(gcwq_has_idle_workers(gcwq));
3456
3457 i = 0;
3458 for_each_worker_pool(pool, gcwq) {
3459 while (!list_empty(&pool->idle_list)) {
3460 worker = list_first_entry(&pool->idle_list,
3461 struct worker, entry);
3462 destroy_worker(worker);
3463 }
3464 i |= pool->nr_workers;
3465 }
3466 } while (i && rc >= 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003467
3468 /*
3469 * At this point, either draining has completed and no worker
3470 * is left, or cpu down has been canceled or the cpu is being
3471 * brought back up. There shouldn't be any idle one left.
3472 * Tell the remaining busy ones to rebind once it finishes the
3473 * currently scheduled works by scheduling the rebind_work.
3474 */
Tejun Heo4ce62e92012-07-13 22:16:44 -07003475 for_each_worker_pool(pool, gcwq)
3476 WARN_ON(!list_empty(&pool->idle_list));
Tejun Heoe22bee72010-06-29 10:07:14 +02003477
3478 for_each_busy_worker(worker, i, pos, gcwq) {
3479 struct work_struct *rebind_work = &worker->rebind_work;
3480
3481 /*
3482 * Rebind_work may race with future cpu hotplug
3483 * operations. Use a separate flag to mark that
3484 * rebinding is scheduled.
3485 */
Tejun Heocb444762010-07-02 10:03:50 +02003486 worker->flags |= WORKER_REBIND;
Tejun Heo403c8212012-07-17 12:39:27 -07003487 worker->flags &= ~WORKER_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02003488
3489 /* queue rebind_work, wq doesn't matter, use the default one */
3490 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
3491 work_data_bits(rebind_work)))
3492 continue;
3493
3494 debug_work_activate(rebind_work);
Tejun Heod320c032010-06-29 10:07:14 +02003495 insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work,
Tejun Heoe22bee72010-06-29 10:07:14 +02003496 worker->scheduled.next,
3497 work_color_to_flags(WORK_NO_COLOR));
3498 }
3499
3500 /* relinquish manager role */
Tejun Heo4ce62e92012-07-13 22:16:44 -07003501 for_each_worker_pool(pool, gcwq)
3502 pool->flags &= ~POOL_MANAGING_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02003503
Tejun Heodb7bccf2010-06-29 10:07:12 +02003504 /* notify completion */
3505 gcwq->trustee = NULL;
3506 gcwq->trustee_state = TRUSTEE_DONE;
3507 wake_up_all(&gcwq->trustee_wait);
3508 spin_unlock_irq(&gcwq->lock);
3509 return 0;
3510}
3511
3512/**
3513 * wait_trustee_state - wait for trustee to enter the specified state
3514 * @gcwq: gcwq the trustee of interest belongs to
3515 * @state: target state to wait for
3516 *
3517 * Wait for the trustee to reach @state. DONE is already matched.
3518 *
3519 * CONTEXT:
3520 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3521 * multiple times. To be used by cpu_callback.
3522 */
3523static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09003524__releases(&gcwq->lock)
3525__acquires(&gcwq->lock)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003526{
3527 if (!(gcwq->trustee_state == state ||
3528 gcwq->trustee_state == TRUSTEE_DONE)) {
3529 spin_unlock_irq(&gcwq->lock);
3530 __wait_event(gcwq->trustee_wait,
3531 gcwq->trustee_state == state ||
3532 gcwq->trustee_state == TRUSTEE_DONE);
3533 spin_lock_irq(&gcwq->lock);
3534 }
3535}
3536
Oleg Nesterov3af244332007-05-09 02:34:09 -07003537static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
3538 unsigned long action,
3539 void *hcpu)
3540{
3541 unsigned int cpu = (unsigned long)hcpu;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003542 struct global_cwq *gcwq = get_gcwq(cpu);
3543 struct task_struct *new_trustee = NULL;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003544 struct worker *new_workers[NR_WORKER_POOLS] = { };
3545 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003546 unsigned long flags;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003547 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003549 action &= ~CPU_TASKS_FROZEN;
3550
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003552 case CPU_DOWN_PREPARE:
3553 new_trustee = kthread_create(trustee_thread, gcwq,
3554 "workqueue_trustee/%d\n", cpu);
3555 if (IS_ERR(new_trustee))
3556 return notifier_from_errno(PTR_ERR(new_trustee));
3557 kthread_bind(new_trustee, cpu);
Tejun Heoe22bee72010-06-29 10:07:14 +02003558 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 case CPU_UP_PREPARE:
Tejun Heo4ce62e92012-07-13 22:16:44 -07003560 i = 0;
3561 for_each_worker_pool(pool, gcwq) {
3562 BUG_ON(pool->first_idle);
3563 new_workers[i] = create_worker(pool, false);
3564 if (!new_workers[i++])
3565 goto err_destroy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 }
3568
Tejun Heodb7bccf2010-06-29 10:07:12 +02003569 /* some are called w/ irq disabled, don't disturb irq status */
3570 spin_lock_irqsave(&gcwq->lock, flags);
3571
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003572 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003573 case CPU_DOWN_PREPARE:
3574 /* initialize trustee and tell it to acquire the gcwq */
3575 BUG_ON(gcwq->trustee || gcwq->trustee_state != TRUSTEE_DONE);
3576 gcwq->trustee = new_trustee;
3577 gcwq->trustee_state = TRUSTEE_START;
3578 wake_up_process(gcwq->trustee);
3579 wait_trustee_state(gcwq, TRUSTEE_IN_CHARGE);
Tejun Heoe22bee72010-06-29 10:07:14 +02003580 /* fall through */
3581 case CPU_UP_PREPARE:
Tejun Heo4ce62e92012-07-13 22:16:44 -07003582 i = 0;
3583 for_each_worker_pool(pool, gcwq) {
3584 BUG_ON(pool->first_idle);
3585 pool->first_idle = new_workers[i++];
3586 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003587 break;
3588
Oleg Nesterov3da1c842008-07-25 01:47:50 -07003589 case CPU_POST_DEAD:
Tejun Heodb7bccf2010-06-29 10:07:12 +02003590 gcwq->trustee_state = TRUSTEE_BUTCHER;
Tejun Heoe22bee72010-06-29 10:07:14 +02003591 /* fall through */
3592 case CPU_UP_CANCELED:
Tejun Heo4ce62e92012-07-13 22:16:44 -07003593 for_each_worker_pool(pool, gcwq) {
3594 destroy_worker(pool->first_idle);
3595 pool->first_idle = NULL;
3596 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003597 break;
3598
3599 case CPU_DOWN_FAILED:
3600 case CPU_ONLINE:
Tejun Heoe22bee72010-06-29 10:07:14 +02003601 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003602 if (gcwq->trustee_state != TRUSTEE_DONE) {
3603 gcwq->trustee_state = TRUSTEE_RELEASE;
3604 wake_up_process(gcwq->trustee);
3605 wait_trustee_state(gcwq, TRUSTEE_DONE);
3606 }
3607
Tejun Heoe22bee72010-06-29 10:07:14 +02003608 /*
3609 * Trustee is done and there might be no worker left.
3610 * Put the first_idle in and request a real manager to
3611 * take a look.
3612 */
Tejun Heo4ce62e92012-07-13 22:16:44 -07003613 for_each_worker_pool(pool, gcwq) {
3614 spin_unlock_irq(&gcwq->lock);
3615 kthread_bind(pool->first_idle->task, cpu);
3616 spin_lock_irq(&gcwq->lock);
3617 pool->flags |= POOL_MANAGE_WORKERS;
3618 start_worker(pool->first_idle);
3619 pool->first_idle = NULL;
3620 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003621 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003622 }
3623
Tejun Heodb7bccf2010-06-29 10:07:12 +02003624 spin_unlock_irqrestore(&gcwq->lock, flags);
3625
Tejun Heo15376632010-06-29 10:07:11 +02003626 return notifier_from_errno(0);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003627
3628err_destroy:
3629 if (new_trustee)
3630 kthread_stop(new_trustee);
3631
3632 spin_lock_irqsave(&gcwq->lock, flags);
3633 for (i = 0; i < NR_WORKER_POOLS; i++)
3634 if (new_workers[i])
3635 destroy_worker(new_workers[i]);
3636 spin_unlock_irqrestore(&gcwq->lock, flags);
3637
3638 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640
Tejun Heo65758202012-07-17 12:39:26 -07003641/*
3642 * Workqueues should be brought up before normal priority CPU notifiers.
3643 * This will be registered high priority CPU notifier.
3644 */
3645static int __devinit workqueue_cpu_up_callback(struct notifier_block *nfb,
3646 unsigned long action,
3647 void *hcpu)
3648{
3649 switch (action & ~CPU_TASKS_FROZEN) {
3650 case CPU_UP_PREPARE:
3651 case CPU_UP_CANCELED:
3652 case CPU_DOWN_FAILED:
3653 case CPU_ONLINE:
3654 return workqueue_cpu_callback(nfb, action, hcpu);
3655 }
3656 return NOTIFY_OK;
3657}
3658
3659/*
3660 * Workqueues should be brought down after normal priority CPU notifiers.
3661 * This will be registered as low priority CPU notifier.
3662 */
3663static int __devinit workqueue_cpu_down_callback(struct notifier_block *nfb,
3664 unsigned long action,
3665 void *hcpu)
3666{
3667 switch (action & ~CPU_TASKS_FROZEN) {
3668 case CPU_DOWN_PREPARE:
Tejun Heo65758202012-07-17 12:39:26 -07003669 case CPU_POST_DEAD:
3670 return workqueue_cpu_callback(nfb, action, hcpu);
3671 }
3672 return NOTIFY_OK;
3673}
3674
Rusty Russell2d3854a2008-11-05 13:39:10 +11003675#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003676
Rusty Russell2d3854a2008-11-05 13:39:10 +11003677struct work_for_cpu {
Andrew Morton6b44003e2009-04-09 09:50:37 -06003678 struct completion completion;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003679 long (*fn)(void *);
3680 void *arg;
3681 long ret;
3682};
3683
Andrew Morton6b44003e2009-04-09 09:50:37 -06003684static int do_work_for_cpu(void *_wfc)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003685{
Andrew Morton6b44003e2009-04-09 09:50:37 -06003686 struct work_for_cpu *wfc = _wfc;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003687 wfc->ret = wfc->fn(wfc->arg);
Andrew Morton6b44003e2009-04-09 09:50:37 -06003688 complete(&wfc->completion);
3689 return 0;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003690}
3691
3692/**
3693 * work_on_cpu - run a function in user context on a particular cpu
3694 * @cpu: the cpu to run on
3695 * @fn: the function to run
3696 * @arg: the function arg
3697 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003698 * This will return the value @fn returns.
3699 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b44003e2009-04-09 09:50:37 -06003700 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003701 */
3702long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3703{
Andrew Morton6b44003e2009-04-09 09:50:37 -06003704 struct task_struct *sub_thread;
3705 struct work_for_cpu wfc = {
3706 .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
3707 .fn = fn,
3708 .arg = arg,
3709 };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003710
Andrew Morton6b44003e2009-04-09 09:50:37 -06003711 sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
3712 if (IS_ERR(sub_thread))
3713 return PTR_ERR(sub_thread);
3714 kthread_bind(sub_thread, cpu);
3715 wake_up_process(sub_thread);
3716 wait_for_completion(&wfc.completion);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003717 return wfc.ret;
3718}
3719EXPORT_SYMBOL_GPL(work_on_cpu);
3720#endif /* CONFIG_SMP */
3721
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003722#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303723
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003724/**
3725 * freeze_workqueues_begin - begin freezing workqueues
3726 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003727 * Start freezing workqueues. After this function returns, all freezable
3728 * workqueues will queue new works to their frozen_works list instead of
3729 * gcwq->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003730 *
3731 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003732 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003733 */
3734void freeze_workqueues_begin(void)
3735{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003736 unsigned int cpu;
3737
3738 spin_lock(&workqueue_lock);
3739
3740 BUG_ON(workqueue_freezing);
3741 workqueue_freezing = true;
3742
Tejun Heof3421792010-07-02 10:03:51 +02003743 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003744 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003745 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003746
3747 spin_lock_irq(&gcwq->lock);
3748
Tejun Heodb7bccf2010-06-29 10:07:12 +02003749 BUG_ON(gcwq->flags & GCWQ_FREEZING);
3750 gcwq->flags |= GCWQ_FREEZING;
3751
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003752 list_for_each_entry(wq, &workqueues, list) {
3753 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3754
Tejun Heo58a69cb2011-02-16 09:25:31 +01003755 if (cwq && wq->flags & WQ_FREEZABLE)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003756 cwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003757 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003758
3759 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003760 }
3761
3762 spin_unlock(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003764
3765/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003766 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003767 *
3768 * Check whether freezing is complete. This function must be called
3769 * between freeze_workqueues_begin() and thaw_workqueues().
3770 *
3771 * CONTEXT:
3772 * Grabs and releases workqueue_lock.
3773 *
3774 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01003775 * %true if some freezable workqueues are still busy. %false if freezing
3776 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003777 */
3778bool freeze_workqueues_busy(void)
3779{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003780 unsigned int cpu;
3781 bool busy = false;
3782
3783 spin_lock(&workqueue_lock);
3784
3785 BUG_ON(!workqueue_freezing);
3786
Tejun Heof3421792010-07-02 10:03:51 +02003787 for_each_gcwq_cpu(cpu) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003788 struct workqueue_struct *wq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003789 /*
3790 * nr_active is monotonically decreasing. It's safe
3791 * to peek without lock.
3792 */
3793 list_for_each_entry(wq, &workqueues, list) {
3794 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3795
Tejun Heo58a69cb2011-02-16 09:25:31 +01003796 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003797 continue;
3798
3799 BUG_ON(cwq->nr_active < 0);
3800 if (cwq->nr_active) {
3801 busy = true;
3802 goto out_unlock;
3803 }
3804 }
3805 }
3806out_unlock:
3807 spin_unlock(&workqueue_lock);
3808 return busy;
3809}
3810
3811/**
3812 * thaw_workqueues - thaw workqueues
3813 *
3814 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo7e116292010-06-29 10:07:13 +02003815 * frozen works are transferred to their respective gcwq worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003816 *
3817 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003818 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003819 */
3820void thaw_workqueues(void)
3821{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003822 unsigned int cpu;
3823
3824 spin_lock(&workqueue_lock);
3825
3826 if (!workqueue_freezing)
3827 goto out_unlock;
3828
Tejun Heof3421792010-07-02 10:03:51 +02003829 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003830 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003831 struct worker_pool *pool;
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003832 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003833
3834 spin_lock_irq(&gcwq->lock);
3835
Tejun Heodb7bccf2010-06-29 10:07:12 +02003836 BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
3837 gcwq->flags &= ~GCWQ_FREEZING;
3838
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003839 list_for_each_entry(wq, &workqueues, list) {
3840 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3841
Tejun Heo58a69cb2011-02-16 09:25:31 +01003842 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003843 continue;
3844
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003845 /* restore max_active and repopulate worklist */
3846 cwq->max_active = wq->saved_max_active;
3847
3848 while (!list_empty(&cwq->delayed_works) &&
3849 cwq->nr_active < cwq->max_active)
3850 cwq_activate_first_delayed(cwq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003851 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003852
Tejun Heo4ce62e92012-07-13 22:16:44 -07003853 for_each_worker_pool(pool, gcwq)
3854 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02003855
Tejun Heo8b03ae32010-06-29 10:07:12 +02003856 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003857 }
3858
3859 workqueue_freezing = false;
3860out_unlock:
3861 spin_unlock(&workqueue_lock);
3862}
3863#endif /* CONFIG_FREEZER */
3864
Suresh Siddha6ee05782010-07-30 14:57:37 -07003865static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866{
Tejun Heoc34056a2010-06-29 10:07:11 +02003867 unsigned int cpu;
Tejun Heoc8e55f32010-06-29 10:07:12 +02003868 int i;
Tejun Heoc34056a2010-06-29 10:07:11 +02003869
Tejun Heo65758202012-07-17 12:39:26 -07003870 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
3871 cpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003872
3873 /* initialize gcwqs */
Tejun Heof3421792010-07-02 10:03:51 +02003874 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003875 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003876 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003877
3878 spin_lock_init(&gcwq->lock);
3879 gcwq->cpu = cpu;
Tejun Heo477a3c32010-08-31 10:54:35 +02003880 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003881
Tejun Heoc8e55f32010-06-29 10:07:12 +02003882 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
3883 INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
3884
Tejun Heo4ce62e92012-07-13 22:16:44 -07003885 for_each_worker_pool(pool, gcwq) {
3886 pool->gcwq = gcwq;
3887 INIT_LIST_HEAD(&pool->worklist);
3888 INIT_LIST_HEAD(&pool->idle_list);
Tejun Heoe22bee72010-06-29 10:07:14 +02003889
Tejun Heo4ce62e92012-07-13 22:16:44 -07003890 init_timer_deferrable(&pool->idle_timer);
3891 pool->idle_timer.function = idle_worker_timeout;
3892 pool->idle_timer.data = (unsigned long)pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003893
Tejun Heo4ce62e92012-07-13 22:16:44 -07003894 setup_timer(&pool->mayday_timer, gcwq_mayday_timeout,
3895 (unsigned long)pool);
3896
3897 ida_init(&pool->worker_ida);
3898 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003899
3900 gcwq->trustee_state = TRUSTEE_DONE;
3901 init_waitqueue_head(&gcwq->trustee_wait);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003902 }
3903
Tejun Heoe22bee72010-06-29 10:07:14 +02003904 /* create the initial worker */
Tejun Heof3421792010-07-02 10:03:51 +02003905 for_each_online_gcwq_cpu(cpu) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003906 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003907 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003908
Tejun Heo477a3c32010-08-31 10:54:35 +02003909 if (cpu != WORK_CPU_UNBOUND)
3910 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003911
3912 for_each_worker_pool(pool, gcwq) {
3913 struct worker *worker;
3914
3915 worker = create_worker(pool, true);
3916 BUG_ON(!worker);
3917 spin_lock_irq(&gcwq->lock);
3918 start_worker(worker);
3919 spin_unlock_irq(&gcwq->lock);
3920 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003921 }
3922
Tejun Heod320c032010-06-29 10:07:14 +02003923 system_wq = alloc_workqueue("events", 0, 0);
3924 system_long_wq = alloc_workqueue("events_long", 0, 0);
3925 system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0);
Tejun Heof3421792010-07-02 10:03:51 +02003926 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3927 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01003928 system_freezable_wq = alloc_workqueue("events_freezable",
3929 WQ_FREEZABLE, 0);
Alan Stern62d3c542012-03-02 10:51:00 +01003930 system_nrt_freezable_wq = alloc_workqueue("events_nrt_freezable",
3931 WQ_NON_REENTRANT | WQ_FREEZABLE, 0);
Hitoshi Mitakee5cba242010-11-26 12:06:44 +01003932 BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq ||
Alan Stern62d3c542012-03-02 10:51:00 +01003933 !system_unbound_wq || !system_freezable_wq ||
3934 !system_nrt_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07003935 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936}
Suresh Siddha6ee05782010-07-30 14:57:37 -07003937early_initcall(init_workqueues);