blob: c936512087607d6e141fa4cc0e2a23428b717107 [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>
Sasha Levin42f85702012-12-17 10:01:23 -050044#include <linux/hashtable.h>
Tejun Heoe22bee72010-06-29 10:07:14 +020045
Tejun Heoea138442013-01-18 14:05:55 -080046#include "workqueue_internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Tejun Heoc8e55f32010-06-29 10:07:12 +020048enum {
Tejun Heobc2ae0f2012-07-17 12:39:27 -070049 /*
Tejun Heo24647572013-01-24 11:01:33 -080050 * worker_pool flags
Tejun Heobc2ae0f2012-07-17 12:39:27 -070051 *
Tejun Heo24647572013-01-24 11:01:33 -080052 * A bound pool is either associated or disassociated with its CPU.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070053 * While associated (!DISASSOCIATED), all workers are bound to the
54 * CPU and none has %WORKER_UNBOUND set and concurrency management
55 * is in effect.
56 *
57 * While DISASSOCIATED, the cpu may be offline and all workers have
58 * %WORKER_UNBOUND set and concurrency management disabled, and may
Tejun Heo24647572013-01-24 11:01:33 -080059 * be executing on any CPU. The pool behaves as an unbound one.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070060 *
61 * Note that DISASSOCIATED can be flipped only while holding
Tejun Heo24647572013-01-24 11:01:33 -080062 * assoc_mutex to avoid changing binding state while
63 * create_worker() is in progress.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070064 */
Tejun Heo11ebea52012-07-12 14:46:37 -070065 POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
Lai Jiangshan552a37e2012-09-10 10:03:33 -070066 POOL_MANAGING_WORKERS = 1 << 1, /* managing workers */
Tejun Heo24647572013-01-24 11:01:33 -080067 POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */
Tejun Heo35b6bb62013-01-24 11:01:33 -080068 POOL_FREEZING = 1 << 3, /* freeze in progress */
Tejun Heodb7bccf2010-06-29 10:07:12 +020069
Tejun Heoc8e55f32010-06-29 10:07:12 +020070 /* worker flags */
71 WORKER_STARTED = 1 << 0, /* started */
72 WORKER_DIE = 1 << 1, /* die die die */
73 WORKER_IDLE = 1 << 2, /* is idle */
Tejun Heoe22bee72010-06-29 10:07:14 +020074 WORKER_PREP = 1 << 3, /* preparing to run works */
Tejun Heofb0e7be2010-06-29 10:07:15 +020075 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
Tejun Heof3421792010-07-02 10:03:51 +020076 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
Tejun Heoe22bee72010-06-29 10:07:14 +020077
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -070078 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_UNBOUND |
Tejun Heo403c8212012-07-17 12:39:27 -070079 WORKER_CPU_INTENSIVE,
Tejun Heodb7bccf2010-06-29 10:07:12 +020080
Tejun Heoe34cdddb2013-01-24 11:01:33 -080081 NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */
Tejun Heo4ce62e92012-07-13 22:16:44 -070082
Tejun Heoc8e55f32010-06-29 10:07:12 +020083 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
Tejun Heodb7bccf2010-06-29 10:07:12 +020084
Tejun Heoe22bee72010-06-29 10:07:14 +020085 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
86 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
87
Tejun Heo3233cdb2011-02-16 18:10:19 +010088 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
89 /* call for help after 10ms
90 (min two ticks) */
Tejun Heoe22bee72010-06-29 10:07:14 +020091 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
92 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heoe22bee72010-06-29 10:07:14 +020093
94 /*
95 * Rescue workers are used only on emergencies and shared by
96 * all cpus. Give -20.
97 */
98 RESCUER_NICE_LEVEL = -20,
Tejun Heo32704762012-07-13 22:16:45 -070099 HIGHPRI_NICE_LEVEL = -20,
Tejun Heoc8e55f32010-06-29 10:07:12 +0200100};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102/*
Tejun Heo4690c4a2010-06-29 10:07:10 +0200103 * Structure fields follow one of the following exclusion rules.
104 *
Tejun Heoe41e7042010-08-24 14:22:47 +0200105 * I: Modifiable by initialization/destruction paths and read-only for
106 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200107 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200108 * P: Preemption protected. Disabling preemption is enough and should
109 * only be modified and accessed from the local cpu.
110 *
Tejun Heod565ed62013-01-24 11:01:33 -0800111 * L: pool->lock protected. Access with pool->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200112 *
Tejun Heod565ed62013-01-24 11:01:33 -0800113 * X: During normal operation, modification requires pool->lock and should
114 * be done only from local cpu. Either disabling preemption on local
115 * cpu or grabbing pool->lock is enough for read access. If
116 * POOL_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200117 *
Tejun Heo73f53c42010-06-29 10:07:11 +0200118 * F: wq->flush_mutex protected.
119 *
Tejun Heo4690c4a2010-06-29 10:07:10 +0200120 * W: workqueue_lock protected.
121 */
122
Tejun Heo2eaebdb2013-01-18 14:05:55 -0800123/* struct worker is defined in workqueue_internal.h */
Tejun Heoc34056a2010-06-29 10:07:11 +0200124
Tejun Heobd7bdd42012-07-12 14:46:37 -0700125struct worker_pool {
126 struct global_cwq *gcwq; /* I: the owning gcwq */
Tejun Heod565ed62013-01-24 11:01:33 -0800127 spinlock_t lock; /* the pool lock */
Tejun Heoec22ca52013-01-24 11:01:33 -0800128 unsigned int cpu; /* I: the associated cpu */
Tejun Heo9daf9e62013-01-24 11:01:33 -0800129 int id; /* I: pool ID */
Tejun Heo11ebea52012-07-12 14:46:37 -0700130 unsigned int flags; /* X: flags */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700131
132 struct list_head worklist; /* L: list of pending works */
133 int nr_workers; /* L: total number of workers */
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700134
135 /* nr_idle includes the ones off idle_list for rebinding */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700136 int nr_idle; /* L: currently idle ones */
137
138 struct list_head idle_list; /* X: list of idle workers */
139 struct timer_list idle_timer; /* L: worker idle timeout */
140 struct timer_list mayday_timer; /* L: SOS timer for workers */
141
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800142 /* workers are chained either in busy_hash or idle_list */
143 DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER);
144 /* L: hash of busy workers */
145
Tejun Heo24647572013-01-24 11:01:33 -0800146 struct mutex assoc_mutex; /* protect POOL_DISASSOCIATED */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700147 struct ida worker_ida; /* L: for worker IDs */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700148};
149
Tejun Heo4690c4a2010-06-29 10:07:10 +0200150/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200151 * Global per-cpu workqueue. There's one and only one for each cpu
152 * and all works are queued and processed here regardless of their
153 * target workqueues.
Tejun Heo8b03ae32010-06-29 10:07:12 +0200154 */
155struct global_cwq {
Tejun Heoe34cdddb2013-01-24 11:01:33 -0800156 struct worker_pool pools[NR_STD_WORKER_POOLS];
Joonsoo Kim330dad52012-08-15 23:25:36 +0900157 /* normal and highpri pools */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200158} ____cacheline_aligned_in_smp;
159
160/*
Tejun Heo502ca9d2010-06-29 10:07:13 +0200161 * The per-CPU workqueue. The lower WORK_STRUCT_FLAG_BITS of
Tejun Heo0f900042010-06-29 10:07:11 +0200162 * work_struct->data are used for flags and thus cwqs need to be
163 * aligned at two's power of the number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 */
165struct cpu_workqueue_struct {
Tejun Heobd7bdd42012-07-12 14:46:37 -0700166 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200167 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200168 int work_color; /* L: current color */
169 int flush_color; /* L: flushing color */
170 int nr_in_flight[WORK_NR_COLORS];
171 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200172 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200173 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200174 struct list_head delayed_works; /* L: delayed works */
Tejun Heo0f900042010-06-29 10:07:11 +0200175};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200178 * Structure used to wait for workqueue flush.
179 */
180struct wq_flusher {
181 struct list_head list; /* F: list of flushers */
182 int flush_color; /* F: flush color waiting for */
183 struct completion done; /* flush completion */
184};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Tejun Heo73f53c42010-06-29 10:07:11 +0200186/*
Tejun Heof2e005a2010-07-20 15:59:09 +0200187 * All cpumasks are assumed to be always set on UP and thus can't be
188 * used to determine whether there's something to be done.
189 */
190#ifdef CONFIG_SMP
191typedef cpumask_var_t mayday_mask_t;
192#define mayday_test_and_set_cpu(cpu, mask) \
193 cpumask_test_and_set_cpu((cpu), (mask))
194#define mayday_clear_cpu(cpu, mask) cpumask_clear_cpu((cpu), (mask))
195#define for_each_mayday_cpu(cpu, mask) for_each_cpu((cpu), (mask))
Tejun Heo9c375472010-08-31 11:18:34 +0200196#define alloc_mayday_mask(maskp, gfp) zalloc_cpumask_var((maskp), (gfp))
Tejun Heof2e005a2010-07-20 15:59:09 +0200197#define free_mayday_mask(mask) free_cpumask_var((mask))
198#else
199typedef unsigned long mayday_mask_t;
200#define mayday_test_and_set_cpu(cpu, mask) test_and_set_bit(0, &(mask))
201#define mayday_clear_cpu(cpu, mask) clear_bit(0, &(mask))
202#define for_each_mayday_cpu(cpu, mask) if ((cpu) = 0, (mask))
203#define alloc_mayday_mask(maskp, gfp) true
204#define free_mayday_mask(mask) do { } while (0)
205#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207/*
208 * The externally visible workqueue abstraction is an array of
209 * per-CPU workqueues:
210 */
211struct workqueue_struct {
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200212 unsigned int flags; /* W: WQ_* flags */
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200213 union {
214 struct cpu_workqueue_struct __percpu *pcpu;
215 struct cpu_workqueue_struct *single;
216 unsigned long v;
217 } cpu_wq; /* I: cwq's */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200218 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200219
220 struct mutex flush_mutex; /* protects wq flushing */
221 int work_color; /* F: current work color */
222 int flush_color; /* F: current flush color */
223 atomic_t nr_cwqs_to_flush; /* flush in progress */
224 struct wq_flusher *first_flusher; /* F: first flusher */
225 struct list_head flusher_queue; /* F: flush waiters */
226 struct list_head flusher_overflow; /* F: flush overflow list */
227
Tejun Heof2e005a2010-07-20 15:59:09 +0200228 mayday_mask_t mayday_mask; /* cpus requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200229 struct worker *rescuer; /* I: rescue worker */
230
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200231 int nr_drainers; /* W: drain in progress */
Tejun Heodcd989c2010-06-29 10:07:14 +0200232 int saved_max_active; /* W: saved cwq max_active */
Johannes Berg4e6045f2007-10-18 23:39:55 -0700233#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200234 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700235#endif
Tejun Heob196be82012-01-10 15:11:35 -0800236 char name[]; /* I: workqueue name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237};
238
Tejun Heod320c032010-06-29 10:07:14 +0200239struct workqueue_struct *system_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200240EXPORT_SYMBOL_GPL(system_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300241struct workqueue_struct *system_highpri_wq __read_mostly;
Joonsoo Kim1aabe902012-08-15 23:25:39 +0900242EXPORT_SYMBOL_GPL(system_highpri_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300243struct workqueue_struct *system_long_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200244EXPORT_SYMBOL_GPL(system_long_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300245struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200246EXPORT_SYMBOL_GPL(system_unbound_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300247struct workqueue_struct *system_freezable_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100248EXPORT_SYMBOL_GPL(system_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200249
Tejun Heo97bd2342010-10-05 10:41:14 +0200250#define CREATE_TRACE_POINTS
251#include <trace/events/workqueue.h>
252
Tejun Heo4ce62e92012-07-13 22:16:44 -0700253#define for_each_worker_pool(pool, gcwq) \
Tejun Heo32704762012-07-13 22:16:45 -0700254 for ((pool) = &(gcwq)->pools[0]; \
Tejun Heoe34cdddb2013-01-24 11:01:33 -0800255 (pool) < &(gcwq)->pools[NR_STD_WORKER_POOLS]; (pool)++)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700256
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800257#define for_each_busy_worker(worker, i, pos, pool) \
258 hash_for_each(pool->busy_hash, i, pos, worker, hentry)
Tejun Heodb7bccf2010-06-29 10:07:12 +0200259
Tejun Heof3421792010-07-02 10:03:51 +0200260static inline int __next_gcwq_cpu(int cpu, const struct cpumask *mask,
261 unsigned int sw)
262{
263 if (cpu < nr_cpu_ids) {
264 if (sw & 1) {
265 cpu = cpumask_next(cpu, mask);
266 if (cpu < nr_cpu_ids)
267 return cpu;
268 }
269 if (sw & 2)
270 return WORK_CPU_UNBOUND;
271 }
272 return WORK_CPU_NONE;
273}
274
275static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
276 struct workqueue_struct *wq)
277{
278 return __next_gcwq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2);
279}
280
Tejun Heo09884952010-08-01 11:50:12 +0200281/*
282 * CPU iterators
283 *
284 * An extra gcwq is defined for an invalid cpu number
285 * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any
286 * specific CPU. The following iterators are similar to
287 * for_each_*_cpu() iterators but also considers the unbound gcwq.
288 *
289 * for_each_gcwq_cpu() : possible CPUs + WORK_CPU_UNBOUND
290 * for_each_online_gcwq_cpu() : online CPUs + WORK_CPU_UNBOUND
291 * for_each_cwq_cpu() : possible CPUs for bound workqueues,
292 * WORK_CPU_UNBOUND for unbound workqueues
293 */
Tejun Heof3421792010-07-02 10:03:51 +0200294#define for_each_gcwq_cpu(cpu) \
295 for ((cpu) = __next_gcwq_cpu(-1, cpu_possible_mask, 3); \
296 (cpu) < WORK_CPU_NONE; \
297 (cpu) = __next_gcwq_cpu((cpu), cpu_possible_mask, 3))
298
299#define for_each_online_gcwq_cpu(cpu) \
300 for ((cpu) = __next_gcwq_cpu(-1, cpu_online_mask, 3); \
301 (cpu) < WORK_CPU_NONE; \
302 (cpu) = __next_gcwq_cpu((cpu), cpu_online_mask, 3))
303
304#define for_each_cwq_cpu(cpu, wq) \
305 for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, (wq)); \
306 (cpu) < WORK_CPU_NONE; \
307 (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq)))
308
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900309#ifdef CONFIG_DEBUG_OBJECTS_WORK
310
311static struct debug_obj_descr work_debug_descr;
312
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100313static void *work_debug_hint(void *addr)
314{
315 return ((struct work_struct *) addr)->func;
316}
317
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900318/*
319 * fixup_init is called when:
320 * - an active object is initialized
321 */
322static int work_fixup_init(void *addr, enum debug_obj_state state)
323{
324 struct work_struct *work = addr;
325
326 switch (state) {
327 case ODEBUG_STATE_ACTIVE:
328 cancel_work_sync(work);
329 debug_object_init(work, &work_debug_descr);
330 return 1;
331 default:
332 return 0;
333 }
334}
335
336/*
337 * fixup_activate is called when:
338 * - an active object is activated
339 * - an unknown object is activated (might be a statically initialized object)
340 */
341static int work_fixup_activate(void *addr, enum debug_obj_state state)
342{
343 struct work_struct *work = addr;
344
345 switch (state) {
346
347 case ODEBUG_STATE_NOTAVAILABLE:
348 /*
349 * This is not really a fixup. The work struct was
350 * statically initialized. We just make sure that it
351 * is tracked in the object tracker.
352 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200353 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900354 debug_object_init(work, &work_debug_descr);
355 debug_object_activate(work, &work_debug_descr);
356 return 0;
357 }
358 WARN_ON_ONCE(1);
359 return 0;
360
361 case ODEBUG_STATE_ACTIVE:
362 WARN_ON(1);
363
364 default:
365 return 0;
366 }
367}
368
369/*
370 * fixup_free is called when:
371 * - an active object is freed
372 */
373static int work_fixup_free(void *addr, enum debug_obj_state state)
374{
375 struct work_struct *work = addr;
376
377 switch (state) {
378 case ODEBUG_STATE_ACTIVE:
379 cancel_work_sync(work);
380 debug_object_free(work, &work_debug_descr);
381 return 1;
382 default:
383 return 0;
384 }
385}
386
387static struct debug_obj_descr work_debug_descr = {
388 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100389 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900390 .fixup_init = work_fixup_init,
391 .fixup_activate = work_fixup_activate,
392 .fixup_free = work_fixup_free,
393};
394
395static inline void debug_work_activate(struct work_struct *work)
396{
397 debug_object_activate(work, &work_debug_descr);
398}
399
400static inline void debug_work_deactivate(struct work_struct *work)
401{
402 debug_object_deactivate(work, &work_debug_descr);
403}
404
405void __init_work(struct work_struct *work, int onstack)
406{
407 if (onstack)
408 debug_object_init_on_stack(work, &work_debug_descr);
409 else
410 debug_object_init(work, &work_debug_descr);
411}
412EXPORT_SYMBOL_GPL(__init_work);
413
414void destroy_work_on_stack(struct work_struct *work)
415{
416 debug_object_free(work, &work_debug_descr);
417}
418EXPORT_SYMBOL_GPL(destroy_work_on_stack);
419
420#else
421static inline void debug_work_activate(struct work_struct *work) { }
422static inline void debug_work_deactivate(struct work_struct *work) { }
423#endif
424
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100425/* Serializes the accesses to the list of workqueues. */
426static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427static LIST_HEAD(workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200428static bool workqueue_freezing; /* W: have wqs started freezing? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Oleg Nesterov14441962007-05-23 13:57:57 -0700430/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200431 * The almighty global cpu workqueues. nr_running is the only field
432 * which is expected to be used frequently by other cpus via
433 * try_to_wake_up(). Put it in a separate cacheline.
Oleg Nesterov14441962007-05-23 13:57:57 -0700434 */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200435static DEFINE_PER_CPU(struct global_cwq, global_cwq);
Tejun Heoe34cdddb2013-01-24 11:01:33 -0800436static DEFINE_PER_CPU_SHARED_ALIGNED(atomic_t, pool_nr_running[NR_STD_WORKER_POOLS]);
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800437
Tejun Heof3421792010-07-02 10:03:51 +0200438/*
Tejun Heo24647572013-01-24 11:01:33 -0800439 * Global cpu workqueue and nr_running counter for unbound gcwq. The pools
440 * for online CPUs have POOL_DISASSOCIATED set, and all their workers have
441 * WORKER_UNBOUND set.
Tejun Heof3421792010-07-02 10:03:51 +0200442 */
443static struct global_cwq unbound_global_cwq;
Tejun Heoe34cdddb2013-01-24 11:01:33 -0800444static atomic_t unbound_pool_nr_running[NR_STD_WORKER_POOLS] = {
445 [0 ... NR_STD_WORKER_POOLS - 1] = ATOMIC_INIT(0), /* always 0 */
Tejun Heo4ce62e92012-07-13 22:16:44 -0700446};
Tejun Heof3421792010-07-02 10:03:51 +0200447
Tejun Heo9daf9e62013-01-24 11:01:33 -0800448/* idr of all pools */
449static DEFINE_MUTEX(worker_pool_idr_mutex);
450static DEFINE_IDR(worker_pool_idr);
451
Tejun Heoc34056a2010-06-29 10:07:11 +0200452static int worker_thread(void *__worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Tejun Heoe34cdddb2013-01-24 11:01:33 -0800454static int std_worker_pool_pri(struct worker_pool *pool)
Tejun Heo32704762012-07-13 22:16:45 -0700455{
456 return pool - pool->gcwq->pools;
457}
458
Tejun Heo8b03ae32010-06-29 10:07:12 +0200459static struct global_cwq *get_gcwq(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Tejun Heof3421792010-07-02 10:03:51 +0200461 if (cpu != WORK_CPU_UNBOUND)
462 return &per_cpu(global_cwq, cpu);
463 else
464 return &unbound_global_cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
Tejun Heo9daf9e62013-01-24 11:01:33 -0800467/* allocate ID and assign it to @pool */
468static int worker_pool_assign_id(struct worker_pool *pool)
469{
470 int ret;
471
472 mutex_lock(&worker_pool_idr_mutex);
473 idr_pre_get(&worker_pool_idr, GFP_KERNEL);
474 ret = idr_get_new(&worker_pool_idr, pool, &pool->id);
475 mutex_unlock(&worker_pool_idr_mutex);
476
477 return ret;
478}
479
Tejun Heo7c3eed52013-01-24 11:01:33 -0800480/*
481 * Lookup worker_pool by id. The idr currently is built during boot and
482 * never modified. Don't worry about locking for now.
483 */
484static struct worker_pool *worker_pool_by_id(int pool_id)
485{
486 return idr_find(&worker_pool_idr, pool_id);
487}
488
Tejun Heod565ed62013-01-24 11:01:33 -0800489static struct worker_pool *get_std_worker_pool(int cpu, bool highpri)
490{
491 struct global_cwq *gcwq = get_gcwq(cpu);
492
493 return &gcwq->pools[highpri];
494}
495
Tejun Heo63d95a92012-07-12 14:46:37 -0700496static atomic_t *get_pool_nr_running(struct worker_pool *pool)
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700497{
Tejun Heoec22ca52013-01-24 11:01:33 -0800498 int cpu = pool->cpu;
Tejun Heoe34cdddb2013-01-24 11:01:33 -0800499 int idx = std_worker_pool_pri(pool);
Tejun Heo63d95a92012-07-12 14:46:37 -0700500
Tejun Heof3421792010-07-02 10:03:51 +0200501 if (cpu != WORK_CPU_UNBOUND)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700502 return &per_cpu(pool_nr_running, cpu)[idx];
Tejun Heof3421792010-07-02 10:03:51 +0200503 else
Tejun Heo4ce62e92012-07-13 22:16:44 -0700504 return &unbound_pool_nr_running[idx];
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700505}
506
Tejun Heo4690c4a2010-06-29 10:07:10 +0200507static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
508 struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700509{
Tejun Heof3421792010-07-02 10:03:51 +0200510 if (!(wq->flags & WQ_UNBOUND)) {
Lai Jiangshane06ffa12012-03-09 18:03:20 +0800511 if (likely(cpu < nr_cpu_ids))
Tejun Heof3421792010-07-02 10:03:51 +0200512 return per_cpu_ptr(wq->cpu_wq.pcpu, cpu);
Tejun Heof3421792010-07-02 10:03:51 +0200513 } else if (likely(cpu == WORK_CPU_UNBOUND))
514 return wq->cpu_wq.single;
515 return NULL;
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700516}
517
Tejun Heo73f53c42010-06-29 10:07:11 +0200518static unsigned int work_color_to_flags(int color)
519{
520 return color << WORK_STRUCT_COLOR_SHIFT;
521}
522
523static int get_work_color(struct work_struct *work)
524{
525 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
526 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
527}
528
529static int work_next_color(int color)
530{
531 return (color + 1) % WORK_NR_COLORS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
David Howells4594bf12006-12-07 11:33:26 +0000534/*
Tejun Heob5490072012-08-03 10:30:46 -0700535 * While queued, %WORK_STRUCT_CWQ is set and non flag bits of a work's data
536 * contain the pointer to the queued cwq. Once execution starts, the flag
Tejun Heo7c3eed52013-01-24 11:01:33 -0800537 * is cleared and the high bits contain OFFQ flags and pool ID.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200538 *
Tejun Heo7c3eed52013-01-24 11:01:33 -0800539 * set_work_cwq(), set_work_pool_and_clear_pending(), mark_work_canceling()
540 * and clear_work_data() can be used to set the cwq, pool or clear
Tejun Heobbb68df2012-08-03 10:30:46 -0700541 * work->data. These functions should only be called while the work is
542 * owned - ie. while the PENDING bit is set.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200543 *
Tejun Heo7c3eed52013-01-24 11:01:33 -0800544 * get_work_pool() and get_work_cwq() can be used to obtain the pool or cwq
545 * corresponding to a work. Pool is available once the work has been
546 * queued anywhere after initialization until it is sync canceled. cwq is
547 * available only while the work item is queued.
Tejun Heobbb68df2012-08-03 10:30:46 -0700548 *
549 * %WORK_OFFQ_CANCELING is used to mark a work item which is being
550 * canceled. While being canceled, a work item may have its PENDING set
551 * but stay off timer and worklist for arbitrarily long and nobody should
552 * try to steal the PENDING bit.
David Howells4594bf12006-12-07 11:33:26 +0000553 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200554static inline void set_work_data(struct work_struct *work, unsigned long data,
555 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000556{
David Howells4594bf12006-12-07 11:33:26 +0000557 BUG_ON(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200558 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000559}
David Howells365970a2006-11-22 14:54:49 +0000560
Tejun Heo7a22ad72010-06-29 10:07:13 +0200561static void set_work_cwq(struct work_struct *work,
562 struct cpu_workqueue_struct *cwq,
563 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200564{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200565 set_work_data(work, (unsigned long)cwq,
Tejun Heoe1201532010-07-22 14:14:25 +0200566 WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200567}
568
Tejun Heo7c3eed52013-01-24 11:01:33 -0800569static void set_work_pool_and_clear_pending(struct work_struct *work,
570 int pool_id)
David Howells365970a2006-11-22 14:54:49 +0000571{
Tejun Heo23657bb2012-08-13 17:08:19 -0700572 /*
573 * The following wmb is paired with the implied mb in
574 * test_and_set_bit(PENDING) and ensures all updates to @work made
575 * here are visible to and precede any updates by the next PENDING
576 * owner.
577 */
578 smp_wmb();
Tejun Heo7c3eed52013-01-24 11:01:33 -0800579 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200580}
581
582static void clear_work_data(struct work_struct *work)
583{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800584 smp_wmb(); /* see set_work_pool_and_clear_pending() */
585 set_work_data(work, WORK_STRUCT_NO_POOL, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200586}
587
Tejun Heo7a22ad72010-06-29 10:07:13 +0200588static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work)
589{
Tejun Heoe1201532010-07-22 14:14:25 +0200590 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200591
Tejun Heoe1201532010-07-22 14:14:25 +0200592 if (data & WORK_STRUCT_CWQ)
593 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
594 else
595 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200596}
597
Tejun Heo7c3eed52013-01-24 11:01:33 -0800598/**
599 * get_work_pool - return the worker_pool a given work was associated with
600 * @work: the work item of interest
601 *
602 * Return the worker_pool @work was last associated with. %NULL if none.
603 */
604static struct worker_pool *get_work_pool(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200605{
Tejun Heoe1201532010-07-22 14:14:25 +0200606 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800607 struct worker_pool *pool;
608 int pool_id;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200609
Tejun Heoe1201532010-07-22 14:14:25 +0200610 if (data & WORK_STRUCT_CWQ)
611 return ((struct cpu_workqueue_struct *)
Tejun Heo7c3eed52013-01-24 11:01:33 -0800612 (data & WORK_STRUCT_WQ_DATA_MASK))->pool;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200613
Tejun Heo7c3eed52013-01-24 11:01:33 -0800614 pool_id = data >> WORK_OFFQ_POOL_SHIFT;
615 if (pool_id == WORK_OFFQ_POOL_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200616 return NULL;
617
Tejun Heo7c3eed52013-01-24 11:01:33 -0800618 pool = worker_pool_by_id(pool_id);
619 WARN_ON_ONCE(!pool);
620 return pool;
621}
622
623/**
624 * get_work_pool_id - return the worker pool ID a given work is associated with
625 * @work: the work item of interest
626 *
627 * Return the worker_pool ID @work was last associated with.
628 * %WORK_OFFQ_POOL_NONE if none.
629 */
630static int get_work_pool_id(struct work_struct *work)
631{
632 struct worker_pool *pool = get_work_pool(work);
633
634 return pool ? pool->id : WORK_OFFQ_POOL_NONE;
635}
636
Tejun Heobbb68df2012-08-03 10:30:46 -0700637static void mark_work_canceling(struct work_struct *work)
638{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800639 unsigned long pool_id = get_work_pool_id(work);
Tejun Heobbb68df2012-08-03 10:30:46 -0700640
Tejun Heo7c3eed52013-01-24 11:01:33 -0800641 pool_id <<= WORK_OFFQ_POOL_SHIFT;
642 set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700643}
644
645static bool work_is_canceling(struct work_struct *work)
646{
647 unsigned long data = atomic_long_read(&work->data);
648
649 return !(data & WORK_STRUCT_CWQ) && (data & WORK_OFFQ_CANCELING);
650}
651
David Howells365970a2006-11-22 14:54:49 +0000652/*
Tejun Heo32704762012-07-13 22:16:45 -0700653 * Policy functions. These define the policies on how the global worker
654 * pools are managed. Unless noted otherwise, these functions assume that
Tejun Heod565ed62013-01-24 11:01:33 -0800655 * they're being called with pool->lock held.
David Howells365970a2006-11-22 14:54:49 +0000656 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200657
Tejun Heo63d95a92012-07-12 14:46:37 -0700658static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000659{
Tejun Heo32704762012-07-13 22:16:45 -0700660 return !atomic_read(get_pool_nr_running(pool));
David Howells365970a2006-11-22 14:54:49 +0000661}
662
Tejun Heoe22bee72010-06-29 10:07:14 +0200663/*
664 * Need to wake up a worker? Called from anything but currently
665 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700666 *
667 * Note that, because unbound workers never contribute to nr_running, this
668 * function will always return %true for unbound gcwq as long as the
669 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200670 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700671static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000672{
Tejun Heo63d95a92012-07-12 14:46:37 -0700673 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000674}
675
Tejun Heoe22bee72010-06-29 10:07:14 +0200676/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700677static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200678{
Tejun Heo63d95a92012-07-12 14:46:37 -0700679 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200680}
681
682/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700683static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200684{
Tejun Heo63d95a92012-07-12 14:46:37 -0700685 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200686
Tejun Heo32704762012-07-13 22:16:45 -0700687 return !list_empty(&pool->worklist) && atomic_read(nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200688}
689
690/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700691static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200692{
Tejun Heo63d95a92012-07-12 14:46:37 -0700693 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200694}
695
696/* Do I need to be the manager? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700697static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200698{
Tejun Heo63d95a92012-07-12 14:46:37 -0700699 return need_to_create_worker(pool) ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700700 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200701}
702
703/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700704static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200705{
Lai Jiangshan552a37e2012-09-10 10:03:33 -0700706 bool managing = pool->flags & POOL_MANAGING_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -0700707 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
708 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200709
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700710 /*
711 * nr_idle and idle_list may disagree if idle rebinding is in
712 * progress. Never return %true if idle_list is empty.
713 */
714 if (list_empty(&pool->idle_list))
715 return false;
716
Tejun Heoe22bee72010-06-29 10:07:14 +0200717 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
718}
719
720/*
721 * Wake up functions.
722 */
723
Tejun Heo7e116292010-06-29 10:07:13 +0200724/* Return the first worker. Safe with preemption disabled */
Tejun Heo63d95a92012-07-12 14:46:37 -0700725static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200726{
Tejun Heo63d95a92012-07-12 14:46:37 -0700727 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200728 return NULL;
729
Tejun Heo63d95a92012-07-12 14:46:37 -0700730 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200731}
732
733/**
734 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700735 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200736 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700737 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200738 *
739 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800740 * spin_lock_irq(pool->lock).
Tejun Heo7e116292010-06-29 10:07:13 +0200741 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700742static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200743{
Tejun Heo63d95a92012-07-12 14:46:37 -0700744 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200745
746 if (likely(worker))
747 wake_up_process(worker->task);
748}
749
Tejun Heo4690c4a2010-06-29 10:07:10 +0200750/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200751 * wq_worker_waking_up - a worker is waking up
752 * @task: task waking up
753 * @cpu: CPU @task is waking up to
754 *
755 * This function is called during try_to_wake_up() when a worker is
756 * being awoken.
757 *
758 * CONTEXT:
759 * spin_lock_irq(rq->lock)
760 */
761void wq_worker_waking_up(struct task_struct *task, unsigned int cpu)
762{
763 struct worker *worker = kthread_data(task);
764
Joonsoo Kim36576002012-10-26 23:03:49 +0900765 if (!(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoec22ca52013-01-24 11:01:33 -0800766 WARN_ON_ONCE(worker->pool->cpu != cpu);
Tejun Heo63d95a92012-07-12 14:46:37 -0700767 atomic_inc(get_pool_nr_running(worker->pool));
Joonsoo Kim36576002012-10-26 23:03:49 +0900768 }
Tejun Heoe22bee72010-06-29 10:07:14 +0200769}
770
771/**
772 * wq_worker_sleeping - a worker is going to sleep
773 * @task: task going to sleep
774 * @cpu: CPU in question, must be the current CPU number
775 *
776 * This function is called during schedule() when a busy worker is
777 * going to sleep. Worker on the same cpu can be woken up by
778 * returning pointer to its task.
779 *
780 * CONTEXT:
781 * spin_lock_irq(rq->lock)
782 *
783 * RETURNS:
784 * Worker task on @cpu to wake up, %NULL if none.
785 */
786struct task_struct *wq_worker_sleeping(struct task_struct *task,
787 unsigned int cpu)
788{
789 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heo111c2252013-01-17 17:16:24 -0800790 struct worker_pool *pool;
791 atomic_t *nr_running;
Tejun Heoe22bee72010-06-29 10:07:14 +0200792
Tejun Heo111c2252013-01-17 17:16:24 -0800793 /*
794 * Rescuers, which may not have all the fields set up like normal
795 * workers, also reach here, let's not access anything before
796 * checking NOT_RUNNING.
797 */
Steven Rostedt2d646722010-12-03 23:12:33 -0500798 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200799 return NULL;
800
Tejun Heo111c2252013-01-17 17:16:24 -0800801 pool = worker->pool;
802 nr_running = get_pool_nr_running(pool);
803
Tejun Heoe22bee72010-06-29 10:07:14 +0200804 /* this can only happen on the local cpu */
805 BUG_ON(cpu != raw_smp_processor_id());
806
807 /*
808 * The counterpart of the following dec_and_test, implied mb,
809 * worklist not empty test sequence is in insert_work().
810 * Please read comment there.
811 *
Tejun Heo628c78e2012-07-17 12:39:27 -0700812 * NOT_RUNNING is clear. This means that we're bound to and
813 * running on the local cpu w/ rq lock held and preemption
814 * disabled, which in turn means that none else could be
Tejun Heod565ed62013-01-24 11:01:33 -0800815 * manipulating idle_list, so dereferencing idle_list without pool
Tejun Heo628c78e2012-07-17 12:39:27 -0700816 * lock is safe.
Tejun Heoe22bee72010-06-29 10:07:14 +0200817 */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700818 if (atomic_dec_and_test(nr_running) && !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700819 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200820 return to_wakeup ? to_wakeup->task : NULL;
821}
822
823/**
824 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200825 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200826 * @flags: flags to set
827 * @wakeup: wakeup an idle worker if necessary
828 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200829 * Set @flags in @worker->flags and adjust nr_running accordingly. If
830 * nr_running becomes zero and @wakeup is %true, an idle worker is
831 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200832 *
Tejun Heocb444762010-07-02 10:03:50 +0200833 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800834 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200835 */
836static inline void worker_set_flags(struct worker *worker, unsigned int flags,
837 bool wakeup)
838{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700839 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200840
Tejun Heocb444762010-07-02 10:03:50 +0200841 WARN_ON_ONCE(worker->task != current);
842
Tejun Heoe22bee72010-06-29 10:07:14 +0200843 /*
844 * If transitioning into NOT_RUNNING, adjust nr_running and
845 * wake up an idle worker as necessary if requested by
846 * @wakeup.
847 */
848 if ((flags & WORKER_NOT_RUNNING) &&
849 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heo63d95a92012-07-12 14:46:37 -0700850 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200851
852 if (wakeup) {
853 if (atomic_dec_and_test(nr_running) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -0700854 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700855 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200856 } else
857 atomic_dec(nr_running);
858 }
859
Tejun Heod302f012010-06-29 10:07:13 +0200860 worker->flags |= flags;
861}
862
863/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200864 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200865 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200866 * @flags: flags to clear
867 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200868 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200869 *
Tejun Heocb444762010-07-02 10:03:50 +0200870 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800871 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200872 */
873static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
874{
Tejun Heo63d95a92012-07-12 14:46:37 -0700875 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200876 unsigned int oflags = worker->flags;
877
Tejun Heocb444762010-07-02 10:03:50 +0200878 WARN_ON_ONCE(worker->task != current);
879
Tejun Heod302f012010-06-29 10:07:13 +0200880 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200881
Tejun Heo42c025f2011-01-11 15:58:49 +0100882 /*
883 * If transitioning out of NOT_RUNNING, increment nr_running. Note
884 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
885 * of multiple flags, not a single flag.
886 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200887 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
888 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heo63d95a92012-07-12 14:46:37 -0700889 atomic_inc(get_pool_nr_running(pool));
Tejun Heod302f012010-06-29 10:07:13 +0200890}
891
892/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200893 * find_worker_executing_work - find worker which is executing a work
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800894 * @pool: pool of interest
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200895 * @work: work to find worker for
896 *
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800897 * Find a worker which is executing @work on @pool by searching
898 * @pool->busy_hash which is keyed by the address of @work. For a worker
Tejun Heoa2c1c572012-12-18 10:35:02 -0800899 * to match, its current execution should match the address of @work and
900 * its work function. This is to avoid unwanted dependency between
901 * unrelated work executions through a work item being recycled while still
902 * being executed.
903 *
904 * This is a bit tricky. A work item may be freed once its execution
905 * starts and nothing prevents the freed area from being recycled for
906 * another work item. If the same work item address ends up being reused
907 * before the original execution finishes, workqueue will identify the
908 * recycled work item as currently executing and make it wait until the
909 * current execution finishes, introducing an unwanted dependency.
910 *
911 * This function checks the work item address, work function and workqueue
912 * to avoid false positives. Note that this isn't complete as one may
913 * construct a work function which can introduce dependency onto itself
914 * through a recycled work item. Well, if somebody wants to shoot oneself
915 * in the foot that badly, there's only so much we can do, and if such
916 * deadlock actually occurs, it should be easy to locate the culprit work
917 * function.
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200918 *
919 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800920 * spin_lock_irq(pool->lock).
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200921 *
922 * RETURNS:
923 * Pointer to worker which is executing @work if found, NULL
924 * otherwise.
925 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800926static struct worker *find_worker_executing_work(struct worker_pool *pool,
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200927 struct work_struct *work)
928{
Sasha Levin42f85702012-12-17 10:01:23 -0500929 struct worker *worker;
930 struct hlist_node *tmp;
931
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800932 hash_for_each_possible(pool->busy_hash, worker, tmp, hentry,
Tejun Heoa2c1c572012-12-18 10:35:02 -0800933 (unsigned long)work)
934 if (worker->current_work == work &&
935 worker->current_func == work->func)
Sasha Levin42f85702012-12-17 10:01:23 -0500936 return worker;
937
938 return NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200939}
940
941/**
Tejun Heobf4ede02012-08-03 10:30:46 -0700942 * move_linked_works - move linked works to a list
943 * @work: start of series of works to be scheduled
944 * @head: target list to append @work to
945 * @nextp: out paramter for nested worklist walking
946 *
947 * Schedule linked works starting from @work to @head. Work series to
948 * be scheduled starts at @work and includes any consecutive work with
949 * WORK_STRUCT_LINKED set in its predecessor.
950 *
951 * If @nextp is not NULL, it's updated to point to the next work of
952 * the last scheduled work. This allows move_linked_works() to be
953 * nested inside outer list_for_each_entry_safe().
954 *
955 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800956 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -0700957 */
958static void move_linked_works(struct work_struct *work, struct list_head *head,
959 struct work_struct **nextp)
960{
961 struct work_struct *n;
962
963 /*
964 * Linked worklist will always end before the end of the list,
965 * use NULL for list head.
966 */
967 list_for_each_entry_safe_from(work, n, NULL, entry) {
968 list_move_tail(&work->entry, head);
969 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
970 break;
971 }
972
973 /*
974 * If we're already inside safe list traversal and have moved
975 * multiple works to the scheduled queue, the next position
976 * needs to be updated.
977 */
978 if (nextp)
979 *nextp = n;
980}
981
Lai Jiangshan3aa62492012-09-18 10:40:00 -0700982static void cwq_activate_delayed_work(struct work_struct *work)
Tejun Heobf4ede02012-08-03 10:30:46 -0700983{
Lai Jiangshan3aa62492012-09-18 10:40:00 -0700984 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heobf4ede02012-08-03 10:30:46 -0700985
986 trace_workqueue_activate_work(work);
987 move_linked_works(work, &cwq->pool->worklist, NULL);
988 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
989 cwq->nr_active++;
990}
991
Lai Jiangshan3aa62492012-09-18 10:40:00 -0700992static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
993{
994 struct work_struct *work = list_first_entry(&cwq->delayed_works,
995 struct work_struct, entry);
996
997 cwq_activate_delayed_work(work);
998}
999
Tejun Heobf4ede02012-08-03 10:30:46 -07001000/**
1001 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
1002 * @cwq: cwq of interest
1003 * @color: color of work which left the queue
Tejun Heobf4ede02012-08-03 10:30:46 -07001004 *
1005 * A work either has completed or is removed from pending queue,
1006 * decrement nr_in_flight of its cwq and handle workqueue flushing.
1007 *
1008 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001009 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -07001010 */
Lai Jiangshanb3f9f402012-09-18 10:40:00 -07001011static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color)
Tejun Heobf4ede02012-08-03 10:30:46 -07001012{
1013 /* ignore uncolored works */
1014 if (color == WORK_NO_COLOR)
1015 return;
1016
1017 cwq->nr_in_flight[color]--;
1018
Lai Jiangshanb3f9f402012-09-18 10:40:00 -07001019 cwq->nr_active--;
1020 if (!list_empty(&cwq->delayed_works)) {
1021 /* one down, submit a delayed one */
1022 if (cwq->nr_active < cwq->max_active)
1023 cwq_activate_first_delayed(cwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001024 }
1025
1026 /* is flush in progress and are we at the flushing tip? */
1027 if (likely(cwq->flush_color != color))
1028 return;
1029
1030 /* are there still in-flight works? */
1031 if (cwq->nr_in_flight[color])
1032 return;
1033
1034 /* this cwq is done, clear flush_color */
1035 cwq->flush_color = -1;
1036
1037 /*
1038 * If this was the last cwq, wake up the first flusher. It
1039 * will handle the rest.
1040 */
1041 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
1042 complete(&cwq->wq->first_flusher->done);
1043}
1044
Tejun Heo36e227d2012-08-03 10:30:46 -07001045/**
Tejun Heobbb68df2012-08-03 10:30:46 -07001046 * try_to_grab_pending - steal work item from worklist and disable irq
Tejun Heo36e227d2012-08-03 10:30:46 -07001047 * @work: work item to steal
1048 * @is_dwork: @work is a delayed_work
Tejun Heobbb68df2012-08-03 10:30:46 -07001049 * @flags: place to store irq state
Tejun Heo36e227d2012-08-03 10:30:46 -07001050 *
1051 * Try to grab PENDING bit of @work. This function can handle @work in any
1052 * stable state - idle, on timer or on worklist. Return values are
1053 *
1054 * 1 if @work was pending and we successfully stole PENDING
1055 * 0 if @work was idle and we claimed PENDING
1056 * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry
Tejun Heobbb68df2012-08-03 10:30:46 -07001057 * -ENOENT if someone else is canceling @work, this state may persist
1058 * for arbitrarily long
Tejun Heo36e227d2012-08-03 10:30:46 -07001059 *
Tejun Heobbb68df2012-08-03 10:30:46 -07001060 * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001061 * interrupted while holding PENDING and @work off queue, irq must be
1062 * disabled on entry. This, combined with delayed_work->timer being
1063 * irqsafe, ensures that we return -EAGAIN for finite short period of time.
Tejun Heobbb68df2012-08-03 10:30:46 -07001064 *
1065 * On successful return, >= 0, irq is disabled and the caller is
1066 * responsible for releasing it using local_irq_restore(*@flags).
1067 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001068 * This function is safe to call from any context including IRQ handler.
Tejun Heobf4ede02012-08-03 10:30:46 -07001069 */
Tejun Heobbb68df2012-08-03 10:30:46 -07001070static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
1071 unsigned long *flags)
Tejun Heobf4ede02012-08-03 10:30:46 -07001072{
Tejun Heod565ed62013-01-24 11:01:33 -08001073 struct worker_pool *pool;
Tejun Heobf4ede02012-08-03 10:30:46 -07001074
Tejun Heobbb68df2012-08-03 10:30:46 -07001075 local_irq_save(*flags);
1076
Tejun Heo36e227d2012-08-03 10:30:46 -07001077 /* try to steal the timer if it exists */
1078 if (is_dwork) {
1079 struct delayed_work *dwork = to_delayed_work(work);
1080
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001081 /*
1082 * dwork->timer is irqsafe. If del_timer() fails, it's
1083 * guaranteed that the timer is not queued anywhere and not
1084 * running on the local CPU.
1085 */
Tejun Heo36e227d2012-08-03 10:30:46 -07001086 if (likely(del_timer(&dwork->timer)))
1087 return 1;
1088 }
1089
1090 /* try to claim PENDING the normal way */
Tejun Heobf4ede02012-08-03 10:30:46 -07001091 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1092 return 0;
1093
1094 /*
1095 * The queueing is in progress, or it is already queued. Try to
1096 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1097 */
Tejun Heod565ed62013-01-24 11:01:33 -08001098 pool = get_work_pool(work);
1099 if (!pool)
Tejun Heobbb68df2012-08-03 10:30:46 -07001100 goto fail;
Tejun Heobf4ede02012-08-03 10:30:46 -07001101
Tejun Heod565ed62013-01-24 11:01:33 -08001102 spin_lock(&pool->lock);
Tejun Heobf4ede02012-08-03 10:30:46 -07001103 if (!list_empty(&work->entry)) {
1104 /*
Tejun Heod565ed62013-01-24 11:01:33 -08001105 * This work is queued, but perhaps we locked the wrong
1106 * pool. In that case we must see the new value after
1107 * rmb(), see insert_work()->wmb().
Tejun Heobf4ede02012-08-03 10:30:46 -07001108 */
1109 smp_rmb();
Tejun Heod565ed62013-01-24 11:01:33 -08001110 if (pool == get_work_pool(work)) {
Tejun Heobf4ede02012-08-03 10:30:46 -07001111 debug_work_deactivate(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001112
1113 /*
1114 * A delayed work item cannot be grabbed directly
1115 * because it might have linked NO_COLOR work items
1116 * which, if left on the delayed_list, will confuse
1117 * cwq->nr_active management later on and cause
1118 * stall. Make sure the work item is activated
1119 * before grabbing.
1120 */
1121 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
1122 cwq_activate_delayed_work(work);
1123
Tejun Heobf4ede02012-08-03 10:30:46 -07001124 list_del_init(&work->entry);
1125 cwq_dec_nr_in_flight(get_work_cwq(work),
Lai Jiangshanb3f9f402012-09-18 10:40:00 -07001126 get_work_color(work));
Tejun Heo36e227d2012-08-03 10:30:46 -07001127
Tejun Heod565ed62013-01-24 11:01:33 -08001128 spin_unlock(&pool->lock);
Tejun Heo36e227d2012-08-03 10:30:46 -07001129 return 1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001130 }
1131 }
Tejun Heod565ed62013-01-24 11:01:33 -08001132 spin_unlock(&pool->lock);
Tejun Heobbb68df2012-08-03 10:30:46 -07001133fail:
1134 local_irq_restore(*flags);
1135 if (work_is_canceling(work))
1136 return -ENOENT;
1137 cpu_relax();
Tejun Heo36e227d2012-08-03 10:30:46 -07001138 return -EAGAIN;
Tejun Heobf4ede02012-08-03 10:30:46 -07001139}
1140
1141/**
Tejun Heo7e116292010-06-29 10:07:13 +02001142 * insert_work - insert a work into gcwq
Tejun Heo4690c4a2010-06-29 10:07:10 +02001143 * @cwq: cwq @work belongs to
1144 * @work: work to insert
1145 * @head: insertion point
1146 * @extra_flags: extra WORK_STRUCT_* flags to set
1147 *
Tejun Heo7e116292010-06-29 10:07:13 +02001148 * Insert @work which belongs to @cwq into @gcwq after @head.
1149 * @extra_flags is or'd to work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +02001150 *
1151 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001152 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02001153 */
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001154static void insert_work(struct cpu_workqueue_struct *cwq,
Tejun Heo4690c4a2010-06-29 10:07:10 +02001155 struct work_struct *work, struct list_head *head,
1156 unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001157{
Tejun Heo63d95a92012-07-12 14:46:37 -07001158 struct worker_pool *pool = cwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01001159
Tejun Heo4690c4a2010-06-29 10:07:10 +02001160 /* we own @work, set data and link */
Tejun Heo7a22ad72010-06-29 10:07:13 +02001161 set_work_cwq(work, cwq, extra_flags);
Tejun Heo4690c4a2010-06-29 10:07:10 +02001162
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001163 /*
1164 * Ensure that we get the right work->data if we see the
1165 * result of list_add() below, see try_to_grab_pending().
1166 */
1167 smp_wmb();
Tejun Heo4690c4a2010-06-29 10:07:10 +02001168
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -07001169 list_add_tail(&work->entry, head);
Tejun Heoe22bee72010-06-29 10:07:14 +02001170
1171 /*
1172 * Ensure either worker_sched_deactivated() sees the above
1173 * list_add_tail() or we see zero nr_running to avoid workers
1174 * lying around lazily while there are works to be processed.
1175 */
1176 smp_mb();
1177
Tejun Heo63d95a92012-07-12 14:46:37 -07001178 if (__need_more_worker(pool))
1179 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001180}
1181
Tejun Heoc8efcc22010-12-20 19:32:04 +01001182/*
1183 * Test whether @work is being queued from another work executing on the
1184 * same workqueue. This is rather expensive and should only be used from
1185 * cold paths.
1186 */
1187static bool is_chained_work(struct workqueue_struct *wq)
1188{
1189 unsigned long flags;
1190 unsigned int cpu;
1191
1192 for_each_gcwq_cpu(cpu) {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001193 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
1194 struct worker_pool *pool = cwq->pool;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001195 struct worker *worker;
1196 struct hlist_node *pos;
1197 int i;
1198
Tejun Heod565ed62013-01-24 11:01:33 -08001199 spin_lock_irqsave(&pool->lock, flags);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001200 for_each_busy_worker(worker, i, pos, pool) {
Tejun Heoc8efcc22010-12-20 19:32:04 +01001201 if (worker->task != current)
1202 continue;
Tejun Heod565ed62013-01-24 11:01:33 -08001203 spin_unlock_irqrestore(&pool->lock, flags);
Tejun Heoc8efcc22010-12-20 19:32:04 +01001204 /*
1205 * I'm @worker, no locking necessary. See if @work
1206 * is headed to the same workqueue.
1207 */
1208 return worker->current_cwq->wq == wq;
1209 }
Tejun Heod565ed62013-01-24 11:01:33 -08001210 spin_unlock_irqrestore(&pool->lock, flags);
Tejun Heoc8efcc22010-12-20 19:32:04 +01001211 }
1212 return false;
1213}
1214
Tejun Heo4690c4a2010-06-29 10:07:10 +02001215static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 struct work_struct *work)
1217{
Tejun Heod565ed62013-01-24 11:01:33 -08001218 bool highpri = wq->flags & WQ_HIGHPRI;
1219 struct worker_pool *pool;
Tejun Heo502ca9d2010-06-29 10:07:13 +02001220 struct cpu_workqueue_struct *cwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001221 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001222 unsigned int work_flags;
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001223 unsigned int req_cpu = cpu;
Tejun Heo8930cab2012-08-03 10:30:45 -07001224
1225 /*
1226 * While a work item is PENDING && off queue, a task trying to
1227 * steal the PENDING will busy-loop waiting for it to either get
1228 * queued or lose PENDING. Grabbing PENDING and queueing should
1229 * happen with IRQ disabled.
1230 */
1231 WARN_ON_ONCE(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001233 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001234
Tejun Heoc8efcc22010-12-20 19:32:04 +01001235 /* if dying, only works from the same workqueue are allowed */
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02001236 if (unlikely(wq->flags & WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001237 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001238 return;
1239
Tejun Heod565ed62013-01-24 11:01:33 -08001240 /* determine pool to use */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001241 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001242 struct worker_pool *last_pool;
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001243
Tejun Heo57469822012-08-03 10:30:45 -07001244 if (cpu == WORK_CPU_UNBOUND)
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001245 cpu = raw_smp_processor_id();
1246
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001247 /*
Tejun Heodbf25762012-08-20 14:51:23 -07001248 * It's multi cpu. If @work was previously on a different
1249 * cpu, it might still be running there, in which case the
1250 * work needs to be queued on that cpu to guarantee
1251 * non-reentrancy.
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001252 */
Tejun Heod565ed62013-01-24 11:01:33 -08001253 pool = get_std_worker_pool(cpu, highpri);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001254 last_pool = get_work_pool(work);
Tejun Heodbf25762012-08-20 14:51:23 -07001255
Tejun Heod565ed62013-01-24 11:01:33 -08001256 if (last_pool && last_pool != pool) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001257 struct worker *worker;
1258
Tejun Heod565ed62013-01-24 11:01:33 -08001259 spin_lock(&last_pool->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001260
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001261 worker = find_worker_executing_work(last_pool, work);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001262
1263 if (worker && worker->current_cwq->wq == wq)
Tejun Heod565ed62013-01-24 11:01:33 -08001264 pool = last_pool;
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001265 else {
1266 /* meh... not running there, queue here */
Tejun Heod565ed62013-01-24 11:01:33 -08001267 spin_unlock(&last_pool->lock);
1268 spin_lock(&pool->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001269 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001270 } else {
Tejun Heod565ed62013-01-24 11:01:33 -08001271 spin_lock(&pool->lock);
Tejun Heo8930cab2012-08-03 10:30:45 -07001272 }
Tejun Heof3421792010-07-02 10:03:51 +02001273 } else {
Tejun Heod565ed62013-01-24 11:01:33 -08001274 pool = get_std_worker_pool(WORK_CPU_UNBOUND, highpri);
1275 spin_lock(&pool->lock);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001276 }
1277
Tejun Heod565ed62013-01-24 11:01:33 -08001278 /* pool determined, get cwq and queue */
1279 cwq = get_cwq(pool->cpu, wq);
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001280 trace_workqueue_queue_work(req_cpu, cwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001281
Dan Carpenterf5b25522012-04-13 22:06:58 +03001282 if (WARN_ON(!list_empty(&work->entry))) {
Tejun Heod565ed62013-01-24 11:01:33 -08001283 spin_unlock(&pool->lock);
Dan Carpenterf5b25522012-04-13 22:06:58 +03001284 return;
1285 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001286
Tejun Heo73f53c42010-06-29 10:07:11 +02001287 cwq->nr_in_flight[cwq->work_color]++;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001288 work_flags = work_color_to_flags(cwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001289
1290 if (likely(cwq->nr_active < cwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001291 trace_workqueue_activate_work(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001292 cwq->nr_active++;
Tejun Heo32704762012-07-13 22:16:45 -07001293 worklist = &cwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001294 } else {
1295 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001296 worklist = &cwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001297 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001298
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001299 insert_work(cwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001300
Tejun Heod565ed62013-01-24 11:01:33 -08001301 spin_unlock(&pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302}
1303
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001304/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07001305 * queue_work_on - queue work on specific cpu
1306 * @cpu: CPU number to execute work on
1307 * @wq: workqueue to use
1308 * @work: work to queue
1309 *
Tejun Heod4283e92012-08-03 10:30:44 -07001310 * Returns %false if @work was already on a queue, %true otherwise.
Zhang Ruic1a220e2008-07-23 21:28:39 -07001311 *
1312 * We queue the work to a specific CPU, the caller must ensure it
1313 * can't go away.
1314 */
Tejun Heod4283e92012-08-03 10:30:44 -07001315bool queue_work_on(int cpu, struct workqueue_struct *wq,
1316 struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07001317{
Tejun Heod4283e92012-08-03 10:30:44 -07001318 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001319 unsigned long flags;
1320
1321 local_irq_save(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001322
Tejun Heo22df02b2010-06-29 10:07:10 +02001323 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001324 __queue_work(cpu, wq, work);
Tejun Heod4283e92012-08-03 10:30:44 -07001325 ret = true;
Zhang Ruic1a220e2008-07-23 21:28:39 -07001326 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001327
1328 local_irq_restore(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001329 return ret;
1330}
1331EXPORT_SYMBOL_GPL(queue_work_on);
1332
Tejun Heo0a13c002012-08-03 10:30:44 -07001333/**
1334 * queue_work - queue work on a workqueue
1335 * @wq: workqueue to use
1336 * @work: work to queue
1337 *
Tejun Heod4283e92012-08-03 10:30:44 -07001338 * Returns %false if @work was already on a queue, %true otherwise.
Tejun Heo0a13c002012-08-03 10:30:44 -07001339 *
1340 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1341 * it can be processed by another CPU.
1342 */
Tejun Heod4283e92012-08-03 10:30:44 -07001343bool queue_work(struct workqueue_struct *wq, struct work_struct *work)
Tejun Heo0a13c002012-08-03 10:30:44 -07001344{
Tejun Heo57469822012-08-03 10:30:45 -07001345 return queue_work_on(WORK_CPU_UNBOUND, wq, work);
Tejun Heo0a13c002012-08-03 10:30:44 -07001346}
1347EXPORT_SYMBOL_GPL(queue_work);
1348
Tejun Heod8e794d2012-08-03 10:30:45 -07001349void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
David Howells52bad642006-11-22 14:54:01 +00001351 struct delayed_work *dwork = (struct delayed_work *)__data;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001352 struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001354 /* should have been called from irqsafe timer with irq already off */
Tejun Heo12650572012-08-08 09:38:42 -07001355 __queue_work(dwork->cpu, cwq->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356}
Tejun Heod8e794d2012-08-03 10:30:45 -07001357EXPORT_SYMBOL_GPL(delayed_work_timer_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001359static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
1360 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001362 struct timer_list *timer = &dwork->timer;
1363 struct work_struct *work = &dwork->work;
1364 unsigned int lcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001366 WARN_ON_ONCE(timer->function != delayed_work_timer_fn ||
1367 timer->data != (unsigned long)dwork);
Tejun Heofc4b5142012-12-04 07:40:39 -08001368 WARN_ON_ONCE(timer_pending(timer));
1369 WARN_ON_ONCE(!list_empty(&work->entry));
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001370
Tejun Heo8852aac2012-12-01 16:23:42 -08001371 /*
1372 * If @delay is 0, queue @dwork->work immediately. This is for
1373 * both optimization and correctness. The earliest @timer can
1374 * expire is on the closest next tick and delayed_work users depend
1375 * on that there's no such delay when @delay is 0.
1376 */
1377 if (!delay) {
1378 __queue_work(cpu, wq, &dwork->work);
1379 return;
1380 }
1381
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001382 timer_stats_timer_set_start_info(&dwork->timer);
1383
1384 /*
1385 * This stores cwq for the moment, for the timer_fn. Note that the
Tejun Heoec22ca52013-01-24 11:01:33 -08001386 * work's pool is preserved to allow reentrance detection for
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001387 * delayed works.
1388 */
1389 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heoec22ca52013-01-24 11:01:33 -08001390 struct worker_pool *pool = get_work_pool(work);
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001391
Joonsoo Kime42986d2012-08-15 23:25:38 +09001392 /*
Tejun Heoec22ca52013-01-24 11:01:33 -08001393 * If we cannot get the last pool from @work directly,
Joonsoo Kime42986d2012-08-15 23:25:38 +09001394 * select the last CPU such that it avoids unnecessarily
1395 * triggering non-reentrancy check in __queue_work().
1396 */
1397 lcpu = cpu;
Tejun Heoec22ca52013-01-24 11:01:33 -08001398 if (pool)
1399 lcpu = pool->cpu;
Joonsoo Kime42986d2012-08-15 23:25:38 +09001400 if (lcpu == WORK_CPU_UNBOUND)
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001401 lcpu = raw_smp_processor_id();
1402 } else {
1403 lcpu = WORK_CPU_UNBOUND;
1404 }
1405
1406 set_work_cwq(work, get_cwq(lcpu, wq), 0);
1407
Tejun Heo12650572012-08-08 09:38:42 -07001408 dwork->cpu = cpu;
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001409 timer->expires = jiffies + delay;
1410
1411 if (unlikely(cpu != WORK_CPU_UNBOUND))
1412 add_timer_on(timer, cpu);
1413 else
1414 add_timer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415}
1416
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001417/**
1418 * queue_delayed_work_on - queue work on specific CPU after delay
1419 * @cpu: CPU number to execute work on
1420 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001421 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001422 * @delay: number of jiffies to wait before queueing
1423 *
Tejun Heo715f1302012-08-03 10:30:46 -07001424 * Returns %false if @work was already on a queue, %true otherwise. If
1425 * @delay is zero and @dwork is idle, it will be scheduled for immediate
1426 * execution.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001427 */
Tejun Heod4283e92012-08-03 10:30:44 -07001428bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
1429 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001430{
David Howells52bad642006-11-22 14:54:01 +00001431 struct work_struct *work = &dwork->work;
Tejun Heod4283e92012-08-03 10:30:44 -07001432 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001433 unsigned long flags;
1434
1435 /* read the comment in __queue_work() */
1436 local_irq_save(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001437
Tejun Heo22df02b2010-06-29 10:07:10 +02001438 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001439 __queue_delayed_work(cpu, wq, dwork, delay);
Tejun Heod4283e92012-08-03 10:30:44 -07001440 ret = true;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001441 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001442
1443 local_irq_restore(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001444 return ret;
1445}
Dave Jonesae90dd52006-06-30 01:40:45 -04001446EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Tejun Heoc8e55f32010-06-29 10:07:12 +02001448/**
Tejun Heo0a13c002012-08-03 10:30:44 -07001449 * queue_delayed_work - queue work on a workqueue after delay
1450 * @wq: workqueue to use
1451 * @dwork: delayable work to queue
1452 * @delay: number of jiffies to wait before queueing
1453 *
Tejun Heo715f1302012-08-03 10:30:46 -07001454 * Equivalent to queue_delayed_work_on() but tries to use the local CPU.
Tejun Heo0a13c002012-08-03 10:30:44 -07001455 */
Tejun Heod4283e92012-08-03 10:30:44 -07001456bool queue_delayed_work(struct workqueue_struct *wq,
Tejun Heo0a13c002012-08-03 10:30:44 -07001457 struct delayed_work *dwork, unsigned long delay)
1458{
Tejun Heo57469822012-08-03 10:30:45 -07001459 return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
Tejun Heo0a13c002012-08-03 10:30:44 -07001460}
1461EXPORT_SYMBOL_GPL(queue_delayed_work);
1462
1463/**
Tejun Heo8376fe22012-08-03 10:30:47 -07001464 * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
1465 * @cpu: CPU number to execute work on
1466 * @wq: workqueue to use
1467 * @dwork: work to queue
1468 * @delay: number of jiffies to wait before queueing
1469 *
1470 * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
1471 * modify @dwork's timer so that it expires after @delay. If @delay is
1472 * zero, @work is guaranteed to be scheduled immediately regardless of its
1473 * current state.
1474 *
1475 * Returns %false if @dwork was idle and queued, %true if @dwork was
1476 * pending and its timer was modified.
1477 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001478 * This function is safe to call from any context including IRQ handler.
Tejun Heo8376fe22012-08-03 10:30:47 -07001479 * See try_to_grab_pending() for details.
1480 */
1481bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
1482 struct delayed_work *dwork, unsigned long delay)
1483{
1484 unsigned long flags;
1485 int ret;
1486
1487 do {
1488 ret = try_to_grab_pending(&dwork->work, true, &flags);
1489 } while (unlikely(ret == -EAGAIN));
1490
1491 if (likely(ret >= 0)) {
1492 __queue_delayed_work(cpu, wq, dwork, delay);
1493 local_irq_restore(flags);
1494 }
1495
1496 /* -ENOENT from try_to_grab_pending() becomes %true */
1497 return ret;
1498}
1499EXPORT_SYMBOL_GPL(mod_delayed_work_on);
1500
1501/**
1502 * mod_delayed_work - modify delay of or queue a delayed work
1503 * @wq: workqueue to use
1504 * @dwork: work to queue
1505 * @delay: number of jiffies to wait before queueing
1506 *
1507 * mod_delayed_work_on() on local CPU.
1508 */
1509bool mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dwork,
1510 unsigned long delay)
1511{
1512 return mod_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
1513}
1514EXPORT_SYMBOL_GPL(mod_delayed_work);
1515
1516/**
Tejun Heoc8e55f32010-06-29 10:07:12 +02001517 * worker_enter_idle - enter idle state
1518 * @worker: worker which is entering idle state
1519 *
1520 * @worker is entering idle state. Update stats and idle timer if
1521 * necessary.
1522 *
1523 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001524 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001525 */
1526static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001528 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Tejun Heoc8e55f32010-06-29 10:07:12 +02001530 BUG_ON(worker->flags & WORKER_IDLE);
1531 BUG_ON(!list_empty(&worker->entry) &&
1532 (worker->hentry.next || worker->hentry.pprev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Tejun Heocb444762010-07-02 10:03:50 +02001534 /* can't use worker_set_flags(), also called from start_worker() */
1535 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001536 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001537 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001538
Tejun Heoc8e55f32010-06-29 10:07:12 +02001539 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001540 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001541
Tejun Heo628c78e2012-07-17 12:39:27 -07001542 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1543 mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
Tejun Heocb444762010-07-02 10:03:50 +02001544
Tejun Heo544ecf32012-05-14 15:04:50 -07001545 /*
Tejun Heo628c78e2012-07-17 12:39:27 -07001546 * Sanity check nr_running. Because gcwq_unbind_fn() releases
Tejun Heod565ed62013-01-24 11:01:33 -08001547 * pool->lock between setting %WORKER_UNBOUND and zapping
Tejun Heo628c78e2012-07-17 12:39:27 -07001548 * nr_running, the warning may trigger spuriously. Check iff
1549 * unbind is not in progress.
Tejun Heo544ecf32012-05-14 15:04:50 -07001550 */
Tejun Heo24647572013-01-24 11:01:33 -08001551 WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001552 pool->nr_workers == pool->nr_idle &&
Tejun Heo63d95a92012-07-12 14:46:37 -07001553 atomic_read(get_pool_nr_running(pool)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554}
1555
Tejun Heoc8e55f32010-06-29 10:07:12 +02001556/**
1557 * worker_leave_idle - leave idle state
1558 * @worker: worker which is leaving idle state
1559 *
1560 * @worker is leaving idle state. Update stats.
1561 *
1562 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001563 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001564 */
1565static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001567 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Tejun Heoc8e55f32010-06-29 10:07:12 +02001569 BUG_ON(!(worker->flags & WORKER_IDLE));
Tejun Heod302f012010-06-29 10:07:13 +02001570 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001571 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001572 list_del_init(&worker->entry);
1573}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Tejun Heoe22bee72010-06-29 10:07:14 +02001575/**
1576 * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq
1577 * @worker: self
1578 *
1579 * Works which are scheduled while the cpu is online must at least be
1580 * scheduled to a worker which is bound to the cpu so that if they are
1581 * flushed from cpu callbacks while cpu is going down, they are
1582 * guaranteed to execute on the cpu.
1583 *
1584 * This function is to be used by rogue workers and rescuers to bind
1585 * themselves to the target cpu and may race with cpu going down or
1586 * coming online. kthread_bind() can't be used because it may put the
1587 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1588 * verbatim as it's best effort and blocking and gcwq may be
1589 * [dis]associated in the meantime.
1590 *
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001591 * This function tries set_cpus_allowed() and locks gcwq and verifies the
Tejun Heo24647572013-01-24 11:01:33 -08001592 * binding against %POOL_DISASSOCIATED which is set during
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001593 * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1594 * enters idle state or fetches works without dropping lock, it can
1595 * guarantee the scheduling requirement described in the first paragraph.
Tejun Heoe22bee72010-06-29 10:07:14 +02001596 *
1597 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001598 * Might sleep. Called without any lock but returns with pool->lock
Tejun Heoe22bee72010-06-29 10:07:14 +02001599 * held.
1600 *
1601 * RETURNS:
1602 * %true if the associated gcwq is online (@worker is successfully
1603 * bound), %false if offline.
1604 */
1605static bool worker_maybe_bind_and_lock(struct worker *worker)
Tejun Heod565ed62013-01-24 11:01:33 -08001606__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001607{
Tejun Heo24647572013-01-24 11:01:33 -08001608 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001609 struct task_struct *task = worker->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Tejun Heoe22bee72010-06-29 10:07:14 +02001611 while (true) {
1612 /*
1613 * The following call may fail, succeed or succeed
1614 * without actually migrating the task to the cpu if
1615 * it races with cpu hotunplug operation. Verify
Tejun Heo24647572013-01-24 11:01:33 -08001616 * against POOL_DISASSOCIATED.
Tejun Heoe22bee72010-06-29 10:07:14 +02001617 */
Tejun Heo24647572013-01-24 11:01:33 -08001618 if (!(pool->flags & POOL_DISASSOCIATED))
Tejun Heoec22ca52013-01-24 11:01:33 -08001619 set_cpus_allowed_ptr(task, get_cpu_mask(pool->cpu));
Oleg Nesterov85f41862007-05-09 02:34:20 -07001620
Tejun Heod565ed62013-01-24 11:01:33 -08001621 spin_lock_irq(&pool->lock);
Tejun Heo24647572013-01-24 11:01:33 -08001622 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heoe22bee72010-06-29 10:07:14 +02001623 return false;
Tejun Heoec22ca52013-01-24 11:01:33 -08001624 if (task_cpu(task) == pool->cpu &&
Tejun Heoe22bee72010-06-29 10:07:14 +02001625 cpumask_equal(&current->cpus_allowed,
Tejun Heoec22ca52013-01-24 11:01:33 -08001626 get_cpu_mask(pool->cpu)))
Tejun Heoe22bee72010-06-29 10:07:14 +02001627 return true;
Tejun Heod565ed62013-01-24 11:01:33 -08001628 spin_unlock_irq(&pool->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001629
Tejun Heo5035b202011-04-29 18:08:37 +02001630 /*
1631 * We've raced with CPU hot[un]plug. Give it a breather
1632 * and retry migration. cond_resched() is required here;
1633 * otherwise, we might deadlock against cpu_stop trying to
1634 * bring down the CPU on non-preemptive kernel.
1635 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001636 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001637 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001638 }
1639}
1640
1641/*
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001642 * Rebind an idle @worker to its CPU. worker_thread() will test
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001643 * list_empty(@worker->entry) before leaving idle and call this function.
Tejun Heo25511a42012-07-17 12:39:27 -07001644 */
1645static void idle_worker_rebind(struct worker *worker)
1646{
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001647 /* CPU may go down again inbetween, clear UNBOUND only on success */
1648 if (worker_maybe_bind_and_lock(worker))
1649 worker_clr_flags(worker, WORKER_UNBOUND);
Tejun Heo25511a42012-07-17 12:39:27 -07001650
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001651 /* rebind complete, become available again */
1652 list_add(&worker->entry, &worker->pool->idle_list);
Tejun Heod565ed62013-01-24 11:01:33 -08001653 spin_unlock_irq(&worker->pool->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07001654}
1655
1656/*
1657 * Function for @worker->rebind.work used to rebind unbound busy workers to
Tejun Heo403c8212012-07-17 12:39:27 -07001658 * the associated cpu which is coming back online. This is scheduled by
1659 * cpu up but can race with other cpu hotplug operations and may be
1660 * executed twice without intervening cpu down.
Tejun Heoe22bee72010-06-29 10:07:14 +02001661 */
Tejun Heo25511a42012-07-17 12:39:27 -07001662static void busy_worker_rebind_fn(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001663{
1664 struct worker *worker = container_of(work, struct worker, rebind_work);
Tejun Heoe22bee72010-06-29 10:07:14 +02001665
Lai Jiangshaneab6d822012-09-18 09:59:22 -07001666 if (worker_maybe_bind_and_lock(worker))
1667 worker_clr_flags(worker, WORKER_UNBOUND);
Tejun Heoe22bee72010-06-29 10:07:14 +02001668
Tejun Heod565ed62013-01-24 11:01:33 -08001669 spin_unlock_irq(&worker->pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001670}
1671
Tejun Heo25511a42012-07-17 12:39:27 -07001672/**
1673 * rebind_workers - rebind all workers of a gcwq to the associated CPU
1674 * @gcwq: gcwq of interest
1675 *
1676 * @gcwq->cpu is coming online. Rebind all workers to the CPU. Rebinding
1677 * is different for idle and busy ones.
1678 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001679 * Idle ones will be removed from the idle_list and woken up. They will
1680 * add themselves back after completing rebind. This ensures that the
1681 * idle_list doesn't contain any unbound workers when re-bound busy workers
1682 * try to perform local wake-ups for concurrency management.
Tejun Heo25511a42012-07-17 12:39:27 -07001683 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001684 * Busy workers can rebind after they finish their current work items.
1685 * Queueing the rebind work item at the head of the scheduled list is
1686 * enough. Note that nr_running will be properly bumped as busy workers
1687 * rebind.
Tejun Heo25511a42012-07-17 12:39:27 -07001688 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001689 * On return, all non-manager workers are scheduled for rebind - see
1690 * manage_workers() for the manager special case. Any idle worker
1691 * including the manager will not appear on @idle_list until rebind is
1692 * complete, making local wake-ups safe.
Tejun Heo25511a42012-07-17 12:39:27 -07001693 */
1694static void rebind_workers(struct global_cwq *gcwq)
Tejun Heo25511a42012-07-17 12:39:27 -07001695{
Tejun Heo25511a42012-07-17 12:39:27 -07001696 struct worker_pool *pool;
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001697 struct worker *worker, *n;
Tejun Heo25511a42012-07-17 12:39:27 -07001698 struct hlist_node *pos;
1699 int i;
1700
Tejun Heod565ed62013-01-24 11:01:33 -08001701 for_each_worker_pool(pool, gcwq) {
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07001702 lockdep_assert_held(&pool->assoc_mutex);
Tejun Heod565ed62013-01-24 11:01:33 -08001703 lockdep_assert_held(&pool->lock);
1704 }
Tejun Heo25511a42012-07-17 12:39:27 -07001705
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001706 /* dequeue and kick idle ones */
Tejun Heo25511a42012-07-17 12:39:27 -07001707 for_each_worker_pool(pool, gcwq) {
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001708 list_for_each_entry_safe(worker, n, &pool->idle_list, entry) {
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001709 /*
1710 * idle workers should be off @pool->idle_list
1711 * until rebind is complete to avoid receiving
1712 * premature local wake-ups.
1713 */
1714 list_del_init(&worker->entry);
Lai Jiangshan96e65302012-09-02 00:28:19 +08001715
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001716 /*
1717 * worker_thread() will see the above dequeuing
1718 * and call idle_worker_rebind().
1719 */
Tejun Heo25511a42012-07-17 12:39:27 -07001720 wake_up_process(worker->task);
1721 }
Tejun Heo25511a42012-07-17 12:39:27 -07001722
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001723 /* rebind busy workers */
1724 for_each_busy_worker(worker, i, pos, pool) {
1725 struct work_struct *rebind_work = &worker->rebind_work;
1726 struct workqueue_struct *wq;
Tejun Heo25511a42012-07-17 12:39:27 -07001727
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001728 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
1729 work_data_bits(rebind_work)))
1730 continue;
Tejun Heo25511a42012-07-17 12:39:27 -07001731
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001732 debug_work_activate(rebind_work);
Tejun Heo90beca52012-09-04 23:12:33 -07001733
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001734 /*
1735 * wq doesn't really matter but let's keep
1736 * @worker->pool and @cwq->pool consistent for
1737 * sanity.
1738 */
1739 if (std_worker_pool_pri(worker->pool))
1740 wq = system_highpri_wq;
1741 else
1742 wq = system_wq;
Tejun Heoec588152012-09-04 23:16:32 -07001743
Tejun Heoec22ca52013-01-24 11:01:33 -08001744 insert_work(get_cwq(pool->cpu, wq), rebind_work,
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001745 worker->scheduled.next,
1746 work_color_to_flags(WORK_NO_COLOR));
1747 }
Tejun Heoec588152012-09-04 23:16:32 -07001748 }
Tejun Heo25511a42012-07-17 12:39:27 -07001749}
1750
Tejun Heoc34056a2010-06-29 10:07:11 +02001751static struct worker *alloc_worker(void)
1752{
1753 struct worker *worker;
1754
1755 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001756 if (worker) {
1757 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001758 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heo25511a42012-07-17 12:39:27 -07001759 INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn);
Tejun Heoe22bee72010-06-29 10:07:14 +02001760 /* on creation a worker is in !idle && prep state */
1761 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001762 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001763 return worker;
1764}
1765
1766/**
1767 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001768 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001769 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001770 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001771 * can be started by calling start_worker() or destroyed using
1772 * destroy_worker().
1773 *
1774 * CONTEXT:
1775 * Might sleep. Does GFP_KERNEL allocations.
1776 *
1777 * RETURNS:
1778 * Pointer to the newly created worker.
1779 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001780static struct worker *create_worker(struct worker_pool *pool)
Tejun Heoc34056a2010-06-29 10:07:11 +02001781{
Tejun Heoe34cdddb2013-01-24 11:01:33 -08001782 const char *pri = std_worker_pool_pri(pool) ? "H" : "";
Tejun Heoc34056a2010-06-29 10:07:11 +02001783 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001784 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001785
Tejun Heod565ed62013-01-24 11:01:33 -08001786 spin_lock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001787 while (ida_get_new(&pool->worker_ida, &id)) {
Tejun Heod565ed62013-01-24 11:01:33 -08001788 spin_unlock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001789 if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001790 goto fail;
Tejun Heod565ed62013-01-24 11:01:33 -08001791 spin_lock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001792 }
Tejun Heod565ed62013-01-24 11:01:33 -08001793 spin_unlock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001794
1795 worker = alloc_worker();
1796 if (!worker)
1797 goto fail;
1798
Tejun Heobd7bdd42012-07-12 14:46:37 -07001799 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001800 worker->id = id;
1801
Tejun Heoec22ca52013-01-24 11:01:33 -08001802 if (pool->cpu != WORK_CPU_UNBOUND)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001803 worker->task = kthread_create_on_node(worker_thread,
Tejun Heoec22ca52013-01-24 11:01:33 -08001804 worker, cpu_to_node(pool->cpu),
1805 "kworker/%u:%d%s", pool->cpu, id, pri);
Tejun Heof3421792010-07-02 10:03:51 +02001806 else
1807 worker->task = kthread_create(worker_thread, worker,
Tejun Heo32704762012-07-13 22:16:45 -07001808 "kworker/u:%d%s", id, pri);
Tejun Heoc34056a2010-06-29 10:07:11 +02001809 if (IS_ERR(worker->task))
1810 goto fail;
1811
Tejun Heoe34cdddb2013-01-24 11:01:33 -08001812 if (std_worker_pool_pri(pool))
Tejun Heo32704762012-07-13 22:16:45 -07001813 set_user_nice(worker->task, HIGHPRI_NICE_LEVEL);
1814
Tejun Heodb7bccf2010-06-29 10:07:12 +02001815 /*
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001816 * Determine CPU binding of the new worker depending on
Tejun Heo24647572013-01-24 11:01:33 -08001817 * %POOL_DISASSOCIATED. The caller is responsible for ensuring the
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001818 * flag remains stable across this function. See the comments
1819 * above the flag definition for details.
1820 *
1821 * As an unbound worker may later become a regular one if CPU comes
1822 * online, make sure every worker has %PF_THREAD_BOUND set.
Tejun Heodb7bccf2010-06-29 10:07:12 +02001823 */
Tejun Heo24647572013-01-24 11:01:33 -08001824 if (!(pool->flags & POOL_DISASSOCIATED)) {
Tejun Heoec22ca52013-01-24 11:01:33 -08001825 kthread_bind(worker->task, pool->cpu);
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001826 } else {
Tejun Heodb7bccf2010-06-29 10:07:12 +02001827 worker->task->flags |= PF_THREAD_BOUND;
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001828 worker->flags |= WORKER_UNBOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001830
Tejun Heoc34056a2010-06-29 10:07:11 +02001831 return worker;
1832fail:
1833 if (id >= 0) {
Tejun Heod565ed62013-01-24 11:01:33 -08001834 spin_lock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001835 ida_remove(&pool->worker_ida, id);
Tejun Heod565ed62013-01-24 11:01:33 -08001836 spin_unlock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001837 }
1838 kfree(worker);
1839 return NULL;
1840}
1841
1842/**
1843 * start_worker - start a newly created worker
1844 * @worker: worker to start
1845 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001846 * Make the gcwq aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001847 *
1848 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001849 * spin_lock_irq(pool->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001850 */
1851static void start_worker(struct worker *worker)
1852{
Tejun Heocb444762010-07-02 10:03:50 +02001853 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001854 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001855 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001856 wake_up_process(worker->task);
1857}
1858
1859/**
1860 * destroy_worker - destroy a workqueue worker
1861 * @worker: worker to be destroyed
1862 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001863 * Destroy @worker and adjust @gcwq stats accordingly.
1864 *
1865 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001866 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001867 */
1868static void destroy_worker(struct worker *worker)
1869{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001870 struct worker_pool *pool = worker->pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001871 int id = worker->id;
1872
1873 /* sanity check frenzy */
1874 BUG_ON(worker->current_work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001875 BUG_ON(!list_empty(&worker->scheduled));
Tejun Heoc34056a2010-06-29 10:07:11 +02001876
Tejun Heoc8e55f32010-06-29 10:07:12 +02001877 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001878 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001879 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001880 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001881
1882 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001883 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001884
Tejun Heod565ed62013-01-24 11:01:33 -08001885 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001886
Tejun Heoc34056a2010-06-29 10:07:11 +02001887 kthread_stop(worker->task);
1888 kfree(worker);
1889
Tejun Heod565ed62013-01-24 11:01:33 -08001890 spin_lock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001891 ida_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001892}
1893
Tejun Heo63d95a92012-07-12 14:46:37 -07001894static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001895{
Tejun Heo63d95a92012-07-12 14:46:37 -07001896 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001897
Tejun Heod565ed62013-01-24 11:01:33 -08001898 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001899
Tejun Heo63d95a92012-07-12 14:46:37 -07001900 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001901 struct worker *worker;
1902 unsigned long expires;
1903
1904 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001905 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001906 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1907
1908 if (time_before(jiffies, expires))
Tejun Heo63d95a92012-07-12 14:46:37 -07001909 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001910 else {
1911 /* it's been idle for too long, wake up manager */
Tejun Heo11ebea52012-07-12 14:46:37 -07001912 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -07001913 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001914 }
1915 }
1916
Tejun Heod565ed62013-01-24 11:01:33 -08001917 spin_unlock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001918}
1919
1920static bool send_mayday(struct work_struct *work)
1921{
1922 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1923 struct workqueue_struct *wq = cwq->wq;
Tejun Heof3421792010-07-02 10:03:51 +02001924 unsigned int cpu;
Tejun Heoe22bee72010-06-29 10:07:14 +02001925
1926 if (!(wq->flags & WQ_RESCUER))
1927 return false;
1928
1929 /* mayday mayday mayday */
Tejun Heoec22ca52013-01-24 11:01:33 -08001930 cpu = cwq->pool->cpu;
Tejun Heof3421792010-07-02 10:03:51 +02001931 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1932 if (cpu == WORK_CPU_UNBOUND)
1933 cpu = 0;
Tejun Heof2e005a2010-07-20 15:59:09 +02001934 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001935 wake_up_process(wq->rescuer->task);
1936 return true;
1937}
1938
Tejun Heo63d95a92012-07-12 14:46:37 -07001939static void gcwq_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001940{
Tejun Heo63d95a92012-07-12 14:46:37 -07001941 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001942 struct work_struct *work;
1943
Tejun Heod565ed62013-01-24 11:01:33 -08001944 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001945
Tejun Heo63d95a92012-07-12 14:46:37 -07001946 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001947 /*
1948 * We've been trying to create a new worker but
1949 * haven't been successful. We might be hitting an
1950 * allocation deadlock. Send distress signals to
1951 * rescuers.
1952 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001953 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001954 send_mayday(work);
1955 }
1956
Tejun Heod565ed62013-01-24 11:01:33 -08001957 spin_unlock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001958
Tejun Heo63d95a92012-07-12 14:46:37 -07001959 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001960}
1961
1962/**
1963 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001964 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001965 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001966 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001967 * have at least one idle worker on return from this function. If
1968 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001969 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001970 * possible allocation deadlock.
1971 *
1972 * On return, need_to_create_worker() is guaranteed to be false and
1973 * may_start_working() true.
1974 *
1975 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001976 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001977 * multiple times. Does GFP_KERNEL allocations. Called only from
1978 * manager.
1979 *
1980 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08001981 * false if no action was taken and pool->lock stayed locked, true
Tejun Heoe22bee72010-06-29 10:07:14 +02001982 * otherwise.
1983 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001984static bool maybe_create_worker(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001985__releases(&pool->lock)
1986__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001987{
Tejun Heo63d95a92012-07-12 14:46:37 -07001988 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001989 return false;
1990restart:
Tejun Heod565ed62013-01-24 11:01:33 -08001991 spin_unlock_irq(&pool->lock);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001992
Tejun Heoe22bee72010-06-29 10:07:14 +02001993 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07001994 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001995
1996 while (true) {
1997 struct worker *worker;
1998
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001999 worker = create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002000 if (worker) {
Tejun Heo63d95a92012-07-12 14:46:37 -07002001 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08002002 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002003 start_worker(worker);
Tejun Heo63d95a92012-07-12 14:46:37 -07002004 BUG_ON(need_to_create_worker(pool));
Tejun Heoe22bee72010-06-29 10:07:14 +02002005 return true;
2006 }
2007
Tejun Heo63d95a92012-07-12 14:46:37 -07002008 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002009 break;
2010
Tejun Heoe22bee72010-06-29 10:07:14 +02002011 __set_current_state(TASK_INTERRUPTIBLE);
2012 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02002013
Tejun Heo63d95a92012-07-12 14:46:37 -07002014 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002015 break;
2016 }
2017
Tejun Heo63d95a92012-07-12 14:46:37 -07002018 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08002019 spin_lock_irq(&pool->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07002020 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002021 goto restart;
2022 return true;
2023}
2024
2025/**
2026 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo63d95a92012-07-12 14:46:37 -07002027 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02002028 *
Tejun Heo63d95a92012-07-12 14:46:37 -07002029 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02002030 * IDLE_WORKER_TIMEOUT.
2031 *
2032 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08002033 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02002034 * multiple times. Called only from manager.
2035 *
2036 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08002037 * false if no action was taken and pool->lock stayed locked, true
Tejun Heoe22bee72010-06-29 10:07:14 +02002038 * otherwise.
2039 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002040static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02002041{
2042 bool ret = false;
2043
Tejun Heo63d95a92012-07-12 14:46:37 -07002044 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02002045 struct worker *worker;
2046 unsigned long expires;
2047
Tejun Heo63d95a92012-07-12 14:46:37 -07002048 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02002049 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
2050
2051 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07002052 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02002053 break;
2054 }
2055
2056 destroy_worker(worker);
2057 ret = true;
2058 }
2059
2060 return ret;
2061}
2062
2063/**
2064 * manage_workers - manage worker pool
2065 * @worker: self
2066 *
2067 * Assume the manager role and manage gcwq worker pool @worker belongs
2068 * to. At any given time, there can be only zero or one manager per
2069 * gcwq. The exclusion is handled automatically by this function.
2070 *
2071 * The caller can safely start processing works on false return. On
2072 * true return, it's guaranteed that need_to_create_worker() is false
2073 * and may_start_working() is true.
2074 *
2075 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002076 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02002077 * multiple times. Does GFP_KERNEL allocations.
2078 *
2079 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08002080 * spin_lock_irq(pool->lock) which may be released and regrabbed
2081 * multiple times. Does GFP_KERNEL allocations.
Tejun Heoe22bee72010-06-29 10:07:14 +02002082 */
2083static bool manage_workers(struct worker *worker)
2084{
Tejun Heo63d95a92012-07-12 14:46:37 -07002085 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002086 bool ret = false;
2087
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002088 if (pool->flags & POOL_MANAGING_WORKERS)
Tejun Heoe22bee72010-06-29 10:07:14 +02002089 return ret;
2090
Lai Jiangshan552a37e2012-09-10 10:03:33 -07002091 pool->flags |= POOL_MANAGING_WORKERS;
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002092
2093 /*
2094 * To simplify both worker management and CPU hotplug, hold off
2095 * management while hotplug is in progress. CPU hotplug path can't
2096 * grab %POOL_MANAGING_WORKERS to achieve this because that can
2097 * lead to idle worker depletion (all become busy thinking someone
2098 * else is managing) which in turn can result in deadlock under
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002099 * extreme circumstances. Use @pool->assoc_mutex to synchronize
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002100 * manager against CPU hotplug.
2101 *
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002102 * assoc_mutex would always be free unless CPU hotplug is in
Tejun Heod565ed62013-01-24 11:01:33 -08002103 * progress. trylock first without dropping @pool->lock.
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002104 */
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002105 if (unlikely(!mutex_trylock(&pool->assoc_mutex))) {
Tejun Heod565ed62013-01-24 11:01:33 -08002106 spin_unlock_irq(&pool->lock);
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002107 mutex_lock(&pool->assoc_mutex);
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002108 /*
2109 * CPU hotplug could have happened while we were waiting
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002110 * for assoc_mutex. Hotplug itself can't handle us
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002111 * because manager isn't either on idle or busy list, and
2112 * @gcwq's state and ours could have deviated.
2113 *
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002114 * As hotplug is now excluded via assoc_mutex, we can
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002115 * simply try to bind. It will succeed or fail depending
2116 * on @gcwq's current state. Try it and adjust
2117 * %WORKER_UNBOUND accordingly.
2118 */
2119 if (worker_maybe_bind_and_lock(worker))
2120 worker->flags &= ~WORKER_UNBOUND;
2121 else
2122 worker->flags |= WORKER_UNBOUND;
2123
2124 ret = true;
2125 }
2126
Tejun Heo11ebea52012-07-12 14:46:37 -07002127 pool->flags &= ~POOL_MANAGE_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02002128
2129 /*
2130 * Destroy and then create so that may_start_working() is true
2131 * on return.
2132 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002133 ret |= maybe_destroy_workers(pool);
2134 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002135
Lai Jiangshan552a37e2012-09-10 10:03:33 -07002136 pool->flags &= ~POOL_MANAGING_WORKERS;
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002137 mutex_unlock(&pool->assoc_mutex);
Tejun Heoe22bee72010-06-29 10:07:14 +02002138 return ret;
2139}
2140
Tejun Heoa62428c2010-06-29 10:07:10 +02002141/**
2142 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02002143 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02002144 * @work: work to process
2145 *
2146 * Process @work. This function contains all the logics necessary to
2147 * process a single work including synchronization against and
2148 * interaction with other workers on the same cpu, queueing and
2149 * flushing. As long as context requirement is met, any worker can
2150 * call this function to process a work.
2151 *
2152 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002153 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02002154 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002155static void process_one_work(struct worker *worker, struct work_struct *work)
Tejun Heod565ed62013-01-24 11:01:33 -08002156__releases(&pool->lock)
2157__acquires(&pool->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02002158{
Tejun Heo7e116292010-06-29 10:07:13 +02002159 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002160 struct worker_pool *pool = worker->pool;
Tejun Heofb0e7be2010-06-29 10:07:15 +02002161 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02002162 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02002163 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02002164#ifdef CONFIG_LOCKDEP
2165 /*
2166 * It is permissible to free the struct work_struct from
2167 * inside the function that is called from it, this we need to
2168 * take into account for lockdep too. To avoid bogus "held
2169 * lock freed" warnings as well as problems when looking into
2170 * work->lockdep_map, make a copy and use that here.
2171 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07002172 struct lockdep_map lockdep_map;
2173
2174 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002175#endif
Tejun Heo6fec10a2012-07-22 10:16:34 -07002176 /*
2177 * Ensure we're on the correct CPU. DISASSOCIATED test is
2178 * necessary to avoid spurious warnings from rescuers servicing the
Tejun Heo24647572013-01-24 11:01:33 -08002179 * unbound or a disassociated pool.
Tejun Heo6fec10a2012-07-22 10:16:34 -07002180 */
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002181 WARN_ON_ONCE(!(worker->flags & WORKER_UNBOUND) &&
Tejun Heo24647572013-01-24 11:01:33 -08002182 !(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heoec22ca52013-01-24 11:01:33 -08002183 raw_smp_processor_id() != pool->cpu);
Tejun Heo25511a42012-07-17 12:39:27 -07002184
Tejun Heo7e116292010-06-29 10:07:13 +02002185 /*
2186 * A single work shouldn't be executed concurrently by
2187 * multiple workers on a single cpu. Check whether anyone is
2188 * already processing the work. If so, defer the work to the
2189 * currently executing one.
2190 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002191 collision = find_worker_executing_work(pool, work);
Tejun Heo7e116292010-06-29 10:07:13 +02002192 if (unlikely(collision)) {
2193 move_linked_works(work, &collision->scheduled, NULL);
2194 return;
2195 }
2196
Tejun Heo8930cab2012-08-03 10:30:45 -07002197 /* claim and dequeue */
Tejun Heoa62428c2010-06-29 10:07:10 +02002198 debug_work_deactivate(work);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002199 hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work);
Tejun Heoc34056a2010-06-29 10:07:11 +02002200 worker->current_work = work;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002201 worker->current_func = work->func;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02002202 worker->current_cwq = cwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002203 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002204
Tejun Heoa62428c2010-06-29 10:07:10 +02002205 list_del_init(&work->entry);
2206
Tejun Heo649027d2010-06-29 10:07:14 +02002207 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02002208 * CPU intensive works don't participate in concurrency
2209 * management. They're the scheduler's responsibility.
2210 */
2211 if (unlikely(cpu_intensive))
2212 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
2213
Tejun Heo974271c2012-07-12 14:46:37 -07002214 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002215 * Unbound pool isn't concurrency managed and work items should be
Tejun Heo974271c2012-07-12 14:46:37 -07002216 * executed ASAP. Wake up another worker if necessary.
2217 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002218 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
2219 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07002220
Tejun Heo8930cab2012-08-03 10:30:45 -07002221 /*
Tejun Heo7c3eed52013-01-24 11:01:33 -08002222 * Record the last pool and clear PENDING which should be the last
Tejun Heod565ed62013-01-24 11:01:33 -08002223 * update to @work. Also, do this inside @pool->lock so that
Tejun Heo23657bb2012-08-13 17:08:19 -07002224 * PENDING and queued state changes happen together while IRQ is
2225 * disabled.
Tejun Heo8930cab2012-08-03 10:30:45 -07002226 */
Tejun Heo7c3eed52013-01-24 11:01:33 -08002227 set_work_pool_and_clear_pending(work, pool->id);
Tejun Heoa62428c2010-06-29 10:07:10 +02002228
Tejun Heod565ed62013-01-24 11:01:33 -08002229 spin_unlock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002230
Tejun Heoe1594892011-01-09 23:32:15 +01002231 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002232 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002233 trace_workqueue_execute_start(work);
Tejun Heoa2c1c572012-12-18 10:35:02 -08002234 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002235 /*
2236 * While we must be careful to not use "work" after this, the trace
2237 * point will only record its address.
2238 */
2239 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002240 lock_map_release(&lockdep_map);
2241 lock_map_release(&cwq->wq->lockdep_map);
2242
2243 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Valentin Ilie044c7822012-08-19 00:52:42 +03002244 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
2245 " last function: %pf\n",
Tejun Heoa2c1c572012-12-18 10:35:02 -08002246 current->comm, preempt_count(), task_pid_nr(current),
2247 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02002248 debug_show_held_locks(current);
2249 dump_stack();
2250 }
2251
Tejun Heod565ed62013-01-24 11:01:33 -08002252 spin_lock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002253
Tejun Heofb0e7be2010-06-29 10:07:15 +02002254 /* clear cpu intensive status */
2255 if (unlikely(cpu_intensive))
2256 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2257
Tejun Heoa62428c2010-06-29 10:07:10 +02002258 /* we're done with it, release */
Sasha Levin42f85702012-12-17 10:01:23 -05002259 hash_del(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002260 worker->current_work = NULL;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002261 worker->current_func = NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02002262 worker->current_cwq = NULL;
Lai Jiangshanb3f9f402012-09-18 10:40:00 -07002263 cwq_dec_nr_in_flight(cwq, work_color);
Tejun Heoa62428c2010-06-29 10:07:10 +02002264}
2265
Tejun Heoaffee4b2010-06-29 10:07:12 +02002266/**
2267 * process_scheduled_works - process scheduled works
2268 * @worker: self
2269 *
2270 * Process all scheduled works. Please note that the scheduled list
2271 * may change while processing a work, so this function repeatedly
2272 * fetches a work from the top and executes it.
2273 *
2274 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002275 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02002276 * multiple times.
2277 */
2278static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002280 while (!list_empty(&worker->scheduled)) {
2281 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002283 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285}
2286
Tejun Heo4690c4a2010-06-29 10:07:10 +02002287/**
2288 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02002289 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02002290 *
Tejun Heoe22bee72010-06-29 10:07:14 +02002291 * The gcwq worker thread function. There's a single dynamic pool of
2292 * these per each cpu. These workers process all works regardless of
2293 * their specific target workqueue. The only exception is works which
2294 * belong to workqueues with a rescuer which will be explained in
2295 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02002296 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002297static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298{
Tejun Heoc34056a2010-06-29 10:07:11 +02002299 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07002300 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
Tejun Heoe22bee72010-06-29 10:07:14 +02002302 /* tell the scheduler that this is a workqueue worker */
2303 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002304woke_up:
Tejun Heod565ed62013-01-24 11:01:33 -08002305 spin_lock_irq(&pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002307 /* we are off idle list if destruction or rebind is requested */
2308 if (unlikely(list_empty(&worker->entry))) {
Tejun Heod565ed62013-01-24 11:01:33 -08002309 spin_unlock_irq(&pool->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07002310
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002311 /* if DIE is set, destruction is requested */
Tejun Heo25511a42012-07-17 12:39:27 -07002312 if (worker->flags & WORKER_DIE) {
2313 worker->task->flags &= ~PF_WQ_WORKER;
2314 return 0;
2315 }
2316
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002317 /* otherwise, rebind */
Tejun Heo25511a42012-07-17 12:39:27 -07002318 idle_worker_rebind(worker);
2319 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 }
2321
Tejun Heoc8e55f32010-06-29 10:07:12 +02002322 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002323recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002324 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002325 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002326 goto sleep;
2327
2328 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002329 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002330 goto recheck;
2331
Tejun Heoc8e55f32010-06-29 10:07:12 +02002332 /*
2333 * ->scheduled list can only be filled while a worker is
2334 * preparing to process a work or actually processing it.
2335 * Make sure nobody diddled with it while I was sleeping.
2336 */
2337 BUG_ON(!list_empty(&worker->scheduled));
2338
Tejun Heoe22bee72010-06-29 10:07:14 +02002339 /*
2340 * When control reaches this point, we're guaranteed to have
2341 * at least one idle worker or that someone else has already
2342 * assumed the manager role.
2343 */
2344 worker_clr_flags(worker, WORKER_PREP);
2345
2346 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002347 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002348 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002349 struct work_struct, entry);
2350
2351 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2352 /* optimization path, not strictly necessary */
2353 process_one_work(worker, work);
2354 if (unlikely(!list_empty(&worker->scheduled)))
2355 process_scheduled_works(worker);
2356 } else {
2357 move_linked_works(work, &worker->scheduled, NULL);
2358 process_scheduled_works(worker);
2359 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002360 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002361
Tejun Heoe22bee72010-06-29 10:07:14 +02002362 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002363sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07002364 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002365 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002366
Tejun Heoc8e55f32010-06-29 10:07:12 +02002367 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002368 * pool->lock is held and there's no work to process and no need to
2369 * manage, sleep. Workers are woken up only while holding
2370 * pool->lock or from local cpu, so setting the current state
2371 * before releasing pool->lock is enough to prevent losing any
2372 * event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002373 */
2374 worker_enter_idle(worker);
2375 __set_current_state(TASK_INTERRUPTIBLE);
Tejun Heod565ed62013-01-24 11:01:33 -08002376 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02002377 schedule();
2378 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379}
2380
Tejun Heoe22bee72010-06-29 10:07:14 +02002381/**
2382 * rescuer_thread - the rescuer thread function
Tejun Heo111c2252013-01-17 17:16:24 -08002383 * @__rescuer: self
Tejun Heoe22bee72010-06-29 10:07:14 +02002384 *
2385 * Workqueue rescuer thread function. There's one rescuer for each
2386 * workqueue which has WQ_RESCUER set.
2387 *
2388 * Regular work processing on a gcwq may block trying to create a new
2389 * worker which uses GFP_KERNEL allocation which has slight chance of
2390 * developing into deadlock if some works currently on the same queue
2391 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2392 * the problem rescuer solves.
2393 *
2394 * When such condition is possible, the gcwq summons rescuers of all
2395 * workqueues which have works queued on the gcwq and let them process
2396 * those works so that forward progress can be guaranteed.
2397 *
2398 * This should happen rarely.
2399 */
Tejun Heo111c2252013-01-17 17:16:24 -08002400static int rescuer_thread(void *__rescuer)
Tejun Heoe22bee72010-06-29 10:07:14 +02002401{
Tejun Heo111c2252013-01-17 17:16:24 -08002402 struct worker *rescuer = __rescuer;
2403 struct workqueue_struct *wq = rescuer->rescue_wq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002404 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heof3421792010-07-02 10:03:51 +02002405 bool is_unbound = wq->flags & WQ_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02002406 unsigned int cpu;
2407
2408 set_user_nice(current, RESCUER_NICE_LEVEL);
Tejun Heo111c2252013-01-17 17:16:24 -08002409
2410 /*
2411 * Mark rescuer as worker too. As WORKER_PREP is never cleared, it
2412 * doesn't participate in concurrency management.
2413 */
2414 rescuer->task->flags |= PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002415repeat:
2416 set_current_state(TASK_INTERRUPTIBLE);
2417
Mike Galbraith412d32e2012-11-28 07:17:18 +01002418 if (kthread_should_stop()) {
2419 __set_current_state(TASK_RUNNING);
Tejun Heo111c2252013-01-17 17:16:24 -08002420 rescuer->task->flags &= ~PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002421 return 0;
Mike Galbraith412d32e2012-11-28 07:17:18 +01002422 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002423
Tejun Heof3421792010-07-02 10:03:51 +02002424 /*
2425 * See whether any cpu is asking for help. Unbounded
2426 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
2427 */
Tejun Heof2e005a2010-07-20 15:59:09 +02002428 for_each_mayday_cpu(cpu, wq->mayday_mask) {
Tejun Heof3421792010-07-02 10:03:51 +02002429 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2430 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002431 struct worker_pool *pool = cwq->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002432 struct work_struct *work, *n;
2433
2434 __set_current_state(TASK_RUNNING);
Tejun Heof2e005a2010-07-20 15:59:09 +02002435 mayday_clear_cpu(cpu, wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002436
2437 /* migrate to the target cpu if possible */
Tejun Heobd7bdd42012-07-12 14:46:37 -07002438 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002439 worker_maybe_bind_and_lock(rescuer);
2440
2441 /*
2442 * Slurp in all works issued via this workqueue and
2443 * process'em.
2444 */
2445 BUG_ON(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002446 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02002447 if (get_work_cwq(work) == cwq)
2448 move_linked_works(work, scheduled, &n);
2449
2450 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002451
2452 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002453 * Leave this pool. If keep_working() is %true, notify a
Tejun Heo75769582011-02-14 14:04:46 +01002454 * regular worker; otherwise, we end up with 0 concurrency
2455 * and stalling the execution.
2456 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002457 if (keep_working(pool))
2458 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002459
Tejun Heod565ed62013-01-24 11:01:33 -08002460 spin_unlock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002461 }
2462
Tejun Heo111c2252013-01-17 17:16:24 -08002463 /* rescuers should never participate in concurrency management */
2464 WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
Tejun Heoe22bee72010-06-29 10:07:14 +02002465 schedule();
2466 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467}
2468
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002469struct wq_barrier {
2470 struct work_struct work;
2471 struct completion done;
2472};
2473
2474static void wq_barrier_func(struct work_struct *work)
2475{
2476 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2477 complete(&barr->done);
2478}
2479
Tejun Heo4690c4a2010-06-29 10:07:10 +02002480/**
2481 * insert_wq_barrier - insert a barrier work
2482 * @cwq: cwq to insert barrier into
2483 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002484 * @target: target work to attach @barr to
2485 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002486 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002487 * @barr is linked to @target such that @barr is completed only after
2488 * @target finishes execution. Please note that the ordering
2489 * guarantee is observed only with respect to @target and on the local
2490 * cpu.
2491 *
2492 * Currently, a queued barrier can't be canceled. This is because
2493 * try_to_grab_pending() can't determine whether the work to be
2494 * grabbed is at the head of the queue and thus can't clear LINKED
2495 * flag of the previous work while there must be a valid next work
2496 * after a work with LINKED flag set.
2497 *
2498 * Note that when @worker is non-NULL, @target may be modified
2499 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002500 *
2501 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002502 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002503 */
Oleg Nesterov83c22522007-05-09 02:33:54 -07002504static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002505 struct wq_barrier *barr,
2506 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002507{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002508 struct list_head *head;
2509 unsigned int linked = 0;
2510
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002511 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002512 * debugobject calls are safe here even with pool->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002513 * as we know for sure that this will not trigger any of the
2514 * checks and call back into the fixup functions where we
2515 * might deadlock.
2516 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002517 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002518 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002519 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002520
Tejun Heoaffee4b2010-06-29 10:07:12 +02002521 /*
2522 * If @target is currently being executed, schedule the
2523 * barrier to the worker; otherwise, put it after @target.
2524 */
2525 if (worker)
2526 head = worker->scheduled.next;
2527 else {
2528 unsigned long *bits = work_data_bits(target);
2529
2530 head = target->entry.next;
2531 /* there can already be other linked works, inherit and set */
2532 linked = *bits & WORK_STRUCT_LINKED;
2533 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2534 }
2535
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002536 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002537 insert_work(cwq, &barr->work, head,
2538 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002539}
2540
Tejun Heo73f53c42010-06-29 10:07:11 +02002541/**
2542 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2543 * @wq: workqueue being flushed
2544 * @flush_color: new flush color, < 0 for no-op
2545 * @work_color: new work color, < 0 for no-op
2546 *
2547 * Prepare cwqs for workqueue flushing.
2548 *
2549 * If @flush_color is non-negative, flush_color on all cwqs should be
2550 * -1. If no cwq has in-flight commands at the specified color, all
2551 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2552 * has in flight commands, its cwq->flush_color is set to
2553 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2554 * wakeup logic is armed and %true is returned.
2555 *
2556 * The caller should have initialized @wq->first_flusher prior to
2557 * calling this function with non-negative @flush_color. If
2558 * @flush_color is negative, no flush color update is done and %false
2559 * is returned.
2560 *
2561 * If @work_color is non-negative, all cwqs should have the same
2562 * work_color which is previous to @work_color and all will be
2563 * advanced to @work_color.
2564 *
2565 * CONTEXT:
2566 * mutex_lock(wq->flush_mutex).
2567 *
2568 * RETURNS:
2569 * %true if @flush_color >= 0 and there's something to flush. %false
2570 * otherwise.
2571 */
2572static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2573 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574{
Tejun Heo73f53c42010-06-29 10:07:11 +02002575 bool wait = false;
2576 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -07002577
Tejun Heo73f53c42010-06-29 10:07:11 +02002578 if (flush_color >= 0) {
2579 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2580 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002581 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002582
Tejun Heof3421792010-07-02 10:03:51 +02002583 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002584 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heod565ed62013-01-24 11:01:33 -08002585 struct worker_pool *pool = cwq->pool;
Tejun Heo73f53c42010-06-29 10:07:11 +02002586
Tejun Heod565ed62013-01-24 11:01:33 -08002587 spin_lock_irq(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002588
2589 if (flush_color >= 0) {
2590 BUG_ON(cwq->flush_color != -1);
2591
2592 if (cwq->nr_in_flight[flush_color]) {
2593 cwq->flush_color = flush_color;
2594 atomic_inc(&wq->nr_cwqs_to_flush);
2595 wait = true;
2596 }
2597 }
2598
2599 if (work_color >= 0) {
2600 BUG_ON(work_color != work_next_color(cwq->work_color));
2601 cwq->work_color = work_color;
2602 }
2603
Tejun Heod565ed62013-01-24 11:01:33 -08002604 spin_unlock_irq(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002605 }
2606
2607 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2608 complete(&wq->first_flusher->done);
2609
2610 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611}
2612
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002613/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002615 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 *
2617 * Forces execution of the workqueue and blocks until its completion.
2618 * This is typically used in driver shutdown handlers.
2619 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002620 * We sleep until all works which were queued on entry have been handled,
2621 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002623void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624{
Tejun Heo73f53c42010-06-29 10:07:11 +02002625 struct wq_flusher this_flusher = {
2626 .list = LIST_HEAD_INIT(this_flusher.list),
2627 .flush_color = -1,
2628 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2629 };
2630 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002631
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002632 lock_map_acquire(&wq->lockdep_map);
2633 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002634
2635 mutex_lock(&wq->flush_mutex);
2636
2637 /*
2638 * Start-to-wait phase
2639 */
2640 next_color = work_next_color(wq->work_color);
2641
2642 if (next_color != wq->flush_color) {
2643 /*
2644 * Color space is not full. The current work_color
2645 * becomes our flush_color and work_color is advanced
2646 * by one.
2647 */
2648 BUG_ON(!list_empty(&wq->flusher_overflow));
2649 this_flusher.flush_color = wq->work_color;
2650 wq->work_color = next_color;
2651
2652 if (!wq->first_flusher) {
2653 /* no flush in progress, become the first flusher */
2654 BUG_ON(wq->flush_color != this_flusher.flush_color);
2655
2656 wq->first_flusher = &this_flusher;
2657
2658 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2659 wq->work_color)) {
2660 /* nothing to flush, done */
2661 wq->flush_color = next_color;
2662 wq->first_flusher = NULL;
2663 goto out_unlock;
2664 }
2665 } else {
2666 /* wait in queue */
2667 BUG_ON(wq->flush_color == this_flusher.flush_color);
2668 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2669 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2670 }
2671 } else {
2672 /*
2673 * Oops, color space is full, wait on overflow queue.
2674 * The next flush completion will assign us
2675 * flush_color and transfer to flusher_queue.
2676 */
2677 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2678 }
2679
2680 mutex_unlock(&wq->flush_mutex);
2681
2682 wait_for_completion(&this_flusher.done);
2683
2684 /*
2685 * Wake-up-and-cascade phase
2686 *
2687 * First flushers are responsible for cascading flushes and
2688 * handling overflow. Non-first flushers can simply return.
2689 */
2690 if (wq->first_flusher != &this_flusher)
2691 return;
2692
2693 mutex_lock(&wq->flush_mutex);
2694
Tejun Heo4ce48b32010-07-02 10:03:51 +02002695 /* we might have raced, check again with mutex held */
2696 if (wq->first_flusher != &this_flusher)
2697 goto out_unlock;
2698
Tejun Heo73f53c42010-06-29 10:07:11 +02002699 wq->first_flusher = NULL;
2700
2701 BUG_ON(!list_empty(&this_flusher.list));
2702 BUG_ON(wq->flush_color != this_flusher.flush_color);
2703
2704 while (true) {
2705 struct wq_flusher *next, *tmp;
2706
2707 /* complete all the flushers sharing the current flush color */
2708 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2709 if (next->flush_color != wq->flush_color)
2710 break;
2711 list_del_init(&next->list);
2712 complete(&next->done);
2713 }
2714
2715 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2716 wq->flush_color != work_next_color(wq->work_color));
2717
2718 /* this flush_color is finished, advance by one */
2719 wq->flush_color = work_next_color(wq->flush_color);
2720
2721 /* one color has been freed, handle overflow queue */
2722 if (!list_empty(&wq->flusher_overflow)) {
2723 /*
2724 * Assign the same color to all overflowed
2725 * flushers, advance work_color and append to
2726 * flusher_queue. This is the start-to-wait
2727 * phase for these overflowed flushers.
2728 */
2729 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2730 tmp->flush_color = wq->work_color;
2731
2732 wq->work_color = work_next_color(wq->work_color);
2733
2734 list_splice_tail_init(&wq->flusher_overflow,
2735 &wq->flusher_queue);
2736 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2737 }
2738
2739 if (list_empty(&wq->flusher_queue)) {
2740 BUG_ON(wq->flush_color != wq->work_color);
2741 break;
2742 }
2743
2744 /*
2745 * Need to flush more colors. Make the next flusher
2746 * the new first flusher and arm cwqs.
2747 */
2748 BUG_ON(wq->flush_color == wq->work_color);
2749 BUG_ON(wq->flush_color != next->flush_color);
2750
2751 list_del_init(&next->list);
2752 wq->first_flusher = next;
2753
2754 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2755 break;
2756
2757 /*
2758 * Meh... this color is already done, clear first
2759 * flusher and repeat cascading.
2760 */
2761 wq->first_flusher = NULL;
2762 }
2763
2764out_unlock:
2765 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766}
Dave Jonesae90dd52006-06-30 01:40:45 -04002767EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002769/**
2770 * drain_workqueue - drain a workqueue
2771 * @wq: workqueue to drain
2772 *
2773 * Wait until the workqueue becomes empty. While draining is in progress,
2774 * only chain queueing is allowed. IOW, only currently pending or running
2775 * work items on @wq can queue further work items on it. @wq is flushed
2776 * repeatedly until it becomes empty. The number of flushing is detemined
2777 * by the depth of chaining and should be relatively short. Whine if it
2778 * takes too long.
2779 */
2780void drain_workqueue(struct workqueue_struct *wq)
2781{
2782 unsigned int flush_cnt = 0;
2783 unsigned int cpu;
2784
2785 /*
2786 * __queue_work() needs to test whether there are drainers, is much
2787 * hotter than drain_workqueue() and already looks at @wq->flags.
2788 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2789 */
2790 spin_lock(&workqueue_lock);
2791 if (!wq->nr_drainers++)
2792 wq->flags |= WQ_DRAINING;
2793 spin_unlock(&workqueue_lock);
2794reflush:
2795 flush_workqueue(wq);
2796
2797 for_each_cwq_cpu(cpu, wq) {
2798 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002799 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002800
Tejun Heod565ed62013-01-24 11:01:33 -08002801 spin_lock_irq(&cwq->pool->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002802 drained = !cwq->nr_active && list_empty(&cwq->delayed_works);
Tejun Heod565ed62013-01-24 11:01:33 -08002803 spin_unlock_irq(&cwq->pool->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002804
2805 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002806 continue;
2807
2808 if (++flush_cnt == 10 ||
2809 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
Valentin Ilie044c7822012-08-19 00:52:42 +03002810 pr_warn("workqueue %s: flush on destruction isn't complete after %u tries\n",
2811 wq->name, flush_cnt);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002812 goto reflush;
2813 }
2814
2815 spin_lock(&workqueue_lock);
2816 if (!--wq->nr_drainers)
2817 wq->flags &= ~WQ_DRAINING;
2818 spin_unlock(&workqueue_lock);
2819}
2820EXPORT_SYMBOL_GPL(drain_workqueue);
2821
Tejun Heo606a5022012-08-20 14:51:23 -07002822static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
Tejun Heobaf59022010-09-16 10:42:16 +02002823{
2824 struct worker *worker = NULL;
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002825 struct worker_pool *pool;
Tejun Heobaf59022010-09-16 10:42:16 +02002826 struct cpu_workqueue_struct *cwq;
2827
2828 might_sleep();
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002829 pool = get_work_pool(work);
2830 if (!pool)
Tejun Heobaf59022010-09-16 10:42:16 +02002831 return false;
2832
Tejun Heod565ed62013-01-24 11:01:33 -08002833 spin_lock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002834 if (!list_empty(&work->entry)) {
2835 /*
2836 * See the comment near try_to_grab_pending()->smp_rmb().
Tejun Heod565ed62013-01-24 11:01:33 -08002837 * If it was re-queued to a different pool under us, we
Tejun Heobaf59022010-09-16 10:42:16 +02002838 * are not going to wait.
2839 */
2840 smp_rmb();
2841 cwq = get_work_cwq(work);
Tejun Heod565ed62013-01-24 11:01:33 -08002842 if (unlikely(!cwq || pool != cwq->pool))
Tejun Heobaf59022010-09-16 10:42:16 +02002843 goto already_gone;
Tejun Heo606a5022012-08-20 14:51:23 -07002844 } else {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002845 worker = find_worker_executing_work(pool, work);
Tejun Heobaf59022010-09-16 10:42:16 +02002846 if (!worker)
2847 goto already_gone;
2848 cwq = worker->current_cwq;
Tejun Heo606a5022012-08-20 14:51:23 -07002849 }
Tejun Heobaf59022010-09-16 10:42:16 +02002850
2851 insert_wq_barrier(cwq, barr, work, worker);
Tejun Heod565ed62013-01-24 11:01:33 -08002852 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002853
Tejun Heoe1594892011-01-09 23:32:15 +01002854 /*
2855 * If @max_active is 1 or rescuer is in use, flushing another work
2856 * item on the same workqueue may lead to deadlock. Make sure the
2857 * flusher is not running on the same workqueue by verifying write
2858 * access.
2859 */
2860 if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER)
2861 lock_map_acquire(&cwq->wq->lockdep_map);
2862 else
2863 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heobaf59022010-09-16 10:42:16 +02002864 lock_map_release(&cwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002865
Tejun Heobaf59022010-09-16 10:42:16 +02002866 return true;
2867already_gone:
Tejun Heod565ed62013-01-24 11:01:33 -08002868 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002869 return false;
2870}
2871
Oleg Nesterovdb700892008-07-25 01:47:49 -07002872/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002873 * flush_work - wait for a work to finish executing the last queueing instance
2874 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002875 *
Tejun Heo606a5022012-08-20 14:51:23 -07002876 * Wait until @work has finished execution. @work is guaranteed to be idle
2877 * on return if it hasn't been requeued since flush started.
Tejun Heo401a8d02010-09-16 10:36:00 +02002878 *
2879 * RETURNS:
2880 * %true if flush_work() waited for the work to finish execution,
2881 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002882 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002883bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002884{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002885 struct wq_barrier barr;
2886
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002887 lock_map_acquire(&work->lockdep_map);
2888 lock_map_release(&work->lockdep_map);
2889
Tejun Heo606a5022012-08-20 14:51:23 -07002890 if (start_flush_work(work, &barr)) {
Tejun Heobaf59022010-09-16 10:42:16 +02002891 wait_for_completion(&barr.done);
2892 destroy_work_on_stack(&barr.work);
2893 return true;
Tejun Heo606a5022012-08-20 14:51:23 -07002894 } else {
Tejun Heobaf59022010-09-16 10:42:16 +02002895 return false;
Tejun Heo606a5022012-08-20 14:51:23 -07002896 }
Oleg Nesterovdb700892008-07-25 01:47:49 -07002897}
2898EXPORT_SYMBOL_GPL(flush_work);
2899
Tejun Heo36e227d2012-08-03 10:30:46 -07002900static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
Tejun Heo401a8d02010-09-16 10:36:00 +02002901{
Tejun Heobbb68df2012-08-03 10:30:46 -07002902 unsigned long flags;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002903 int ret;
2904
2905 do {
Tejun Heobbb68df2012-08-03 10:30:46 -07002906 ret = try_to_grab_pending(work, is_dwork, &flags);
2907 /*
2908 * If someone else is canceling, wait for the same event it
2909 * would be waiting for before retrying.
2910 */
2911 if (unlikely(ret == -ENOENT))
Tejun Heo606a5022012-08-20 14:51:23 -07002912 flush_work(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002913 } while (unlikely(ret < 0));
2914
Tejun Heobbb68df2012-08-03 10:30:46 -07002915 /* tell other tasks trying to grab @work to back off */
2916 mark_work_canceling(work);
2917 local_irq_restore(flags);
2918
Tejun Heo606a5022012-08-20 14:51:23 -07002919 flush_work(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002920 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002921 return ret;
2922}
2923
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002924/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002925 * cancel_work_sync - cancel a work and wait for it to finish
2926 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002927 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002928 * Cancel @work and wait for its execution to finish. This function
2929 * can be used even if the work re-queues itself or migrates to
2930 * another workqueue. On return from this function, @work is
2931 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002932 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002933 * cancel_work_sync(&delayed_work->work) must not be used for
2934 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002935 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002936 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002937 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002938 *
2939 * RETURNS:
2940 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002941 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002942bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002943{
Tejun Heo36e227d2012-08-03 10:30:46 -07002944 return __cancel_work_timer(work, false);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002945}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002946EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002947
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002948/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002949 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2950 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002951 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002952 * Delayed timer is cancelled and the pending work is queued for
2953 * immediate execution. Like flush_work(), this function only
2954 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002955 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002956 * RETURNS:
2957 * %true if flush_work() waited for the work to finish execution,
2958 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002959 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002960bool flush_delayed_work(struct delayed_work *dwork)
2961{
Tejun Heo8930cab2012-08-03 10:30:45 -07002962 local_irq_disable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002963 if (del_timer_sync(&dwork->timer))
Tejun Heo12650572012-08-08 09:38:42 -07002964 __queue_work(dwork->cpu,
Tejun Heo401a8d02010-09-16 10:36:00 +02002965 get_work_cwq(&dwork->work)->wq, &dwork->work);
Tejun Heo8930cab2012-08-03 10:30:45 -07002966 local_irq_enable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002967 return flush_work(&dwork->work);
2968}
2969EXPORT_SYMBOL(flush_delayed_work);
2970
2971/**
Tejun Heo57b30ae2012-08-21 13:18:24 -07002972 * cancel_delayed_work - cancel a delayed work
2973 * @dwork: delayed_work to cancel
Tejun Heo09383492010-09-16 10:48:29 +02002974 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002975 * Kill off a pending delayed_work. Returns %true if @dwork was pending
2976 * and canceled; %false if wasn't pending. Note that the work callback
2977 * function may still be running on return, unless it returns %true and the
2978 * work doesn't re-arm itself. Explicitly flush or use
2979 * cancel_delayed_work_sync() to wait on it.
Tejun Heo09383492010-09-16 10:48:29 +02002980 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002981 * This function is safe to call from any context including IRQ handler.
Tejun Heo09383492010-09-16 10:48:29 +02002982 */
Tejun Heo57b30ae2012-08-21 13:18:24 -07002983bool cancel_delayed_work(struct delayed_work *dwork)
Tejun Heo09383492010-09-16 10:48:29 +02002984{
Tejun Heo57b30ae2012-08-21 13:18:24 -07002985 unsigned long flags;
2986 int ret;
2987
2988 do {
2989 ret = try_to_grab_pending(&dwork->work, true, &flags);
2990 } while (unlikely(ret == -EAGAIN));
2991
2992 if (unlikely(ret < 0))
2993 return false;
2994
Tejun Heo7c3eed52013-01-24 11:01:33 -08002995 set_work_pool_and_clear_pending(&dwork->work,
2996 get_work_pool_id(&dwork->work));
Tejun Heo57b30ae2012-08-21 13:18:24 -07002997 local_irq_restore(flags);
Dan Magenheimerc0158ca2012-10-18 16:31:37 -07002998 return ret;
Tejun Heo09383492010-09-16 10:48:29 +02002999}
Tejun Heo57b30ae2012-08-21 13:18:24 -07003000EXPORT_SYMBOL(cancel_delayed_work);
Tejun Heo09383492010-09-16 10:48:29 +02003001
3002/**
Tejun Heo401a8d02010-09-16 10:36:00 +02003003 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
3004 * @dwork: the delayed work cancel
3005 *
3006 * This is cancel_work_sync() for delayed works.
3007 *
3008 * RETURNS:
3009 * %true if @dwork was pending, %false otherwise.
3010 */
3011bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07003012{
Tejun Heo36e227d2012-08-03 10:30:46 -07003013 return __cancel_work_timer(&dwork->work, true);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07003014}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07003015EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003017/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07003018 * schedule_work_on - put work task on a specific cpu
3019 * @cpu: cpu to put the work task on
3020 * @work: job to be done
3021 *
3022 * This puts a job on a specific cpu
3023 */
Tejun Heod4283e92012-08-03 10:30:44 -07003024bool schedule_work_on(int cpu, struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07003025{
Tejun Heod320c032010-06-29 10:07:14 +02003026 return queue_work_on(cpu, system_wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07003027}
3028EXPORT_SYMBOL(schedule_work_on);
3029
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003030/**
Dave Jonesae90dd52006-06-30 01:40:45 -04003031 * schedule_work - put work task in global workqueue
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 * @work: job to be done
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 *
Tejun Heod4283e92012-08-03 10:30:44 -07003034 * Returns %false if @work was already on the kernel-global workqueue and
3035 * %true otherwise.
David Howells52bad642006-11-22 14:54:01 +00003036 *
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003037 * This puts a job in the kernel-global workqueue if it was not already
3038 * queued and leaves it in the same position on the kernel-global
3039 * workqueue otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 */
Tejun Heod4283e92012-08-03 10:30:44 -07003041bool schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042{
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003043 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044}
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003045EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003047/**
3048 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
3049 * @cpu: cpu to use
3050 * @dwork: job to be done
3051 * @delay: number of jiffies to wait
3052 *
3053 * After waiting for a given time this puts a job in the kernel-global
3054 * workqueue on the specified CPU.
3055 */
Tejun Heod4283e92012-08-03 10:30:44 -07003056bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork,
3057 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058{
Tejun Heod320c032010-06-29 10:07:14 +02003059 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060}
Dave Jonesae90dd52006-06-30 01:40:45 -04003061EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
Andrew Mortonb6136772006-06-25 05:47:49 -07003063/**
Tejun Heo0a13c002012-08-03 10:30:44 -07003064 * schedule_delayed_work - put work task in global workqueue after delay
3065 * @dwork: job to be done
3066 * @delay: number of jiffies to wait or 0 for immediate execution
3067 *
3068 * After waiting for a given time this puts a job in the kernel-global
3069 * workqueue.
3070 */
Tejun Heod4283e92012-08-03 10:30:44 -07003071bool schedule_delayed_work(struct delayed_work *dwork, unsigned long delay)
Tejun Heo0a13c002012-08-03 10:30:44 -07003072{
3073 return queue_delayed_work(system_wq, dwork, delay);
3074}
3075EXPORT_SYMBOL(schedule_delayed_work);
3076
3077/**
Tejun Heo31ddd872010-10-19 11:14:49 +02003078 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07003079 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07003080 *
Tejun Heo31ddd872010-10-19 11:14:49 +02003081 * schedule_on_each_cpu() executes @func on each online CPU using the
3082 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07003083 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02003084 *
3085 * RETURNS:
3086 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07003087 */
David Howells65f27f32006-11-22 14:55:48 +00003088int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08003089{
3090 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02003091 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08003092
Andrew Mortonb6136772006-06-25 05:47:49 -07003093 works = alloc_percpu(struct work_struct);
3094 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08003095 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07003096
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003097 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08003098
Christoph Lameter15316ba2006-01-08 01:00:43 -08003099 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01003100 struct work_struct *work = per_cpu_ptr(works, cpu);
3101
3102 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003103 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02003104 }
Tejun Heo93981802009-11-17 14:06:20 -08003105
3106 for_each_online_cpu(cpu)
3107 flush_work(per_cpu_ptr(works, cpu));
3108
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003109 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07003110 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08003111 return 0;
3112}
3113
Alan Sterneef6a7d2010-02-12 17:39:21 +09003114/**
3115 * flush_scheduled_work - ensure that any scheduled work has run to completion.
3116 *
3117 * Forces execution of the kernel-global workqueue and blocks until its
3118 * completion.
3119 *
3120 * Think twice before calling this function! It's very easy to get into
3121 * trouble if you don't take great care. Either of the following situations
3122 * will lead to deadlock:
3123 *
3124 * One of the work items currently on the workqueue needs to acquire
3125 * a lock held by your code or its caller.
3126 *
3127 * Your code is running in the context of a work routine.
3128 *
3129 * They will be detected by lockdep when they occur, but the first might not
3130 * occur very often. It depends on what work items are on the workqueue and
3131 * what locks they need, which you have no control over.
3132 *
3133 * In most situations flushing the entire workqueue is overkill; you merely
3134 * need to know that a particular work item isn't queued and isn't running.
3135 * In such cases you should use cancel_delayed_work_sync() or
3136 * cancel_work_sync() instead.
3137 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138void flush_scheduled_work(void)
3139{
Tejun Heod320c032010-06-29 10:07:14 +02003140 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141}
Dave Jonesae90dd52006-06-30 01:40:45 -04003142EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143
3144/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06003145 * execute_in_process_context - reliably execute the routine with user context
3146 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06003147 * @ew: guaranteed storage for the execute work structure (must
3148 * be available when the work executes)
3149 *
3150 * Executes the function immediately if process context is available,
3151 * otherwise schedules the function for delayed execution.
3152 *
3153 * Returns: 0 - function was executed
3154 * 1 - function was scheduled for execution
3155 */
David Howells65f27f32006-11-22 14:55:48 +00003156int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06003157{
3158 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00003159 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003160 return 0;
3161 }
3162
David Howells65f27f32006-11-22 14:55:48 +00003163 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003164 schedule_work(&ew->work);
3165
3166 return 1;
3167}
3168EXPORT_SYMBOL_GPL(execute_in_process_context);
3169
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170int keventd_up(void)
3171{
Tejun Heod320c032010-06-29 10:07:14 +02003172 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173}
3174
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003175static int alloc_cwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176{
Oleg Nesterov3af244332007-05-09 02:34:09 -07003177 /*
Tejun Heo0f900042010-06-29 10:07:11 +02003178 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
3179 * Make sure that the alignment isn't lower than that of
3180 * unsigned long long.
Oleg Nesterov3af244332007-05-09 02:34:09 -07003181 */
Tejun Heo0f900042010-06-29 10:07:11 +02003182 const size_t size = sizeof(struct cpu_workqueue_struct);
3183 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
3184 __alignof__(unsigned long long));
Oleg Nesterov3af244332007-05-09 02:34:09 -07003185
Lai Jiangshane06ffa12012-03-09 18:03:20 +08003186 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02003187 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
Tejun Heo931ac772010-07-20 11:07:48 +02003188 else {
Tejun Heof3421792010-07-02 10:03:51 +02003189 void *ptr;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01003190
Tejun Heof3421792010-07-02 10:03:51 +02003191 /*
3192 * Allocate enough room to align cwq and put an extra
3193 * pointer at the end pointing back to the originally
3194 * allocated pointer which will be used for free.
3195 */
3196 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
3197 if (ptr) {
3198 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
3199 *(void **)(wq->cpu_wq.single + 1) = ptr;
3200 }
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003201 }
Tejun Heof3421792010-07-02 10:03:51 +02003202
Tejun Heo0415b00d12011-03-24 18:50:09 +01003203 /* just in case, make sure it's actually aligned */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003204 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
3205 return wq->cpu_wq.v ? 0 : -ENOMEM;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003206}
3207
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003208static void free_cwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07003209{
Lai Jiangshane06ffa12012-03-09 18:03:20 +08003210 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02003211 free_percpu(wq->cpu_wq.pcpu);
3212 else if (wq->cpu_wq.single) {
3213 /* the pointer to free is stored right after the cwq */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003214 kfree(*(void **)(wq->cpu_wq.single + 1));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07003215 }
3216}
3217
Tejun Heof3421792010-07-02 10:03:51 +02003218static int wq_clamp_max_active(int max_active, unsigned int flags,
3219 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003220{
Tejun Heof3421792010-07-02 10:03:51 +02003221 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3222
3223 if (max_active < 1 || max_active > lim)
Valentin Ilie044c7822012-08-19 00:52:42 +03003224 pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
3225 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003226
Tejun Heof3421792010-07-02 10:03:51 +02003227 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003228}
3229
Tejun Heob196be82012-01-10 15:11:35 -08003230struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02003231 unsigned int flags,
3232 int max_active,
3233 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08003234 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003235{
Tejun Heob196be82012-01-10 15:11:35 -08003236 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003237 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02003238 unsigned int cpu;
Tejun Heob196be82012-01-10 15:11:35 -08003239 size_t namelen;
3240
3241 /* determine namelen, allocate wq and format name */
3242 va_start(args, lock_name);
3243 va_copy(args1, args);
3244 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3245
3246 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3247 if (!wq)
3248 goto err;
3249
3250 vsnprintf(wq->name, namelen, fmt, args1);
3251 va_end(args);
3252 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003253
Tejun Heof3421792010-07-02 10:03:51 +02003254 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02003255 * Workqueues which may be used during memory reclaim should
3256 * have a rescuer to guarantee forward progress.
3257 */
3258 if (flags & WQ_MEM_RECLAIM)
3259 flags |= WQ_RESCUER;
3260
Tejun Heod320c032010-06-29 10:07:14 +02003261 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003262 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003263
Tejun Heob196be82012-01-10 15:11:35 -08003264 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003265 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003266 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003267 mutex_init(&wq->flush_mutex);
3268 atomic_set(&wq->nr_cwqs_to_flush, 0);
3269 INIT_LIST_HEAD(&wq->flusher_queue);
3270 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003271
Johannes Bergeb13ba82008-01-16 09:51:58 +01003272 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003273 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003274
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003275 if (alloc_cwqs(wq) < 0)
3276 goto err;
3277
Tejun Heof3421792010-07-02 10:03:51 +02003278 for_each_cwq_cpu(cpu, wq) {
Tejun Heo15376632010-06-29 10:07:11 +02003279 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003280 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo32704762012-07-13 22:16:45 -07003281 int pool_idx = (bool)(flags & WQ_HIGHPRI);
Tejun Heo15376632010-06-29 10:07:11 +02003282
Tejun Heo0f900042010-06-29 10:07:11 +02003283 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heo32704762012-07-13 22:16:45 -07003284 cwq->pool = &gcwq->pools[pool_idx];
Tejun Heoc34056a2010-06-29 10:07:11 +02003285 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02003286 cwq->flush_color = -1;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003287 cwq->max_active = max_active;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003288 INIT_LIST_HEAD(&cwq->delayed_works);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003289 }
3290
Tejun Heoe22bee72010-06-29 10:07:14 +02003291 if (flags & WQ_RESCUER) {
3292 struct worker *rescuer;
3293
Tejun Heof2e005a2010-07-20 15:59:09 +02003294 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
Tejun Heoe22bee72010-06-29 10:07:14 +02003295 goto err;
3296
3297 wq->rescuer = rescuer = alloc_worker();
3298 if (!rescuer)
3299 goto err;
3300
Tejun Heo111c2252013-01-17 17:16:24 -08003301 rescuer->rescue_wq = wq;
3302 rescuer->task = kthread_create(rescuer_thread, rescuer, "%s",
Tejun Heob196be82012-01-10 15:11:35 -08003303 wq->name);
Tejun Heoe22bee72010-06-29 10:07:14 +02003304 if (IS_ERR(rescuer->task))
3305 goto err;
3306
Tejun Heoe22bee72010-06-29 10:07:14 +02003307 rescuer->task->flags |= PF_THREAD_BOUND;
3308 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003309 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003310
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003311 /*
3312 * workqueue_lock protects global freeze state and workqueues
3313 * list. Grab it, set max_active accordingly and add the new
3314 * workqueue to workqueues list.
3315 */
Tejun Heo15376632010-06-29 10:07:11 +02003316 spin_lock(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003317
Tejun Heo58a69cb2011-02-16 09:25:31 +01003318 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heof3421792010-07-02 10:03:51 +02003319 for_each_cwq_cpu(cpu, wq)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003320 get_cwq(cpu, wq)->max_active = 0;
3321
Tejun Heo15376632010-06-29 10:07:11 +02003322 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003323
Tejun Heo15376632010-06-29 10:07:11 +02003324 spin_unlock(&workqueue_lock);
3325
Oleg Nesterov3af244332007-05-09 02:34:09 -07003326 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02003327err:
3328 if (wq) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003329 free_cwqs(wq);
Tejun Heof2e005a2010-07-20 15:59:09 +02003330 free_mayday_mask(wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02003331 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003332 kfree(wq);
3333 }
3334 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003335}
Tejun Heod320c032010-06-29 10:07:14 +02003336EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003337
3338/**
3339 * destroy_workqueue - safely terminate a workqueue
3340 * @wq: target workqueue
3341 *
3342 * Safely destroy a workqueue. All work currently pending will be done first.
3343 */
3344void destroy_workqueue(struct workqueue_struct *wq)
3345{
Tejun Heoc8e55f32010-06-29 10:07:12 +02003346 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003347
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003348 /* drain it before proceeding with destruction */
3349 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003350
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003351 /*
3352 * wq list is used to freeze wq, remove from list after
3353 * flushing is complete in case freeze races us.
3354 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003355 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07003356 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003357 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003358
Tejun Heoe22bee72010-06-29 10:07:14 +02003359 /* sanity check */
Tejun Heof3421792010-07-02 10:03:51 +02003360 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02003361 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3362 int i;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003363
Tejun Heo73f53c42010-06-29 10:07:11 +02003364 for (i = 0; i < WORK_NR_COLORS; i++)
3365 BUG_ON(cwq->nr_in_flight[i]);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003366 BUG_ON(cwq->nr_active);
3367 BUG_ON(!list_empty(&cwq->delayed_works));
Tejun Heo73f53c42010-06-29 10:07:11 +02003368 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003369
Tejun Heoe22bee72010-06-29 10:07:14 +02003370 if (wq->flags & WQ_RESCUER) {
3371 kthread_stop(wq->rescuer->task);
Tejun Heof2e005a2010-07-20 15:59:09 +02003372 free_mayday_mask(wq->mayday_mask);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003373 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003374 }
3375
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003376 free_cwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003377 kfree(wq);
3378}
3379EXPORT_SYMBOL_GPL(destroy_workqueue);
3380
Tejun Heodcd989c2010-06-29 10:07:14 +02003381/**
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003382 * cwq_set_max_active - adjust max_active of a cwq
3383 * @cwq: target cpu_workqueue_struct
3384 * @max_active: new max_active value.
3385 *
3386 * Set @cwq->max_active to @max_active and activate delayed works if
3387 * increased.
3388 *
3389 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08003390 * spin_lock_irq(pool->lock).
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003391 */
3392static void cwq_set_max_active(struct cpu_workqueue_struct *cwq, int max_active)
3393{
3394 cwq->max_active = max_active;
3395
3396 while (!list_empty(&cwq->delayed_works) &&
3397 cwq->nr_active < cwq->max_active)
3398 cwq_activate_first_delayed(cwq);
3399}
3400
3401/**
Tejun Heodcd989c2010-06-29 10:07:14 +02003402 * workqueue_set_max_active - adjust max_active of a workqueue
3403 * @wq: target workqueue
3404 * @max_active: new max_active value.
3405 *
3406 * Set max_active of @wq to @max_active.
3407 *
3408 * CONTEXT:
3409 * Don't call from IRQ context.
3410 */
3411void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3412{
3413 unsigned int cpu;
3414
Tejun Heof3421792010-07-02 10:03:51 +02003415 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003416
3417 spin_lock(&workqueue_lock);
3418
3419 wq->saved_max_active = max_active;
3420
Tejun Heof3421792010-07-02 10:03:51 +02003421 for_each_cwq_cpu(cpu, wq) {
Tejun Heo35b6bb62013-01-24 11:01:33 -08003422 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3423 struct worker_pool *pool = cwq->pool;
Tejun Heodcd989c2010-06-29 10:07:14 +02003424
Tejun Heod565ed62013-01-24 11:01:33 -08003425 spin_lock_irq(&pool->lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02003426
Tejun Heo58a69cb2011-02-16 09:25:31 +01003427 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heo35b6bb62013-01-24 11:01:33 -08003428 !(pool->flags & POOL_FREEZING))
3429 cwq_set_max_active(cwq, max_active);
Tejun Heodcd989c2010-06-29 10:07:14 +02003430
Tejun Heod565ed62013-01-24 11:01:33 -08003431 spin_unlock_irq(&pool->lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02003432 }
3433
3434 spin_unlock(&workqueue_lock);
3435}
3436EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3437
3438/**
3439 * workqueue_congested - test whether a workqueue is congested
3440 * @cpu: CPU in question
3441 * @wq: target workqueue
3442 *
3443 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3444 * no synchronization around this function and the test result is
3445 * unreliable and only useful as advisory hints or for debugging.
3446 *
3447 * RETURNS:
3448 * %true if congested, %false otherwise.
3449 */
3450bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3451{
3452 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3453
3454 return !list_empty(&cwq->delayed_works);
3455}
3456EXPORT_SYMBOL_GPL(workqueue_congested);
3457
3458/**
Tejun Heodcd989c2010-06-29 10:07:14 +02003459 * work_busy - test whether a work is currently pending or running
3460 * @work: the work to be tested
3461 *
3462 * Test whether @work is currently pending or running. There is no
3463 * synchronization around this function and the test result is
3464 * unreliable and only useful as advisory hints or for debugging.
3465 * Especially for reentrant wqs, the pending state might hide the
3466 * running state.
3467 *
3468 * RETURNS:
3469 * OR'd bitmask of WORK_BUSY_* bits.
3470 */
3471unsigned int work_busy(struct work_struct *work)
3472{
Tejun Heoc9e7cf22013-01-24 11:01:33 -08003473 struct worker_pool *pool = get_work_pool(work);
Tejun Heodcd989c2010-06-29 10:07:14 +02003474 unsigned long flags;
3475 unsigned int ret = 0;
3476
Tejun Heoc9e7cf22013-01-24 11:01:33 -08003477 if (!pool)
Joonsoo Kim999767b2012-10-21 01:30:06 +09003478 return 0;
Tejun Heodcd989c2010-06-29 10:07:14 +02003479
Tejun Heod565ed62013-01-24 11:01:33 -08003480 spin_lock_irqsave(&pool->lock, flags);
Tejun Heodcd989c2010-06-29 10:07:14 +02003481
3482 if (work_pending(work))
3483 ret |= WORK_BUSY_PENDING;
Tejun Heoc9e7cf22013-01-24 11:01:33 -08003484 if (find_worker_executing_work(pool, work))
Tejun Heodcd989c2010-06-29 10:07:14 +02003485 ret |= WORK_BUSY_RUNNING;
3486
Tejun Heod565ed62013-01-24 11:01:33 -08003487 spin_unlock_irqrestore(&pool->lock, flags);
Tejun Heodcd989c2010-06-29 10:07:14 +02003488
3489 return ret;
3490}
3491EXPORT_SYMBOL_GPL(work_busy);
3492
Tejun Heodb7bccf2010-06-29 10:07:12 +02003493/*
3494 * CPU hotplug.
3495 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003496 * There are two challenges in supporting CPU hotplug. Firstly, there
3497 * are a lot of assumptions on strong associations among work, cwq and
3498 * gcwq which make migrating pending and scheduled works very
3499 * difficult to implement without impacting hot paths. Secondly,
3500 * gcwqs serve mix of short, long and very long running works making
3501 * blocked draining impractical.
3502 *
Tejun Heo24647572013-01-24 11:01:33 -08003503 * This is solved by allowing the pools to be disassociated from the CPU
Tejun Heo628c78e2012-07-17 12:39:27 -07003504 * running as an unbound one and allowing it to be reattached later if the
3505 * cpu comes back online.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003506 */
3507
Tejun Heo60373152012-07-17 12:39:27 -07003508/* claim manager positions of all pools */
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07003509static void gcwq_claim_assoc_and_lock(struct global_cwq *gcwq)
Tejun Heo60373152012-07-17 12:39:27 -07003510{
3511 struct worker_pool *pool;
3512
3513 for_each_worker_pool(pool, gcwq)
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07003514 mutex_lock_nested(&pool->assoc_mutex, pool - gcwq->pools);
Tejun Heod565ed62013-01-24 11:01:33 -08003515
3516 local_irq_disable();
3517 for_each_worker_pool(pool, gcwq)
3518 spin_lock_nested(&pool->lock, pool - gcwq->pools);
Tejun Heo60373152012-07-17 12:39:27 -07003519}
3520
3521/* release manager positions */
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07003522static void gcwq_release_assoc_and_unlock(struct global_cwq *gcwq)
Tejun Heo60373152012-07-17 12:39:27 -07003523{
3524 struct worker_pool *pool;
3525
Tejun Heod565ed62013-01-24 11:01:33 -08003526 for_each_worker_pool(pool, gcwq)
3527 spin_unlock(&pool->lock);
3528 local_irq_enable();
3529
Tejun Heo60373152012-07-17 12:39:27 -07003530 for_each_worker_pool(pool, gcwq)
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07003531 mutex_unlock(&pool->assoc_mutex);
Tejun Heo60373152012-07-17 12:39:27 -07003532}
3533
Tejun Heo628c78e2012-07-17 12:39:27 -07003534static void gcwq_unbind_fn(struct work_struct *work)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003535{
Tejun Heo628c78e2012-07-17 12:39:27 -07003536 struct global_cwq *gcwq = get_gcwq(smp_processor_id());
Tejun Heo4ce62e92012-07-13 22:16:44 -07003537 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003538 struct worker *worker;
3539 struct hlist_node *pos;
3540 int i;
3541
Tejun Heoec22ca52013-01-24 11:01:33 -08003542 BUG_ON(gcwq->pools[0].cpu != smp_processor_id());
Tejun Heodb7bccf2010-06-29 10:07:12 +02003543
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07003544 gcwq_claim_assoc_and_lock(gcwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02003545
Tejun Heof2d5a0e2012-07-17 12:39:26 -07003546 /*
3547 * We've claimed all manager positions. Make all workers unbound
3548 * and set DISASSOCIATED. Before this, all workers except for the
3549 * ones which are still executing works from before the last CPU
3550 * down must be on the cpu. After this, they may become diasporas.
3551 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -08003552 for_each_worker_pool(pool, gcwq) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07003553 list_for_each_entry(worker, &pool->idle_list, entry)
Tejun Heo403c8212012-07-17 12:39:27 -07003554 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003555
Tejun Heoc9e7cf22013-01-24 11:01:33 -08003556 for_each_busy_worker(worker, i, pos, pool)
3557 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003558
Tejun Heo24647572013-01-24 11:01:33 -08003559 pool->flags |= POOL_DISASSOCIATED;
Tejun Heoc9e7cf22013-01-24 11:01:33 -08003560 }
Tejun Heof2d5a0e2012-07-17 12:39:26 -07003561
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07003562 gcwq_release_assoc_and_unlock(gcwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02003563
3564 /*
Tejun Heo628c78e2012-07-17 12:39:27 -07003565 * Call schedule() so that we cross rq->lock and thus can guarantee
3566 * sched callbacks see the %WORKER_UNBOUND flag. This is necessary
3567 * as scheduler callbacks may be invoked from other cpus.
3568 */
3569 schedule();
3570
3571 /*
3572 * Sched callbacks are disabled now. Zap nr_running. After this,
3573 * nr_running stays zero and need_more_worker() and keep_working()
3574 * are always true as long as the worklist is not empty. @gcwq now
3575 * behaves as unbound (in terms of concurrency management) gcwq
3576 * which is served by workers tied to the CPU.
3577 *
3578 * On return from this function, the current worker would trigger
3579 * unbound chain execution of pending work items if other workers
3580 * didn't already.
Tejun Heoe22bee72010-06-29 10:07:14 +02003581 */
Tejun Heo4ce62e92012-07-13 22:16:44 -07003582 for_each_worker_pool(pool, gcwq)
3583 atomic_set(get_pool_nr_running(pool), 0);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003584}
3585
Tejun Heo8db25e72012-07-17 12:39:28 -07003586/*
3587 * Workqueues should be brought up before normal priority CPU notifiers.
3588 * This will be registered high priority CPU notifier.
3589 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07003590static int __cpuinit workqueue_cpu_up_callback(struct notifier_block *nfb,
Tejun Heo8db25e72012-07-17 12:39:28 -07003591 unsigned long action,
3592 void *hcpu)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003593{
3594 unsigned int cpu = (unsigned long)hcpu;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003595 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003596 struct worker_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597
Tejun Heo8db25e72012-07-17 12:39:28 -07003598 switch (action & ~CPU_TASKS_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 case CPU_UP_PREPARE:
Tejun Heo4ce62e92012-07-13 22:16:44 -07003600 for_each_worker_pool(pool, gcwq) {
Tejun Heo3ce63372012-07-17 12:39:27 -07003601 struct worker *worker;
3602
3603 if (pool->nr_workers)
3604 continue;
3605
3606 worker = create_worker(pool);
3607 if (!worker)
3608 return NOTIFY_BAD;
3609
Tejun Heod565ed62013-01-24 11:01:33 -08003610 spin_lock_irq(&pool->lock);
Tejun Heo3ce63372012-07-17 12:39:27 -07003611 start_worker(worker);
Tejun Heod565ed62013-01-24 11:01:33 -08003612 spin_unlock_irq(&pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003614 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003615
Tejun Heo65758202012-07-17 12:39:26 -07003616 case CPU_DOWN_FAILED:
3617 case CPU_ONLINE:
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07003618 gcwq_claim_assoc_and_lock(gcwq);
Tejun Heo24647572013-01-24 11:01:33 -08003619 for_each_worker_pool(pool, gcwq)
3620 pool->flags &= ~POOL_DISASSOCIATED;
Tejun Heo8db25e72012-07-17 12:39:28 -07003621 rebind_workers(gcwq);
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07003622 gcwq_release_assoc_and_unlock(gcwq);
Tejun Heo8db25e72012-07-17 12:39:28 -07003623 break;
Tejun Heo65758202012-07-17 12:39:26 -07003624 }
3625 return NOTIFY_OK;
3626}
3627
3628/*
3629 * Workqueues should be brought down after normal priority CPU notifiers.
3630 * This will be registered as low priority CPU notifier.
3631 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07003632static int __cpuinit workqueue_cpu_down_callback(struct notifier_block *nfb,
Tejun Heo65758202012-07-17 12:39:26 -07003633 unsigned long action,
3634 void *hcpu)
3635{
Tejun Heo8db25e72012-07-17 12:39:28 -07003636 unsigned int cpu = (unsigned long)hcpu;
3637 struct work_struct unbind_work;
3638
Tejun Heo65758202012-07-17 12:39:26 -07003639 switch (action & ~CPU_TASKS_FROZEN) {
3640 case CPU_DOWN_PREPARE:
Tejun Heo8db25e72012-07-17 12:39:28 -07003641 /* unbinding should happen on the local CPU */
3642 INIT_WORK_ONSTACK(&unbind_work, gcwq_unbind_fn);
Joonsoo Kim7635d2f2012-08-15 23:25:41 +09003643 queue_work_on(cpu, system_highpri_wq, &unbind_work);
Tejun Heo8db25e72012-07-17 12:39:28 -07003644 flush_work(&unbind_work);
3645 break;
Tejun Heo65758202012-07-17 12:39:26 -07003646 }
3647 return NOTIFY_OK;
3648}
3649
Rusty Russell2d3854a2008-11-05 13:39:10 +11003650#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003651
Rusty Russell2d3854a2008-11-05 13:39:10 +11003652struct work_for_cpu {
Tejun Heoed48ece2012-09-18 12:48:43 -07003653 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003654 long (*fn)(void *);
3655 void *arg;
3656 long ret;
3657};
3658
Tejun Heoed48ece2012-09-18 12:48:43 -07003659static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003660{
Tejun Heoed48ece2012-09-18 12:48:43 -07003661 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
3662
Rusty Russell2d3854a2008-11-05 13:39:10 +11003663 wfc->ret = wfc->fn(wfc->arg);
3664}
3665
3666/**
3667 * work_on_cpu - run a function in user context on a particular cpu
3668 * @cpu: the cpu to run on
3669 * @fn: the function to run
3670 * @arg: the function arg
3671 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003672 * This will return the value @fn returns.
3673 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06003674 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003675 */
3676long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3677{
Tejun Heoed48ece2012-09-18 12:48:43 -07003678 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003679
Tejun Heoed48ece2012-09-18 12:48:43 -07003680 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
3681 schedule_work_on(cpu, &wfc.work);
3682 flush_work(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003683 return wfc.ret;
3684}
3685EXPORT_SYMBOL_GPL(work_on_cpu);
3686#endif /* CONFIG_SMP */
3687
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003688#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303689
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003690/**
3691 * freeze_workqueues_begin - begin freezing workqueues
3692 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003693 * Start freezing workqueues. After this function returns, all freezable
3694 * workqueues will queue new works to their frozen_works list instead of
3695 * gcwq->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003696 *
3697 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08003698 * Grabs and releases workqueue_lock and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003699 */
3700void freeze_workqueues_begin(void)
3701{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003702 unsigned int cpu;
3703
3704 spin_lock(&workqueue_lock);
3705
3706 BUG_ON(workqueue_freezing);
3707 workqueue_freezing = true;
3708
Tejun Heof3421792010-07-02 10:03:51 +02003709 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003710 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo35b6bb62013-01-24 11:01:33 -08003711 struct worker_pool *pool;
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003712 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003713
Tejun Heod565ed62013-01-24 11:01:33 -08003714 local_irq_disable();
Tejun Heo8b03ae32010-06-29 10:07:12 +02003715
Tejun Heo35b6bb62013-01-24 11:01:33 -08003716 for_each_worker_pool(pool, gcwq) {
Tejun Heod565ed62013-01-24 11:01:33 -08003717 spin_lock_nested(&pool->lock, pool - gcwq->pools);
3718
Tejun Heo35b6bb62013-01-24 11:01:33 -08003719 WARN_ON_ONCE(pool->flags & POOL_FREEZING);
3720 pool->flags |= POOL_FREEZING;
3721 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003722
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003723 list_for_each_entry(wq, &workqueues, list) {
3724 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3725
Tejun Heo58a69cb2011-02-16 09:25:31 +01003726 if (cwq && wq->flags & WQ_FREEZABLE)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003727 cwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003728 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003729
Tejun Heod565ed62013-01-24 11:01:33 -08003730 for_each_worker_pool(pool, gcwq)
3731 spin_unlock(&pool->lock);
3732 local_irq_enable();
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003733 }
3734
3735 spin_unlock(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003737
3738/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003739 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003740 *
3741 * Check whether freezing is complete. This function must be called
3742 * between freeze_workqueues_begin() and thaw_workqueues().
3743 *
3744 * CONTEXT:
3745 * Grabs and releases workqueue_lock.
3746 *
3747 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01003748 * %true if some freezable workqueues are still busy. %false if freezing
3749 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003750 */
3751bool freeze_workqueues_busy(void)
3752{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003753 unsigned int cpu;
3754 bool busy = false;
3755
3756 spin_lock(&workqueue_lock);
3757
3758 BUG_ON(!workqueue_freezing);
3759
Tejun Heof3421792010-07-02 10:03:51 +02003760 for_each_gcwq_cpu(cpu) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003761 struct workqueue_struct *wq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003762 /*
3763 * nr_active is monotonically decreasing. It's safe
3764 * to peek without lock.
3765 */
3766 list_for_each_entry(wq, &workqueues, list) {
3767 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3768
Tejun Heo58a69cb2011-02-16 09:25:31 +01003769 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003770 continue;
3771
3772 BUG_ON(cwq->nr_active < 0);
3773 if (cwq->nr_active) {
3774 busy = true;
3775 goto out_unlock;
3776 }
3777 }
3778 }
3779out_unlock:
3780 spin_unlock(&workqueue_lock);
3781 return busy;
3782}
3783
3784/**
3785 * thaw_workqueues - thaw workqueues
3786 *
3787 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo7e116292010-06-29 10:07:13 +02003788 * frozen works are transferred to their respective gcwq worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003789 *
3790 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08003791 * Grabs and releases workqueue_lock and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003792 */
3793void thaw_workqueues(void)
3794{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003795 unsigned int cpu;
3796
3797 spin_lock(&workqueue_lock);
3798
3799 if (!workqueue_freezing)
3800 goto out_unlock;
3801
Tejun Heof3421792010-07-02 10:03:51 +02003802 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003803 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003804 struct worker_pool *pool;
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003805 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003806
Tejun Heod565ed62013-01-24 11:01:33 -08003807 local_irq_disable();
Tejun Heo8b03ae32010-06-29 10:07:12 +02003808
Tejun Heo35b6bb62013-01-24 11:01:33 -08003809 for_each_worker_pool(pool, gcwq) {
Tejun Heod565ed62013-01-24 11:01:33 -08003810 spin_lock_nested(&pool->lock, pool - gcwq->pools);
3811
Tejun Heo35b6bb62013-01-24 11:01:33 -08003812 WARN_ON_ONCE(!(pool->flags & POOL_FREEZING));
3813 pool->flags &= ~POOL_FREEZING;
3814 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003815
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003816 list_for_each_entry(wq, &workqueues, list) {
3817 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3818
Tejun Heo58a69cb2011-02-16 09:25:31 +01003819 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003820 continue;
3821
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003822 /* restore max_active and repopulate worklist */
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003823 cwq_set_max_active(cwq, wq->saved_max_active);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003824 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003825
Tejun Heod565ed62013-01-24 11:01:33 -08003826 for_each_worker_pool(pool, gcwq) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07003827 wake_up_worker(pool);
Tejun Heod565ed62013-01-24 11:01:33 -08003828 spin_unlock(&pool->lock);
3829 }
3830 local_irq_enable();
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003831 }
3832
3833 workqueue_freezing = false;
3834out_unlock:
3835 spin_unlock(&workqueue_lock);
3836}
3837#endif /* CONFIG_FREEZER */
3838
Suresh Siddha6ee05782010-07-30 14:57:37 -07003839static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840{
Tejun Heoc34056a2010-06-29 10:07:11 +02003841 unsigned int cpu;
3842
Tejun Heo7c3eed52013-01-24 11:01:33 -08003843 /* make sure we have enough bits for OFFQ pool ID */
3844 BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) <
3845 WORK_CPU_LAST * NR_STD_WORKER_POOLS);
Tejun Heob5490072012-08-03 10:30:46 -07003846
Tejun Heo65758202012-07-17 12:39:26 -07003847 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
Lai Jiangshana5b4e572012-09-18 09:59:23 -07003848 hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003849
3850 /* initialize gcwqs */
Tejun Heof3421792010-07-02 10:03:51 +02003851 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003852 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003853 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003854
Tejun Heo4ce62e92012-07-13 22:16:44 -07003855 for_each_worker_pool(pool, gcwq) {
3856 pool->gcwq = gcwq;
Tejun Heod565ed62013-01-24 11:01:33 -08003857 spin_lock_init(&pool->lock);
Tejun Heoec22ca52013-01-24 11:01:33 -08003858 pool->cpu = cpu;
Tejun Heo24647572013-01-24 11:01:33 -08003859 pool->flags |= POOL_DISASSOCIATED;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003860 INIT_LIST_HEAD(&pool->worklist);
3861 INIT_LIST_HEAD(&pool->idle_list);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08003862 hash_init(pool->busy_hash);
Tejun Heoe22bee72010-06-29 10:07:14 +02003863
Tejun Heo4ce62e92012-07-13 22:16:44 -07003864 init_timer_deferrable(&pool->idle_timer);
3865 pool->idle_timer.function = idle_worker_timeout;
3866 pool->idle_timer.data = (unsigned long)pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003867
Tejun Heo4ce62e92012-07-13 22:16:44 -07003868 setup_timer(&pool->mayday_timer, gcwq_mayday_timeout,
3869 (unsigned long)pool);
3870
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07003871 mutex_init(&pool->assoc_mutex);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003872 ida_init(&pool->worker_ida);
Tejun Heo9daf9e62013-01-24 11:01:33 -08003873
3874 /* alloc pool ID */
3875 BUG_ON(worker_pool_assign_id(pool));
Tejun Heo4ce62e92012-07-13 22:16:44 -07003876 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003877 }
3878
Tejun Heoe22bee72010-06-29 10:07:14 +02003879 /* create the initial worker */
Tejun Heof3421792010-07-02 10:03:51 +02003880 for_each_online_gcwq_cpu(cpu) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003881 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003882 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003883
Tejun Heo4ce62e92012-07-13 22:16:44 -07003884 for_each_worker_pool(pool, gcwq) {
3885 struct worker *worker;
3886
Tejun Heo24647572013-01-24 11:01:33 -08003887 if (cpu != WORK_CPU_UNBOUND)
3888 pool->flags &= ~POOL_DISASSOCIATED;
3889
Tejun Heobc2ae0f2012-07-17 12:39:27 -07003890 worker = create_worker(pool);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003891 BUG_ON(!worker);
Tejun Heod565ed62013-01-24 11:01:33 -08003892 spin_lock_irq(&pool->lock);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003893 start_worker(worker);
Tejun Heod565ed62013-01-24 11:01:33 -08003894 spin_unlock_irq(&pool->lock);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003895 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003896 }
3897
Tejun Heod320c032010-06-29 10:07:14 +02003898 system_wq = alloc_workqueue("events", 0, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09003899 system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
Tejun Heod320c032010-06-29 10:07:14 +02003900 system_long_wq = alloc_workqueue("events_long", 0, 0);
Tejun Heof3421792010-07-02 10:03:51 +02003901 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3902 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01003903 system_freezable_wq = alloc_workqueue("events_freezable",
3904 WQ_FREEZABLE, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09003905 BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
Tejun Heoae930e02012-08-20 14:51:23 -07003906 !system_unbound_wq || !system_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07003907 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908}
Suresh Siddha6ee05782010-07-30 14:57:37 -07003909early_initcall(init_workqueues);