blob: 322d29dd24a28f2cf635b725284b24337fc54383 [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>
Syed Rameez Mustafa1bee7b92013-07-15 11:52:09 -070044#include <linux/bug.h>
Tejun Heoe22bee72010-06-29 10:07:14 +020045
46#include "workqueue_sched.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Tejun Heoc8e55f32010-06-29 10:07:12 +020048enum {
Tejun Heodb7bccf2010-06-29 10:07:12 +020049 /* global_cwq flags */
Tejun Heo22ad5642012-07-12 14:46:37 -070050 GCWQ_DISASSOCIATED = 1 << 0, /* cpu can't serve workers */
51 GCWQ_FREEZING = 1 << 1, /* freeze in progress */
52
53 /* pool flags */
54 POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
55 POOL_MANAGING_WORKERS = 1 << 1, /* managing workers */
Tejun Heodb7bccf2010-06-29 10:07:12 +020056
Tejun Heoc8e55f32010-06-29 10:07:12 +020057 /* worker flags */
58 WORKER_STARTED = 1 << 0, /* started */
59 WORKER_DIE = 1 << 1, /* die die die */
60 WORKER_IDLE = 1 << 2, /* is idle */
Tejun Heoe22bee72010-06-29 10:07:14 +020061 WORKER_PREP = 1 << 3, /* preparing to run works */
Tejun Heodb7bccf2010-06-29 10:07:12 +020062 WORKER_ROGUE = 1 << 4, /* not bound to any cpu */
Tejun Heoe22bee72010-06-29 10:07:14 +020063 WORKER_REBIND = 1 << 5, /* mom is home, come back */
Tejun Heofb0e7be2010-06-29 10:07:15 +020064 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
Tejun Heof3421792010-07-02 10:03:51 +020065 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
Tejun Heoe22bee72010-06-29 10:07:14 +020066
Tejun Heofb0e7be2010-06-29 10:07:15 +020067 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_ROGUE | WORKER_REBIND |
Tejun Heof3421792010-07-02 10:03:51 +020068 WORKER_CPU_INTENSIVE | WORKER_UNBOUND,
Tejun Heodb7bccf2010-06-29 10:07:12 +020069
70 /* gcwq->trustee_state */
71 TRUSTEE_START = 0, /* start */
72 TRUSTEE_IN_CHARGE = 1, /* trustee in charge of gcwq */
73 TRUSTEE_BUTCHER = 2, /* butcher workers */
74 TRUSTEE_RELEASE = 3, /* release workers */
75 TRUSTEE_DONE = 4, /* trustee is done */
Tejun Heoc8e55f32010-06-29 10:07:12 +020076
Tejun Heodcb32ee2012-07-13 22:16:45 -070077 NR_WORKER_POOLS = 2, /* # worker pools per gcwq */
Tejun Heo9c6bae02012-07-13 22:16:44 -070078
Tejun Heoc8e55f32010-06-29 10:07:12 +020079 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
80 BUSY_WORKER_HASH_SIZE = 1 << BUSY_WORKER_HASH_ORDER,
81 BUSY_WORKER_HASH_MASK = BUSY_WORKER_HASH_SIZE - 1,
Tejun Heodb7bccf2010-06-29 10:07:12 +020082
Tejun Heoe22bee72010-06-29 10:07:14 +020083 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
84 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
85
Tejun Heo3233cdb2011-02-16 18:10:19 +010086 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
87 /* call for help after 10ms
88 (min two ticks) */
Tejun Heoe22bee72010-06-29 10:07:14 +020089 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
90 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heodb7bccf2010-06-29 10:07:12 +020091 TRUSTEE_COOLDOWN = HZ / 10, /* for trustee draining */
Tejun Heoe22bee72010-06-29 10:07:14 +020092
93 /*
94 * Rescue workers are used only on emergencies and shared by
95 * all cpus. Give -20.
96 */
97 RESCUER_NICE_LEVEL = -20,
Tejun Heodcb32ee2012-07-13 22:16:45 -070098 HIGHPRI_NICE_LEVEL = -20,
Tejun Heoc8e55f32010-06-29 10:07:12 +020099};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101/*
Tejun Heo4690c4a2010-06-29 10:07:10 +0200102 * Structure fields follow one of the following exclusion rules.
103 *
Tejun Heoe41e7042010-08-24 14:22:47 +0200104 * I: Modifiable by initialization/destruction paths and read-only for
105 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200106 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200107 * P: Preemption protected. Disabling preemption is enough and should
108 * only be modified and accessed from the local cpu.
109 *
Tejun Heo8b03ae32010-06-29 10:07:12 +0200110 * L: gcwq->lock protected. Access with gcwq->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200111 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200112 * X: During normal operation, modification requires gcwq->lock and
113 * should be done only from local cpu. Either disabling preemption
114 * on local cpu or grabbing gcwq->lock is enough for read access.
Tejun Heof3421792010-07-02 10:03:51 +0200115 * If GCWQ_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200116 *
Tejun Heo73f53c42010-06-29 10:07:11 +0200117 * F: wq->flush_mutex protected.
118 *
Tejun Heo4690c4a2010-06-29 10:07:10 +0200119 * W: workqueue_lock protected.
120 */
121
Tejun Heo8b03ae32010-06-29 10:07:12 +0200122struct global_cwq;
Tejun Heo58658882012-07-12 14:46:37 -0700123struct worker_pool;
Tejun Heoc34056a2010-06-29 10:07:11 +0200124
Tejun Heoe22bee72010-06-29 10:07:14 +0200125/*
126 * The poor guys doing the actual heavy lifting. All on-duty workers
127 * are either serving the manager role, on idle list or on busy hash.
128 */
Tejun Heoc34056a2010-06-29 10:07:11 +0200129struct worker {
Tejun Heoc8e55f32010-06-29 10:07:12 +0200130 /* on idle list while idle, on busy hash table while busy */
131 union {
132 struct list_head entry; /* L: while idle */
133 struct hlist_node hentry; /* L: while busy */
134 };
135
Tejun Heoc34056a2010-06-29 10:07:11 +0200136 struct work_struct *current_work; /* L: work being processed */
Tejun Heo55e3e1f2012-12-18 10:35:02 -0800137 work_func_t current_func; /* L: current_work's fn */
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200138 struct cpu_workqueue_struct *current_cwq; /* L: current_work's cwq */
Tejun Heoaffee4b2010-06-29 10:07:12 +0200139 struct list_head scheduled; /* L: scheduled works */
Tejun Heoc34056a2010-06-29 10:07:11 +0200140 struct task_struct *task; /* I: worker task */
Tejun Heo58658882012-07-12 14:46:37 -0700141 struct worker_pool *pool; /* I: the associated pool */
Tejun Heoe22bee72010-06-29 10:07:14 +0200142 /* 64 bytes boundary on 64bit, 32 on 32bit */
143 unsigned long last_active; /* L: last active timestamp */
144 unsigned int flags; /* X: flags */
Tejun Heoc34056a2010-06-29 10:07:11 +0200145 int id; /* I: worker id */
Tejun Heoe22bee72010-06-29 10:07:14 +0200146 struct work_struct rebind_work; /* L: rebind worker to cpu */
Tejun Heoc34056a2010-06-29 10:07:11 +0200147};
148
Tejun Heo58658882012-07-12 14:46:37 -0700149struct worker_pool {
150 struct global_cwq *gcwq; /* I: the owning gcwq */
Tejun Heo22ad5642012-07-12 14:46:37 -0700151 unsigned int flags; /* X: flags */
Tejun Heo58658882012-07-12 14:46:37 -0700152
153 struct list_head worklist; /* L: list of pending works */
154 int nr_workers; /* L: total number of workers */
155 int nr_idle; /* L: currently idle ones */
156
157 struct list_head idle_list; /* X: list of idle workers */
158 struct timer_list idle_timer; /* L: worker idle timeout */
159 struct timer_list mayday_timer; /* L: SOS timer for workers */
160
161 struct ida worker_ida; /* L: for worker IDs */
162 struct worker *first_idle; /* L: first idle worker */
163};
164
Tejun Heo4690c4a2010-06-29 10:07:10 +0200165/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200166 * Global per-cpu workqueue. There's one and only one for each cpu
167 * and all works are queued and processed here regardless of their
168 * target workqueues.
Tejun Heo8b03ae32010-06-29 10:07:12 +0200169 */
170struct global_cwq {
171 spinlock_t lock; /* the gcwq lock */
172 unsigned int cpu; /* I: the associated cpu */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200173 unsigned int flags; /* L: GCWQ_* flags */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200174
Tejun Heo58658882012-07-12 14:46:37 -0700175 /* workers are chained either in busy_hash or pool idle_list */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200176 struct hlist_head busy_hash[BUSY_WORKER_HASH_SIZE];
177 /* L: hash of busy workers */
178
Tejun Heodcb32ee2012-07-13 22:16:45 -0700179 struct worker_pool pools[2]; /* normal and highpri pools */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200180
181 struct task_struct *trustee; /* L: for gcwq shutdown */
182 unsigned int trustee_state; /* L: trustee state */
183 wait_queue_head_t trustee_wait; /* trustee wait */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200184} ____cacheline_aligned_in_smp;
185
186/*
Tejun Heo502ca9d2010-06-29 10:07:13 +0200187 * The per-CPU workqueue. The lower WORK_STRUCT_FLAG_BITS of
Tejun Heo0f900042010-06-29 10:07:11 +0200188 * work_struct->data are used for flags and thus cwqs need to be
189 * aligned at two's power of the number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 */
191struct cpu_workqueue_struct {
Tejun Heo58658882012-07-12 14:46:37 -0700192 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200193 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200194 int work_color; /* L: current color */
195 int flush_color; /* L: flushing color */
196 int nr_in_flight[WORK_NR_COLORS];
197 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200198 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200199 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200200 struct list_head delayed_works; /* L: delayed works */
Tejun Heo0f900042010-06-29 10:07:11 +0200201};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200204 * Structure used to wait for workqueue flush.
205 */
206struct wq_flusher {
207 struct list_head list; /* F: list of flushers */
208 int flush_color; /* F: flush color waiting for */
209 struct completion done; /* flush completion */
210};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Tejun Heo73f53c42010-06-29 10:07:11 +0200212/*
Tejun Heof2e005a2010-07-20 15:59:09 +0200213 * All cpumasks are assumed to be always set on UP and thus can't be
214 * used to determine whether there's something to be done.
215 */
216#ifdef CONFIG_SMP
217typedef cpumask_var_t mayday_mask_t;
218#define mayday_test_and_set_cpu(cpu, mask) \
219 cpumask_test_and_set_cpu((cpu), (mask))
220#define mayday_clear_cpu(cpu, mask) cpumask_clear_cpu((cpu), (mask))
221#define for_each_mayday_cpu(cpu, mask) for_each_cpu((cpu), (mask))
Tejun Heo9c375472010-08-31 11:18:34 +0200222#define alloc_mayday_mask(maskp, gfp) zalloc_cpumask_var((maskp), (gfp))
Tejun Heof2e005a2010-07-20 15:59:09 +0200223#define free_mayday_mask(mask) free_cpumask_var((mask))
224#else
225typedef unsigned long mayday_mask_t;
226#define mayday_test_and_set_cpu(cpu, mask) test_and_set_bit(0, &(mask))
227#define mayday_clear_cpu(cpu, mask) clear_bit(0, &(mask))
228#define for_each_mayday_cpu(cpu, mask) if ((cpu) = 0, (mask))
229#define alloc_mayday_mask(maskp, gfp) true
230#define free_mayday_mask(mask) do { } while (0)
231#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233/*
234 * The externally visible workqueue abstraction is an array of
235 * per-CPU workqueues:
236 */
237struct workqueue_struct {
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200238 unsigned int flags; /* W: WQ_* flags */
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200239 union {
240 struct cpu_workqueue_struct __percpu *pcpu;
241 struct cpu_workqueue_struct *single;
242 unsigned long v;
243 } cpu_wq; /* I: cwq's */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200244 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200245
246 struct mutex flush_mutex; /* protects wq flushing */
247 int work_color; /* F: current work color */
248 int flush_color; /* F: current flush color */
249 atomic_t nr_cwqs_to_flush; /* flush in progress */
250 struct wq_flusher *first_flusher; /* F: first flusher */
251 struct list_head flusher_queue; /* F: flush waiters */
252 struct list_head flusher_overflow; /* F: flush overflow list */
253
Tejun Heof2e005a2010-07-20 15:59:09 +0200254 mayday_mask_t mayday_mask; /* cpus requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200255 struct worker *rescuer; /* I: rescue worker */
256
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200257 int nr_drainers; /* W: drain in progress */
Tejun Heodcd989c2010-06-29 10:07:14 +0200258 int saved_max_active; /* W: saved cwq max_active */
Johannes Berg4e6045f2007-10-18 23:39:55 -0700259#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200260 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700261#endif
Tejun Heob196be82012-01-10 15:11:35 -0800262 char name[]; /* I: workqueue name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263};
264
shumashb2f60df2015-07-18 09:03:08 -0600265/* see the comment above the definition of WQ_POWER_EFFICIENT */
266#ifdef CONFIG_WQ_POWER_EFFICIENT_DEFAULT
267static bool wq_power_efficient = true;
268#else
269static bool wq_power_efficient;
270#endif
271
272module_param_named(power_efficient, wq_power_efficient, bool, 0644);
273
Tejun Heod320c032010-06-29 10:07:14 +0200274struct workqueue_struct *system_wq __read_mostly;
275struct workqueue_struct *system_long_wq __read_mostly;
276struct workqueue_struct *system_nrt_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200277struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100278struct workqueue_struct *system_freezable_wq __read_mostly;
Alan Stern62d3c542012-03-02 10:51:00 +0100279struct workqueue_struct *system_nrt_freezable_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200280EXPORT_SYMBOL_GPL(system_wq);
281EXPORT_SYMBOL_GPL(system_long_wq);
282EXPORT_SYMBOL_GPL(system_nrt_wq);
Tejun Heof3421792010-07-02 10:03:51 +0200283EXPORT_SYMBOL_GPL(system_unbound_wq);
Tejun Heo24d51ad2011-02-21 09:52:50 +0100284EXPORT_SYMBOL_GPL(system_freezable_wq);
Alan Stern62d3c542012-03-02 10:51:00 +0100285EXPORT_SYMBOL_GPL(system_nrt_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200286
Tejun Heo97bd2342010-10-05 10:41:14 +0200287#define CREATE_TRACE_POINTS
288#include <trace/events/workqueue.h>
289
Tejun Heo9c6bae02012-07-13 22:16:44 -0700290#define for_each_worker_pool(pool, gcwq) \
Tejun Heodcb32ee2012-07-13 22:16:45 -0700291 for ((pool) = &(gcwq)->pools[0]; \
292 (pool) < &(gcwq)->pools[NR_WORKER_POOLS]; (pool)++)
Tejun Heo9c6bae02012-07-13 22:16:44 -0700293
Tejun Heodb7bccf2010-06-29 10:07:12 +0200294#define for_each_busy_worker(worker, i, pos, gcwq) \
295 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \
296 hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry)
297
Tejun Heof3421792010-07-02 10:03:51 +0200298static inline int __next_gcwq_cpu(int cpu, const struct cpumask *mask,
299 unsigned int sw)
300{
301 if (cpu < nr_cpu_ids) {
302 if (sw & 1) {
303 cpu = cpumask_next(cpu, mask);
304 if (cpu < nr_cpu_ids)
305 return cpu;
306 }
307 if (sw & 2)
308 return WORK_CPU_UNBOUND;
309 }
310 return WORK_CPU_NONE;
311}
312
313static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
314 struct workqueue_struct *wq)
315{
316 return __next_gcwq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2);
317}
318
Tejun Heo09884952010-08-01 11:50:12 +0200319/*
320 * CPU iterators
321 *
322 * An extra gcwq is defined for an invalid cpu number
323 * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any
324 * specific CPU. The following iterators are similar to
325 * for_each_*_cpu() iterators but also considers the unbound gcwq.
326 *
327 * for_each_gcwq_cpu() : possible CPUs + WORK_CPU_UNBOUND
328 * for_each_online_gcwq_cpu() : online CPUs + WORK_CPU_UNBOUND
329 * for_each_cwq_cpu() : possible CPUs for bound workqueues,
330 * WORK_CPU_UNBOUND for unbound workqueues
331 */
Tejun Heof3421792010-07-02 10:03:51 +0200332#define for_each_gcwq_cpu(cpu) \
333 for ((cpu) = __next_gcwq_cpu(-1, cpu_possible_mask, 3); \
334 (cpu) < WORK_CPU_NONE; \
335 (cpu) = __next_gcwq_cpu((cpu), cpu_possible_mask, 3))
336
337#define for_each_online_gcwq_cpu(cpu) \
338 for ((cpu) = __next_gcwq_cpu(-1, cpu_online_mask, 3); \
339 (cpu) < WORK_CPU_NONE; \
340 (cpu) = __next_gcwq_cpu((cpu), cpu_online_mask, 3))
341
342#define for_each_cwq_cpu(cpu, wq) \
343 for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, (wq)); \
344 (cpu) < WORK_CPU_NONE; \
345 (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq)))
346
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900347#ifdef CONFIG_DEBUG_OBJECTS_WORK
348
349static struct debug_obj_descr work_debug_descr;
350
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100351static void *work_debug_hint(void *addr)
352{
353 return ((struct work_struct *) addr)->func;
354}
355
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900356/*
357 * fixup_init is called when:
358 * - an active object is initialized
359 */
360static int work_fixup_init(void *addr, enum debug_obj_state state)
361{
362 struct work_struct *work = addr;
363
364 switch (state) {
365 case ODEBUG_STATE_ACTIVE:
366 cancel_work_sync(work);
367 debug_object_init(work, &work_debug_descr);
368 return 1;
369 default:
370 return 0;
371 }
372}
373
374/*
375 * fixup_activate is called when:
376 * - an active object is activated
377 * - an unknown object is activated (might be a statically initialized object)
378 */
379static int work_fixup_activate(void *addr, enum debug_obj_state state)
380{
381 struct work_struct *work = addr;
382
383 switch (state) {
384
385 case ODEBUG_STATE_NOTAVAILABLE:
386 /*
387 * This is not really a fixup. The work struct was
388 * statically initialized. We just make sure that it
389 * is tracked in the object tracker.
390 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200391 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900392 debug_object_init(work, &work_debug_descr);
393 debug_object_activate(work, &work_debug_descr);
394 return 0;
395 }
396 WARN_ON_ONCE(1);
397 return 0;
398
399 case ODEBUG_STATE_ACTIVE:
400 WARN_ON(1);
401
402 default:
403 return 0;
404 }
405}
406
407/*
408 * fixup_free is called when:
409 * - an active object is freed
410 */
411static int work_fixup_free(void *addr, enum debug_obj_state state)
412{
413 struct work_struct *work = addr;
414
415 switch (state) {
416 case ODEBUG_STATE_ACTIVE:
417 cancel_work_sync(work);
418 debug_object_free(work, &work_debug_descr);
419 return 1;
420 default:
421 return 0;
422 }
423}
424
425static struct debug_obj_descr work_debug_descr = {
426 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100427 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900428 .fixup_init = work_fixup_init,
429 .fixup_activate = work_fixup_activate,
430 .fixup_free = work_fixup_free,
431};
432
433static inline void debug_work_activate(struct work_struct *work)
434{
435 debug_object_activate(work, &work_debug_descr);
436}
437
438static inline void debug_work_deactivate(struct work_struct *work)
439{
440 debug_object_deactivate(work, &work_debug_descr);
441}
442
443void __init_work(struct work_struct *work, int onstack)
444{
445 if (onstack)
446 debug_object_init_on_stack(work, &work_debug_descr);
447 else
448 debug_object_init(work, &work_debug_descr);
449}
450EXPORT_SYMBOL_GPL(__init_work);
451
452void destroy_work_on_stack(struct work_struct *work)
453{
454 debug_object_free(work, &work_debug_descr);
455}
456EXPORT_SYMBOL_GPL(destroy_work_on_stack);
457
458#else
459static inline void debug_work_activate(struct work_struct *work) { }
460static inline void debug_work_deactivate(struct work_struct *work) { }
461#endif
462
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100463/* Serializes the accesses to the list of workqueues. */
464static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465static LIST_HEAD(workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200466static bool workqueue_freezing; /* W: have wqs started freezing? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Oleg Nesterov14441962007-05-23 13:57:57 -0700468/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200469 * The almighty global cpu workqueues. nr_running is the only field
470 * which is expected to be used frequently by other cpus via
471 * try_to_wake_up(). Put it in a separate cacheline.
Oleg Nesterov14441962007-05-23 13:57:57 -0700472 */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200473static DEFINE_PER_CPU(struct global_cwq, global_cwq);
Tejun Heo9c6bae02012-07-13 22:16:44 -0700474static DEFINE_PER_CPU_SHARED_ALIGNED(atomic_t, pool_nr_running[NR_WORKER_POOLS]);
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800475
Tejun Heof3421792010-07-02 10:03:51 +0200476/*
477 * Global cpu workqueue and nr_running counter for unbound gcwq. The
478 * gcwq is always online, has GCWQ_DISASSOCIATED set, and all its
479 * workers have WORKER_UNBOUND set.
480 */
481static struct global_cwq unbound_global_cwq;
Tejun Heo9c6bae02012-07-13 22:16:44 -0700482static atomic_t unbound_pool_nr_running[NR_WORKER_POOLS] = {
483 [0 ... NR_WORKER_POOLS - 1] = ATOMIC_INIT(0), /* always 0 */
484};
Tejun Heof3421792010-07-02 10:03:51 +0200485
Tejun Heoc34056a2010-06-29 10:07:11 +0200486static int worker_thread(void *__worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Tejun Heodcb32ee2012-07-13 22:16:45 -0700488static int worker_pool_pri(struct worker_pool *pool)
489{
490 return pool - pool->gcwq->pools;
491}
492
Tejun Heo8b03ae32010-06-29 10:07:12 +0200493static struct global_cwq *get_gcwq(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Tejun Heof3421792010-07-02 10:03:51 +0200495 if (cpu != WORK_CPU_UNBOUND)
496 return &per_cpu(global_cwq, cpu);
497 else
498 return &unbound_global_cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
Tejun Heo7ef6a932012-07-12 14:46:37 -0700501static atomic_t *get_pool_nr_running(struct worker_pool *pool)
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700502{
Tejun Heo7ef6a932012-07-12 14:46:37 -0700503 int cpu = pool->gcwq->cpu;
Tejun Heodcb32ee2012-07-13 22:16:45 -0700504 int idx = worker_pool_pri(pool);
Tejun Heo7ef6a932012-07-12 14:46:37 -0700505
Tejun Heof3421792010-07-02 10:03:51 +0200506 if (cpu != WORK_CPU_UNBOUND)
Tejun Heo9c6bae02012-07-13 22:16:44 -0700507 return &per_cpu(pool_nr_running, cpu)[idx];
Tejun Heof3421792010-07-02 10:03:51 +0200508 else
Tejun Heo9c6bae02012-07-13 22:16:44 -0700509 return &unbound_pool_nr_running[idx];
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700510}
511
Tejun Heo4690c4a2010-06-29 10:07:10 +0200512static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
513 struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700514{
Tejun Heof3421792010-07-02 10:03:51 +0200515 if (!(wq->flags & WQ_UNBOUND)) {
Lai Jiangshane06ffa12012-03-09 18:03:20 +0800516 if (likely(cpu < nr_cpu_ids))
Tejun Heof3421792010-07-02 10:03:51 +0200517 return per_cpu_ptr(wq->cpu_wq.pcpu, cpu);
Tejun Heof3421792010-07-02 10:03:51 +0200518 } else if (likely(cpu == WORK_CPU_UNBOUND))
519 return wq->cpu_wq.single;
520 return NULL;
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700521}
522
Tejun Heo73f53c42010-06-29 10:07:11 +0200523static unsigned int work_color_to_flags(int color)
524{
525 return color << WORK_STRUCT_COLOR_SHIFT;
526}
527
528static int get_work_color(struct work_struct *work)
529{
530 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
531 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
532}
533
534static int work_next_color(int color)
535{
536 return (color + 1) % WORK_NR_COLORS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
David Howells4594bf12006-12-07 11:33:26 +0000539/*
Tejun Heoe1201532010-07-22 14:14:25 +0200540 * A work's data points to the cwq with WORK_STRUCT_CWQ set while the
541 * work is on queue. Once execution starts, WORK_STRUCT_CWQ is
542 * cleared and the work data contains the cpu number it was last on.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200543 *
544 * set_work_{cwq|cpu}() and clear_work_data() can be used to set the
545 * cwq, cpu or clear work->data. These functions should only be
546 * called while the work is owned - ie. while the PENDING bit is set.
547 *
548 * get_work_[g]cwq() can be used to obtain the gcwq or cwq
549 * corresponding to a work. gcwq is available once the work has been
550 * queued anywhere after initialization. cwq is available only from
551 * queueing until execution starts.
David Howells4594bf12006-12-07 11:33:26 +0000552 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200553static inline void set_work_data(struct work_struct *work, unsigned long data,
554 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000555{
David Howells4594bf12006-12-07 11:33:26 +0000556 BUG_ON(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200557 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000558}
David Howells365970a2006-11-22 14:54:49 +0000559
Tejun Heo7a22ad72010-06-29 10:07:13 +0200560static void set_work_cwq(struct work_struct *work,
561 struct cpu_workqueue_struct *cwq,
562 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200563{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200564 set_work_data(work, (unsigned long)cwq,
Tejun Heoe1201532010-07-22 14:14:25 +0200565 WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200566}
567
Tejun Heo7a22ad72010-06-29 10:07:13 +0200568static void set_work_cpu(struct work_struct *work, unsigned int cpu)
David Howells365970a2006-11-22 14:54:49 +0000569{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200570 set_work_data(work, cpu << WORK_STRUCT_FLAG_BITS, WORK_STRUCT_PENDING);
571}
572
573static void clear_work_data(struct work_struct *work)
574{
575 set_work_data(work, WORK_STRUCT_NO_CPU, 0);
576}
577
Tejun Heo7a22ad72010-06-29 10:07:13 +0200578static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work)
579{
Tejun Heoe1201532010-07-22 14:14:25 +0200580 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200581
Tejun Heoe1201532010-07-22 14:14:25 +0200582 if (data & WORK_STRUCT_CWQ)
583 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
584 else
Srinivasarao Pb6e586c2013-09-18 14:33:45 +0530585 {
586 WARN_ON_ONCE(1);
Tejun Heoe1201532010-07-22 14:14:25 +0200587 return NULL;
Srinivasarao Pb6e586c2013-09-18 14:33:45 +0530588 }
Tejun Heo7a22ad72010-06-29 10:07:13 +0200589}
590
591static struct global_cwq *get_work_gcwq(struct work_struct *work)
592{
Tejun Heoe1201532010-07-22 14:14:25 +0200593 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200594 unsigned int cpu;
595
Tejun Heoe1201532010-07-22 14:14:25 +0200596 if (data & WORK_STRUCT_CWQ)
597 return ((struct cpu_workqueue_struct *)
Tejun Heo58658882012-07-12 14:46:37 -0700598 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->gcwq;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200599
600 cpu = data >> WORK_STRUCT_FLAG_BITS;
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200601 if (cpu == WORK_CPU_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200602 return NULL;
603
Tejun Heof3421792010-07-02 10:03:51 +0200604 BUG_ON(cpu >= nr_cpu_ids && cpu != WORK_CPU_UNBOUND);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200605 return get_gcwq(cpu);
David Howells365970a2006-11-22 14:54:49 +0000606}
607
608/*
Tejun Heodcb32ee2012-07-13 22:16:45 -0700609 * Policy functions. These define the policies on how the global worker
610 * pools are managed. Unless noted otherwise, these functions assume that
611 * they're being called with gcwq->lock held.
David Howells365970a2006-11-22 14:54:49 +0000612 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200613
Tejun Heo7ef6a932012-07-12 14:46:37 -0700614static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000615{
Tejun Heodcb32ee2012-07-13 22:16:45 -0700616 return !atomic_read(get_pool_nr_running(pool));
David Howells365970a2006-11-22 14:54:49 +0000617}
618
Tejun Heoe22bee72010-06-29 10:07:14 +0200619/*
620 * Need to wake up a worker? Called from anything but currently
621 * running workers.
Tejun Heob7b5c682012-07-12 14:46:37 -0700622 *
623 * Note that, because unbound workers never contribute to nr_running, this
624 * function will always return %true for unbound gcwq as long as the
625 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200626 */
Tejun Heo7ef6a932012-07-12 14:46:37 -0700627static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000628{
Tejun Heo7ef6a932012-07-12 14:46:37 -0700629 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000630}
631
Tejun Heoe22bee72010-06-29 10:07:14 +0200632/* Can I start working? Called from busy but !running workers. */
Tejun Heo7ef6a932012-07-12 14:46:37 -0700633static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200634{
Tejun Heo7ef6a932012-07-12 14:46:37 -0700635 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200636}
637
638/* Do I need to keep working? Called from currently running workers. */
Tejun Heo7ef6a932012-07-12 14:46:37 -0700639static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200640{
Tejun Heo7ef6a932012-07-12 14:46:37 -0700641 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200642
Tejun Heodcb32ee2012-07-13 22:16:45 -0700643 return !list_empty(&pool->worklist) && atomic_read(nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200644}
645
646/* Do we need a new worker? Called from manager. */
Tejun Heo7ef6a932012-07-12 14:46:37 -0700647static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200648{
Tejun Heo7ef6a932012-07-12 14:46:37 -0700649 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200650}
651
652/* Do I need to be the manager? */
Tejun Heo7ef6a932012-07-12 14:46:37 -0700653static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200654{
Tejun Heo7ef6a932012-07-12 14:46:37 -0700655 return need_to_create_worker(pool) ||
Tejun Heo22ad5642012-07-12 14:46:37 -0700656 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200657}
658
659/* Do we have too many workers and should some go away? */
Tejun Heo7ef6a932012-07-12 14:46:37 -0700660static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200661{
Tejun Heo22ad5642012-07-12 14:46:37 -0700662 bool managing = pool->flags & POOL_MANAGING_WORKERS;
Tejun Heo7ef6a932012-07-12 14:46:37 -0700663 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
664 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200665
666 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
667}
668
669/*
670 * Wake up functions.
671 */
672
Tejun Heo7e116292010-06-29 10:07:13 +0200673/* Return the first worker. Safe with preemption disabled */
Tejun Heo7ef6a932012-07-12 14:46:37 -0700674static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200675{
Tejun Heo7ef6a932012-07-12 14:46:37 -0700676 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200677 return NULL;
678
Tejun Heo7ef6a932012-07-12 14:46:37 -0700679 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200680}
681
682/**
683 * wake_up_worker - wake up an idle worker
Tejun Heo7ef6a932012-07-12 14:46:37 -0700684 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200685 *
Tejun Heo7ef6a932012-07-12 14:46:37 -0700686 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200687 *
688 * CONTEXT:
689 * spin_lock_irq(gcwq->lock).
690 */
Tejun Heo7ef6a932012-07-12 14:46:37 -0700691static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200692{
Tejun Heo7ef6a932012-07-12 14:46:37 -0700693 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200694
695 if (likely(worker))
696 wake_up_process(worker->task);
697}
698
Tejun Heo4690c4a2010-06-29 10:07:10 +0200699/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200700 * wq_worker_waking_up - a worker is waking up
701 * @task: task waking up
702 * @cpu: CPU @task is waking up to
703 *
704 * This function is called during try_to_wake_up() when a worker is
705 * being awoken.
706 *
707 * CONTEXT:
708 * spin_lock_irq(rq->lock)
709 */
710void wq_worker_waking_up(struct task_struct *task, unsigned int cpu)
711{
712 struct worker *worker = kthread_data(task);
713
Steven Rostedt2d646722010-12-03 23:12:33 -0500714 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heo7ef6a932012-07-12 14:46:37 -0700715 atomic_inc(get_pool_nr_running(worker->pool));
Tejun Heoe22bee72010-06-29 10:07:14 +0200716}
717
718/**
719 * wq_worker_sleeping - a worker is going to sleep
720 * @task: task going to sleep
721 * @cpu: CPU in question, must be the current CPU number
722 *
723 * This function is called during schedule() when a busy worker is
724 * going to sleep. Worker on the same cpu can be woken up by
725 * returning pointer to its task.
726 *
727 * CONTEXT:
728 * spin_lock_irq(rq->lock)
729 *
730 * RETURNS:
731 * Worker task on @cpu to wake up, %NULL if none.
732 */
733struct task_struct *wq_worker_sleeping(struct task_struct *task,
734 unsigned int cpu)
735{
736 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heo58658882012-07-12 14:46:37 -0700737 struct worker_pool *pool = worker->pool;
Tejun Heo7ef6a932012-07-12 14:46:37 -0700738 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200739
Steven Rostedt2d646722010-12-03 23:12:33 -0500740 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200741 return NULL;
742
743 /* this can only happen on the local cpu */
744 BUG_ON(cpu != raw_smp_processor_id());
745
746 /*
747 * The counterpart of the following dec_and_test, implied mb,
748 * worklist not empty test sequence is in insert_work().
749 * Please read comment there.
750 *
751 * NOT_RUNNING is clear. This means that trustee is not in
752 * charge and we're running on the local cpu w/ rq lock held
753 * and preemption disabled, which in turn means that none else
754 * could be manipulating idle_list, so dereferencing idle_list
755 * without gcwq lock is safe.
756 */
Tejun Heo58658882012-07-12 14:46:37 -0700757 if (atomic_dec_and_test(nr_running) && !list_empty(&pool->worklist))
Tejun Heo7ef6a932012-07-12 14:46:37 -0700758 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200759 return to_wakeup ? to_wakeup->task : NULL;
760}
761
762/**
763 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200764 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200765 * @flags: flags to set
766 * @wakeup: wakeup an idle worker if necessary
767 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200768 * Set @flags in @worker->flags and adjust nr_running accordingly. If
769 * nr_running becomes zero and @wakeup is %true, an idle worker is
770 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200771 *
Tejun Heocb444762010-07-02 10:03:50 +0200772 * CONTEXT:
773 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200774 */
775static inline void worker_set_flags(struct worker *worker, unsigned int flags,
776 bool wakeup)
777{
Tejun Heo58658882012-07-12 14:46:37 -0700778 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200779
Tejun Heocb444762010-07-02 10:03:50 +0200780 WARN_ON_ONCE(worker->task != current);
781
Tejun Heoe22bee72010-06-29 10:07:14 +0200782 /*
783 * If transitioning into NOT_RUNNING, adjust nr_running and
784 * wake up an idle worker as necessary if requested by
785 * @wakeup.
786 */
787 if ((flags & WORKER_NOT_RUNNING) &&
788 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heo7ef6a932012-07-12 14:46:37 -0700789 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200790
791 if (wakeup) {
792 if (atomic_dec_and_test(nr_running) &&
Tejun Heo58658882012-07-12 14:46:37 -0700793 !list_empty(&pool->worklist))
Tejun Heo7ef6a932012-07-12 14:46:37 -0700794 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200795 } else
796 atomic_dec(nr_running);
797 }
798
Tejun Heod302f012010-06-29 10:07:13 +0200799 worker->flags |= flags;
800}
801
802/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200803 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200804 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200805 * @flags: flags to clear
806 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200807 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200808 *
Tejun Heocb444762010-07-02 10:03:50 +0200809 * CONTEXT:
810 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200811 */
812static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
813{
Tejun Heo7ef6a932012-07-12 14:46:37 -0700814 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200815 unsigned int oflags = worker->flags;
816
Tejun Heocb444762010-07-02 10:03:50 +0200817 WARN_ON_ONCE(worker->task != current);
818
Tejun Heod302f012010-06-29 10:07:13 +0200819 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200820
Tejun Heo42c025f2011-01-11 15:58:49 +0100821 /*
822 * If transitioning out of NOT_RUNNING, increment nr_running. Note
823 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
824 * of multiple flags, not a single flag.
825 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200826 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
827 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heo7ef6a932012-07-12 14:46:37 -0700828 atomic_inc(get_pool_nr_running(pool));
Tejun Heod302f012010-06-29 10:07:13 +0200829}
830
831/**
Tejun Heoc8e55f32010-06-29 10:07:12 +0200832 * busy_worker_head - return the busy hash head for a work
833 * @gcwq: gcwq of interest
834 * @work: work to be hashed
835 *
836 * Return hash head of @gcwq for @work.
837 *
838 * CONTEXT:
839 * spin_lock_irq(gcwq->lock).
840 *
841 * RETURNS:
842 * Pointer to the hash head.
843 */
844static struct hlist_head *busy_worker_head(struct global_cwq *gcwq,
845 struct work_struct *work)
846{
847 const int base_shift = ilog2(sizeof(struct work_struct));
848 unsigned long v = (unsigned long)work;
849
850 /* simple shift and fold hash, do we need something better? */
851 v >>= base_shift;
852 v += v >> BUSY_WORKER_HASH_ORDER;
853 v &= BUSY_WORKER_HASH_MASK;
854
855 return &gcwq->busy_hash[v];
856}
857
858/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200859 * __find_worker_executing_work - find worker which is executing a work
860 * @gcwq: gcwq of interest
861 * @bwh: hash head as returned by busy_worker_head()
862 * @work: work to find worker for
863 *
864 * Find a worker which is executing @work on @gcwq. @bwh should be
865 * the hash head obtained by calling busy_worker_head() with the same
866 * work.
867 *
868 * CONTEXT:
869 * spin_lock_irq(gcwq->lock).
870 *
871 * RETURNS:
872 * Pointer to worker which is executing @work if found, NULL
873 * otherwise.
874 */
875static struct worker *__find_worker_executing_work(struct global_cwq *gcwq,
876 struct hlist_head *bwh,
877 struct work_struct *work)
878{
879 struct worker *worker;
880 struct hlist_node *tmp;
881
882 hlist_for_each_entry(worker, tmp, bwh, hentry)
Tejun Heo55e3e1f2012-12-18 10:35:02 -0800883 if (worker->current_work == work &&
884 worker->current_func == work->func)
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200885 return worker;
886 return NULL;
887}
888
889/**
890 * find_worker_executing_work - find worker which is executing a work
891 * @gcwq: gcwq of interest
892 * @work: work to find worker for
893 *
Tejun Heo55e3e1f2012-12-18 10:35:02 -0800894 * Find a worker which is executing @work on @gcwq by searching
895 * @gcwq->busy_hash which is keyed by the address of @work. For a worker
896 * to match, its current execution should match the address of @work and
897 * its work function. This is to avoid unwanted dependency between
898 * unrelated work executions through a work item being recycled while still
899 * being executed.
900 *
901 * This is a bit tricky. A work item may be freed once its execution
902 * starts and nothing prevents the freed area from being recycled for
903 * another work item. If the same work item address ends up being reused
904 * before the original execution finishes, workqueue will identify the
905 * recycled work item as currently executing and make it wait until the
906 * current execution finishes, introducing an unwanted dependency.
907 *
908 * This function checks the work item address, work function and workqueue
909 * to avoid false positives. Note that this isn't complete as one may
910 * construct a work function which can introduce dependency onto itself
911 * through a recycled work item. Well, if somebody wants to shoot oneself
912 * in the foot that badly, there's only so much we can do, and if such
913 * deadlock actually occurs, it should be easy to locate the culprit work
914 * function.
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200915 *
916 * CONTEXT:
917 * spin_lock_irq(gcwq->lock).
918 *
919 * RETURNS:
920 * Pointer to worker which is executing @work if found, NULL
921 * otherwise.
922 */
923static struct worker *find_worker_executing_work(struct global_cwq *gcwq,
924 struct work_struct *work)
925{
926 return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work),
927 work);
928}
929
930/**
Tejun Heo7e116292010-06-29 10:07:13 +0200931 * insert_work - insert a work into gcwq
Tejun Heo4690c4a2010-06-29 10:07:10 +0200932 * @cwq: cwq @work belongs to
933 * @work: work to insert
934 * @head: insertion point
935 * @extra_flags: extra WORK_STRUCT_* flags to set
936 *
Tejun Heo7e116292010-06-29 10:07:13 +0200937 * Insert @work which belongs to @cwq into @gcwq after @head.
938 * @extra_flags is or'd to work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200939 *
940 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +0200941 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +0200942 */
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700943static void insert_work(struct cpu_workqueue_struct *cwq,
Tejun Heo4690c4a2010-06-29 10:07:10 +0200944 struct work_struct *work, struct list_head *head,
945 unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700946{
Tejun Heo7ef6a932012-07-12 14:46:37 -0700947 struct worker_pool *pool = cwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100948
Tejun Heo4690c4a2010-06-29 10:07:10 +0200949 /* we own @work, set data and link */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200950 set_work_cwq(work, cwq, extra_flags);
Tejun Heo4690c4a2010-06-29 10:07:10 +0200951
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700952 /*
953 * Ensure that we get the right work->data if we see the
954 * result of list_add() below, see try_to_grab_pending().
955 */
956 smp_wmb();
Tejun Heo4690c4a2010-06-29 10:07:10 +0200957
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700958 list_add_tail(&work->entry, head);
Tejun Heoe22bee72010-06-29 10:07:14 +0200959
960 /*
961 * Ensure either worker_sched_deactivated() sees the above
962 * list_add_tail() or we see zero nr_running to avoid workers
963 * lying around lazily while there are works to be processed.
964 */
965 smp_mb();
966
Tejun Heo7ef6a932012-07-12 14:46:37 -0700967 if (__need_more_worker(pool))
968 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700969}
970
Tejun Heoc8efcc22010-12-20 19:32:04 +0100971/*
972 * Test whether @work is being queued from another work executing on the
973 * same workqueue. This is rather expensive and should only be used from
974 * cold paths.
975 */
976static bool is_chained_work(struct workqueue_struct *wq)
977{
978 unsigned long flags;
979 unsigned int cpu;
980
981 for_each_gcwq_cpu(cpu) {
982 struct global_cwq *gcwq = get_gcwq(cpu);
983 struct worker *worker;
984 struct hlist_node *pos;
985 int i;
986
987 spin_lock_irqsave(&gcwq->lock, flags);
988 for_each_busy_worker(worker, i, pos, gcwq) {
989 if (worker->task != current)
990 continue;
991 spin_unlock_irqrestore(&gcwq->lock, flags);
992 /*
993 * I'm @worker, no locking necessary. See if @work
994 * is headed to the same workqueue.
995 */
996 return worker->current_cwq->wq == wq;
997 }
998 spin_unlock_irqrestore(&gcwq->lock, flags);
999 }
1000 return false;
1001}
1002
Tejun Heo4690c4a2010-06-29 10:07:10 +02001003static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 struct work_struct *work)
1005{
Tejun Heo502ca9d2010-06-29 10:07:13 +02001006 struct global_cwq *gcwq;
1007 struct cpu_workqueue_struct *cwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001008 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001009 unsigned int work_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 unsigned long flags;
1011
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001012 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001013
Tejun Heoc8efcc22010-12-20 19:32:04 +01001014 /* if dying, only works from the same workqueue are allowed */
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02001015 if (unlikely(wq->flags & WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001016 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001017 return;
1018
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001019 /* determine gcwq to use */
1020 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001021 struct global_cwq *last_gcwq;
1022
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001023 if (unlikely(cpu == WORK_CPU_UNBOUND))
1024 cpu = raw_smp_processor_id();
1025
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001026 /*
1027 * It's multi cpu. If @wq is non-reentrant and @work
1028 * was previously on a different cpu, it might still
1029 * be running there, in which case the work needs to
1030 * be queued on that cpu to guarantee non-reentrance.
1031 */
Tejun Heo502ca9d2010-06-29 10:07:13 +02001032 gcwq = get_gcwq(cpu);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001033 if (wq->flags & WQ_NON_REENTRANT &&
1034 (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) {
1035 struct worker *worker;
1036
1037 spin_lock_irqsave(&last_gcwq->lock, flags);
1038
1039 worker = find_worker_executing_work(last_gcwq, work);
1040
1041 if (worker && worker->current_cwq->wq == wq)
1042 gcwq = last_gcwq;
1043 else {
1044 /* meh... not running there, queue here */
1045 spin_unlock_irqrestore(&last_gcwq->lock, flags);
1046 spin_lock_irqsave(&gcwq->lock, flags);
1047 }
1048 } else
1049 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heof3421792010-07-02 10:03:51 +02001050 } else {
1051 gcwq = get_gcwq(WORK_CPU_UNBOUND);
1052 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001053 }
1054
1055 /* gcwq determined, get cwq and queue */
1056 cwq = get_cwq(gcwq->cpu, wq);
Tejun Heocdadf002010-10-05 10:49:55 +02001057 trace_workqueue_queue_work(cpu, cwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001058
Tejun Heo4690c4a2010-06-29 10:07:10 +02001059 BUG_ON(!list_empty(&work->entry));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001060
Tejun Heo73f53c42010-06-29 10:07:11 +02001061 cwq->nr_in_flight[cwq->work_color]++;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001062 work_flags = work_color_to_flags(cwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001063
1064 if (likely(cwq->nr_active < cwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001065 trace_workqueue_activate_work(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001066 cwq->nr_active++;
Tejun Heodcb32ee2012-07-13 22:16:45 -07001067 worklist = &cwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001068 } else {
1069 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001070 worklist = &cwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001071 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001072
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001073 insert_work(cwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001074
Tejun Heo8b03ae32010-06-29 10:07:12 +02001075 spin_unlock_irqrestore(&gcwq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076}
1077
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001078/**
1079 * queue_work - queue work on a workqueue
1080 * @wq: workqueue to use
1081 * @work: work to queue
1082 *
Alan Stern057647f2006-10-28 10:38:58 -07001083 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 *
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07001085 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1086 * it can be processed by another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001088int queue_work(struct workqueue_struct *wq, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001090 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001092 ret = queue_work_on(get_cpu(), wq, work);
1093 put_cpu();
1094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 return ret;
1096}
Dave Jonesae90dd52006-06-30 01:40:45 -04001097EXPORT_SYMBOL_GPL(queue_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Zhang Ruic1a220e2008-07-23 21:28:39 -07001099/**
1100 * queue_work_on - queue work on specific cpu
1101 * @cpu: CPU number to execute work on
1102 * @wq: workqueue to use
1103 * @work: work to queue
1104 *
1105 * Returns 0 if @work was already on a queue, non-zero otherwise.
1106 *
1107 * We queue the work to a specific CPU, the caller must ensure it
1108 * can't go away.
1109 */
1110int
1111queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
1112{
1113 int ret = 0;
1114
Tejun Heo22df02b2010-06-29 10:07:10 +02001115 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001116 __queue_work(cpu, wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001117 ret = 1;
1118 }
1119 return ret;
1120}
1121EXPORT_SYMBOL_GPL(queue_work_on);
1122
Li Zefan6d141c32008-02-08 04:21:09 -08001123static void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
David Howells52bad642006-11-22 14:54:01 +00001125 struct delayed_work *dwork = (struct delayed_work *)__data;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001126 struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Srinivasarao Pb6e586c2013-09-18 14:33:45 +05301128 if (cwq != NULL)
1129 __queue_work(smp_processor_id(), cwq->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130}
1131
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001132/**
1133 * queue_delayed_work - queue work on a workqueue after delay
1134 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001135 * @dwork: delayable work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001136 * @delay: number of jiffies to wait before queueing
1137 *
Alan Stern057647f2006-10-28 10:38:58 -07001138 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001139 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001140int queue_delayed_work(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001141 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
David Howells52bad642006-11-22 14:54:01 +00001143 if (delay == 0)
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001144 return queue_work(wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001146 return queue_delayed_work_on(-1, wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147}
Dave Jonesae90dd52006-06-30 01:40:45 -04001148EXPORT_SYMBOL_GPL(queue_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001150/**
1151 * queue_delayed_work_on - queue work on specific CPU after delay
1152 * @cpu: CPU number to execute work on
1153 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001154 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001155 * @delay: number of jiffies to wait before queueing
1156 *
Alan Stern057647f2006-10-28 10:38:58 -07001157 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001158 */
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001159int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001160 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001161{
1162 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +00001163 struct timer_list *timer = &dwork->timer;
1164 struct work_struct *work = &dwork->work;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001165
Tejun Heo22df02b2010-06-29 10:07:10 +02001166 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001167 unsigned int lcpu;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001168
Tejun Heo4afca922012-12-04 07:40:39 -08001169 WARN_ON_ONCE(timer_pending(timer));
1170 WARN_ON_ONCE(!list_empty(&work->entry));
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001171
Andrew Liu8a3e77c2008-05-01 04:35:14 -07001172 timer_stats_timer_set_start_info(&dwork->timer);
1173
Tejun Heo7a22ad72010-06-29 10:07:13 +02001174 /*
1175 * This stores cwq for the moment, for the timer_fn.
1176 * Note that the work's gcwq is preserved to allow
1177 * reentrance detection for delayed works.
1178 */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001179 if (!(wq->flags & WQ_UNBOUND)) {
1180 struct global_cwq *gcwq = get_work_gcwq(work);
1181
1182 if (gcwq && gcwq->cpu != WORK_CPU_UNBOUND)
1183 lcpu = gcwq->cpu;
1184 else
1185 lcpu = raw_smp_processor_id();
1186 } else
1187 lcpu = WORK_CPU_UNBOUND;
1188
Tejun Heo7a22ad72010-06-29 10:07:13 +02001189 set_work_cwq(work, get_cwq(lcpu, wq), 0);
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001190
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001191 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +00001192 timer->data = (unsigned long)dwork;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001193 timer->function = delayed_work_timer_fn;
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001194
1195 if (unlikely(cpu >= 0))
1196 add_timer_on(timer, cpu);
1197 else
1198 add_timer(timer);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001199 ret = 1;
1200 }
1201 return ret;
1202}
Dave Jonesae90dd52006-06-30 01:40:45 -04001203EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Tejun Heoc8e55f32010-06-29 10:07:12 +02001205/**
1206 * worker_enter_idle - enter idle state
1207 * @worker: worker which is entering idle state
1208 *
1209 * @worker is entering idle state. Update stats and idle timer if
1210 * necessary.
1211 *
1212 * LOCKING:
1213 * spin_lock_irq(gcwq->lock).
1214 */
1215static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
Tejun Heo58658882012-07-12 14:46:37 -07001217 struct worker_pool *pool = worker->pool;
1218 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Tejun Heoc8e55f32010-06-29 10:07:12 +02001220 BUG_ON(worker->flags & WORKER_IDLE);
1221 BUG_ON(!list_empty(&worker->entry) &&
1222 (worker->hentry.next || worker->hentry.pprev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Tejun Heocb444762010-07-02 10:03:50 +02001224 /* can't use worker_set_flags(), also called from start_worker() */
1225 worker->flags |= WORKER_IDLE;
Tejun Heo58658882012-07-12 14:46:37 -07001226 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001227 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001228
Tejun Heoc8e55f32010-06-29 10:07:12 +02001229 /* idle_list is LIFO */
Tejun Heo58658882012-07-12 14:46:37 -07001230 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001231
Tejun Heoe22bee72010-06-29 10:07:14 +02001232 if (likely(!(worker->flags & WORKER_ROGUE))) {
Tejun Heo7ef6a932012-07-12 14:46:37 -07001233 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
Tejun Heo58658882012-07-12 14:46:37 -07001234 mod_timer(&pool->idle_timer,
Tejun Heoe22bee72010-06-29 10:07:14 +02001235 jiffies + IDLE_WORKER_TIMEOUT);
1236 } else
Tejun Heodb7bccf2010-06-29 10:07:12 +02001237 wake_up_all(&gcwq->trustee_wait);
Tejun Heocb444762010-07-02 10:03:50 +02001238
Tejun Heo24312d32012-05-14 15:04:50 -07001239 /*
1240 * Sanity check nr_running. Because trustee releases gcwq->lock
1241 * between setting %WORKER_ROGUE and zapping nr_running, the
1242 * warning may trigger spuriously. Check iff trustee is idle.
1243 */
1244 WARN_ON_ONCE(gcwq->trustee_state == TRUSTEE_DONE &&
Tejun Heo58658882012-07-12 14:46:37 -07001245 pool->nr_workers == pool->nr_idle &&
Tejun Heo7ef6a932012-07-12 14:46:37 -07001246 atomic_read(get_pool_nr_running(pool)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247}
1248
Tejun Heoc8e55f32010-06-29 10:07:12 +02001249/**
1250 * worker_leave_idle - leave idle state
1251 * @worker: worker which is leaving idle state
1252 *
1253 * @worker is leaving idle state. Update stats.
1254 *
1255 * LOCKING:
1256 * spin_lock_irq(gcwq->lock).
1257 */
1258static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
Tejun Heo58658882012-07-12 14:46:37 -07001260 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Tejun Heoc8e55f32010-06-29 10:07:12 +02001262 BUG_ON(!(worker->flags & WORKER_IDLE));
Tejun Heod302f012010-06-29 10:07:13 +02001263 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heo58658882012-07-12 14:46:37 -07001264 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001265 list_del_init(&worker->entry);
1266}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Tejun Heoe22bee72010-06-29 10:07:14 +02001268/**
1269 * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq
1270 * @worker: self
1271 *
1272 * Works which are scheduled while the cpu is online must at least be
1273 * scheduled to a worker which is bound to the cpu so that if they are
1274 * flushed from cpu callbacks while cpu is going down, they are
1275 * guaranteed to execute on the cpu.
1276 *
1277 * This function is to be used by rogue workers and rescuers to bind
1278 * themselves to the target cpu and may race with cpu going down or
1279 * coming online. kthread_bind() can't be used because it may put the
1280 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1281 * verbatim as it's best effort and blocking and gcwq may be
1282 * [dis]associated in the meantime.
1283 *
1284 * This function tries set_cpus_allowed() and locks gcwq and verifies
1285 * the binding against GCWQ_DISASSOCIATED which is set during
1286 * CPU_DYING and cleared during CPU_ONLINE, so if the worker enters
1287 * idle state or fetches works without dropping lock, it can guarantee
1288 * the scheduling requirement described in the first paragraph.
1289 *
1290 * CONTEXT:
1291 * Might sleep. Called without any lock but returns with gcwq->lock
1292 * held.
1293 *
1294 * RETURNS:
1295 * %true if the associated gcwq is online (@worker is successfully
1296 * bound), %false if offline.
1297 */
1298static bool worker_maybe_bind_and_lock(struct worker *worker)
Namhyung Kim972fa1c2010-08-22 23:19:43 +09001299__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001300{
Tejun Heo58658882012-07-12 14:46:37 -07001301 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001302 struct task_struct *task = worker->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Tejun Heoe22bee72010-06-29 10:07:14 +02001304 while (true) {
1305 /*
1306 * The following call may fail, succeed or succeed
1307 * without actually migrating the task to the cpu if
1308 * it races with cpu hotunplug operation. Verify
1309 * against GCWQ_DISASSOCIATED.
1310 */
Tejun Heof3421792010-07-02 10:03:51 +02001311 if (!(gcwq->flags & GCWQ_DISASSOCIATED))
1312 set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu));
Oleg Nesterov85f41862007-05-09 02:34:20 -07001313
Tejun Heoe22bee72010-06-29 10:07:14 +02001314 spin_lock_irq(&gcwq->lock);
1315 if (gcwq->flags & GCWQ_DISASSOCIATED)
1316 return false;
1317 if (task_cpu(task) == gcwq->cpu &&
1318 cpumask_equal(&current->cpus_allowed,
1319 get_cpu_mask(gcwq->cpu)))
1320 return true;
1321 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001322
Tejun Heo5035b202011-04-29 18:08:37 +02001323 /*
1324 * We've raced with CPU hot[un]plug. Give it a breather
1325 * and retry migration. cond_resched() is required here;
1326 * otherwise, we might deadlock against cpu_stop trying to
1327 * bring down the CPU on non-preemptive kernel.
1328 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001329 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001330 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001331 }
1332}
1333
1334/*
1335 * Function for worker->rebind_work used to rebind rogue busy workers
1336 * to the associated cpu which is coming back online. This is
1337 * scheduled by cpu up but can race with other cpu hotplug operations
1338 * and may be executed twice without intervening cpu down.
1339 */
1340static void worker_rebind_fn(struct work_struct *work)
1341{
1342 struct worker *worker = container_of(work, struct worker, rebind_work);
Tejun Heo58658882012-07-12 14:46:37 -07001343 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001344
1345 if (worker_maybe_bind_and_lock(worker))
1346 worker_clr_flags(worker, WORKER_REBIND);
1347
1348 spin_unlock_irq(&gcwq->lock);
1349}
1350
Tejun Heoc34056a2010-06-29 10:07:11 +02001351static struct worker *alloc_worker(void)
1352{
1353 struct worker *worker;
1354
1355 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001356 if (worker) {
1357 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001358 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heoe22bee72010-06-29 10:07:14 +02001359 INIT_WORK(&worker->rebind_work, worker_rebind_fn);
1360 /* on creation a worker is in !idle && prep state */
1361 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001362 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001363 return worker;
1364}
1365
1366/**
1367 * create_worker - create a new workqueue worker
Tejun Heo7ef6a932012-07-12 14:46:37 -07001368 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001369 * @bind: whether to set affinity to @cpu or not
1370 *
Tejun Heo7ef6a932012-07-12 14:46:37 -07001371 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001372 * can be started by calling start_worker() or destroyed using
1373 * destroy_worker().
1374 *
1375 * CONTEXT:
1376 * Might sleep. Does GFP_KERNEL allocations.
1377 *
1378 * RETURNS:
1379 * Pointer to the newly created worker.
1380 */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001381static struct worker *create_worker(struct worker_pool *pool, bool bind)
Tejun Heoc34056a2010-06-29 10:07:11 +02001382{
Tejun Heo7ef6a932012-07-12 14:46:37 -07001383 struct global_cwq *gcwq = pool->gcwq;
Tejun Heof3421792010-07-02 10:03:51 +02001384 bool on_unbound_cpu = gcwq->cpu == WORK_CPU_UNBOUND;
Tejun Heodcb32ee2012-07-13 22:16:45 -07001385 const char *pri = worker_pool_pri(pool) ? "H" : "";
Tejun Heoc34056a2010-06-29 10:07:11 +02001386 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001387 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001388
Tejun Heo8b03ae32010-06-29 10:07:12 +02001389 spin_lock_irq(&gcwq->lock);
Tejun Heo58658882012-07-12 14:46:37 -07001390 while (ida_get_new(&pool->worker_ida, &id)) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001391 spin_unlock_irq(&gcwq->lock);
Tejun Heo58658882012-07-12 14:46:37 -07001392 if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001393 goto fail;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001394 spin_lock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001395 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02001396 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001397
1398 worker = alloc_worker();
1399 if (!worker)
1400 goto fail;
1401
Tejun Heo58658882012-07-12 14:46:37 -07001402 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001403 worker->id = id;
1404
Tejun Heof3421792010-07-02 10:03:51 +02001405 if (!on_unbound_cpu)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001406 worker->task = kthread_create_on_node(worker_thread,
Tejun Heodcb32ee2012-07-13 22:16:45 -07001407 worker, cpu_to_node(gcwq->cpu),
1408 "kworker/%u:%d%s", gcwq->cpu, id, pri);
Tejun Heof3421792010-07-02 10:03:51 +02001409 else
1410 worker->task = kthread_create(worker_thread, worker,
Tejun Heodcb32ee2012-07-13 22:16:45 -07001411 "kworker/u:%d%s", id, pri);
Tejun Heoc34056a2010-06-29 10:07:11 +02001412 if (IS_ERR(worker->task))
1413 goto fail;
1414
Tejun Heodcb32ee2012-07-13 22:16:45 -07001415 if (worker_pool_pri(pool))
1416 set_user_nice(worker->task, HIGHPRI_NICE_LEVEL);
1417
Tejun Heodb7bccf2010-06-29 10:07:12 +02001418 /*
1419 * A rogue worker will become a regular one if CPU comes
1420 * online later on. Make sure every worker has
1421 * PF_THREAD_BOUND set.
1422 */
Tejun Heof3421792010-07-02 10:03:51 +02001423 if (bind && !on_unbound_cpu)
Tejun Heo8b03ae32010-06-29 10:07:12 +02001424 kthread_bind(worker->task, gcwq->cpu);
Tejun Heof3421792010-07-02 10:03:51 +02001425 else {
Tejun Heodb7bccf2010-06-29 10:07:12 +02001426 worker->task->flags |= PF_THREAD_BOUND;
Tejun Heof3421792010-07-02 10:03:51 +02001427 if (on_unbound_cpu)
1428 worker->flags |= WORKER_UNBOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001430
Tejun Heoc34056a2010-06-29 10:07:11 +02001431 return worker;
1432fail:
1433 if (id >= 0) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001434 spin_lock_irq(&gcwq->lock);
Tejun Heo58658882012-07-12 14:46:37 -07001435 ida_remove(&pool->worker_ida, id);
Tejun Heo8b03ae32010-06-29 10:07:12 +02001436 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001437 }
1438 kfree(worker);
1439 return NULL;
1440}
1441
1442/**
1443 * start_worker - start a newly created worker
1444 * @worker: worker to start
1445 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001446 * Make the gcwq aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001447 *
1448 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001449 * spin_lock_irq(gcwq->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001450 */
1451static void start_worker(struct worker *worker)
1452{
Tejun Heocb444762010-07-02 10:03:50 +02001453 worker->flags |= WORKER_STARTED;
Tejun Heo58658882012-07-12 14:46:37 -07001454 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001455 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001456 wake_up_process(worker->task);
1457}
1458
1459/**
1460 * destroy_worker - destroy a workqueue worker
1461 * @worker: worker to be destroyed
1462 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001463 * Destroy @worker and adjust @gcwq stats accordingly.
1464 *
1465 * CONTEXT:
1466 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001467 */
1468static void destroy_worker(struct worker *worker)
1469{
Tejun Heo58658882012-07-12 14:46:37 -07001470 struct worker_pool *pool = worker->pool;
1471 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001472 int id = worker->id;
1473
1474 /* sanity check frenzy */
1475 BUG_ON(worker->current_work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001476 BUG_ON(!list_empty(&worker->scheduled));
Tejun Heoc34056a2010-06-29 10:07:11 +02001477
Tejun Heoc8e55f32010-06-29 10:07:12 +02001478 if (worker->flags & WORKER_STARTED)
Tejun Heo58658882012-07-12 14:46:37 -07001479 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001480 if (worker->flags & WORKER_IDLE)
Tejun Heo58658882012-07-12 14:46:37 -07001481 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001482
Lai Jiangshan23f09132014-02-15 22:02:28 +08001483 /*
1484 * Once WORKER_DIE is set, the kworker may destroy itself at any
1485 * point. Pin to ensure the task stays until we're done with it.
1486 */
1487 get_task_struct(worker->task);
1488
Tejun Heoc8e55f32010-06-29 10:07:12 +02001489 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001490 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001491
1492 spin_unlock_irq(&gcwq->lock);
1493
Tejun Heoc34056a2010-06-29 10:07:11 +02001494 kthread_stop(worker->task);
Lai Jiangshan23f09132014-02-15 22:02:28 +08001495 put_task_struct(worker->task);
Tejun Heoc34056a2010-06-29 10:07:11 +02001496 kfree(worker);
1497
Tejun Heo8b03ae32010-06-29 10:07:12 +02001498 spin_lock_irq(&gcwq->lock);
Tejun Heo58658882012-07-12 14:46:37 -07001499 ida_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001500}
1501
Tejun Heo7ef6a932012-07-12 14:46:37 -07001502static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001503{
Tejun Heo7ef6a932012-07-12 14:46:37 -07001504 struct worker_pool *pool = (void *)__pool;
1505 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001506
1507 spin_lock_irq(&gcwq->lock);
1508
Tejun Heo7ef6a932012-07-12 14:46:37 -07001509 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001510 struct worker *worker;
1511 unsigned long expires;
1512
1513 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001514 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001515 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1516
1517 if (time_before(jiffies, expires))
Tejun Heo7ef6a932012-07-12 14:46:37 -07001518 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001519 else {
1520 /* it's been idle for too long, wake up manager */
Tejun Heo22ad5642012-07-12 14:46:37 -07001521 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo7ef6a932012-07-12 14:46:37 -07001522 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001523 }
1524 }
1525
1526 spin_unlock_irq(&gcwq->lock);
1527}
1528
1529static bool send_mayday(struct work_struct *work)
1530{
1531 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1532 struct workqueue_struct *wq = cwq->wq;
Tejun Heof3421792010-07-02 10:03:51 +02001533 unsigned int cpu;
Tejun Heoe22bee72010-06-29 10:07:14 +02001534
1535 if (!(wq->flags & WQ_RESCUER))
1536 return false;
1537
1538 /* mayday mayday mayday */
Tejun Heo58658882012-07-12 14:46:37 -07001539 cpu = cwq->pool->gcwq->cpu;
Tejun Heof3421792010-07-02 10:03:51 +02001540 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1541 if (cpu == WORK_CPU_UNBOUND)
1542 cpu = 0;
Tejun Heof2e005a2010-07-20 15:59:09 +02001543 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001544 wake_up_process(wq->rescuer->task);
1545 return true;
1546}
1547
Tejun Heo7ef6a932012-07-12 14:46:37 -07001548static void gcwq_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001549{
Tejun Heo7ef6a932012-07-12 14:46:37 -07001550 struct worker_pool *pool = (void *)__pool;
1551 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001552 struct work_struct *work;
1553
1554 spin_lock_irq(&gcwq->lock);
1555
Tejun Heo7ef6a932012-07-12 14:46:37 -07001556 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001557 /*
1558 * We've been trying to create a new worker but
1559 * haven't been successful. We might be hitting an
1560 * allocation deadlock. Send distress signals to
1561 * rescuers.
1562 */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001563 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001564 send_mayday(work);
1565 }
1566
1567 spin_unlock_irq(&gcwq->lock);
1568
Tejun Heo7ef6a932012-07-12 14:46:37 -07001569 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001570}
1571
1572/**
1573 * maybe_create_worker - create a new worker if necessary
Tejun Heo7ef6a932012-07-12 14:46:37 -07001574 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001575 *
Tejun Heo7ef6a932012-07-12 14:46:37 -07001576 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001577 * have at least one idle worker on return from this function. If
1578 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo7ef6a932012-07-12 14:46:37 -07001579 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001580 * possible allocation deadlock.
1581 *
1582 * On return, need_to_create_worker() is guaranteed to be false and
1583 * may_start_working() true.
1584 *
1585 * LOCKING:
1586 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1587 * multiple times. Does GFP_KERNEL allocations. Called only from
1588 * manager.
1589 *
1590 * RETURNS:
1591 * false if no action was taken and gcwq->lock stayed locked, true
1592 * otherwise.
1593 */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001594static bool maybe_create_worker(struct worker_pool *pool)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001595__releases(&gcwq->lock)
1596__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001597{
Tejun Heo7ef6a932012-07-12 14:46:37 -07001598 struct global_cwq *gcwq = pool->gcwq;
1599
1600 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001601 return false;
1602restart:
Tejun Heo9f9c2362010-07-14 11:31:20 +02001603 spin_unlock_irq(&gcwq->lock);
1604
Tejun Heoe22bee72010-06-29 10:07:14 +02001605 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001606 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001607
1608 while (true) {
1609 struct worker *worker;
1610
Tejun Heo7ef6a932012-07-12 14:46:37 -07001611 worker = create_worker(pool, true);
Tejun Heoe22bee72010-06-29 10:07:14 +02001612 if (worker) {
Tejun Heo7ef6a932012-07-12 14:46:37 -07001613 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001614 spin_lock_irq(&gcwq->lock);
1615 start_worker(worker);
Tejun Heo7ef6a932012-07-12 14:46:37 -07001616 BUG_ON(need_to_create_worker(pool));
Tejun Heoe22bee72010-06-29 10:07:14 +02001617 return true;
1618 }
1619
Tejun Heo7ef6a932012-07-12 14:46:37 -07001620 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001621 break;
1622
Tejun Heoe22bee72010-06-29 10:07:14 +02001623 __set_current_state(TASK_INTERRUPTIBLE);
1624 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001625
Tejun Heo7ef6a932012-07-12 14:46:37 -07001626 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001627 break;
1628 }
1629
Tejun Heo7ef6a932012-07-12 14:46:37 -07001630 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001631 spin_lock_irq(&gcwq->lock);
Tejun Heo7ef6a932012-07-12 14:46:37 -07001632 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001633 goto restart;
1634 return true;
1635}
1636
1637/**
1638 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo7ef6a932012-07-12 14:46:37 -07001639 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001640 *
Tejun Heo7ef6a932012-07-12 14:46:37 -07001641 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02001642 * IDLE_WORKER_TIMEOUT.
1643 *
1644 * LOCKING:
1645 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1646 * multiple times. Called only from manager.
1647 *
1648 * RETURNS:
1649 * false if no action was taken and gcwq->lock stayed locked, true
1650 * otherwise.
1651 */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001652static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001653{
1654 bool ret = false;
1655
Tejun Heo7ef6a932012-07-12 14:46:37 -07001656 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001657 struct worker *worker;
1658 unsigned long expires;
1659
Tejun Heo7ef6a932012-07-12 14:46:37 -07001660 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001661 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1662
1663 if (time_before(jiffies, expires)) {
Tejun Heo7ef6a932012-07-12 14:46:37 -07001664 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001665 break;
1666 }
1667
1668 destroy_worker(worker);
1669 ret = true;
1670 }
1671
1672 return ret;
1673}
1674
1675/**
1676 * manage_workers - manage worker pool
1677 * @worker: self
1678 *
1679 * Assume the manager role and manage gcwq worker pool @worker belongs
1680 * to. At any given time, there can be only zero or one manager per
1681 * gcwq. The exclusion is handled automatically by this function.
1682 *
1683 * The caller can safely start processing works on false return. On
1684 * true return, it's guaranteed that need_to_create_worker() is false
1685 * and may_start_working() is true.
1686 *
1687 * CONTEXT:
1688 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1689 * multiple times. Does GFP_KERNEL allocations.
1690 *
1691 * RETURNS:
1692 * false if no action was taken and gcwq->lock stayed locked, true if
1693 * some action was taken.
1694 */
1695static bool manage_workers(struct worker *worker)
1696{
Tejun Heo7ef6a932012-07-12 14:46:37 -07001697 struct worker_pool *pool = worker->pool;
1698 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001699 bool ret = false;
1700
Tejun Heo22ad5642012-07-12 14:46:37 -07001701 if (pool->flags & POOL_MANAGING_WORKERS)
Tejun Heoe22bee72010-06-29 10:07:14 +02001702 return ret;
1703
Tejun Heo22ad5642012-07-12 14:46:37 -07001704 pool->flags &= ~POOL_MANAGE_WORKERS;
1705 pool->flags |= POOL_MANAGING_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02001706
1707 /*
1708 * Destroy and then create so that may_start_working() is true
1709 * on return.
1710 */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001711 ret |= maybe_destroy_workers(pool);
1712 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001713
Tejun Heo22ad5642012-07-12 14:46:37 -07001714 pool->flags &= ~POOL_MANAGING_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02001715
1716 /*
1717 * The trustee might be waiting to take over the manager
1718 * position, tell it we're done.
1719 */
1720 if (unlikely(gcwq->trustee))
1721 wake_up_all(&gcwq->trustee_wait);
1722
1723 return ret;
1724}
1725
Tejun Heoa62428c2010-06-29 10:07:10 +02001726/**
Tejun Heoaffee4b2010-06-29 10:07:12 +02001727 * move_linked_works - move linked works to a list
1728 * @work: start of series of works to be scheduled
1729 * @head: target list to append @work to
1730 * @nextp: out paramter for nested worklist walking
1731 *
1732 * Schedule linked works starting from @work to @head. Work series to
1733 * be scheduled starts at @work and includes any consecutive work with
1734 * WORK_STRUCT_LINKED set in its predecessor.
1735 *
1736 * If @nextp is not NULL, it's updated to point to the next work of
1737 * the last scheduled work. This allows move_linked_works() to be
1738 * nested inside outer list_for_each_entry_safe().
1739 *
1740 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001741 * spin_lock_irq(gcwq->lock).
Tejun Heoaffee4b2010-06-29 10:07:12 +02001742 */
1743static void move_linked_works(struct work_struct *work, struct list_head *head,
1744 struct work_struct **nextp)
1745{
1746 struct work_struct *n;
1747
1748 /*
1749 * Linked worklist will always end before the end of the list,
1750 * use NULL for list head.
1751 */
1752 list_for_each_entry_safe_from(work, n, NULL, entry) {
1753 list_move_tail(&work->entry, head);
1754 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1755 break;
1756 }
1757
1758 /*
1759 * If we're already inside safe list traversal and have moved
1760 * multiple works to the scheduled queue, the next position
1761 * needs to be updated.
1762 */
1763 if (nextp)
1764 *nextp = n;
1765}
1766
Lai Jiangshan31eafff2012-09-18 10:40:00 -07001767static void cwq_activate_delayed_work(struct work_struct *work)
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001768{
Lai Jiangshan31eafff2012-09-18 10:40:00 -07001769 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001770
Tejun Heocdadf002010-10-05 10:49:55 +02001771 trace_workqueue_activate_work(work);
Tejun Heodcb32ee2012-07-13 22:16:45 -07001772 move_linked_works(work, &cwq->pool->worklist, NULL);
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001773 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001774 cwq->nr_active++;
1775}
1776
Lai Jiangshan31eafff2012-09-18 10:40:00 -07001777static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
1778{
1779 struct work_struct *work = list_first_entry(&cwq->delayed_works,
1780 struct work_struct, entry);
1781
1782 cwq_activate_delayed_work(work);
1783}
1784
Tejun Heoaffee4b2010-06-29 10:07:12 +02001785/**
Tejun Heo73f53c42010-06-29 10:07:11 +02001786 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
1787 * @cwq: cwq of interest
1788 * @color: color of work which left the queue
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001789 * @delayed: for a delayed work
Tejun Heo73f53c42010-06-29 10:07:11 +02001790 *
1791 * A work either has completed or is removed from pending queue,
1792 * decrement nr_in_flight of its cwq and handle workqueue flushing.
1793 *
1794 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001795 * spin_lock_irq(gcwq->lock).
Tejun Heo73f53c42010-06-29 10:07:11 +02001796 */
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001797static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color,
1798 bool delayed)
Tejun Heo73f53c42010-06-29 10:07:11 +02001799{
1800 /* ignore uncolored works */
1801 if (color == WORK_NO_COLOR)
1802 return;
1803
1804 cwq->nr_in_flight[color]--;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001805
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001806 if (!delayed) {
1807 cwq->nr_active--;
1808 if (!list_empty(&cwq->delayed_works)) {
1809 /* one down, submit a delayed one */
1810 if (cwq->nr_active < cwq->max_active)
1811 cwq_activate_first_delayed(cwq);
1812 }
Tejun Heo502ca9d2010-06-29 10:07:13 +02001813 }
Tejun Heo73f53c42010-06-29 10:07:11 +02001814
1815 /* is flush in progress and are we at the flushing tip? */
1816 if (likely(cwq->flush_color != color))
1817 return;
1818
1819 /* are there still in-flight works? */
1820 if (cwq->nr_in_flight[color])
1821 return;
1822
1823 /* this cwq is done, clear flush_color */
1824 cwq->flush_color = -1;
1825
1826 /*
1827 * If this was the last cwq, wake up the first flusher. It
1828 * will handle the rest.
1829 */
1830 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
1831 complete(&cwq->wq->first_flusher->done);
1832}
1833
1834/**
Tejun Heoa62428c2010-06-29 10:07:10 +02001835 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02001836 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02001837 * @work: work to process
1838 *
1839 * Process @work. This function contains all the logics necessary to
1840 * process a single work including synchronization against and
1841 * interaction with other workers on the same cpu, queueing and
1842 * flushing. As long as context requirement is met, any worker can
1843 * call this function to process a work.
1844 *
1845 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001846 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02001847 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001848static void process_one_work(struct worker *worker, struct work_struct *work)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001849__releases(&gcwq->lock)
1850__acquires(&gcwq->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02001851{
Tejun Heo7e116292010-06-29 10:07:13 +02001852 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heo58658882012-07-12 14:46:37 -07001853 struct worker_pool *pool = worker->pool;
1854 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001855 struct hlist_head *bwh = busy_worker_head(gcwq, work);
Tejun Heofb0e7be2010-06-29 10:07:15 +02001856 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02001857 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02001858 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02001859#ifdef CONFIG_LOCKDEP
1860 /*
1861 * It is permissible to free the struct work_struct from
1862 * inside the function that is called from it, this we need to
1863 * take into account for lockdep too. To avoid bogus "held
1864 * lock freed" warnings as well as problems when looking into
1865 * work->lockdep_map, make a copy and use that here.
1866 */
1867 struct lockdep_map lockdep_map = work->lockdep_map;
1868#endif
Tejun Heo7e116292010-06-29 10:07:13 +02001869 /*
1870 * A single work shouldn't be executed concurrently by
1871 * multiple workers on a single cpu. Check whether anyone is
1872 * already processing the work. If so, defer the work to the
1873 * currently executing one.
1874 */
1875 collision = __find_worker_executing_work(gcwq, bwh, work);
1876 if (unlikely(collision)) {
1877 move_linked_works(work, &collision->scheduled, NULL);
1878 return;
1879 }
1880
Tejun Heoa62428c2010-06-29 10:07:10 +02001881 /* claim and process */
Tejun Heoa62428c2010-06-29 10:07:10 +02001882 debug_work_deactivate(work);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001883 hlist_add_head(&worker->hentry, bwh);
Tejun Heoc34056a2010-06-29 10:07:11 +02001884 worker->current_work = work;
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001885 worker->current_func = work->func;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001886 worker->current_cwq = cwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02001887 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02001888
Tejun Heo7a22ad72010-06-29 10:07:13 +02001889 /* record the current cpu number in the work data and dequeue */
1890 set_work_cpu(work, gcwq->cpu);
Tejun Heoa62428c2010-06-29 10:07:10 +02001891 list_del_init(&work->entry);
1892
Tejun Heo649027d2010-06-29 10:07:14 +02001893 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02001894 * CPU intensive works don't participate in concurrency
1895 * management. They're the scheduler's responsibility.
1896 */
1897 if (unlikely(cpu_intensive))
1898 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
1899
Tejun Heob7b5c682012-07-12 14:46:37 -07001900 /*
1901 * Unbound gcwq isn't concurrency managed and work items should be
1902 * executed ASAP. Wake up another worker if necessary.
1903 */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001904 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
1905 wake_up_worker(pool);
Tejun Heob7b5c682012-07-12 14:46:37 -07001906
Tejun Heo8b03ae32010-06-29 10:07:12 +02001907 spin_unlock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001908
Tejun Heo66307ae2012-08-03 10:30:45 -07001909 smp_wmb(); /* paired with test_and_set_bit(PENDING) */
Tejun Heoa62428c2010-06-29 10:07:10 +02001910 work_clear_pending(work);
Tejun Heo66307ae2012-08-03 10:30:45 -07001911
Tejun Heoe1594892011-01-09 23:32:15 +01001912 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02001913 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001914 trace_workqueue_execute_start(work);
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001915 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001916 /*
1917 * While we must be careful to not use "work" after this, the trace
1918 * point will only record its address.
1919 */
1920 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02001921 lock_map_release(&lockdep_map);
1922 lock_map_release(&cwq->wq->lockdep_map);
1923
1924 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001925 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
1926 " last function: %pf\n",
1927 current->comm, preempt_count(), task_pid_nr(current),
1928 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02001929 debug_show_held_locks(current);
Syed Rameez Mustafa1bee7b92013-07-15 11:52:09 -07001930 BUG_ON(PANIC_CORRUPTION);
Tejun Heoa62428c2010-06-29 10:07:10 +02001931 dump_stack();
1932 }
1933
Tejun Heo00cef7a2013-08-28 17:33:37 -04001934 /*
1935 * The following prevents a kworker from hogging CPU on !PREEMPT
1936 * kernels, where a requeueing work item waiting for something to
1937 * happen could deadlock with stop_machine as such work item could
1938 * indefinitely requeue itself while all other CPUs are trapped in
1939 * stop_machine.
1940 */
1941 cond_resched();
1942
Tejun Heo8b03ae32010-06-29 10:07:12 +02001943 spin_lock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001944
Tejun Heofb0e7be2010-06-29 10:07:15 +02001945 /* clear cpu intensive status */
1946 if (unlikely(cpu_intensive))
1947 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
1948
Tejun Heoa62428c2010-06-29 10:07:10 +02001949 /* we're done with it, release */
Tejun Heoc8e55f32010-06-29 10:07:12 +02001950 hlist_del_init(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001951 worker->current_work = NULL;
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001952 worker->current_func = NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001953 worker->current_cwq = NULL;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001954 cwq_dec_nr_in_flight(cwq, work_color, false);
Tejun Heoa62428c2010-06-29 10:07:10 +02001955}
1956
Tejun Heoaffee4b2010-06-29 10:07:12 +02001957/**
1958 * process_scheduled_works - process scheduled works
1959 * @worker: self
1960 *
1961 * Process all scheduled works. Please note that the scheduled list
1962 * may change while processing a work, so this function repeatedly
1963 * fetches a work from the top and executes it.
1964 *
1965 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001966 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02001967 * multiple times.
1968 */
1969static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
Tejun Heoaffee4b2010-06-29 10:07:12 +02001971 while (!list_empty(&worker->scheduled)) {
1972 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001974 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976}
1977
Tejun Heo4690c4a2010-06-29 10:07:10 +02001978/**
1979 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02001980 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02001981 *
Tejun Heoe22bee72010-06-29 10:07:14 +02001982 * The gcwq worker thread function. There's a single dynamic pool of
1983 * these per each cpu. These workers process all works regardless of
1984 * their specific target workqueue. The only exception is works which
1985 * belong to workqueues with a rescuer which will be explained in
1986 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02001987 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001988static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989{
Tejun Heoc34056a2010-06-29 10:07:11 +02001990 struct worker *worker = __worker;
Tejun Heo58658882012-07-12 14:46:37 -07001991 struct worker_pool *pool = worker->pool;
1992 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Tejun Heoe22bee72010-06-29 10:07:14 +02001994 /* tell the scheduler that this is a workqueue worker */
1995 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001996woke_up:
Tejun Heoc8e55f32010-06-29 10:07:12 +02001997 spin_lock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Tejun Heoc8e55f32010-06-29 10:07:12 +02001999 /* DIE can be set only while we're idle, checking here is enough */
2000 if (worker->flags & WORKER_DIE) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02002001 spin_unlock_irq(&gcwq->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002002 worker->task->flags &= ~PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002003 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 }
2005
Tejun Heoc8e55f32010-06-29 10:07:12 +02002006 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002007recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002008 /* no more worker necessary? */
Tejun Heo7ef6a932012-07-12 14:46:37 -07002009 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002010 goto sleep;
2011
2012 /* do we need to manage? */
Tejun Heo7ef6a932012-07-12 14:46:37 -07002013 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002014 goto recheck;
2015
Tejun Heoc8e55f32010-06-29 10:07:12 +02002016 /*
2017 * ->scheduled list can only be filled while a worker is
2018 * preparing to process a work or actually processing it.
2019 * Make sure nobody diddled with it while I was sleeping.
2020 */
2021 BUG_ON(!list_empty(&worker->scheduled));
2022
Tejun Heoe22bee72010-06-29 10:07:14 +02002023 /*
2024 * When control reaches this point, we're guaranteed to have
2025 * at least one idle worker or that someone else has already
2026 * assumed the manager role.
2027 */
2028 worker_clr_flags(worker, WORKER_PREP);
2029
2030 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002031 struct work_struct *work =
Tejun Heo58658882012-07-12 14:46:37 -07002032 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002033 struct work_struct, entry);
2034
2035 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2036 /* optimization path, not strictly necessary */
2037 process_one_work(worker, work);
2038 if (unlikely(!list_empty(&worker->scheduled)))
2039 process_scheduled_works(worker);
2040 } else {
2041 move_linked_works(work, &worker->scheduled, NULL);
2042 process_scheduled_works(worker);
2043 }
Tejun Heo7ef6a932012-07-12 14:46:37 -07002044 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002045
Tejun Heoe22bee72010-06-29 10:07:14 +02002046 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002047sleep:
Tejun Heo7ef6a932012-07-12 14:46:37 -07002048 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002049 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002050
Tejun Heoc8e55f32010-06-29 10:07:12 +02002051 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02002052 * gcwq->lock is held and there's no work to process and no
2053 * need to manage, sleep. Workers are woken up only while
2054 * holding gcwq->lock or from local cpu, so setting the
2055 * current state before releasing gcwq->lock is enough to
2056 * prevent losing any event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002057 */
2058 worker_enter_idle(worker);
2059 __set_current_state(TASK_INTERRUPTIBLE);
2060 spin_unlock_irq(&gcwq->lock);
2061 schedule();
2062 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063}
2064
Tejun Heoe22bee72010-06-29 10:07:14 +02002065/**
2066 * rescuer_thread - the rescuer thread function
2067 * @__wq: the associated workqueue
2068 *
2069 * Workqueue rescuer thread function. There's one rescuer for each
2070 * workqueue which has WQ_RESCUER set.
2071 *
2072 * Regular work processing on a gcwq may block trying to create a new
2073 * worker which uses GFP_KERNEL allocation which has slight chance of
2074 * developing into deadlock if some works currently on the same queue
2075 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2076 * the problem rescuer solves.
2077 *
2078 * When such condition is possible, the gcwq summons rescuers of all
2079 * workqueues which have works queued on the gcwq and let them process
2080 * those works so that forward progress can be guaranteed.
2081 *
2082 * This should happen rarely.
2083 */
2084static int rescuer_thread(void *__wq)
2085{
2086 struct workqueue_struct *wq = __wq;
2087 struct worker *rescuer = wq->rescuer;
2088 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heof3421792010-07-02 10:03:51 +02002089 bool is_unbound = wq->flags & WQ_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02002090 unsigned int cpu;
2091
2092 set_user_nice(current, RESCUER_NICE_LEVEL);
2093repeat:
2094 set_current_state(TASK_INTERRUPTIBLE);
2095
Mike Galbraithdbdd7f02012-11-28 07:17:18 +01002096 if (kthread_should_stop()) {
2097 __set_current_state(TASK_RUNNING);
Tejun Heoe22bee72010-06-29 10:07:14 +02002098 return 0;
Mike Galbraithdbdd7f02012-11-28 07:17:18 +01002099 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002100
Tejun Heof3421792010-07-02 10:03:51 +02002101 /*
2102 * See whether any cpu is asking for help. Unbounded
2103 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
2104 */
Tejun Heof2e005a2010-07-20 15:59:09 +02002105 for_each_mayday_cpu(cpu, wq->mayday_mask) {
Tejun Heof3421792010-07-02 10:03:51 +02002106 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2107 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
Tejun Heo58658882012-07-12 14:46:37 -07002108 struct worker_pool *pool = cwq->pool;
2109 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002110 struct work_struct *work, *n;
2111
2112 __set_current_state(TASK_RUNNING);
Tejun Heof2e005a2010-07-20 15:59:09 +02002113 mayday_clear_cpu(cpu, wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002114
2115 /* migrate to the target cpu if possible */
Tejun Heo58658882012-07-12 14:46:37 -07002116 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002117 worker_maybe_bind_and_lock(rescuer);
2118
2119 /*
2120 * Slurp in all works issued via this workqueue and
2121 * process'em.
2122 */
2123 BUG_ON(!list_empty(&rescuer->scheduled));
Tejun Heo58658882012-07-12 14:46:37 -07002124 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02002125 if (get_work_cwq(work) == cwq)
2126 move_linked_works(work, scheduled, &n);
2127
2128 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002129
2130 /*
2131 * Leave this gcwq. If keep_working() is %true, notify a
2132 * regular worker; otherwise, we end up with 0 concurrency
2133 * and stalling the execution.
2134 */
Tejun Heo7ef6a932012-07-12 14:46:37 -07002135 if (keep_working(pool))
2136 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002137
Tejun Heoe22bee72010-06-29 10:07:14 +02002138 spin_unlock_irq(&gcwq->lock);
2139 }
2140
2141 schedule();
2142 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143}
2144
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002145struct wq_barrier {
2146 struct work_struct work;
2147 struct completion done;
2148};
2149
2150static void wq_barrier_func(struct work_struct *work)
2151{
2152 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2153 complete(&barr->done);
2154}
2155
Tejun Heo4690c4a2010-06-29 10:07:10 +02002156/**
2157 * insert_wq_barrier - insert a barrier work
2158 * @cwq: cwq to insert barrier into
2159 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002160 * @target: target work to attach @barr to
2161 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002162 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002163 * @barr is linked to @target such that @barr is completed only after
2164 * @target finishes execution. Please note that the ordering
2165 * guarantee is observed only with respect to @target and on the local
2166 * cpu.
2167 *
2168 * Currently, a queued barrier can't be canceled. This is because
2169 * try_to_grab_pending() can't determine whether the work to be
2170 * grabbed is at the head of the queue and thus can't clear LINKED
2171 * flag of the previous work while there must be a valid next work
2172 * after a work with LINKED flag set.
2173 *
2174 * Note that when @worker is non-NULL, @target may be modified
2175 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002176 *
2177 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002178 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002179 */
Oleg Nesterov83c22522007-05-09 02:33:54 -07002180static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002181 struct wq_barrier *barr,
2182 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002183{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002184 struct list_head *head;
2185 unsigned int linked = 0;
2186
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002187 /*
Tejun Heo8b03ae32010-06-29 10:07:12 +02002188 * debugobject calls are safe here even with gcwq->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002189 * as we know for sure that this will not trigger any of the
2190 * checks and call back into the fixup functions where we
2191 * might deadlock.
2192 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002193 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002194 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002195 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002196
Tejun Heoaffee4b2010-06-29 10:07:12 +02002197 /*
2198 * If @target is currently being executed, schedule the
2199 * barrier to the worker; otherwise, put it after @target.
2200 */
2201 if (worker)
2202 head = worker->scheduled.next;
2203 else {
2204 unsigned long *bits = work_data_bits(target);
2205
2206 head = target->entry.next;
2207 /* there can already be other linked works, inherit and set */
2208 linked = *bits & WORK_STRUCT_LINKED;
2209 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2210 }
2211
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002212 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002213 insert_work(cwq, &barr->work, head,
2214 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002215}
2216
Tejun Heo73f53c42010-06-29 10:07:11 +02002217/**
2218 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2219 * @wq: workqueue being flushed
2220 * @flush_color: new flush color, < 0 for no-op
2221 * @work_color: new work color, < 0 for no-op
2222 *
2223 * Prepare cwqs for workqueue flushing.
2224 *
2225 * If @flush_color is non-negative, flush_color on all cwqs should be
2226 * -1. If no cwq has in-flight commands at the specified color, all
2227 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2228 * has in flight commands, its cwq->flush_color is set to
2229 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2230 * wakeup logic is armed and %true is returned.
2231 *
2232 * The caller should have initialized @wq->first_flusher prior to
2233 * calling this function with non-negative @flush_color. If
2234 * @flush_color is negative, no flush color update is done and %false
2235 * is returned.
2236 *
2237 * If @work_color is non-negative, all cwqs should have the same
2238 * work_color which is previous to @work_color and all will be
2239 * advanced to @work_color.
2240 *
2241 * CONTEXT:
2242 * mutex_lock(wq->flush_mutex).
2243 *
2244 * RETURNS:
2245 * %true if @flush_color >= 0 and there's something to flush. %false
2246 * otherwise.
2247 */
2248static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2249 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250{
Tejun Heo73f53c42010-06-29 10:07:11 +02002251 bool wait = false;
2252 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -07002253
Tejun Heo73f53c42010-06-29 10:07:11 +02002254 if (flush_color >= 0) {
2255 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2256 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002257 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002258
Tejun Heof3421792010-07-02 10:03:51 +02002259 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002260 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo58658882012-07-12 14:46:37 -07002261 struct global_cwq *gcwq = cwq->pool->gcwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002262
Tejun Heo8b03ae32010-06-29 10:07:12 +02002263 spin_lock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002264
2265 if (flush_color >= 0) {
2266 BUG_ON(cwq->flush_color != -1);
2267
2268 if (cwq->nr_in_flight[flush_color]) {
2269 cwq->flush_color = flush_color;
2270 atomic_inc(&wq->nr_cwqs_to_flush);
2271 wait = true;
2272 }
2273 }
2274
2275 if (work_color >= 0) {
2276 BUG_ON(work_color != work_next_color(cwq->work_color));
2277 cwq->work_color = work_color;
2278 }
2279
Tejun Heo8b03ae32010-06-29 10:07:12 +02002280 spin_unlock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002281 }
2282
2283 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2284 complete(&wq->first_flusher->done);
2285
2286 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287}
2288
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002289/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002291 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 *
2293 * Forces execution of the workqueue and blocks until its completion.
2294 * This is typically used in driver shutdown handlers.
2295 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002296 * We sleep until all works which were queued on entry have been handled,
2297 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002299void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300{
Tejun Heo73f53c42010-06-29 10:07:11 +02002301 struct wq_flusher this_flusher = {
2302 .list = LIST_HEAD_INIT(this_flusher.list),
2303 .flush_color = -1,
2304 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2305 };
2306 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002307
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002308 lock_map_acquire(&wq->lockdep_map);
2309 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002310
2311 mutex_lock(&wq->flush_mutex);
2312
2313 /*
2314 * Start-to-wait phase
2315 */
2316 next_color = work_next_color(wq->work_color);
2317
2318 if (next_color != wq->flush_color) {
2319 /*
2320 * Color space is not full. The current work_color
2321 * becomes our flush_color and work_color is advanced
2322 * by one.
2323 */
2324 BUG_ON(!list_empty(&wq->flusher_overflow));
2325 this_flusher.flush_color = wq->work_color;
2326 wq->work_color = next_color;
2327
2328 if (!wq->first_flusher) {
2329 /* no flush in progress, become the first flusher */
2330 BUG_ON(wq->flush_color != this_flusher.flush_color);
2331
2332 wq->first_flusher = &this_flusher;
2333
2334 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2335 wq->work_color)) {
2336 /* nothing to flush, done */
2337 wq->flush_color = next_color;
2338 wq->first_flusher = NULL;
2339 goto out_unlock;
2340 }
2341 } else {
2342 /* wait in queue */
2343 BUG_ON(wq->flush_color == this_flusher.flush_color);
2344 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2345 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2346 }
2347 } else {
2348 /*
2349 * Oops, color space is full, wait on overflow queue.
2350 * The next flush completion will assign us
2351 * flush_color and transfer to flusher_queue.
2352 */
2353 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2354 }
2355
2356 mutex_unlock(&wq->flush_mutex);
2357
2358 wait_for_completion(&this_flusher.done);
2359
2360 /*
2361 * Wake-up-and-cascade phase
2362 *
2363 * First flushers are responsible for cascading flushes and
2364 * handling overflow. Non-first flushers can simply return.
2365 */
2366 if (wq->first_flusher != &this_flusher)
2367 return;
2368
2369 mutex_lock(&wq->flush_mutex);
2370
Tejun Heo4ce48b32010-07-02 10:03:51 +02002371 /* we might have raced, check again with mutex held */
2372 if (wq->first_flusher != &this_flusher)
2373 goto out_unlock;
2374
Tejun Heo73f53c42010-06-29 10:07:11 +02002375 wq->first_flusher = NULL;
2376
2377 BUG_ON(!list_empty(&this_flusher.list));
2378 BUG_ON(wq->flush_color != this_flusher.flush_color);
2379
2380 while (true) {
2381 struct wq_flusher *next, *tmp;
2382
2383 /* complete all the flushers sharing the current flush color */
2384 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2385 if (next->flush_color != wq->flush_color)
2386 break;
2387 list_del_init(&next->list);
2388 complete(&next->done);
2389 }
2390
2391 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2392 wq->flush_color != work_next_color(wq->work_color));
2393
2394 /* this flush_color is finished, advance by one */
2395 wq->flush_color = work_next_color(wq->flush_color);
2396
2397 /* one color has been freed, handle overflow queue */
2398 if (!list_empty(&wq->flusher_overflow)) {
2399 /*
2400 * Assign the same color to all overflowed
2401 * flushers, advance work_color and append to
2402 * flusher_queue. This is the start-to-wait
2403 * phase for these overflowed flushers.
2404 */
2405 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2406 tmp->flush_color = wq->work_color;
2407
2408 wq->work_color = work_next_color(wq->work_color);
2409
2410 list_splice_tail_init(&wq->flusher_overflow,
2411 &wq->flusher_queue);
2412 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2413 }
2414
2415 if (list_empty(&wq->flusher_queue)) {
2416 BUG_ON(wq->flush_color != wq->work_color);
2417 break;
2418 }
2419
2420 /*
2421 * Need to flush more colors. Make the next flusher
2422 * the new first flusher and arm cwqs.
2423 */
2424 BUG_ON(wq->flush_color == wq->work_color);
2425 BUG_ON(wq->flush_color != next->flush_color);
2426
2427 list_del_init(&next->list);
2428 wq->first_flusher = next;
2429
2430 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2431 break;
2432
2433 /*
2434 * Meh... this color is already done, clear first
2435 * flusher and repeat cascading.
2436 */
2437 wq->first_flusher = NULL;
2438 }
2439
2440out_unlock:
2441 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442}
Dave Jonesae90dd52006-06-30 01:40:45 -04002443EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002445/**
2446 * drain_workqueue - drain a workqueue
2447 * @wq: workqueue to drain
2448 *
2449 * Wait until the workqueue becomes empty. While draining is in progress,
2450 * only chain queueing is allowed. IOW, only currently pending or running
2451 * work items on @wq can queue further work items on it. @wq is flushed
2452 * repeatedly until it becomes empty. The number of flushing is detemined
2453 * by the depth of chaining and should be relatively short. Whine if it
2454 * takes too long.
2455 */
2456void drain_workqueue(struct workqueue_struct *wq)
2457{
2458 unsigned int flush_cnt = 0;
2459 unsigned int cpu;
2460
2461 /*
2462 * __queue_work() needs to test whether there are drainers, is much
2463 * hotter than drain_workqueue() and already looks at @wq->flags.
2464 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2465 */
2466 spin_lock(&workqueue_lock);
2467 if (!wq->nr_drainers++)
2468 wq->flags |= WQ_DRAINING;
2469 spin_unlock(&workqueue_lock);
2470reflush:
2471 flush_workqueue(wq);
2472
2473 for_each_cwq_cpu(cpu, wq) {
2474 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002475 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002476
Tejun Heo58658882012-07-12 14:46:37 -07002477 spin_lock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002478 drained = !cwq->nr_active && list_empty(&cwq->delayed_works);
Tejun Heo58658882012-07-12 14:46:37 -07002479 spin_unlock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002480
2481 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002482 continue;
2483
2484 if (++flush_cnt == 10 ||
2485 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
2486 pr_warning("workqueue %s: flush on destruction isn't complete after %u tries\n",
2487 wq->name, flush_cnt);
2488 goto reflush;
2489 }
2490
2491 spin_lock(&workqueue_lock);
2492 if (!--wq->nr_drainers)
2493 wq->flags &= ~WQ_DRAINING;
2494 spin_unlock(&workqueue_lock);
2495}
2496EXPORT_SYMBOL_GPL(drain_workqueue);
2497
Tejun Heobaf59022010-09-16 10:42:16 +02002498static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
2499 bool wait_executing)
2500{
2501 struct worker *worker = NULL;
2502 struct global_cwq *gcwq;
2503 struct cpu_workqueue_struct *cwq;
2504
2505 might_sleep();
2506 gcwq = get_work_gcwq(work);
2507 if (!gcwq)
2508 return false;
2509
2510 spin_lock_irq(&gcwq->lock);
2511 if (!list_empty(&work->entry)) {
2512 /*
2513 * See the comment near try_to_grab_pending()->smp_rmb().
2514 * If it was re-queued to a different gcwq under us, we
2515 * are not going to wait.
2516 */
2517 smp_rmb();
2518 cwq = get_work_cwq(work);
Tejun Heo58658882012-07-12 14:46:37 -07002519 if (unlikely(!cwq || gcwq != cwq->pool->gcwq))
Tejun Heobaf59022010-09-16 10:42:16 +02002520 goto already_gone;
2521 } else if (wait_executing) {
2522 worker = find_worker_executing_work(gcwq, work);
2523 if (!worker)
2524 goto already_gone;
2525 cwq = worker->current_cwq;
2526 } else
2527 goto already_gone;
2528
2529 insert_wq_barrier(cwq, barr, work, worker);
2530 spin_unlock_irq(&gcwq->lock);
2531
Tejun Heoe1594892011-01-09 23:32:15 +01002532 /*
2533 * If @max_active is 1 or rescuer is in use, flushing another work
2534 * item on the same workqueue may lead to deadlock. Make sure the
2535 * flusher is not running on the same workqueue by verifying write
2536 * access.
2537 */
2538 if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER)
2539 lock_map_acquire(&cwq->wq->lockdep_map);
2540 else
2541 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heobaf59022010-09-16 10:42:16 +02002542 lock_map_release(&cwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002543
Tejun Heobaf59022010-09-16 10:42:16 +02002544 return true;
2545already_gone:
2546 spin_unlock_irq(&gcwq->lock);
2547 return false;
2548}
2549
Oleg Nesterovdb700892008-07-25 01:47:49 -07002550/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002551 * flush_work - wait for a work to finish executing the last queueing instance
2552 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002553 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002554 * Wait until @work has finished execution. This function considers
2555 * only the last queueing instance of @work. If @work has been
2556 * enqueued across different CPUs on a non-reentrant workqueue or on
2557 * multiple workqueues, @work might still be executing on return on
2558 * some of the CPUs from earlier queueing.
Oleg Nesterova67da702008-07-25 01:47:52 -07002559 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002560 * If @work was queued only on a non-reentrant, ordered or unbound
2561 * workqueue, @work is guaranteed to be idle on return if it hasn't
2562 * been requeued since flush started.
2563 *
2564 * RETURNS:
2565 * %true if flush_work() waited for the work to finish execution,
2566 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002567 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002568bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002569{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002570 struct wq_barrier barr;
2571
Tejun Heobaf59022010-09-16 10:42:16 +02002572 if (start_flush_work(work, &barr, true)) {
2573 wait_for_completion(&barr.done);
2574 destroy_work_on_stack(&barr.work);
2575 return true;
2576 } else
2577 return false;
Oleg Nesterovdb700892008-07-25 01:47:49 -07002578}
2579EXPORT_SYMBOL_GPL(flush_work);
2580
Tejun Heo401a8d02010-09-16 10:36:00 +02002581static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work)
2582{
2583 struct wq_barrier barr;
2584 struct worker *worker;
2585
2586 spin_lock_irq(&gcwq->lock);
2587
2588 worker = find_worker_executing_work(gcwq, work);
2589 if (unlikely(worker))
2590 insert_wq_barrier(worker->current_cwq, &barr, work, worker);
2591
2592 spin_unlock_irq(&gcwq->lock);
2593
2594 if (unlikely(worker)) {
2595 wait_for_completion(&barr.done);
2596 destroy_work_on_stack(&barr.work);
2597 return true;
2598 } else
2599 return false;
2600}
2601
2602static bool wait_on_work(struct work_struct *work)
2603{
2604 bool ret = false;
2605 int cpu;
2606
2607 might_sleep();
2608
2609 lock_map_acquire(&work->lockdep_map);
2610 lock_map_release(&work->lockdep_map);
2611
2612 for_each_gcwq_cpu(cpu)
2613 ret |= wait_on_cpu_work(get_gcwq(cpu), work);
2614 return ret;
2615}
2616
Tejun Heo09383492010-09-16 10:48:29 +02002617/**
2618 * flush_work_sync - wait until a work has finished execution
2619 * @work: the work to flush
2620 *
2621 * Wait until @work has finished execution. On return, it's
2622 * guaranteed that all queueing instances of @work which happened
2623 * before this function is called are finished. In other words, if
2624 * @work hasn't been requeued since this function was called, @work is
2625 * guaranteed to be idle on return.
2626 *
2627 * RETURNS:
2628 * %true if flush_work_sync() waited for the work to finish execution,
2629 * %false if it was already idle.
2630 */
2631bool flush_work_sync(struct work_struct *work)
2632{
2633 struct wq_barrier barr;
2634 bool pending, waited;
2635
2636 /* we'll wait for executions separately, queue barr only if pending */
2637 pending = start_flush_work(work, &barr, false);
2638
2639 /* wait for executions to finish */
2640 waited = wait_on_work(work);
2641
2642 /* wait for the pending one */
2643 if (pending) {
2644 wait_for_completion(&barr.done);
2645 destroy_work_on_stack(&barr.work);
2646 }
2647
2648 return pending || waited;
2649}
2650EXPORT_SYMBOL_GPL(flush_work_sync);
2651
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002652/*
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002653 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002654 * so this work can't be re-armed in any way.
2655 */
2656static int try_to_grab_pending(struct work_struct *work)
2657{
Tejun Heo8b03ae32010-06-29 10:07:12 +02002658 struct global_cwq *gcwq;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002659 int ret = -1;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002660
Tejun Heo22df02b2010-06-29 10:07:10 +02002661 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002662 return 0;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002663
2664 /*
2665 * The queueing is in progress, or it is already queued. Try to
2666 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
2667 */
Tejun Heo7a22ad72010-06-29 10:07:13 +02002668 gcwq = get_work_gcwq(work);
2669 if (!gcwq)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002670 return ret;
2671
Tejun Heo8b03ae32010-06-29 10:07:12 +02002672 spin_lock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002673 if (!list_empty(&work->entry)) {
2674 /*
Tejun Heo7a22ad72010-06-29 10:07:13 +02002675 * This work is queued, but perhaps we locked the wrong gcwq.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002676 * In that case we must see the new value after rmb(), see
2677 * insert_work()->wmb().
2678 */
2679 smp_rmb();
Tejun Heo7a22ad72010-06-29 10:07:13 +02002680 if (gcwq == get_work_gcwq(work)) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002681 debug_work_deactivate(work);
Lai Jiangshan31eafff2012-09-18 10:40:00 -07002682
2683 /*
2684 * A delayed work item cannot be grabbed directly
2685 * because it might have linked NO_COLOR work items
2686 * which, if left on the delayed_list, will confuse
2687 * cwq->nr_active management later on and cause
2688 * stall. Make sure the work item is activated
2689 * before grabbing.
2690 */
2691 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
2692 cwq_activate_delayed_work(work);
2693
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002694 list_del_init(&work->entry);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002695 cwq_dec_nr_in_flight(get_work_cwq(work),
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02002696 get_work_color(work),
2697 *work_data_bits(work) & WORK_STRUCT_DELAYED);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002698 ret = 1;
2699 }
2700 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02002701 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002702
2703 return ret;
2704}
2705
Tejun Heo401a8d02010-09-16 10:36:00 +02002706static bool __cancel_work_timer(struct work_struct *work,
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002707 struct timer_list* timer)
2708{
2709 int ret;
2710
2711 do {
2712 ret = (timer && likely(del_timer(timer)));
2713 if (!ret)
2714 ret = try_to_grab_pending(work);
2715 wait_on_work(work);
2716 } while (unlikely(ret < 0));
2717
Tejun Heo7a22ad72010-06-29 10:07:13 +02002718 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002719 return ret;
2720}
2721
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002722/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002723 * cancel_work_sync - cancel a work and wait for it to finish
2724 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002725 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002726 * Cancel @work and wait for its execution to finish. This function
2727 * can be used even if the work re-queues itself or migrates to
2728 * another workqueue. On return from this function, @work is
2729 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002730 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002731 * cancel_work_sync(&delayed_work->work) must not be used for
2732 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002733 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002734 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002735 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002736 *
2737 * RETURNS:
2738 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002739 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002740bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002741{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002742 return __cancel_work_timer(work, NULL);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002743}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002744EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002745
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002746/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002747 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2748 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002749 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002750 * Delayed timer is cancelled and the pending work is queued for
2751 * immediate execution. Like flush_work(), this function only
2752 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002753 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002754 * RETURNS:
2755 * %true if flush_work() waited for the work to finish execution,
2756 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002757 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002758bool flush_delayed_work(struct delayed_work *dwork)
2759{
2760 if (del_timer_sync(&dwork->timer))
2761 __queue_work(raw_smp_processor_id(),
2762 get_work_cwq(&dwork->work)->wq, &dwork->work);
2763 return flush_work(&dwork->work);
2764}
2765EXPORT_SYMBOL(flush_delayed_work);
2766
2767/**
Tejun Heo09383492010-09-16 10:48:29 +02002768 * flush_delayed_work_sync - wait for a dwork to finish
2769 * @dwork: the delayed work to flush
2770 *
2771 * Delayed timer is cancelled and the pending work is queued for
2772 * execution immediately. Other than timer handling, its behavior
2773 * is identical to flush_work_sync().
2774 *
2775 * RETURNS:
2776 * %true if flush_work_sync() waited for the work to finish execution,
2777 * %false if it was already idle.
2778 */
2779bool flush_delayed_work_sync(struct delayed_work *dwork)
2780{
2781 if (del_timer_sync(&dwork->timer))
2782 __queue_work(raw_smp_processor_id(),
2783 get_work_cwq(&dwork->work)->wq, &dwork->work);
2784 return flush_work_sync(&dwork->work);
2785}
2786EXPORT_SYMBOL(flush_delayed_work_sync);
2787
2788/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002789 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2790 * @dwork: the delayed work cancel
2791 *
2792 * This is cancel_work_sync() for delayed works.
2793 *
2794 * RETURNS:
2795 * %true if @dwork was pending, %false otherwise.
2796 */
2797bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002798{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002799 return __cancel_work_timer(&dwork->work, &dwork->timer);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002800}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002801EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002803/**
2804 * schedule_work - put work task in global workqueue
2805 * @work: job to be done
2806 *
Bart Van Assche5b0f437d2009-07-30 19:00:53 +02002807 * Returns zero if @work was already on the kernel-global workqueue and
2808 * non-zero otherwise.
2809 *
2810 * This puts a job in the kernel-global workqueue if it was not already
2811 * queued and leaves it in the same position on the kernel-global
2812 * workqueue otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002813 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002814int schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815{
Tejun Heod320c032010-06-29 10:07:14 +02002816 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817}
Dave Jonesae90dd52006-06-30 01:40:45 -04002818EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819
Zhang Ruic1a220e2008-07-23 21:28:39 -07002820/*
2821 * schedule_work_on - put work task on a specific cpu
2822 * @cpu: cpu to put the work task on
2823 * @work: job to be done
2824 *
2825 * This puts a job on a specific cpu
2826 */
2827int schedule_work_on(int cpu, struct work_struct *work)
2828{
Tejun Heod320c032010-06-29 10:07:14 +02002829 return queue_work_on(cpu, system_wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07002830}
2831EXPORT_SYMBOL(schedule_work_on);
2832
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002833/**
2834 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +00002835 * @dwork: job to be done
2836 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002837 *
2838 * After waiting for a given time this puts a job in the kernel-global
2839 * workqueue.
2840 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002841int schedule_delayed_work(struct delayed_work *dwork,
Ingo Molnar82f67cd2007-02-16 01:28:13 -08002842 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843{
Tejun Heod320c032010-06-29 10:07:14 +02002844 return queue_delayed_work(system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845}
Dave Jonesae90dd52006-06-30 01:40:45 -04002846EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002848/**
2849 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
2850 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +00002851 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002852 * @delay: number of jiffies to wait
2853 *
2854 * After waiting for a given time this puts a job in the kernel-global
2855 * workqueue on the specified CPU.
2856 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +00002858 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859{
Tejun Heod320c032010-06-29 10:07:14 +02002860 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861}
Dave Jonesae90dd52006-06-30 01:40:45 -04002862EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
Andrew Mortonb6136772006-06-25 05:47:49 -07002864/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002865 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002866 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002867 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002868 * schedule_on_each_cpu() executes @func on each online CPU using the
2869 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002870 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002871 *
2872 * RETURNS:
2873 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002874 */
David Howells65f27f32006-11-22 14:55:48 +00002875int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002876{
2877 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002878 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002879
Andrew Mortonb6136772006-06-25 05:47:49 -07002880 works = alloc_percpu(struct work_struct);
2881 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002882 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002883
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002884 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002885
Christoph Lameter15316ba2006-01-08 01:00:43 -08002886 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002887 struct work_struct *work = per_cpu_ptr(works, cpu);
2888
2889 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002890 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002891 }
Tejun Heo93981802009-11-17 14:06:20 -08002892
2893 for_each_online_cpu(cpu)
2894 flush_work(per_cpu_ptr(works, cpu));
2895
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002896 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002897 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002898 return 0;
2899}
2900
Alan Sterneef6a7d2010-02-12 17:39:21 +09002901/**
2902 * flush_scheduled_work - ensure that any scheduled work has run to completion.
2903 *
2904 * Forces execution of the kernel-global workqueue and blocks until its
2905 * completion.
2906 *
2907 * Think twice before calling this function! It's very easy to get into
2908 * trouble if you don't take great care. Either of the following situations
2909 * will lead to deadlock:
2910 *
2911 * One of the work items currently on the workqueue needs to acquire
2912 * a lock held by your code or its caller.
2913 *
2914 * Your code is running in the context of a work routine.
2915 *
2916 * They will be detected by lockdep when they occur, but the first might not
2917 * occur very often. It depends on what work items are on the workqueue and
2918 * what locks they need, which you have no control over.
2919 *
2920 * In most situations flushing the entire workqueue is overkill; you merely
2921 * need to know that a particular work item isn't queued and isn't running.
2922 * In such cases you should use cancel_delayed_work_sync() or
2923 * cancel_work_sync() instead.
2924 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925void flush_scheduled_work(void)
2926{
Tejun Heod320c032010-06-29 10:07:14 +02002927 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928}
Dave Jonesae90dd52006-06-30 01:40:45 -04002929EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
2931/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06002932 * execute_in_process_context - reliably execute the routine with user context
2933 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06002934 * @ew: guaranteed storage for the execute work structure (must
2935 * be available when the work executes)
2936 *
2937 * Executes the function immediately if process context is available,
2938 * otherwise schedules the function for delayed execution.
2939 *
2940 * Returns: 0 - function was executed
2941 * 1 - function was scheduled for execution
2942 */
David Howells65f27f32006-11-22 14:55:48 +00002943int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06002944{
2945 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00002946 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002947 return 0;
2948 }
2949
David Howells65f27f32006-11-22 14:55:48 +00002950 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002951 schedule_work(&ew->work);
2952
2953 return 1;
2954}
2955EXPORT_SYMBOL_GPL(execute_in_process_context);
2956
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957int keventd_up(void)
2958{
Tejun Heod320c032010-06-29 10:07:14 +02002959 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960}
2961
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002962static int alloc_cwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963{
Oleg Nesterov3af244332007-05-09 02:34:09 -07002964 /*
Tejun Heo0f900042010-06-29 10:07:11 +02002965 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
2966 * Make sure that the alignment isn't lower than that of
2967 * unsigned long long.
Oleg Nesterov3af244332007-05-09 02:34:09 -07002968 */
Tejun Heo0f900042010-06-29 10:07:11 +02002969 const size_t size = sizeof(struct cpu_workqueue_struct);
2970 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
2971 __alignof__(unsigned long long));
Oleg Nesterov3af244332007-05-09 02:34:09 -07002972
Lai Jiangshane06ffa12012-03-09 18:03:20 +08002973 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02002974 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
Tejun Heo931ac772010-07-20 11:07:48 +02002975 else {
Tejun Heof3421792010-07-02 10:03:51 +02002976 void *ptr;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01002977
Tejun Heof3421792010-07-02 10:03:51 +02002978 /*
2979 * Allocate enough room to align cwq and put an extra
2980 * pointer at the end pointing back to the originally
2981 * allocated pointer which will be used for free.
2982 */
2983 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
2984 if (ptr) {
2985 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
2986 *(void **)(wq->cpu_wq.single + 1) = ptr;
2987 }
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002988 }
Tejun Heof3421792010-07-02 10:03:51 +02002989
Tejun Heo0415b002011-03-24 18:50:09 +01002990 /* just in case, make sure it's actually aligned */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002991 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
2992 return wq->cpu_wq.v ? 0 : -ENOMEM;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002993}
2994
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002995static void free_cwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002996{
Lai Jiangshane06ffa12012-03-09 18:03:20 +08002997 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02002998 free_percpu(wq->cpu_wq.pcpu);
2999 else if (wq->cpu_wq.single) {
3000 /* the pointer to free is stored right after the cwq */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003001 kfree(*(void **)(wq->cpu_wq.single + 1));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07003002 }
3003}
3004
Tejun Heof3421792010-07-02 10:03:51 +02003005static int wq_clamp_max_active(int max_active, unsigned int flags,
3006 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003007{
Tejun Heof3421792010-07-02 10:03:51 +02003008 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3009
3010 if (max_active < 1 || max_active > lim)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003011 printk(KERN_WARNING "workqueue: max_active %d requested for %s "
3012 "is out of range, clamping between %d and %d\n",
Tejun Heof3421792010-07-02 10:03:51 +02003013 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003014
Tejun Heof3421792010-07-02 10:03:51 +02003015 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003016}
3017
Tejun Heob196be82012-01-10 15:11:35 -08003018struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02003019 unsigned int flags,
3020 int max_active,
3021 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08003022 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003023{
Tejun Heob196be82012-01-10 15:11:35 -08003024 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003025 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02003026 unsigned int cpu;
shumashb2f60df2015-07-18 09:03:08 -06003027 /* see the comment above the definition of WQ_POWER_EFFICIENT */
3028 if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient)
3029 flags |= WQ_UNBOUND;
3030
Tejun Heob196be82012-01-10 15:11:35 -08003031 size_t namelen;
3032
3033 /* determine namelen, allocate wq and format name */
3034 va_start(args, lock_name);
3035 va_copy(args1, args);
3036 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3037
3038 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3039 if (!wq)
3040 goto err;
3041
3042 vsnprintf(wq->name, namelen, fmt, args1);
3043 va_end(args);
3044 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003045
Tejun Heof3421792010-07-02 10:03:51 +02003046 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02003047 * Workqueues which may be used during memory reclaim should
3048 * have a rescuer to guarantee forward progress.
3049 */
3050 if (flags & WQ_MEM_RECLAIM)
3051 flags |= WQ_RESCUER;
3052
Tejun Heod320c032010-06-29 10:07:14 +02003053 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003054 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003055
Tejun Heob196be82012-01-10 15:11:35 -08003056 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003057 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003058 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003059 mutex_init(&wq->flush_mutex);
3060 atomic_set(&wq->nr_cwqs_to_flush, 0);
3061 INIT_LIST_HEAD(&wq->flusher_queue);
3062 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003063
Johannes Bergeb13ba82008-01-16 09:51:58 +01003064 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003065 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003066
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003067 if (alloc_cwqs(wq) < 0)
3068 goto err;
3069
Tejun Heof3421792010-07-02 10:03:51 +02003070 for_each_cwq_cpu(cpu, wq) {
Tejun Heo15376632010-06-29 10:07:11 +02003071 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003072 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heodcb32ee2012-07-13 22:16:45 -07003073 int pool_idx = (bool)(flags & WQ_HIGHPRI);
Tejun Heo15376632010-06-29 10:07:11 +02003074
Tejun Heo0f900042010-06-29 10:07:11 +02003075 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heodcb32ee2012-07-13 22:16:45 -07003076 cwq->pool = &gcwq->pools[pool_idx];
Tejun Heoc34056a2010-06-29 10:07:11 +02003077 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02003078 cwq->flush_color = -1;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003079 cwq->max_active = max_active;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003080 INIT_LIST_HEAD(&cwq->delayed_works);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003081 }
3082
Tejun Heoe22bee72010-06-29 10:07:14 +02003083 if (flags & WQ_RESCUER) {
3084 struct worker *rescuer;
3085
Tejun Heof2e005a2010-07-20 15:59:09 +02003086 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
Tejun Heoe22bee72010-06-29 10:07:14 +02003087 goto err;
3088
3089 wq->rescuer = rescuer = alloc_worker();
3090 if (!rescuer)
3091 goto err;
3092
Tejun Heob196be82012-01-10 15:11:35 -08003093 rescuer->task = kthread_create(rescuer_thread, wq, "%s",
3094 wq->name);
Tejun Heoe22bee72010-06-29 10:07:14 +02003095 if (IS_ERR(rescuer->task))
3096 goto err;
3097
Tejun Heoe22bee72010-06-29 10:07:14 +02003098 rescuer->task->flags |= PF_THREAD_BOUND;
3099 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003100 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003101
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003102 /*
3103 * workqueue_lock protects global freeze state and workqueues
3104 * list. Grab it, set max_active accordingly and add the new
3105 * workqueue to workqueues list.
3106 */
Tejun Heo15376632010-06-29 10:07:11 +02003107 spin_lock(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003108
Tejun Heo58a69cb2011-02-16 09:25:31 +01003109 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heof3421792010-07-02 10:03:51 +02003110 for_each_cwq_cpu(cpu, wq)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003111 get_cwq(cpu, wq)->max_active = 0;
3112
Tejun Heo15376632010-06-29 10:07:11 +02003113 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003114
Tejun Heo15376632010-06-29 10:07:11 +02003115 spin_unlock(&workqueue_lock);
3116
Oleg Nesterov3af244332007-05-09 02:34:09 -07003117 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02003118err:
3119 if (wq) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003120 free_cwqs(wq);
Tejun Heof2e005a2010-07-20 15:59:09 +02003121 free_mayday_mask(wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02003122 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003123 kfree(wq);
3124 }
3125 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003126}
Tejun Heod320c032010-06-29 10:07:14 +02003127EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003128
3129/**
3130 * destroy_workqueue - safely terminate a workqueue
3131 * @wq: target workqueue
3132 *
3133 * Safely destroy a workqueue. All work currently pending will be done first.
3134 */
3135void destroy_workqueue(struct workqueue_struct *wq)
3136{
Tejun Heoc8e55f32010-06-29 10:07:12 +02003137 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003138
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003139 /* drain it before proceeding with destruction */
3140 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003141
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003142 /*
3143 * wq list is used to freeze wq, remove from list after
3144 * flushing is complete in case freeze races us.
3145 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003146 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07003147 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003148 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003149
Tejun Heoe22bee72010-06-29 10:07:14 +02003150 /* sanity check */
Tejun Heof3421792010-07-02 10:03:51 +02003151 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02003152 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3153 int i;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003154
Tejun Heo73f53c42010-06-29 10:07:11 +02003155 for (i = 0; i < WORK_NR_COLORS; i++)
3156 BUG_ON(cwq->nr_in_flight[i]);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003157 BUG_ON(cwq->nr_active);
3158 BUG_ON(!list_empty(&cwq->delayed_works));
Tejun Heo73f53c42010-06-29 10:07:11 +02003159 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003160
Tejun Heoe22bee72010-06-29 10:07:14 +02003161 if (wq->flags & WQ_RESCUER) {
3162 kthread_stop(wq->rescuer->task);
Tejun Heof2e005a2010-07-20 15:59:09 +02003163 free_mayday_mask(wq->mayday_mask);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003164 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003165 }
3166
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003167 free_cwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003168 kfree(wq);
3169}
3170EXPORT_SYMBOL_GPL(destroy_workqueue);
3171
Tejun Heodcd989c2010-06-29 10:07:14 +02003172/**
3173 * workqueue_set_max_active - adjust max_active of a workqueue
3174 * @wq: target workqueue
3175 * @max_active: new max_active value.
3176 *
3177 * Set max_active of @wq to @max_active.
3178 *
3179 * CONTEXT:
3180 * Don't call from IRQ context.
3181 */
3182void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3183{
3184 unsigned int cpu;
3185
Tejun Heof3421792010-07-02 10:03:51 +02003186 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003187
3188 spin_lock(&workqueue_lock);
3189
3190 wq->saved_max_active = max_active;
3191
Tejun Heof3421792010-07-02 10:03:51 +02003192 for_each_cwq_cpu(cpu, wq) {
Tejun Heodcd989c2010-06-29 10:07:14 +02003193 struct global_cwq *gcwq = get_gcwq(cpu);
3194
3195 spin_lock_irq(&gcwq->lock);
3196
Tejun Heo58a69cb2011-02-16 09:25:31 +01003197 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heodcd989c2010-06-29 10:07:14 +02003198 !(gcwq->flags & GCWQ_FREEZING))
3199 get_cwq(gcwq->cpu, wq)->max_active = max_active;
3200
3201 spin_unlock_irq(&gcwq->lock);
3202 }
3203
3204 spin_unlock(&workqueue_lock);
3205}
3206EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3207
3208/**
3209 * workqueue_congested - test whether a workqueue is congested
3210 * @cpu: CPU in question
3211 * @wq: target workqueue
3212 *
3213 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3214 * no synchronization around this function and the test result is
3215 * unreliable and only useful as advisory hints or for debugging.
3216 *
3217 * RETURNS:
3218 * %true if congested, %false otherwise.
3219 */
3220bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3221{
3222 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3223
3224 return !list_empty(&cwq->delayed_works);
3225}
3226EXPORT_SYMBOL_GPL(workqueue_congested);
3227
3228/**
3229 * work_cpu - return the last known associated cpu for @work
3230 * @work: the work of interest
3231 *
3232 * RETURNS:
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003233 * CPU number if @work was ever queued. WORK_CPU_NONE otherwise.
Tejun Heodcd989c2010-06-29 10:07:14 +02003234 */
3235unsigned int work_cpu(struct work_struct *work)
3236{
3237 struct global_cwq *gcwq = get_work_gcwq(work);
3238
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003239 return gcwq ? gcwq->cpu : WORK_CPU_NONE;
Tejun Heodcd989c2010-06-29 10:07:14 +02003240}
3241EXPORT_SYMBOL_GPL(work_cpu);
3242
3243/**
3244 * work_busy - test whether a work is currently pending or running
3245 * @work: the work to be tested
3246 *
3247 * Test whether @work is currently pending or running. There is no
3248 * synchronization around this function and the test result is
3249 * unreliable and only useful as advisory hints or for debugging.
3250 * Especially for reentrant wqs, the pending state might hide the
3251 * running state.
3252 *
3253 * RETURNS:
3254 * OR'd bitmask of WORK_BUSY_* bits.
3255 */
3256unsigned int work_busy(struct work_struct *work)
3257{
3258 struct global_cwq *gcwq = get_work_gcwq(work);
3259 unsigned long flags;
3260 unsigned int ret = 0;
3261
3262 if (!gcwq)
3263 return false;
3264
3265 spin_lock_irqsave(&gcwq->lock, flags);
3266
3267 if (work_pending(work))
3268 ret |= WORK_BUSY_PENDING;
3269 if (find_worker_executing_work(gcwq, work))
3270 ret |= WORK_BUSY_RUNNING;
3271
3272 spin_unlock_irqrestore(&gcwq->lock, flags);
3273
3274 return ret;
3275}
3276EXPORT_SYMBOL_GPL(work_busy);
3277
Tejun Heodb7bccf2010-06-29 10:07:12 +02003278/*
3279 * CPU hotplug.
3280 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003281 * There are two challenges in supporting CPU hotplug. Firstly, there
3282 * are a lot of assumptions on strong associations among work, cwq and
3283 * gcwq which make migrating pending and scheduled works very
3284 * difficult to implement without impacting hot paths. Secondly,
3285 * gcwqs serve mix of short, long and very long running works making
3286 * blocked draining impractical.
3287 *
3288 * This is solved by allowing a gcwq to be detached from CPU, running
3289 * it with unbound (rogue) workers and allowing it to be reattached
3290 * later if the cpu comes back online. A separate thread is created
3291 * to govern a gcwq in such state and is called the trustee of the
3292 * gcwq.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003293 *
3294 * Trustee states and their descriptions.
3295 *
3296 * START Command state used on startup. On CPU_DOWN_PREPARE, a
3297 * new trustee is started with this state.
3298 *
3299 * IN_CHARGE Once started, trustee will enter this state after
Tejun Heoe22bee72010-06-29 10:07:14 +02003300 * assuming the manager role and making all existing
3301 * workers rogue. DOWN_PREPARE waits for trustee to
3302 * enter this state. After reaching IN_CHARGE, trustee
3303 * tries to execute the pending worklist until it's empty
3304 * and the state is set to BUTCHER, or the state is set
3305 * to RELEASE.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003306 *
3307 * BUTCHER Command state which is set by the cpu callback after
3308 * the cpu has went down. Once this state is set trustee
3309 * knows that there will be no new works on the worklist
3310 * and once the worklist is empty it can proceed to
3311 * killing idle workers.
3312 *
3313 * RELEASE Command state which is set by the cpu callback if the
3314 * cpu down has been canceled or it has come online
3315 * again. After recognizing this state, trustee stops
Tejun Heoe22bee72010-06-29 10:07:14 +02003316 * trying to drain or butcher and clears ROGUE, rebinds
3317 * all remaining workers back to the cpu and releases
3318 * manager role.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003319 *
3320 * DONE Trustee will enter this state after BUTCHER or RELEASE
3321 * is complete.
3322 *
3323 * trustee CPU draining
3324 * took over down complete
3325 * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE
3326 * | | ^
3327 * | CPU is back online v return workers |
3328 * ----------------> RELEASE --------------
3329 */
3330
3331/**
3332 * trustee_wait_event_timeout - timed event wait for trustee
3333 * @cond: condition to wait for
3334 * @timeout: timeout in jiffies
3335 *
3336 * wait_event_timeout() for trustee to use. Handles locking and
3337 * checks for RELEASE request.
3338 *
3339 * CONTEXT:
3340 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3341 * multiple times. To be used by trustee.
3342 *
3343 * RETURNS:
3344 * Positive indicating left time if @cond is satisfied, 0 if timed
3345 * out, -1 if canceled.
3346 */
3347#define trustee_wait_event_timeout(cond, timeout) ({ \
3348 long __ret = (timeout); \
3349 while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) && \
3350 __ret) { \
3351 spin_unlock_irq(&gcwq->lock); \
3352 __wait_event_timeout(gcwq->trustee_wait, (cond) || \
3353 (gcwq->trustee_state == TRUSTEE_RELEASE), \
3354 __ret); \
3355 spin_lock_irq(&gcwq->lock); \
3356 } \
3357 gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret); \
3358})
3359
3360/**
3361 * trustee_wait_event - event wait for trustee
3362 * @cond: condition to wait for
3363 *
3364 * wait_event() for trustee to use. Automatically handles locking and
3365 * checks for CANCEL request.
3366 *
3367 * CONTEXT:
3368 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3369 * multiple times. To be used by trustee.
3370 *
3371 * RETURNS:
3372 * 0 if @cond is satisfied, -1 if canceled.
3373 */
3374#define trustee_wait_event(cond) ({ \
3375 long __ret1; \
3376 __ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\
3377 __ret1 < 0 ? -1 : 0; \
3378})
3379
Tejun Heo9c6bae02012-07-13 22:16:44 -07003380static bool gcwq_is_managing_workers(struct global_cwq *gcwq)
3381{
3382 struct worker_pool *pool;
3383
3384 for_each_worker_pool(pool, gcwq)
3385 if (pool->flags & POOL_MANAGING_WORKERS)
3386 return true;
3387 return false;
3388}
3389
3390static bool gcwq_has_idle_workers(struct global_cwq *gcwq)
3391{
3392 struct worker_pool *pool;
3393
3394 for_each_worker_pool(pool, gcwq)
3395 if (!list_empty(&pool->idle_list))
3396 return true;
3397 return false;
3398}
3399
Tejun Heodb7bccf2010-06-29 10:07:12 +02003400static int __cpuinit trustee_thread(void *__gcwq)
3401{
3402 struct global_cwq *gcwq = __gcwq;
Tejun Heo9c6bae02012-07-13 22:16:44 -07003403 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003404 struct worker *worker;
Tejun Heoe22bee72010-06-29 10:07:14 +02003405 struct work_struct *work;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003406 struct hlist_node *pos;
Tejun Heoe22bee72010-06-29 10:07:14 +02003407 long rc;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003408 int i;
3409
3410 BUG_ON(gcwq->cpu != smp_processor_id());
3411
3412 spin_lock_irq(&gcwq->lock);
3413 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003414 * Claim the manager position and make all workers rogue.
3415 * Trustee must be bound to the target cpu and can't be
3416 * cancelled.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003417 */
3418 BUG_ON(gcwq->cpu != smp_processor_id());
Tejun Heo9c6bae02012-07-13 22:16:44 -07003419 rc = trustee_wait_event(!gcwq_is_managing_workers(gcwq));
Tejun Heoe22bee72010-06-29 10:07:14 +02003420 BUG_ON(rc < 0);
3421
Tejun Heo9c6bae02012-07-13 22:16:44 -07003422 for_each_worker_pool(pool, gcwq) {
3423 pool->flags |= POOL_MANAGING_WORKERS;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003424
Tejun Heo9c6bae02012-07-13 22:16:44 -07003425 list_for_each_entry(worker, &pool->idle_list, entry)
3426 worker->flags |= WORKER_ROGUE;
3427 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003428
3429 for_each_busy_worker(worker, i, pos, gcwq)
Tejun Heocb444762010-07-02 10:03:50 +02003430 worker->flags |= WORKER_ROGUE;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003431
3432 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003433 * Call schedule() so that we cross rq->lock and thus can
3434 * guarantee sched callbacks see the rogue flag. This is
3435 * necessary as scheduler callbacks may be invoked from other
3436 * cpus.
3437 */
3438 spin_unlock_irq(&gcwq->lock);
3439 schedule();
3440 spin_lock_irq(&gcwq->lock);
3441
3442 /*
Tejun Heocb444762010-07-02 10:03:50 +02003443 * Sched callbacks are disabled now. Zap nr_running. After
3444 * this, nr_running stays zero and need_more_worker() and
3445 * keep_working() are always true as long as the worklist is
3446 * not empty.
Tejun Heoe22bee72010-06-29 10:07:14 +02003447 */
Tejun Heo9c6bae02012-07-13 22:16:44 -07003448 for_each_worker_pool(pool, gcwq)
3449 atomic_set(get_pool_nr_running(pool), 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003450
3451 spin_unlock_irq(&gcwq->lock);
Tejun Heo9c6bae02012-07-13 22:16:44 -07003452 for_each_worker_pool(pool, gcwq)
3453 del_timer_sync(&pool->idle_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003454 spin_lock_irq(&gcwq->lock);
3455
3456 /*
Tejun Heodb7bccf2010-06-29 10:07:12 +02003457 * We're now in charge. Notify and proceed to drain. We need
3458 * to keep the gcwq running during the whole CPU down
3459 * procedure as other cpu hotunplug callbacks may need to
3460 * flush currently running tasks.
3461 */
3462 gcwq->trustee_state = TRUSTEE_IN_CHARGE;
3463 wake_up_all(&gcwq->trustee_wait);
3464
3465 /*
3466 * The original cpu is in the process of dying and may go away
3467 * anytime now. When that happens, we and all workers would
Tejun Heoe22bee72010-06-29 10:07:14 +02003468 * be migrated to other cpus. Try draining any left work. We
3469 * want to get it over with ASAP - spam rescuers, wake up as
3470 * many idlers as necessary and create new ones till the
3471 * worklist is empty. Note that if the gcwq is frozen, there
Tejun Heo58a69cb2011-02-16 09:25:31 +01003472 * may be frozen works in freezable cwqs. Don't declare
Tejun Heoe22bee72010-06-29 10:07:14 +02003473 * completion while frozen.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003474 */
Tejun Heo9c6bae02012-07-13 22:16:44 -07003475 while (true) {
3476 bool busy = false;
Tejun Heoe22bee72010-06-29 10:07:14 +02003477
Tejun Heo9c6bae02012-07-13 22:16:44 -07003478 for_each_worker_pool(pool, gcwq)
3479 busy |= pool->nr_workers != pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +02003480
Tejun Heo9c6bae02012-07-13 22:16:44 -07003481 if (!busy && !(gcwq->flags & GCWQ_FREEZING) &&
3482 gcwq->trustee_state != TRUSTEE_IN_CHARGE)
3483 break;
Tejun Heoe22bee72010-06-29 10:07:14 +02003484
Tejun Heo9c6bae02012-07-13 22:16:44 -07003485 for_each_worker_pool(pool, gcwq) {
3486 int nr_works = 0;
3487
3488 list_for_each_entry(work, &pool->worklist, entry) {
3489 send_mayday(work);
3490 nr_works++;
3491 }
3492
3493 list_for_each_entry(worker, &pool->idle_list, entry) {
3494 if (!nr_works--)
3495 break;
3496 wake_up_process(worker->task);
3497 }
3498
3499 if (need_to_create_worker(pool)) {
3500 spin_unlock_irq(&gcwq->lock);
3501 worker = create_worker(pool, false);
3502 spin_lock_irq(&gcwq->lock);
3503 if (worker) {
3504 worker->flags |= WORKER_ROGUE;
3505 start_worker(worker);
3506 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003507 }
3508 }
3509
Tejun Heodb7bccf2010-06-29 10:07:12 +02003510 /* give a breather */
3511 if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN) < 0)
3512 break;
3513 }
3514
Tejun Heoe22bee72010-06-29 10:07:14 +02003515 /*
3516 * Either all works have been scheduled and cpu is down, or
3517 * cpu down has already been canceled. Wait for and butcher
3518 * all workers till we're canceled.
3519 */
3520 do {
Tejun Heo9c6bae02012-07-13 22:16:44 -07003521 rc = trustee_wait_event(gcwq_has_idle_workers(gcwq));
3522
3523 i = 0;
3524 for_each_worker_pool(pool, gcwq) {
3525 while (!list_empty(&pool->idle_list)) {
3526 worker = list_first_entry(&pool->idle_list,
3527 struct worker, entry);
3528 destroy_worker(worker);
3529 }
3530 i |= pool->nr_workers;
3531 }
3532 } while (i && rc >= 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003533
3534 /*
3535 * At this point, either draining has completed and no worker
3536 * is left, or cpu down has been canceled or the cpu is being
3537 * brought back up. There shouldn't be any idle one left.
3538 * Tell the remaining busy ones to rebind once it finishes the
3539 * currently scheduled works by scheduling the rebind_work.
3540 */
Tejun Heo9c6bae02012-07-13 22:16:44 -07003541 for_each_worker_pool(pool, gcwq)
3542 WARN_ON(!list_empty(&pool->idle_list));
Tejun Heoe22bee72010-06-29 10:07:14 +02003543
3544 for_each_busy_worker(worker, i, pos, gcwq) {
3545 struct work_struct *rebind_work = &worker->rebind_work;
Lai Jiangshan6adebb02012-09-02 00:28:19 +08003546 unsigned long worker_flags = worker->flags;
Tejun Heoe22bee72010-06-29 10:07:14 +02003547
3548 /*
3549 * Rebind_work may race with future cpu hotplug
3550 * operations. Use a separate flag to mark that
Lai Jiangshan6adebb02012-09-02 00:28:19 +08003551 * rebinding is scheduled. The morphing should
3552 * be atomic.
Tejun Heoe22bee72010-06-29 10:07:14 +02003553 */
Lai Jiangshan6adebb02012-09-02 00:28:19 +08003554 worker_flags |= WORKER_REBIND;
3555 worker_flags &= ~WORKER_ROGUE;
3556 ACCESS_ONCE(worker->flags) = worker_flags;
Tejun Heoe22bee72010-06-29 10:07:14 +02003557
3558 /* queue rebind_work, wq doesn't matter, use the default one */
3559 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
3560 work_data_bits(rebind_work)))
3561 continue;
3562
3563 debug_work_activate(rebind_work);
Tejun Heod320c032010-06-29 10:07:14 +02003564 insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work,
Tejun Heoe22bee72010-06-29 10:07:14 +02003565 worker->scheduled.next,
3566 work_color_to_flags(WORK_NO_COLOR));
3567 }
3568
3569 /* relinquish manager role */
Tejun Heo9c6bae02012-07-13 22:16:44 -07003570 for_each_worker_pool(pool, gcwq)
3571 pool->flags &= ~POOL_MANAGING_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02003572
Tejun Heodb7bccf2010-06-29 10:07:12 +02003573 /* notify completion */
3574 gcwq->trustee = NULL;
3575 gcwq->trustee_state = TRUSTEE_DONE;
3576 wake_up_all(&gcwq->trustee_wait);
3577 spin_unlock_irq(&gcwq->lock);
3578 return 0;
3579}
3580
3581/**
3582 * wait_trustee_state - wait for trustee to enter the specified state
3583 * @gcwq: gcwq the trustee of interest belongs to
3584 * @state: target state to wait for
3585 *
3586 * Wait for the trustee to reach @state. DONE is already matched.
3587 *
3588 * CONTEXT:
3589 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3590 * multiple times. To be used by cpu_callback.
3591 */
3592static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09003593__releases(&gcwq->lock)
3594__acquires(&gcwq->lock)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003595{
3596 if (!(gcwq->trustee_state == state ||
3597 gcwq->trustee_state == TRUSTEE_DONE)) {
3598 spin_unlock_irq(&gcwq->lock);
3599 __wait_event(gcwq->trustee_wait,
3600 gcwq->trustee_state == state ||
3601 gcwq->trustee_state == TRUSTEE_DONE);
3602 spin_lock_irq(&gcwq->lock);
3603 }
3604}
3605
Oleg Nesterov3af244332007-05-09 02:34:09 -07003606static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
3607 unsigned long action,
3608 void *hcpu)
3609{
3610 unsigned int cpu = (unsigned long)hcpu;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003611 struct global_cwq *gcwq = get_gcwq(cpu);
3612 struct task_struct *new_trustee = NULL;
Tejun Heo9c6bae02012-07-13 22:16:44 -07003613 struct worker *new_workers[NR_WORKER_POOLS] = { };
3614 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003615 unsigned long flags;
Tejun Heo9c6bae02012-07-13 22:16:44 -07003616 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003618 action &= ~CPU_TASKS_FROZEN;
3619
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003621 case CPU_DOWN_PREPARE:
3622 new_trustee = kthread_create(trustee_thread, gcwq,
3623 "workqueue_trustee/%d\n", cpu);
3624 if (IS_ERR(new_trustee))
3625 return notifier_from_errno(PTR_ERR(new_trustee));
3626 kthread_bind(new_trustee, cpu);
Tejun Heoe22bee72010-06-29 10:07:14 +02003627 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 case CPU_UP_PREPARE:
Tejun Heo9c6bae02012-07-13 22:16:44 -07003629 i = 0;
3630 for_each_worker_pool(pool, gcwq) {
3631 BUG_ON(pool->first_idle);
3632 new_workers[i] = create_worker(pool, false);
3633 if (!new_workers[i++])
3634 goto err_destroy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 }
3637
Tejun Heodb7bccf2010-06-29 10:07:12 +02003638 /* some are called w/ irq disabled, don't disturb irq status */
3639 spin_lock_irqsave(&gcwq->lock, flags);
3640
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003641 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003642 case CPU_DOWN_PREPARE:
3643 /* initialize trustee and tell it to acquire the gcwq */
3644 BUG_ON(gcwq->trustee || gcwq->trustee_state != TRUSTEE_DONE);
3645 gcwq->trustee = new_trustee;
3646 gcwq->trustee_state = TRUSTEE_START;
3647 wake_up_process(gcwq->trustee);
3648 wait_trustee_state(gcwq, TRUSTEE_IN_CHARGE);
Tejun Heoe22bee72010-06-29 10:07:14 +02003649 /* fall through */
3650 case CPU_UP_PREPARE:
Tejun Heo9c6bae02012-07-13 22:16:44 -07003651 i = 0;
3652 for_each_worker_pool(pool, gcwq) {
3653 BUG_ON(pool->first_idle);
3654 pool->first_idle = new_workers[i++];
3655 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003656 break;
3657
3658 case CPU_DYING:
3659 /*
3660 * Before this, the trustee and all workers except for
3661 * the ones which are still executing works from
3662 * before the last CPU down must be on the cpu. After
3663 * this, they'll all be diasporas.
3664 */
3665 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003666 break;
3667
Oleg Nesterov3da1c842008-07-25 01:47:50 -07003668 case CPU_POST_DEAD:
Tejun Heodb7bccf2010-06-29 10:07:12 +02003669 gcwq->trustee_state = TRUSTEE_BUTCHER;
Tejun Heoe22bee72010-06-29 10:07:14 +02003670 /* fall through */
3671 case CPU_UP_CANCELED:
Tejun Heo9c6bae02012-07-13 22:16:44 -07003672 for_each_worker_pool(pool, gcwq) {
3673 destroy_worker(pool->first_idle);
3674 pool->first_idle = NULL;
3675 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003676 break;
3677
3678 case CPU_DOWN_FAILED:
3679 case CPU_ONLINE:
Tejun Heoe22bee72010-06-29 10:07:14 +02003680 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003681 if (gcwq->trustee_state != TRUSTEE_DONE) {
3682 gcwq->trustee_state = TRUSTEE_RELEASE;
3683 wake_up_process(gcwq->trustee);
3684 wait_trustee_state(gcwq, TRUSTEE_DONE);
3685 }
3686
Tejun Heoe22bee72010-06-29 10:07:14 +02003687 /*
3688 * Trustee is done and there might be no worker left.
3689 * Put the first_idle in and request a real manager to
3690 * take a look.
3691 */
Tejun Heo9c6bae02012-07-13 22:16:44 -07003692 for_each_worker_pool(pool, gcwq) {
3693 spin_unlock_irq(&gcwq->lock);
3694 kthread_bind(pool->first_idle->task, cpu);
3695 spin_lock_irq(&gcwq->lock);
3696 pool->flags |= POOL_MANAGE_WORKERS;
3697 start_worker(pool->first_idle);
3698 pool->first_idle = NULL;
3699 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003700 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003701 }
3702
Tejun Heodb7bccf2010-06-29 10:07:12 +02003703 spin_unlock_irqrestore(&gcwq->lock, flags);
3704
Tejun Heo15376632010-06-29 10:07:11 +02003705 return notifier_from_errno(0);
Tejun Heo9c6bae02012-07-13 22:16:44 -07003706
3707err_destroy:
3708 if (new_trustee)
3709 kthread_stop(new_trustee);
3710
3711 spin_lock_irqsave(&gcwq->lock, flags);
3712 for (i = 0; i < NR_WORKER_POOLS; i++)
3713 if (new_workers[i])
3714 destroy_worker(new_workers[i]);
3715 spin_unlock_irqrestore(&gcwq->lock, flags);
3716
3717 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719
Tejun Heod3b42542012-07-17 12:39:26 -07003720/*
3721 * Workqueues should be brought up before normal priority CPU notifiers.
3722 * This will be registered high priority CPU notifier.
3723 */
3724static int __devinit workqueue_cpu_up_callback(struct notifier_block *nfb,
3725 unsigned long action,
3726 void *hcpu)
3727{
3728 switch (action & ~CPU_TASKS_FROZEN) {
3729 case CPU_UP_PREPARE:
3730 case CPU_UP_CANCELED:
3731 case CPU_DOWN_FAILED:
3732 case CPU_ONLINE:
3733 return workqueue_cpu_callback(nfb, action, hcpu);
3734 }
3735 return NOTIFY_OK;
3736}
3737
3738/*
3739 * Workqueues should be brought down after normal priority CPU notifiers.
3740 * This will be registered as low priority CPU notifier.
3741 */
3742static int __devinit workqueue_cpu_down_callback(struct notifier_block *nfb,
3743 unsigned long action,
3744 void *hcpu)
3745{
3746 switch (action & ~CPU_TASKS_FROZEN) {
3747 case CPU_DOWN_PREPARE:
3748 case CPU_DYING:
3749 case CPU_POST_DEAD:
3750 return workqueue_cpu_callback(nfb, action, hcpu);
3751 }
3752 return NOTIFY_OK;
3753}
3754
Rusty Russell2d3854a2008-11-05 13:39:10 +11003755#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003756
Rusty Russell2d3854a2008-11-05 13:39:10 +11003757struct work_for_cpu {
Tejun Heofc7da7e2012-09-18 12:48:43 -07003758 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003759 long (*fn)(void *);
3760 void *arg;
3761 long ret;
3762};
3763
Tejun Heofc7da7e2012-09-18 12:48:43 -07003764static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003765{
Tejun Heofc7da7e2012-09-18 12:48:43 -07003766 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
3767
Rusty Russell2d3854a2008-11-05 13:39:10 +11003768 wfc->ret = wfc->fn(wfc->arg);
3769}
3770
3771/**
3772 * work_on_cpu - run a function in user context on a particular cpu
3773 * @cpu: the cpu to run on
3774 * @fn: the function to run
3775 * @arg: the function arg
3776 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003777 * This will return the value @fn returns.
3778 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06003779 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003780 */
3781long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3782{
Tejun Heofc7da7e2012-09-18 12:48:43 -07003783 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003784
Tejun Heofc7da7e2012-09-18 12:48:43 -07003785 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
3786 schedule_work_on(cpu, &wfc.work);
3787 flush_work(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003788 return wfc.ret;
3789}
3790EXPORT_SYMBOL_GPL(work_on_cpu);
3791#endif /* CONFIG_SMP */
3792
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003793#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303794
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003795/**
3796 * freeze_workqueues_begin - begin freezing workqueues
3797 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003798 * Start freezing workqueues. After this function returns, all freezable
3799 * workqueues will queue new works to their frozen_works list instead of
3800 * gcwq->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003801 *
3802 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003803 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003804 */
3805void freeze_workqueues_begin(void)
3806{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003807 unsigned int cpu;
3808
3809 spin_lock(&workqueue_lock);
3810
3811 BUG_ON(workqueue_freezing);
3812 workqueue_freezing = true;
3813
Tejun Heof3421792010-07-02 10:03:51 +02003814 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003815 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003816 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003817
3818 spin_lock_irq(&gcwq->lock);
3819
Tejun Heodb7bccf2010-06-29 10:07:12 +02003820 BUG_ON(gcwq->flags & GCWQ_FREEZING);
3821 gcwq->flags |= GCWQ_FREEZING;
3822
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003823 list_for_each_entry(wq, &workqueues, list) {
3824 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3825
Tejun Heo58a69cb2011-02-16 09:25:31 +01003826 if (cwq && wq->flags & WQ_FREEZABLE)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003827 cwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003828 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003829
3830 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003831 }
3832
3833 spin_unlock(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003835
3836/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003837 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003838 *
3839 * Check whether freezing is complete. This function must be called
3840 * between freeze_workqueues_begin() and thaw_workqueues().
3841 *
3842 * CONTEXT:
3843 * Grabs and releases workqueue_lock.
3844 *
3845 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01003846 * %true if some freezable workqueues are still busy. %false if freezing
3847 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003848 */
3849bool freeze_workqueues_busy(void)
3850{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003851 unsigned int cpu;
3852 bool busy = false;
3853
3854 spin_lock(&workqueue_lock);
3855
3856 BUG_ON(!workqueue_freezing);
3857
Tejun Heof3421792010-07-02 10:03:51 +02003858 for_each_gcwq_cpu(cpu) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003859 struct workqueue_struct *wq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003860 /*
3861 * nr_active is monotonically decreasing. It's safe
3862 * to peek without lock.
3863 */
3864 list_for_each_entry(wq, &workqueues, list) {
3865 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3866
Tejun Heo58a69cb2011-02-16 09:25:31 +01003867 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003868 continue;
3869
3870 BUG_ON(cwq->nr_active < 0);
3871 if (cwq->nr_active) {
3872 busy = true;
3873 goto out_unlock;
3874 }
3875 }
3876 }
3877out_unlock:
3878 spin_unlock(&workqueue_lock);
3879 return busy;
3880}
3881
3882/**
3883 * thaw_workqueues - thaw workqueues
3884 *
3885 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo7e116292010-06-29 10:07:13 +02003886 * frozen works are transferred to their respective gcwq worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003887 *
3888 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003889 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003890 */
3891void thaw_workqueues(void)
3892{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003893 unsigned int cpu;
3894
3895 spin_lock(&workqueue_lock);
3896
3897 if (!workqueue_freezing)
3898 goto out_unlock;
3899
Tejun Heof3421792010-07-02 10:03:51 +02003900 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003901 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo9c6bae02012-07-13 22:16:44 -07003902 struct worker_pool *pool;
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003903 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003904
3905 spin_lock_irq(&gcwq->lock);
3906
Tejun Heodb7bccf2010-06-29 10:07:12 +02003907 BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
3908 gcwq->flags &= ~GCWQ_FREEZING;
3909
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003910 list_for_each_entry(wq, &workqueues, list) {
3911 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3912
Tejun Heo58a69cb2011-02-16 09:25:31 +01003913 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003914 continue;
3915
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003916 /* restore max_active and repopulate worklist */
3917 cwq->max_active = wq->saved_max_active;
3918
3919 while (!list_empty(&cwq->delayed_works) &&
3920 cwq->nr_active < cwq->max_active)
3921 cwq_activate_first_delayed(cwq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003922 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003923
Tejun Heo9c6bae02012-07-13 22:16:44 -07003924 for_each_worker_pool(pool, gcwq)
3925 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02003926
Tejun Heo8b03ae32010-06-29 10:07:12 +02003927 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003928 }
3929
3930 workqueue_freezing = false;
3931out_unlock:
3932 spin_unlock(&workqueue_lock);
3933}
3934#endif /* CONFIG_FREEZER */
3935
Suresh Siddha6ee05782010-07-30 14:57:37 -07003936static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937{
Tejun Heoc34056a2010-06-29 10:07:11 +02003938 unsigned int cpu;
Tejun Heoc8e55f32010-06-29 10:07:12 +02003939 int i;
Tejun Heoc34056a2010-06-29 10:07:11 +02003940
Tejun Heod3b42542012-07-17 12:39:26 -07003941 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
3942 cpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003943
3944 /* initialize gcwqs */
Tejun Heof3421792010-07-02 10:03:51 +02003945 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003946 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo9c6bae02012-07-13 22:16:44 -07003947 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003948
3949 spin_lock_init(&gcwq->lock);
3950 gcwq->cpu = cpu;
Tejun Heo477a3c32010-08-31 10:54:35 +02003951 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003952
Tejun Heoc8e55f32010-06-29 10:07:12 +02003953 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
3954 INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
3955
Tejun Heo9c6bae02012-07-13 22:16:44 -07003956 for_each_worker_pool(pool, gcwq) {
3957 pool->gcwq = gcwq;
3958 INIT_LIST_HEAD(&pool->worklist);
3959 INIT_LIST_HEAD(&pool->idle_list);
Tejun Heoe22bee72010-06-29 10:07:14 +02003960
Tejun Heo9c6bae02012-07-13 22:16:44 -07003961 init_timer_deferrable(&pool->idle_timer);
3962 pool->idle_timer.function = idle_worker_timeout;
3963 pool->idle_timer.data = (unsigned long)pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003964
Tejun Heo9c6bae02012-07-13 22:16:44 -07003965 setup_timer(&pool->mayday_timer, gcwq_mayday_timeout,
3966 (unsigned long)pool);
3967
3968 ida_init(&pool->worker_ida);
3969 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003970
3971 gcwq->trustee_state = TRUSTEE_DONE;
3972 init_waitqueue_head(&gcwq->trustee_wait);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003973 }
3974
Tejun Heoe22bee72010-06-29 10:07:14 +02003975 /* create the initial worker */
Tejun Heof3421792010-07-02 10:03:51 +02003976 for_each_online_gcwq_cpu(cpu) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003977 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo9c6bae02012-07-13 22:16:44 -07003978 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003979
Tejun Heo477a3c32010-08-31 10:54:35 +02003980 if (cpu != WORK_CPU_UNBOUND)
3981 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heo9c6bae02012-07-13 22:16:44 -07003982
3983 for_each_worker_pool(pool, gcwq) {
3984 struct worker *worker;
3985
3986 worker = create_worker(pool, true);
3987 BUG_ON(!worker);
3988 spin_lock_irq(&gcwq->lock);
3989 start_worker(worker);
3990 spin_unlock_irq(&gcwq->lock);
3991 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003992 }
3993
Tejun Heod320c032010-06-29 10:07:14 +02003994 system_wq = alloc_workqueue("events", 0, 0);
3995 system_long_wq = alloc_workqueue("events_long", 0, 0);
3996 system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0);
Tejun Heof3421792010-07-02 10:03:51 +02003997 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3998 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01003999 system_freezable_wq = alloc_workqueue("events_freezable",
4000 WQ_FREEZABLE, 0);
Alan Stern62d3c542012-03-02 10:51:00 +01004001 system_nrt_freezable_wq = alloc_workqueue("events_nrt_freezable",
4002 WQ_NON_REENTRANT | WQ_FREEZABLE, 0);
Hitoshi Mitakee5cba242010-11-26 12:06:44 +01004003 BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq ||
Alan Stern62d3c542012-03-02 10:51:00 +01004004 !system_unbound_wq || !system_freezable_wq ||
4005 !system_nrt_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07004006 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007}
Suresh Siddha6ee05782010-07-30 14:57:37 -07004008early_initcall(init_workqueues);