blob: 85bd3409b9f53cb753c4ced7e0e9cbf3458e54d6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tejun Heoc54fce62010-09-10 16:51:36 +02002 * kernel/workqueue.c - generic async execution with shared worker pool
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Tejun Heoc54fce62010-09-10 16:51:36 +02004 * Copyright (C) 2002 Ingo Molnar
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Tejun Heoc54fce62010-09-10 16:51:36 +02006 * Derived from the taskqueue/keventd code by:
7 * David Woodhouse <dwmw2@infradead.org>
8 * Andrew Morton
9 * Kai Petzke <wpp@marie.physik.tu-berlin.de>
10 * Theodore Ts'o <tytso@mit.edu>
Christoph Lameter89ada672005-10-30 15:01:59 -080011 *
Christoph Lametercde53532008-07-04 09:59:22 -070012 * Made to use alloc_percpu by Christoph Lameter.
Tejun Heoc54fce62010-09-10 16:51:36 +020013 *
14 * Copyright (C) 2010 SUSE Linux Products GmbH
15 * Copyright (C) 2010 Tejun Heo <tj@kernel.org>
16 *
17 * This is the generic async execution mechanism. Work items as are
18 * executed in process context. The worker pool is shared and
19 * automatically managed. There is one worker pool for each CPU and
20 * one extra for works which are better served by workers which are
21 * not bound to any specific CPU.
22 *
23 * Please read Documentation/workqueue.txt for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 */
25
Paul Gortmaker9984de12011-05-23 14:51:41 -040026#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/kernel.h>
28#include <linux/sched.h>
29#include <linux/init.h>
30#include <linux/signal.h>
31#include <linux/completion.h>
32#include <linux/workqueue.h>
33#include <linux/slab.h>
34#include <linux/cpu.h>
35#include <linux/notifier.h>
36#include <linux/kthread.h>
James Bottomley1fa44ec2006-02-23 12:43:43 -060037#include <linux/hardirq.h>
Christoph Lameter46934022006-10-11 01:21:26 -070038#include <linux/mempolicy.h>
Rafael J. Wysocki341a5952006-12-06 20:34:49 -080039#include <linux/freezer.h>
Peter Zijlstrad5abe662006-12-06 20:37:26 -080040#include <linux/kallsyms.h>
41#include <linux/debug_locks.h>
Johannes Berg4e6045f2007-10-18 23:39:55 -070042#include <linux/lockdep.h>
Tejun Heoc34056a2010-06-29 10:07:11 +020043#include <linux/idr.h>
Tejun Heoe22bee72010-06-29 10:07:14 +020044
45#include "workqueue_sched.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Tejun Heoc8e55f32010-06-29 10:07:12 +020047enum {
Tejun Heobc2ae0f2012-07-17 12:39:27 -070048 /*
49 * global_cwq flags
50 *
51 * A bound gcwq is either associated or disassociated with its CPU.
52 * While associated (!DISASSOCIATED), all workers are bound to the
53 * CPU and none has %WORKER_UNBOUND set and concurrency management
54 * is in effect.
55 *
56 * While DISASSOCIATED, the cpu may be offline and all workers have
57 * %WORKER_UNBOUND set and concurrency management disabled, and may
58 * be executing on any CPU. The gcwq behaves as an unbound one.
59 *
60 * Note that DISASSOCIATED can be flipped only while holding
61 * managership of all pools on the gcwq to avoid changing binding
62 * state while create_worker() is in progress.
63 */
Tejun Heo11ebea52012-07-12 14:46:37 -070064 GCWQ_DISASSOCIATED = 1 << 0, /* cpu can't serve workers */
65 GCWQ_FREEZING = 1 << 1, /* freeze in progress */
66
67 /* pool flags */
68 POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
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 Heoe22bee72010-06-29 10:07:14 +020075 WORKER_REBIND = 1 << 5, /* mom is home, come back */
Tejun Heofb0e7be2010-06-29 10:07:15 +020076 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
Tejun Heof3421792010-07-02 10:03:51 +020077 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
Tejun Heoe22bee72010-06-29 10:07:14 +020078
Tejun Heo403c8212012-07-17 12:39:27 -070079 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_REBIND | WORKER_UNBOUND |
80 WORKER_CPU_INTENSIVE,
Tejun Heodb7bccf2010-06-29 10:07:12 +020081
Tejun Heo32704762012-07-13 22:16:45 -070082 NR_WORKER_POOLS = 2, /* # worker pools per gcwq */
Tejun Heo4ce62e92012-07-13 22:16:44 -070083
Tejun Heoc8e55f32010-06-29 10:07:12 +020084 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
85 BUSY_WORKER_HASH_SIZE = 1 << BUSY_WORKER_HASH_ORDER,
86 BUSY_WORKER_HASH_MASK = BUSY_WORKER_HASH_SIZE - 1,
Tejun Heodb7bccf2010-06-29 10:07:12 +020087
Tejun Heoe22bee72010-06-29 10:07:14 +020088 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
89 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
90
Tejun Heo3233cdb2011-02-16 18:10:19 +010091 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
92 /* call for help after 10ms
93 (min two ticks) */
Tejun Heoe22bee72010-06-29 10:07:14 +020094 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
95 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heoe22bee72010-06-29 10:07:14 +020096
97 /*
98 * Rescue workers are used only on emergencies and shared by
99 * all cpus. Give -20.
100 */
101 RESCUER_NICE_LEVEL = -20,
Tejun Heo32704762012-07-13 22:16:45 -0700102 HIGHPRI_NICE_LEVEL = -20,
Tejun Heoc8e55f32010-06-29 10:07:12 +0200103};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105/*
Tejun Heo4690c4a2010-06-29 10:07:10 +0200106 * Structure fields follow one of the following exclusion rules.
107 *
Tejun Heoe41e7042010-08-24 14:22:47 +0200108 * I: Modifiable by initialization/destruction paths and read-only for
109 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200110 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200111 * P: Preemption protected. Disabling preemption is enough and should
112 * only be modified and accessed from the local cpu.
113 *
Tejun Heo8b03ae32010-06-29 10:07:12 +0200114 * L: gcwq->lock protected. Access with gcwq->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200115 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200116 * X: During normal operation, modification requires gcwq->lock and
117 * should be done only from local cpu. Either disabling preemption
118 * on local cpu or grabbing gcwq->lock is enough for read access.
Tejun Heof3421792010-07-02 10:03:51 +0200119 * If GCWQ_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200120 *
Tejun Heo73f53c42010-06-29 10:07:11 +0200121 * F: wq->flush_mutex protected.
122 *
Tejun Heo4690c4a2010-06-29 10:07:10 +0200123 * W: workqueue_lock protected.
124 */
125
Tejun Heo8b03ae32010-06-29 10:07:12 +0200126struct global_cwq;
Tejun Heobd7bdd42012-07-12 14:46:37 -0700127struct worker_pool;
Tejun Heo25511a42012-07-17 12:39:27 -0700128struct idle_rebind;
Tejun Heoc34056a2010-06-29 10:07:11 +0200129
Tejun Heoe22bee72010-06-29 10:07:14 +0200130/*
131 * The poor guys doing the actual heavy lifting. All on-duty workers
132 * are either serving the manager role, on idle list or on busy hash.
133 */
Tejun Heoc34056a2010-06-29 10:07:11 +0200134struct worker {
Tejun Heoc8e55f32010-06-29 10:07:12 +0200135 /* on idle list while idle, on busy hash table while busy */
136 union {
137 struct list_head entry; /* L: while idle */
138 struct hlist_node hentry; /* L: while busy */
139 };
140
Tejun Heoc34056a2010-06-29 10:07:11 +0200141 struct work_struct *current_work; /* L: work being processed */
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200142 struct cpu_workqueue_struct *current_cwq; /* L: current_work's cwq */
Tejun Heoaffee4b2010-06-29 10:07:12 +0200143 struct list_head scheduled; /* L: scheduled works */
Tejun Heoc34056a2010-06-29 10:07:11 +0200144 struct task_struct *task; /* I: worker task */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700145 struct worker_pool *pool; /* I: the associated pool */
Tejun Heoe22bee72010-06-29 10:07:14 +0200146 /* 64 bytes boundary on 64bit, 32 on 32bit */
147 unsigned long last_active; /* L: last active timestamp */
148 unsigned int flags; /* X: flags */
Tejun Heoc34056a2010-06-29 10:07:11 +0200149 int id; /* I: worker id */
Tejun Heo25511a42012-07-17 12:39:27 -0700150
151 /* for rebinding worker to CPU */
152 struct idle_rebind *idle_rebind; /* L: for idle worker */
153 struct work_struct rebind_work; /* L: for busy worker */
Tejun Heoc34056a2010-06-29 10:07:11 +0200154};
155
Tejun Heobd7bdd42012-07-12 14:46:37 -0700156struct worker_pool {
157 struct global_cwq *gcwq; /* I: the owning gcwq */
Tejun Heo11ebea52012-07-12 14:46:37 -0700158 unsigned int flags; /* X: flags */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700159
160 struct list_head worklist; /* L: list of pending works */
161 int nr_workers; /* L: total number of workers */
162 int nr_idle; /* L: currently idle ones */
163
164 struct list_head idle_list; /* X: list of idle workers */
165 struct timer_list idle_timer; /* L: worker idle timeout */
166 struct timer_list mayday_timer; /* L: SOS timer for workers */
167
Tejun Heo60373152012-07-17 12:39:27 -0700168 struct mutex manager_mutex; /* mutex manager should hold */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700169 struct ida worker_ida; /* L: for worker IDs */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700170};
171
Tejun Heo4690c4a2010-06-29 10:07:10 +0200172/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200173 * Global per-cpu workqueue. There's one and only one for each cpu
174 * and all works are queued and processed here regardless of their
175 * target workqueues.
Tejun Heo8b03ae32010-06-29 10:07:12 +0200176 */
177struct global_cwq {
178 spinlock_t lock; /* the gcwq lock */
179 unsigned int cpu; /* I: the associated cpu */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200180 unsigned int flags; /* L: GCWQ_* flags */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200181
Tejun Heobd7bdd42012-07-12 14:46:37 -0700182 /* workers are chained either in busy_hash or pool idle_list */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200183 struct hlist_head busy_hash[BUSY_WORKER_HASH_SIZE];
184 /* L: hash of busy workers */
185
Joonsoo Kim330dad52012-08-15 23:25:36 +0900186 struct worker_pool pools[NR_WORKER_POOLS];
187 /* normal and highpri pools */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200188
Tejun Heo25511a42012-07-17 12:39:27 -0700189 wait_queue_head_t rebind_hold; /* rebind hold wait */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200190} ____cacheline_aligned_in_smp;
191
192/*
Tejun Heo502ca9d2010-06-29 10:07:13 +0200193 * The per-CPU workqueue. The lower WORK_STRUCT_FLAG_BITS of
Tejun Heo0f900042010-06-29 10:07:11 +0200194 * work_struct->data are used for flags and thus cwqs need to be
195 * aligned at two's power of the number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 */
197struct cpu_workqueue_struct {
Tejun Heobd7bdd42012-07-12 14:46:37 -0700198 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200199 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200200 int work_color; /* L: current color */
201 int flush_color; /* L: flushing color */
202 int nr_in_flight[WORK_NR_COLORS];
203 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200204 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200205 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200206 struct list_head delayed_works; /* L: delayed works */
Tejun Heo0f900042010-06-29 10:07:11 +0200207};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200210 * Structure used to wait for workqueue flush.
211 */
212struct wq_flusher {
213 struct list_head list; /* F: list of flushers */
214 int flush_color; /* F: flush color waiting for */
215 struct completion done; /* flush completion */
216};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Tejun Heo73f53c42010-06-29 10:07:11 +0200218/*
Tejun Heof2e005a2010-07-20 15:59:09 +0200219 * All cpumasks are assumed to be always set on UP and thus can't be
220 * used to determine whether there's something to be done.
221 */
222#ifdef CONFIG_SMP
223typedef cpumask_var_t mayday_mask_t;
224#define mayday_test_and_set_cpu(cpu, mask) \
225 cpumask_test_and_set_cpu((cpu), (mask))
226#define mayday_clear_cpu(cpu, mask) cpumask_clear_cpu((cpu), (mask))
227#define for_each_mayday_cpu(cpu, mask) for_each_cpu((cpu), (mask))
Tejun Heo9c375472010-08-31 11:18:34 +0200228#define alloc_mayday_mask(maskp, gfp) zalloc_cpumask_var((maskp), (gfp))
Tejun Heof2e005a2010-07-20 15:59:09 +0200229#define free_mayday_mask(mask) free_cpumask_var((mask))
230#else
231typedef unsigned long mayday_mask_t;
232#define mayday_test_and_set_cpu(cpu, mask) test_and_set_bit(0, &(mask))
233#define mayday_clear_cpu(cpu, mask) clear_bit(0, &(mask))
234#define for_each_mayday_cpu(cpu, mask) if ((cpu) = 0, (mask))
235#define alloc_mayday_mask(maskp, gfp) true
236#define free_mayday_mask(mask) do { } while (0)
237#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239/*
240 * The externally visible workqueue abstraction is an array of
241 * per-CPU workqueues:
242 */
243struct workqueue_struct {
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200244 unsigned int flags; /* W: WQ_* flags */
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200245 union {
246 struct cpu_workqueue_struct __percpu *pcpu;
247 struct cpu_workqueue_struct *single;
248 unsigned long v;
249 } cpu_wq; /* I: cwq's */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200250 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200251
252 struct mutex flush_mutex; /* protects wq flushing */
253 int work_color; /* F: current work color */
254 int flush_color; /* F: current flush color */
255 atomic_t nr_cwqs_to_flush; /* flush in progress */
256 struct wq_flusher *first_flusher; /* F: first flusher */
257 struct list_head flusher_queue; /* F: flush waiters */
258 struct list_head flusher_overflow; /* F: flush overflow list */
259
Tejun Heof2e005a2010-07-20 15:59:09 +0200260 mayday_mask_t mayday_mask; /* cpus requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200261 struct worker *rescuer; /* I: rescue worker */
262
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200263 int nr_drainers; /* W: drain in progress */
Tejun Heodcd989c2010-06-29 10:07:14 +0200264 int saved_max_active; /* W: saved cwq max_active */
Johannes Berg4e6045f2007-10-18 23:39:55 -0700265#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200266 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700267#endif
Tejun Heob196be82012-01-10 15:11:35 -0800268 char name[]; /* I: workqueue name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269};
270
Tejun Heod320c032010-06-29 10:07:14 +0200271struct workqueue_struct *system_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200272EXPORT_SYMBOL_GPL(system_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300273struct workqueue_struct *system_highpri_wq __read_mostly;
Joonsoo Kim1aabe902012-08-15 23:25:39 +0900274EXPORT_SYMBOL_GPL(system_highpri_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300275struct workqueue_struct *system_long_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200276EXPORT_SYMBOL_GPL(system_long_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300277struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200278EXPORT_SYMBOL_GPL(system_unbound_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300279struct workqueue_struct *system_freezable_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100280EXPORT_SYMBOL_GPL(system_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200281
Tejun Heo97bd2342010-10-05 10:41:14 +0200282#define CREATE_TRACE_POINTS
283#include <trace/events/workqueue.h>
284
Tejun Heo4ce62e92012-07-13 22:16:44 -0700285#define for_each_worker_pool(pool, gcwq) \
Tejun Heo32704762012-07-13 22:16:45 -0700286 for ((pool) = &(gcwq)->pools[0]; \
287 (pool) < &(gcwq)->pools[NR_WORKER_POOLS]; (pool)++)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700288
Tejun Heodb7bccf2010-06-29 10:07:12 +0200289#define for_each_busy_worker(worker, i, pos, gcwq) \
290 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \
291 hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry)
292
Tejun Heof3421792010-07-02 10:03:51 +0200293static inline int __next_gcwq_cpu(int cpu, const struct cpumask *mask,
294 unsigned int sw)
295{
296 if (cpu < nr_cpu_ids) {
297 if (sw & 1) {
298 cpu = cpumask_next(cpu, mask);
299 if (cpu < nr_cpu_ids)
300 return cpu;
301 }
302 if (sw & 2)
303 return WORK_CPU_UNBOUND;
304 }
305 return WORK_CPU_NONE;
306}
307
308static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
309 struct workqueue_struct *wq)
310{
311 return __next_gcwq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2);
312}
313
Tejun Heo09884952010-08-01 11:50:12 +0200314/*
315 * CPU iterators
316 *
317 * An extra gcwq is defined for an invalid cpu number
318 * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any
319 * specific CPU. The following iterators are similar to
320 * for_each_*_cpu() iterators but also considers the unbound gcwq.
321 *
322 * for_each_gcwq_cpu() : possible CPUs + WORK_CPU_UNBOUND
323 * for_each_online_gcwq_cpu() : online CPUs + WORK_CPU_UNBOUND
324 * for_each_cwq_cpu() : possible CPUs for bound workqueues,
325 * WORK_CPU_UNBOUND for unbound workqueues
326 */
Tejun Heof3421792010-07-02 10:03:51 +0200327#define for_each_gcwq_cpu(cpu) \
328 for ((cpu) = __next_gcwq_cpu(-1, cpu_possible_mask, 3); \
329 (cpu) < WORK_CPU_NONE; \
330 (cpu) = __next_gcwq_cpu((cpu), cpu_possible_mask, 3))
331
332#define for_each_online_gcwq_cpu(cpu) \
333 for ((cpu) = __next_gcwq_cpu(-1, cpu_online_mask, 3); \
334 (cpu) < WORK_CPU_NONE; \
335 (cpu) = __next_gcwq_cpu((cpu), cpu_online_mask, 3))
336
337#define for_each_cwq_cpu(cpu, wq) \
338 for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, (wq)); \
339 (cpu) < WORK_CPU_NONE; \
340 (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq)))
341
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900342#ifdef CONFIG_DEBUG_OBJECTS_WORK
343
344static struct debug_obj_descr work_debug_descr;
345
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100346static void *work_debug_hint(void *addr)
347{
348 return ((struct work_struct *) addr)->func;
349}
350
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900351/*
352 * fixup_init is called when:
353 * - an active object is initialized
354 */
355static int work_fixup_init(void *addr, enum debug_obj_state state)
356{
357 struct work_struct *work = addr;
358
359 switch (state) {
360 case ODEBUG_STATE_ACTIVE:
361 cancel_work_sync(work);
362 debug_object_init(work, &work_debug_descr);
363 return 1;
364 default:
365 return 0;
366 }
367}
368
369/*
370 * fixup_activate is called when:
371 * - an active object is activated
372 * - an unknown object is activated (might be a statically initialized object)
373 */
374static int work_fixup_activate(void *addr, enum debug_obj_state state)
375{
376 struct work_struct *work = addr;
377
378 switch (state) {
379
380 case ODEBUG_STATE_NOTAVAILABLE:
381 /*
382 * This is not really a fixup. The work struct was
383 * statically initialized. We just make sure that it
384 * is tracked in the object tracker.
385 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200386 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900387 debug_object_init(work, &work_debug_descr);
388 debug_object_activate(work, &work_debug_descr);
389 return 0;
390 }
391 WARN_ON_ONCE(1);
392 return 0;
393
394 case ODEBUG_STATE_ACTIVE:
395 WARN_ON(1);
396
397 default:
398 return 0;
399 }
400}
401
402/*
403 * fixup_free is called when:
404 * - an active object is freed
405 */
406static int work_fixup_free(void *addr, enum debug_obj_state state)
407{
408 struct work_struct *work = addr;
409
410 switch (state) {
411 case ODEBUG_STATE_ACTIVE:
412 cancel_work_sync(work);
413 debug_object_free(work, &work_debug_descr);
414 return 1;
415 default:
416 return 0;
417 }
418}
419
420static struct debug_obj_descr work_debug_descr = {
421 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100422 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900423 .fixup_init = work_fixup_init,
424 .fixup_activate = work_fixup_activate,
425 .fixup_free = work_fixup_free,
426};
427
428static inline void debug_work_activate(struct work_struct *work)
429{
430 debug_object_activate(work, &work_debug_descr);
431}
432
433static inline void debug_work_deactivate(struct work_struct *work)
434{
435 debug_object_deactivate(work, &work_debug_descr);
436}
437
438void __init_work(struct work_struct *work, int onstack)
439{
440 if (onstack)
441 debug_object_init_on_stack(work, &work_debug_descr);
442 else
443 debug_object_init(work, &work_debug_descr);
444}
445EXPORT_SYMBOL_GPL(__init_work);
446
447void destroy_work_on_stack(struct work_struct *work)
448{
449 debug_object_free(work, &work_debug_descr);
450}
451EXPORT_SYMBOL_GPL(destroy_work_on_stack);
452
453#else
454static inline void debug_work_activate(struct work_struct *work) { }
455static inline void debug_work_deactivate(struct work_struct *work) { }
456#endif
457
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100458/* Serializes the accesses to the list of workqueues. */
459static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460static LIST_HEAD(workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200461static bool workqueue_freezing; /* W: have wqs started freezing? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Oleg Nesterov14441962007-05-23 13:57:57 -0700463/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200464 * The almighty global cpu workqueues. nr_running is the only field
465 * which is expected to be used frequently by other cpus via
466 * try_to_wake_up(). Put it in a separate cacheline.
Oleg Nesterov14441962007-05-23 13:57:57 -0700467 */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200468static DEFINE_PER_CPU(struct global_cwq, global_cwq);
Tejun Heo4ce62e92012-07-13 22:16:44 -0700469static DEFINE_PER_CPU_SHARED_ALIGNED(atomic_t, pool_nr_running[NR_WORKER_POOLS]);
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800470
Tejun Heof3421792010-07-02 10:03:51 +0200471/*
472 * Global cpu workqueue and nr_running counter for unbound gcwq. The
473 * gcwq is always online, has GCWQ_DISASSOCIATED set, and all its
474 * workers have WORKER_UNBOUND set.
475 */
476static struct global_cwq unbound_global_cwq;
Tejun Heo4ce62e92012-07-13 22:16:44 -0700477static atomic_t unbound_pool_nr_running[NR_WORKER_POOLS] = {
478 [0 ... NR_WORKER_POOLS - 1] = ATOMIC_INIT(0), /* always 0 */
479};
Tejun Heof3421792010-07-02 10:03:51 +0200480
Tejun Heoc34056a2010-06-29 10:07:11 +0200481static int worker_thread(void *__worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Tejun Heo32704762012-07-13 22:16:45 -0700483static int worker_pool_pri(struct worker_pool *pool)
484{
485 return pool - pool->gcwq->pools;
486}
487
Tejun Heo8b03ae32010-06-29 10:07:12 +0200488static struct global_cwq *get_gcwq(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Tejun Heof3421792010-07-02 10:03:51 +0200490 if (cpu != WORK_CPU_UNBOUND)
491 return &per_cpu(global_cwq, cpu);
492 else
493 return &unbound_global_cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
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 Heo63d95a92012-07-12 14:46:37 -0700498 int cpu = pool->gcwq->cpu;
Tejun Heo32704762012-07-13 22:16:45 -0700499 int idx = 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
537 * is cleared and the high bits contain OFFQ flags and CPU number.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200538 *
Tejun Heobbb68df2012-08-03 10:30:46 -0700539 * set_work_cwq(), set_work_cpu_and_clear_pending(), mark_work_canceling()
540 * and clear_work_data() can be used to set the cwq, cpu or clear
541 * 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 Heobbb68df2012-08-03 10:30:46 -0700544 * get_work_[g]cwq() can be used to obtain the gcwq or cwq corresponding to
545 * a work. gcwq is available once the work has been queued anywhere after
546 * initialization until it is sync canceled. cwq is available only while
547 * the work item is queued.
548 *
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 Heo8930cab2012-08-03 10:30:45 -0700569static void set_work_cpu_and_clear_pending(struct work_struct *work,
570 unsigned int cpu)
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 Heob5490072012-08-03 10:30:46 -0700579 set_work_data(work, (unsigned long)cpu << WORK_OFFQ_CPU_SHIFT, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200580}
581
582static void clear_work_data(struct work_struct *work)
583{
Tejun Heo23657bb2012-08-13 17:08:19 -0700584 smp_wmb(); /* see set_work_cpu_and_clear_pending() */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200585 set_work_data(work, WORK_STRUCT_NO_CPU, 0);
586}
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
598static struct global_cwq *get_work_gcwq(struct work_struct *work)
599{
Tejun Heoe1201532010-07-22 14:14:25 +0200600 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200601 unsigned int cpu;
602
Tejun Heoe1201532010-07-22 14:14:25 +0200603 if (data & WORK_STRUCT_CWQ)
604 return ((struct cpu_workqueue_struct *)
Tejun Heobd7bdd42012-07-12 14:46:37 -0700605 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->gcwq;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200606
Tejun Heob5490072012-08-03 10:30:46 -0700607 cpu = data >> WORK_OFFQ_CPU_SHIFT;
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200608 if (cpu == WORK_CPU_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200609 return NULL;
610
Tejun Heof3421792010-07-02 10:03:51 +0200611 BUG_ON(cpu >= nr_cpu_ids && cpu != WORK_CPU_UNBOUND);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200612 return get_gcwq(cpu);
David Howells365970a2006-11-22 14:54:49 +0000613}
614
Tejun Heobbb68df2012-08-03 10:30:46 -0700615static void mark_work_canceling(struct work_struct *work)
616{
617 struct global_cwq *gcwq = get_work_gcwq(work);
618 unsigned long cpu = gcwq ? gcwq->cpu : WORK_CPU_NONE;
619
620 set_work_data(work, (cpu << WORK_OFFQ_CPU_SHIFT) | WORK_OFFQ_CANCELING,
621 WORK_STRUCT_PENDING);
622}
623
624static bool work_is_canceling(struct work_struct *work)
625{
626 unsigned long data = atomic_long_read(&work->data);
627
628 return !(data & WORK_STRUCT_CWQ) && (data & WORK_OFFQ_CANCELING);
629}
630
David Howells365970a2006-11-22 14:54:49 +0000631/*
Tejun Heo32704762012-07-13 22:16:45 -0700632 * Policy functions. These define the policies on how the global worker
633 * pools are managed. Unless noted otherwise, these functions assume that
634 * they're being called with gcwq->lock held.
David Howells365970a2006-11-22 14:54:49 +0000635 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200636
Tejun Heo63d95a92012-07-12 14:46:37 -0700637static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000638{
Tejun Heo32704762012-07-13 22:16:45 -0700639 return !atomic_read(get_pool_nr_running(pool));
David Howells365970a2006-11-22 14:54:49 +0000640}
641
Tejun Heoe22bee72010-06-29 10:07:14 +0200642/*
643 * Need to wake up a worker? Called from anything but currently
644 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700645 *
646 * Note that, because unbound workers never contribute to nr_running, this
647 * function will always return %true for unbound gcwq as long as the
648 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200649 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700650static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000651{
Tejun Heo63d95a92012-07-12 14:46:37 -0700652 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000653}
654
Tejun Heoe22bee72010-06-29 10:07:14 +0200655/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700656static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200657{
Tejun Heo63d95a92012-07-12 14:46:37 -0700658 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200659}
660
661/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700662static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200663{
Tejun Heo63d95a92012-07-12 14:46:37 -0700664 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200665
Tejun Heo32704762012-07-13 22:16:45 -0700666 return !list_empty(&pool->worklist) && atomic_read(nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200667}
668
669/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700670static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200671{
Tejun Heo63d95a92012-07-12 14:46:37 -0700672 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200673}
674
675/* Do I need to be the manager? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700676static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200677{
Tejun Heo63d95a92012-07-12 14:46:37 -0700678 return need_to_create_worker(pool) ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700679 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200680}
681
682/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700683static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200684{
Tejun Heo60373152012-07-17 12:39:27 -0700685 bool managing = mutex_is_locked(&pool->manager_mutex);
Tejun Heo63d95a92012-07-12 14:46:37 -0700686 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
687 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200688
689 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
690}
691
692/*
693 * Wake up functions.
694 */
695
Tejun Heo7e116292010-06-29 10:07:13 +0200696/* Return the first worker. Safe with preemption disabled */
Tejun Heo63d95a92012-07-12 14:46:37 -0700697static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200698{
Tejun Heo63d95a92012-07-12 14:46:37 -0700699 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200700 return NULL;
701
Tejun Heo63d95a92012-07-12 14:46:37 -0700702 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200703}
704
705/**
706 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700707 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200708 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700709 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200710 *
711 * CONTEXT:
712 * spin_lock_irq(gcwq->lock).
713 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700714static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200715{
Tejun Heo63d95a92012-07-12 14:46:37 -0700716 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200717
718 if (likely(worker))
719 wake_up_process(worker->task);
720}
721
Tejun Heo4690c4a2010-06-29 10:07:10 +0200722/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200723 * wq_worker_waking_up - a worker is waking up
724 * @task: task waking up
725 * @cpu: CPU @task is waking up to
726 *
727 * This function is called during try_to_wake_up() when a worker is
728 * being awoken.
729 *
730 * CONTEXT:
731 * spin_lock_irq(rq->lock)
732 */
733void wq_worker_waking_up(struct task_struct *task, unsigned int cpu)
734{
735 struct worker *worker = kthread_data(task);
736
Steven Rostedt2d646722010-12-03 23:12:33 -0500737 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heo63d95a92012-07-12 14:46:37 -0700738 atomic_inc(get_pool_nr_running(worker->pool));
Tejun Heoe22bee72010-06-29 10:07:14 +0200739}
740
741/**
742 * wq_worker_sleeping - a worker is going to sleep
743 * @task: task going to sleep
744 * @cpu: CPU in question, must be the current CPU number
745 *
746 * This function is called during schedule() when a busy worker is
747 * going to sleep. Worker on the same cpu can be woken up by
748 * returning pointer to its task.
749 *
750 * CONTEXT:
751 * spin_lock_irq(rq->lock)
752 *
753 * RETURNS:
754 * Worker task on @cpu to wake up, %NULL if none.
755 */
756struct task_struct *wq_worker_sleeping(struct task_struct *task,
757 unsigned int cpu)
758{
759 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heobd7bdd42012-07-12 14:46:37 -0700760 struct worker_pool *pool = worker->pool;
Tejun Heo63d95a92012-07-12 14:46:37 -0700761 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200762
Steven Rostedt2d646722010-12-03 23:12:33 -0500763 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200764 return NULL;
765
766 /* this can only happen on the local cpu */
767 BUG_ON(cpu != raw_smp_processor_id());
768
769 /*
770 * The counterpart of the following dec_and_test, implied mb,
771 * worklist not empty test sequence is in insert_work().
772 * Please read comment there.
773 *
Tejun Heo628c78e2012-07-17 12:39:27 -0700774 * NOT_RUNNING is clear. This means that we're bound to and
775 * running on the local cpu w/ rq lock held and preemption
776 * disabled, which in turn means that none else could be
777 * manipulating idle_list, so dereferencing idle_list without gcwq
778 * lock is safe.
Tejun Heoe22bee72010-06-29 10:07:14 +0200779 */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700780 if (atomic_dec_and_test(nr_running) && !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700781 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200782 return to_wakeup ? to_wakeup->task : NULL;
783}
784
785/**
786 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200787 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200788 * @flags: flags to set
789 * @wakeup: wakeup an idle worker if necessary
790 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200791 * Set @flags in @worker->flags and adjust nr_running accordingly. If
792 * nr_running becomes zero and @wakeup is %true, an idle worker is
793 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200794 *
Tejun Heocb444762010-07-02 10:03:50 +0200795 * CONTEXT:
796 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200797 */
798static inline void worker_set_flags(struct worker *worker, unsigned int flags,
799 bool wakeup)
800{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700801 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200802
Tejun Heocb444762010-07-02 10:03:50 +0200803 WARN_ON_ONCE(worker->task != current);
804
Tejun Heoe22bee72010-06-29 10:07:14 +0200805 /*
806 * If transitioning into NOT_RUNNING, adjust nr_running and
807 * wake up an idle worker as necessary if requested by
808 * @wakeup.
809 */
810 if ((flags & WORKER_NOT_RUNNING) &&
811 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heo63d95a92012-07-12 14:46:37 -0700812 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200813
814 if (wakeup) {
815 if (atomic_dec_and_test(nr_running) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -0700816 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700817 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200818 } else
819 atomic_dec(nr_running);
820 }
821
Tejun Heod302f012010-06-29 10:07:13 +0200822 worker->flags |= flags;
823}
824
825/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200826 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200827 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200828 * @flags: flags to clear
829 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200830 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200831 *
Tejun Heocb444762010-07-02 10:03:50 +0200832 * CONTEXT:
833 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200834 */
835static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
836{
Tejun Heo63d95a92012-07-12 14:46:37 -0700837 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200838 unsigned int oflags = worker->flags;
839
Tejun Heocb444762010-07-02 10:03:50 +0200840 WARN_ON_ONCE(worker->task != current);
841
Tejun Heod302f012010-06-29 10:07:13 +0200842 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200843
Tejun Heo42c025f2011-01-11 15:58:49 +0100844 /*
845 * If transitioning out of NOT_RUNNING, increment nr_running. Note
846 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
847 * of multiple flags, not a single flag.
848 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200849 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
850 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heo63d95a92012-07-12 14:46:37 -0700851 atomic_inc(get_pool_nr_running(pool));
Tejun Heod302f012010-06-29 10:07:13 +0200852}
853
854/**
Tejun Heoc8e55f32010-06-29 10:07:12 +0200855 * busy_worker_head - return the busy hash head for a work
856 * @gcwq: gcwq of interest
857 * @work: work to be hashed
858 *
859 * Return hash head of @gcwq for @work.
860 *
861 * CONTEXT:
862 * spin_lock_irq(gcwq->lock).
863 *
864 * RETURNS:
865 * Pointer to the hash head.
866 */
867static struct hlist_head *busy_worker_head(struct global_cwq *gcwq,
868 struct work_struct *work)
869{
870 const int base_shift = ilog2(sizeof(struct work_struct));
871 unsigned long v = (unsigned long)work;
872
873 /* simple shift and fold hash, do we need something better? */
874 v >>= base_shift;
875 v += v >> BUSY_WORKER_HASH_ORDER;
876 v &= BUSY_WORKER_HASH_MASK;
877
878 return &gcwq->busy_hash[v];
879}
880
881/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200882 * __find_worker_executing_work - find worker which is executing a work
883 * @gcwq: gcwq of interest
884 * @bwh: hash head as returned by busy_worker_head()
885 * @work: work to find worker for
886 *
887 * Find a worker which is executing @work on @gcwq. @bwh should be
888 * the hash head obtained by calling busy_worker_head() with the same
889 * work.
890 *
891 * CONTEXT:
892 * spin_lock_irq(gcwq->lock).
893 *
894 * RETURNS:
895 * Pointer to worker which is executing @work if found, NULL
896 * otherwise.
897 */
898static struct worker *__find_worker_executing_work(struct global_cwq *gcwq,
899 struct hlist_head *bwh,
900 struct work_struct *work)
901{
902 struct worker *worker;
903 struct hlist_node *tmp;
904
905 hlist_for_each_entry(worker, tmp, bwh, hentry)
906 if (worker->current_work == work)
907 return worker;
908 return NULL;
909}
910
911/**
912 * find_worker_executing_work - find worker which is executing a work
913 * @gcwq: gcwq of interest
914 * @work: work to find worker for
915 *
916 * Find a worker which is executing @work on @gcwq. This function is
917 * identical to __find_worker_executing_work() except that this
918 * function calculates @bwh itself.
919 *
920 * CONTEXT:
921 * spin_lock_irq(gcwq->lock).
922 *
923 * RETURNS:
924 * Pointer to worker which is executing @work if found, NULL
925 * otherwise.
926 */
927static struct worker *find_worker_executing_work(struct global_cwq *gcwq,
928 struct work_struct *work)
929{
930 return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work),
931 work);
932}
933
934/**
Tejun Heobf4ede02012-08-03 10:30:46 -0700935 * move_linked_works - move linked works to a list
936 * @work: start of series of works to be scheduled
937 * @head: target list to append @work to
938 * @nextp: out paramter for nested worklist walking
939 *
940 * Schedule linked works starting from @work to @head. Work series to
941 * be scheduled starts at @work and includes any consecutive work with
942 * WORK_STRUCT_LINKED set in its predecessor.
943 *
944 * If @nextp is not NULL, it's updated to point to the next work of
945 * the last scheduled work. This allows move_linked_works() to be
946 * nested inside outer list_for_each_entry_safe().
947 *
948 * CONTEXT:
949 * spin_lock_irq(gcwq->lock).
950 */
951static void move_linked_works(struct work_struct *work, struct list_head *head,
952 struct work_struct **nextp)
953{
954 struct work_struct *n;
955
956 /*
957 * Linked worklist will always end before the end of the list,
958 * use NULL for list head.
959 */
960 list_for_each_entry_safe_from(work, n, NULL, entry) {
961 list_move_tail(&work->entry, head);
962 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
963 break;
964 }
965
966 /*
967 * If we're already inside safe list traversal and have moved
968 * multiple works to the scheduled queue, the next position
969 * needs to be updated.
970 */
971 if (nextp)
972 *nextp = n;
973}
974
975static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
976{
977 struct work_struct *work = list_first_entry(&cwq->delayed_works,
978 struct work_struct, entry);
979
980 trace_workqueue_activate_work(work);
981 move_linked_works(work, &cwq->pool->worklist, NULL);
982 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
983 cwq->nr_active++;
984}
985
986/**
987 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
988 * @cwq: cwq of interest
989 * @color: color of work which left the queue
990 * @delayed: for a delayed work
991 *
992 * A work either has completed or is removed from pending queue,
993 * decrement nr_in_flight of its cwq and handle workqueue flushing.
994 *
995 * CONTEXT:
996 * spin_lock_irq(gcwq->lock).
997 */
998static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color,
999 bool delayed)
1000{
1001 /* ignore uncolored works */
1002 if (color == WORK_NO_COLOR)
1003 return;
1004
1005 cwq->nr_in_flight[color]--;
1006
1007 if (!delayed) {
1008 cwq->nr_active--;
1009 if (!list_empty(&cwq->delayed_works)) {
1010 /* one down, submit a delayed one */
1011 if (cwq->nr_active < cwq->max_active)
1012 cwq_activate_first_delayed(cwq);
1013 }
1014 }
1015
1016 /* is flush in progress and are we at the flushing tip? */
1017 if (likely(cwq->flush_color != color))
1018 return;
1019
1020 /* are there still in-flight works? */
1021 if (cwq->nr_in_flight[color])
1022 return;
1023
1024 /* this cwq is done, clear flush_color */
1025 cwq->flush_color = -1;
1026
1027 /*
1028 * If this was the last cwq, wake up the first flusher. It
1029 * will handle the rest.
1030 */
1031 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
1032 complete(&cwq->wq->first_flusher->done);
1033}
1034
Tejun Heo36e227d2012-08-03 10:30:46 -07001035/**
Tejun Heobbb68df2012-08-03 10:30:46 -07001036 * try_to_grab_pending - steal work item from worklist and disable irq
Tejun Heo36e227d2012-08-03 10:30:46 -07001037 * @work: work item to steal
1038 * @is_dwork: @work is a delayed_work
Tejun Heobbb68df2012-08-03 10:30:46 -07001039 * @flags: place to store irq state
Tejun Heo36e227d2012-08-03 10:30:46 -07001040 *
1041 * Try to grab PENDING bit of @work. This function can handle @work in any
1042 * stable state - idle, on timer or on worklist. Return values are
1043 *
1044 * 1 if @work was pending and we successfully stole PENDING
1045 * 0 if @work was idle and we claimed PENDING
1046 * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry
Tejun Heobbb68df2012-08-03 10:30:46 -07001047 * -ENOENT if someone else is canceling @work, this state may persist
1048 * for arbitrarily long
Tejun Heo36e227d2012-08-03 10:30:46 -07001049 *
Tejun Heobbb68df2012-08-03 10:30:46 -07001050 * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting
1051 * preempted while holding PENDING and @work off queue, preemption must be
1052 * disabled on entry. This ensures that we don't return -EAGAIN while
1053 * another task is preempted in this function.
1054 *
1055 * On successful return, >= 0, irq is disabled and the caller is
1056 * responsible for releasing it using local_irq_restore(*@flags).
1057 *
1058 * This function is safe to call from any context other than IRQ handler.
1059 * An IRQ handler may run on top of delayed_work_timer_fn() which can make
1060 * this function return -EAGAIN perpetually.
Tejun Heobf4ede02012-08-03 10:30:46 -07001061 */
Tejun Heobbb68df2012-08-03 10:30:46 -07001062static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
1063 unsigned long *flags)
Tejun Heobf4ede02012-08-03 10:30:46 -07001064{
1065 struct global_cwq *gcwq;
Tejun Heobf4ede02012-08-03 10:30:46 -07001066
Tejun Heobbb68df2012-08-03 10:30:46 -07001067 WARN_ON_ONCE(in_irq());
1068
1069 local_irq_save(*flags);
1070
Tejun Heo36e227d2012-08-03 10:30:46 -07001071 /* try to steal the timer if it exists */
1072 if (is_dwork) {
1073 struct delayed_work *dwork = to_delayed_work(work);
1074
1075 if (likely(del_timer(&dwork->timer)))
1076 return 1;
1077 }
1078
1079 /* try to claim PENDING the normal way */
Tejun Heobf4ede02012-08-03 10:30:46 -07001080 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1081 return 0;
1082
1083 /*
1084 * The queueing is in progress, or it is already queued. Try to
1085 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1086 */
1087 gcwq = get_work_gcwq(work);
1088 if (!gcwq)
Tejun Heobbb68df2012-08-03 10:30:46 -07001089 goto fail;
Tejun Heobf4ede02012-08-03 10:30:46 -07001090
Tejun Heobbb68df2012-08-03 10:30:46 -07001091 spin_lock(&gcwq->lock);
Tejun Heobf4ede02012-08-03 10:30:46 -07001092 if (!list_empty(&work->entry)) {
1093 /*
1094 * This work is queued, but perhaps we locked the wrong gcwq.
1095 * In that case we must see the new value after rmb(), see
1096 * insert_work()->wmb().
1097 */
1098 smp_rmb();
1099 if (gcwq == get_work_gcwq(work)) {
1100 debug_work_deactivate(work);
1101 list_del_init(&work->entry);
1102 cwq_dec_nr_in_flight(get_work_cwq(work),
1103 get_work_color(work),
1104 *work_data_bits(work) & WORK_STRUCT_DELAYED);
Tejun Heo36e227d2012-08-03 10:30:46 -07001105
Tejun Heobbb68df2012-08-03 10:30:46 -07001106 spin_unlock(&gcwq->lock);
Tejun Heo36e227d2012-08-03 10:30:46 -07001107 return 1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001108 }
1109 }
Tejun Heobbb68df2012-08-03 10:30:46 -07001110 spin_unlock(&gcwq->lock);
1111fail:
1112 local_irq_restore(*flags);
1113 if (work_is_canceling(work))
1114 return -ENOENT;
1115 cpu_relax();
Tejun Heo36e227d2012-08-03 10:30:46 -07001116 return -EAGAIN;
Tejun Heobf4ede02012-08-03 10:30:46 -07001117}
1118
1119/**
Tejun Heo7e116292010-06-29 10:07:13 +02001120 * insert_work - insert a work into gcwq
Tejun Heo4690c4a2010-06-29 10:07:10 +02001121 * @cwq: cwq @work belongs to
1122 * @work: work to insert
1123 * @head: insertion point
1124 * @extra_flags: extra WORK_STRUCT_* flags to set
1125 *
Tejun Heo7e116292010-06-29 10:07:13 +02001126 * Insert @work which belongs to @cwq into @gcwq after @head.
1127 * @extra_flags is or'd to work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +02001128 *
1129 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001130 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02001131 */
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001132static void insert_work(struct cpu_workqueue_struct *cwq,
Tejun Heo4690c4a2010-06-29 10:07:10 +02001133 struct work_struct *work, struct list_head *head,
1134 unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001135{
Tejun Heo63d95a92012-07-12 14:46:37 -07001136 struct worker_pool *pool = cwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01001137
Tejun Heo4690c4a2010-06-29 10:07:10 +02001138 /* we own @work, set data and link */
Tejun Heo7a22ad72010-06-29 10:07:13 +02001139 set_work_cwq(work, cwq, extra_flags);
Tejun Heo4690c4a2010-06-29 10:07:10 +02001140
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001141 /*
1142 * Ensure that we get the right work->data if we see the
1143 * result of list_add() below, see try_to_grab_pending().
1144 */
1145 smp_wmb();
Tejun Heo4690c4a2010-06-29 10:07:10 +02001146
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -07001147 list_add_tail(&work->entry, head);
Tejun Heoe22bee72010-06-29 10:07:14 +02001148
1149 /*
1150 * Ensure either worker_sched_deactivated() sees the above
1151 * list_add_tail() or we see zero nr_running to avoid workers
1152 * lying around lazily while there are works to be processed.
1153 */
1154 smp_mb();
1155
Tejun Heo63d95a92012-07-12 14:46:37 -07001156 if (__need_more_worker(pool))
1157 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001158}
1159
Tejun Heoc8efcc22010-12-20 19:32:04 +01001160/*
1161 * Test whether @work is being queued from another work executing on the
1162 * same workqueue. This is rather expensive and should only be used from
1163 * cold paths.
1164 */
1165static bool is_chained_work(struct workqueue_struct *wq)
1166{
1167 unsigned long flags;
1168 unsigned int cpu;
1169
1170 for_each_gcwq_cpu(cpu) {
1171 struct global_cwq *gcwq = get_gcwq(cpu);
1172 struct worker *worker;
1173 struct hlist_node *pos;
1174 int i;
1175
1176 spin_lock_irqsave(&gcwq->lock, flags);
1177 for_each_busy_worker(worker, i, pos, gcwq) {
1178 if (worker->task != current)
1179 continue;
1180 spin_unlock_irqrestore(&gcwq->lock, flags);
1181 /*
1182 * I'm @worker, no locking necessary. See if @work
1183 * is headed to the same workqueue.
1184 */
1185 return worker->current_cwq->wq == wq;
1186 }
1187 spin_unlock_irqrestore(&gcwq->lock, flags);
1188 }
1189 return false;
1190}
1191
Tejun Heo4690c4a2010-06-29 10:07:10 +02001192static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 struct work_struct *work)
1194{
Tejun Heo502ca9d2010-06-29 10:07:13 +02001195 struct global_cwq *gcwq;
1196 struct cpu_workqueue_struct *cwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001197 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001198 unsigned int work_flags;
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001199 unsigned int req_cpu = cpu;
Tejun Heo8930cab2012-08-03 10:30:45 -07001200
1201 /*
1202 * While a work item is PENDING && off queue, a task trying to
1203 * steal the PENDING will busy-loop waiting for it to either get
1204 * queued or lose PENDING. Grabbing PENDING and queueing should
1205 * happen with IRQ disabled.
1206 */
1207 WARN_ON_ONCE(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001209 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001210
Tejun Heoc8efcc22010-12-20 19:32:04 +01001211 /* if dying, only works from the same workqueue are allowed */
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02001212 if (unlikely(wq->flags & WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001213 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001214 return;
1215
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001216 /* determine gcwq to use */
1217 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001218 struct global_cwq *last_gcwq;
1219
Tejun Heo57469822012-08-03 10:30:45 -07001220 if (cpu == WORK_CPU_UNBOUND)
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001221 cpu = raw_smp_processor_id();
1222
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001223 /*
Tejun Heodbf25762012-08-20 14:51:23 -07001224 * It's multi cpu. If @work was previously on a different
1225 * cpu, it might still be running there, in which case the
1226 * work needs to be queued on that cpu to guarantee
1227 * non-reentrancy.
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001228 */
Tejun Heo502ca9d2010-06-29 10:07:13 +02001229 gcwq = get_gcwq(cpu);
Tejun Heodbf25762012-08-20 14:51:23 -07001230 last_gcwq = get_work_gcwq(work);
1231
1232 if (last_gcwq && last_gcwq != gcwq) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001233 struct worker *worker;
1234
Tejun Heo8930cab2012-08-03 10:30:45 -07001235 spin_lock(&last_gcwq->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001236
1237 worker = find_worker_executing_work(last_gcwq, work);
1238
1239 if (worker && worker->current_cwq->wq == wq)
1240 gcwq = last_gcwq;
1241 else {
1242 /* meh... not running there, queue here */
Tejun Heo8930cab2012-08-03 10:30:45 -07001243 spin_unlock(&last_gcwq->lock);
1244 spin_lock(&gcwq->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001245 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001246 } else {
1247 spin_lock(&gcwq->lock);
1248 }
Tejun Heof3421792010-07-02 10:03:51 +02001249 } else {
1250 gcwq = get_gcwq(WORK_CPU_UNBOUND);
Tejun Heo8930cab2012-08-03 10:30:45 -07001251 spin_lock(&gcwq->lock);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001252 }
1253
1254 /* gcwq determined, get cwq and queue */
1255 cwq = get_cwq(gcwq->cpu, wq);
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001256 trace_workqueue_queue_work(req_cpu, cwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001257
Dan Carpenterf5b25522012-04-13 22:06:58 +03001258 if (WARN_ON(!list_empty(&work->entry))) {
Tejun Heo8930cab2012-08-03 10:30:45 -07001259 spin_unlock(&gcwq->lock);
Dan Carpenterf5b25522012-04-13 22:06:58 +03001260 return;
1261 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001262
Tejun Heo73f53c42010-06-29 10:07:11 +02001263 cwq->nr_in_flight[cwq->work_color]++;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001264 work_flags = work_color_to_flags(cwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001265
1266 if (likely(cwq->nr_active < cwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001267 trace_workqueue_activate_work(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001268 cwq->nr_active++;
Tejun Heo32704762012-07-13 22:16:45 -07001269 worklist = &cwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001270 } else {
1271 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001272 worklist = &cwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001273 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001274
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001275 insert_work(cwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001276
Tejun Heo8930cab2012-08-03 10:30:45 -07001277 spin_unlock(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001280/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07001281 * queue_work_on - queue work on specific cpu
1282 * @cpu: CPU number to execute work on
1283 * @wq: workqueue to use
1284 * @work: work to queue
1285 *
Tejun Heod4283e92012-08-03 10:30:44 -07001286 * Returns %false if @work was already on a queue, %true otherwise.
Zhang Ruic1a220e2008-07-23 21:28:39 -07001287 *
1288 * We queue the work to a specific CPU, the caller must ensure it
1289 * can't go away.
1290 */
Tejun Heod4283e92012-08-03 10:30:44 -07001291bool queue_work_on(int cpu, struct workqueue_struct *wq,
1292 struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07001293{
Tejun Heod4283e92012-08-03 10:30:44 -07001294 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001295 unsigned long flags;
1296
1297 local_irq_save(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001298
Tejun Heo22df02b2010-06-29 10:07:10 +02001299 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001300 __queue_work(cpu, wq, work);
Tejun Heod4283e92012-08-03 10:30:44 -07001301 ret = true;
Zhang Ruic1a220e2008-07-23 21:28:39 -07001302 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001303
1304 local_irq_restore(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001305 return ret;
1306}
1307EXPORT_SYMBOL_GPL(queue_work_on);
1308
Tejun Heo0a13c002012-08-03 10:30:44 -07001309/**
1310 * queue_work - queue work on a workqueue
1311 * @wq: workqueue to use
1312 * @work: work to queue
1313 *
Tejun Heod4283e92012-08-03 10:30:44 -07001314 * Returns %false if @work was already on a queue, %true otherwise.
Tejun Heo0a13c002012-08-03 10:30:44 -07001315 *
1316 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1317 * it can be processed by another CPU.
1318 */
Tejun Heod4283e92012-08-03 10:30:44 -07001319bool queue_work(struct workqueue_struct *wq, struct work_struct *work)
Tejun Heo0a13c002012-08-03 10:30:44 -07001320{
Tejun Heo57469822012-08-03 10:30:45 -07001321 return queue_work_on(WORK_CPU_UNBOUND, wq, work);
Tejun Heo0a13c002012-08-03 10:30:44 -07001322}
1323EXPORT_SYMBOL_GPL(queue_work);
1324
Tejun Heod8e794d2012-08-03 10:30:45 -07001325void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
David Howells52bad642006-11-22 14:54:01 +00001327 struct delayed_work *dwork = (struct delayed_work *)__data;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001328 struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Tejun Heo8930cab2012-08-03 10:30:45 -07001330 local_irq_disable();
Tejun Heo12650572012-08-08 09:38:42 -07001331 __queue_work(dwork->cpu, cwq->wq, &dwork->work);
Tejun Heo8930cab2012-08-03 10:30:45 -07001332 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333}
Tejun Heod8e794d2012-08-03 10:30:45 -07001334EXPORT_SYMBOL_GPL(delayed_work_timer_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001336static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
1337 struct delayed_work *dwork, unsigned long delay)
1338{
1339 struct timer_list *timer = &dwork->timer;
1340 struct work_struct *work = &dwork->work;
1341 unsigned int lcpu;
1342
1343 WARN_ON_ONCE(timer->function != delayed_work_timer_fn ||
1344 timer->data != (unsigned long)dwork);
1345 BUG_ON(timer_pending(timer));
1346 BUG_ON(!list_empty(&work->entry));
1347
1348 timer_stats_timer_set_start_info(&dwork->timer);
1349
1350 /*
1351 * This stores cwq for the moment, for the timer_fn. Note that the
1352 * work's gcwq is preserved to allow reentrance detection for
1353 * delayed works.
1354 */
1355 if (!(wq->flags & WQ_UNBOUND)) {
1356 struct global_cwq *gcwq = get_work_gcwq(work);
1357
Joonsoo Kime42986d2012-08-15 23:25:38 +09001358 /*
1359 * If we cannot get the last gcwq from @work directly,
1360 * select the last CPU such that it avoids unnecessarily
1361 * triggering non-reentrancy check in __queue_work().
1362 */
1363 lcpu = cpu;
1364 if (gcwq)
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001365 lcpu = gcwq->cpu;
Joonsoo Kime42986d2012-08-15 23:25:38 +09001366 if (lcpu == WORK_CPU_UNBOUND)
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001367 lcpu = raw_smp_processor_id();
1368 } else {
1369 lcpu = WORK_CPU_UNBOUND;
1370 }
1371
1372 set_work_cwq(work, get_cwq(lcpu, wq), 0);
1373
Tejun Heo12650572012-08-08 09:38:42 -07001374 dwork->cpu = cpu;
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001375 timer->expires = jiffies + delay;
1376
1377 if (unlikely(cpu != WORK_CPU_UNBOUND))
1378 add_timer_on(timer, cpu);
1379 else
1380 add_timer(timer);
1381}
1382
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001383/**
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001384 * queue_delayed_work_on - queue work on specific CPU after delay
1385 * @cpu: CPU number to execute work on
1386 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001387 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001388 * @delay: number of jiffies to wait before queueing
1389 *
Tejun Heo715f1302012-08-03 10:30:46 -07001390 * Returns %false if @work was already on a queue, %true otherwise. If
1391 * @delay is zero and @dwork is idle, it will be scheduled for immediate
1392 * execution.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001393 */
Tejun Heod4283e92012-08-03 10:30:44 -07001394bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
1395 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001396{
David Howells52bad642006-11-22 14:54:01 +00001397 struct work_struct *work = &dwork->work;
Tejun Heod4283e92012-08-03 10:30:44 -07001398 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001399 unsigned long flags;
1400
Tejun Heo715f1302012-08-03 10:30:46 -07001401 if (!delay)
1402 return queue_work_on(cpu, wq, &dwork->work);
1403
Tejun Heo8930cab2012-08-03 10:30:45 -07001404 /* read the comment in __queue_work() */
1405 local_irq_save(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001406
Tejun Heo22df02b2010-06-29 10:07:10 +02001407 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001408 __queue_delayed_work(cpu, wq, dwork, delay);
Tejun Heod4283e92012-08-03 10:30:44 -07001409 ret = true;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001410 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001411
1412 local_irq_restore(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001413 return ret;
1414}
Dave Jonesae90dd52006-06-30 01:40:45 -04001415EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Tejun Heoc8e55f32010-06-29 10:07:12 +02001417/**
Tejun Heo0a13c002012-08-03 10:30:44 -07001418 * queue_delayed_work - queue work on a workqueue after delay
1419 * @wq: workqueue to use
1420 * @dwork: delayable work to queue
1421 * @delay: number of jiffies to wait before queueing
1422 *
Tejun Heo715f1302012-08-03 10:30:46 -07001423 * Equivalent to queue_delayed_work_on() but tries to use the local CPU.
Tejun Heo0a13c002012-08-03 10:30:44 -07001424 */
Tejun Heod4283e92012-08-03 10:30:44 -07001425bool queue_delayed_work(struct workqueue_struct *wq,
Tejun Heo0a13c002012-08-03 10:30:44 -07001426 struct delayed_work *dwork, unsigned long delay)
1427{
Tejun Heo57469822012-08-03 10:30:45 -07001428 return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
Tejun Heo0a13c002012-08-03 10:30:44 -07001429}
1430EXPORT_SYMBOL_GPL(queue_delayed_work);
1431
1432/**
Tejun Heo8376fe22012-08-03 10:30:47 -07001433 * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
1434 * @cpu: CPU number to execute work on
1435 * @wq: workqueue to use
1436 * @dwork: work to queue
1437 * @delay: number of jiffies to wait before queueing
1438 *
1439 * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
1440 * modify @dwork's timer so that it expires after @delay. If @delay is
1441 * zero, @work is guaranteed to be scheduled immediately regardless of its
1442 * current state.
1443 *
1444 * Returns %false if @dwork was idle and queued, %true if @dwork was
1445 * pending and its timer was modified.
1446 *
1447 * This function is safe to call from any context other than IRQ handler.
1448 * See try_to_grab_pending() for details.
1449 */
1450bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
1451 struct delayed_work *dwork, unsigned long delay)
1452{
1453 unsigned long flags;
1454 int ret;
1455
1456 do {
1457 ret = try_to_grab_pending(&dwork->work, true, &flags);
1458 } while (unlikely(ret == -EAGAIN));
1459
1460 if (likely(ret >= 0)) {
1461 __queue_delayed_work(cpu, wq, dwork, delay);
1462 local_irq_restore(flags);
1463 }
1464
1465 /* -ENOENT from try_to_grab_pending() becomes %true */
1466 return ret;
1467}
1468EXPORT_SYMBOL_GPL(mod_delayed_work_on);
1469
1470/**
1471 * mod_delayed_work - modify delay of or queue a delayed work
1472 * @wq: workqueue to use
1473 * @dwork: work to queue
1474 * @delay: number of jiffies to wait before queueing
1475 *
1476 * mod_delayed_work_on() on local CPU.
1477 */
1478bool mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dwork,
1479 unsigned long delay)
1480{
1481 return mod_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
1482}
1483EXPORT_SYMBOL_GPL(mod_delayed_work);
1484
1485/**
Tejun Heoc8e55f32010-06-29 10:07:12 +02001486 * worker_enter_idle - enter idle state
1487 * @worker: worker which is entering idle state
1488 *
1489 * @worker is entering idle state. Update stats and idle timer if
1490 * necessary.
1491 *
1492 * LOCKING:
1493 * spin_lock_irq(gcwq->lock).
1494 */
1495static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001497 struct worker_pool *pool = worker->pool;
1498 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Tejun Heoc8e55f32010-06-29 10:07:12 +02001500 BUG_ON(worker->flags & WORKER_IDLE);
1501 BUG_ON(!list_empty(&worker->entry) &&
1502 (worker->hentry.next || worker->hentry.pprev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Tejun Heocb444762010-07-02 10:03:50 +02001504 /* can't use worker_set_flags(), also called from start_worker() */
1505 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001506 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001507 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001508
Tejun Heoc8e55f32010-06-29 10:07:12 +02001509 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001510 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001511
Tejun Heo628c78e2012-07-17 12:39:27 -07001512 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1513 mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
Tejun Heocb444762010-07-02 10:03:50 +02001514
Tejun Heo544ecf32012-05-14 15:04:50 -07001515 /*
Tejun Heo628c78e2012-07-17 12:39:27 -07001516 * Sanity check nr_running. Because gcwq_unbind_fn() releases
1517 * gcwq->lock between setting %WORKER_UNBOUND and zapping
1518 * nr_running, the warning may trigger spuriously. Check iff
1519 * unbind is not in progress.
Tejun Heo544ecf32012-05-14 15:04:50 -07001520 */
Tejun Heo628c78e2012-07-17 12:39:27 -07001521 WARN_ON_ONCE(!(gcwq->flags & GCWQ_DISASSOCIATED) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001522 pool->nr_workers == pool->nr_idle &&
Tejun Heo63d95a92012-07-12 14:46:37 -07001523 atomic_read(get_pool_nr_running(pool)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
1525
Tejun Heoc8e55f32010-06-29 10:07:12 +02001526/**
1527 * worker_leave_idle - leave idle state
1528 * @worker: worker which is leaving idle state
1529 *
1530 * @worker is leaving idle state. Update stats.
1531 *
1532 * LOCKING:
1533 * spin_lock_irq(gcwq->lock).
1534 */
1535static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001537 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Tejun Heoc8e55f32010-06-29 10:07:12 +02001539 BUG_ON(!(worker->flags & WORKER_IDLE));
Tejun Heod302f012010-06-29 10:07:13 +02001540 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001541 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001542 list_del_init(&worker->entry);
1543}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Tejun Heoe22bee72010-06-29 10:07:14 +02001545/**
1546 * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq
1547 * @worker: self
1548 *
1549 * Works which are scheduled while the cpu is online must at least be
1550 * scheduled to a worker which is bound to the cpu so that if they are
1551 * flushed from cpu callbacks while cpu is going down, they are
1552 * guaranteed to execute on the cpu.
1553 *
1554 * This function is to be used by rogue workers and rescuers to bind
1555 * themselves to the target cpu and may race with cpu going down or
1556 * coming online. kthread_bind() can't be used because it may put the
1557 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1558 * verbatim as it's best effort and blocking and gcwq may be
1559 * [dis]associated in the meantime.
1560 *
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001561 * This function tries set_cpus_allowed() and locks gcwq and verifies the
1562 * binding against %GCWQ_DISASSOCIATED which is set during
1563 * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1564 * enters idle state or fetches works without dropping lock, it can
1565 * guarantee the scheduling requirement described in the first paragraph.
Tejun Heoe22bee72010-06-29 10:07:14 +02001566 *
1567 * CONTEXT:
1568 * Might sleep. Called without any lock but returns with gcwq->lock
1569 * held.
1570 *
1571 * RETURNS:
1572 * %true if the associated gcwq is online (@worker is successfully
1573 * bound), %false if offline.
1574 */
1575static bool worker_maybe_bind_and_lock(struct worker *worker)
Namhyung Kim972fa1c2010-08-22 23:19:43 +09001576__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001577{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001578 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001579 struct task_struct *task = worker->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Tejun Heoe22bee72010-06-29 10:07:14 +02001581 while (true) {
1582 /*
1583 * The following call may fail, succeed or succeed
1584 * without actually migrating the task to the cpu if
1585 * it races with cpu hotunplug operation. Verify
1586 * against GCWQ_DISASSOCIATED.
1587 */
Tejun Heof3421792010-07-02 10:03:51 +02001588 if (!(gcwq->flags & GCWQ_DISASSOCIATED))
1589 set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu));
Oleg Nesterov85f41862007-05-09 02:34:20 -07001590
Tejun Heoe22bee72010-06-29 10:07:14 +02001591 spin_lock_irq(&gcwq->lock);
1592 if (gcwq->flags & GCWQ_DISASSOCIATED)
1593 return false;
1594 if (task_cpu(task) == gcwq->cpu &&
1595 cpumask_equal(&current->cpus_allowed,
1596 get_cpu_mask(gcwq->cpu)))
1597 return true;
1598 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001599
Tejun Heo5035b202011-04-29 18:08:37 +02001600 /*
1601 * We've raced with CPU hot[un]plug. Give it a breather
1602 * and retry migration. cond_resched() is required here;
1603 * otherwise, we might deadlock against cpu_stop trying to
1604 * bring down the CPU on non-preemptive kernel.
1605 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001606 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001607 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001608 }
1609}
1610
Tejun Heo25511a42012-07-17 12:39:27 -07001611struct idle_rebind {
1612 int cnt; /* # workers to be rebound */
1613 struct completion done; /* all workers rebound */
1614};
1615
Tejun Heoe22bee72010-06-29 10:07:14 +02001616/*
Tejun Heo25511a42012-07-17 12:39:27 -07001617 * Rebind an idle @worker to its CPU. During CPU onlining, this has to
1618 * happen synchronously for idle workers. worker_thread() will test
1619 * %WORKER_REBIND before leaving idle and call this function.
1620 */
1621static void idle_worker_rebind(struct worker *worker)
1622{
1623 struct global_cwq *gcwq = worker->pool->gcwq;
1624
1625 /* CPU must be online at this point */
1626 WARN_ON(!worker_maybe_bind_and_lock(worker));
1627 if (!--worker->idle_rebind->cnt)
1628 complete(&worker->idle_rebind->done);
1629 spin_unlock_irq(&worker->pool->gcwq->lock);
1630
1631 /* we did our part, wait for rebind_workers() to finish up */
1632 wait_event(gcwq->rebind_hold, !(worker->flags & WORKER_REBIND));
1633}
1634
1635/*
1636 * Function for @worker->rebind.work used to rebind unbound busy workers to
Tejun Heo403c8212012-07-17 12:39:27 -07001637 * the associated cpu which is coming back online. This is scheduled by
1638 * cpu up but can race with other cpu hotplug operations and may be
1639 * executed twice without intervening cpu down.
Tejun Heoe22bee72010-06-29 10:07:14 +02001640 */
Tejun Heo25511a42012-07-17 12:39:27 -07001641static void busy_worker_rebind_fn(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001642{
1643 struct worker *worker = container_of(work, struct worker, rebind_work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001644 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001645
1646 if (worker_maybe_bind_and_lock(worker))
1647 worker_clr_flags(worker, WORKER_REBIND);
1648
1649 spin_unlock_irq(&gcwq->lock);
1650}
1651
Tejun Heo25511a42012-07-17 12:39:27 -07001652/**
1653 * rebind_workers - rebind all workers of a gcwq to the associated CPU
1654 * @gcwq: gcwq of interest
1655 *
1656 * @gcwq->cpu is coming online. Rebind all workers to the CPU. Rebinding
1657 * is different for idle and busy ones.
1658 *
1659 * The idle ones should be rebound synchronously and idle rebinding should
1660 * be complete before any worker starts executing work items with
1661 * concurrency management enabled; otherwise, scheduler may oops trying to
1662 * wake up non-local idle worker from wq_worker_sleeping().
1663 *
1664 * This is achieved by repeatedly requesting rebinding until all idle
1665 * workers are known to have been rebound under @gcwq->lock and holding all
1666 * idle workers from becoming busy until idle rebinding is complete.
1667 *
1668 * Once idle workers are rebound, busy workers can be rebound as they
1669 * finish executing their current work items. Queueing the rebind work at
1670 * the head of their scheduled lists is enough. Note that nr_running will
1671 * be properbly bumped as busy workers rebind.
1672 *
1673 * On return, all workers are guaranteed to either be bound or have rebind
1674 * work item scheduled.
1675 */
1676static void rebind_workers(struct global_cwq *gcwq)
1677 __releases(&gcwq->lock) __acquires(&gcwq->lock)
1678{
1679 struct idle_rebind idle_rebind;
1680 struct worker_pool *pool;
1681 struct worker *worker;
1682 struct hlist_node *pos;
1683 int i;
1684
1685 lockdep_assert_held(&gcwq->lock);
1686
1687 for_each_worker_pool(pool, gcwq)
1688 lockdep_assert_held(&pool->manager_mutex);
1689
1690 /*
1691 * Rebind idle workers. Interlocked both ways. We wait for
1692 * workers to rebind via @idle_rebind.done. Workers will wait for
1693 * us to finish up by watching %WORKER_REBIND.
1694 */
1695 init_completion(&idle_rebind.done);
1696retry:
1697 idle_rebind.cnt = 1;
1698 INIT_COMPLETION(idle_rebind.done);
1699
1700 /* set REBIND and kick idle ones, we'll wait for these later */
1701 for_each_worker_pool(pool, gcwq) {
1702 list_for_each_entry(worker, &pool->idle_list, entry) {
1703 if (worker->flags & WORKER_REBIND)
1704 continue;
1705
1706 /* morph UNBOUND to REBIND */
1707 worker->flags &= ~WORKER_UNBOUND;
1708 worker->flags |= WORKER_REBIND;
1709
1710 idle_rebind.cnt++;
1711 worker->idle_rebind = &idle_rebind;
1712
1713 /* worker_thread() will call idle_worker_rebind() */
1714 wake_up_process(worker->task);
1715 }
1716 }
1717
1718 if (--idle_rebind.cnt) {
1719 spin_unlock_irq(&gcwq->lock);
1720 wait_for_completion(&idle_rebind.done);
1721 spin_lock_irq(&gcwq->lock);
1722 /* busy ones might have become idle while waiting, retry */
1723 goto retry;
1724 }
1725
1726 /*
1727 * All idle workers are rebound and waiting for %WORKER_REBIND to
1728 * be cleared inside idle_worker_rebind(). Clear and release.
1729 * Clearing %WORKER_REBIND from this foreign context is safe
1730 * because these workers are still guaranteed to be idle.
1731 */
1732 for_each_worker_pool(pool, gcwq)
1733 list_for_each_entry(worker, &pool->idle_list, entry)
1734 worker->flags &= ~WORKER_REBIND;
1735
1736 wake_up_all(&gcwq->rebind_hold);
1737
1738 /* rebind busy workers */
1739 for_each_busy_worker(worker, i, pos, gcwq) {
1740 struct work_struct *rebind_work = &worker->rebind_work;
Joonsoo Kime2b6a6d2012-08-15 23:25:40 +09001741 struct workqueue_struct *wq;
Tejun Heo25511a42012-07-17 12:39:27 -07001742
1743 /* morph UNBOUND to REBIND */
1744 worker->flags &= ~WORKER_UNBOUND;
1745 worker->flags |= WORKER_REBIND;
1746
1747 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
1748 work_data_bits(rebind_work)))
1749 continue;
1750
Tejun Heo25511a42012-07-17 12:39:27 -07001751 debug_work_activate(rebind_work);
Joonsoo Kime2b6a6d2012-08-15 23:25:40 +09001752
1753 /*
1754 * wq doesn't really matter but let's keep @worker->pool
1755 * and @cwq->pool consistent for sanity.
1756 */
1757 if (worker_pool_pri(worker->pool))
1758 wq = system_highpri_wq;
1759 else
1760 wq = system_wq;
1761
1762 insert_work(get_cwq(gcwq->cpu, wq), rebind_work,
1763 worker->scheduled.next,
1764 work_color_to_flags(WORK_NO_COLOR));
Tejun Heo25511a42012-07-17 12:39:27 -07001765 }
1766}
1767
Tejun Heoc34056a2010-06-29 10:07:11 +02001768static struct worker *alloc_worker(void)
1769{
1770 struct worker *worker;
1771
1772 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001773 if (worker) {
1774 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001775 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heo25511a42012-07-17 12:39:27 -07001776 INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn);
Tejun Heoe22bee72010-06-29 10:07:14 +02001777 /* on creation a worker is in !idle && prep state */
1778 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001779 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001780 return worker;
1781}
1782
1783/**
1784 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001785 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001786 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001787 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001788 * can be started by calling start_worker() or destroyed using
1789 * destroy_worker().
1790 *
1791 * CONTEXT:
1792 * Might sleep. Does GFP_KERNEL allocations.
1793 *
1794 * RETURNS:
1795 * Pointer to the newly created worker.
1796 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001797static struct worker *create_worker(struct worker_pool *pool)
Tejun Heoc34056a2010-06-29 10:07:11 +02001798{
Tejun Heo63d95a92012-07-12 14:46:37 -07001799 struct global_cwq *gcwq = pool->gcwq;
Tejun Heo32704762012-07-13 22:16:45 -07001800 const char *pri = worker_pool_pri(pool) ? "H" : "";
Tejun Heoc34056a2010-06-29 10:07:11 +02001801 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001802 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001803
Tejun Heo8b03ae32010-06-29 10:07:12 +02001804 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001805 while (ida_get_new(&pool->worker_ida, &id)) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001806 spin_unlock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001807 if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001808 goto fail;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001809 spin_lock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001810 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02001811 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001812
1813 worker = alloc_worker();
1814 if (!worker)
1815 goto fail;
1816
Tejun Heobd7bdd42012-07-12 14:46:37 -07001817 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001818 worker->id = id;
1819
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001820 if (gcwq->cpu != WORK_CPU_UNBOUND)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001821 worker->task = kthread_create_on_node(worker_thread,
Tejun Heo32704762012-07-13 22:16:45 -07001822 worker, cpu_to_node(gcwq->cpu),
1823 "kworker/%u:%d%s", gcwq->cpu, id, pri);
Tejun Heof3421792010-07-02 10:03:51 +02001824 else
1825 worker->task = kthread_create(worker_thread, worker,
Tejun Heo32704762012-07-13 22:16:45 -07001826 "kworker/u:%d%s", id, pri);
Tejun Heoc34056a2010-06-29 10:07:11 +02001827 if (IS_ERR(worker->task))
1828 goto fail;
1829
Tejun Heo32704762012-07-13 22:16:45 -07001830 if (worker_pool_pri(pool))
1831 set_user_nice(worker->task, HIGHPRI_NICE_LEVEL);
1832
Tejun Heodb7bccf2010-06-29 10:07:12 +02001833 /*
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001834 * Determine CPU binding of the new worker depending on
1835 * %GCWQ_DISASSOCIATED. The caller is responsible for ensuring the
1836 * flag remains stable across this function. See the comments
1837 * above the flag definition for details.
1838 *
1839 * As an unbound worker may later become a regular one if CPU comes
1840 * online, make sure every worker has %PF_THREAD_BOUND set.
Tejun Heodb7bccf2010-06-29 10:07:12 +02001841 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001842 if (!(gcwq->flags & GCWQ_DISASSOCIATED)) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001843 kthread_bind(worker->task, gcwq->cpu);
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001844 } else {
Tejun Heodb7bccf2010-06-29 10:07:12 +02001845 worker->task->flags |= PF_THREAD_BOUND;
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001846 worker->flags |= WORKER_UNBOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001848
Tejun Heoc34056a2010-06-29 10:07:11 +02001849 return worker;
1850fail:
1851 if (id >= 0) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001852 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001853 ida_remove(&pool->worker_ida, id);
Tejun Heo8b03ae32010-06-29 10:07:12 +02001854 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001855 }
1856 kfree(worker);
1857 return NULL;
1858}
1859
1860/**
1861 * start_worker - start a newly created worker
1862 * @worker: worker to start
1863 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001864 * Make the gcwq aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001865 *
1866 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001867 * spin_lock_irq(gcwq->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001868 */
1869static void start_worker(struct worker *worker)
1870{
Tejun Heocb444762010-07-02 10:03:50 +02001871 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001872 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001873 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001874 wake_up_process(worker->task);
1875}
1876
1877/**
1878 * destroy_worker - destroy a workqueue worker
1879 * @worker: worker to be destroyed
1880 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001881 * Destroy @worker and adjust @gcwq stats accordingly.
1882 *
1883 * CONTEXT:
1884 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001885 */
1886static void destroy_worker(struct worker *worker)
1887{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001888 struct worker_pool *pool = worker->pool;
1889 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001890 int id = worker->id;
1891
1892 /* sanity check frenzy */
1893 BUG_ON(worker->current_work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001894 BUG_ON(!list_empty(&worker->scheduled));
Tejun Heoc34056a2010-06-29 10:07:11 +02001895
Tejun Heoc8e55f32010-06-29 10:07:12 +02001896 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001897 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001898 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001899 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001900
1901 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001902 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001903
1904 spin_unlock_irq(&gcwq->lock);
1905
Tejun Heoc34056a2010-06-29 10:07:11 +02001906 kthread_stop(worker->task);
1907 kfree(worker);
1908
Tejun Heo8b03ae32010-06-29 10:07:12 +02001909 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001910 ida_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001911}
1912
Tejun Heo63d95a92012-07-12 14:46:37 -07001913static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001914{
Tejun Heo63d95a92012-07-12 14:46:37 -07001915 struct worker_pool *pool = (void *)__pool;
1916 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001917
1918 spin_lock_irq(&gcwq->lock);
1919
Tejun Heo63d95a92012-07-12 14:46:37 -07001920 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001921 struct worker *worker;
1922 unsigned long expires;
1923
1924 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001925 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001926 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1927
1928 if (time_before(jiffies, expires))
Tejun Heo63d95a92012-07-12 14:46:37 -07001929 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001930 else {
1931 /* it's been idle for too long, wake up manager */
Tejun Heo11ebea52012-07-12 14:46:37 -07001932 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -07001933 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001934 }
1935 }
1936
1937 spin_unlock_irq(&gcwq->lock);
1938}
1939
1940static bool send_mayday(struct work_struct *work)
1941{
1942 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1943 struct workqueue_struct *wq = cwq->wq;
Tejun Heof3421792010-07-02 10:03:51 +02001944 unsigned int cpu;
Tejun Heoe22bee72010-06-29 10:07:14 +02001945
1946 if (!(wq->flags & WQ_RESCUER))
1947 return false;
1948
1949 /* mayday mayday mayday */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001950 cpu = cwq->pool->gcwq->cpu;
Tejun Heof3421792010-07-02 10:03:51 +02001951 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1952 if (cpu == WORK_CPU_UNBOUND)
1953 cpu = 0;
Tejun Heof2e005a2010-07-20 15:59:09 +02001954 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001955 wake_up_process(wq->rescuer->task);
1956 return true;
1957}
1958
Tejun Heo63d95a92012-07-12 14:46:37 -07001959static void gcwq_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001960{
Tejun Heo63d95a92012-07-12 14:46:37 -07001961 struct worker_pool *pool = (void *)__pool;
1962 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001963 struct work_struct *work;
1964
1965 spin_lock_irq(&gcwq->lock);
1966
Tejun Heo63d95a92012-07-12 14:46:37 -07001967 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001968 /*
1969 * We've been trying to create a new worker but
1970 * haven't been successful. We might be hitting an
1971 * allocation deadlock. Send distress signals to
1972 * rescuers.
1973 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001974 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001975 send_mayday(work);
1976 }
1977
1978 spin_unlock_irq(&gcwq->lock);
1979
Tejun Heo63d95a92012-07-12 14:46:37 -07001980 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001981}
1982
1983/**
1984 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001985 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001986 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001987 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001988 * have at least one idle worker on return from this function. If
1989 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001990 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001991 * possible allocation deadlock.
1992 *
1993 * On return, need_to_create_worker() is guaranteed to be false and
1994 * may_start_working() true.
1995 *
1996 * LOCKING:
1997 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1998 * multiple times. Does GFP_KERNEL allocations. Called only from
1999 * manager.
2000 *
2001 * RETURNS:
2002 * false if no action was taken and gcwq->lock stayed locked, true
2003 * otherwise.
2004 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002005static bool maybe_create_worker(struct worker_pool *pool)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09002006__releases(&gcwq->lock)
2007__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02002008{
Tejun Heo63d95a92012-07-12 14:46:37 -07002009 struct global_cwq *gcwq = pool->gcwq;
2010
2011 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002012 return false;
2013restart:
Tejun Heo9f9c2362010-07-14 11:31:20 +02002014 spin_unlock_irq(&gcwq->lock);
2015
Tejun Heoe22bee72010-06-29 10:07:14 +02002016 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07002017 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02002018
2019 while (true) {
2020 struct worker *worker;
2021
Tejun Heobc2ae0f2012-07-17 12:39:27 -07002022 worker = create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002023 if (worker) {
Tejun Heo63d95a92012-07-12 14:46:37 -07002024 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02002025 spin_lock_irq(&gcwq->lock);
2026 start_worker(worker);
Tejun Heo63d95a92012-07-12 14:46:37 -07002027 BUG_ON(need_to_create_worker(pool));
Tejun Heoe22bee72010-06-29 10:07:14 +02002028 return true;
2029 }
2030
Tejun Heo63d95a92012-07-12 14:46:37 -07002031 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002032 break;
2033
Tejun Heoe22bee72010-06-29 10:07:14 +02002034 __set_current_state(TASK_INTERRUPTIBLE);
2035 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02002036
Tejun Heo63d95a92012-07-12 14:46:37 -07002037 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002038 break;
2039 }
2040
Tejun Heo63d95a92012-07-12 14:46:37 -07002041 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02002042 spin_lock_irq(&gcwq->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07002043 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002044 goto restart;
2045 return true;
2046}
2047
2048/**
2049 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo63d95a92012-07-12 14:46:37 -07002050 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02002051 *
Tejun Heo63d95a92012-07-12 14:46:37 -07002052 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02002053 * IDLE_WORKER_TIMEOUT.
2054 *
2055 * LOCKING:
2056 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
2057 * multiple times. Called only from manager.
2058 *
2059 * RETURNS:
2060 * false if no action was taken and gcwq->lock stayed locked, true
2061 * otherwise.
2062 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002063static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02002064{
2065 bool ret = false;
2066
Tejun Heo63d95a92012-07-12 14:46:37 -07002067 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02002068 struct worker *worker;
2069 unsigned long expires;
2070
Tejun Heo63d95a92012-07-12 14:46:37 -07002071 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02002072 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
2073
2074 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07002075 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02002076 break;
2077 }
2078
2079 destroy_worker(worker);
2080 ret = true;
2081 }
2082
2083 return ret;
2084}
2085
2086/**
2087 * manage_workers - manage worker pool
2088 * @worker: self
2089 *
2090 * Assume the manager role and manage gcwq worker pool @worker belongs
2091 * to. At any given time, there can be only zero or one manager per
2092 * gcwq. The exclusion is handled automatically by this function.
2093 *
2094 * The caller can safely start processing works on false return. On
2095 * true return, it's guaranteed that need_to_create_worker() is false
2096 * and may_start_working() is true.
2097 *
2098 * CONTEXT:
2099 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
2100 * multiple times. Does GFP_KERNEL allocations.
2101 *
2102 * RETURNS:
2103 * false if no action was taken and gcwq->lock stayed locked, true if
2104 * some action was taken.
2105 */
2106static bool manage_workers(struct worker *worker)
2107{
Tejun Heo63d95a92012-07-12 14:46:37 -07002108 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002109 bool ret = false;
2110
Tejun Heo60373152012-07-17 12:39:27 -07002111 if (!mutex_trylock(&pool->manager_mutex))
Tejun Heoe22bee72010-06-29 10:07:14 +02002112 return ret;
2113
Tejun Heo11ebea52012-07-12 14:46:37 -07002114 pool->flags &= ~POOL_MANAGE_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02002115
2116 /*
2117 * Destroy and then create so that may_start_working() is true
2118 * on return.
2119 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002120 ret |= maybe_destroy_workers(pool);
2121 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002122
Tejun Heo60373152012-07-17 12:39:27 -07002123 mutex_unlock(&pool->manager_mutex);
Tejun Heoe22bee72010-06-29 10:07:14 +02002124 return ret;
2125}
2126
Tejun Heoa62428c2010-06-29 10:07:10 +02002127/**
2128 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02002129 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02002130 * @work: work to process
2131 *
2132 * Process @work. This function contains all the logics necessary to
2133 * process a single work including synchronization against and
2134 * interaction with other workers on the same cpu, queueing and
2135 * flushing. As long as context requirement is met, any worker can
2136 * call this function to process a work.
2137 *
2138 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002139 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02002140 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002141static void process_one_work(struct worker *worker, struct work_struct *work)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09002142__releases(&gcwq->lock)
2143__acquires(&gcwq->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02002144{
Tejun Heo7e116292010-06-29 10:07:13 +02002145 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002146 struct worker_pool *pool = worker->pool;
2147 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002148 struct hlist_head *bwh = busy_worker_head(gcwq, work);
Tejun Heofb0e7be2010-06-29 10:07:15 +02002149 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heoa62428c2010-06-29 10:07:10 +02002150 work_func_t f = work->func;
Tejun Heo73f53c42010-06-29 10:07:11 +02002151 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02002152 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02002153#ifdef CONFIG_LOCKDEP
2154 /*
2155 * It is permissible to free the struct work_struct from
2156 * inside the function that is called from it, this we need to
2157 * take into account for lockdep too. To avoid bogus "held
2158 * lock freed" warnings as well as problems when looking into
2159 * work->lockdep_map, make a copy and use that here.
2160 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07002161 struct lockdep_map lockdep_map;
2162
2163 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002164#endif
Tejun Heo6fec10a2012-07-22 10:16:34 -07002165 /*
2166 * Ensure we're on the correct CPU. DISASSOCIATED test is
2167 * necessary to avoid spurious warnings from rescuers servicing the
2168 * unbound or a disassociated gcwq.
2169 */
Tejun Heo25511a42012-07-17 12:39:27 -07002170 WARN_ON_ONCE(!(worker->flags & (WORKER_UNBOUND | WORKER_REBIND)) &&
Tejun Heo6fec10a2012-07-22 10:16:34 -07002171 !(gcwq->flags & GCWQ_DISASSOCIATED) &&
Tejun Heo25511a42012-07-17 12:39:27 -07002172 raw_smp_processor_id() != gcwq->cpu);
2173
Tejun Heo7e116292010-06-29 10:07:13 +02002174 /*
2175 * A single work shouldn't be executed concurrently by
2176 * multiple workers on a single cpu. Check whether anyone is
2177 * already processing the work. If so, defer the work to the
2178 * currently executing one.
2179 */
2180 collision = __find_worker_executing_work(gcwq, bwh, work);
2181 if (unlikely(collision)) {
2182 move_linked_works(work, &collision->scheduled, NULL);
2183 return;
2184 }
2185
Tejun Heo8930cab2012-08-03 10:30:45 -07002186 /* claim and dequeue */
Tejun Heoa62428c2010-06-29 10:07:10 +02002187 debug_work_deactivate(work);
Tejun Heoc8e55f32010-06-29 10:07:12 +02002188 hlist_add_head(&worker->hentry, bwh);
Tejun Heoc34056a2010-06-29 10:07:11 +02002189 worker->current_work = work;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02002190 worker->current_cwq = cwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002191 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002192
Tejun Heoa62428c2010-06-29 10:07:10 +02002193 list_del_init(&work->entry);
2194
Tejun Heo649027d2010-06-29 10:07:14 +02002195 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02002196 * CPU intensive works don't participate in concurrency
2197 * management. They're the scheduler's responsibility.
2198 */
2199 if (unlikely(cpu_intensive))
2200 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
2201
Tejun Heo974271c2012-07-12 14:46:37 -07002202 /*
2203 * Unbound gcwq isn't concurrency managed and work items should be
2204 * executed ASAP. Wake up another worker if necessary.
2205 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002206 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
2207 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07002208
Tejun Heo8930cab2012-08-03 10:30:45 -07002209 /*
Tejun Heo23657bb2012-08-13 17:08:19 -07002210 * Record the last CPU and clear PENDING which should be the last
2211 * update to @work. Also, do this inside @gcwq->lock so that
2212 * PENDING and queued state changes happen together while IRQ is
2213 * disabled.
Tejun Heo8930cab2012-08-03 10:30:45 -07002214 */
Tejun Heo8930cab2012-08-03 10:30:45 -07002215 set_work_cpu_and_clear_pending(work, gcwq->cpu);
Tejun Heoa62428c2010-06-29 10:07:10 +02002216
Tejun Heo8930cab2012-08-03 10:30:45 -07002217 spin_unlock_irq(&gcwq->lock);
Tejun Heo959d1af2012-08-03 10:30:45 -07002218
Tejun Heoe1594892011-01-09 23:32:15 +01002219 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002220 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002221 trace_workqueue_execute_start(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002222 f(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002223 /*
2224 * While we must be careful to not use "work" after this, the trace
2225 * point will only record its address.
2226 */
2227 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002228 lock_map_release(&lockdep_map);
2229 lock_map_release(&cwq->wq->lockdep_map);
2230
2231 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Valentin Ilie044c7822012-08-19 00:52:42 +03002232 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
2233 " last function: %pf\n",
2234 current->comm, preempt_count(), task_pid_nr(current), f);
Tejun Heoa62428c2010-06-29 10:07:10 +02002235 debug_show_held_locks(current);
2236 dump_stack();
2237 }
2238
Tejun Heo8b03ae32010-06-29 10:07:12 +02002239 spin_lock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002240
Tejun Heofb0e7be2010-06-29 10:07:15 +02002241 /* clear cpu intensive status */
2242 if (unlikely(cpu_intensive))
2243 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2244
Tejun Heoa62428c2010-06-29 10:07:10 +02002245 /* we're done with it, release */
Tejun Heoc8e55f32010-06-29 10:07:12 +02002246 hlist_del_init(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002247 worker->current_work = NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02002248 worker->current_cwq = NULL;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02002249 cwq_dec_nr_in_flight(cwq, work_color, false);
Tejun Heoa62428c2010-06-29 10:07:10 +02002250}
2251
Tejun Heoaffee4b2010-06-29 10:07:12 +02002252/**
2253 * process_scheduled_works - process scheduled works
2254 * @worker: self
2255 *
2256 * Process all scheduled works. Please note that the scheduled list
2257 * may change while processing a work, so this function repeatedly
2258 * fetches a work from the top and executes it.
2259 *
2260 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002261 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02002262 * multiple times.
2263 */
2264static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002266 while (!list_empty(&worker->scheduled)) {
2267 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002269 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271}
2272
Tejun Heo4690c4a2010-06-29 10:07:10 +02002273/**
2274 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02002275 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02002276 *
Tejun Heoe22bee72010-06-29 10:07:14 +02002277 * The gcwq worker thread function. There's a single dynamic pool of
2278 * these per each cpu. These workers process all works regardless of
2279 * their specific target workqueue. The only exception is works which
2280 * belong to workqueues with a rescuer which will be explained in
2281 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02002282 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002283static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284{
Tejun Heoc34056a2010-06-29 10:07:11 +02002285 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07002286 struct worker_pool *pool = worker->pool;
2287 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
Tejun Heoe22bee72010-06-29 10:07:14 +02002289 /* tell the scheduler that this is a workqueue worker */
2290 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002291woke_up:
Tejun Heoc8e55f32010-06-29 10:07:12 +02002292 spin_lock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
Tejun Heo25511a42012-07-17 12:39:27 -07002294 /*
2295 * DIE can be set only while idle and REBIND set while busy has
2296 * @worker->rebind_work scheduled. Checking here is enough.
2297 */
2298 if (unlikely(worker->flags & (WORKER_REBIND | WORKER_DIE))) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02002299 spin_unlock_irq(&gcwq->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07002300
2301 if (worker->flags & WORKER_DIE) {
2302 worker->task->flags &= ~PF_WQ_WORKER;
2303 return 0;
2304 }
2305
2306 idle_worker_rebind(worker);
2307 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 }
2309
Tejun Heoc8e55f32010-06-29 10:07:12 +02002310 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002311recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002312 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002313 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002314 goto sleep;
2315
2316 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002317 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002318 goto recheck;
2319
Tejun Heoc8e55f32010-06-29 10:07:12 +02002320 /*
2321 * ->scheduled list can only be filled while a worker is
2322 * preparing to process a work or actually processing it.
2323 * Make sure nobody diddled with it while I was sleeping.
2324 */
2325 BUG_ON(!list_empty(&worker->scheduled));
2326
Tejun Heoe22bee72010-06-29 10:07:14 +02002327 /*
2328 * When control reaches this point, we're guaranteed to have
2329 * at least one idle worker or that someone else has already
2330 * assumed the manager role.
2331 */
2332 worker_clr_flags(worker, WORKER_PREP);
2333
2334 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002335 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002336 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002337 struct work_struct, entry);
2338
2339 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2340 /* optimization path, not strictly necessary */
2341 process_one_work(worker, work);
2342 if (unlikely(!list_empty(&worker->scheduled)))
2343 process_scheduled_works(worker);
2344 } else {
2345 move_linked_works(work, &worker->scheduled, NULL);
2346 process_scheduled_works(worker);
2347 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002348 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002349
Tejun Heoe22bee72010-06-29 10:07:14 +02002350 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002351sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07002352 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002353 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002354
Tejun Heoc8e55f32010-06-29 10:07:12 +02002355 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02002356 * gcwq->lock is held and there's no work to process and no
2357 * need to manage, sleep. Workers are woken up only while
2358 * holding gcwq->lock or from local cpu, so setting the
2359 * current state before releasing gcwq->lock is enough to
2360 * prevent losing any event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002361 */
2362 worker_enter_idle(worker);
2363 __set_current_state(TASK_INTERRUPTIBLE);
2364 spin_unlock_irq(&gcwq->lock);
2365 schedule();
2366 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367}
2368
Tejun Heoe22bee72010-06-29 10:07:14 +02002369/**
2370 * rescuer_thread - the rescuer thread function
2371 * @__wq: the associated workqueue
2372 *
2373 * Workqueue rescuer thread function. There's one rescuer for each
2374 * workqueue which has WQ_RESCUER set.
2375 *
2376 * Regular work processing on a gcwq may block trying to create a new
2377 * worker which uses GFP_KERNEL allocation which has slight chance of
2378 * developing into deadlock if some works currently on the same queue
2379 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2380 * the problem rescuer solves.
2381 *
2382 * When such condition is possible, the gcwq summons rescuers of all
2383 * workqueues which have works queued on the gcwq and let them process
2384 * those works so that forward progress can be guaranteed.
2385 *
2386 * This should happen rarely.
2387 */
2388static int rescuer_thread(void *__wq)
2389{
2390 struct workqueue_struct *wq = __wq;
2391 struct worker *rescuer = wq->rescuer;
2392 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heof3421792010-07-02 10:03:51 +02002393 bool is_unbound = wq->flags & WQ_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02002394 unsigned int cpu;
2395
2396 set_user_nice(current, RESCUER_NICE_LEVEL);
2397repeat:
2398 set_current_state(TASK_INTERRUPTIBLE);
2399
2400 if (kthread_should_stop())
2401 return 0;
2402
Tejun Heof3421792010-07-02 10:03:51 +02002403 /*
2404 * See whether any cpu is asking for help. Unbounded
2405 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
2406 */
Tejun Heof2e005a2010-07-20 15:59:09 +02002407 for_each_mayday_cpu(cpu, wq->mayday_mask) {
Tejun Heof3421792010-07-02 10:03:51 +02002408 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2409 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002410 struct worker_pool *pool = cwq->pool;
2411 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002412 struct work_struct *work, *n;
2413
2414 __set_current_state(TASK_RUNNING);
Tejun Heof2e005a2010-07-20 15:59:09 +02002415 mayday_clear_cpu(cpu, wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002416
2417 /* migrate to the target cpu if possible */
Tejun Heobd7bdd42012-07-12 14:46:37 -07002418 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002419 worker_maybe_bind_and_lock(rescuer);
2420
2421 /*
2422 * Slurp in all works issued via this workqueue and
2423 * process'em.
2424 */
2425 BUG_ON(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002426 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02002427 if (get_work_cwq(work) == cwq)
2428 move_linked_works(work, scheduled, &n);
2429
2430 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002431
2432 /*
2433 * Leave this gcwq. If keep_working() is %true, notify a
2434 * regular worker; otherwise, we end up with 0 concurrency
2435 * and stalling the execution.
2436 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002437 if (keep_working(pool))
2438 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002439
Tejun Heoe22bee72010-06-29 10:07:14 +02002440 spin_unlock_irq(&gcwq->lock);
2441 }
2442
2443 schedule();
2444 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445}
2446
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002447struct wq_barrier {
2448 struct work_struct work;
2449 struct completion done;
2450};
2451
2452static void wq_barrier_func(struct work_struct *work)
2453{
2454 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2455 complete(&barr->done);
2456}
2457
Tejun Heo4690c4a2010-06-29 10:07:10 +02002458/**
2459 * insert_wq_barrier - insert a barrier work
2460 * @cwq: cwq to insert barrier into
2461 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002462 * @target: target work to attach @barr to
2463 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002464 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002465 * @barr is linked to @target such that @barr is completed only after
2466 * @target finishes execution. Please note that the ordering
2467 * guarantee is observed only with respect to @target and on the local
2468 * cpu.
2469 *
2470 * Currently, a queued barrier can't be canceled. This is because
2471 * try_to_grab_pending() can't determine whether the work to be
2472 * grabbed is at the head of the queue and thus can't clear LINKED
2473 * flag of the previous work while there must be a valid next work
2474 * after a work with LINKED flag set.
2475 *
2476 * Note that when @worker is non-NULL, @target may be modified
2477 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002478 *
2479 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002480 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002481 */
Oleg Nesterov83c22522007-05-09 02:33:54 -07002482static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002483 struct wq_barrier *barr,
2484 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002485{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002486 struct list_head *head;
2487 unsigned int linked = 0;
2488
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002489 /*
Tejun Heo8b03ae32010-06-29 10:07:12 +02002490 * debugobject calls are safe here even with gcwq->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002491 * as we know for sure that this will not trigger any of the
2492 * checks and call back into the fixup functions where we
2493 * might deadlock.
2494 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002495 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002496 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002497 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002498
Tejun Heoaffee4b2010-06-29 10:07:12 +02002499 /*
2500 * If @target is currently being executed, schedule the
2501 * barrier to the worker; otherwise, put it after @target.
2502 */
2503 if (worker)
2504 head = worker->scheduled.next;
2505 else {
2506 unsigned long *bits = work_data_bits(target);
2507
2508 head = target->entry.next;
2509 /* there can already be other linked works, inherit and set */
2510 linked = *bits & WORK_STRUCT_LINKED;
2511 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2512 }
2513
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002514 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002515 insert_work(cwq, &barr->work, head,
2516 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002517}
2518
Tejun Heo73f53c42010-06-29 10:07:11 +02002519/**
2520 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2521 * @wq: workqueue being flushed
2522 * @flush_color: new flush color, < 0 for no-op
2523 * @work_color: new work color, < 0 for no-op
2524 *
2525 * Prepare cwqs for workqueue flushing.
2526 *
2527 * If @flush_color is non-negative, flush_color on all cwqs should be
2528 * -1. If no cwq has in-flight commands at the specified color, all
2529 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2530 * has in flight commands, its cwq->flush_color is set to
2531 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2532 * wakeup logic is armed and %true is returned.
2533 *
2534 * The caller should have initialized @wq->first_flusher prior to
2535 * calling this function with non-negative @flush_color. If
2536 * @flush_color is negative, no flush color update is done and %false
2537 * is returned.
2538 *
2539 * If @work_color is non-negative, all cwqs should have the same
2540 * work_color which is previous to @work_color and all will be
2541 * advanced to @work_color.
2542 *
2543 * CONTEXT:
2544 * mutex_lock(wq->flush_mutex).
2545 *
2546 * RETURNS:
2547 * %true if @flush_color >= 0 and there's something to flush. %false
2548 * otherwise.
2549 */
2550static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2551 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552{
Tejun Heo73f53c42010-06-29 10:07:11 +02002553 bool wait = false;
2554 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -07002555
Tejun Heo73f53c42010-06-29 10:07:11 +02002556 if (flush_color >= 0) {
2557 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2558 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002559 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002560
Tejun Heof3421792010-07-02 10:03:51 +02002561 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002562 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002563 struct global_cwq *gcwq = cwq->pool->gcwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002564
Tejun Heo8b03ae32010-06-29 10:07:12 +02002565 spin_lock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002566
2567 if (flush_color >= 0) {
2568 BUG_ON(cwq->flush_color != -1);
2569
2570 if (cwq->nr_in_flight[flush_color]) {
2571 cwq->flush_color = flush_color;
2572 atomic_inc(&wq->nr_cwqs_to_flush);
2573 wait = true;
2574 }
2575 }
2576
2577 if (work_color >= 0) {
2578 BUG_ON(work_color != work_next_color(cwq->work_color));
2579 cwq->work_color = work_color;
2580 }
2581
Tejun Heo8b03ae32010-06-29 10:07:12 +02002582 spin_unlock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002583 }
2584
2585 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2586 complete(&wq->first_flusher->done);
2587
2588 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589}
2590
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002591/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002593 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 *
2595 * Forces execution of the workqueue and blocks until its completion.
2596 * This is typically used in driver shutdown handlers.
2597 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002598 * We sleep until all works which were queued on entry have been handled,
2599 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002601void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602{
Tejun Heo73f53c42010-06-29 10:07:11 +02002603 struct wq_flusher this_flusher = {
2604 .list = LIST_HEAD_INIT(this_flusher.list),
2605 .flush_color = -1,
2606 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2607 };
2608 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002609
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002610 lock_map_acquire(&wq->lockdep_map);
2611 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002612
2613 mutex_lock(&wq->flush_mutex);
2614
2615 /*
2616 * Start-to-wait phase
2617 */
2618 next_color = work_next_color(wq->work_color);
2619
2620 if (next_color != wq->flush_color) {
2621 /*
2622 * Color space is not full. The current work_color
2623 * becomes our flush_color and work_color is advanced
2624 * by one.
2625 */
2626 BUG_ON(!list_empty(&wq->flusher_overflow));
2627 this_flusher.flush_color = wq->work_color;
2628 wq->work_color = next_color;
2629
2630 if (!wq->first_flusher) {
2631 /* no flush in progress, become the first flusher */
2632 BUG_ON(wq->flush_color != this_flusher.flush_color);
2633
2634 wq->first_flusher = &this_flusher;
2635
2636 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2637 wq->work_color)) {
2638 /* nothing to flush, done */
2639 wq->flush_color = next_color;
2640 wq->first_flusher = NULL;
2641 goto out_unlock;
2642 }
2643 } else {
2644 /* wait in queue */
2645 BUG_ON(wq->flush_color == this_flusher.flush_color);
2646 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2647 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2648 }
2649 } else {
2650 /*
2651 * Oops, color space is full, wait on overflow queue.
2652 * The next flush completion will assign us
2653 * flush_color and transfer to flusher_queue.
2654 */
2655 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2656 }
2657
2658 mutex_unlock(&wq->flush_mutex);
2659
2660 wait_for_completion(&this_flusher.done);
2661
2662 /*
2663 * Wake-up-and-cascade phase
2664 *
2665 * First flushers are responsible for cascading flushes and
2666 * handling overflow. Non-first flushers can simply return.
2667 */
2668 if (wq->first_flusher != &this_flusher)
2669 return;
2670
2671 mutex_lock(&wq->flush_mutex);
2672
Tejun Heo4ce48b32010-07-02 10:03:51 +02002673 /* we might have raced, check again with mutex held */
2674 if (wq->first_flusher != &this_flusher)
2675 goto out_unlock;
2676
Tejun Heo73f53c42010-06-29 10:07:11 +02002677 wq->first_flusher = NULL;
2678
2679 BUG_ON(!list_empty(&this_flusher.list));
2680 BUG_ON(wq->flush_color != this_flusher.flush_color);
2681
2682 while (true) {
2683 struct wq_flusher *next, *tmp;
2684
2685 /* complete all the flushers sharing the current flush color */
2686 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2687 if (next->flush_color != wq->flush_color)
2688 break;
2689 list_del_init(&next->list);
2690 complete(&next->done);
2691 }
2692
2693 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2694 wq->flush_color != work_next_color(wq->work_color));
2695
2696 /* this flush_color is finished, advance by one */
2697 wq->flush_color = work_next_color(wq->flush_color);
2698
2699 /* one color has been freed, handle overflow queue */
2700 if (!list_empty(&wq->flusher_overflow)) {
2701 /*
2702 * Assign the same color to all overflowed
2703 * flushers, advance work_color and append to
2704 * flusher_queue. This is the start-to-wait
2705 * phase for these overflowed flushers.
2706 */
2707 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2708 tmp->flush_color = wq->work_color;
2709
2710 wq->work_color = work_next_color(wq->work_color);
2711
2712 list_splice_tail_init(&wq->flusher_overflow,
2713 &wq->flusher_queue);
2714 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2715 }
2716
2717 if (list_empty(&wq->flusher_queue)) {
2718 BUG_ON(wq->flush_color != wq->work_color);
2719 break;
2720 }
2721
2722 /*
2723 * Need to flush more colors. Make the next flusher
2724 * the new first flusher and arm cwqs.
2725 */
2726 BUG_ON(wq->flush_color == wq->work_color);
2727 BUG_ON(wq->flush_color != next->flush_color);
2728
2729 list_del_init(&next->list);
2730 wq->first_flusher = next;
2731
2732 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2733 break;
2734
2735 /*
2736 * Meh... this color is already done, clear first
2737 * flusher and repeat cascading.
2738 */
2739 wq->first_flusher = NULL;
2740 }
2741
2742out_unlock:
2743 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744}
Dave Jonesae90dd52006-06-30 01:40:45 -04002745EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002747/**
2748 * drain_workqueue - drain a workqueue
2749 * @wq: workqueue to drain
2750 *
2751 * Wait until the workqueue becomes empty. While draining is in progress,
2752 * only chain queueing is allowed. IOW, only currently pending or running
2753 * work items on @wq can queue further work items on it. @wq is flushed
2754 * repeatedly until it becomes empty. The number of flushing is detemined
2755 * by the depth of chaining and should be relatively short. Whine if it
2756 * takes too long.
2757 */
2758void drain_workqueue(struct workqueue_struct *wq)
2759{
2760 unsigned int flush_cnt = 0;
2761 unsigned int cpu;
2762
2763 /*
2764 * __queue_work() needs to test whether there are drainers, is much
2765 * hotter than drain_workqueue() and already looks at @wq->flags.
2766 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2767 */
2768 spin_lock(&workqueue_lock);
2769 if (!wq->nr_drainers++)
2770 wq->flags |= WQ_DRAINING;
2771 spin_unlock(&workqueue_lock);
2772reflush:
2773 flush_workqueue(wq);
2774
2775 for_each_cwq_cpu(cpu, wq) {
2776 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002777 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002778
Tejun Heobd7bdd42012-07-12 14:46:37 -07002779 spin_lock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002780 drained = !cwq->nr_active && list_empty(&cwq->delayed_works);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002781 spin_unlock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002782
2783 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002784 continue;
2785
2786 if (++flush_cnt == 10 ||
2787 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
Valentin Ilie044c7822012-08-19 00:52:42 +03002788 pr_warn("workqueue %s: flush on destruction isn't complete after %u tries\n",
2789 wq->name, flush_cnt);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002790 goto reflush;
2791 }
2792
2793 spin_lock(&workqueue_lock);
2794 if (!--wq->nr_drainers)
2795 wq->flags &= ~WQ_DRAINING;
2796 spin_unlock(&workqueue_lock);
2797}
2798EXPORT_SYMBOL_GPL(drain_workqueue);
2799
Tejun Heo606a5022012-08-20 14:51:23 -07002800static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
Tejun Heobaf59022010-09-16 10:42:16 +02002801{
2802 struct worker *worker = NULL;
2803 struct global_cwq *gcwq;
2804 struct cpu_workqueue_struct *cwq;
2805
2806 might_sleep();
2807 gcwq = get_work_gcwq(work);
2808 if (!gcwq)
2809 return false;
2810
2811 spin_lock_irq(&gcwq->lock);
2812 if (!list_empty(&work->entry)) {
2813 /*
2814 * See the comment near try_to_grab_pending()->smp_rmb().
2815 * If it was re-queued to a different gcwq under us, we
2816 * are not going to wait.
2817 */
2818 smp_rmb();
2819 cwq = get_work_cwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002820 if (unlikely(!cwq || gcwq != cwq->pool->gcwq))
Tejun Heobaf59022010-09-16 10:42:16 +02002821 goto already_gone;
Tejun Heo606a5022012-08-20 14:51:23 -07002822 } else {
Tejun Heobaf59022010-09-16 10:42:16 +02002823 worker = find_worker_executing_work(gcwq, work);
2824 if (!worker)
2825 goto already_gone;
2826 cwq = worker->current_cwq;
Tejun Heo606a5022012-08-20 14:51:23 -07002827 }
Tejun Heobaf59022010-09-16 10:42:16 +02002828
2829 insert_wq_barrier(cwq, barr, work, worker);
2830 spin_unlock_irq(&gcwq->lock);
2831
Tejun Heoe1594892011-01-09 23:32:15 +01002832 /*
2833 * If @max_active is 1 or rescuer is in use, flushing another work
2834 * item on the same workqueue may lead to deadlock. Make sure the
2835 * flusher is not running on the same workqueue by verifying write
2836 * access.
2837 */
2838 if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER)
2839 lock_map_acquire(&cwq->wq->lockdep_map);
2840 else
2841 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heobaf59022010-09-16 10:42:16 +02002842 lock_map_release(&cwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002843
Tejun Heobaf59022010-09-16 10:42:16 +02002844 return true;
2845already_gone:
2846 spin_unlock_irq(&gcwq->lock);
2847 return false;
2848}
2849
Oleg Nesterovdb700892008-07-25 01:47:49 -07002850/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002851 * flush_work - wait for a work to finish executing the last queueing instance
2852 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002853 *
Tejun Heo606a5022012-08-20 14:51:23 -07002854 * Wait until @work has finished execution. @work is guaranteed to be idle
2855 * on return if it hasn't been requeued since flush started.
Tejun Heo401a8d02010-09-16 10:36:00 +02002856 *
2857 * RETURNS:
2858 * %true if flush_work() waited for the work to finish execution,
2859 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002860 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002861bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002862{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002863 struct wq_barrier barr;
2864
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002865 lock_map_acquire(&work->lockdep_map);
2866 lock_map_release(&work->lockdep_map);
2867
Tejun Heo606a5022012-08-20 14:51:23 -07002868 if (start_flush_work(work, &barr)) {
Tejun Heobaf59022010-09-16 10:42:16 +02002869 wait_for_completion(&barr.done);
2870 destroy_work_on_stack(&barr.work);
2871 return true;
Tejun Heo606a5022012-08-20 14:51:23 -07002872 } else {
Tejun Heobaf59022010-09-16 10:42:16 +02002873 return false;
Tejun Heo606a5022012-08-20 14:51:23 -07002874 }
Oleg Nesterovdb700892008-07-25 01:47:49 -07002875}
2876EXPORT_SYMBOL_GPL(flush_work);
2877
Tejun Heo36e227d2012-08-03 10:30:46 -07002878static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002879{
Tejun Heobbb68df2012-08-03 10:30:46 -07002880 unsigned long flags;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002881 int ret;
2882
2883 do {
Tejun Heobbb68df2012-08-03 10:30:46 -07002884 ret = try_to_grab_pending(work, is_dwork, &flags);
2885 /*
2886 * If someone else is canceling, wait for the same event it
2887 * would be waiting for before retrying.
2888 */
2889 if (unlikely(ret == -ENOENT))
Tejun Heo606a5022012-08-20 14:51:23 -07002890 flush_work(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002891 } while (unlikely(ret < 0));
2892
Tejun Heobbb68df2012-08-03 10:30:46 -07002893 /* tell other tasks trying to grab @work to back off */
2894 mark_work_canceling(work);
2895 local_irq_restore(flags);
2896
Tejun Heo606a5022012-08-20 14:51:23 -07002897 flush_work(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002898 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002899 return ret;
2900}
2901
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002902/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002903 * cancel_work_sync - cancel a work and wait for it to finish
2904 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002905 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002906 * Cancel @work and wait for its execution to finish. This function
2907 * can be used even if the work re-queues itself or migrates to
2908 * another workqueue. On return from this function, @work is
2909 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002910 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002911 * cancel_work_sync(&delayed_work->work) must not be used for
2912 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002913 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002914 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002915 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002916 *
2917 * RETURNS:
2918 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002919 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002920bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002921{
Tejun Heo36e227d2012-08-03 10:30:46 -07002922 return __cancel_work_timer(work, false);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002923}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002924EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002925
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002926/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002927 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2928 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002929 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002930 * Delayed timer is cancelled and the pending work is queued for
2931 * immediate execution. Like flush_work(), this function only
2932 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002933 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002934 * RETURNS:
2935 * %true if flush_work() waited for the work to finish execution,
2936 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002937 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002938bool flush_delayed_work(struct delayed_work *dwork)
2939{
Tejun Heo8930cab2012-08-03 10:30:45 -07002940 local_irq_disable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002941 if (del_timer_sync(&dwork->timer))
Tejun Heo12650572012-08-08 09:38:42 -07002942 __queue_work(dwork->cpu,
Tejun Heo401a8d02010-09-16 10:36:00 +02002943 get_work_cwq(&dwork->work)->wq, &dwork->work);
Tejun Heo8930cab2012-08-03 10:30:45 -07002944 local_irq_enable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002945 return flush_work(&dwork->work);
2946}
2947EXPORT_SYMBOL(flush_delayed_work);
2948
2949/**
2950 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2951 * @dwork: the delayed work cancel
2952 *
2953 * This is cancel_work_sync() for delayed works.
2954 *
2955 * RETURNS:
2956 * %true if @dwork was pending, %false otherwise.
2957 */
2958bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002959{
Tejun Heo36e227d2012-08-03 10:30:46 -07002960 return __cancel_work_timer(&dwork->work, true);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002961}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002962EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
Tejun Heod4283e92012-08-03 10:30:44 -07002964/**
Tejun Heo0a13c002012-08-03 10:30:44 -07002965 * schedule_work_on - put work task on a specific cpu
2966 * @cpu: cpu to put the work task on
2967 * @work: job to be done
2968 *
2969 * This puts a job on a specific cpu
2970 */
Tejun Heod4283e92012-08-03 10:30:44 -07002971bool schedule_work_on(int cpu, struct work_struct *work)
Tejun Heo0a13c002012-08-03 10:30:44 -07002972{
2973 return queue_work_on(cpu, system_wq, work);
2974}
2975EXPORT_SYMBOL(schedule_work_on);
2976
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002977/**
2978 * schedule_work - put work task in global workqueue
2979 * @work: job to be done
2980 *
Tejun Heod4283e92012-08-03 10:30:44 -07002981 * Returns %false if @work was already on the kernel-global workqueue and
2982 * %true otherwise.
Bart Van Assche5b0f437d2009-07-30 19:00:53 +02002983 *
2984 * This puts a job in the kernel-global workqueue if it was not already
2985 * queued and leaves it in the same position on the kernel-global
2986 * workqueue otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002987 */
Tejun Heod4283e92012-08-03 10:30:44 -07002988bool schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989{
Tejun Heod320c032010-06-29 10:07:14 +02002990 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991}
Dave Jonesae90dd52006-06-30 01:40:45 -04002992EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002994/**
2995 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
2996 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +00002997 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002998 * @delay: number of jiffies to wait
2999 *
3000 * After waiting for a given time this puts a job in the kernel-global
3001 * workqueue on the specified CPU.
3002 */
Tejun Heod4283e92012-08-03 10:30:44 -07003003bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork,
3004 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005{
Tejun Heod320c032010-06-29 10:07:14 +02003006 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007}
Dave Jonesae90dd52006-06-30 01:40:45 -04003008EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Andrew Mortonb6136772006-06-25 05:47:49 -07003010/**
Tejun Heo0a13c002012-08-03 10:30:44 -07003011 * schedule_delayed_work - put work task in global workqueue after delay
3012 * @dwork: job to be done
3013 * @delay: number of jiffies to wait or 0 for immediate execution
3014 *
3015 * After waiting for a given time this puts a job in the kernel-global
3016 * workqueue.
3017 */
Tejun Heod4283e92012-08-03 10:30:44 -07003018bool schedule_delayed_work(struct delayed_work *dwork, unsigned long delay)
Tejun Heo0a13c002012-08-03 10:30:44 -07003019{
3020 return queue_delayed_work(system_wq, dwork, delay);
3021}
3022EXPORT_SYMBOL(schedule_delayed_work);
3023
3024/**
Tejun Heo31ddd872010-10-19 11:14:49 +02003025 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07003026 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07003027 *
Tejun Heo31ddd872010-10-19 11:14:49 +02003028 * schedule_on_each_cpu() executes @func on each online CPU using the
3029 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07003030 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02003031 *
3032 * RETURNS:
3033 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07003034 */
David Howells65f27f32006-11-22 14:55:48 +00003035int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08003036{
3037 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02003038 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08003039
Andrew Mortonb6136772006-06-25 05:47:49 -07003040 works = alloc_percpu(struct work_struct);
3041 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08003042 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07003043
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003044 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08003045
Christoph Lameter15316ba2006-01-08 01:00:43 -08003046 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01003047 struct work_struct *work = per_cpu_ptr(works, cpu);
3048
3049 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003050 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02003051 }
Tejun Heo93981802009-11-17 14:06:20 -08003052
3053 for_each_online_cpu(cpu)
3054 flush_work(per_cpu_ptr(works, cpu));
3055
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003056 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07003057 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08003058 return 0;
3059}
3060
Alan Sterneef6a7d2010-02-12 17:39:21 +09003061/**
3062 * flush_scheduled_work - ensure that any scheduled work has run to completion.
3063 *
3064 * Forces execution of the kernel-global workqueue and blocks until its
3065 * completion.
3066 *
3067 * Think twice before calling this function! It's very easy to get into
3068 * trouble if you don't take great care. Either of the following situations
3069 * will lead to deadlock:
3070 *
3071 * One of the work items currently on the workqueue needs to acquire
3072 * a lock held by your code or its caller.
3073 *
3074 * Your code is running in the context of a work routine.
3075 *
3076 * They will be detected by lockdep when they occur, but the first might not
3077 * occur very often. It depends on what work items are on the workqueue and
3078 * what locks they need, which you have no control over.
3079 *
3080 * In most situations flushing the entire workqueue is overkill; you merely
3081 * need to know that a particular work item isn't queued and isn't running.
3082 * In such cases you should use cancel_delayed_work_sync() or
3083 * cancel_work_sync() instead.
3084 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085void flush_scheduled_work(void)
3086{
Tejun Heod320c032010-06-29 10:07:14 +02003087 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088}
Dave Jonesae90dd52006-06-30 01:40:45 -04003089EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
3091/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06003092 * execute_in_process_context - reliably execute the routine with user context
3093 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06003094 * @ew: guaranteed storage for the execute work structure (must
3095 * be available when the work executes)
3096 *
3097 * Executes the function immediately if process context is available,
3098 * otherwise schedules the function for delayed execution.
3099 *
3100 * Returns: 0 - function was executed
3101 * 1 - function was scheduled for execution
3102 */
David Howells65f27f32006-11-22 14:55:48 +00003103int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06003104{
3105 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00003106 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003107 return 0;
3108 }
3109
David Howells65f27f32006-11-22 14:55:48 +00003110 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003111 schedule_work(&ew->work);
3112
3113 return 1;
3114}
3115EXPORT_SYMBOL_GPL(execute_in_process_context);
3116
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117int keventd_up(void)
3118{
Tejun Heod320c032010-06-29 10:07:14 +02003119 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120}
3121
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003122static int alloc_cwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123{
Oleg Nesterov3af244332007-05-09 02:34:09 -07003124 /*
Tejun Heo0f900042010-06-29 10:07:11 +02003125 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
3126 * Make sure that the alignment isn't lower than that of
3127 * unsigned long long.
Oleg Nesterov3af244332007-05-09 02:34:09 -07003128 */
Tejun Heo0f900042010-06-29 10:07:11 +02003129 const size_t size = sizeof(struct cpu_workqueue_struct);
3130 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
3131 __alignof__(unsigned long long));
Oleg Nesterov3af244332007-05-09 02:34:09 -07003132
Lai Jiangshane06ffa12012-03-09 18:03:20 +08003133 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02003134 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
Tejun Heo931ac772010-07-20 11:07:48 +02003135 else {
Tejun Heof3421792010-07-02 10:03:51 +02003136 void *ptr;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01003137
Tejun Heof3421792010-07-02 10:03:51 +02003138 /*
3139 * Allocate enough room to align cwq and put an extra
3140 * pointer at the end pointing back to the originally
3141 * allocated pointer which will be used for free.
3142 */
3143 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
3144 if (ptr) {
3145 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
3146 *(void **)(wq->cpu_wq.single + 1) = ptr;
3147 }
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003148 }
Tejun Heof3421792010-07-02 10:03:51 +02003149
Tejun Heo0415b00d12011-03-24 18:50:09 +01003150 /* just in case, make sure it's actually aligned */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003151 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
3152 return wq->cpu_wq.v ? 0 : -ENOMEM;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003153}
3154
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003155static void free_cwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07003156{
Lai Jiangshane06ffa12012-03-09 18:03:20 +08003157 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02003158 free_percpu(wq->cpu_wq.pcpu);
3159 else if (wq->cpu_wq.single) {
3160 /* the pointer to free is stored right after the cwq */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003161 kfree(*(void **)(wq->cpu_wq.single + 1));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07003162 }
3163}
3164
Tejun Heof3421792010-07-02 10:03:51 +02003165static int wq_clamp_max_active(int max_active, unsigned int flags,
3166 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003167{
Tejun Heof3421792010-07-02 10:03:51 +02003168 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3169
3170 if (max_active < 1 || max_active > lim)
Valentin Ilie044c7822012-08-19 00:52:42 +03003171 pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
3172 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003173
Tejun Heof3421792010-07-02 10:03:51 +02003174 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003175}
3176
Tejun Heob196be82012-01-10 15:11:35 -08003177struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02003178 unsigned int flags,
3179 int max_active,
3180 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08003181 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003182{
Tejun Heob196be82012-01-10 15:11:35 -08003183 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003184 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02003185 unsigned int cpu;
Tejun Heob196be82012-01-10 15:11:35 -08003186 size_t namelen;
3187
3188 /* determine namelen, allocate wq and format name */
3189 va_start(args, lock_name);
3190 va_copy(args1, args);
3191 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3192
3193 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3194 if (!wq)
3195 goto err;
3196
3197 vsnprintf(wq->name, namelen, fmt, args1);
3198 va_end(args);
3199 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003200
Tejun Heof3421792010-07-02 10:03:51 +02003201 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02003202 * Workqueues which may be used during memory reclaim should
3203 * have a rescuer to guarantee forward progress.
3204 */
3205 if (flags & WQ_MEM_RECLAIM)
3206 flags |= WQ_RESCUER;
3207
Tejun Heod320c032010-06-29 10:07:14 +02003208 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003209 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003210
Tejun Heob196be82012-01-10 15:11:35 -08003211 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003212 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003213 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003214 mutex_init(&wq->flush_mutex);
3215 atomic_set(&wq->nr_cwqs_to_flush, 0);
3216 INIT_LIST_HEAD(&wq->flusher_queue);
3217 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003218
Johannes Bergeb13ba82008-01-16 09:51:58 +01003219 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003220 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003221
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003222 if (alloc_cwqs(wq) < 0)
3223 goto err;
3224
Tejun Heof3421792010-07-02 10:03:51 +02003225 for_each_cwq_cpu(cpu, wq) {
Tejun Heo15376632010-06-29 10:07:11 +02003226 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003227 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo32704762012-07-13 22:16:45 -07003228 int pool_idx = (bool)(flags & WQ_HIGHPRI);
Tejun Heo15376632010-06-29 10:07:11 +02003229
Tejun Heo0f900042010-06-29 10:07:11 +02003230 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heo32704762012-07-13 22:16:45 -07003231 cwq->pool = &gcwq->pools[pool_idx];
Tejun Heoc34056a2010-06-29 10:07:11 +02003232 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02003233 cwq->flush_color = -1;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003234 cwq->max_active = max_active;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003235 INIT_LIST_HEAD(&cwq->delayed_works);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003236 }
3237
Tejun Heoe22bee72010-06-29 10:07:14 +02003238 if (flags & WQ_RESCUER) {
3239 struct worker *rescuer;
3240
Tejun Heof2e005a2010-07-20 15:59:09 +02003241 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
Tejun Heoe22bee72010-06-29 10:07:14 +02003242 goto err;
3243
3244 wq->rescuer = rescuer = alloc_worker();
3245 if (!rescuer)
3246 goto err;
3247
Tejun Heob196be82012-01-10 15:11:35 -08003248 rescuer->task = kthread_create(rescuer_thread, wq, "%s",
3249 wq->name);
Tejun Heoe22bee72010-06-29 10:07:14 +02003250 if (IS_ERR(rescuer->task))
3251 goto err;
3252
Tejun Heoe22bee72010-06-29 10:07:14 +02003253 rescuer->task->flags |= PF_THREAD_BOUND;
3254 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003255 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003256
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003257 /*
3258 * workqueue_lock protects global freeze state and workqueues
3259 * list. Grab it, set max_active accordingly and add the new
3260 * workqueue to workqueues list.
3261 */
Tejun Heo15376632010-06-29 10:07:11 +02003262 spin_lock(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003263
Tejun Heo58a69cb2011-02-16 09:25:31 +01003264 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heof3421792010-07-02 10:03:51 +02003265 for_each_cwq_cpu(cpu, wq)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003266 get_cwq(cpu, wq)->max_active = 0;
3267
Tejun Heo15376632010-06-29 10:07:11 +02003268 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003269
Tejun Heo15376632010-06-29 10:07:11 +02003270 spin_unlock(&workqueue_lock);
3271
Oleg Nesterov3af244332007-05-09 02:34:09 -07003272 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02003273err:
3274 if (wq) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003275 free_cwqs(wq);
Tejun Heof2e005a2010-07-20 15:59:09 +02003276 free_mayday_mask(wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02003277 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003278 kfree(wq);
3279 }
3280 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003281}
Tejun Heod320c032010-06-29 10:07:14 +02003282EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003283
3284/**
3285 * destroy_workqueue - safely terminate a workqueue
3286 * @wq: target workqueue
3287 *
3288 * Safely destroy a workqueue. All work currently pending will be done first.
3289 */
3290void destroy_workqueue(struct workqueue_struct *wq)
3291{
Tejun Heoc8e55f32010-06-29 10:07:12 +02003292 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003293
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003294 /* drain it before proceeding with destruction */
3295 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003296
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003297 /*
3298 * wq list is used to freeze wq, remove from list after
3299 * flushing is complete in case freeze races us.
3300 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003301 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07003302 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003303 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003304
Tejun Heoe22bee72010-06-29 10:07:14 +02003305 /* sanity check */
Tejun Heof3421792010-07-02 10:03:51 +02003306 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02003307 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3308 int i;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003309
Tejun Heo73f53c42010-06-29 10:07:11 +02003310 for (i = 0; i < WORK_NR_COLORS; i++)
3311 BUG_ON(cwq->nr_in_flight[i]);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003312 BUG_ON(cwq->nr_active);
3313 BUG_ON(!list_empty(&cwq->delayed_works));
Tejun Heo73f53c42010-06-29 10:07:11 +02003314 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003315
Tejun Heoe22bee72010-06-29 10:07:14 +02003316 if (wq->flags & WQ_RESCUER) {
3317 kthread_stop(wq->rescuer->task);
Tejun Heof2e005a2010-07-20 15:59:09 +02003318 free_mayday_mask(wq->mayday_mask);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003319 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003320 }
3321
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003322 free_cwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003323 kfree(wq);
3324}
3325EXPORT_SYMBOL_GPL(destroy_workqueue);
3326
Tejun Heodcd989c2010-06-29 10:07:14 +02003327/**
3328 * workqueue_set_max_active - adjust max_active of a workqueue
3329 * @wq: target workqueue
3330 * @max_active: new max_active value.
3331 *
3332 * Set max_active of @wq to @max_active.
3333 *
3334 * CONTEXT:
3335 * Don't call from IRQ context.
3336 */
3337void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3338{
3339 unsigned int cpu;
3340
Tejun Heof3421792010-07-02 10:03:51 +02003341 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003342
3343 spin_lock(&workqueue_lock);
3344
3345 wq->saved_max_active = max_active;
3346
Tejun Heof3421792010-07-02 10:03:51 +02003347 for_each_cwq_cpu(cpu, wq) {
Tejun Heodcd989c2010-06-29 10:07:14 +02003348 struct global_cwq *gcwq = get_gcwq(cpu);
3349
3350 spin_lock_irq(&gcwq->lock);
3351
Tejun Heo58a69cb2011-02-16 09:25:31 +01003352 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heodcd989c2010-06-29 10:07:14 +02003353 !(gcwq->flags & GCWQ_FREEZING))
3354 get_cwq(gcwq->cpu, wq)->max_active = max_active;
3355
3356 spin_unlock_irq(&gcwq->lock);
3357 }
3358
3359 spin_unlock(&workqueue_lock);
3360}
3361EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3362
3363/**
3364 * workqueue_congested - test whether a workqueue is congested
3365 * @cpu: CPU in question
3366 * @wq: target workqueue
3367 *
3368 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3369 * no synchronization around this function and the test result is
3370 * unreliable and only useful as advisory hints or for debugging.
3371 *
3372 * RETURNS:
3373 * %true if congested, %false otherwise.
3374 */
3375bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3376{
3377 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3378
3379 return !list_empty(&cwq->delayed_works);
3380}
3381EXPORT_SYMBOL_GPL(workqueue_congested);
3382
3383/**
3384 * work_cpu - return the last known associated cpu for @work
3385 * @work: the work of interest
3386 *
3387 * RETURNS:
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003388 * CPU number if @work was ever queued. WORK_CPU_NONE otherwise.
Tejun Heodcd989c2010-06-29 10:07:14 +02003389 */
3390unsigned int work_cpu(struct work_struct *work)
3391{
3392 struct global_cwq *gcwq = get_work_gcwq(work);
3393
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003394 return gcwq ? gcwq->cpu : WORK_CPU_NONE;
Tejun Heodcd989c2010-06-29 10:07:14 +02003395}
3396EXPORT_SYMBOL_GPL(work_cpu);
3397
3398/**
3399 * work_busy - test whether a work is currently pending or running
3400 * @work: the work to be tested
3401 *
3402 * Test whether @work is currently pending or running. There is no
3403 * synchronization around this function and the test result is
3404 * unreliable and only useful as advisory hints or for debugging.
3405 * Especially for reentrant wqs, the pending state might hide the
3406 * running state.
3407 *
3408 * RETURNS:
3409 * OR'd bitmask of WORK_BUSY_* bits.
3410 */
3411unsigned int work_busy(struct work_struct *work)
3412{
3413 struct global_cwq *gcwq = get_work_gcwq(work);
3414 unsigned long flags;
3415 unsigned int ret = 0;
3416
3417 if (!gcwq)
3418 return false;
3419
3420 spin_lock_irqsave(&gcwq->lock, flags);
3421
3422 if (work_pending(work))
3423 ret |= WORK_BUSY_PENDING;
3424 if (find_worker_executing_work(gcwq, work))
3425 ret |= WORK_BUSY_RUNNING;
3426
3427 spin_unlock_irqrestore(&gcwq->lock, flags);
3428
3429 return ret;
3430}
3431EXPORT_SYMBOL_GPL(work_busy);
3432
Tejun Heodb7bccf2010-06-29 10:07:12 +02003433/*
3434 * CPU hotplug.
3435 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003436 * There are two challenges in supporting CPU hotplug. Firstly, there
3437 * are a lot of assumptions on strong associations among work, cwq and
3438 * gcwq which make migrating pending and scheduled works very
3439 * difficult to implement without impacting hot paths. Secondly,
3440 * gcwqs serve mix of short, long and very long running works making
3441 * blocked draining impractical.
3442 *
Tejun Heo628c78e2012-07-17 12:39:27 -07003443 * This is solved by allowing a gcwq to be disassociated from the CPU
3444 * running as an unbound one and allowing it to be reattached later if the
3445 * cpu comes back online.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003446 */
3447
Tejun Heo60373152012-07-17 12:39:27 -07003448/* claim manager positions of all pools */
Tejun Heo8db25e72012-07-17 12:39:28 -07003449static void gcwq_claim_management_and_lock(struct global_cwq *gcwq)
Tejun Heo60373152012-07-17 12:39:27 -07003450{
3451 struct worker_pool *pool;
3452
3453 for_each_worker_pool(pool, gcwq)
3454 mutex_lock_nested(&pool->manager_mutex, pool - gcwq->pools);
Tejun Heo8db25e72012-07-17 12:39:28 -07003455 spin_lock_irq(&gcwq->lock);
Tejun Heo60373152012-07-17 12:39:27 -07003456}
3457
3458/* release manager positions */
Tejun Heo8db25e72012-07-17 12:39:28 -07003459static void gcwq_release_management_and_unlock(struct global_cwq *gcwq)
Tejun Heo60373152012-07-17 12:39:27 -07003460{
3461 struct worker_pool *pool;
3462
Tejun Heo8db25e72012-07-17 12:39:28 -07003463 spin_unlock_irq(&gcwq->lock);
Tejun Heo60373152012-07-17 12:39:27 -07003464 for_each_worker_pool(pool, gcwq)
3465 mutex_unlock(&pool->manager_mutex);
3466}
3467
Tejun Heo628c78e2012-07-17 12:39:27 -07003468static void gcwq_unbind_fn(struct work_struct *work)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003469{
Tejun Heo628c78e2012-07-17 12:39:27 -07003470 struct global_cwq *gcwq = get_gcwq(smp_processor_id());
Tejun Heo4ce62e92012-07-13 22:16:44 -07003471 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003472 struct worker *worker;
3473 struct hlist_node *pos;
3474 int i;
3475
3476 BUG_ON(gcwq->cpu != smp_processor_id());
3477
Tejun Heo8db25e72012-07-17 12:39:28 -07003478 gcwq_claim_management_and_lock(gcwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02003479
Tejun Heof2d5a0e2012-07-17 12:39:26 -07003480 /*
3481 * We've claimed all manager positions. Make all workers unbound
3482 * and set DISASSOCIATED. Before this, all workers except for the
3483 * ones which are still executing works from before the last CPU
3484 * down must be on the cpu. After this, they may become diasporas.
3485 */
Tejun Heo60373152012-07-17 12:39:27 -07003486 for_each_worker_pool(pool, gcwq)
Tejun Heo4ce62e92012-07-13 22:16:44 -07003487 list_for_each_entry(worker, &pool->idle_list, entry)
Tejun Heo403c8212012-07-17 12:39:27 -07003488 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003489
3490 for_each_busy_worker(worker, i, pos, gcwq)
Tejun Heo403c8212012-07-17 12:39:27 -07003491 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003492
Tejun Heof2d5a0e2012-07-17 12:39:26 -07003493 gcwq->flags |= GCWQ_DISASSOCIATED;
3494
Tejun Heo8db25e72012-07-17 12:39:28 -07003495 gcwq_release_management_and_unlock(gcwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02003496
3497 /*
Tejun Heo628c78e2012-07-17 12:39:27 -07003498 * Call schedule() so that we cross rq->lock and thus can guarantee
3499 * sched callbacks see the %WORKER_UNBOUND flag. This is necessary
3500 * as scheduler callbacks may be invoked from other cpus.
3501 */
3502 schedule();
3503
3504 /*
3505 * Sched callbacks are disabled now. Zap nr_running. After this,
3506 * nr_running stays zero and need_more_worker() and keep_working()
3507 * are always true as long as the worklist is not empty. @gcwq now
3508 * behaves as unbound (in terms of concurrency management) gcwq
3509 * which is served by workers tied to the CPU.
3510 *
3511 * On return from this function, the current worker would trigger
3512 * unbound chain execution of pending work items if other workers
3513 * didn't already.
Tejun Heoe22bee72010-06-29 10:07:14 +02003514 */
Tejun Heo4ce62e92012-07-13 22:16:44 -07003515 for_each_worker_pool(pool, gcwq)
3516 atomic_set(get_pool_nr_running(pool), 0);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003517}
3518
Tejun Heo8db25e72012-07-17 12:39:28 -07003519/*
3520 * Workqueues should be brought up before normal priority CPU notifiers.
3521 * This will be registered high priority CPU notifier.
3522 */
3523static int __devinit workqueue_cpu_up_callback(struct notifier_block *nfb,
3524 unsigned long action,
3525 void *hcpu)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003526{
3527 unsigned int cpu = (unsigned long)hcpu;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003528 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003529 struct worker_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530
Tejun Heo8db25e72012-07-17 12:39:28 -07003531 switch (action & ~CPU_TASKS_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 case CPU_UP_PREPARE:
Tejun Heo4ce62e92012-07-13 22:16:44 -07003533 for_each_worker_pool(pool, gcwq) {
Tejun Heo3ce63372012-07-17 12:39:27 -07003534 struct worker *worker;
3535
3536 if (pool->nr_workers)
3537 continue;
3538
3539 worker = create_worker(pool);
3540 if (!worker)
3541 return NOTIFY_BAD;
3542
3543 spin_lock_irq(&gcwq->lock);
3544 start_worker(worker);
3545 spin_unlock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003547 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003548
Tejun Heo65758202012-07-17 12:39:26 -07003549 case CPU_DOWN_FAILED:
3550 case CPU_ONLINE:
Tejun Heo8db25e72012-07-17 12:39:28 -07003551 gcwq_claim_management_and_lock(gcwq);
3552 gcwq->flags &= ~GCWQ_DISASSOCIATED;
3553 rebind_workers(gcwq);
3554 gcwq_release_management_and_unlock(gcwq);
3555 break;
Tejun Heo65758202012-07-17 12:39:26 -07003556 }
3557 return NOTIFY_OK;
3558}
3559
3560/*
3561 * Workqueues should be brought down after normal priority CPU notifiers.
3562 * This will be registered as low priority CPU notifier.
3563 */
3564static int __devinit workqueue_cpu_down_callback(struct notifier_block *nfb,
3565 unsigned long action,
3566 void *hcpu)
3567{
Tejun Heo8db25e72012-07-17 12:39:28 -07003568 unsigned int cpu = (unsigned long)hcpu;
3569 struct work_struct unbind_work;
3570
Tejun Heo65758202012-07-17 12:39:26 -07003571 switch (action & ~CPU_TASKS_FROZEN) {
3572 case CPU_DOWN_PREPARE:
Tejun Heo8db25e72012-07-17 12:39:28 -07003573 /* unbinding should happen on the local CPU */
3574 INIT_WORK_ONSTACK(&unbind_work, gcwq_unbind_fn);
Joonsoo Kim7635d2f2012-08-15 23:25:41 +09003575 queue_work_on(cpu, system_highpri_wq, &unbind_work);
Tejun Heo8db25e72012-07-17 12:39:28 -07003576 flush_work(&unbind_work);
3577 break;
Tejun Heo65758202012-07-17 12:39:26 -07003578 }
3579 return NOTIFY_OK;
3580}
3581
Rusty Russell2d3854a2008-11-05 13:39:10 +11003582#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003583
Rusty Russell2d3854a2008-11-05 13:39:10 +11003584struct work_for_cpu {
Andrew Morton6b440032009-04-09 09:50:37 -06003585 struct completion completion;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003586 long (*fn)(void *);
3587 void *arg;
3588 long ret;
3589};
3590
Andrew Morton6b440032009-04-09 09:50:37 -06003591static int do_work_for_cpu(void *_wfc)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003592{
Andrew Morton6b440032009-04-09 09:50:37 -06003593 struct work_for_cpu *wfc = _wfc;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003594 wfc->ret = wfc->fn(wfc->arg);
Andrew Morton6b440032009-04-09 09:50:37 -06003595 complete(&wfc->completion);
3596 return 0;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003597}
3598
3599/**
3600 * work_on_cpu - run a function in user context on a particular cpu
3601 * @cpu: the cpu to run on
3602 * @fn: the function to run
3603 * @arg: the function arg
3604 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003605 * This will return the value @fn returns.
3606 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06003607 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003608 */
3609long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3610{
Andrew Morton6b440032009-04-09 09:50:37 -06003611 struct task_struct *sub_thread;
3612 struct work_for_cpu wfc = {
3613 .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
3614 .fn = fn,
3615 .arg = arg,
3616 };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003617
Andrew Morton6b440032009-04-09 09:50:37 -06003618 sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
3619 if (IS_ERR(sub_thread))
3620 return PTR_ERR(sub_thread);
3621 kthread_bind(sub_thread, cpu);
3622 wake_up_process(sub_thread);
3623 wait_for_completion(&wfc.completion);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003624 return wfc.ret;
3625}
3626EXPORT_SYMBOL_GPL(work_on_cpu);
3627#endif /* CONFIG_SMP */
3628
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003629#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303630
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003631/**
3632 * freeze_workqueues_begin - begin freezing workqueues
3633 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003634 * Start freezing workqueues. After this function returns, all freezable
3635 * workqueues will queue new works to their frozen_works list instead of
3636 * gcwq->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003637 *
3638 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003639 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003640 */
3641void freeze_workqueues_begin(void)
3642{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003643 unsigned int cpu;
3644
3645 spin_lock(&workqueue_lock);
3646
3647 BUG_ON(workqueue_freezing);
3648 workqueue_freezing = true;
3649
Tejun Heof3421792010-07-02 10:03:51 +02003650 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003651 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003652 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003653
3654 spin_lock_irq(&gcwq->lock);
3655
Tejun Heodb7bccf2010-06-29 10:07:12 +02003656 BUG_ON(gcwq->flags & GCWQ_FREEZING);
3657 gcwq->flags |= GCWQ_FREEZING;
3658
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003659 list_for_each_entry(wq, &workqueues, list) {
3660 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3661
Tejun Heo58a69cb2011-02-16 09:25:31 +01003662 if (cwq && wq->flags & WQ_FREEZABLE)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003663 cwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003664 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003665
3666 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003667 }
3668
3669 spin_unlock(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003671
3672/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003673 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003674 *
3675 * Check whether freezing is complete. This function must be called
3676 * between freeze_workqueues_begin() and thaw_workqueues().
3677 *
3678 * CONTEXT:
3679 * Grabs and releases workqueue_lock.
3680 *
3681 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01003682 * %true if some freezable workqueues are still busy. %false if freezing
3683 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003684 */
3685bool freeze_workqueues_busy(void)
3686{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003687 unsigned int cpu;
3688 bool busy = false;
3689
3690 spin_lock(&workqueue_lock);
3691
3692 BUG_ON(!workqueue_freezing);
3693
Tejun Heof3421792010-07-02 10:03:51 +02003694 for_each_gcwq_cpu(cpu) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003695 struct workqueue_struct *wq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003696 /*
3697 * nr_active is monotonically decreasing. It's safe
3698 * to peek without lock.
3699 */
3700 list_for_each_entry(wq, &workqueues, list) {
3701 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3702
Tejun Heo58a69cb2011-02-16 09:25:31 +01003703 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003704 continue;
3705
3706 BUG_ON(cwq->nr_active < 0);
3707 if (cwq->nr_active) {
3708 busy = true;
3709 goto out_unlock;
3710 }
3711 }
3712 }
3713out_unlock:
3714 spin_unlock(&workqueue_lock);
3715 return busy;
3716}
3717
3718/**
3719 * thaw_workqueues - thaw workqueues
3720 *
3721 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo7e116292010-06-29 10:07:13 +02003722 * frozen works are transferred to their respective gcwq worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003723 *
3724 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003725 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003726 */
3727void thaw_workqueues(void)
3728{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003729 unsigned int cpu;
3730
3731 spin_lock(&workqueue_lock);
3732
3733 if (!workqueue_freezing)
3734 goto out_unlock;
3735
Tejun Heof3421792010-07-02 10:03:51 +02003736 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003737 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003738 struct worker_pool *pool;
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003739 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003740
3741 spin_lock_irq(&gcwq->lock);
3742
Tejun Heodb7bccf2010-06-29 10:07:12 +02003743 BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
3744 gcwq->flags &= ~GCWQ_FREEZING;
3745
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003746 list_for_each_entry(wq, &workqueues, list) {
3747 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3748
Tejun Heo58a69cb2011-02-16 09:25:31 +01003749 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003750 continue;
3751
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003752 /* restore max_active and repopulate worklist */
3753 cwq->max_active = wq->saved_max_active;
3754
3755 while (!list_empty(&cwq->delayed_works) &&
3756 cwq->nr_active < cwq->max_active)
3757 cwq_activate_first_delayed(cwq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003758 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003759
Tejun Heo4ce62e92012-07-13 22:16:44 -07003760 for_each_worker_pool(pool, gcwq)
3761 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02003762
Tejun Heo8b03ae32010-06-29 10:07:12 +02003763 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003764 }
3765
3766 workqueue_freezing = false;
3767out_unlock:
3768 spin_unlock(&workqueue_lock);
3769}
3770#endif /* CONFIG_FREEZER */
3771
Suresh Siddha6ee05782010-07-30 14:57:37 -07003772static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773{
Tejun Heoc34056a2010-06-29 10:07:11 +02003774 unsigned int cpu;
Tejun Heoc8e55f32010-06-29 10:07:12 +02003775 int i;
Tejun Heoc34056a2010-06-29 10:07:11 +02003776
Tejun Heob5490072012-08-03 10:30:46 -07003777 /* make sure we have enough bits for OFFQ CPU number */
3778 BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_CPU_SHIFT)) <
3779 WORK_CPU_LAST);
3780
Tejun Heo65758202012-07-17 12:39:26 -07003781 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
3782 cpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003783
3784 /* initialize gcwqs */
Tejun Heof3421792010-07-02 10:03:51 +02003785 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003786 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003787 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003788
3789 spin_lock_init(&gcwq->lock);
3790 gcwq->cpu = cpu;
Tejun Heo477a3c32010-08-31 10:54:35 +02003791 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003792
Tejun Heoc8e55f32010-06-29 10:07:12 +02003793 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
3794 INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
3795
Tejun Heo4ce62e92012-07-13 22:16:44 -07003796 for_each_worker_pool(pool, gcwq) {
3797 pool->gcwq = gcwq;
3798 INIT_LIST_HEAD(&pool->worklist);
3799 INIT_LIST_HEAD(&pool->idle_list);
Tejun Heoe22bee72010-06-29 10:07:14 +02003800
Tejun Heo4ce62e92012-07-13 22:16:44 -07003801 init_timer_deferrable(&pool->idle_timer);
3802 pool->idle_timer.function = idle_worker_timeout;
3803 pool->idle_timer.data = (unsigned long)pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003804
Tejun Heo4ce62e92012-07-13 22:16:44 -07003805 setup_timer(&pool->mayday_timer, gcwq_mayday_timeout,
3806 (unsigned long)pool);
3807
Tejun Heo60373152012-07-17 12:39:27 -07003808 mutex_init(&pool->manager_mutex);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003809 ida_init(&pool->worker_ida);
3810 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003811
Tejun Heo25511a42012-07-17 12:39:27 -07003812 init_waitqueue_head(&gcwq->rebind_hold);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003813 }
3814
Tejun Heoe22bee72010-06-29 10:07:14 +02003815 /* create the initial worker */
Tejun Heof3421792010-07-02 10:03:51 +02003816 for_each_online_gcwq_cpu(cpu) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003817 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003818 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003819
Tejun Heo477a3c32010-08-31 10:54:35 +02003820 if (cpu != WORK_CPU_UNBOUND)
3821 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003822
3823 for_each_worker_pool(pool, gcwq) {
3824 struct worker *worker;
3825
Tejun Heobc2ae0f2012-07-17 12:39:27 -07003826 worker = create_worker(pool);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003827 BUG_ON(!worker);
3828 spin_lock_irq(&gcwq->lock);
3829 start_worker(worker);
3830 spin_unlock_irq(&gcwq->lock);
3831 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003832 }
3833
Tejun Heod320c032010-06-29 10:07:14 +02003834 system_wq = alloc_workqueue("events", 0, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09003835 system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
Tejun Heod320c032010-06-29 10:07:14 +02003836 system_long_wq = alloc_workqueue("events_long", 0, 0);
Tejun Heof3421792010-07-02 10:03:51 +02003837 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3838 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01003839 system_freezable_wq = alloc_workqueue("events_freezable",
3840 WQ_FREEZABLE, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09003841 BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
Tejun Heoae930e02012-08-20 14:51:23 -07003842 !system_unbound_wq || !system_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07003843 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844}
Suresh Siddha6ee05782010-07-30 14:57:37 -07003845early_initcall(init_workqueues);