blob: 039d0fae171a3d462e4f4a119761b3350112bc30 [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
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001051 * interrupted while holding PENDING and @work off queue, irq must be
1052 * disabled on entry. This, combined with delayed_work->timer being
1053 * irqsafe, ensures that we return -EAGAIN for finite short period of time.
Tejun Heobbb68df2012-08-03 10:30:46 -07001054 *
1055 * On successful return, >= 0, irq is disabled and the caller is
1056 * responsible for releasing it using local_irq_restore(*@flags).
1057 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001058 * This function is safe to call from any context including IRQ handler.
Tejun Heobf4ede02012-08-03 10:30:46 -07001059 */
Tejun Heobbb68df2012-08-03 10:30:46 -07001060static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
1061 unsigned long *flags)
Tejun Heobf4ede02012-08-03 10:30:46 -07001062{
1063 struct global_cwq *gcwq;
Tejun Heobf4ede02012-08-03 10:30:46 -07001064
Tejun Heobbb68df2012-08-03 10:30:46 -07001065 WARN_ON_ONCE(in_irq());
1066
1067 local_irq_save(*flags);
1068
Tejun Heo36e227d2012-08-03 10:30:46 -07001069 /* try to steal the timer if it exists */
1070 if (is_dwork) {
1071 struct delayed_work *dwork = to_delayed_work(work);
1072
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001073 /*
1074 * dwork->timer is irqsafe. If del_timer() fails, it's
1075 * guaranteed that the timer is not queued anywhere and not
1076 * running on the local CPU.
1077 */
Tejun Heo36e227d2012-08-03 10:30:46 -07001078 if (likely(del_timer(&dwork->timer)))
1079 return 1;
1080 }
1081
1082 /* try to claim PENDING the normal way */
Tejun Heobf4ede02012-08-03 10:30:46 -07001083 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1084 return 0;
1085
1086 /*
1087 * The queueing is in progress, or it is already queued. Try to
1088 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1089 */
1090 gcwq = get_work_gcwq(work);
1091 if (!gcwq)
Tejun Heobbb68df2012-08-03 10:30:46 -07001092 goto fail;
Tejun Heobf4ede02012-08-03 10:30:46 -07001093
Tejun Heobbb68df2012-08-03 10:30:46 -07001094 spin_lock(&gcwq->lock);
Tejun Heobf4ede02012-08-03 10:30:46 -07001095 if (!list_empty(&work->entry)) {
1096 /*
1097 * This work is queued, but perhaps we locked the wrong gcwq.
1098 * In that case we must see the new value after rmb(), see
1099 * insert_work()->wmb().
1100 */
1101 smp_rmb();
1102 if (gcwq == get_work_gcwq(work)) {
1103 debug_work_deactivate(work);
1104 list_del_init(&work->entry);
1105 cwq_dec_nr_in_flight(get_work_cwq(work),
1106 get_work_color(work),
1107 *work_data_bits(work) & WORK_STRUCT_DELAYED);
Tejun Heo36e227d2012-08-03 10:30:46 -07001108
Tejun Heobbb68df2012-08-03 10:30:46 -07001109 spin_unlock(&gcwq->lock);
Tejun Heo36e227d2012-08-03 10:30:46 -07001110 return 1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001111 }
1112 }
Tejun Heobbb68df2012-08-03 10:30:46 -07001113 spin_unlock(&gcwq->lock);
1114fail:
1115 local_irq_restore(*flags);
1116 if (work_is_canceling(work))
1117 return -ENOENT;
1118 cpu_relax();
Tejun Heo36e227d2012-08-03 10:30:46 -07001119 return -EAGAIN;
Tejun Heobf4ede02012-08-03 10:30:46 -07001120}
1121
1122/**
Tejun Heo7e116292010-06-29 10:07:13 +02001123 * insert_work - insert a work into gcwq
Tejun Heo4690c4a2010-06-29 10:07:10 +02001124 * @cwq: cwq @work belongs to
1125 * @work: work to insert
1126 * @head: insertion point
1127 * @extra_flags: extra WORK_STRUCT_* flags to set
1128 *
Tejun Heo7e116292010-06-29 10:07:13 +02001129 * Insert @work which belongs to @cwq into @gcwq after @head.
1130 * @extra_flags is or'd to work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +02001131 *
1132 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001133 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02001134 */
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001135static void insert_work(struct cpu_workqueue_struct *cwq,
Tejun Heo4690c4a2010-06-29 10:07:10 +02001136 struct work_struct *work, struct list_head *head,
1137 unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001138{
Tejun Heo63d95a92012-07-12 14:46:37 -07001139 struct worker_pool *pool = cwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01001140
Tejun Heo4690c4a2010-06-29 10:07:10 +02001141 /* we own @work, set data and link */
Tejun Heo7a22ad72010-06-29 10:07:13 +02001142 set_work_cwq(work, cwq, extra_flags);
Tejun Heo4690c4a2010-06-29 10:07:10 +02001143
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001144 /*
1145 * Ensure that we get the right work->data if we see the
1146 * result of list_add() below, see try_to_grab_pending().
1147 */
1148 smp_wmb();
Tejun Heo4690c4a2010-06-29 10:07:10 +02001149
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -07001150 list_add_tail(&work->entry, head);
Tejun Heoe22bee72010-06-29 10:07:14 +02001151
1152 /*
1153 * Ensure either worker_sched_deactivated() sees the above
1154 * list_add_tail() or we see zero nr_running to avoid workers
1155 * lying around lazily while there are works to be processed.
1156 */
1157 smp_mb();
1158
Tejun Heo63d95a92012-07-12 14:46:37 -07001159 if (__need_more_worker(pool))
1160 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001161}
1162
Tejun Heoc8efcc22010-12-20 19:32:04 +01001163/*
1164 * Test whether @work is being queued from another work executing on the
1165 * same workqueue. This is rather expensive and should only be used from
1166 * cold paths.
1167 */
1168static bool is_chained_work(struct workqueue_struct *wq)
1169{
1170 unsigned long flags;
1171 unsigned int cpu;
1172
1173 for_each_gcwq_cpu(cpu) {
1174 struct global_cwq *gcwq = get_gcwq(cpu);
1175 struct worker *worker;
1176 struct hlist_node *pos;
1177 int i;
1178
1179 spin_lock_irqsave(&gcwq->lock, flags);
1180 for_each_busy_worker(worker, i, pos, gcwq) {
1181 if (worker->task != current)
1182 continue;
1183 spin_unlock_irqrestore(&gcwq->lock, flags);
1184 /*
1185 * I'm @worker, no locking necessary. See if @work
1186 * is headed to the same workqueue.
1187 */
1188 return worker->current_cwq->wq == wq;
1189 }
1190 spin_unlock_irqrestore(&gcwq->lock, flags);
1191 }
1192 return false;
1193}
1194
Tejun Heo4690c4a2010-06-29 10:07:10 +02001195static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 struct work_struct *work)
1197{
Tejun Heo502ca9d2010-06-29 10:07:13 +02001198 struct global_cwq *gcwq;
1199 struct cpu_workqueue_struct *cwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001200 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001201 unsigned int work_flags;
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001202 unsigned int req_cpu = cpu;
Tejun Heo8930cab2012-08-03 10:30:45 -07001203
1204 /*
1205 * While a work item is PENDING && off queue, a task trying to
1206 * steal the PENDING will busy-loop waiting for it to either get
1207 * queued or lose PENDING. Grabbing PENDING and queueing should
1208 * happen with IRQ disabled.
1209 */
1210 WARN_ON_ONCE(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001212 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001213
Tejun Heoc8efcc22010-12-20 19:32:04 +01001214 /* if dying, only works from the same workqueue are allowed */
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02001215 if (unlikely(wq->flags & WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001216 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001217 return;
1218
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001219 /* determine gcwq to use */
1220 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001221 struct global_cwq *last_gcwq;
1222
Tejun Heo57469822012-08-03 10:30:45 -07001223 if (cpu == WORK_CPU_UNBOUND)
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001224 cpu = raw_smp_processor_id();
1225
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001226 /*
Tejun Heodbf25762012-08-20 14:51:23 -07001227 * It's multi cpu. If @work was previously on a different
1228 * cpu, it might still be running there, in which case the
1229 * work needs to be queued on that cpu to guarantee
1230 * non-reentrancy.
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001231 */
Tejun Heo502ca9d2010-06-29 10:07:13 +02001232 gcwq = get_gcwq(cpu);
Tejun Heodbf25762012-08-20 14:51:23 -07001233 last_gcwq = get_work_gcwq(work);
1234
1235 if (last_gcwq && last_gcwq != gcwq) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001236 struct worker *worker;
1237
Tejun Heo8930cab2012-08-03 10:30:45 -07001238 spin_lock(&last_gcwq->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001239
1240 worker = find_worker_executing_work(last_gcwq, work);
1241
1242 if (worker && worker->current_cwq->wq == wq)
1243 gcwq = last_gcwq;
1244 else {
1245 /* meh... not running there, queue here */
Tejun Heo8930cab2012-08-03 10:30:45 -07001246 spin_unlock(&last_gcwq->lock);
1247 spin_lock(&gcwq->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001248 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001249 } else {
1250 spin_lock(&gcwq->lock);
1251 }
Tejun Heof3421792010-07-02 10:03:51 +02001252 } else {
1253 gcwq = get_gcwq(WORK_CPU_UNBOUND);
Tejun Heo8930cab2012-08-03 10:30:45 -07001254 spin_lock(&gcwq->lock);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001255 }
1256
1257 /* gcwq determined, get cwq and queue */
1258 cwq = get_cwq(gcwq->cpu, wq);
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001259 trace_workqueue_queue_work(req_cpu, cwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001260
Dan Carpenterf5b25522012-04-13 22:06:58 +03001261 if (WARN_ON(!list_empty(&work->entry))) {
Tejun Heo8930cab2012-08-03 10:30:45 -07001262 spin_unlock(&gcwq->lock);
Dan Carpenterf5b25522012-04-13 22:06:58 +03001263 return;
1264 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001265
Tejun Heo73f53c42010-06-29 10:07:11 +02001266 cwq->nr_in_flight[cwq->work_color]++;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001267 work_flags = work_color_to_flags(cwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001268
1269 if (likely(cwq->nr_active < cwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001270 trace_workqueue_activate_work(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001271 cwq->nr_active++;
Tejun Heo32704762012-07-13 22:16:45 -07001272 worklist = &cwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001273 } else {
1274 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001275 worklist = &cwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001276 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001277
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001278 insert_work(cwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001279
Tejun Heo8930cab2012-08-03 10:30:45 -07001280 spin_unlock(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
1282
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001283/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07001284 * queue_work_on - queue work on specific cpu
1285 * @cpu: CPU number to execute work on
1286 * @wq: workqueue to use
1287 * @work: work to queue
1288 *
Tejun Heod4283e92012-08-03 10:30:44 -07001289 * Returns %false if @work was already on a queue, %true otherwise.
Zhang Ruic1a220e2008-07-23 21:28:39 -07001290 *
1291 * We queue the work to a specific CPU, the caller must ensure it
1292 * can't go away.
1293 */
Tejun Heod4283e92012-08-03 10:30:44 -07001294bool queue_work_on(int cpu, struct workqueue_struct *wq,
1295 struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07001296{
Tejun Heod4283e92012-08-03 10:30:44 -07001297 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001298 unsigned long flags;
1299
1300 local_irq_save(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001301
Tejun Heo22df02b2010-06-29 10:07:10 +02001302 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001303 __queue_work(cpu, wq, work);
Tejun Heod4283e92012-08-03 10:30:44 -07001304 ret = true;
Zhang Ruic1a220e2008-07-23 21:28:39 -07001305 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001306
1307 local_irq_restore(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001308 return ret;
1309}
1310EXPORT_SYMBOL_GPL(queue_work_on);
1311
Tejun Heo0a13c002012-08-03 10:30:44 -07001312/**
1313 * queue_work - queue work on a workqueue
1314 * @wq: workqueue to use
1315 * @work: work to queue
1316 *
Tejun Heod4283e92012-08-03 10:30:44 -07001317 * Returns %false if @work was already on a queue, %true otherwise.
Tejun Heo0a13c002012-08-03 10:30:44 -07001318 *
1319 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1320 * it can be processed by another CPU.
1321 */
Tejun Heod4283e92012-08-03 10:30:44 -07001322bool queue_work(struct workqueue_struct *wq, struct work_struct *work)
Tejun Heo0a13c002012-08-03 10:30:44 -07001323{
Tejun Heo57469822012-08-03 10:30:45 -07001324 return queue_work_on(WORK_CPU_UNBOUND, wq, work);
Tejun Heo0a13c002012-08-03 10:30:44 -07001325}
1326EXPORT_SYMBOL_GPL(queue_work);
1327
Tejun Heod8e794d2012-08-03 10:30:45 -07001328void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
David Howells52bad642006-11-22 14:54:01 +00001330 struct delayed_work *dwork = (struct delayed_work *)__data;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001331 struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001333 /* should have been called from irqsafe timer with irq already off */
Tejun Heo12650572012-08-08 09:38:42 -07001334 __queue_work(dwork->cpu, cwq->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
Tejun Heod8e794d2012-08-03 10:30:45 -07001336EXPORT_SYMBOL_GPL(delayed_work_timer_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001338static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
1339 struct delayed_work *dwork, unsigned long delay)
1340{
1341 struct timer_list *timer = &dwork->timer;
1342 struct work_struct *work = &dwork->work;
1343 unsigned int lcpu;
1344
1345 WARN_ON_ONCE(timer->function != delayed_work_timer_fn ||
1346 timer->data != (unsigned long)dwork);
1347 BUG_ON(timer_pending(timer));
1348 BUG_ON(!list_empty(&work->entry));
1349
1350 timer_stats_timer_set_start_info(&dwork->timer);
1351
1352 /*
1353 * This stores cwq for the moment, for the timer_fn. Note that the
1354 * work's gcwq is preserved to allow reentrance detection for
1355 * delayed works.
1356 */
1357 if (!(wq->flags & WQ_UNBOUND)) {
1358 struct global_cwq *gcwq = get_work_gcwq(work);
1359
Joonsoo Kime42986d2012-08-15 23:25:38 +09001360 /*
1361 * If we cannot get the last gcwq from @work directly,
1362 * select the last CPU such that it avoids unnecessarily
1363 * triggering non-reentrancy check in __queue_work().
1364 */
1365 lcpu = cpu;
1366 if (gcwq)
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001367 lcpu = gcwq->cpu;
Joonsoo Kime42986d2012-08-15 23:25:38 +09001368 if (lcpu == WORK_CPU_UNBOUND)
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001369 lcpu = raw_smp_processor_id();
1370 } else {
1371 lcpu = WORK_CPU_UNBOUND;
1372 }
1373
1374 set_work_cwq(work, get_cwq(lcpu, wq), 0);
1375
Tejun Heo12650572012-08-08 09:38:42 -07001376 dwork->cpu = cpu;
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001377 timer->expires = jiffies + delay;
1378
1379 if (unlikely(cpu != WORK_CPU_UNBOUND))
1380 add_timer_on(timer, cpu);
1381 else
1382 add_timer(timer);
1383}
1384
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001385/**
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001386 * queue_delayed_work_on - queue work on specific CPU after delay
1387 * @cpu: CPU number to execute work on
1388 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001389 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001390 * @delay: number of jiffies to wait before queueing
1391 *
Tejun Heo715f1302012-08-03 10:30:46 -07001392 * Returns %false if @work was already on a queue, %true otherwise. If
1393 * @delay is zero and @dwork is idle, it will be scheduled for immediate
1394 * execution.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001395 */
Tejun Heod4283e92012-08-03 10:30:44 -07001396bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
1397 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001398{
David Howells52bad642006-11-22 14:54:01 +00001399 struct work_struct *work = &dwork->work;
Tejun Heod4283e92012-08-03 10:30:44 -07001400 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001401 unsigned long flags;
1402
Tejun Heo715f1302012-08-03 10:30:46 -07001403 if (!delay)
1404 return queue_work_on(cpu, wq, &dwork->work);
1405
Tejun Heo8930cab2012-08-03 10:30:45 -07001406 /* read the comment in __queue_work() */
1407 local_irq_save(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001408
Tejun Heo22df02b2010-06-29 10:07:10 +02001409 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001410 __queue_delayed_work(cpu, wq, dwork, delay);
Tejun Heod4283e92012-08-03 10:30:44 -07001411 ret = true;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001412 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001413
1414 local_irq_restore(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001415 return ret;
1416}
Dave Jonesae90dd52006-06-30 01:40:45 -04001417EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Tejun Heoc8e55f32010-06-29 10:07:12 +02001419/**
Tejun Heo0a13c002012-08-03 10:30:44 -07001420 * queue_delayed_work - queue work on a workqueue after delay
1421 * @wq: workqueue to use
1422 * @dwork: delayable work to queue
1423 * @delay: number of jiffies to wait before queueing
1424 *
Tejun Heo715f1302012-08-03 10:30:46 -07001425 * Equivalent to queue_delayed_work_on() but tries to use the local CPU.
Tejun Heo0a13c002012-08-03 10:30:44 -07001426 */
Tejun Heod4283e92012-08-03 10:30:44 -07001427bool queue_delayed_work(struct workqueue_struct *wq,
Tejun Heo0a13c002012-08-03 10:30:44 -07001428 struct delayed_work *dwork, unsigned long delay)
1429{
Tejun Heo57469822012-08-03 10:30:45 -07001430 return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
Tejun Heo0a13c002012-08-03 10:30:44 -07001431}
1432EXPORT_SYMBOL_GPL(queue_delayed_work);
1433
1434/**
Tejun Heo8376fe22012-08-03 10:30:47 -07001435 * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
1436 * @cpu: CPU number to execute work on
1437 * @wq: workqueue to use
1438 * @dwork: work to queue
1439 * @delay: number of jiffies to wait before queueing
1440 *
1441 * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
1442 * modify @dwork's timer so that it expires after @delay. If @delay is
1443 * zero, @work is guaranteed to be scheduled immediately regardless of its
1444 * current state.
1445 *
1446 * Returns %false if @dwork was idle and queued, %true if @dwork was
1447 * pending and its timer was modified.
1448 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001449 * This function is safe to call from any context including IRQ handler.
Tejun Heo8376fe22012-08-03 10:30:47 -07001450 * See try_to_grab_pending() for details.
1451 */
1452bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
1453 struct delayed_work *dwork, unsigned long delay)
1454{
1455 unsigned long flags;
1456 int ret;
1457
1458 do {
1459 ret = try_to_grab_pending(&dwork->work, true, &flags);
1460 } while (unlikely(ret == -EAGAIN));
1461
1462 if (likely(ret >= 0)) {
1463 __queue_delayed_work(cpu, wq, dwork, delay);
1464 local_irq_restore(flags);
1465 }
1466
1467 /* -ENOENT from try_to_grab_pending() becomes %true */
1468 return ret;
1469}
1470EXPORT_SYMBOL_GPL(mod_delayed_work_on);
1471
1472/**
1473 * mod_delayed_work - modify delay of or queue a delayed work
1474 * @wq: workqueue to use
1475 * @dwork: work to queue
1476 * @delay: number of jiffies to wait before queueing
1477 *
1478 * mod_delayed_work_on() on local CPU.
1479 */
1480bool mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dwork,
1481 unsigned long delay)
1482{
1483 return mod_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
1484}
1485EXPORT_SYMBOL_GPL(mod_delayed_work);
1486
1487/**
Tejun Heoc8e55f32010-06-29 10:07:12 +02001488 * worker_enter_idle - enter idle state
1489 * @worker: worker which is entering idle state
1490 *
1491 * @worker is entering idle state. Update stats and idle timer if
1492 * necessary.
1493 *
1494 * LOCKING:
1495 * spin_lock_irq(gcwq->lock).
1496 */
1497static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001499 struct worker_pool *pool = worker->pool;
1500 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Tejun Heoc8e55f32010-06-29 10:07:12 +02001502 BUG_ON(worker->flags & WORKER_IDLE);
1503 BUG_ON(!list_empty(&worker->entry) &&
1504 (worker->hentry.next || worker->hentry.pprev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Tejun Heocb444762010-07-02 10:03:50 +02001506 /* can't use worker_set_flags(), also called from start_worker() */
1507 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001508 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001509 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001510
Tejun Heoc8e55f32010-06-29 10:07:12 +02001511 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001512 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001513
Tejun Heo628c78e2012-07-17 12:39:27 -07001514 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1515 mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
Tejun Heocb444762010-07-02 10:03:50 +02001516
Tejun Heo544ecf32012-05-14 15:04:50 -07001517 /*
Tejun Heo628c78e2012-07-17 12:39:27 -07001518 * Sanity check nr_running. Because gcwq_unbind_fn() releases
1519 * gcwq->lock between setting %WORKER_UNBOUND and zapping
1520 * nr_running, the warning may trigger spuriously. Check iff
1521 * unbind is not in progress.
Tejun Heo544ecf32012-05-14 15:04:50 -07001522 */
Tejun Heo628c78e2012-07-17 12:39:27 -07001523 WARN_ON_ONCE(!(gcwq->flags & GCWQ_DISASSOCIATED) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001524 pool->nr_workers == pool->nr_idle &&
Tejun Heo63d95a92012-07-12 14:46:37 -07001525 atomic_read(get_pool_nr_running(pool)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526}
1527
Tejun Heoc8e55f32010-06-29 10:07:12 +02001528/**
1529 * worker_leave_idle - leave idle state
1530 * @worker: worker which is leaving idle state
1531 *
1532 * @worker is leaving idle state. Update stats.
1533 *
1534 * LOCKING:
1535 * spin_lock_irq(gcwq->lock).
1536 */
1537static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001539 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Tejun Heoc8e55f32010-06-29 10:07:12 +02001541 BUG_ON(!(worker->flags & WORKER_IDLE));
Tejun Heod302f012010-06-29 10:07:13 +02001542 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001543 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001544 list_del_init(&worker->entry);
1545}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Tejun Heoe22bee72010-06-29 10:07:14 +02001547/**
1548 * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq
1549 * @worker: self
1550 *
1551 * Works which are scheduled while the cpu is online must at least be
1552 * scheduled to a worker which is bound to the cpu so that if they are
1553 * flushed from cpu callbacks while cpu is going down, they are
1554 * guaranteed to execute on the cpu.
1555 *
1556 * This function is to be used by rogue workers and rescuers to bind
1557 * themselves to the target cpu and may race with cpu going down or
1558 * coming online. kthread_bind() can't be used because it may put the
1559 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1560 * verbatim as it's best effort and blocking and gcwq may be
1561 * [dis]associated in the meantime.
1562 *
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001563 * This function tries set_cpus_allowed() and locks gcwq and verifies the
1564 * binding against %GCWQ_DISASSOCIATED which is set during
1565 * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1566 * enters idle state or fetches works without dropping lock, it can
1567 * guarantee the scheduling requirement described in the first paragraph.
Tejun Heoe22bee72010-06-29 10:07:14 +02001568 *
1569 * CONTEXT:
1570 * Might sleep. Called without any lock but returns with gcwq->lock
1571 * held.
1572 *
1573 * RETURNS:
1574 * %true if the associated gcwq is online (@worker is successfully
1575 * bound), %false if offline.
1576 */
1577static bool worker_maybe_bind_and_lock(struct worker *worker)
Namhyung Kim972fa1c2010-08-22 23:19:43 +09001578__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001579{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001580 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001581 struct task_struct *task = worker->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Tejun Heoe22bee72010-06-29 10:07:14 +02001583 while (true) {
1584 /*
1585 * The following call may fail, succeed or succeed
1586 * without actually migrating the task to the cpu if
1587 * it races with cpu hotunplug operation. Verify
1588 * against GCWQ_DISASSOCIATED.
1589 */
Tejun Heof3421792010-07-02 10:03:51 +02001590 if (!(gcwq->flags & GCWQ_DISASSOCIATED))
1591 set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu));
Oleg Nesterov85f41862007-05-09 02:34:20 -07001592
Tejun Heoe22bee72010-06-29 10:07:14 +02001593 spin_lock_irq(&gcwq->lock);
1594 if (gcwq->flags & GCWQ_DISASSOCIATED)
1595 return false;
1596 if (task_cpu(task) == gcwq->cpu &&
1597 cpumask_equal(&current->cpus_allowed,
1598 get_cpu_mask(gcwq->cpu)))
1599 return true;
1600 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001601
Tejun Heo5035b202011-04-29 18:08:37 +02001602 /*
1603 * We've raced with CPU hot[un]plug. Give it a breather
1604 * and retry migration. cond_resched() is required here;
1605 * otherwise, we might deadlock against cpu_stop trying to
1606 * bring down the CPU on non-preemptive kernel.
1607 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001608 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001609 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001610 }
1611}
1612
Tejun Heo25511a42012-07-17 12:39:27 -07001613struct idle_rebind {
1614 int cnt; /* # workers to be rebound */
1615 struct completion done; /* all workers rebound */
1616};
1617
Tejun Heoe22bee72010-06-29 10:07:14 +02001618/*
Tejun Heo25511a42012-07-17 12:39:27 -07001619 * Rebind an idle @worker to its CPU. During CPU onlining, this has to
1620 * happen synchronously for idle workers. worker_thread() will test
1621 * %WORKER_REBIND before leaving idle and call this function.
1622 */
1623static void idle_worker_rebind(struct worker *worker)
1624{
1625 struct global_cwq *gcwq = worker->pool->gcwq;
1626
1627 /* CPU must be online at this point */
1628 WARN_ON(!worker_maybe_bind_and_lock(worker));
1629 if (!--worker->idle_rebind->cnt)
1630 complete(&worker->idle_rebind->done);
1631 spin_unlock_irq(&worker->pool->gcwq->lock);
1632
1633 /* we did our part, wait for rebind_workers() to finish up */
1634 wait_event(gcwq->rebind_hold, !(worker->flags & WORKER_REBIND));
1635}
1636
1637/*
1638 * Function for @worker->rebind.work used to rebind unbound busy workers to
Tejun Heo403c8212012-07-17 12:39:27 -07001639 * the associated cpu which is coming back online. This is scheduled by
1640 * cpu up but can race with other cpu hotplug operations and may be
1641 * executed twice without intervening cpu down.
Tejun Heoe22bee72010-06-29 10:07:14 +02001642 */
Tejun Heo25511a42012-07-17 12:39:27 -07001643static void busy_worker_rebind_fn(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001644{
1645 struct worker *worker = container_of(work, struct worker, rebind_work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001646 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001647
1648 if (worker_maybe_bind_and_lock(worker))
1649 worker_clr_flags(worker, WORKER_REBIND);
1650
1651 spin_unlock_irq(&gcwq->lock);
1652}
1653
Tejun Heo25511a42012-07-17 12:39:27 -07001654/**
1655 * rebind_workers - rebind all workers of a gcwq to the associated CPU
1656 * @gcwq: gcwq of interest
1657 *
1658 * @gcwq->cpu is coming online. Rebind all workers to the CPU. Rebinding
1659 * is different for idle and busy ones.
1660 *
1661 * The idle ones should be rebound synchronously and idle rebinding should
1662 * be complete before any worker starts executing work items with
1663 * concurrency management enabled; otherwise, scheduler may oops trying to
1664 * wake up non-local idle worker from wq_worker_sleeping().
1665 *
1666 * This is achieved by repeatedly requesting rebinding until all idle
1667 * workers are known to have been rebound under @gcwq->lock and holding all
1668 * idle workers from becoming busy until idle rebinding is complete.
1669 *
1670 * Once idle workers are rebound, busy workers can be rebound as they
1671 * finish executing their current work items. Queueing the rebind work at
1672 * the head of their scheduled lists is enough. Note that nr_running will
1673 * be properbly bumped as busy workers rebind.
1674 *
1675 * On return, all workers are guaranteed to either be bound or have rebind
1676 * work item scheduled.
1677 */
1678static void rebind_workers(struct global_cwq *gcwq)
1679 __releases(&gcwq->lock) __acquires(&gcwq->lock)
1680{
1681 struct idle_rebind idle_rebind;
1682 struct worker_pool *pool;
1683 struct worker *worker;
1684 struct hlist_node *pos;
1685 int i;
1686
1687 lockdep_assert_held(&gcwq->lock);
1688
1689 for_each_worker_pool(pool, gcwq)
1690 lockdep_assert_held(&pool->manager_mutex);
1691
1692 /*
1693 * Rebind idle workers. Interlocked both ways. We wait for
1694 * workers to rebind via @idle_rebind.done. Workers will wait for
1695 * us to finish up by watching %WORKER_REBIND.
1696 */
1697 init_completion(&idle_rebind.done);
1698retry:
1699 idle_rebind.cnt = 1;
1700 INIT_COMPLETION(idle_rebind.done);
1701
1702 /* set REBIND and kick idle ones, we'll wait for these later */
1703 for_each_worker_pool(pool, gcwq) {
1704 list_for_each_entry(worker, &pool->idle_list, entry) {
1705 if (worker->flags & WORKER_REBIND)
1706 continue;
1707
1708 /* morph UNBOUND to REBIND */
1709 worker->flags &= ~WORKER_UNBOUND;
1710 worker->flags |= WORKER_REBIND;
1711
1712 idle_rebind.cnt++;
1713 worker->idle_rebind = &idle_rebind;
1714
1715 /* worker_thread() will call idle_worker_rebind() */
1716 wake_up_process(worker->task);
1717 }
1718 }
1719
1720 if (--idle_rebind.cnt) {
1721 spin_unlock_irq(&gcwq->lock);
1722 wait_for_completion(&idle_rebind.done);
1723 spin_lock_irq(&gcwq->lock);
1724 /* busy ones might have become idle while waiting, retry */
1725 goto retry;
1726 }
1727
1728 /*
1729 * All idle workers are rebound and waiting for %WORKER_REBIND to
1730 * be cleared inside idle_worker_rebind(). Clear and release.
1731 * Clearing %WORKER_REBIND from this foreign context is safe
1732 * because these workers are still guaranteed to be idle.
1733 */
1734 for_each_worker_pool(pool, gcwq)
1735 list_for_each_entry(worker, &pool->idle_list, entry)
1736 worker->flags &= ~WORKER_REBIND;
1737
1738 wake_up_all(&gcwq->rebind_hold);
1739
1740 /* rebind busy workers */
1741 for_each_busy_worker(worker, i, pos, gcwq) {
1742 struct work_struct *rebind_work = &worker->rebind_work;
Joonsoo Kime2b6a6d2012-08-15 23:25:40 +09001743 struct workqueue_struct *wq;
Tejun Heo25511a42012-07-17 12:39:27 -07001744
1745 /* morph UNBOUND to REBIND */
1746 worker->flags &= ~WORKER_UNBOUND;
1747 worker->flags |= WORKER_REBIND;
1748
1749 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
1750 work_data_bits(rebind_work)))
1751 continue;
1752
Tejun Heo25511a42012-07-17 12:39:27 -07001753 debug_work_activate(rebind_work);
Joonsoo Kime2b6a6d2012-08-15 23:25:40 +09001754
1755 /*
1756 * wq doesn't really matter but let's keep @worker->pool
1757 * and @cwq->pool consistent for sanity.
1758 */
1759 if (worker_pool_pri(worker->pool))
1760 wq = system_highpri_wq;
1761 else
1762 wq = system_wq;
1763
1764 insert_work(get_cwq(gcwq->cpu, wq), rebind_work,
1765 worker->scheduled.next,
1766 work_color_to_flags(WORK_NO_COLOR));
Tejun Heo25511a42012-07-17 12:39:27 -07001767 }
1768}
1769
Tejun Heoc34056a2010-06-29 10:07:11 +02001770static struct worker *alloc_worker(void)
1771{
1772 struct worker *worker;
1773
1774 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001775 if (worker) {
1776 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001777 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heo25511a42012-07-17 12:39:27 -07001778 INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn);
Tejun Heoe22bee72010-06-29 10:07:14 +02001779 /* on creation a worker is in !idle && prep state */
1780 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001781 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001782 return worker;
1783}
1784
1785/**
1786 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001787 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001788 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001789 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001790 * can be started by calling start_worker() or destroyed using
1791 * destroy_worker().
1792 *
1793 * CONTEXT:
1794 * Might sleep. Does GFP_KERNEL allocations.
1795 *
1796 * RETURNS:
1797 * Pointer to the newly created worker.
1798 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001799static struct worker *create_worker(struct worker_pool *pool)
Tejun Heoc34056a2010-06-29 10:07:11 +02001800{
Tejun Heo63d95a92012-07-12 14:46:37 -07001801 struct global_cwq *gcwq = pool->gcwq;
Tejun Heo32704762012-07-13 22:16:45 -07001802 const char *pri = worker_pool_pri(pool) ? "H" : "";
Tejun Heoc34056a2010-06-29 10:07:11 +02001803 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001804 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001805
Tejun Heo8b03ae32010-06-29 10:07:12 +02001806 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001807 while (ida_get_new(&pool->worker_ida, &id)) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001808 spin_unlock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001809 if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001810 goto fail;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001811 spin_lock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001812 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02001813 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001814
1815 worker = alloc_worker();
1816 if (!worker)
1817 goto fail;
1818
Tejun Heobd7bdd42012-07-12 14:46:37 -07001819 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001820 worker->id = id;
1821
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001822 if (gcwq->cpu != WORK_CPU_UNBOUND)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001823 worker->task = kthread_create_on_node(worker_thread,
Tejun Heo32704762012-07-13 22:16:45 -07001824 worker, cpu_to_node(gcwq->cpu),
1825 "kworker/%u:%d%s", gcwq->cpu, id, pri);
Tejun Heof3421792010-07-02 10:03:51 +02001826 else
1827 worker->task = kthread_create(worker_thread, worker,
Tejun Heo32704762012-07-13 22:16:45 -07001828 "kworker/u:%d%s", id, pri);
Tejun Heoc34056a2010-06-29 10:07:11 +02001829 if (IS_ERR(worker->task))
1830 goto fail;
1831
Tejun Heo32704762012-07-13 22:16:45 -07001832 if (worker_pool_pri(pool))
1833 set_user_nice(worker->task, HIGHPRI_NICE_LEVEL);
1834
Tejun Heodb7bccf2010-06-29 10:07:12 +02001835 /*
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001836 * Determine CPU binding of the new worker depending on
1837 * %GCWQ_DISASSOCIATED. The caller is responsible for ensuring the
1838 * flag remains stable across this function. See the comments
1839 * above the flag definition for details.
1840 *
1841 * As an unbound worker may later become a regular one if CPU comes
1842 * online, make sure every worker has %PF_THREAD_BOUND set.
Tejun Heodb7bccf2010-06-29 10:07:12 +02001843 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001844 if (!(gcwq->flags & GCWQ_DISASSOCIATED)) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001845 kthread_bind(worker->task, gcwq->cpu);
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001846 } else {
Tejun Heodb7bccf2010-06-29 10:07:12 +02001847 worker->task->flags |= PF_THREAD_BOUND;
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001848 worker->flags |= WORKER_UNBOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001850
Tejun Heoc34056a2010-06-29 10:07:11 +02001851 return worker;
1852fail:
1853 if (id >= 0) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001854 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001855 ida_remove(&pool->worker_ida, id);
Tejun Heo8b03ae32010-06-29 10:07:12 +02001856 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001857 }
1858 kfree(worker);
1859 return NULL;
1860}
1861
1862/**
1863 * start_worker - start a newly created worker
1864 * @worker: worker to start
1865 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001866 * Make the gcwq aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001867 *
1868 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001869 * spin_lock_irq(gcwq->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001870 */
1871static void start_worker(struct worker *worker)
1872{
Tejun Heocb444762010-07-02 10:03:50 +02001873 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001874 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001875 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001876 wake_up_process(worker->task);
1877}
1878
1879/**
1880 * destroy_worker - destroy a workqueue worker
1881 * @worker: worker to be destroyed
1882 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001883 * Destroy @worker and adjust @gcwq stats accordingly.
1884 *
1885 * CONTEXT:
1886 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001887 */
1888static void destroy_worker(struct worker *worker)
1889{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001890 struct worker_pool *pool = worker->pool;
1891 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001892 int id = worker->id;
1893
1894 /* sanity check frenzy */
1895 BUG_ON(worker->current_work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001896 BUG_ON(!list_empty(&worker->scheduled));
Tejun Heoc34056a2010-06-29 10:07:11 +02001897
Tejun Heoc8e55f32010-06-29 10:07:12 +02001898 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001899 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001900 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001901 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001902
1903 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001904 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001905
1906 spin_unlock_irq(&gcwq->lock);
1907
Tejun Heoc34056a2010-06-29 10:07:11 +02001908 kthread_stop(worker->task);
1909 kfree(worker);
1910
Tejun Heo8b03ae32010-06-29 10:07:12 +02001911 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001912 ida_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001913}
1914
Tejun Heo63d95a92012-07-12 14:46:37 -07001915static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001916{
Tejun Heo63d95a92012-07-12 14:46:37 -07001917 struct worker_pool *pool = (void *)__pool;
1918 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001919
1920 spin_lock_irq(&gcwq->lock);
1921
Tejun Heo63d95a92012-07-12 14:46:37 -07001922 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001923 struct worker *worker;
1924 unsigned long expires;
1925
1926 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001927 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001928 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1929
1930 if (time_before(jiffies, expires))
Tejun Heo63d95a92012-07-12 14:46:37 -07001931 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001932 else {
1933 /* it's been idle for too long, wake up manager */
Tejun Heo11ebea52012-07-12 14:46:37 -07001934 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -07001935 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001936 }
1937 }
1938
1939 spin_unlock_irq(&gcwq->lock);
1940}
1941
1942static bool send_mayday(struct work_struct *work)
1943{
1944 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1945 struct workqueue_struct *wq = cwq->wq;
Tejun Heof3421792010-07-02 10:03:51 +02001946 unsigned int cpu;
Tejun Heoe22bee72010-06-29 10:07:14 +02001947
1948 if (!(wq->flags & WQ_RESCUER))
1949 return false;
1950
1951 /* mayday mayday mayday */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001952 cpu = cwq->pool->gcwq->cpu;
Tejun Heof3421792010-07-02 10:03:51 +02001953 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1954 if (cpu == WORK_CPU_UNBOUND)
1955 cpu = 0;
Tejun Heof2e005a2010-07-20 15:59:09 +02001956 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001957 wake_up_process(wq->rescuer->task);
1958 return true;
1959}
1960
Tejun Heo63d95a92012-07-12 14:46:37 -07001961static void gcwq_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001962{
Tejun Heo63d95a92012-07-12 14:46:37 -07001963 struct worker_pool *pool = (void *)__pool;
1964 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001965 struct work_struct *work;
1966
1967 spin_lock_irq(&gcwq->lock);
1968
Tejun Heo63d95a92012-07-12 14:46:37 -07001969 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001970 /*
1971 * We've been trying to create a new worker but
1972 * haven't been successful. We might be hitting an
1973 * allocation deadlock. Send distress signals to
1974 * rescuers.
1975 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001976 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001977 send_mayday(work);
1978 }
1979
1980 spin_unlock_irq(&gcwq->lock);
1981
Tejun Heo63d95a92012-07-12 14:46:37 -07001982 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001983}
1984
1985/**
1986 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001987 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001988 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001989 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001990 * have at least one idle worker on return from this function. If
1991 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001992 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001993 * possible allocation deadlock.
1994 *
1995 * On return, need_to_create_worker() is guaranteed to be false and
1996 * may_start_working() true.
1997 *
1998 * LOCKING:
1999 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
2000 * multiple times. Does GFP_KERNEL allocations. Called only from
2001 * manager.
2002 *
2003 * RETURNS:
2004 * false if no action was taken and gcwq->lock stayed locked, true
2005 * otherwise.
2006 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002007static bool maybe_create_worker(struct worker_pool *pool)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09002008__releases(&gcwq->lock)
2009__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02002010{
Tejun Heo63d95a92012-07-12 14:46:37 -07002011 struct global_cwq *gcwq = pool->gcwq;
2012
2013 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002014 return false;
2015restart:
Tejun Heo9f9c2362010-07-14 11:31:20 +02002016 spin_unlock_irq(&gcwq->lock);
2017
Tejun Heoe22bee72010-06-29 10:07:14 +02002018 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07002019 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02002020
2021 while (true) {
2022 struct worker *worker;
2023
Tejun Heobc2ae0f2012-07-17 12:39:27 -07002024 worker = create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002025 if (worker) {
Tejun Heo63d95a92012-07-12 14:46:37 -07002026 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02002027 spin_lock_irq(&gcwq->lock);
2028 start_worker(worker);
Tejun Heo63d95a92012-07-12 14:46:37 -07002029 BUG_ON(need_to_create_worker(pool));
Tejun Heoe22bee72010-06-29 10:07:14 +02002030 return true;
2031 }
2032
Tejun Heo63d95a92012-07-12 14:46:37 -07002033 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002034 break;
2035
Tejun Heoe22bee72010-06-29 10:07:14 +02002036 __set_current_state(TASK_INTERRUPTIBLE);
2037 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02002038
Tejun Heo63d95a92012-07-12 14:46:37 -07002039 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002040 break;
2041 }
2042
Tejun Heo63d95a92012-07-12 14:46:37 -07002043 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02002044 spin_lock_irq(&gcwq->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07002045 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002046 goto restart;
2047 return true;
2048}
2049
2050/**
2051 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo63d95a92012-07-12 14:46:37 -07002052 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02002053 *
Tejun Heo63d95a92012-07-12 14:46:37 -07002054 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02002055 * IDLE_WORKER_TIMEOUT.
2056 *
2057 * LOCKING:
2058 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
2059 * multiple times. Called only from manager.
2060 *
2061 * RETURNS:
2062 * false if no action was taken and gcwq->lock stayed locked, true
2063 * otherwise.
2064 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002065static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02002066{
2067 bool ret = false;
2068
Tejun Heo63d95a92012-07-12 14:46:37 -07002069 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02002070 struct worker *worker;
2071 unsigned long expires;
2072
Tejun Heo63d95a92012-07-12 14:46:37 -07002073 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02002074 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
2075
2076 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07002077 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02002078 break;
2079 }
2080
2081 destroy_worker(worker);
2082 ret = true;
2083 }
2084
2085 return ret;
2086}
2087
2088/**
2089 * manage_workers - manage worker pool
2090 * @worker: self
2091 *
2092 * Assume the manager role and manage gcwq worker pool @worker belongs
2093 * to. At any given time, there can be only zero or one manager per
2094 * gcwq. The exclusion is handled automatically by this function.
2095 *
2096 * The caller can safely start processing works on false return. On
2097 * true return, it's guaranteed that need_to_create_worker() is false
2098 * and may_start_working() is true.
2099 *
2100 * CONTEXT:
2101 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
2102 * multiple times. Does GFP_KERNEL allocations.
2103 *
2104 * RETURNS:
2105 * false if no action was taken and gcwq->lock stayed locked, true if
2106 * some action was taken.
2107 */
2108static bool manage_workers(struct worker *worker)
2109{
Tejun Heo63d95a92012-07-12 14:46:37 -07002110 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002111 bool ret = false;
2112
Tejun Heo60373152012-07-17 12:39:27 -07002113 if (!mutex_trylock(&pool->manager_mutex))
Tejun Heoe22bee72010-06-29 10:07:14 +02002114 return ret;
2115
Tejun Heo11ebea52012-07-12 14:46:37 -07002116 pool->flags &= ~POOL_MANAGE_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02002117
2118 /*
2119 * Destroy and then create so that may_start_working() is true
2120 * on return.
2121 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002122 ret |= maybe_destroy_workers(pool);
2123 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002124
Tejun Heo60373152012-07-17 12:39:27 -07002125 mutex_unlock(&pool->manager_mutex);
Tejun Heoe22bee72010-06-29 10:07:14 +02002126 return ret;
2127}
2128
Tejun Heoa62428c2010-06-29 10:07:10 +02002129/**
2130 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02002131 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02002132 * @work: work to process
2133 *
2134 * Process @work. This function contains all the logics necessary to
2135 * process a single work including synchronization against and
2136 * interaction with other workers on the same cpu, queueing and
2137 * flushing. As long as context requirement is met, any worker can
2138 * call this function to process a work.
2139 *
2140 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002141 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02002142 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002143static void process_one_work(struct worker *worker, struct work_struct *work)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09002144__releases(&gcwq->lock)
2145__acquires(&gcwq->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02002146{
Tejun Heo7e116292010-06-29 10:07:13 +02002147 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002148 struct worker_pool *pool = worker->pool;
2149 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002150 struct hlist_head *bwh = busy_worker_head(gcwq, work);
Tejun Heofb0e7be2010-06-29 10:07:15 +02002151 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heoa62428c2010-06-29 10:07:10 +02002152 work_func_t f = work->func;
Tejun Heo73f53c42010-06-29 10:07:11 +02002153 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02002154 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02002155#ifdef CONFIG_LOCKDEP
2156 /*
2157 * It is permissible to free the struct work_struct from
2158 * inside the function that is called from it, this we need to
2159 * take into account for lockdep too. To avoid bogus "held
2160 * lock freed" warnings as well as problems when looking into
2161 * work->lockdep_map, make a copy and use that here.
2162 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07002163 struct lockdep_map lockdep_map;
2164
2165 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002166#endif
Tejun Heo6fec10a2012-07-22 10:16:34 -07002167 /*
2168 * Ensure we're on the correct CPU. DISASSOCIATED test is
2169 * necessary to avoid spurious warnings from rescuers servicing the
2170 * unbound or a disassociated gcwq.
2171 */
Tejun Heo25511a42012-07-17 12:39:27 -07002172 WARN_ON_ONCE(!(worker->flags & (WORKER_UNBOUND | WORKER_REBIND)) &&
Tejun Heo6fec10a2012-07-22 10:16:34 -07002173 !(gcwq->flags & GCWQ_DISASSOCIATED) &&
Tejun Heo25511a42012-07-17 12:39:27 -07002174 raw_smp_processor_id() != gcwq->cpu);
2175
Tejun Heo7e116292010-06-29 10:07:13 +02002176 /*
2177 * A single work shouldn't be executed concurrently by
2178 * multiple workers on a single cpu. Check whether anyone is
2179 * already processing the work. If so, defer the work to the
2180 * currently executing one.
2181 */
2182 collision = __find_worker_executing_work(gcwq, bwh, work);
2183 if (unlikely(collision)) {
2184 move_linked_works(work, &collision->scheduled, NULL);
2185 return;
2186 }
2187
Tejun Heo8930cab2012-08-03 10:30:45 -07002188 /* claim and dequeue */
Tejun Heoa62428c2010-06-29 10:07:10 +02002189 debug_work_deactivate(work);
Tejun Heoc8e55f32010-06-29 10:07:12 +02002190 hlist_add_head(&worker->hentry, bwh);
Tejun Heoc34056a2010-06-29 10:07:11 +02002191 worker->current_work = work;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02002192 worker->current_cwq = cwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002193 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002194
Tejun Heoa62428c2010-06-29 10:07:10 +02002195 list_del_init(&work->entry);
2196
Tejun Heo649027d2010-06-29 10:07:14 +02002197 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02002198 * CPU intensive works don't participate in concurrency
2199 * management. They're the scheduler's responsibility.
2200 */
2201 if (unlikely(cpu_intensive))
2202 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
2203
Tejun Heo974271c2012-07-12 14:46:37 -07002204 /*
2205 * Unbound gcwq isn't concurrency managed and work items should be
2206 * executed ASAP. Wake up another worker if necessary.
2207 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002208 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
2209 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07002210
Tejun Heo8930cab2012-08-03 10:30:45 -07002211 /*
Tejun Heo23657bb2012-08-13 17:08:19 -07002212 * Record the last CPU and clear PENDING which should be the last
2213 * update to @work. Also, do this inside @gcwq->lock so that
2214 * PENDING and queued state changes happen together while IRQ is
2215 * disabled.
Tejun Heo8930cab2012-08-03 10:30:45 -07002216 */
Tejun Heo8930cab2012-08-03 10:30:45 -07002217 set_work_cpu_and_clear_pending(work, gcwq->cpu);
Tejun Heoa62428c2010-06-29 10:07:10 +02002218
Tejun Heo8930cab2012-08-03 10:30:45 -07002219 spin_unlock_irq(&gcwq->lock);
Tejun Heo959d1af2012-08-03 10:30:45 -07002220
Tejun Heoe1594892011-01-09 23:32:15 +01002221 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002222 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002223 trace_workqueue_execute_start(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002224 f(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002225 /*
2226 * While we must be careful to not use "work" after this, the trace
2227 * point will only record its address.
2228 */
2229 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002230 lock_map_release(&lockdep_map);
2231 lock_map_release(&cwq->wq->lockdep_map);
2232
2233 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Valentin Ilie044c7822012-08-19 00:52:42 +03002234 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
2235 " last function: %pf\n",
2236 current->comm, preempt_count(), task_pid_nr(current), f);
Tejun Heoa62428c2010-06-29 10:07:10 +02002237 debug_show_held_locks(current);
2238 dump_stack();
2239 }
2240
Tejun Heo8b03ae32010-06-29 10:07:12 +02002241 spin_lock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002242
Tejun Heofb0e7be2010-06-29 10:07:15 +02002243 /* clear cpu intensive status */
2244 if (unlikely(cpu_intensive))
2245 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2246
Tejun Heoa62428c2010-06-29 10:07:10 +02002247 /* we're done with it, release */
Tejun Heoc8e55f32010-06-29 10:07:12 +02002248 hlist_del_init(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002249 worker->current_work = NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02002250 worker->current_cwq = NULL;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02002251 cwq_dec_nr_in_flight(cwq, work_color, false);
Tejun Heoa62428c2010-06-29 10:07:10 +02002252}
2253
Tejun Heoaffee4b2010-06-29 10:07:12 +02002254/**
2255 * process_scheduled_works - process scheduled works
2256 * @worker: self
2257 *
2258 * Process all scheduled works. Please note that the scheduled list
2259 * may change while processing a work, so this function repeatedly
2260 * fetches a work from the top and executes it.
2261 *
2262 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002263 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02002264 * multiple times.
2265 */
2266static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002268 while (!list_empty(&worker->scheduled)) {
2269 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002271 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273}
2274
Tejun Heo4690c4a2010-06-29 10:07:10 +02002275/**
2276 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02002277 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02002278 *
Tejun Heoe22bee72010-06-29 10:07:14 +02002279 * The gcwq worker thread function. There's a single dynamic pool of
2280 * these per each cpu. These workers process all works regardless of
2281 * their specific target workqueue. The only exception is works which
2282 * belong to workqueues with a rescuer which will be explained in
2283 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02002284 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002285static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286{
Tejun Heoc34056a2010-06-29 10:07:11 +02002287 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07002288 struct worker_pool *pool = worker->pool;
2289 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
Tejun Heoe22bee72010-06-29 10:07:14 +02002291 /* tell the scheduler that this is a workqueue worker */
2292 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002293woke_up:
Tejun Heoc8e55f32010-06-29 10:07:12 +02002294 spin_lock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
Tejun Heo25511a42012-07-17 12:39:27 -07002296 /*
2297 * DIE can be set only while idle and REBIND set while busy has
2298 * @worker->rebind_work scheduled. Checking here is enough.
2299 */
2300 if (unlikely(worker->flags & (WORKER_REBIND | WORKER_DIE))) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02002301 spin_unlock_irq(&gcwq->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07002302
2303 if (worker->flags & WORKER_DIE) {
2304 worker->task->flags &= ~PF_WQ_WORKER;
2305 return 0;
2306 }
2307
2308 idle_worker_rebind(worker);
2309 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 }
2311
Tejun Heoc8e55f32010-06-29 10:07:12 +02002312 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002313recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002314 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002315 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002316 goto sleep;
2317
2318 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002319 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002320 goto recheck;
2321
Tejun Heoc8e55f32010-06-29 10:07:12 +02002322 /*
2323 * ->scheduled list can only be filled while a worker is
2324 * preparing to process a work or actually processing it.
2325 * Make sure nobody diddled with it while I was sleeping.
2326 */
2327 BUG_ON(!list_empty(&worker->scheduled));
2328
Tejun Heoe22bee72010-06-29 10:07:14 +02002329 /*
2330 * When control reaches this point, we're guaranteed to have
2331 * at least one idle worker or that someone else has already
2332 * assumed the manager role.
2333 */
2334 worker_clr_flags(worker, WORKER_PREP);
2335
2336 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002337 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002338 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002339 struct work_struct, entry);
2340
2341 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2342 /* optimization path, not strictly necessary */
2343 process_one_work(worker, work);
2344 if (unlikely(!list_empty(&worker->scheduled)))
2345 process_scheduled_works(worker);
2346 } else {
2347 move_linked_works(work, &worker->scheduled, NULL);
2348 process_scheduled_works(worker);
2349 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002350 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002351
Tejun Heoe22bee72010-06-29 10:07:14 +02002352 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002353sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07002354 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002355 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002356
Tejun Heoc8e55f32010-06-29 10:07:12 +02002357 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02002358 * gcwq->lock is held and there's no work to process and no
2359 * need to manage, sleep. Workers are woken up only while
2360 * holding gcwq->lock or from local cpu, so setting the
2361 * current state before releasing gcwq->lock is enough to
2362 * prevent losing any event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002363 */
2364 worker_enter_idle(worker);
2365 __set_current_state(TASK_INTERRUPTIBLE);
2366 spin_unlock_irq(&gcwq->lock);
2367 schedule();
2368 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369}
2370
Tejun Heoe22bee72010-06-29 10:07:14 +02002371/**
2372 * rescuer_thread - the rescuer thread function
2373 * @__wq: the associated workqueue
2374 *
2375 * Workqueue rescuer thread function. There's one rescuer for each
2376 * workqueue which has WQ_RESCUER set.
2377 *
2378 * Regular work processing on a gcwq may block trying to create a new
2379 * worker which uses GFP_KERNEL allocation which has slight chance of
2380 * developing into deadlock if some works currently on the same queue
2381 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2382 * the problem rescuer solves.
2383 *
2384 * When such condition is possible, the gcwq summons rescuers of all
2385 * workqueues which have works queued on the gcwq and let them process
2386 * those works so that forward progress can be guaranteed.
2387 *
2388 * This should happen rarely.
2389 */
2390static int rescuer_thread(void *__wq)
2391{
2392 struct workqueue_struct *wq = __wq;
2393 struct worker *rescuer = wq->rescuer;
2394 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heof3421792010-07-02 10:03:51 +02002395 bool is_unbound = wq->flags & WQ_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02002396 unsigned int cpu;
2397
2398 set_user_nice(current, RESCUER_NICE_LEVEL);
2399repeat:
2400 set_current_state(TASK_INTERRUPTIBLE);
2401
2402 if (kthread_should_stop())
2403 return 0;
2404
Tejun Heof3421792010-07-02 10:03:51 +02002405 /*
2406 * See whether any cpu is asking for help. Unbounded
2407 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
2408 */
Tejun Heof2e005a2010-07-20 15:59:09 +02002409 for_each_mayday_cpu(cpu, wq->mayday_mask) {
Tejun Heof3421792010-07-02 10:03:51 +02002410 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2411 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002412 struct worker_pool *pool = cwq->pool;
2413 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002414 struct work_struct *work, *n;
2415
2416 __set_current_state(TASK_RUNNING);
Tejun Heof2e005a2010-07-20 15:59:09 +02002417 mayday_clear_cpu(cpu, wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002418
2419 /* migrate to the target cpu if possible */
Tejun Heobd7bdd42012-07-12 14:46:37 -07002420 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002421 worker_maybe_bind_and_lock(rescuer);
2422
2423 /*
2424 * Slurp in all works issued via this workqueue and
2425 * process'em.
2426 */
2427 BUG_ON(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002428 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02002429 if (get_work_cwq(work) == cwq)
2430 move_linked_works(work, scheduled, &n);
2431
2432 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002433
2434 /*
2435 * Leave this gcwq. If keep_working() is %true, notify a
2436 * regular worker; otherwise, we end up with 0 concurrency
2437 * and stalling the execution.
2438 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002439 if (keep_working(pool))
2440 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002441
Tejun Heoe22bee72010-06-29 10:07:14 +02002442 spin_unlock_irq(&gcwq->lock);
2443 }
2444
2445 schedule();
2446 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447}
2448
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002449struct wq_barrier {
2450 struct work_struct work;
2451 struct completion done;
2452};
2453
2454static void wq_barrier_func(struct work_struct *work)
2455{
2456 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2457 complete(&barr->done);
2458}
2459
Tejun Heo4690c4a2010-06-29 10:07:10 +02002460/**
2461 * insert_wq_barrier - insert a barrier work
2462 * @cwq: cwq to insert barrier into
2463 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002464 * @target: target work to attach @barr to
2465 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002466 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002467 * @barr is linked to @target such that @barr is completed only after
2468 * @target finishes execution. Please note that the ordering
2469 * guarantee is observed only with respect to @target and on the local
2470 * cpu.
2471 *
2472 * Currently, a queued barrier can't be canceled. This is because
2473 * try_to_grab_pending() can't determine whether the work to be
2474 * grabbed is at the head of the queue and thus can't clear LINKED
2475 * flag of the previous work while there must be a valid next work
2476 * after a work with LINKED flag set.
2477 *
2478 * Note that when @worker is non-NULL, @target may be modified
2479 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002480 *
2481 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002482 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002483 */
Oleg Nesterov83c22522007-05-09 02:33:54 -07002484static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002485 struct wq_barrier *barr,
2486 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002487{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002488 struct list_head *head;
2489 unsigned int linked = 0;
2490
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002491 /*
Tejun Heo8b03ae32010-06-29 10:07:12 +02002492 * debugobject calls are safe here even with gcwq->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002493 * as we know for sure that this will not trigger any of the
2494 * checks and call back into the fixup functions where we
2495 * might deadlock.
2496 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002497 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002498 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002499 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002500
Tejun Heoaffee4b2010-06-29 10:07:12 +02002501 /*
2502 * If @target is currently being executed, schedule the
2503 * barrier to the worker; otherwise, put it after @target.
2504 */
2505 if (worker)
2506 head = worker->scheduled.next;
2507 else {
2508 unsigned long *bits = work_data_bits(target);
2509
2510 head = target->entry.next;
2511 /* there can already be other linked works, inherit and set */
2512 linked = *bits & WORK_STRUCT_LINKED;
2513 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2514 }
2515
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002516 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002517 insert_work(cwq, &barr->work, head,
2518 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002519}
2520
Tejun Heo73f53c42010-06-29 10:07:11 +02002521/**
2522 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2523 * @wq: workqueue being flushed
2524 * @flush_color: new flush color, < 0 for no-op
2525 * @work_color: new work color, < 0 for no-op
2526 *
2527 * Prepare cwqs for workqueue flushing.
2528 *
2529 * If @flush_color is non-negative, flush_color on all cwqs should be
2530 * -1. If no cwq has in-flight commands at the specified color, all
2531 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2532 * has in flight commands, its cwq->flush_color is set to
2533 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2534 * wakeup logic is armed and %true is returned.
2535 *
2536 * The caller should have initialized @wq->first_flusher prior to
2537 * calling this function with non-negative @flush_color. If
2538 * @flush_color is negative, no flush color update is done and %false
2539 * is returned.
2540 *
2541 * If @work_color is non-negative, all cwqs should have the same
2542 * work_color which is previous to @work_color and all will be
2543 * advanced to @work_color.
2544 *
2545 * CONTEXT:
2546 * mutex_lock(wq->flush_mutex).
2547 *
2548 * RETURNS:
2549 * %true if @flush_color >= 0 and there's something to flush. %false
2550 * otherwise.
2551 */
2552static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2553 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554{
Tejun Heo73f53c42010-06-29 10:07:11 +02002555 bool wait = false;
2556 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -07002557
Tejun Heo73f53c42010-06-29 10:07:11 +02002558 if (flush_color >= 0) {
2559 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2560 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002561 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002562
Tejun Heof3421792010-07-02 10:03:51 +02002563 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002564 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002565 struct global_cwq *gcwq = cwq->pool->gcwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002566
Tejun Heo8b03ae32010-06-29 10:07:12 +02002567 spin_lock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002568
2569 if (flush_color >= 0) {
2570 BUG_ON(cwq->flush_color != -1);
2571
2572 if (cwq->nr_in_flight[flush_color]) {
2573 cwq->flush_color = flush_color;
2574 atomic_inc(&wq->nr_cwqs_to_flush);
2575 wait = true;
2576 }
2577 }
2578
2579 if (work_color >= 0) {
2580 BUG_ON(work_color != work_next_color(cwq->work_color));
2581 cwq->work_color = work_color;
2582 }
2583
Tejun Heo8b03ae32010-06-29 10:07:12 +02002584 spin_unlock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002585 }
2586
2587 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2588 complete(&wq->first_flusher->done);
2589
2590 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591}
2592
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002593/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002595 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 *
2597 * Forces execution of the workqueue and blocks until its completion.
2598 * This is typically used in driver shutdown handlers.
2599 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002600 * We sleep until all works which were queued on entry have been handled,
2601 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002603void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604{
Tejun Heo73f53c42010-06-29 10:07:11 +02002605 struct wq_flusher this_flusher = {
2606 .list = LIST_HEAD_INIT(this_flusher.list),
2607 .flush_color = -1,
2608 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2609 };
2610 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002611
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002612 lock_map_acquire(&wq->lockdep_map);
2613 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002614
2615 mutex_lock(&wq->flush_mutex);
2616
2617 /*
2618 * Start-to-wait phase
2619 */
2620 next_color = work_next_color(wq->work_color);
2621
2622 if (next_color != wq->flush_color) {
2623 /*
2624 * Color space is not full. The current work_color
2625 * becomes our flush_color and work_color is advanced
2626 * by one.
2627 */
2628 BUG_ON(!list_empty(&wq->flusher_overflow));
2629 this_flusher.flush_color = wq->work_color;
2630 wq->work_color = next_color;
2631
2632 if (!wq->first_flusher) {
2633 /* no flush in progress, become the first flusher */
2634 BUG_ON(wq->flush_color != this_flusher.flush_color);
2635
2636 wq->first_flusher = &this_flusher;
2637
2638 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2639 wq->work_color)) {
2640 /* nothing to flush, done */
2641 wq->flush_color = next_color;
2642 wq->first_flusher = NULL;
2643 goto out_unlock;
2644 }
2645 } else {
2646 /* wait in queue */
2647 BUG_ON(wq->flush_color == this_flusher.flush_color);
2648 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2649 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2650 }
2651 } else {
2652 /*
2653 * Oops, color space is full, wait on overflow queue.
2654 * The next flush completion will assign us
2655 * flush_color and transfer to flusher_queue.
2656 */
2657 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2658 }
2659
2660 mutex_unlock(&wq->flush_mutex);
2661
2662 wait_for_completion(&this_flusher.done);
2663
2664 /*
2665 * Wake-up-and-cascade phase
2666 *
2667 * First flushers are responsible for cascading flushes and
2668 * handling overflow. Non-first flushers can simply return.
2669 */
2670 if (wq->first_flusher != &this_flusher)
2671 return;
2672
2673 mutex_lock(&wq->flush_mutex);
2674
Tejun Heo4ce48b32010-07-02 10:03:51 +02002675 /* we might have raced, check again with mutex held */
2676 if (wq->first_flusher != &this_flusher)
2677 goto out_unlock;
2678
Tejun Heo73f53c42010-06-29 10:07:11 +02002679 wq->first_flusher = NULL;
2680
2681 BUG_ON(!list_empty(&this_flusher.list));
2682 BUG_ON(wq->flush_color != this_flusher.flush_color);
2683
2684 while (true) {
2685 struct wq_flusher *next, *tmp;
2686
2687 /* complete all the flushers sharing the current flush color */
2688 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2689 if (next->flush_color != wq->flush_color)
2690 break;
2691 list_del_init(&next->list);
2692 complete(&next->done);
2693 }
2694
2695 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2696 wq->flush_color != work_next_color(wq->work_color));
2697
2698 /* this flush_color is finished, advance by one */
2699 wq->flush_color = work_next_color(wq->flush_color);
2700
2701 /* one color has been freed, handle overflow queue */
2702 if (!list_empty(&wq->flusher_overflow)) {
2703 /*
2704 * Assign the same color to all overflowed
2705 * flushers, advance work_color and append to
2706 * flusher_queue. This is the start-to-wait
2707 * phase for these overflowed flushers.
2708 */
2709 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2710 tmp->flush_color = wq->work_color;
2711
2712 wq->work_color = work_next_color(wq->work_color);
2713
2714 list_splice_tail_init(&wq->flusher_overflow,
2715 &wq->flusher_queue);
2716 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2717 }
2718
2719 if (list_empty(&wq->flusher_queue)) {
2720 BUG_ON(wq->flush_color != wq->work_color);
2721 break;
2722 }
2723
2724 /*
2725 * Need to flush more colors. Make the next flusher
2726 * the new first flusher and arm cwqs.
2727 */
2728 BUG_ON(wq->flush_color == wq->work_color);
2729 BUG_ON(wq->flush_color != next->flush_color);
2730
2731 list_del_init(&next->list);
2732 wq->first_flusher = next;
2733
2734 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2735 break;
2736
2737 /*
2738 * Meh... this color is already done, clear first
2739 * flusher and repeat cascading.
2740 */
2741 wq->first_flusher = NULL;
2742 }
2743
2744out_unlock:
2745 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746}
Dave Jonesae90dd52006-06-30 01:40:45 -04002747EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002749/**
2750 * drain_workqueue - drain a workqueue
2751 * @wq: workqueue to drain
2752 *
2753 * Wait until the workqueue becomes empty. While draining is in progress,
2754 * only chain queueing is allowed. IOW, only currently pending or running
2755 * work items on @wq can queue further work items on it. @wq is flushed
2756 * repeatedly until it becomes empty. The number of flushing is detemined
2757 * by the depth of chaining and should be relatively short. Whine if it
2758 * takes too long.
2759 */
2760void drain_workqueue(struct workqueue_struct *wq)
2761{
2762 unsigned int flush_cnt = 0;
2763 unsigned int cpu;
2764
2765 /*
2766 * __queue_work() needs to test whether there are drainers, is much
2767 * hotter than drain_workqueue() and already looks at @wq->flags.
2768 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2769 */
2770 spin_lock(&workqueue_lock);
2771 if (!wq->nr_drainers++)
2772 wq->flags |= WQ_DRAINING;
2773 spin_unlock(&workqueue_lock);
2774reflush:
2775 flush_workqueue(wq);
2776
2777 for_each_cwq_cpu(cpu, wq) {
2778 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002779 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002780
Tejun Heobd7bdd42012-07-12 14:46:37 -07002781 spin_lock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002782 drained = !cwq->nr_active && list_empty(&cwq->delayed_works);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002783 spin_unlock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002784
2785 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002786 continue;
2787
2788 if (++flush_cnt == 10 ||
2789 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
Valentin Ilie044c7822012-08-19 00:52:42 +03002790 pr_warn("workqueue %s: flush on destruction isn't complete after %u tries\n",
2791 wq->name, flush_cnt);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002792 goto reflush;
2793 }
2794
2795 spin_lock(&workqueue_lock);
2796 if (!--wq->nr_drainers)
2797 wq->flags &= ~WQ_DRAINING;
2798 spin_unlock(&workqueue_lock);
2799}
2800EXPORT_SYMBOL_GPL(drain_workqueue);
2801
Tejun Heo606a5022012-08-20 14:51:23 -07002802static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
Tejun Heobaf59022010-09-16 10:42:16 +02002803{
2804 struct worker *worker = NULL;
2805 struct global_cwq *gcwq;
2806 struct cpu_workqueue_struct *cwq;
2807
2808 might_sleep();
2809 gcwq = get_work_gcwq(work);
2810 if (!gcwq)
2811 return false;
2812
2813 spin_lock_irq(&gcwq->lock);
2814 if (!list_empty(&work->entry)) {
2815 /*
2816 * See the comment near try_to_grab_pending()->smp_rmb().
2817 * If it was re-queued to a different gcwq under us, we
2818 * are not going to wait.
2819 */
2820 smp_rmb();
2821 cwq = get_work_cwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002822 if (unlikely(!cwq || gcwq != cwq->pool->gcwq))
Tejun Heobaf59022010-09-16 10:42:16 +02002823 goto already_gone;
Tejun Heo606a5022012-08-20 14:51:23 -07002824 } else {
Tejun Heobaf59022010-09-16 10:42:16 +02002825 worker = find_worker_executing_work(gcwq, work);
2826 if (!worker)
2827 goto already_gone;
2828 cwq = worker->current_cwq;
Tejun Heo606a5022012-08-20 14:51:23 -07002829 }
Tejun Heobaf59022010-09-16 10:42:16 +02002830
2831 insert_wq_barrier(cwq, barr, work, worker);
2832 spin_unlock_irq(&gcwq->lock);
2833
Tejun Heoe1594892011-01-09 23:32:15 +01002834 /*
2835 * If @max_active is 1 or rescuer is in use, flushing another work
2836 * item on the same workqueue may lead to deadlock. Make sure the
2837 * flusher is not running on the same workqueue by verifying write
2838 * access.
2839 */
2840 if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER)
2841 lock_map_acquire(&cwq->wq->lockdep_map);
2842 else
2843 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heobaf59022010-09-16 10:42:16 +02002844 lock_map_release(&cwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002845
Tejun Heobaf59022010-09-16 10:42:16 +02002846 return true;
2847already_gone:
2848 spin_unlock_irq(&gcwq->lock);
2849 return false;
2850}
2851
Oleg Nesterovdb700892008-07-25 01:47:49 -07002852/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002853 * flush_work - wait for a work to finish executing the last queueing instance
2854 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002855 *
Tejun Heo606a5022012-08-20 14:51:23 -07002856 * Wait until @work has finished execution. @work is guaranteed to be idle
2857 * on return if it hasn't been requeued since flush started.
Tejun Heo401a8d02010-09-16 10:36:00 +02002858 *
2859 * RETURNS:
2860 * %true if flush_work() waited for the work to finish execution,
2861 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002862 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002863bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002864{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002865 struct wq_barrier barr;
2866
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002867 lock_map_acquire(&work->lockdep_map);
2868 lock_map_release(&work->lockdep_map);
2869
Tejun Heo606a5022012-08-20 14:51:23 -07002870 if (start_flush_work(work, &barr)) {
Tejun Heobaf59022010-09-16 10:42:16 +02002871 wait_for_completion(&barr.done);
2872 destroy_work_on_stack(&barr.work);
2873 return true;
Tejun Heo606a5022012-08-20 14:51:23 -07002874 } else {
Tejun Heobaf59022010-09-16 10:42:16 +02002875 return false;
Tejun Heo606a5022012-08-20 14:51:23 -07002876 }
Oleg Nesterovdb700892008-07-25 01:47:49 -07002877}
2878EXPORT_SYMBOL_GPL(flush_work);
2879
Tejun Heo36e227d2012-08-03 10:30:46 -07002880static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002881{
Tejun Heobbb68df2012-08-03 10:30:46 -07002882 unsigned long flags;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002883 int ret;
2884
2885 do {
Tejun Heobbb68df2012-08-03 10:30:46 -07002886 ret = try_to_grab_pending(work, is_dwork, &flags);
2887 /*
2888 * If someone else is canceling, wait for the same event it
2889 * would be waiting for before retrying.
2890 */
2891 if (unlikely(ret == -ENOENT))
Tejun Heo606a5022012-08-20 14:51:23 -07002892 flush_work(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002893 } while (unlikely(ret < 0));
2894
Tejun Heobbb68df2012-08-03 10:30:46 -07002895 /* tell other tasks trying to grab @work to back off */
2896 mark_work_canceling(work);
2897 local_irq_restore(flags);
2898
Tejun Heo606a5022012-08-20 14:51:23 -07002899 flush_work(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002900 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002901 return ret;
2902}
2903
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002904/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002905 * cancel_work_sync - cancel a work and wait for it to finish
2906 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002907 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002908 * Cancel @work and wait for its execution to finish. This function
2909 * can be used even if the work re-queues itself or migrates to
2910 * another workqueue. On return from this function, @work is
2911 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002912 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002913 * cancel_work_sync(&delayed_work->work) must not be used for
2914 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002915 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002916 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002917 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002918 *
2919 * RETURNS:
2920 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002921 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002922bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002923{
Tejun Heo36e227d2012-08-03 10:30:46 -07002924 return __cancel_work_timer(work, false);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002925}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002926EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002927
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002928/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002929 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2930 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002931 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002932 * Delayed timer is cancelled and the pending work is queued for
2933 * immediate execution. Like flush_work(), this function only
2934 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002935 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002936 * RETURNS:
2937 * %true if flush_work() waited for the work to finish execution,
2938 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002939 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002940bool flush_delayed_work(struct delayed_work *dwork)
2941{
Tejun Heo8930cab2012-08-03 10:30:45 -07002942 local_irq_disable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002943 if (del_timer_sync(&dwork->timer))
Tejun Heo12650572012-08-08 09:38:42 -07002944 __queue_work(dwork->cpu,
Tejun Heo401a8d02010-09-16 10:36:00 +02002945 get_work_cwq(&dwork->work)->wq, &dwork->work);
Tejun Heo8930cab2012-08-03 10:30:45 -07002946 local_irq_enable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002947 return flush_work(&dwork->work);
2948}
2949EXPORT_SYMBOL(flush_delayed_work);
2950
2951/**
Tejun Heo57b30ae2012-08-21 13:18:24 -07002952 * cancel_delayed_work - cancel a delayed work
2953 * @dwork: delayed_work to cancel
2954 *
2955 * Kill off a pending delayed_work. Returns %true if @dwork was pending
2956 * and canceled; %false if wasn't pending. Note that the work callback
2957 * function may still be running on return, unless it returns %true and the
2958 * work doesn't re-arm itself. Explicitly flush or use
2959 * cancel_delayed_work_sync() to wait on it.
2960 *
2961 * This function is safe to call from any context including IRQ handler.
2962 */
2963bool cancel_delayed_work(struct delayed_work *dwork)
2964{
2965 unsigned long flags;
2966 int ret;
2967
2968 do {
2969 ret = try_to_grab_pending(&dwork->work, true, &flags);
2970 } while (unlikely(ret == -EAGAIN));
2971
2972 if (unlikely(ret < 0))
2973 return false;
2974
2975 set_work_cpu_and_clear_pending(&dwork->work, work_cpu(&dwork->work));
2976 local_irq_restore(flags);
2977 return true;
2978}
2979EXPORT_SYMBOL(cancel_delayed_work);
2980
2981/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002982 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2983 * @dwork: the delayed work cancel
2984 *
2985 * This is cancel_work_sync() for delayed works.
2986 *
2987 * RETURNS:
2988 * %true if @dwork was pending, %false otherwise.
2989 */
2990bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002991{
Tejun Heo36e227d2012-08-03 10:30:46 -07002992 return __cancel_work_timer(&dwork->work, true);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002993}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002994EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
Tejun Heod4283e92012-08-03 10:30:44 -07002996/**
Tejun Heo0a13c002012-08-03 10:30:44 -07002997 * schedule_work_on - put work task on a specific cpu
2998 * @cpu: cpu to put the work task on
2999 * @work: job to be done
3000 *
3001 * This puts a job on a specific cpu
3002 */
Tejun Heod4283e92012-08-03 10:30:44 -07003003bool schedule_work_on(int cpu, struct work_struct *work)
Tejun Heo0a13c002012-08-03 10:30:44 -07003004{
3005 return queue_work_on(cpu, system_wq, work);
3006}
3007EXPORT_SYMBOL(schedule_work_on);
3008
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003009/**
3010 * schedule_work - put work task in global workqueue
3011 * @work: job to be done
3012 *
Tejun Heod4283e92012-08-03 10:30:44 -07003013 * Returns %false if @work was already on the kernel-global workqueue and
3014 * %true otherwise.
Bart Van Assche5b0f437d2009-07-30 19:00:53 +02003015 *
3016 * This puts a job in the kernel-global workqueue if it was not already
3017 * queued and leaves it in the same position on the kernel-global
3018 * workqueue otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003019 */
Tejun Heod4283e92012-08-03 10:30:44 -07003020bool schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021{
Tejun Heod320c032010-06-29 10:07:14 +02003022 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023}
Dave Jonesae90dd52006-06-30 01:40:45 -04003024EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003026/**
3027 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
3028 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +00003029 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003030 * @delay: number of jiffies to wait
3031 *
3032 * After waiting for a given time this puts a job in the kernel-global
3033 * workqueue on the specified CPU.
3034 */
Tejun Heod4283e92012-08-03 10:30:44 -07003035bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork,
3036 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037{
Tejun Heod320c032010-06-29 10:07:14 +02003038 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039}
Dave Jonesae90dd52006-06-30 01:40:45 -04003040EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
Andrew Mortonb6136772006-06-25 05:47:49 -07003042/**
Tejun Heo0a13c002012-08-03 10:30:44 -07003043 * schedule_delayed_work - put work task in global workqueue after delay
3044 * @dwork: job to be done
3045 * @delay: number of jiffies to wait or 0 for immediate execution
3046 *
3047 * After waiting for a given time this puts a job in the kernel-global
3048 * workqueue.
3049 */
Tejun Heod4283e92012-08-03 10:30:44 -07003050bool schedule_delayed_work(struct delayed_work *dwork, unsigned long delay)
Tejun Heo0a13c002012-08-03 10:30:44 -07003051{
3052 return queue_delayed_work(system_wq, dwork, delay);
3053}
3054EXPORT_SYMBOL(schedule_delayed_work);
3055
3056/**
Tejun Heo31ddd872010-10-19 11:14:49 +02003057 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07003058 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07003059 *
Tejun Heo31ddd872010-10-19 11:14:49 +02003060 * schedule_on_each_cpu() executes @func on each online CPU using the
3061 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07003062 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02003063 *
3064 * RETURNS:
3065 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07003066 */
David Howells65f27f32006-11-22 14:55:48 +00003067int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08003068{
3069 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02003070 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08003071
Andrew Mortonb6136772006-06-25 05:47:49 -07003072 works = alloc_percpu(struct work_struct);
3073 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08003074 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07003075
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003076 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08003077
Christoph Lameter15316ba2006-01-08 01:00:43 -08003078 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01003079 struct work_struct *work = per_cpu_ptr(works, cpu);
3080
3081 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003082 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02003083 }
Tejun Heo93981802009-11-17 14:06:20 -08003084
3085 for_each_online_cpu(cpu)
3086 flush_work(per_cpu_ptr(works, cpu));
3087
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003088 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07003089 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08003090 return 0;
3091}
3092
Alan Sterneef6a7d2010-02-12 17:39:21 +09003093/**
3094 * flush_scheduled_work - ensure that any scheduled work has run to completion.
3095 *
3096 * Forces execution of the kernel-global workqueue and blocks until its
3097 * completion.
3098 *
3099 * Think twice before calling this function! It's very easy to get into
3100 * trouble if you don't take great care. Either of the following situations
3101 * will lead to deadlock:
3102 *
3103 * One of the work items currently on the workqueue needs to acquire
3104 * a lock held by your code or its caller.
3105 *
3106 * Your code is running in the context of a work routine.
3107 *
3108 * They will be detected by lockdep when they occur, but the first might not
3109 * occur very often. It depends on what work items are on the workqueue and
3110 * what locks they need, which you have no control over.
3111 *
3112 * In most situations flushing the entire workqueue is overkill; you merely
3113 * need to know that a particular work item isn't queued and isn't running.
3114 * In such cases you should use cancel_delayed_work_sync() or
3115 * cancel_work_sync() instead.
3116 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117void flush_scheduled_work(void)
3118{
Tejun Heod320c032010-06-29 10:07:14 +02003119 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120}
Dave Jonesae90dd52006-06-30 01:40:45 -04003121EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
3123/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06003124 * execute_in_process_context - reliably execute the routine with user context
3125 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06003126 * @ew: guaranteed storage for the execute work structure (must
3127 * be available when the work executes)
3128 *
3129 * Executes the function immediately if process context is available,
3130 * otherwise schedules the function for delayed execution.
3131 *
3132 * Returns: 0 - function was executed
3133 * 1 - function was scheduled for execution
3134 */
David Howells65f27f32006-11-22 14:55:48 +00003135int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06003136{
3137 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00003138 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003139 return 0;
3140 }
3141
David Howells65f27f32006-11-22 14:55:48 +00003142 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003143 schedule_work(&ew->work);
3144
3145 return 1;
3146}
3147EXPORT_SYMBOL_GPL(execute_in_process_context);
3148
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149int keventd_up(void)
3150{
Tejun Heod320c032010-06-29 10:07:14 +02003151 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152}
3153
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003154static int alloc_cwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155{
Oleg Nesterov3af244332007-05-09 02:34:09 -07003156 /*
Tejun Heo0f900042010-06-29 10:07:11 +02003157 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
3158 * Make sure that the alignment isn't lower than that of
3159 * unsigned long long.
Oleg Nesterov3af244332007-05-09 02:34:09 -07003160 */
Tejun Heo0f900042010-06-29 10:07:11 +02003161 const size_t size = sizeof(struct cpu_workqueue_struct);
3162 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
3163 __alignof__(unsigned long long));
Oleg Nesterov3af244332007-05-09 02:34:09 -07003164
Lai Jiangshane06ffa12012-03-09 18:03:20 +08003165 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02003166 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
Tejun Heo931ac772010-07-20 11:07:48 +02003167 else {
Tejun Heof3421792010-07-02 10:03:51 +02003168 void *ptr;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01003169
Tejun Heof3421792010-07-02 10:03:51 +02003170 /*
3171 * Allocate enough room to align cwq and put an extra
3172 * pointer at the end pointing back to the originally
3173 * allocated pointer which will be used for free.
3174 */
3175 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
3176 if (ptr) {
3177 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
3178 *(void **)(wq->cpu_wq.single + 1) = ptr;
3179 }
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003180 }
Tejun Heof3421792010-07-02 10:03:51 +02003181
Tejun Heo0415b00d12011-03-24 18:50:09 +01003182 /* just in case, make sure it's actually aligned */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003183 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
3184 return wq->cpu_wq.v ? 0 : -ENOMEM;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003185}
3186
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003187static void free_cwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07003188{
Lai Jiangshane06ffa12012-03-09 18:03:20 +08003189 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02003190 free_percpu(wq->cpu_wq.pcpu);
3191 else if (wq->cpu_wq.single) {
3192 /* the pointer to free is stored right after the cwq */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003193 kfree(*(void **)(wq->cpu_wq.single + 1));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07003194 }
3195}
3196
Tejun Heof3421792010-07-02 10:03:51 +02003197static int wq_clamp_max_active(int max_active, unsigned int flags,
3198 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003199{
Tejun Heof3421792010-07-02 10:03:51 +02003200 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3201
3202 if (max_active < 1 || max_active > lim)
Valentin Ilie044c7822012-08-19 00:52:42 +03003203 pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
3204 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003205
Tejun Heof3421792010-07-02 10:03:51 +02003206 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003207}
3208
Tejun Heob196be82012-01-10 15:11:35 -08003209struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02003210 unsigned int flags,
3211 int max_active,
3212 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08003213 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003214{
Tejun Heob196be82012-01-10 15:11:35 -08003215 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003216 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02003217 unsigned int cpu;
Tejun Heob196be82012-01-10 15:11:35 -08003218 size_t namelen;
3219
3220 /* determine namelen, allocate wq and format name */
3221 va_start(args, lock_name);
3222 va_copy(args1, args);
3223 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3224
3225 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3226 if (!wq)
3227 goto err;
3228
3229 vsnprintf(wq->name, namelen, fmt, args1);
3230 va_end(args);
3231 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003232
Tejun Heof3421792010-07-02 10:03:51 +02003233 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02003234 * Workqueues which may be used during memory reclaim should
3235 * have a rescuer to guarantee forward progress.
3236 */
3237 if (flags & WQ_MEM_RECLAIM)
3238 flags |= WQ_RESCUER;
3239
Tejun Heod320c032010-06-29 10:07:14 +02003240 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003241 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003242
Tejun Heob196be82012-01-10 15:11:35 -08003243 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003244 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003245 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003246 mutex_init(&wq->flush_mutex);
3247 atomic_set(&wq->nr_cwqs_to_flush, 0);
3248 INIT_LIST_HEAD(&wq->flusher_queue);
3249 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003250
Johannes Bergeb13ba82008-01-16 09:51:58 +01003251 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003252 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003253
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003254 if (alloc_cwqs(wq) < 0)
3255 goto err;
3256
Tejun Heof3421792010-07-02 10:03:51 +02003257 for_each_cwq_cpu(cpu, wq) {
Tejun Heo15376632010-06-29 10:07:11 +02003258 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003259 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo32704762012-07-13 22:16:45 -07003260 int pool_idx = (bool)(flags & WQ_HIGHPRI);
Tejun Heo15376632010-06-29 10:07:11 +02003261
Tejun Heo0f900042010-06-29 10:07:11 +02003262 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heo32704762012-07-13 22:16:45 -07003263 cwq->pool = &gcwq->pools[pool_idx];
Tejun Heoc34056a2010-06-29 10:07:11 +02003264 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02003265 cwq->flush_color = -1;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003266 cwq->max_active = max_active;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003267 INIT_LIST_HEAD(&cwq->delayed_works);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003268 }
3269
Tejun Heoe22bee72010-06-29 10:07:14 +02003270 if (flags & WQ_RESCUER) {
3271 struct worker *rescuer;
3272
Tejun Heof2e005a2010-07-20 15:59:09 +02003273 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
Tejun Heoe22bee72010-06-29 10:07:14 +02003274 goto err;
3275
3276 wq->rescuer = rescuer = alloc_worker();
3277 if (!rescuer)
3278 goto err;
3279
Tejun Heob196be82012-01-10 15:11:35 -08003280 rescuer->task = kthread_create(rescuer_thread, wq, "%s",
3281 wq->name);
Tejun Heoe22bee72010-06-29 10:07:14 +02003282 if (IS_ERR(rescuer->task))
3283 goto err;
3284
Tejun Heoe22bee72010-06-29 10:07:14 +02003285 rescuer->task->flags |= PF_THREAD_BOUND;
3286 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003287 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003288
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003289 /*
3290 * workqueue_lock protects global freeze state and workqueues
3291 * list. Grab it, set max_active accordingly and add the new
3292 * workqueue to workqueues list.
3293 */
Tejun Heo15376632010-06-29 10:07:11 +02003294 spin_lock(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003295
Tejun Heo58a69cb2011-02-16 09:25:31 +01003296 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heof3421792010-07-02 10:03:51 +02003297 for_each_cwq_cpu(cpu, wq)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003298 get_cwq(cpu, wq)->max_active = 0;
3299
Tejun Heo15376632010-06-29 10:07:11 +02003300 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003301
Tejun Heo15376632010-06-29 10:07:11 +02003302 spin_unlock(&workqueue_lock);
3303
Oleg Nesterov3af244332007-05-09 02:34:09 -07003304 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02003305err:
3306 if (wq) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003307 free_cwqs(wq);
Tejun Heof2e005a2010-07-20 15:59:09 +02003308 free_mayday_mask(wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02003309 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003310 kfree(wq);
3311 }
3312 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003313}
Tejun Heod320c032010-06-29 10:07:14 +02003314EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003315
3316/**
3317 * destroy_workqueue - safely terminate a workqueue
3318 * @wq: target workqueue
3319 *
3320 * Safely destroy a workqueue. All work currently pending will be done first.
3321 */
3322void destroy_workqueue(struct workqueue_struct *wq)
3323{
Tejun Heoc8e55f32010-06-29 10:07:12 +02003324 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003325
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003326 /* drain it before proceeding with destruction */
3327 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003328
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003329 /*
3330 * wq list is used to freeze wq, remove from list after
3331 * flushing is complete in case freeze races us.
3332 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003333 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07003334 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003335 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003336
Tejun Heoe22bee72010-06-29 10:07:14 +02003337 /* sanity check */
Tejun Heof3421792010-07-02 10:03:51 +02003338 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02003339 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3340 int i;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003341
Tejun Heo73f53c42010-06-29 10:07:11 +02003342 for (i = 0; i < WORK_NR_COLORS; i++)
3343 BUG_ON(cwq->nr_in_flight[i]);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003344 BUG_ON(cwq->nr_active);
3345 BUG_ON(!list_empty(&cwq->delayed_works));
Tejun Heo73f53c42010-06-29 10:07:11 +02003346 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003347
Tejun Heoe22bee72010-06-29 10:07:14 +02003348 if (wq->flags & WQ_RESCUER) {
3349 kthread_stop(wq->rescuer->task);
Tejun Heof2e005a2010-07-20 15:59:09 +02003350 free_mayday_mask(wq->mayday_mask);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003351 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003352 }
3353
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003354 free_cwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003355 kfree(wq);
3356}
3357EXPORT_SYMBOL_GPL(destroy_workqueue);
3358
Tejun Heodcd989c2010-06-29 10:07:14 +02003359/**
3360 * workqueue_set_max_active - adjust max_active of a workqueue
3361 * @wq: target workqueue
3362 * @max_active: new max_active value.
3363 *
3364 * Set max_active of @wq to @max_active.
3365 *
3366 * CONTEXT:
3367 * Don't call from IRQ context.
3368 */
3369void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3370{
3371 unsigned int cpu;
3372
Tejun Heof3421792010-07-02 10:03:51 +02003373 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003374
3375 spin_lock(&workqueue_lock);
3376
3377 wq->saved_max_active = max_active;
3378
Tejun Heof3421792010-07-02 10:03:51 +02003379 for_each_cwq_cpu(cpu, wq) {
Tejun Heodcd989c2010-06-29 10:07:14 +02003380 struct global_cwq *gcwq = get_gcwq(cpu);
3381
3382 spin_lock_irq(&gcwq->lock);
3383
Tejun Heo58a69cb2011-02-16 09:25:31 +01003384 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heodcd989c2010-06-29 10:07:14 +02003385 !(gcwq->flags & GCWQ_FREEZING))
3386 get_cwq(gcwq->cpu, wq)->max_active = max_active;
3387
3388 spin_unlock_irq(&gcwq->lock);
3389 }
3390
3391 spin_unlock(&workqueue_lock);
3392}
3393EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3394
3395/**
3396 * workqueue_congested - test whether a workqueue is congested
3397 * @cpu: CPU in question
3398 * @wq: target workqueue
3399 *
3400 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3401 * no synchronization around this function and the test result is
3402 * unreliable and only useful as advisory hints or for debugging.
3403 *
3404 * RETURNS:
3405 * %true if congested, %false otherwise.
3406 */
3407bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3408{
3409 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3410
3411 return !list_empty(&cwq->delayed_works);
3412}
3413EXPORT_SYMBOL_GPL(workqueue_congested);
3414
3415/**
3416 * work_cpu - return the last known associated cpu for @work
3417 * @work: the work of interest
3418 *
3419 * RETURNS:
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003420 * CPU number if @work was ever queued. WORK_CPU_NONE otherwise.
Tejun Heodcd989c2010-06-29 10:07:14 +02003421 */
3422unsigned int work_cpu(struct work_struct *work)
3423{
3424 struct global_cwq *gcwq = get_work_gcwq(work);
3425
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003426 return gcwq ? gcwq->cpu : WORK_CPU_NONE;
Tejun Heodcd989c2010-06-29 10:07:14 +02003427}
3428EXPORT_SYMBOL_GPL(work_cpu);
3429
3430/**
3431 * work_busy - test whether a work is currently pending or running
3432 * @work: the work to be tested
3433 *
3434 * Test whether @work is currently pending or running. There is no
3435 * synchronization around this function and the test result is
3436 * unreliable and only useful as advisory hints or for debugging.
3437 * Especially for reentrant wqs, the pending state might hide the
3438 * running state.
3439 *
3440 * RETURNS:
3441 * OR'd bitmask of WORK_BUSY_* bits.
3442 */
3443unsigned int work_busy(struct work_struct *work)
3444{
3445 struct global_cwq *gcwq = get_work_gcwq(work);
3446 unsigned long flags;
3447 unsigned int ret = 0;
3448
3449 if (!gcwq)
3450 return false;
3451
3452 spin_lock_irqsave(&gcwq->lock, flags);
3453
3454 if (work_pending(work))
3455 ret |= WORK_BUSY_PENDING;
3456 if (find_worker_executing_work(gcwq, work))
3457 ret |= WORK_BUSY_RUNNING;
3458
3459 spin_unlock_irqrestore(&gcwq->lock, flags);
3460
3461 return ret;
3462}
3463EXPORT_SYMBOL_GPL(work_busy);
3464
Tejun Heodb7bccf2010-06-29 10:07:12 +02003465/*
3466 * CPU hotplug.
3467 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003468 * There are two challenges in supporting CPU hotplug. Firstly, there
3469 * are a lot of assumptions on strong associations among work, cwq and
3470 * gcwq which make migrating pending and scheduled works very
3471 * difficult to implement without impacting hot paths. Secondly,
3472 * gcwqs serve mix of short, long and very long running works making
3473 * blocked draining impractical.
3474 *
Tejun Heo628c78e2012-07-17 12:39:27 -07003475 * This is solved by allowing a gcwq to be disassociated from the CPU
3476 * running as an unbound one and allowing it to be reattached later if the
3477 * cpu comes back online.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003478 */
3479
Tejun Heo60373152012-07-17 12:39:27 -07003480/* claim manager positions of all pools */
Tejun Heo8db25e72012-07-17 12:39:28 -07003481static void gcwq_claim_management_and_lock(struct global_cwq *gcwq)
Tejun Heo60373152012-07-17 12:39:27 -07003482{
3483 struct worker_pool *pool;
3484
3485 for_each_worker_pool(pool, gcwq)
3486 mutex_lock_nested(&pool->manager_mutex, pool - gcwq->pools);
Tejun Heo8db25e72012-07-17 12:39:28 -07003487 spin_lock_irq(&gcwq->lock);
Tejun Heo60373152012-07-17 12:39:27 -07003488}
3489
3490/* release manager positions */
Tejun Heo8db25e72012-07-17 12:39:28 -07003491static void gcwq_release_management_and_unlock(struct global_cwq *gcwq)
Tejun Heo60373152012-07-17 12:39:27 -07003492{
3493 struct worker_pool *pool;
3494
Tejun Heo8db25e72012-07-17 12:39:28 -07003495 spin_unlock_irq(&gcwq->lock);
Tejun Heo60373152012-07-17 12:39:27 -07003496 for_each_worker_pool(pool, gcwq)
3497 mutex_unlock(&pool->manager_mutex);
3498}
3499
Tejun Heo628c78e2012-07-17 12:39:27 -07003500static void gcwq_unbind_fn(struct work_struct *work)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003501{
Tejun Heo628c78e2012-07-17 12:39:27 -07003502 struct global_cwq *gcwq = get_gcwq(smp_processor_id());
Tejun Heo4ce62e92012-07-13 22:16:44 -07003503 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003504 struct worker *worker;
3505 struct hlist_node *pos;
3506 int i;
3507
3508 BUG_ON(gcwq->cpu != smp_processor_id());
3509
Tejun Heo8db25e72012-07-17 12:39:28 -07003510 gcwq_claim_management_and_lock(gcwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02003511
Tejun Heof2d5a0e2012-07-17 12:39:26 -07003512 /*
3513 * We've claimed all manager positions. Make all workers unbound
3514 * and set DISASSOCIATED. Before this, all workers except for the
3515 * ones which are still executing works from before the last CPU
3516 * down must be on the cpu. After this, they may become diasporas.
3517 */
Tejun Heo60373152012-07-17 12:39:27 -07003518 for_each_worker_pool(pool, gcwq)
Tejun Heo4ce62e92012-07-13 22:16:44 -07003519 list_for_each_entry(worker, &pool->idle_list, entry)
Tejun Heo403c8212012-07-17 12:39:27 -07003520 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003521
3522 for_each_busy_worker(worker, i, pos, gcwq)
Tejun Heo403c8212012-07-17 12:39:27 -07003523 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003524
Tejun Heof2d5a0e2012-07-17 12:39:26 -07003525 gcwq->flags |= GCWQ_DISASSOCIATED;
3526
Tejun Heo8db25e72012-07-17 12:39:28 -07003527 gcwq_release_management_and_unlock(gcwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02003528
3529 /*
Tejun Heo628c78e2012-07-17 12:39:27 -07003530 * Call schedule() so that we cross rq->lock and thus can guarantee
3531 * sched callbacks see the %WORKER_UNBOUND flag. This is necessary
3532 * as scheduler callbacks may be invoked from other cpus.
3533 */
3534 schedule();
3535
3536 /*
3537 * Sched callbacks are disabled now. Zap nr_running. After this,
3538 * nr_running stays zero and need_more_worker() and keep_working()
3539 * are always true as long as the worklist is not empty. @gcwq now
3540 * behaves as unbound (in terms of concurrency management) gcwq
3541 * which is served by workers tied to the CPU.
3542 *
3543 * On return from this function, the current worker would trigger
3544 * unbound chain execution of pending work items if other workers
3545 * didn't already.
Tejun Heoe22bee72010-06-29 10:07:14 +02003546 */
Tejun Heo4ce62e92012-07-13 22:16:44 -07003547 for_each_worker_pool(pool, gcwq)
3548 atomic_set(get_pool_nr_running(pool), 0);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003549}
3550
Tejun Heo8db25e72012-07-17 12:39:28 -07003551/*
3552 * Workqueues should be brought up before normal priority CPU notifiers.
3553 * This will be registered high priority CPU notifier.
3554 */
3555static int __devinit workqueue_cpu_up_callback(struct notifier_block *nfb,
3556 unsigned long action,
3557 void *hcpu)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003558{
3559 unsigned int cpu = (unsigned long)hcpu;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003560 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003561 struct worker_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562
Tejun Heo8db25e72012-07-17 12:39:28 -07003563 switch (action & ~CPU_TASKS_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 case CPU_UP_PREPARE:
Tejun Heo4ce62e92012-07-13 22:16:44 -07003565 for_each_worker_pool(pool, gcwq) {
Tejun Heo3ce63372012-07-17 12:39:27 -07003566 struct worker *worker;
3567
3568 if (pool->nr_workers)
3569 continue;
3570
3571 worker = create_worker(pool);
3572 if (!worker)
3573 return NOTIFY_BAD;
3574
3575 spin_lock_irq(&gcwq->lock);
3576 start_worker(worker);
3577 spin_unlock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003579 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003580
Tejun Heo65758202012-07-17 12:39:26 -07003581 case CPU_DOWN_FAILED:
3582 case CPU_ONLINE:
Tejun Heo8db25e72012-07-17 12:39:28 -07003583 gcwq_claim_management_and_lock(gcwq);
3584 gcwq->flags &= ~GCWQ_DISASSOCIATED;
3585 rebind_workers(gcwq);
3586 gcwq_release_management_and_unlock(gcwq);
3587 break;
Tejun Heo65758202012-07-17 12:39:26 -07003588 }
3589 return NOTIFY_OK;
3590}
3591
3592/*
3593 * Workqueues should be brought down after normal priority CPU notifiers.
3594 * This will be registered as low priority CPU notifier.
3595 */
3596static int __devinit workqueue_cpu_down_callback(struct notifier_block *nfb,
3597 unsigned long action,
3598 void *hcpu)
3599{
Tejun Heo8db25e72012-07-17 12:39:28 -07003600 unsigned int cpu = (unsigned long)hcpu;
3601 struct work_struct unbind_work;
3602
Tejun Heo65758202012-07-17 12:39:26 -07003603 switch (action & ~CPU_TASKS_FROZEN) {
3604 case CPU_DOWN_PREPARE:
Tejun Heo8db25e72012-07-17 12:39:28 -07003605 /* unbinding should happen on the local CPU */
3606 INIT_WORK_ONSTACK(&unbind_work, gcwq_unbind_fn);
Joonsoo Kim7635d2f2012-08-15 23:25:41 +09003607 queue_work_on(cpu, system_highpri_wq, &unbind_work);
Tejun Heo8db25e72012-07-17 12:39:28 -07003608 flush_work(&unbind_work);
3609 break;
Tejun Heo65758202012-07-17 12:39:26 -07003610 }
3611 return NOTIFY_OK;
3612}
3613
Rusty Russell2d3854a2008-11-05 13:39:10 +11003614#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003615
Rusty Russell2d3854a2008-11-05 13:39:10 +11003616struct work_for_cpu {
Andrew Morton6b440032009-04-09 09:50:37 -06003617 struct completion completion;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003618 long (*fn)(void *);
3619 void *arg;
3620 long ret;
3621};
3622
Andrew Morton6b440032009-04-09 09:50:37 -06003623static int do_work_for_cpu(void *_wfc)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003624{
Andrew Morton6b440032009-04-09 09:50:37 -06003625 struct work_for_cpu *wfc = _wfc;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003626 wfc->ret = wfc->fn(wfc->arg);
Andrew Morton6b440032009-04-09 09:50:37 -06003627 complete(&wfc->completion);
3628 return 0;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003629}
3630
3631/**
3632 * work_on_cpu - run a function in user context on a particular cpu
3633 * @cpu: the cpu to run on
3634 * @fn: the function to run
3635 * @arg: the function arg
3636 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003637 * This will return the value @fn returns.
3638 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06003639 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003640 */
3641long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3642{
Andrew Morton6b440032009-04-09 09:50:37 -06003643 struct task_struct *sub_thread;
3644 struct work_for_cpu wfc = {
3645 .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
3646 .fn = fn,
3647 .arg = arg,
3648 };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003649
Andrew Morton6b440032009-04-09 09:50:37 -06003650 sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
3651 if (IS_ERR(sub_thread))
3652 return PTR_ERR(sub_thread);
3653 kthread_bind(sub_thread, cpu);
3654 wake_up_process(sub_thread);
3655 wait_for_completion(&wfc.completion);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003656 return wfc.ret;
3657}
3658EXPORT_SYMBOL_GPL(work_on_cpu);
3659#endif /* CONFIG_SMP */
3660
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003661#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303662
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003663/**
3664 * freeze_workqueues_begin - begin freezing workqueues
3665 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003666 * Start freezing workqueues. After this function returns, all freezable
3667 * workqueues will queue new works to their frozen_works list instead of
3668 * gcwq->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003669 *
3670 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003671 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003672 */
3673void freeze_workqueues_begin(void)
3674{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003675 unsigned int cpu;
3676
3677 spin_lock(&workqueue_lock);
3678
3679 BUG_ON(workqueue_freezing);
3680 workqueue_freezing = true;
3681
Tejun Heof3421792010-07-02 10:03:51 +02003682 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003683 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003684 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003685
3686 spin_lock_irq(&gcwq->lock);
3687
Tejun Heodb7bccf2010-06-29 10:07:12 +02003688 BUG_ON(gcwq->flags & GCWQ_FREEZING);
3689 gcwq->flags |= GCWQ_FREEZING;
3690
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003691 list_for_each_entry(wq, &workqueues, list) {
3692 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3693
Tejun Heo58a69cb2011-02-16 09:25:31 +01003694 if (cwq && wq->flags & WQ_FREEZABLE)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003695 cwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003696 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003697
3698 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003699 }
3700
3701 spin_unlock(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003703
3704/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003705 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003706 *
3707 * Check whether freezing is complete. This function must be called
3708 * between freeze_workqueues_begin() and thaw_workqueues().
3709 *
3710 * CONTEXT:
3711 * Grabs and releases workqueue_lock.
3712 *
3713 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01003714 * %true if some freezable workqueues are still busy. %false if freezing
3715 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003716 */
3717bool freeze_workqueues_busy(void)
3718{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003719 unsigned int cpu;
3720 bool busy = false;
3721
3722 spin_lock(&workqueue_lock);
3723
3724 BUG_ON(!workqueue_freezing);
3725
Tejun Heof3421792010-07-02 10:03:51 +02003726 for_each_gcwq_cpu(cpu) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003727 struct workqueue_struct *wq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003728 /*
3729 * nr_active is monotonically decreasing. It's safe
3730 * to peek without lock.
3731 */
3732 list_for_each_entry(wq, &workqueues, list) {
3733 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3734
Tejun Heo58a69cb2011-02-16 09:25:31 +01003735 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003736 continue;
3737
3738 BUG_ON(cwq->nr_active < 0);
3739 if (cwq->nr_active) {
3740 busy = true;
3741 goto out_unlock;
3742 }
3743 }
3744 }
3745out_unlock:
3746 spin_unlock(&workqueue_lock);
3747 return busy;
3748}
3749
3750/**
3751 * thaw_workqueues - thaw workqueues
3752 *
3753 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo7e116292010-06-29 10:07:13 +02003754 * frozen works are transferred to their respective gcwq worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003755 *
3756 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003757 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003758 */
3759void thaw_workqueues(void)
3760{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003761 unsigned int cpu;
3762
3763 spin_lock(&workqueue_lock);
3764
3765 if (!workqueue_freezing)
3766 goto out_unlock;
3767
Tejun Heof3421792010-07-02 10:03:51 +02003768 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003769 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003770 struct worker_pool *pool;
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003771 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003772
3773 spin_lock_irq(&gcwq->lock);
3774
Tejun Heodb7bccf2010-06-29 10:07:12 +02003775 BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
3776 gcwq->flags &= ~GCWQ_FREEZING;
3777
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003778 list_for_each_entry(wq, &workqueues, list) {
3779 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3780
Tejun Heo58a69cb2011-02-16 09:25:31 +01003781 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003782 continue;
3783
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003784 /* restore max_active and repopulate worklist */
3785 cwq->max_active = wq->saved_max_active;
3786
3787 while (!list_empty(&cwq->delayed_works) &&
3788 cwq->nr_active < cwq->max_active)
3789 cwq_activate_first_delayed(cwq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003790 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003791
Tejun Heo4ce62e92012-07-13 22:16:44 -07003792 for_each_worker_pool(pool, gcwq)
3793 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02003794
Tejun Heo8b03ae32010-06-29 10:07:12 +02003795 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003796 }
3797
3798 workqueue_freezing = false;
3799out_unlock:
3800 spin_unlock(&workqueue_lock);
3801}
3802#endif /* CONFIG_FREEZER */
3803
Suresh Siddha6ee05782010-07-30 14:57:37 -07003804static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805{
Tejun Heoc34056a2010-06-29 10:07:11 +02003806 unsigned int cpu;
Tejun Heoc8e55f32010-06-29 10:07:12 +02003807 int i;
Tejun Heoc34056a2010-06-29 10:07:11 +02003808
Tejun Heob5490072012-08-03 10:30:46 -07003809 /* make sure we have enough bits for OFFQ CPU number */
3810 BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_CPU_SHIFT)) <
3811 WORK_CPU_LAST);
3812
Tejun Heo65758202012-07-17 12:39:26 -07003813 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
3814 cpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003815
3816 /* initialize gcwqs */
Tejun Heof3421792010-07-02 10:03:51 +02003817 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003818 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003819 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003820
3821 spin_lock_init(&gcwq->lock);
3822 gcwq->cpu = cpu;
Tejun Heo477a3c32010-08-31 10:54:35 +02003823 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003824
Tejun Heoc8e55f32010-06-29 10:07:12 +02003825 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
3826 INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
3827
Tejun Heo4ce62e92012-07-13 22:16:44 -07003828 for_each_worker_pool(pool, gcwq) {
3829 pool->gcwq = gcwq;
3830 INIT_LIST_HEAD(&pool->worklist);
3831 INIT_LIST_HEAD(&pool->idle_list);
Tejun Heoe22bee72010-06-29 10:07:14 +02003832
Tejun Heo4ce62e92012-07-13 22:16:44 -07003833 init_timer_deferrable(&pool->idle_timer);
3834 pool->idle_timer.function = idle_worker_timeout;
3835 pool->idle_timer.data = (unsigned long)pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003836
Tejun Heo4ce62e92012-07-13 22:16:44 -07003837 setup_timer(&pool->mayday_timer, gcwq_mayday_timeout,
3838 (unsigned long)pool);
3839
Tejun Heo60373152012-07-17 12:39:27 -07003840 mutex_init(&pool->manager_mutex);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003841 ida_init(&pool->worker_ida);
3842 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003843
Tejun Heo25511a42012-07-17 12:39:27 -07003844 init_waitqueue_head(&gcwq->rebind_hold);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003845 }
3846
Tejun Heoe22bee72010-06-29 10:07:14 +02003847 /* create the initial worker */
Tejun Heof3421792010-07-02 10:03:51 +02003848 for_each_online_gcwq_cpu(cpu) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003849 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003850 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003851
Tejun Heo477a3c32010-08-31 10:54:35 +02003852 if (cpu != WORK_CPU_UNBOUND)
3853 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003854
3855 for_each_worker_pool(pool, gcwq) {
3856 struct worker *worker;
3857
Tejun Heobc2ae0f2012-07-17 12:39:27 -07003858 worker = create_worker(pool);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003859 BUG_ON(!worker);
3860 spin_lock_irq(&gcwq->lock);
3861 start_worker(worker);
3862 spin_unlock_irq(&gcwq->lock);
3863 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003864 }
3865
Tejun Heod320c032010-06-29 10:07:14 +02003866 system_wq = alloc_workqueue("events", 0, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09003867 system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
Tejun Heod320c032010-06-29 10:07:14 +02003868 system_long_wq = alloc_workqueue("events_long", 0, 0);
Tejun Heof3421792010-07-02 10:03:51 +02003869 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3870 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01003871 system_freezable_wq = alloc_workqueue("events_freezable",
3872 WQ_FREEZABLE, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09003873 BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
Tejun Heoae930e02012-08-20 14:51:23 -07003874 !system_unbound_wq || !system_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07003875 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876}
Suresh Siddha6ee05782010-07-30 14:57:37 -07003877early_initcall(init_workqueues);