blob: 0d2f14211085621f06b0aa65f47a43e85e09f3ae [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 Mustafa99625ba2013-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 Heo1f19ade2012-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 Heo2bfedd32012-07-13 22:16:45 -070077 NR_WORKER_POOLS = 2, /* # worker pools per gcwq */
Tejun Heo0290d202012-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 Heo2bfedd32012-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 Heo47303942012-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 Heo8cca0ee2010-06-29 10:07:13 +0200137 struct cpu_workqueue_struct *current_cwq; /* L: current_work's cwq */
Tejun Heoaffee4b2010-06-29 10:07:12 +0200138 struct list_head scheduled; /* L: scheduled works */
Tejun Heoc34056a2010-06-29 10:07:11 +0200139 struct task_struct *task; /* I: worker task */
Tejun Heo47303942012-07-12 14:46:37 -0700140 struct worker_pool *pool; /* I: the associated pool */
Tejun Heoe22bee72010-06-29 10:07:14 +0200141 /* 64 bytes boundary on 64bit, 32 on 32bit */
142 unsigned long last_active; /* L: last active timestamp */
143 unsigned int flags; /* X: flags */
Tejun Heoc34056a2010-06-29 10:07:11 +0200144 int id; /* I: worker id */
Tejun Heoe22bee72010-06-29 10:07:14 +0200145 struct work_struct rebind_work; /* L: rebind worker to cpu */
Tejun Heoc34056a2010-06-29 10:07:11 +0200146};
147
Tejun Heo47303942012-07-12 14:46:37 -0700148struct worker_pool {
149 struct global_cwq *gcwq; /* I: the owning gcwq */
Tejun Heo1f19ade2012-07-12 14:46:37 -0700150 unsigned int flags; /* X: flags */
Tejun Heo47303942012-07-12 14:46:37 -0700151
152 struct list_head worklist; /* L: list of pending works */
153 int nr_workers; /* L: total number of workers */
154 int nr_idle; /* L: currently idle ones */
155
156 struct list_head idle_list; /* X: list of idle workers */
157 struct timer_list idle_timer; /* L: worker idle timeout */
158 struct timer_list mayday_timer; /* L: SOS timer for workers */
159
160 struct ida worker_ida; /* L: for worker IDs */
161 struct worker *first_idle; /* L: first idle worker */
162};
163
Tejun Heo4690c4a2010-06-29 10:07:10 +0200164/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200165 * Global per-cpu workqueue. There's one and only one for each cpu
166 * and all works are queued and processed here regardless of their
167 * target workqueues.
Tejun Heo8b03ae32010-06-29 10:07:12 +0200168 */
169struct global_cwq {
170 spinlock_t lock; /* the gcwq lock */
171 unsigned int cpu; /* I: the associated cpu */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200172 unsigned int flags; /* L: GCWQ_* flags */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200173
Tejun Heo47303942012-07-12 14:46:37 -0700174 /* workers are chained either in busy_hash or pool idle_list */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200175 struct hlist_head busy_hash[BUSY_WORKER_HASH_SIZE];
176 /* L: hash of busy workers */
177
Tejun Heo2bfedd32012-07-13 22:16:45 -0700178 struct worker_pool pools[2]; /* normal and highpri pools */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200179
180 struct task_struct *trustee; /* L: for gcwq shutdown */
181 unsigned int trustee_state; /* L: trustee state */
182 wait_queue_head_t trustee_wait; /* trustee wait */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200183} ____cacheline_aligned_in_smp;
184
185/*
Tejun Heo502ca9d2010-06-29 10:07:13 +0200186 * The per-CPU workqueue. The lower WORK_STRUCT_FLAG_BITS of
Tejun Heo0f900042010-06-29 10:07:11 +0200187 * work_struct->data are used for flags and thus cwqs need to be
188 * aligned at two's power of the number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 */
190struct cpu_workqueue_struct {
Tejun Heo47303942012-07-12 14:46:37 -0700191 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200192 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200193 int work_color; /* L: current color */
194 int flush_color; /* L: flushing color */
195 int nr_in_flight[WORK_NR_COLORS];
196 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200197 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200198 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200199 struct list_head delayed_works; /* L: delayed works */
Tejun Heo0f900042010-06-29 10:07:11 +0200200};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200203 * Structure used to wait for workqueue flush.
204 */
205struct wq_flusher {
206 struct list_head list; /* F: list of flushers */
207 int flush_color; /* F: flush color waiting for */
208 struct completion done; /* flush completion */
209};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Tejun Heo73f53c42010-06-29 10:07:11 +0200211/*
Tejun Heof2e005a2010-07-20 15:59:09 +0200212 * All cpumasks are assumed to be always set on UP and thus can't be
213 * used to determine whether there's something to be done.
214 */
215#ifdef CONFIG_SMP
216typedef cpumask_var_t mayday_mask_t;
217#define mayday_test_and_set_cpu(cpu, mask) \
218 cpumask_test_and_set_cpu((cpu), (mask))
219#define mayday_clear_cpu(cpu, mask) cpumask_clear_cpu((cpu), (mask))
220#define for_each_mayday_cpu(cpu, mask) for_each_cpu((cpu), (mask))
Tejun Heo9c375472010-08-31 11:18:34 +0200221#define alloc_mayday_mask(maskp, gfp) zalloc_cpumask_var((maskp), (gfp))
Tejun Heof2e005a2010-07-20 15:59:09 +0200222#define free_mayday_mask(mask) free_cpumask_var((mask))
223#else
224typedef unsigned long mayday_mask_t;
225#define mayday_test_and_set_cpu(cpu, mask) test_and_set_bit(0, &(mask))
226#define mayday_clear_cpu(cpu, mask) clear_bit(0, &(mask))
227#define for_each_mayday_cpu(cpu, mask) if ((cpu) = 0, (mask))
228#define alloc_mayday_mask(maskp, gfp) true
229#define free_mayday_mask(mask) do { } while (0)
230#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232/*
233 * The externally visible workqueue abstraction is an array of
234 * per-CPU workqueues:
235 */
236struct workqueue_struct {
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200237 unsigned int flags; /* W: WQ_* flags */
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200238 union {
239 struct cpu_workqueue_struct __percpu *pcpu;
240 struct cpu_workqueue_struct *single;
241 unsigned long v;
242 } cpu_wq; /* I: cwq's */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200243 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200244
245 struct mutex flush_mutex; /* protects wq flushing */
246 int work_color; /* F: current work color */
247 int flush_color; /* F: current flush color */
248 atomic_t nr_cwqs_to_flush; /* flush in progress */
249 struct wq_flusher *first_flusher; /* F: first flusher */
250 struct list_head flusher_queue; /* F: flush waiters */
251 struct list_head flusher_overflow; /* F: flush overflow list */
252
Tejun Heof2e005a2010-07-20 15:59:09 +0200253 mayday_mask_t mayday_mask; /* cpus requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200254 struct worker *rescuer; /* I: rescue worker */
255
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200256 int nr_drainers; /* W: drain in progress */
Tejun Heodcd989c2010-06-29 10:07:14 +0200257 int saved_max_active; /* W: saved cwq max_active */
Johannes Berg4e6045f2007-10-18 23:39:55 -0700258#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200259 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700260#endif
Tejun Heob196be82012-01-10 15:11:35 -0800261 char name[]; /* I: workqueue name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262};
263
Tejun Heod320c032010-06-29 10:07:14 +0200264struct workqueue_struct *system_wq __read_mostly;
265struct workqueue_struct *system_long_wq __read_mostly;
266struct workqueue_struct *system_nrt_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200267struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100268struct workqueue_struct *system_freezable_wq __read_mostly;
Alan Stern62d3c542012-03-02 10:51:00 +0100269struct workqueue_struct *system_nrt_freezable_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200270EXPORT_SYMBOL_GPL(system_wq);
271EXPORT_SYMBOL_GPL(system_long_wq);
272EXPORT_SYMBOL_GPL(system_nrt_wq);
Tejun Heof3421792010-07-02 10:03:51 +0200273EXPORT_SYMBOL_GPL(system_unbound_wq);
Tejun Heo24d51ad2011-02-21 09:52:50 +0100274EXPORT_SYMBOL_GPL(system_freezable_wq);
Alan Stern62d3c542012-03-02 10:51:00 +0100275EXPORT_SYMBOL_GPL(system_nrt_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200276
Tejun Heo97bd2342010-10-05 10:41:14 +0200277#define CREATE_TRACE_POINTS
278#include <trace/events/workqueue.h>
279
Tejun Heo0290d202012-07-13 22:16:44 -0700280#define for_each_worker_pool(pool, gcwq) \
Tejun Heo2bfedd32012-07-13 22:16:45 -0700281 for ((pool) = &(gcwq)->pools[0]; \
282 (pool) < &(gcwq)->pools[NR_WORKER_POOLS]; (pool)++)
Tejun Heo0290d202012-07-13 22:16:44 -0700283
Tejun Heodb7bccf2010-06-29 10:07:12 +0200284#define for_each_busy_worker(worker, i, pos, gcwq) \
285 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \
286 hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry)
287
Tejun Heof3421792010-07-02 10:03:51 +0200288static inline int __next_gcwq_cpu(int cpu, const struct cpumask *mask,
289 unsigned int sw)
290{
291 if (cpu < nr_cpu_ids) {
292 if (sw & 1) {
293 cpu = cpumask_next(cpu, mask);
294 if (cpu < nr_cpu_ids)
295 return cpu;
296 }
297 if (sw & 2)
298 return WORK_CPU_UNBOUND;
299 }
300 return WORK_CPU_NONE;
301}
302
303static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
304 struct workqueue_struct *wq)
305{
306 return __next_gcwq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2);
307}
308
Tejun Heo09884952010-08-01 11:50:12 +0200309/*
310 * CPU iterators
311 *
312 * An extra gcwq is defined for an invalid cpu number
313 * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any
314 * specific CPU. The following iterators are similar to
315 * for_each_*_cpu() iterators but also considers the unbound gcwq.
316 *
317 * for_each_gcwq_cpu() : possible CPUs + WORK_CPU_UNBOUND
318 * for_each_online_gcwq_cpu() : online CPUs + WORK_CPU_UNBOUND
319 * for_each_cwq_cpu() : possible CPUs for bound workqueues,
320 * WORK_CPU_UNBOUND for unbound workqueues
321 */
Tejun Heof3421792010-07-02 10:03:51 +0200322#define for_each_gcwq_cpu(cpu) \
323 for ((cpu) = __next_gcwq_cpu(-1, cpu_possible_mask, 3); \
324 (cpu) < WORK_CPU_NONE; \
325 (cpu) = __next_gcwq_cpu((cpu), cpu_possible_mask, 3))
326
327#define for_each_online_gcwq_cpu(cpu) \
328 for ((cpu) = __next_gcwq_cpu(-1, cpu_online_mask, 3); \
329 (cpu) < WORK_CPU_NONE; \
330 (cpu) = __next_gcwq_cpu((cpu), cpu_online_mask, 3))
331
332#define for_each_cwq_cpu(cpu, wq) \
333 for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, (wq)); \
334 (cpu) < WORK_CPU_NONE; \
335 (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq)))
336
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900337#ifdef CONFIG_DEBUG_OBJECTS_WORK
338
339static struct debug_obj_descr work_debug_descr;
340
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100341static void *work_debug_hint(void *addr)
342{
343 return ((struct work_struct *) addr)->func;
344}
345
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900346/*
347 * fixup_init is called when:
348 * - an active object is initialized
349 */
350static int work_fixup_init(void *addr, enum debug_obj_state state)
351{
352 struct work_struct *work = addr;
353
354 switch (state) {
355 case ODEBUG_STATE_ACTIVE:
356 cancel_work_sync(work);
357 debug_object_init(work, &work_debug_descr);
358 return 1;
359 default:
360 return 0;
361 }
362}
363
364/*
365 * fixup_activate is called when:
366 * - an active object is activated
367 * - an unknown object is activated (might be a statically initialized object)
368 */
369static int work_fixup_activate(void *addr, enum debug_obj_state state)
370{
371 struct work_struct *work = addr;
372
373 switch (state) {
374
375 case ODEBUG_STATE_NOTAVAILABLE:
376 /*
377 * This is not really a fixup. The work struct was
378 * statically initialized. We just make sure that it
379 * is tracked in the object tracker.
380 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200381 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900382 debug_object_init(work, &work_debug_descr);
383 debug_object_activate(work, &work_debug_descr);
384 return 0;
385 }
386 WARN_ON_ONCE(1);
387 return 0;
388
389 case ODEBUG_STATE_ACTIVE:
390 WARN_ON(1);
391
392 default:
393 return 0;
394 }
395}
396
397/*
398 * fixup_free is called when:
399 * - an active object is freed
400 */
401static int work_fixup_free(void *addr, enum debug_obj_state state)
402{
403 struct work_struct *work = addr;
404
405 switch (state) {
406 case ODEBUG_STATE_ACTIVE:
407 cancel_work_sync(work);
408 debug_object_free(work, &work_debug_descr);
409 return 1;
410 default:
411 return 0;
412 }
413}
414
415static struct debug_obj_descr work_debug_descr = {
416 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100417 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900418 .fixup_init = work_fixup_init,
419 .fixup_activate = work_fixup_activate,
420 .fixup_free = work_fixup_free,
421};
422
423static inline void debug_work_activate(struct work_struct *work)
424{
425 debug_object_activate(work, &work_debug_descr);
426}
427
428static inline void debug_work_deactivate(struct work_struct *work)
429{
430 debug_object_deactivate(work, &work_debug_descr);
431}
432
433void __init_work(struct work_struct *work, int onstack)
434{
435 if (onstack)
436 debug_object_init_on_stack(work, &work_debug_descr);
437 else
438 debug_object_init(work, &work_debug_descr);
439}
440EXPORT_SYMBOL_GPL(__init_work);
441
442void destroy_work_on_stack(struct work_struct *work)
443{
444 debug_object_free(work, &work_debug_descr);
445}
446EXPORT_SYMBOL_GPL(destroy_work_on_stack);
447
448#else
449static inline void debug_work_activate(struct work_struct *work) { }
450static inline void debug_work_deactivate(struct work_struct *work) { }
451#endif
452
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100453/* Serializes the accesses to the list of workqueues. */
454static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455static LIST_HEAD(workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200456static bool workqueue_freezing; /* W: have wqs started freezing? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Oleg Nesterov14441962007-05-23 13:57:57 -0700458/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200459 * The almighty global cpu workqueues. nr_running is the only field
460 * which is expected to be used frequently by other cpus via
461 * try_to_wake_up(). Put it in a separate cacheline.
Oleg Nesterov14441962007-05-23 13:57:57 -0700462 */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200463static DEFINE_PER_CPU(struct global_cwq, global_cwq);
Tejun Heo0290d202012-07-13 22:16:44 -0700464static DEFINE_PER_CPU_SHARED_ALIGNED(atomic_t, pool_nr_running[NR_WORKER_POOLS]);
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800465
Tejun Heof3421792010-07-02 10:03:51 +0200466/*
467 * Global cpu workqueue and nr_running counter for unbound gcwq. The
468 * gcwq is always online, has GCWQ_DISASSOCIATED set, and all its
469 * workers have WORKER_UNBOUND set.
470 */
471static struct global_cwq unbound_global_cwq;
Tejun Heo0290d202012-07-13 22:16:44 -0700472static atomic_t unbound_pool_nr_running[NR_WORKER_POOLS] = {
473 [0 ... NR_WORKER_POOLS - 1] = ATOMIC_INIT(0), /* always 0 */
474};
Tejun Heof3421792010-07-02 10:03:51 +0200475
Tejun Heoc34056a2010-06-29 10:07:11 +0200476static int worker_thread(void *__worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Tejun Heo2bfedd32012-07-13 22:16:45 -0700478static int worker_pool_pri(struct worker_pool *pool)
479{
480 return pool - pool->gcwq->pools;
481}
482
Tejun Heo8b03ae32010-06-29 10:07:12 +0200483static struct global_cwq *get_gcwq(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Tejun Heof3421792010-07-02 10:03:51 +0200485 if (cpu != WORK_CPU_UNBOUND)
486 return &per_cpu(global_cwq, cpu);
487 else
488 return &unbound_global_cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
Tejun Heob0f31fe2012-07-12 14:46:37 -0700491static atomic_t *get_pool_nr_running(struct worker_pool *pool)
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700492{
Tejun Heob0f31fe2012-07-12 14:46:37 -0700493 int cpu = pool->gcwq->cpu;
Tejun Heo2bfedd32012-07-13 22:16:45 -0700494 int idx = worker_pool_pri(pool);
Tejun Heob0f31fe2012-07-12 14:46:37 -0700495
Tejun Heof3421792010-07-02 10:03:51 +0200496 if (cpu != WORK_CPU_UNBOUND)
Tejun Heo0290d202012-07-13 22:16:44 -0700497 return &per_cpu(pool_nr_running, cpu)[idx];
Tejun Heof3421792010-07-02 10:03:51 +0200498 else
Tejun Heo0290d202012-07-13 22:16:44 -0700499 return &unbound_pool_nr_running[idx];
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700500}
501
Tejun Heo4690c4a2010-06-29 10:07:10 +0200502static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
503 struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700504{
Tejun Heof3421792010-07-02 10:03:51 +0200505 if (!(wq->flags & WQ_UNBOUND)) {
Lai Jiangshane06ffa12012-03-09 18:03:20 +0800506 if (likely(cpu < nr_cpu_ids))
Tejun Heof3421792010-07-02 10:03:51 +0200507 return per_cpu_ptr(wq->cpu_wq.pcpu, cpu);
Tejun Heof3421792010-07-02 10:03:51 +0200508 } else if (likely(cpu == WORK_CPU_UNBOUND))
509 return wq->cpu_wq.single;
510 return NULL;
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700511}
512
Tejun Heo73f53c42010-06-29 10:07:11 +0200513static unsigned int work_color_to_flags(int color)
514{
515 return color << WORK_STRUCT_COLOR_SHIFT;
516}
517
518static int get_work_color(struct work_struct *work)
519{
520 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
521 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
522}
523
524static int work_next_color(int color)
525{
526 return (color + 1) % WORK_NR_COLORS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
David Howells4594bf12006-12-07 11:33:26 +0000529/*
Tejun Heoe1201532010-07-22 14:14:25 +0200530 * A work's data points to the cwq with WORK_STRUCT_CWQ set while the
531 * work is on queue. Once execution starts, WORK_STRUCT_CWQ is
532 * cleared and the work data contains the cpu number it was last on.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200533 *
534 * set_work_{cwq|cpu}() and clear_work_data() can be used to set the
535 * cwq, cpu or clear work->data. These functions should only be
536 * called while the work is owned - ie. while the PENDING bit is set.
537 *
538 * get_work_[g]cwq() can be used to obtain the gcwq or cwq
539 * corresponding to a work. gcwq is available once the work has been
540 * queued anywhere after initialization. cwq is available only from
541 * queueing until execution starts.
David Howells4594bf12006-12-07 11:33:26 +0000542 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200543static inline void set_work_data(struct work_struct *work, unsigned long data,
544 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000545{
David Howells4594bf12006-12-07 11:33:26 +0000546 BUG_ON(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200547 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000548}
David Howells365970a2006-11-22 14:54:49 +0000549
Tejun Heo7a22ad72010-06-29 10:07:13 +0200550static void set_work_cwq(struct work_struct *work,
551 struct cpu_workqueue_struct *cwq,
552 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200553{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200554 set_work_data(work, (unsigned long)cwq,
Tejun Heoe1201532010-07-22 14:14:25 +0200555 WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200556}
557
Tejun Heo7a22ad72010-06-29 10:07:13 +0200558static void set_work_cpu(struct work_struct *work, unsigned int cpu)
David Howells365970a2006-11-22 14:54:49 +0000559{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200560 set_work_data(work, cpu << WORK_STRUCT_FLAG_BITS, WORK_STRUCT_PENDING);
561}
562
563static void clear_work_data(struct work_struct *work)
564{
565 set_work_data(work, WORK_STRUCT_NO_CPU, 0);
566}
567
Tejun Heo7a22ad72010-06-29 10:07:13 +0200568static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work)
569{
Tejun Heoe1201532010-07-22 14:14:25 +0200570 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200571
Tejun Heoe1201532010-07-22 14:14:25 +0200572 if (data & WORK_STRUCT_CWQ)
573 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
574 else
Srinivasarao Pb0bfc292013-09-18 14:33:45 +0530575 {
576 WARN_ON_ONCE(1);
Tejun Heoe1201532010-07-22 14:14:25 +0200577 return NULL;
Srinivasarao Pb0bfc292013-09-18 14:33:45 +0530578 }
Tejun Heo7a22ad72010-06-29 10:07:13 +0200579}
580
581static struct global_cwq *get_work_gcwq(struct work_struct *work)
582{
Tejun Heoe1201532010-07-22 14:14:25 +0200583 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200584 unsigned int cpu;
585
Tejun Heoe1201532010-07-22 14:14:25 +0200586 if (data & WORK_STRUCT_CWQ)
587 return ((struct cpu_workqueue_struct *)
Tejun Heo47303942012-07-12 14:46:37 -0700588 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->gcwq;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200589
590 cpu = data >> WORK_STRUCT_FLAG_BITS;
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200591 if (cpu == WORK_CPU_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200592 return NULL;
593
Tejun Heof3421792010-07-02 10:03:51 +0200594 BUG_ON(cpu >= nr_cpu_ids && cpu != WORK_CPU_UNBOUND);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200595 return get_gcwq(cpu);
David Howells365970a2006-11-22 14:54:49 +0000596}
597
598/*
Tejun Heo2bfedd32012-07-13 22:16:45 -0700599 * Policy functions. These define the policies on how the global worker
600 * pools are managed. Unless noted otherwise, these functions assume that
601 * they're being called with gcwq->lock held.
David Howells365970a2006-11-22 14:54:49 +0000602 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200603
Tejun Heob0f31fe2012-07-12 14:46:37 -0700604static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000605{
Tejun Heo2bfedd32012-07-13 22:16:45 -0700606 return !atomic_read(get_pool_nr_running(pool));
David Howells365970a2006-11-22 14:54:49 +0000607}
608
Tejun Heoe22bee72010-06-29 10:07:14 +0200609/*
610 * Need to wake up a worker? Called from anything but currently
611 * running workers.
Tejun Heo0d8a2ff2012-07-12 14:46:37 -0700612 *
613 * Note that, because unbound workers never contribute to nr_running, this
614 * function will always return %true for unbound gcwq as long as the
615 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200616 */
Tejun Heob0f31fe2012-07-12 14:46:37 -0700617static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000618{
Tejun Heob0f31fe2012-07-12 14:46:37 -0700619 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000620}
621
Tejun Heoe22bee72010-06-29 10:07:14 +0200622/* Can I start working? Called from busy but !running workers. */
Tejun Heob0f31fe2012-07-12 14:46:37 -0700623static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200624{
Tejun Heob0f31fe2012-07-12 14:46:37 -0700625 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200626}
627
628/* Do I need to keep working? Called from currently running workers. */
Tejun Heob0f31fe2012-07-12 14:46:37 -0700629static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200630{
Tejun Heob0f31fe2012-07-12 14:46:37 -0700631 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200632
Tejun Heo2bfedd32012-07-13 22:16:45 -0700633 return !list_empty(&pool->worklist) && atomic_read(nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200634}
635
636/* Do we need a new worker? Called from manager. */
Tejun Heob0f31fe2012-07-12 14:46:37 -0700637static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200638{
Tejun Heob0f31fe2012-07-12 14:46:37 -0700639 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200640}
641
642/* Do I need to be the manager? */
Tejun Heob0f31fe2012-07-12 14:46:37 -0700643static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200644{
Tejun Heob0f31fe2012-07-12 14:46:37 -0700645 return need_to_create_worker(pool) ||
Tejun Heo1f19ade2012-07-12 14:46:37 -0700646 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200647}
648
649/* Do we have too many workers and should some go away? */
Tejun Heob0f31fe2012-07-12 14:46:37 -0700650static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200651{
Tejun Heo1f19ade2012-07-12 14:46:37 -0700652 bool managing = pool->flags & POOL_MANAGING_WORKERS;
Tejun Heob0f31fe2012-07-12 14:46:37 -0700653 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
654 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200655
656 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
657}
658
659/*
660 * Wake up functions.
661 */
662
Tejun Heo7e116292010-06-29 10:07:13 +0200663/* Return the first worker. Safe with preemption disabled */
Tejun Heob0f31fe2012-07-12 14:46:37 -0700664static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200665{
Tejun Heob0f31fe2012-07-12 14:46:37 -0700666 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200667 return NULL;
668
Tejun Heob0f31fe2012-07-12 14:46:37 -0700669 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200670}
671
672/**
673 * wake_up_worker - wake up an idle worker
Tejun Heob0f31fe2012-07-12 14:46:37 -0700674 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200675 *
Tejun Heob0f31fe2012-07-12 14:46:37 -0700676 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200677 *
678 * CONTEXT:
679 * spin_lock_irq(gcwq->lock).
680 */
Tejun Heob0f31fe2012-07-12 14:46:37 -0700681static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200682{
Tejun Heob0f31fe2012-07-12 14:46:37 -0700683 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200684
685 if (likely(worker))
686 wake_up_process(worker->task);
687}
688
Tejun Heo4690c4a2010-06-29 10:07:10 +0200689/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200690 * wq_worker_waking_up - a worker is waking up
691 * @task: task waking up
692 * @cpu: CPU @task is waking up to
693 *
694 * This function is called during try_to_wake_up() when a worker is
695 * being awoken.
696 *
697 * CONTEXT:
698 * spin_lock_irq(rq->lock)
699 */
700void wq_worker_waking_up(struct task_struct *task, unsigned int cpu)
701{
702 struct worker *worker = kthread_data(task);
703
Steven Rostedt2d646722010-12-03 23:12:33 -0500704 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heob0f31fe2012-07-12 14:46:37 -0700705 atomic_inc(get_pool_nr_running(worker->pool));
Tejun Heoe22bee72010-06-29 10:07:14 +0200706}
707
708/**
709 * wq_worker_sleeping - a worker is going to sleep
710 * @task: task going to sleep
711 * @cpu: CPU in question, must be the current CPU number
712 *
713 * This function is called during schedule() when a busy worker is
714 * going to sleep. Worker on the same cpu can be woken up by
715 * returning pointer to its task.
716 *
717 * CONTEXT:
718 * spin_lock_irq(rq->lock)
719 *
720 * RETURNS:
721 * Worker task on @cpu to wake up, %NULL if none.
722 */
723struct task_struct *wq_worker_sleeping(struct task_struct *task,
724 unsigned int cpu)
725{
726 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heo47303942012-07-12 14:46:37 -0700727 struct worker_pool *pool = worker->pool;
Tejun Heob0f31fe2012-07-12 14:46:37 -0700728 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200729
Steven Rostedt2d646722010-12-03 23:12:33 -0500730 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200731 return NULL;
732
733 /* this can only happen on the local cpu */
734 BUG_ON(cpu != raw_smp_processor_id());
735
736 /*
737 * The counterpart of the following dec_and_test, implied mb,
738 * worklist not empty test sequence is in insert_work().
739 * Please read comment there.
740 *
741 * NOT_RUNNING is clear. This means that trustee is not in
742 * charge and we're running on the local cpu w/ rq lock held
743 * and preemption disabled, which in turn means that none else
744 * could be manipulating idle_list, so dereferencing idle_list
745 * without gcwq lock is safe.
746 */
Tejun Heo47303942012-07-12 14:46:37 -0700747 if (atomic_dec_and_test(nr_running) && !list_empty(&pool->worklist))
Tejun Heob0f31fe2012-07-12 14:46:37 -0700748 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200749 return to_wakeup ? to_wakeup->task : NULL;
750}
751
752/**
753 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200754 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200755 * @flags: flags to set
756 * @wakeup: wakeup an idle worker if necessary
757 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200758 * Set @flags in @worker->flags and adjust nr_running accordingly. If
759 * nr_running becomes zero and @wakeup is %true, an idle worker is
760 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200761 *
Tejun Heocb444762010-07-02 10:03:50 +0200762 * CONTEXT:
763 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200764 */
765static inline void worker_set_flags(struct worker *worker, unsigned int flags,
766 bool wakeup)
767{
Tejun Heo47303942012-07-12 14:46:37 -0700768 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200769
Tejun Heocb444762010-07-02 10:03:50 +0200770 WARN_ON_ONCE(worker->task != current);
771
Tejun Heoe22bee72010-06-29 10:07:14 +0200772 /*
773 * If transitioning into NOT_RUNNING, adjust nr_running and
774 * wake up an idle worker as necessary if requested by
775 * @wakeup.
776 */
777 if ((flags & WORKER_NOT_RUNNING) &&
778 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heob0f31fe2012-07-12 14:46:37 -0700779 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200780
781 if (wakeup) {
782 if (atomic_dec_and_test(nr_running) &&
Tejun Heo47303942012-07-12 14:46:37 -0700783 !list_empty(&pool->worklist))
Tejun Heob0f31fe2012-07-12 14:46:37 -0700784 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200785 } else
786 atomic_dec(nr_running);
787 }
788
Tejun Heod302f012010-06-29 10:07:13 +0200789 worker->flags |= flags;
790}
791
792/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200793 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200794 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200795 * @flags: flags to clear
796 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200797 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200798 *
Tejun Heocb444762010-07-02 10:03:50 +0200799 * CONTEXT:
800 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200801 */
802static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
803{
Tejun Heob0f31fe2012-07-12 14:46:37 -0700804 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200805 unsigned int oflags = worker->flags;
806
Tejun Heocb444762010-07-02 10:03:50 +0200807 WARN_ON_ONCE(worker->task != current);
808
Tejun Heod302f012010-06-29 10:07:13 +0200809 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200810
Tejun Heo42c025f2011-01-11 15:58:49 +0100811 /*
812 * If transitioning out of NOT_RUNNING, increment nr_running. Note
813 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
814 * of multiple flags, not a single flag.
815 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200816 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
817 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heob0f31fe2012-07-12 14:46:37 -0700818 atomic_inc(get_pool_nr_running(pool));
Tejun Heod302f012010-06-29 10:07:13 +0200819}
820
821/**
Tejun Heoc8e55f32010-06-29 10:07:12 +0200822 * busy_worker_head - return the busy hash head for a work
823 * @gcwq: gcwq of interest
824 * @work: work to be hashed
825 *
826 * Return hash head of @gcwq for @work.
827 *
828 * CONTEXT:
829 * spin_lock_irq(gcwq->lock).
830 *
831 * RETURNS:
832 * Pointer to the hash head.
833 */
834static struct hlist_head *busy_worker_head(struct global_cwq *gcwq,
835 struct work_struct *work)
836{
837 const int base_shift = ilog2(sizeof(struct work_struct));
838 unsigned long v = (unsigned long)work;
839
840 /* simple shift and fold hash, do we need something better? */
841 v >>= base_shift;
842 v += v >> BUSY_WORKER_HASH_ORDER;
843 v &= BUSY_WORKER_HASH_MASK;
844
845 return &gcwq->busy_hash[v];
846}
847
848/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200849 * __find_worker_executing_work - find worker which is executing a work
850 * @gcwq: gcwq of interest
851 * @bwh: hash head as returned by busy_worker_head()
852 * @work: work to find worker for
853 *
854 * Find a worker which is executing @work on @gcwq. @bwh should be
855 * the hash head obtained by calling busy_worker_head() with the same
856 * work.
857 *
858 * CONTEXT:
859 * spin_lock_irq(gcwq->lock).
860 *
861 * RETURNS:
862 * Pointer to worker which is executing @work if found, NULL
863 * otherwise.
864 */
865static struct worker *__find_worker_executing_work(struct global_cwq *gcwq,
866 struct hlist_head *bwh,
867 struct work_struct *work)
868{
869 struct worker *worker;
870 struct hlist_node *tmp;
871
872 hlist_for_each_entry(worker, tmp, bwh, hentry)
873 if (worker->current_work == work)
874 return worker;
875 return NULL;
876}
877
878/**
879 * find_worker_executing_work - find worker which is executing a work
880 * @gcwq: gcwq of interest
881 * @work: work to find worker for
882 *
883 * Find a worker which is executing @work on @gcwq. This function is
884 * identical to __find_worker_executing_work() except that this
885 * function calculates @bwh itself.
886 *
887 * CONTEXT:
888 * spin_lock_irq(gcwq->lock).
889 *
890 * RETURNS:
891 * Pointer to worker which is executing @work if found, NULL
892 * otherwise.
893 */
894static struct worker *find_worker_executing_work(struct global_cwq *gcwq,
895 struct work_struct *work)
896{
897 return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work),
898 work);
899}
900
901/**
Tejun Heo7e116292010-06-29 10:07:13 +0200902 * insert_work - insert a work into gcwq
Tejun Heo4690c4a2010-06-29 10:07:10 +0200903 * @cwq: cwq @work belongs to
904 * @work: work to insert
905 * @head: insertion point
906 * @extra_flags: extra WORK_STRUCT_* flags to set
907 *
Tejun Heo7e116292010-06-29 10:07:13 +0200908 * Insert @work which belongs to @cwq into @gcwq after @head.
909 * @extra_flags is or'd to work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200910 *
911 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +0200912 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +0200913 */
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700914static void insert_work(struct cpu_workqueue_struct *cwq,
Tejun Heo4690c4a2010-06-29 10:07:10 +0200915 struct work_struct *work, struct list_head *head,
916 unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700917{
Tejun Heob0f31fe2012-07-12 14:46:37 -0700918 struct worker_pool *pool = cwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100919
Tejun Heo4690c4a2010-06-29 10:07:10 +0200920 /* we own @work, set data and link */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200921 set_work_cwq(work, cwq, extra_flags);
Tejun Heo4690c4a2010-06-29 10:07:10 +0200922
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700923 /*
924 * Ensure that we get the right work->data if we see the
925 * result of list_add() below, see try_to_grab_pending().
926 */
927 smp_wmb();
Tejun Heo4690c4a2010-06-29 10:07:10 +0200928
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700929 list_add_tail(&work->entry, head);
Tejun Heoe22bee72010-06-29 10:07:14 +0200930
931 /*
932 * Ensure either worker_sched_deactivated() sees the above
933 * list_add_tail() or we see zero nr_running to avoid workers
934 * lying around lazily while there are works to be processed.
935 */
936 smp_mb();
937
Tejun Heob0f31fe2012-07-12 14:46:37 -0700938 if (__need_more_worker(pool))
939 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700940}
941
Tejun Heoc8efcc22010-12-20 19:32:04 +0100942/*
943 * Test whether @work is being queued from another work executing on the
944 * same workqueue. This is rather expensive and should only be used from
945 * cold paths.
946 */
947static bool is_chained_work(struct workqueue_struct *wq)
948{
949 unsigned long flags;
950 unsigned int cpu;
951
952 for_each_gcwq_cpu(cpu) {
953 struct global_cwq *gcwq = get_gcwq(cpu);
954 struct worker *worker;
955 struct hlist_node *pos;
956 int i;
957
958 spin_lock_irqsave(&gcwq->lock, flags);
959 for_each_busy_worker(worker, i, pos, gcwq) {
960 if (worker->task != current)
961 continue;
962 spin_unlock_irqrestore(&gcwq->lock, flags);
963 /*
964 * I'm @worker, no locking necessary. See if @work
965 * is headed to the same workqueue.
966 */
967 return worker->current_cwq->wq == wq;
968 }
969 spin_unlock_irqrestore(&gcwq->lock, flags);
970 }
971 return false;
972}
973
Tejun Heo4690c4a2010-06-29 10:07:10 +0200974static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 struct work_struct *work)
976{
Tejun Heo502ca9d2010-06-29 10:07:13 +0200977 struct global_cwq *gcwq;
978 struct cpu_workqueue_struct *cwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200979 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +0200980 unsigned int work_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 unsigned long flags;
982
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900983 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200984
Tejun Heoc8efcc22010-12-20 19:32:04 +0100985 /* if dying, only works from the same workqueue are allowed */
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200986 if (unlikely(wq->flags & WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +0100987 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +0200988 return;
989
Tejun Heoc7fc77f2010-07-02 10:03:51 +0200990 /* determine gcwq to use */
991 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +0200992 struct global_cwq *last_gcwq;
993
Tejun Heoc7fc77f2010-07-02 10:03:51 +0200994 if (unlikely(cpu == WORK_CPU_UNBOUND))
995 cpu = raw_smp_processor_id();
996
Tejun Heo18aa9ef2010-06-29 10:07:13 +0200997 /*
998 * It's multi cpu. If @wq is non-reentrant and @work
999 * was previously on a different cpu, it might still
1000 * be running there, in which case the work needs to
1001 * be queued on that cpu to guarantee non-reentrance.
1002 */
Tejun Heo502ca9d2010-06-29 10:07:13 +02001003 gcwq = get_gcwq(cpu);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001004 if (wq->flags & WQ_NON_REENTRANT &&
1005 (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) {
1006 struct worker *worker;
1007
1008 spin_lock_irqsave(&last_gcwq->lock, flags);
1009
1010 worker = find_worker_executing_work(last_gcwq, work);
1011
1012 if (worker && worker->current_cwq->wq == wq)
1013 gcwq = last_gcwq;
1014 else {
1015 /* meh... not running there, queue here */
1016 spin_unlock_irqrestore(&last_gcwq->lock, flags);
1017 spin_lock_irqsave(&gcwq->lock, flags);
1018 }
1019 } else
1020 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heof3421792010-07-02 10:03:51 +02001021 } else {
1022 gcwq = get_gcwq(WORK_CPU_UNBOUND);
1023 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001024 }
1025
1026 /* gcwq determined, get cwq and queue */
1027 cwq = get_cwq(gcwq->cpu, wq);
Tejun Heocdadf002010-10-05 10:49:55 +02001028 trace_workqueue_queue_work(cpu, cwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001029
Tejun Heo4690c4a2010-06-29 10:07:10 +02001030 BUG_ON(!list_empty(&work->entry));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001031
Tejun Heo73f53c42010-06-29 10:07:11 +02001032 cwq->nr_in_flight[cwq->work_color]++;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001033 work_flags = work_color_to_flags(cwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001034
1035 if (likely(cwq->nr_active < cwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001036 trace_workqueue_activate_work(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001037 cwq->nr_active++;
Tejun Heo2bfedd32012-07-13 22:16:45 -07001038 worklist = &cwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001039 } else {
1040 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001041 worklist = &cwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001042 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001043
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001044 insert_work(cwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001045
Tejun Heo8b03ae32010-06-29 10:07:12 +02001046 spin_unlock_irqrestore(&gcwq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047}
1048
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001049/**
1050 * queue_work - queue work on a workqueue
1051 * @wq: workqueue to use
1052 * @work: work to queue
1053 *
Alan Stern057647f2006-10-28 10:38:58 -07001054 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 *
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07001056 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1057 * it can be processed by another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001059int queue_work(struct workqueue_struct *wq, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001061 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001063 ret = queue_work_on(get_cpu(), wq, work);
1064 put_cpu();
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 return ret;
1067}
Dave Jonesae90dd52006-06-30 01:40:45 -04001068EXPORT_SYMBOL_GPL(queue_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Zhang Ruic1a220e2008-07-23 21:28:39 -07001070/**
1071 * queue_work_on - queue work on specific cpu
1072 * @cpu: CPU number to execute work on
1073 * @wq: workqueue to use
1074 * @work: work to queue
1075 *
1076 * Returns 0 if @work was already on a queue, non-zero otherwise.
1077 *
1078 * We queue the work to a specific CPU, the caller must ensure it
1079 * can't go away.
1080 */
1081int
1082queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
1083{
1084 int ret = 0;
1085
Tejun Heo22df02b2010-06-29 10:07:10 +02001086 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001087 __queue_work(cpu, wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001088 ret = 1;
1089 }
1090 return ret;
1091}
1092EXPORT_SYMBOL_GPL(queue_work_on);
1093
Li Zefan6d141c32008-02-08 04:21:09 -08001094static void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
David Howells52bad642006-11-22 14:54:01 +00001096 struct delayed_work *dwork = (struct delayed_work *)__data;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001097 struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Srinivasarao Pb0bfc292013-09-18 14:33:45 +05301099 if (cwq != NULL)
1100 __queue_work(smp_processor_id(), cwq->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101}
1102
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001103/**
1104 * queue_delayed_work - queue work on a workqueue after delay
1105 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001106 * @dwork: delayable work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001107 * @delay: number of jiffies to wait before queueing
1108 *
Alan Stern057647f2006-10-28 10:38:58 -07001109 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001110 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001111int queue_delayed_work(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001112 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
David Howells52bad642006-11-22 14:54:01 +00001114 if (delay == 0)
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001115 return queue_work(wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001117 return queue_delayed_work_on(-1, wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
Dave Jonesae90dd52006-06-30 01:40:45 -04001119EXPORT_SYMBOL_GPL(queue_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001121/**
1122 * queue_delayed_work_on - queue work on specific CPU after delay
1123 * @cpu: CPU number to execute work on
1124 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001125 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001126 * @delay: number of jiffies to wait before queueing
1127 *
Alan Stern057647f2006-10-28 10:38:58 -07001128 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001129 */
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001130int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001131 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001132{
1133 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +00001134 struct timer_list *timer = &dwork->timer;
1135 struct work_struct *work = &dwork->work;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001136
Tejun Heo22df02b2010-06-29 10:07:10 +02001137 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001138 unsigned int lcpu;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001139
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001140 BUG_ON(timer_pending(timer));
1141 BUG_ON(!list_empty(&work->entry));
1142
Andrew Liu8a3e77c2008-05-01 04:35:14 -07001143 timer_stats_timer_set_start_info(&dwork->timer);
1144
Tejun Heo7a22ad72010-06-29 10:07:13 +02001145 /*
1146 * This stores cwq for the moment, for the timer_fn.
1147 * Note that the work's gcwq is preserved to allow
1148 * reentrance detection for delayed works.
1149 */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001150 if (!(wq->flags & WQ_UNBOUND)) {
1151 struct global_cwq *gcwq = get_work_gcwq(work);
1152
1153 if (gcwq && gcwq->cpu != WORK_CPU_UNBOUND)
1154 lcpu = gcwq->cpu;
1155 else
1156 lcpu = raw_smp_processor_id();
1157 } else
1158 lcpu = WORK_CPU_UNBOUND;
1159
Tejun Heo7a22ad72010-06-29 10:07:13 +02001160 set_work_cwq(work, get_cwq(lcpu, wq), 0);
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001161
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001162 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +00001163 timer->data = (unsigned long)dwork;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001164 timer->function = delayed_work_timer_fn;
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001165
1166 if (unlikely(cpu >= 0))
1167 add_timer_on(timer, cpu);
1168 else
1169 add_timer(timer);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001170 ret = 1;
1171 }
1172 return ret;
1173}
Dave Jonesae90dd52006-06-30 01:40:45 -04001174EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Tejun Heoc8e55f32010-06-29 10:07:12 +02001176/**
1177 * worker_enter_idle - enter idle state
1178 * @worker: worker which is entering idle state
1179 *
1180 * @worker is entering idle state. Update stats and idle timer if
1181 * necessary.
1182 *
1183 * LOCKING:
1184 * spin_lock_irq(gcwq->lock).
1185 */
1186static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
Tejun Heo47303942012-07-12 14:46:37 -07001188 struct worker_pool *pool = worker->pool;
1189 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Tejun Heoc8e55f32010-06-29 10:07:12 +02001191 BUG_ON(worker->flags & WORKER_IDLE);
1192 BUG_ON(!list_empty(&worker->entry) &&
1193 (worker->hentry.next || worker->hentry.pprev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Tejun Heocb444762010-07-02 10:03:50 +02001195 /* can't use worker_set_flags(), also called from start_worker() */
1196 worker->flags |= WORKER_IDLE;
Tejun Heo47303942012-07-12 14:46:37 -07001197 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001198 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001199
Tejun Heoc8e55f32010-06-29 10:07:12 +02001200 /* idle_list is LIFO */
Tejun Heo47303942012-07-12 14:46:37 -07001201 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001202
Tejun Heoe22bee72010-06-29 10:07:14 +02001203 if (likely(!(worker->flags & WORKER_ROGUE))) {
Tejun Heob0f31fe2012-07-12 14:46:37 -07001204 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
Tejun Heo47303942012-07-12 14:46:37 -07001205 mod_timer(&pool->idle_timer,
Tejun Heoe22bee72010-06-29 10:07:14 +02001206 jiffies + IDLE_WORKER_TIMEOUT);
1207 } else
Tejun Heodb7bccf2010-06-29 10:07:12 +02001208 wake_up_all(&gcwq->trustee_wait);
Tejun Heocb444762010-07-02 10:03:50 +02001209
Tejun Heo776a17372012-05-14 15:04:50 -07001210 /*
1211 * Sanity check nr_running. Because trustee releases gcwq->lock
1212 * between setting %WORKER_ROGUE and zapping nr_running, the
1213 * warning may trigger spuriously. Check iff trustee is idle.
1214 */
1215 WARN_ON_ONCE(gcwq->trustee_state == TRUSTEE_DONE &&
Tejun Heo47303942012-07-12 14:46:37 -07001216 pool->nr_workers == pool->nr_idle &&
Tejun Heob0f31fe2012-07-12 14:46:37 -07001217 atomic_read(get_pool_nr_running(pool)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
1219
Tejun Heoc8e55f32010-06-29 10:07:12 +02001220/**
1221 * worker_leave_idle - leave idle state
1222 * @worker: worker which is leaving idle state
1223 *
1224 * @worker is leaving idle state. Update stats.
1225 *
1226 * LOCKING:
1227 * spin_lock_irq(gcwq->lock).
1228 */
1229static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
Tejun Heo47303942012-07-12 14:46:37 -07001231 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Tejun Heoc8e55f32010-06-29 10:07:12 +02001233 BUG_ON(!(worker->flags & WORKER_IDLE));
Tejun Heod302f012010-06-29 10:07:13 +02001234 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heo47303942012-07-12 14:46:37 -07001235 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001236 list_del_init(&worker->entry);
1237}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Tejun Heoe22bee72010-06-29 10:07:14 +02001239/**
1240 * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq
1241 * @worker: self
1242 *
1243 * Works which are scheduled while the cpu is online must at least be
1244 * scheduled to a worker which is bound to the cpu so that if they are
1245 * flushed from cpu callbacks while cpu is going down, they are
1246 * guaranteed to execute on the cpu.
1247 *
1248 * This function is to be used by rogue workers and rescuers to bind
1249 * themselves to the target cpu and may race with cpu going down or
1250 * coming online. kthread_bind() can't be used because it may put the
1251 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1252 * verbatim as it's best effort and blocking and gcwq may be
1253 * [dis]associated in the meantime.
1254 *
1255 * This function tries set_cpus_allowed() and locks gcwq and verifies
1256 * the binding against GCWQ_DISASSOCIATED which is set during
1257 * CPU_DYING and cleared during CPU_ONLINE, so if the worker enters
1258 * idle state or fetches works without dropping lock, it can guarantee
1259 * the scheduling requirement described in the first paragraph.
1260 *
1261 * CONTEXT:
1262 * Might sleep. Called without any lock but returns with gcwq->lock
1263 * held.
1264 *
1265 * RETURNS:
1266 * %true if the associated gcwq is online (@worker is successfully
1267 * bound), %false if offline.
1268 */
1269static bool worker_maybe_bind_and_lock(struct worker *worker)
Namhyung Kim972fa1c2010-08-22 23:19:43 +09001270__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001271{
Tejun Heo47303942012-07-12 14:46:37 -07001272 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001273 struct task_struct *task = worker->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Tejun Heoe22bee72010-06-29 10:07:14 +02001275 while (true) {
1276 /*
1277 * The following call may fail, succeed or succeed
1278 * without actually migrating the task to the cpu if
1279 * it races with cpu hotunplug operation. Verify
1280 * against GCWQ_DISASSOCIATED.
1281 */
Tejun Heof3421792010-07-02 10:03:51 +02001282 if (!(gcwq->flags & GCWQ_DISASSOCIATED))
1283 set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu));
Oleg Nesterov85f41862007-05-09 02:34:20 -07001284
Tejun Heoe22bee72010-06-29 10:07:14 +02001285 spin_lock_irq(&gcwq->lock);
1286 if (gcwq->flags & GCWQ_DISASSOCIATED)
1287 return false;
1288 if (task_cpu(task) == gcwq->cpu &&
1289 cpumask_equal(&current->cpus_allowed,
1290 get_cpu_mask(gcwq->cpu)))
1291 return true;
1292 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001293
Tejun Heo5035b202011-04-29 18:08:37 +02001294 /*
1295 * We've raced with CPU hot[un]plug. Give it a breather
1296 * and retry migration. cond_resched() is required here;
1297 * otherwise, we might deadlock against cpu_stop trying to
1298 * bring down the CPU on non-preemptive kernel.
1299 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001300 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001301 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001302 }
1303}
1304
1305/*
1306 * Function for worker->rebind_work used to rebind rogue busy workers
1307 * to the associated cpu which is coming back online. This is
1308 * scheduled by cpu up but can race with other cpu hotplug operations
1309 * and may be executed twice without intervening cpu down.
1310 */
1311static void worker_rebind_fn(struct work_struct *work)
1312{
1313 struct worker *worker = container_of(work, struct worker, rebind_work);
Tejun Heo47303942012-07-12 14:46:37 -07001314 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001315
1316 if (worker_maybe_bind_and_lock(worker))
1317 worker_clr_flags(worker, WORKER_REBIND);
1318
1319 spin_unlock_irq(&gcwq->lock);
1320}
1321
Tejun Heoc34056a2010-06-29 10:07:11 +02001322static struct worker *alloc_worker(void)
1323{
1324 struct worker *worker;
1325
1326 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001327 if (worker) {
1328 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001329 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heoe22bee72010-06-29 10:07:14 +02001330 INIT_WORK(&worker->rebind_work, worker_rebind_fn);
1331 /* on creation a worker is in !idle && prep state */
1332 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001333 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001334 return worker;
1335}
1336
1337/**
1338 * create_worker - create a new workqueue worker
Tejun Heob0f31fe2012-07-12 14:46:37 -07001339 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001340 * @bind: whether to set affinity to @cpu or not
1341 *
Tejun Heob0f31fe2012-07-12 14:46:37 -07001342 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001343 * can be started by calling start_worker() or destroyed using
1344 * destroy_worker().
1345 *
1346 * CONTEXT:
1347 * Might sleep. Does GFP_KERNEL allocations.
1348 *
1349 * RETURNS:
1350 * Pointer to the newly created worker.
1351 */
Tejun Heob0f31fe2012-07-12 14:46:37 -07001352static struct worker *create_worker(struct worker_pool *pool, bool bind)
Tejun Heoc34056a2010-06-29 10:07:11 +02001353{
Tejun Heob0f31fe2012-07-12 14:46:37 -07001354 struct global_cwq *gcwq = pool->gcwq;
Tejun Heof3421792010-07-02 10:03:51 +02001355 bool on_unbound_cpu = gcwq->cpu == WORK_CPU_UNBOUND;
Tejun Heo2bfedd32012-07-13 22:16:45 -07001356 const char *pri = worker_pool_pri(pool) ? "H" : "";
Tejun Heoc34056a2010-06-29 10:07:11 +02001357 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001358 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001359
Tejun Heo8b03ae32010-06-29 10:07:12 +02001360 spin_lock_irq(&gcwq->lock);
Tejun Heo47303942012-07-12 14:46:37 -07001361 while (ida_get_new(&pool->worker_ida, &id)) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001362 spin_unlock_irq(&gcwq->lock);
Tejun Heo47303942012-07-12 14:46:37 -07001363 if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001364 goto fail;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001365 spin_lock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001366 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02001367 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001368
1369 worker = alloc_worker();
1370 if (!worker)
1371 goto fail;
1372
Tejun Heo47303942012-07-12 14:46:37 -07001373 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001374 worker->id = id;
1375
Tejun Heof3421792010-07-02 10:03:51 +02001376 if (!on_unbound_cpu)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001377 worker->task = kthread_create_on_node(worker_thread,
Tejun Heo2bfedd32012-07-13 22:16:45 -07001378 worker, cpu_to_node(gcwq->cpu),
1379 "kworker/%u:%d%s", gcwq->cpu, id, pri);
Tejun Heof3421792010-07-02 10:03:51 +02001380 else
1381 worker->task = kthread_create(worker_thread, worker,
Tejun Heo2bfedd32012-07-13 22:16:45 -07001382 "kworker/u:%d%s", id, pri);
Tejun Heoc34056a2010-06-29 10:07:11 +02001383 if (IS_ERR(worker->task))
1384 goto fail;
1385
Tejun Heo2bfedd32012-07-13 22:16:45 -07001386 if (worker_pool_pri(pool))
1387 set_user_nice(worker->task, HIGHPRI_NICE_LEVEL);
1388
Tejun Heodb7bccf2010-06-29 10:07:12 +02001389 /*
1390 * A rogue worker will become a regular one if CPU comes
1391 * online later on. Make sure every worker has
1392 * PF_THREAD_BOUND set.
1393 */
Tejun Heof3421792010-07-02 10:03:51 +02001394 if (bind && !on_unbound_cpu)
Tejun Heo8b03ae32010-06-29 10:07:12 +02001395 kthread_bind(worker->task, gcwq->cpu);
Tejun Heof3421792010-07-02 10:03:51 +02001396 else {
Tejun Heodb7bccf2010-06-29 10:07:12 +02001397 worker->task->flags |= PF_THREAD_BOUND;
Tejun Heof3421792010-07-02 10:03:51 +02001398 if (on_unbound_cpu)
1399 worker->flags |= WORKER_UNBOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001401
Tejun Heoc34056a2010-06-29 10:07:11 +02001402 return worker;
1403fail:
1404 if (id >= 0) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001405 spin_lock_irq(&gcwq->lock);
Tejun Heo47303942012-07-12 14:46:37 -07001406 ida_remove(&pool->worker_ida, id);
Tejun Heo8b03ae32010-06-29 10:07:12 +02001407 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001408 }
1409 kfree(worker);
1410 return NULL;
1411}
1412
1413/**
1414 * start_worker - start a newly created worker
1415 * @worker: worker to start
1416 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001417 * Make the gcwq aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001418 *
1419 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001420 * spin_lock_irq(gcwq->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001421 */
1422static void start_worker(struct worker *worker)
1423{
Tejun Heocb444762010-07-02 10:03:50 +02001424 worker->flags |= WORKER_STARTED;
Tejun Heo47303942012-07-12 14:46:37 -07001425 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001426 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001427 wake_up_process(worker->task);
1428}
1429
1430/**
1431 * destroy_worker - destroy a workqueue worker
1432 * @worker: worker to be destroyed
1433 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001434 * Destroy @worker and adjust @gcwq stats accordingly.
1435 *
1436 * CONTEXT:
1437 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001438 */
1439static void destroy_worker(struct worker *worker)
1440{
Tejun Heo47303942012-07-12 14:46:37 -07001441 struct worker_pool *pool = worker->pool;
1442 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001443 int id = worker->id;
1444
1445 /* sanity check frenzy */
1446 BUG_ON(worker->current_work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001447 BUG_ON(!list_empty(&worker->scheduled));
Tejun Heoc34056a2010-06-29 10:07:11 +02001448
Tejun Heoc8e55f32010-06-29 10:07:12 +02001449 if (worker->flags & WORKER_STARTED)
Tejun Heo47303942012-07-12 14:46:37 -07001450 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001451 if (worker->flags & WORKER_IDLE)
Tejun Heo47303942012-07-12 14:46:37 -07001452 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001453
1454 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001455 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001456
1457 spin_unlock_irq(&gcwq->lock);
1458
Tejun Heoc34056a2010-06-29 10:07:11 +02001459 kthread_stop(worker->task);
1460 kfree(worker);
1461
Tejun Heo8b03ae32010-06-29 10:07:12 +02001462 spin_lock_irq(&gcwq->lock);
Tejun Heo47303942012-07-12 14:46:37 -07001463 ida_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001464}
1465
Tejun Heob0f31fe2012-07-12 14:46:37 -07001466static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001467{
Tejun Heob0f31fe2012-07-12 14:46:37 -07001468 struct worker_pool *pool = (void *)__pool;
1469 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001470
1471 spin_lock_irq(&gcwq->lock);
1472
Tejun Heob0f31fe2012-07-12 14:46:37 -07001473 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001474 struct worker *worker;
1475 unsigned long expires;
1476
1477 /* idle_list is kept in LIFO order, check the last one */
Tejun Heob0f31fe2012-07-12 14:46:37 -07001478 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001479 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1480
1481 if (time_before(jiffies, expires))
Tejun Heob0f31fe2012-07-12 14:46:37 -07001482 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001483 else {
1484 /* it's been idle for too long, wake up manager */
Tejun Heo1f19ade2012-07-12 14:46:37 -07001485 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heob0f31fe2012-07-12 14:46:37 -07001486 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001487 }
1488 }
1489
1490 spin_unlock_irq(&gcwq->lock);
1491}
1492
1493static bool send_mayday(struct work_struct *work)
1494{
1495 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1496 struct workqueue_struct *wq = cwq->wq;
Tejun Heof3421792010-07-02 10:03:51 +02001497 unsigned int cpu;
Tejun Heoe22bee72010-06-29 10:07:14 +02001498
1499 if (!(wq->flags & WQ_RESCUER))
1500 return false;
1501
1502 /* mayday mayday mayday */
Tejun Heo47303942012-07-12 14:46:37 -07001503 cpu = cwq->pool->gcwq->cpu;
Tejun Heof3421792010-07-02 10:03:51 +02001504 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1505 if (cpu == WORK_CPU_UNBOUND)
1506 cpu = 0;
Tejun Heof2e005a2010-07-20 15:59:09 +02001507 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001508 wake_up_process(wq->rescuer->task);
1509 return true;
1510}
1511
Tejun Heob0f31fe2012-07-12 14:46:37 -07001512static void gcwq_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001513{
Tejun Heob0f31fe2012-07-12 14:46:37 -07001514 struct worker_pool *pool = (void *)__pool;
1515 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001516 struct work_struct *work;
1517
1518 spin_lock_irq(&gcwq->lock);
1519
Tejun Heob0f31fe2012-07-12 14:46:37 -07001520 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001521 /*
1522 * We've been trying to create a new worker but
1523 * haven't been successful. We might be hitting an
1524 * allocation deadlock. Send distress signals to
1525 * rescuers.
1526 */
Tejun Heob0f31fe2012-07-12 14:46:37 -07001527 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001528 send_mayday(work);
1529 }
1530
1531 spin_unlock_irq(&gcwq->lock);
1532
Tejun Heob0f31fe2012-07-12 14:46:37 -07001533 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001534}
1535
1536/**
1537 * maybe_create_worker - create a new worker if necessary
Tejun Heob0f31fe2012-07-12 14:46:37 -07001538 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001539 *
Tejun Heob0f31fe2012-07-12 14:46:37 -07001540 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001541 * have at least one idle worker on return from this function. If
1542 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heob0f31fe2012-07-12 14:46:37 -07001543 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001544 * possible allocation deadlock.
1545 *
1546 * On return, need_to_create_worker() is guaranteed to be false and
1547 * may_start_working() true.
1548 *
1549 * LOCKING:
1550 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1551 * multiple times. Does GFP_KERNEL allocations. Called only from
1552 * manager.
1553 *
1554 * RETURNS:
1555 * false if no action was taken and gcwq->lock stayed locked, true
1556 * otherwise.
1557 */
Tejun Heob0f31fe2012-07-12 14:46:37 -07001558static bool maybe_create_worker(struct worker_pool *pool)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001559__releases(&gcwq->lock)
1560__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001561{
Tejun Heob0f31fe2012-07-12 14:46:37 -07001562 struct global_cwq *gcwq = pool->gcwq;
1563
1564 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001565 return false;
1566restart:
Tejun Heo9f9c2362010-07-14 11:31:20 +02001567 spin_unlock_irq(&gcwq->lock);
1568
Tejun Heoe22bee72010-06-29 10:07:14 +02001569 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heob0f31fe2012-07-12 14:46:37 -07001570 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001571
1572 while (true) {
1573 struct worker *worker;
1574
Tejun Heob0f31fe2012-07-12 14:46:37 -07001575 worker = create_worker(pool, true);
Tejun Heoe22bee72010-06-29 10:07:14 +02001576 if (worker) {
Tejun Heob0f31fe2012-07-12 14:46:37 -07001577 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001578 spin_lock_irq(&gcwq->lock);
1579 start_worker(worker);
Tejun Heob0f31fe2012-07-12 14:46:37 -07001580 BUG_ON(need_to_create_worker(pool));
Tejun Heoe22bee72010-06-29 10:07:14 +02001581 return true;
1582 }
1583
Tejun Heob0f31fe2012-07-12 14:46:37 -07001584 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001585 break;
1586
Tejun Heoe22bee72010-06-29 10:07:14 +02001587 __set_current_state(TASK_INTERRUPTIBLE);
1588 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001589
Tejun Heob0f31fe2012-07-12 14:46:37 -07001590 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001591 break;
1592 }
1593
Tejun Heob0f31fe2012-07-12 14:46:37 -07001594 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001595 spin_lock_irq(&gcwq->lock);
Tejun Heob0f31fe2012-07-12 14:46:37 -07001596 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001597 goto restart;
1598 return true;
1599}
1600
1601/**
1602 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heob0f31fe2012-07-12 14:46:37 -07001603 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001604 *
Tejun Heob0f31fe2012-07-12 14:46:37 -07001605 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02001606 * IDLE_WORKER_TIMEOUT.
1607 *
1608 * LOCKING:
1609 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1610 * multiple times. Called only from manager.
1611 *
1612 * RETURNS:
1613 * false if no action was taken and gcwq->lock stayed locked, true
1614 * otherwise.
1615 */
Tejun Heob0f31fe2012-07-12 14:46:37 -07001616static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001617{
1618 bool ret = false;
1619
Tejun Heob0f31fe2012-07-12 14:46:37 -07001620 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001621 struct worker *worker;
1622 unsigned long expires;
1623
Tejun Heob0f31fe2012-07-12 14:46:37 -07001624 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001625 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1626
1627 if (time_before(jiffies, expires)) {
Tejun Heob0f31fe2012-07-12 14:46:37 -07001628 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001629 break;
1630 }
1631
1632 destroy_worker(worker);
1633 ret = true;
1634 }
1635
1636 return ret;
1637}
1638
1639/**
1640 * manage_workers - manage worker pool
1641 * @worker: self
1642 *
1643 * Assume the manager role and manage gcwq worker pool @worker belongs
1644 * to. At any given time, there can be only zero or one manager per
1645 * gcwq. The exclusion is handled automatically by this function.
1646 *
1647 * The caller can safely start processing works on false return. On
1648 * true return, it's guaranteed that need_to_create_worker() is false
1649 * and may_start_working() is true.
1650 *
1651 * CONTEXT:
1652 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1653 * multiple times. Does GFP_KERNEL allocations.
1654 *
1655 * RETURNS:
1656 * false if no action was taken and gcwq->lock stayed locked, true if
1657 * some action was taken.
1658 */
1659static bool manage_workers(struct worker *worker)
1660{
Tejun Heob0f31fe2012-07-12 14:46:37 -07001661 struct worker_pool *pool = worker->pool;
1662 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001663 bool ret = false;
1664
Tejun Heo1f19ade2012-07-12 14:46:37 -07001665 if (pool->flags & POOL_MANAGING_WORKERS)
Tejun Heoe22bee72010-06-29 10:07:14 +02001666 return ret;
1667
Tejun Heo1f19ade2012-07-12 14:46:37 -07001668 pool->flags &= ~POOL_MANAGE_WORKERS;
1669 pool->flags |= POOL_MANAGING_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02001670
1671 /*
1672 * Destroy and then create so that may_start_working() is true
1673 * on return.
1674 */
Tejun Heob0f31fe2012-07-12 14:46:37 -07001675 ret |= maybe_destroy_workers(pool);
1676 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001677
Tejun Heo1f19ade2012-07-12 14:46:37 -07001678 pool->flags &= ~POOL_MANAGING_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02001679
1680 /*
1681 * The trustee might be waiting to take over the manager
1682 * position, tell it we're done.
1683 */
1684 if (unlikely(gcwq->trustee))
1685 wake_up_all(&gcwq->trustee_wait);
1686
1687 return ret;
1688}
1689
Tejun Heoa62428c2010-06-29 10:07:10 +02001690/**
Tejun Heoaffee4b2010-06-29 10:07:12 +02001691 * move_linked_works - move linked works to a list
1692 * @work: start of series of works to be scheduled
1693 * @head: target list to append @work to
1694 * @nextp: out paramter for nested worklist walking
1695 *
1696 * Schedule linked works starting from @work to @head. Work series to
1697 * be scheduled starts at @work and includes any consecutive work with
1698 * WORK_STRUCT_LINKED set in its predecessor.
1699 *
1700 * If @nextp is not NULL, it's updated to point to the next work of
1701 * the last scheduled work. This allows move_linked_works() to be
1702 * nested inside outer list_for_each_entry_safe().
1703 *
1704 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001705 * spin_lock_irq(gcwq->lock).
Tejun Heoaffee4b2010-06-29 10:07:12 +02001706 */
1707static void move_linked_works(struct work_struct *work, struct list_head *head,
1708 struct work_struct **nextp)
1709{
1710 struct work_struct *n;
1711
1712 /*
1713 * Linked worklist will always end before the end of the list,
1714 * use NULL for list head.
1715 */
1716 list_for_each_entry_safe_from(work, n, NULL, entry) {
1717 list_move_tail(&work->entry, head);
1718 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1719 break;
1720 }
1721
1722 /*
1723 * If we're already inside safe list traversal and have moved
1724 * multiple works to the scheduled queue, the next position
1725 * needs to be updated.
1726 */
1727 if (nextp)
1728 *nextp = n;
1729}
1730
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001731static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
1732{
1733 struct work_struct *work = list_first_entry(&cwq->delayed_works,
1734 struct work_struct, entry);
1735
Tejun Heocdadf002010-10-05 10:49:55 +02001736 trace_workqueue_activate_work(work);
Tejun Heo2bfedd32012-07-13 22:16:45 -07001737 move_linked_works(work, &cwq->pool->worklist, NULL);
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001738 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001739 cwq->nr_active++;
1740}
1741
Tejun Heoaffee4b2010-06-29 10:07:12 +02001742/**
Tejun Heo73f53c42010-06-29 10:07:11 +02001743 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
1744 * @cwq: cwq of interest
1745 * @color: color of work which left the queue
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001746 * @delayed: for a delayed work
Tejun Heo73f53c42010-06-29 10:07:11 +02001747 *
1748 * A work either has completed or is removed from pending queue,
1749 * decrement nr_in_flight of its cwq and handle workqueue flushing.
1750 *
1751 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001752 * spin_lock_irq(gcwq->lock).
Tejun Heo73f53c42010-06-29 10:07:11 +02001753 */
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001754static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color,
1755 bool delayed)
Tejun Heo73f53c42010-06-29 10:07:11 +02001756{
1757 /* ignore uncolored works */
1758 if (color == WORK_NO_COLOR)
1759 return;
1760
1761 cwq->nr_in_flight[color]--;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001762
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001763 if (!delayed) {
1764 cwq->nr_active--;
1765 if (!list_empty(&cwq->delayed_works)) {
1766 /* one down, submit a delayed one */
1767 if (cwq->nr_active < cwq->max_active)
1768 cwq_activate_first_delayed(cwq);
1769 }
Tejun Heo502ca9d2010-06-29 10:07:13 +02001770 }
Tejun Heo73f53c42010-06-29 10:07:11 +02001771
1772 /* is flush in progress and are we at the flushing tip? */
1773 if (likely(cwq->flush_color != color))
1774 return;
1775
1776 /* are there still in-flight works? */
1777 if (cwq->nr_in_flight[color])
1778 return;
1779
1780 /* this cwq is done, clear flush_color */
1781 cwq->flush_color = -1;
1782
1783 /*
1784 * If this was the last cwq, wake up the first flusher. It
1785 * will handle the rest.
1786 */
1787 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
1788 complete(&cwq->wq->first_flusher->done);
1789}
1790
1791/**
Tejun Heoa62428c2010-06-29 10:07:10 +02001792 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02001793 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02001794 * @work: work to process
1795 *
1796 * Process @work. This function contains all the logics necessary to
1797 * process a single work including synchronization against and
1798 * interaction with other workers on the same cpu, queueing and
1799 * flushing. As long as context requirement is met, any worker can
1800 * call this function to process a work.
1801 *
1802 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001803 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02001804 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001805static void process_one_work(struct worker *worker, struct work_struct *work)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001806__releases(&gcwq->lock)
1807__acquires(&gcwq->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02001808{
Tejun Heo7e116292010-06-29 10:07:13 +02001809 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heo47303942012-07-12 14:46:37 -07001810 struct worker_pool *pool = worker->pool;
1811 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001812 struct hlist_head *bwh = busy_worker_head(gcwq, work);
Tejun Heofb0e7be2010-06-29 10:07:15 +02001813 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heoa62428c2010-06-29 10:07:10 +02001814 work_func_t f = work->func;
Tejun Heo73f53c42010-06-29 10:07:11 +02001815 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02001816 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02001817#ifdef CONFIG_LOCKDEP
1818 /*
1819 * It is permissible to free the struct work_struct from
1820 * inside the function that is called from it, this we need to
1821 * take into account for lockdep too. To avoid bogus "held
1822 * lock freed" warnings as well as problems when looking into
1823 * work->lockdep_map, make a copy and use that here.
1824 */
1825 struct lockdep_map lockdep_map = work->lockdep_map;
1826#endif
Tejun Heo7e116292010-06-29 10:07:13 +02001827 /*
1828 * A single work shouldn't be executed concurrently by
1829 * multiple workers on a single cpu. Check whether anyone is
1830 * already processing the work. If so, defer the work to the
1831 * currently executing one.
1832 */
1833 collision = __find_worker_executing_work(gcwq, bwh, work);
1834 if (unlikely(collision)) {
1835 move_linked_works(work, &collision->scheduled, NULL);
1836 return;
1837 }
1838
Tejun Heoa62428c2010-06-29 10:07:10 +02001839 /* claim and process */
Tejun Heoa62428c2010-06-29 10:07:10 +02001840 debug_work_deactivate(work);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001841 hlist_add_head(&worker->hentry, bwh);
Tejun Heoc34056a2010-06-29 10:07:11 +02001842 worker->current_work = work;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001843 worker->current_cwq = cwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02001844 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02001845
Tejun Heo7a22ad72010-06-29 10:07:13 +02001846 /* record the current cpu number in the work data and dequeue */
1847 set_work_cpu(work, gcwq->cpu);
Tejun Heoa62428c2010-06-29 10:07:10 +02001848 list_del_init(&work->entry);
1849
Tejun Heo649027d2010-06-29 10:07:14 +02001850 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02001851 * CPU intensive works don't participate in concurrency
1852 * management. They're the scheduler's responsibility.
1853 */
1854 if (unlikely(cpu_intensive))
1855 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
1856
Tejun Heo0d8a2ff2012-07-12 14:46:37 -07001857 /*
1858 * Unbound gcwq isn't concurrency managed and work items should be
1859 * executed ASAP. Wake up another worker if necessary.
1860 */
Tejun Heob0f31fe2012-07-12 14:46:37 -07001861 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
1862 wake_up_worker(pool);
Tejun Heo0d8a2ff2012-07-12 14:46:37 -07001863
Tejun Heo8b03ae32010-06-29 10:07:12 +02001864 spin_unlock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001865
Tejun Heoa62428c2010-06-29 10:07:10 +02001866 work_clear_pending(work);
Tejun Heoe1594892011-01-09 23:32:15 +01001867 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02001868 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001869 trace_workqueue_execute_start(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02001870 f(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001871 /*
1872 * While we must be careful to not use "work" after this, the trace
1873 * point will only record its address.
1874 */
1875 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02001876 lock_map_release(&lockdep_map);
1877 lock_map_release(&cwq->wq->lockdep_map);
1878
1879 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
1880 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
1881 "%s/0x%08x/%d\n",
1882 current->comm, preempt_count(), task_pid_nr(current));
1883 printk(KERN_ERR " last function: ");
1884 print_symbol("%s\n", (unsigned long)f);
1885 debug_show_held_locks(current);
Syed Rameez Mustafa99625ba2013-07-15 11:52:09 -07001886 BUG_ON(PANIC_CORRUPTION);
Tejun Heoa62428c2010-06-29 10:07:10 +02001887 dump_stack();
1888 }
1889
Tejun Heo8b03ae32010-06-29 10:07:12 +02001890 spin_lock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001891
Tejun Heofb0e7be2010-06-29 10:07:15 +02001892 /* clear cpu intensive status */
1893 if (unlikely(cpu_intensive))
1894 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
1895
Tejun Heoa62428c2010-06-29 10:07:10 +02001896 /* we're done with it, release */
Tejun Heoc8e55f32010-06-29 10:07:12 +02001897 hlist_del_init(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001898 worker->current_work = NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001899 worker->current_cwq = NULL;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001900 cwq_dec_nr_in_flight(cwq, work_color, false);
Tejun Heoa62428c2010-06-29 10:07:10 +02001901}
1902
Tejun Heoaffee4b2010-06-29 10:07:12 +02001903/**
1904 * process_scheduled_works - process scheduled works
1905 * @worker: self
1906 *
1907 * Process all scheduled works. Please note that the scheduled list
1908 * may change while processing a work, so this function repeatedly
1909 * fetches a work from the top and executes it.
1910 *
1911 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001912 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02001913 * multiple times.
1914 */
1915static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916{
Tejun Heoaffee4b2010-06-29 10:07:12 +02001917 while (!list_empty(&worker->scheduled)) {
1918 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001920 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922}
1923
Tejun Heo4690c4a2010-06-29 10:07:10 +02001924/**
1925 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02001926 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02001927 *
Tejun Heoe22bee72010-06-29 10:07:14 +02001928 * The gcwq worker thread function. There's a single dynamic pool of
1929 * these per each cpu. These workers process all works regardless of
1930 * their specific target workqueue. The only exception is works which
1931 * belong to workqueues with a rescuer which will be explained in
1932 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02001933 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001934static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
Tejun Heoc34056a2010-06-29 10:07:11 +02001936 struct worker *worker = __worker;
Tejun Heo47303942012-07-12 14:46:37 -07001937 struct worker_pool *pool = worker->pool;
1938 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Tejun Heoe22bee72010-06-29 10:07:14 +02001940 /* tell the scheduler that this is a workqueue worker */
1941 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001942woke_up:
Tejun Heoc8e55f32010-06-29 10:07:12 +02001943 spin_lock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Tejun Heoc8e55f32010-06-29 10:07:12 +02001945 /* DIE can be set only while we're idle, checking here is enough */
1946 if (worker->flags & WORKER_DIE) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001947 spin_unlock_irq(&gcwq->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001948 worker->task->flags &= ~PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001949 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 }
1951
Tejun Heoc8e55f32010-06-29 10:07:12 +02001952 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001953recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02001954 /* no more worker necessary? */
Tejun Heob0f31fe2012-07-12 14:46:37 -07001955 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001956 goto sleep;
1957
1958 /* do we need to manage? */
Tejun Heob0f31fe2012-07-12 14:46:37 -07001959 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02001960 goto recheck;
1961
Tejun Heoc8e55f32010-06-29 10:07:12 +02001962 /*
1963 * ->scheduled list can only be filled while a worker is
1964 * preparing to process a work or actually processing it.
1965 * Make sure nobody diddled with it while I was sleeping.
1966 */
1967 BUG_ON(!list_empty(&worker->scheduled));
1968
Tejun Heoe22bee72010-06-29 10:07:14 +02001969 /*
1970 * When control reaches this point, we're guaranteed to have
1971 * at least one idle worker or that someone else has already
1972 * assumed the manager role.
1973 */
1974 worker_clr_flags(worker, WORKER_PREP);
1975
1976 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02001977 struct work_struct *work =
Tejun Heo47303942012-07-12 14:46:37 -07001978 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02001979 struct work_struct, entry);
1980
1981 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
1982 /* optimization path, not strictly necessary */
1983 process_one_work(worker, work);
1984 if (unlikely(!list_empty(&worker->scheduled)))
1985 process_scheduled_works(worker);
1986 } else {
1987 move_linked_works(work, &worker->scheduled, NULL);
1988 process_scheduled_works(worker);
1989 }
Tejun Heob0f31fe2012-07-12 14:46:37 -07001990 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02001991
Tejun Heoe22bee72010-06-29 10:07:14 +02001992 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02001993sleep:
Tejun Heob0f31fe2012-07-12 14:46:37 -07001994 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02001995 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02001996
Tejun Heoc8e55f32010-06-29 10:07:12 +02001997 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02001998 * gcwq->lock is held and there's no work to process and no
1999 * need to manage, sleep. Workers are woken up only while
2000 * holding gcwq->lock or from local cpu, so setting the
2001 * current state before releasing gcwq->lock is enough to
2002 * prevent losing any event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002003 */
2004 worker_enter_idle(worker);
2005 __set_current_state(TASK_INTERRUPTIBLE);
2006 spin_unlock_irq(&gcwq->lock);
2007 schedule();
2008 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009}
2010
Tejun Heoe22bee72010-06-29 10:07:14 +02002011/**
2012 * rescuer_thread - the rescuer thread function
2013 * @__wq: the associated workqueue
2014 *
2015 * Workqueue rescuer thread function. There's one rescuer for each
2016 * workqueue which has WQ_RESCUER set.
2017 *
2018 * Regular work processing on a gcwq may block trying to create a new
2019 * worker which uses GFP_KERNEL allocation which has slight chance of
2020 * developing into deadlock if some works currently on the same queue
2021 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2022 * the problem rescuer solves.
2023 *
2024 * When such condition is possible, the gcwq summons rescuers of all
2025 * workqueues which have works queued on the gcwq and let them process
2026 * those works so that forward progress can be guaranteed.
2027 *
2028 * This should happen rarely.
2029 */
2030static int rescuer_thread(void *__wq)
2031{
2032 struct workqueue_struct *wq = __wq;
2033 struct worker *rescuer = wq->rescuer;
2034 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heof3421792010-07-02 10:03:51 +02002035 bool is_unbound = wq->flags & WQ_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02002036 unsigned int cpu;
2037
2038 set_user_nice(current, RESCUER_NICE_LEVEL);
2039repeat:
2040 set_current_state(TASK_INTERRUPTIBLE);
2041
2042 if (kthread_should_stop())
2043 return 0;
2044
Tejun Heof3421792010-07-02 10:03:51 +02002045 /*
2046 * See whether any cpu is asking for help. Unbounded
2047 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
2048 */
Tejun Heof2e005a2010-07-20 15:59:09 +02002049 for_each_mayday_cpu(cpu, wq->mayday_mask) {
Tejun Heof3421792010-07-02 10:03:51 +02002050 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2051 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
Tejun Heo47303942012-07-12 14:46:37 -07002052 struct worker_pool *pool = cwq->pool;
2053 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002054 struct work_struct *work, *n;
2055
2056 __set_current_state(TASK_RUNNING);
Tejun Heof2e005a2010-07-20 15:59:09 +02002057 mayday_clear_cpu(cpu, wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002058
2059 /* migrate to the target cpu if possible */
Tejun Heo47303942012-07-12 14:46:37 -07002060 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002061 worker_maybe_bind_and_lock(rescuer);
2062
2063 /*
2064 * Slurp in all works issued via this workqueue and
2065 * process'em.
2066 */
2067 BUG_ON(!list_empty(&rescuer->scheduled));
Tejun Heo47303942012-07-12 14:46:37 -07002068 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02002069 if (get_work_cwq(work) == cwq)
2070 move_linked_works(work, scheduled, &n);
2071
2072 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002073
2074 /*
2075 * Leave this gcwq. If keep_working() is %true, notify a
2076 * regular worker; otherwise, we end up with 0 concurrency
2077 * and stalling the execution.
2078 */
Tejun Heob0f31fe2012-07-12 14:46:37 -07002079 if (keep_working(pool))
2080 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002081
Tejun Heoe22bee72010-06-29 10:07:14 +02002082 spin_unlock_irq(&gcwq->lock);
2083 }
2084
2085 schedule();
2086 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087}
2088
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002089struct wq_barrier {
2090 struct work_struct work;
2091 struct completion done;
2092};
2093
2094static void wq_barrier_func(struct work_struct *work)
2095{
2096 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2097 complete(&barr->done);
2098}
2099
Tejun Heo4690c4a2010-06-29 10:07:10 +02002100/**
2101 * insert_wq_barrier - insert a barrier work
2102 * @cwq: cwq to insert barrier into
2103 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002104 * @target: target work to attach @barr to
2105 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002106 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002107 * @barr is linked to @target such that @barr is completed only after
2108 * @target finishes execution. Please note that the ordering
2109 * guarantee is observed only with respect to @target and on the local
2110 * cpu.
2111 *
2112 * Currently, a queued barrier can't be canceled. This is because
2113 * try_to_grab_pending() can't determine whether the work to be
2114 * grabbed is at the head of the queue and thus can't clear LINKED
2115 * flag of the previous work while there must be a valid next work
2116 * after a work with LINKED flag set.
2117 *
2118 * Note that when @worker is non-NULL, @target may be modified
2119 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002120 *
2121 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002122 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002123 */
Oleg Nesterov83c22522007-05-09 02:33:54 -07002124static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002125 struct wq_barrier *barr,
2126 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002127{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002128 struct list_head *head;
2129 unsigned int linked = 0;
2130
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002131 /*
Tejun Heo8b03ae32010-06-29 10:07:12 +02002132 * debugobject calls are safe here even with gcwq->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002133 * as we know for sure that this will not trigger any of the
2134 * checks and call back into the fixup functions where we
2135 * might deadlock.
2136 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002137 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002138 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002139 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002140
Tejun Heoaffee4b2010-06-29 10:07:12 +02002141 /*
2142 * If @target is currently being executed, schedule the
2143 * barrier to the worker; otherwise, put it after @target.
2144 */
2145 if (worker)
2146 head = worker->scheduled.next;
2147 else {
2148 unsigned long *bits = work_data_bits(target);
2149
2150 head = target->entry.next;
2151 /* there can already be other linked works, inherit and set */
2152 linked = *bits & WORK_STRUCT_LINKED;
2153 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2154 }
2155
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002156 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002157 insert_work(cwq, &barr->work, head,
2158 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002159}
2160
Tejun Heo73f53c42010-06-29 10:07:11 +02002161/**
2162 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2163 * @wq: workqueue being flushed
2164 * @flush_color: new flush color, < 0 for no-op
2165 * @work_color: new work color, < 0 for no-op
2166 *
2167 * Prepare cwqs for workqueue flushing.
2168 *
2169 * If @flush_color is non-negative, flush_color on all cwqs should be
2170 * -1. If no cwq has in-flight commands at the specified color, all
2171 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2172 * has in flight commands, its cwq->flush_color is set to
2173 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2174 * wakeup logic is armed and %true is returned.
2175 *
2176 * The caller should have initialized @wq->first_flusher prior to
2177 * calling this function with non-negative @flush_color. If
2178 * @flush_color is negative, no flush color update is done and %false
2179 * is returned.
2180 *
2181 * If @work_color is non-negative, all cwqs should have the same
2182 * work_color which is previous to @work_color and all will be
2183 * advanced to @work_color.
2184 *
2185 * CONTEXT:
2186 * mutex_lock(wq->flush_mutex).
2187 *
2188 * RETURNS:
2189 * %true if @flush_color >= 0 and there's something to flush. %false
2190 * otherwise.
2191 */
2192static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2193 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194{
Tejun Heo73f53c42010-06-29 10:07:11 +02002195 bool wait = false;
2196 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -07002197
Tejun Heo73f53c42010-06-29 10:07:11 +02002198 if (flush_color >= 0) {
2199 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2200 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002201 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002202
Tejun Heof3421792010-07-02 10:03:51 +02002203 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002204 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo47303942012-07-12 14:46:37 -07002205 struct global_cwq *gcwq = cwq->pool->gcwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002206
Tejun Heo8b03ae32010-06-29 10:07:12 +02002207 spin_lock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002208
2209 if (flush_color >= 0) {
2210 BUG_ON(cwq->flush_color != -1);
2211
2212 if (cwq->nr_in_flight[flush_color]) {
2213 cwq->flush_color = flush_color;
2214 atomic_inc(&wq->nr_cwqs_to_flush);
2215 wait = true;
2216 }
2217 }
2218
2219 if (work_color >= 0) {
2220 BUG_ON(work_color != work_next_color(cwq->work_color));
2221 cwq->work_color = work_color;
2222 }
2223
Tejun Heo8b03ae32010-06-29 10:07:12 +02002224 spin_unlock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002225 }
2226
2227 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2228 complete(&wq->first_flusher->done);
2229
2230 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231}
2232
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002233/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002235 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 *
2237 * Forces execution of the workqueue and blocks until its completion.
2238 * This is typically used in driver shutdown handlers.
2239 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002240 * We sleep until all works which were queued on entry have been handled,
2241 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002243void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244{
Tejun Heo73f53c42010-06-29 10:07:11 +02002245 struct wq_flusher this_flusher = {
2246 .list = LIST_HEAD_INIT(this_flusher.list),
2247 .flush_color = -1,
2248 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2249 };
2250 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002251
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002252 lock_map_acquire(&wq->lockdep_map);
2253 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002254
2255 mutex_lock(&wq->flush_mutex);
2256
2257 /*
2258 * Start-to-wait phase
2259 */
2260 next_color = work_next_color(wq->work_color);
2261
2262 if (next_color != wq->flush_color) {
2263 /*
2264 * Color space is not full. The current work_color
2265 * becomes our flush_color and work_color is advanced
2266 * by one.
2267 */
2268 BUG_ON(!list_empty(&wq->flusher_overflow));
2269 this_flusher.flush_color = wq->work_color;
2270 wq->work_color = next_color;
2271
2272 if (!wq->first_flusher) {
2273 /* no flush in progress, become the first flusher */
2274 BUG_ON(wq->flush_color != this_flusher.flush_color);
2275
2276 wq->first_flusher = &this_flusher;
2277
2278 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2279 wq->work_color)) {
2280 /* nothing to flush, done */
2281 wq->flush_color = next_color;
2282 wq->first_flusher = NULL;
2283 goto out_unlock;
2284 }
2285 } else {
2286 /* wait in queue */
2287 BUG_ON(wq->flush_color == this_flusher.flush_color);
2288 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2289 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2290 }
2291 } else {
2292 /*
2293 * Oops, color space is full, wait on overflow queue.
2294 * The next flush completion will assign us
2295 * flush_color and transfer to flusher_queue.
2296 */
2297 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2298 }
2299
2300 mutex_unlock(&wq->flush_mutex);
2301
2302 wait_for_completion(&this_flusher.done);
2303
2304 /*
2305 * Wake-up-and-cascade phase
2306 *
2307 * First flushers are responsible for cascading flushes and
2308 * handling overflow. Non-first flushers can simply return.
2309 */
2310 if (wq->first_flusher != &this_flusher)
2311 return;
2312
2313 mutex_lock(&wq->flush_mutex);
2314
Tejun Heo4ce48b32010-07-02 10:03:51 +02002315 /* we might have raced, check again with mutex held */
2316 if (wq->first_flusher != &this_flusher)
2317 goto out_unlock;
2318
Tejun Heo73f53c42010-06-29 10:07:11 +02002319 wq->first_flusher = NULL;
2320
2321 BUG_ON(!list_empty(&this_flusher.list));
2322 BUG_ON(wq->flush_color != this_flusher.flush_color);
2323
2324 while (true) {
2325 struct wq_flusher *next, *tmp;
2326
2327 /* complete all the flushers sharing the current flush color */
2328 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2329 if (next->flush_color != wq->flush_color)
2330 break;
2331 list_del_init(&next->list);
2332 complete(&next->done);
2333 }
2334
2335 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2336 wq->flush_color != work_next_color(wq->work_color));
2337
2338 /* this flush_color is finished, advance by one */
2339 wq->flush_color = work_next_color(wq->flush_color);
2340
2341 /* one color has been freed, handle overflow queue */
2342 if (!list_empty(&wq->flusher_overflow)) {
2343 /*
2344 * Assign the same color to all overflowed
2345 * flushers, advance work_color and append to
2346 * flusher_queue. This is the start-to-wait
2347 * phase for these overflowed flushers.
2348 */
2349 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2350 tmp->flush_color = wq->work_color;
2351
2352 wq->work_color = work_next_color(wq->work_color);
2353
2354 list_splice_tail_init(&wq->flusher_overflow,
2355 &wq->flusher_queue);
2356 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2357 }
2358
2359 if (list_empty(&wq->flusher_queue)) {
2360 BUG_ON(wq->flush_color != wq->work_color);
2361 break;
2362 }
2363
2364 /*
2365 * Need to flush more colors. Make the next flusher
2366 * the new first flusher and arm cwqs.
2367 */
2368 BUG_ON(wq->flush_color == wq->work_color);
2369 BUG_ON(wq->flush_color != next->flush_color);
2370
2371 list_del_init(&next->list);
2372 wq->first_flusher = next;
2373
2374 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2375 break;
2376
2377 /*
2378 * Meh... this color is already done, clear first
2379 * flusher and repeat cascading.
2380 */
2381 wq->first_flusher = NULL;
2382 }
2383
2384out_unlock:
2385 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386}
Dave Jonesae90dd52006-06-30 01:40:45 -04002387EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002389/**
2390 * drain_workqueue - drain a workqueue
2391 * @wq: workqueue to drain
2392 *
2393 * Wait until the workqueue becomes empty. While draining is in progress,
2394 * only chain queueing is allowed. IOW, only currently pending or running
2395 * work items on @wq can queue further work items on it. @wq is flushed
2396 * repeatedly until it becomes empty. The number of flushing is detemined
2397 * by the depth of chaining and should be relatively short. Whine if it
2398 * takes too long.
2399 */
2400void drain_workqueue(struct workqueue_struct *wq)
2401{
2402 unsigned int flush_cnt = 0;
2403 unsigned int cpu;
2404
2405 /*
2406 * __queue_work() needs to test whether there are drainers, is much
2407 * hotter than drain_workqueue() and already looks at @wq->flags.
2408 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2409 */
2410 spin_lock(&workqueue_lock);
2411 if (!wq->nr_drainers++)
2412 wq->flags |= WQ_DRAINING;
2413 spin_unlock(&workqueue_lock);
2414reflush:
2415 flush_workqueue(wq);
2416
2417 for_each_cwq_cpu(cpu, wq) {
2418 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002419 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002420
Tejun Heo47303942012-07-12 14:46:37 -07002421 spin_lock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002422 drained = !cwq->nr_active && list_empty(&cwq->delayed_works);
Tejun Heo47303942012-07-12 14:46:37 -07002423 spin_unlock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002424
2425 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002426 continue;
2427
2428 if (++flush_cnt == 10 ||
2429 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
2430 pr_warning("workqueue %s: flush on destruction isn't complete after %u tries\n",
2431 wq->name, flush_cnt);
2432 goto reflush;
2433 }
2434
2435 spin_lock(&workqueue_lock);
2436 if (!--wq->nr_drainers)
2437 wq->flags &= ~WQ_DRAINING;
2438 spin_unlock(&workqueue_lock);
2439}
2440EXPORT_SYMBOL_GPL(drain_workqueue);
2441
Tejun Heobaf59022010-09-16 10:42:16 +02002442static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
2443 bool wait_executing)
2444{
2445 struct worker *worker = NULL;
2446 struct global_cwq *gcwq;
2447 struct cpu_workqueue_struct *cwq;
2448
2449 might_sleep();
2450 gcwq = get_work_gcwq(work);
2451 if (!gcwq)
2452 return false;
2453
2454 spin_lock_irq(&gcwq->lock);
2455 if (!list_empty(&work->entry)) {
2456 /*
2457 * See the comment near try_to_grab_pending()->smp_rmb().
2458 * If it was re-queued to a different gcwq under us, we
2459 * are not going to wait.
2460 */
2461 smp_rmb();
2462 cwq = get_work_cwq(work);
Tejun Heo47303942012-07-12 14:46:37 -07002463 if (unlikely(!cwq || gcwq != cwq->pool->gcwq))
Tejun Heobaf59022010-09-16 10:42:16 +02002464 goto already_gone;
2465 } else if (wait_executing) {
2466 worker = find_worker_executing_work(gcwq, work);
2467 if (!worker)
2468 goto already_gone;
2469 cwq = worker->current_cwq;
2470 } else
2471 goto already_gone;
2472
2473 insert_wq_barrier(cwq, barr, work, worker);
2474 spin_unlock_irq(&gcwq->lock);
2475
Tejun Heoe1594892011-01-09 23:32:15 +01002476 /*
2477 * If @max_active is 1 or rescuer is in use, flushing another work
2478 * item on the same workqueue may lead to deadlock. Make sure the
2479 * flusher is not running on the same workqueue by verifying write
2480 * access.
2481 */
2482 if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER)
2483 lock_map_acquire(&cwq->wq->lockdep_map);
2484 else
2485 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heobaf59022010-09-16 10:42:16 +02002486 lock_map_release(&cwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002487
Tejun Heobaf59022010-09-16 10:42:16 +02002488 return true;
2489already_gone:
2490 spin_unlock_irq(&gcwq->lock);
2491 return false;
2492}
2493
Oleg Nesterovdb700892008-07-25 01:47:49 -07002494/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002495 * flush_work - wait for a work to finish executing the last queueing instance
2496 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002497 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002498 * Wait until @work has finished execution. This function considers
2499 * only the last queueing instance of @work. If @work has been
2500 * enqueued across different CPUs on a non-reentrant workqueue or on
2501 * multiple workqueues, @work might still be executing on return on
2502 * some of the CPUs from earlier queueing.
Oleg Nesterova67da702008-07-25 01:47:52 -07002503 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002504 * If @work was queued only on a non-reentrant, ordered or unbound
2505 * workqueue, @work is guaranteed to be idle on return if it hasn't
2506 * been requeued since flush started.
2507 *
2508 * RETURNS:
2509 * %true if flush_work() waited for the work to finish execution,
2510 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002511 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002512bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002513{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002514 struct wq_barrier barr;
2515
Tejun Heobaf59022010-09-16 10:42:16 +02002516 if (start_flush_work(work, &barr, true)) {
2517 wait_for_completion(&barr.done);
2518 destroy_work_on_stack(&barr.work);
2519 return true;
2520 } else
2521 return false;
Oleg Nesterovdb700892008-07-25 01:47:49 -07002522}
2523EXPORT_SYMBOL_GPL(flush_work);
2524
Tejun Heo401a8d02010-09-16 10:36:00 +02002525static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work)
2526{
2527 struct wq_barrier barr;
2528 struct worker *worker;
2529
2530 spin_lock_irq(&gcwq->lock);
2531
2532 worker = find_worker_executing_work(gcwq, work);
2533 if (unlikely(worker))
2534 insert_wq_barrier(worker->current_cwq, &barr, work, worker);
2535
2536 spin_unlock_irq(&gcwq->lock);
2537
2538 if (unlikely(worker)) {
2539 wait_for_completion(&barr.done);
2540 destroy_work_on_stack(&barr.work);
2541 return true;
2542 } else
2543 return false;
2544}
2545
2546static bool wait_on_work(struct work_struct *work)
2547{
2548 bool ret = false;
2549 int cpu;
2550
2551 might_sleep();
2552
2553 lock_map_acquire(&work->lockdep_map);
2554 lock_map_release(&work->lockdep_map);
2555
2556 for_each_gcwq_cpu(cpu)
2557 ret |= wait_on_cpu_work(get_gcwq(cpu), work);
2558 return ret;
2559}
2560
Tejun Heo09383492010-09-16 10:48:29 +02002561/**
2562 * flush_work_sync - wait until a work has finished execution
2563 * @work: the work to flush
2564 *
2565 * Wait until @work has finished execution. On return, it's
2566 * guaranteed that all queueing instances of @work which happened
2567 * before this function is called are finished. In other words, if
2568 * @work hasn't been requeued since this function was called, @work is
2569 * guaranteed to be idle on return.
2570 *
2571 * RETURNS:
2572 * %true if flush_work_sync() waited for the work to finish execution,
2573 * %false if it was already idle.
2574 */
2575bool flush_work_sync(struct work_struct *work)
2576{
2577 struct wq_barrier barr;
2578 bool pending, waited;
2579
2580 /* we'll wait for executions separately, queue barr only if pending */
2581 pending = start_flush_work(work, &barr, false);
2582
2583 /* wait for executions to finish */
2584 waited = wait_on_work(work);
2585
2586 /* wait for the pending one */
2587 if (pending) {
2588 wait_for_completion(&barr.done);
2589 destroy_work_on_stack(&barr.work);
2590 }
2591
2592 return pending || waited;
2593}
2594EXPORT_SYMBOL_GPL(flush_work_sync);
2595
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002596/*
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002597 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002598 * so this work can't be re-armed in any way.
2599 */
2600static int try_to_grab_pending(struct work_struct *work)
2601{
Tejun Heo8b03ae32010-06-29 10:07:12 +02002602 struct global_cwq *gcwq;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002603 int ret = -1;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002604
Tejun Heo22df02b2010-06-29 10:07:10 +02002605 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002606 return 0;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002607
2608 /*
2609 * The queueing is in progress, or it is already queued. Try to
2610 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
2611 */
Tejun Heo7a22ad72010-06-29 10:07:13 +02002612 gcwq = get_work_gcwq(work);
2613 if (!gcwq)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002614 return ret;
2615
Tejun Heo8b03ae32010-06-29 10:07:12 +02002616 spin_lock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002617 if (!list_empty(&work->entry)) {
2618 /*
Tejun Heo7a22ad72010-06-29 10:07:13 +02002619 * This work is queued, but perhaps we locked the wrong gcwq.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002620 * In that case we must see the new value after rmb(), see
2621 * insert_work()->wmb().
2622 */
2623 smp_rmb();
Tejun Heo7a22ad72010-06-29 10:07:13 +02002624 if (gcwq == get_work_gcwq(work)) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002625 debug_work_deactivate(work);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002626 list_del_init(&work->entry);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002627 cwq_dec_nr_in_flight(get_work_cwq(work),
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02002628 get_work_color(work),
2629 *work_data_bits(work) & WORK_STRUCT_DELAYED);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002630 ret = 1;
2631 }
2632 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02002633 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002634
2635 return ret;
2636}
2637
Tejun Heo401a8d02010-09-16 10:36:00 +02002638static bool __cancel_work_timer(struct work_struct *work,
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002639 struct timer_list* timer)
2640{
2641 int ret;
2642
2643 do {
2644 ret = (timer && likely(del_timer(timer)));
2645 if (!ret)
2646 ret = try_to_grab_pending(work);
2647 wait_on_work(work);
2648 } while (unlikely(ret < 0));
2649
Tejun Heo7a22ad72010-06-29 10:07:13 +02002650 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002651 return ret;
2652}
2653
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002654/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002655 * cancel_work_sync - cancel a work and wait for it to finish
2656 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002657 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002658 * Cancel @work and wait for its execution to finish. This function
2659 * can be used even if the work re-queues itself or migrates to
2660 * another workqueue. On return from this function, @work is
2661 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002662 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002663 * cancel_work_sync(&delayed_work->work) must not be used for
2664 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002665 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002666 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002667 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002668 *
2669 * RETURNS:
2670 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002671 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002672bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002673{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002674 return __cancel_work_timer(work, NULL);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002675}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002676EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002677
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002678/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002679 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2680 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002681 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002682 * Delayed timer is cancelled and the pending work is queued for
2683 * immediate execution. Like flush_work(), this function only
2684 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002685 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002686 * RETURNS:
2687 * %true if flush_work() waited for the work to finish execution,
2688 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002689 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002690bool flush_delayed_work(struct delayed_work *dwork)
2691{
2692 if (del_timer_sync(&dwork->timer))
2693 __queue_work(raw_smp_processor_id(),
2694 get_work_cwq(&dwork->work)->wq, &dwork->work);
2695 return flush_work(&dwork->work);
2696}
2697EXPORT_SYMBOL(flush_delayed_work);
2698
2699/**
Tejun Heo09383492010-09-16 10:48:29 +02002700 * flush_delayed_work_sync - wait for a dwork to finish
2701 * @dwork: the delayed work to flush
2702 *
2703 * Delayed timer is cancelled and the pending work is queued for
2704 * execution immediately. Other than timer handling, its behavior
2705 * is identical to flush_work_sync().
2706 *
2707 * RETURNS:
2708 * %true if flush_work_sync() waited for the work to finish execution,
2709 * %false if it was already idle.
2710 */
2711bool flush_delayed_work_sync(struct delayed_work *dwork)
2712{
2713 if (del_timer_sync(&dwork->timer))
2714 __queue_work(raw_smp_processor_id(),
2715 get_work_cwq(&dwork->work)->wq, &dwork->work);
2716 return flush_work_sync(&dwork->work);
2717}
2718EXPORT_SYMBOL(flush_delayed_work_sync);
2719
2720/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002721 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2722 * @dwork: the delayed work cancel
2723 *
2724 * This is cancel_work_sync() for delayed works.
2725 *
2726 * RETURNS:
2727 * %true if @dwork was pending, %false otherwise.
2728 */
2729bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002730{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002731 return __cancel_work_timer(&dwork->work, &dwork->timer);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002732}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002733EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002735/**
2736 * schedule_work - put work task in global workqueue
2737 * @work: job to be done
2738 *
Bart Van Assche5b0f437d2009-07-30 19:00:53 +02002739 * Returns zero if @work was already on the kernel-global workqueue and
2740 * non-zero otherwise.
2741 *
2742 * This puts a job in the kernel-global workqueue if it was not already
2743 * queued and leaves it in the same position on the kernel-global
2744 * workqueue otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002745 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002746int schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747{
Tejun Heod320c032010-06-29 10:07:14 +02002748 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749}
Dave Jonesae90dd52006-06-30 01:40:45 -04002750EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
Zhang Ruic1a220e2008-07-23 21:28:39 -07002752/*
2753 * schedule_work_on - put work task on a specific cpu
2754 * @cpu: cpu to put the work task on
2755 * @work: job to be done
2756 *
2757 * This puts a job on a specific cpu
2758 */
2759int schedule_work_on(int cpu, struct work_struct *work)
2760{
Tejun Heod320c032010-06-29 10:07:14 +02002761 return queue_work_on(cpu, system_wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07002762}
2763EXPORT_SYMBOL(schedule_work_on);
2764
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002765/**
2766 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +00002767 * @dwork: job to be done
2768 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002769 *
2770 * After waiting for a given time this puts a job in the kernel-global
2771 * workqueue.
2772 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002773int schedule_delayed_work(struct delayed_work *dwork,
Ingo Molnar82f67cd2007-02-16 01:28:13 -08002774 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775{
Tejun Heod320c032010-06-29 10:07:14 +02002776 return queue_delayed_work(system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777}
Dave Jonesae90dd52006-06-30 01:40:45 -04002778EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002780/**
2781 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
2782 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +00002783 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002784 * @delay: number of jiffies to wait
2785 *
2786 * After waiting for a given time this puts a job in the kernel-global
2787 * workqueue on the specified CPU.
2788 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +00002790 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791{
Tejun Heod320c032010-06-29 10:07:14 +02002792 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793}
Dave Jonesae90dd52006-06-30 01:40:45 -04002794EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
Andrew Mortonb6136772006-06-25 05:47:49 -07002796/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002797 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002798 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002799 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002800 * schedule_on_each_cpu() executes @func on each online CPU using the
2801 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002802 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002803 *
2804 * RETURNS:
2805 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002806 */
David Howells65f27f32006-11-22 14:55:48 +00002807int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002808{
2809 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002810 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002811
Andrew Mortonb6136772006-06-25 05:47:49 -07002812 works = alloc_percpu(struct work_struct);
2813 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002814 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002815
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002816 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002817
Christoph Lameter15316ba2006-01-08 01:00:43 -08002818 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002819 struct work_struct *work = per_cpu_ptr(works, cpu);
2820
2821 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002822 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002823 }
Tejun Heo93981802009-11-17 14:06:20 -08002824
2825 for_each_online_cpu(cpu)
2826 flush_work(per_cpu_ptr(works, cpu));
2827
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002828 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002829 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002830 return 0;
2831}
2832
Alan Sterneef6a7d2010-02-12 17:39:21 +09002833/**
2834 * flush_scheduled_work - ensure that any scheduled work has run to completion.
2835 *
2836 * Forces execution of the kernel-global workqueue and blocks until its
2837 * completion.
2838 *
2839 * Think twice before calling this function! It's very easy to get into
2840 * trouble if you don't take great care. Either of the following situations
2841 * will lead to deadlock:
2842 *
2843 * One of the work items currently on the workqueue needs to acquire
2844 * a lock held by your code or its caller.
2845 *
2846 * Your code is running in the context of a work routine.
2847 *
2848 * They will be detected by lockdep when they occur, but the first might not
2849 * occur very often. It depends on what work items are on the workqueue and
2850 * what locks they need, which you have no control over.
2851 *
2852 * In most situations flushing the entire workqueue is overkill; you merely
2853 * need to know that a particular work item isn't queued and isn't running.
2854 * In such cases you should use cancel_delayed_work_sync() or
2855 * cancel_work_sync() instead.
2856 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857void flush_scheduled_work(void)
2858{
Tejun Heod320c032010-06-29 10:07:14 +02002859 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860}
Dave Jonesae90dd52006-06-30 01:40:45 -04002861EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
2863/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06002864 * execute_in_process_context - reliably execute the routine with user context
2865 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06002866 * @ew: guaranteed storage for the execute work structure (must
2867 * be available when the work executes)
2868 *
2869 * Executes the function immediately if process context is available,
2870 * otherwise schedules the function for delayed execution.
2871 *
2872 * Returns: 0 - function was executed
2873 * 1 - function was scheduled for execution
2874 */
David Howells65f27f32006-11-22 14:55:48 +00002875int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06002876{
2877 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00002878 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002879 return 0;
2880 }
2881
David Howells65f27f32006-11-22 14:55:48 +00002882 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002883 schedule_work(&ew->work);
2884
2885 return 1;
2886}
2887EXPORT_SYMBOL_GPL(execute_in_process_context);
2888
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889int keventd_up(void)
2890{
Tejun Heod320c032010-06-29 10:07:14 +02002891 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892}
2893
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002894static int alloc_cwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895{
Oleg Nesterov3af244332007-05-09 02:34:09 -07002896 /*
Tejun Heo0f900042010-06-29 10:07:11 +02002897 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
2898 * Make sure that the alignment isn't lower than that of
2899 * unsigned long long.
Oleg Nesterov3af244332007-05-09 02:34:09 -07002900 */
Tejun Heo0f900042010-06-29 10:07:11 +02002901 const size_t size = sizeof(struct cpu_workqueue_struct);
2902 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
2903 __alignof__(unsigned long long));
Oleg Nesterov3af244332007-05-09 02:34:09 -07002904
Lai Jiangshane06ffa12012-03-09 18:03:20 +08002905 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02002906 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
Tejun Heo931ac772010-07-20 11:07:48 +02002907 else {
Tejun Heof3421792010-07-02 10:03:51 +02002908 void *ptr;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01002909
Tejun Heof3421792010-07-02 10:03:51 +02002910 /*
2911 * Allocate enough room to align cwq and put an extra
2912 * pointer at the end pointing back to the originally
2913 * allocated pointer which will be used for free.
2914 */
2915 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
2916 if (ptr) {
2917 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
2918 *(void **)(wq->cpu_wq.single + 1) = ptr;
2919 }
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002920 }
Tejun Heof3421792010-07-02 10:03:51 +02002921
Tejun Heo0415b002011-03-24 18:50:09 +01002922 /* just in case, make sure it's actually aligned */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002923 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
2924 return wq->cpu_wq.v ? 0 : -ENOMEM;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002925}
2926
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002927static void free_cwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002928{
Lai Jiangshane06ffa12012-03-09 18:03:20 +08002929 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02002930 free_percpu(wq->cpu_wq.pcpu);
2931 else if (wq->cpu_wq.single) {
2932 /* the pointer to free is stored right after the cwq */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002933 kfree(*(void **)(wq->cpu_wq.single + 1));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002934 }
2935}
2936
Tejun Heof3421792010-07-02 10:03:51 +02002937static int wq_clamp_max_active(int max_active, unsigned int flags,
2938 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002939{
Tejun Heof3421792010-07-02 10:03:51 +02002940 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
2941
2942 if (max_active < 1 || max_active > lim)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002943 printk(KERN_WARNING "workqueue: max_active %d requested for %s "
2944 "is out of range, clamping between %d and %d\n",
Tejun Heof3421792010-07-02 10:03:51 +02002945 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002946
Tejun Heof3421792010-07-02 10:03:51 +02002947 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002948}
2949
Tejun Heob196be82012-01-10 15:11:35 -08002950struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02002951 unsigned int flags,
2952 int max_active,
2953 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08002954 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07002955{
Tejun Heob196be82012-01-10 15:11:35 -08002956 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002957 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02002958 unsigned int cpu;
Tejun Heob196be82012-01-10 15:11:35 -08002959 size_t namelen;
2960
2961 /* determine namelen, allocate wq and format name */
2962 va_start(args, lock_name);
2963 va_copy(args1, args);
2964 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
2965
2966 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
2967 if (!wq)
2968 goto err;
2969
2970 vsnprintf(wq->name, namelen, fmt, args1);
2971 va_end(args);
2972 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002973
Tejun Heof3421792010-07-02 10:03:51 +02002974 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02002975 * Workqueues which may be used during memory reclaim should
2976 * have a rescuer to guarantee forward progress.
2977 */
2978 if (flags & WQ_MEM_RECLAIM)
2979 flags |= WQ_RESCUER;
2980
Tejun Heod320c032010-06-29 10:07:14 +02002981 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08002982 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002983
Tejun Heob196be82012-01-10 15:11:35 -08002984 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02002985 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002986 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02002987 mutex_init(&wq->flush_mutex);
2988 atomic_set(&wq->nr_cwqs_to_flush, 0);
2989 INIT_LIST_HEAD(&wq->flusher_queue);
2990 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002991
Johannes Bergeb13ba82008-01-16 09:51:58 +01002992 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07002993 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002994
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002995 if (alloc_cwqs(wq) < 0)
2996 goto err;
2997
Tejun Heof3421792010-07-02 10:03:51 +02002998 for_each_cwq_cpu(cpu, wq) {
Tejun Heo15376632010-06-29 10:07:11 +02002999 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003000 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo2bfedd32012-07-13 22:16:45 -07003001 int pool_idx = (bool)(flags & WQ_HIGHPRI);
Tejun Heo15376632010-06-29 10:07:11 +02003002
Tejun Heo0f900042010-06-29 10:07:11 +02003003 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heo2bfedd32012-07-13 22:16:45 -07003004 cwq->pool = &gcwq->pools[pool_idx];
Tejun Heoc34056a2010-06-29 10:07:11 +02003005 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02003006 cwq->flush_color = -1;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003007 cwq->max_active = max_active;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003008 INIT_LIST_HEAD(&cwq->delayed_works);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003009 }
3010
Tejun Heoe22bee72010-06-29 10:07:14 +02003011 if (flags & WQ_RESCUER) {
3012 struct worker *rescuer;
3013
Tejun Heof2e005a2010-07-20 15:59:09 +02003014 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
Tejun Heoe22bee72010-06-29 10:07:14 +02003015 goto err;
3016
3017 wq->rescuer = rescuer = alloc_worker();
3018 if (!rescuer)
3019 goto err;
3020
Tejun Heob196be82012-01-10 15:11:35 -08003021 rescuer->task = kthread_create(rescuer_thread, wq, "%s",
3022 wq->name);
Tejun Heoe22bee72010-06-29 10:07:14 +02003023 if (IS_ERR(rescuer->task))
3024 goto err;
3025
Tejun Heoe22bee72010-06-29 10:07:14 +02003026 rescuer->task->flags |= PF_THREAD_BOUND;
3027 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003028 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003029
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003030 /*
3031 * workqueue_lock protects global freeze state and workqueues
3032 * list. Grab it, set max_active accordingly and add the new
3033 * workqueue to workqueues list.
3034 */
Tejun Heo15376632010-06-29 10:07:11 +02003035 spin_lock(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003036
Tejun Heo58a69cb2011-02-16 09:25:31 +01003037 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heof3421792010-07-02 10:03:51 +02003038 for_each_cwq_cpu(cpu, wq)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003039 get_cwq(cpu, wq)->max_active = 0;
3040
Tejun Heo15376632010-06-29 10:07:11 +02003041 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003042
Tejun Heo15376632010-06-29 10:07:11 +02003043 spin_unlock(&workqueue_lock);
3044
Oleg Nesterov3af244332007-05-09 02:34:09 -07003045 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02003046err:
3047 if (wq) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003048 free_cwqs(wq);
Tejun Heof2e005a2010-07-20 15:59:09 +02003049 free_mayday_mask(wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02003050 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003051 kfree(wq);
3052 }
3053 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003054}
Tejun Heod320c032010-06-29 10:07:14 +02003055EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003056
3057/**
3058 * destroy_workqueue - safely terminate a workqueue
3059 * @wq: target workqueue
3060 *
3061 * Safely destroy a workqueue. All work currently pending will be done first.
3062 */
3063void destroy_workqueue(struct workqueue_struct *wq)
3064{
Tejun Heoc8e55f32010-06-29 10:07:12 +02003065 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003066
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003067 /* drain it before proceeding with destruction */
3068 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003069
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003070 /*
3071 * wq list is used to freeze wq, remove from list after
3072 * flushing is complete in case freeze races us.
3073 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003074 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07003075 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003076 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003077
Tejun Heoe22bee72010-06-29 10:07:14 +02003078 /* sanity check */
Tejun Heof3421792010-07-02 10:03:51 +02003079 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02003080 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3081 int i;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003082
Tejun Heo73f53c42010-06-29 10:07:11 +02003083 for (i = 0; i < WORK_NR_COLORS; i++)
3084 BUG_ON(cwq->nr_in_flight[i]);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003085 BUG_ON(cwq->nr_active);
3086 BUG_ON(!list_empty(&cwq->delayed_works));
Tejun Heo73f53c42010-06-29 10:07:11 +02003087 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003088
Tejun Heoe22bee72010-06-29 10:07:14 +02003089 if (wq->flags & WQ_RESCUER) {
3090 kthread_stop(wq->rescuer->task);
Tejun Heof2e005a2010-07-20 15:59:09 +02003091 free_mayday_mask(wq->mayday_mask);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003092 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003093 }
3094
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003095 free_cwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003096 kfree(wq);
3097}
3098EXPORT_SYMBOL_GPL(destroy_workqueue);
3099
Tejun Heodcd989c2010-06-29 10:07:14 +02003100/**
3101 * workqueue_set_max_active - adjust max_active of a workqueue
3102 * @wq: target workqueue
3103 * @max_active: new max_active value.
3104 *
3105 * Set max_active of @wq to @max_active.
3106 *
3107 * CONTEXT:
3108 * Don't call from IRQ context.
3109 */
3110void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3111{
3112 unsigned int cpu;
3113
Tejun Heof3421792010-07-02 10:03:51 +02003114 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003115
3116 spin_lock(&workqueue_lock);
3117
3118 wq->saved_max_active = max_active;
3119
Tejun Heof3421792010-07-02 10:03:51 +02003120 for_each_cwq_cpu(cpu, wq) {
Tejun Heodcd989c2010-06-29 10:07:14 +02003121 struct global_cwq *gcwq = get_gcwq(cpu);
3122
3123 spin_lock_irq(&gcwq->lock);
3124
Tejun Heo58a69cb2011-02-16 09:25:31 +01003125 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heodcd989c2010-06-29 10:07:14 +02003126 !(gcwq->flags & GCWQ_FREEZING))
3127 get_cwq(gcwq->cpu, wq)->max_active = max_active;
3128
3129 spin_unlock_irq(&gcwq->lock);
3130 }
3131
3132 spin_unlock(&workqueue_lock);
3133}
3134EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3135
3136/**
3137 * workqueue_congested - test whether a workqueue is congested
3138 * @cpu: CPU in question
3139 * @wq: target workqueue
3140 *
3141 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3142 * no synchronization around this function and the test result is
3143 * unreliable and only useful as advisory hints or for debugging.
3144 *
3145 * RETURNS:
3146 * %true if congested, %false otherwise.
3147 */
3148bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3149{
3150 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3151
3152 return !list_empty(&cwq->delayed_works);
3153}
3154EXPORT_SYMBOL_GPL(workqueue_congested);
3155
3156/**
3157 * work_cpu - return the last known associated cpu for @work
3158 * @work: the work of interest
3159 *
3160 * RETURNS:
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003161 * CPU number if @work was ever queued. WORK_CPU_NONE otherwise.
Tejun Heodcd989c2010-06-29 10:07:14 +02003162 */
3163unsigned int work_cpu(struct work_struct *work)
3164{
3165 struct global_cwq *gcwq = get_work_gcwq(work);
3166
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003167 return gcwq ? gcwq->cpu : WORK_CPU_NONE;
Tejun Heodcd989c2010-06-29 10:07:14 +02003168}
3169EXPORT_SYMBOL_GPL(work_cpu);
3170
3171/**
3172 * work_busy - test whether a work is currently pending or running
3173 * @work: the work to be tested
3174 *
3175 * Test whether @work is currently pending or running. There is no
3176 * synchronization around this function and the test result is
3177 * unreliable and only useful as advisory hints or for debugging.
3178 * Especially for reentrant wqs, the pending state might hide the
3179 * running state.
3180 *
3181 * RETURNS:
3182 * OR'd bitmask of WORK_BUSY_* bits.
3183 */
3184unsigned int work_busy(struct work_struct *work)
3185{
3186 struct global_cwq *gcwq = get_work_gcwq(work);
3187 unsigned long flags;
3188 unsigned int ret = 0;
3189
3190 if (!gcwq)
3191 return false;
3192
3193 spin_lock_irqsave(&gcwq->lock, flags);
3194
3195 if (work_pending(work))
3196 ret |= WORK_BUSY_PENDING;
3197 if (find_worker_executing_work(gcwq, work))
3198 ret |= WORK_BUSY_RUNNING;
3199
3200 spin_unlock_irqrestore(&gcwq->lock, flags);
3201
3202 return ret;
3203}
3204EXPORT_SYMBOL_GPL(work_busy);
3205
Tejun Heodb7bccf2010-06-29 10:07:12 +02003206/*
3207 * CPU hotplug.
3208 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003209 * There are two challenges in supporting CPU hotplug. Firstly, there
3210 * are a lot of assumptions on strong associations among work, cwq and
3211 * gcwq which make migrating pending and scheduled works very
3212 * difficult to implement without impacting hot paths. Secondly,
3213 * gcwqs serve mix of short, long and very long running works making
3214 * blocked draining impractical.
3215 *
3216 * This is solved by allowing a gcwq to be detached from CPU, running
3217 * it with unbound (rogue) workers and allowing it to be reattached
3218 * later if the cpu comes back online. A separate thread is created
3219 * to govern a gcwq in such state and is called the trustee of the
3220 * gcwq.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003221 *
3222 * Trustee states and their descriptions.
3223 *
3224 * START Command state used on startup. On CPU_DOWN_PREPARE, a
3225 * new trustee is started with this state.
3226 *
3227 * IN_CHARGE Once started, trustee will enter this state after
Tejun Heoe22bee72010-06-29 10:07:14 +02003228 * assuming the manager role and making all existing
3229 * workers rogue. DOWN_PREPARE waits for trustee to
3230 * enter this state. After reaching IN_CHARGE, trustee
3231 * tries to execute the pending worklist until it's empty
3232 * and the state is set to BUTCHER, or the state is set
3233 * to RELEASE.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003234 *
3235 * BUTCHER Command state which is set by the cpu callback after
3236 * the cpu has went down. Once this state is set trustee
3237 * knows that there will be no new works on the worklist
3238 * and once the worklist is empty it can proceed to
3239 * killing idle workers.
3240 *
3241 * RELEASE Command state which is set by the cpu callback if the
3242 * cpu down has been canceled or it has come online
3243 * again. After recognizing this state, trustee stops
Tejun Heoe22bee72010-06-29 10:07:14 +02003244 * trying to drain or butcher and clears ROGUE, rebinds
3245 * all remaining workers back to the cpu and releases
3246 * manager role.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003247 *
3248 * DONE Trustee will enter this state after BUTCHER or RELEASE
3249 * is complete.
3250 *
3251 * trustee CPU draining
3252 * took over down complete
3253 * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE
3254 * | | ^
3255 * | CPU is back online v return workers |
3256 * ----------------> RELEASE --------------
3257 */
3258
3259/**
3260 * trustee_wait_event_timeout - timed event wait for trustee
3261 * @cond: condition to wait for
3262 * @timeout: timeout in jiffies
3263 *
3264 * wait_event_timeout() for trustee to use. Handles locking and
3265 * checks for RELEASE request.
3266 *
3267 * CONTEXT:
3268 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3269 * multiple times. To be used by trustee.
3270 *
3271 * RETURNS:
3272 * Positive indicating left time if @cond is satisfied, 0 if timed
3273 * out, -1 if canceled.
3274 */
3275#define trustee_wait_event_timeout(cond, timeout) ({ \
3276 long __ret = (timeout); \
3277 while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) && \
3278 __ret) { \
3279 spin_unlock_irq(&gcwq->lock); \
3280 __wait_event_timeout(gcwq->trustee_wait, (cond) || \
3281 (gcwq->trustee_state == TRUSTEE_RELEASE), \
3282 __ret); \
3283 spin_lock_irq(&gcwq->lock); \
3284 } \
3285 gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret); \
3286})
3287
3288/**
3289 * trustee_wait_event - event wait for trustee
3290 * @cond: condition to wait for
3291 *
3292 * wait_event() for trustee to use. Automatically handles locking and
3293 * checks for CANCEL request.
3294 *
3295 * CONTEXT:
3296 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3297 * multiple times. To be used by trustee.
3298 *
3299 * RETURNS:
3300 * 0 if @cond is satisfied, -1 if canceled.
3301 */
3302#define trustee_wait_event(cond) ({ \
3303 long __ret1; \
3304 __ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\
3305 __ret1 < 0 ? -1 : 0; \
3306})
3307
Tejun Heo0290d202012-07-13 22:16:44 -07003308static bool gcwq_is_managing_workers(struct global_cwq *gcwq)
3309{
3310 struct worker_pool *pool;
3311
3312 for_each_worker_pool(pool, gcwq)
3313 if (pool->flags & POOL_MANAGING_WORKERS)
3314 return true;
3315 return false;
3316}
3317
3318static bool gcwq_has_idle_workers(struct global_cwq *gcwq)
3319{
3320 struct worker_pool *pool;
3321
3322 for_each_worker_pool(pool, gcwq)
3323 if (!list_empty(&pool->idle_list))
3324 return true;
3325 return false;
3326}
3327
Tejun Heodb7bccf2010-06-29 10:07:12 +02003328static int __cpuinit trustee_thread(void *__gcwq)
3329{
3330 struct global_cwq *gcwq = __gcwq;
Tejun Heo0290d202012-07-13 22:16:44 -07003331 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003332 struct worker *worker;
Tejun Heoe22bee72010-06-29 10:07:14 +02003333 struct work_struct *work;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003334 struct hlist_node *pos;
Tejun Heoe22bee72010-06-29 10:07:14 +02003335 long rc;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003336 int i;
3337
3338 BUG_ON(gcwq->cpu != smp_processor_id());
3339
3340 spin_lock_irq(&gcwq->lock);
3341 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003342 * Claim the manager position and make all workers rogue.
3343 * Trustee must be bound to the target cpu and can't be
3344 * cancelled.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003345 */
3346 BUG_ON(gcwq->cpu != smp_processor_id());
Tejun Heo0290d202012-07-13 22:16:44 -07003347 rc = trustee_wait_event(!gcwq_is_managing_workers(gcwq));
Tejun Heoe22bee72010-06-29 10:07:14 +02003348 BUG_ON(rc < 0);
3349
Tejun Heo0290d202012-07-13 22:16:44 -07003350 for_each_worker_pool(pool, gcwq) {
3351 pool->flags |= POOL_MANAGING_WORKERS;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003352
Tejun Heo0290d202012-07-13 22:16:44 -07003353 list_for_each_entry(worker, &pool->idle_list, entry)
3354 worker->flags |= WORKER_ROGUE;
3355 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003356
3357 for_each_busy_worker(worker, i, pos, gcwq)
Tejun Heocb444762010-07-02 10:03:50 +02003358 worker->flags |= WORKER_ROGUE;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003359
3360 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003361 * Call schedule() so that we cross rq->lock and thus can
3362 * guarantee sched callbacks see the rogue flag. This is
3363 * necessary as scheduler callbacks may be invoked from other
3364 * cpus.
3365 */
3366 spin_unlock_irq(&gcwq->lock);
3367 schedule();
3368 spin_lock_irq(&gcwq->lock);
3369
3370 /*
Tejun Heocb444762010-07-02 10:03:50 +02003371 * Sched callbacks are disabled now. Zap nr_running. After
3372 * this, nr_running stays zero and need_more_worker() and
3373 * keep_working() are always true as long as the worklist is
3374 * not empty.
Tejun Heoe22bee72010-06-29 10:07:14 +02003375 */
Tejun Heo0290d202012-07-13 22:16:44 -07003376 for_each_worker_pool(pool, gcwq)
3377 atomic_set(get_pool_nr_running(pool), 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003378
3379 spin_unlock_irq(&gcwq->lock);
Tejun Heo0290d202012-07-13 22:16:44 -07003380 for_each_worker_pool(pool, gcwq)
3381 del_timer_sync(&pool->idle_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003382 spin_lock_irq(&gcwq->lock);
3383
3384 /*
Tejun Heodb7bccf2010-06-29 10:07:12 +02003385 * We're now in charge. Notify and proceed to drain. We need
3386 * to keep the gcwq running during the whole CPU down
3387 * procedure as other cpu hotunplug callbacks may need to
3388 * flush currently running tasks.
3389 */
3390 gcwq->trustee_state = TRUSTEE_IN_CHARGE;
3391 wake_up_all(&gcwq->trustee_wait);
3392
3393 /*
3394 * The original cpu is in the process of dying and may go away
3395 * anytime now. When that happens, we and all workers would
Tejun Heoe22bee72010-06-29 10:07:14 +02003396 * be migrated to other cpus. Try draining any left work. We
3397 * want to get it over with ASAP - spam rescuers, wake up as
3398 * many idlers as necessary and create new ones till the
3399 * worklist is empty. Note that if the gcwq is frozen, there
Tejun Heo58a69cb2011-02-16 09:25:31 +01003400 * may be frozen works in freezable cwqs. Don't declare
Tejun Heoe22bee72010-06-29 10:07:14 +02003401 * completion while frozen.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003402 */
Tejun Heo0290d202012-07-13 22:16:44 -07003403 while (true) {
3404 bool busy = false;
Tejun Heoe22bee72010-06-29 10:07:14 +02003405
Tejun Heo0290d202012-07-13 22:16:44 -07003406 for_each_worker_pool(pool, gcwq)
3407 busy |= pool->nr_workers != pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +02003408
Tejun Heo0290d202012-07-13 22:16:44 -07003409 if (!busy && !(gcwq->flags & GCWQ_FREEZING) &&
3410 gcwq->trustee_state != TRUSTEE_IN_CHARGE)
3411 break;
Tejun Heoe22bee72010-06-29 10:07:14 +02003412
Tejun Heo0290d202012-07-13 22:16:44 -07003413 for_each_worker_pool(pool, gcwq) {
3414 int nr_works = 0;
3415
3416 list_for_each_entry(work, &pool->worklist, entry) {
3417 send_mayday(work);
3418 nr_works++;
3419 }
3420
3421 list_for_each_entry(worker, &pool->idle_list, entry) {
3422 if (!nr_works--)
3423 break;
3424 wake_up_process(worker->task);
3425 }
3426
3427 if (need_to_create_worker(pool)) {
3428 spin_unlock_irq(&gcwq->lock);
3429 worker = create_worker(pool, false);
3430 spin_lock_irq(&gcwq->lock);
3431 if (worker) {
3432 worker->flags |= WORKER_ROGUE;
3433 start_worker(worker);
3434 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003435 }
3436 }
3437
Tejun Heodb7bccf2010-06-29 10:07:12 +02003438 /* give a breather */
3439 if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN) < 0)
3440 break;
3441 }
3442
Tejun Heoe22bee72010-06-29 10:07:14 +02003443 /*
3444 * Either all works have been scheduled and cpu is down, or
3445 * cpu down has already been canceled. Wait for and butcher
3446 * all workers till we're canceled.
3447 */
3448 do {
Tejun Heo0290d202012-07-13 22:16:44 -07003449 rc = trustee_wait_event(gcwq_has_idle_workers(gcwq));
3450
3451 i = 0;
3452 for_each_worker_pool(pool, gcwq) {
3453 while (!list_empty(&pool->idle_list)) {
3454 worker = list_first_entry(&pool->idle_list,
3455 struct worker, entry);
3456 destroy_worker(worker);
3457 }
3458 i |= pool->nr_workers;
3459 }
3460 } while (i && rc >= 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003461
3462 /*
3463 * At this point, either draining has completed and no worker
3464 * is left, or cpu down has been canceled or the cpu is being
3465 * brought back up. There shouldn't be any idle one left.
3466 * Tell the remaining busy ones to rebind once it finishes the
3467 * currently scheduled works by scheduling the rebind_work.
3468 */
Tejun Heo0290d202012-07-13 22:16:44 -07003469 for_each_worker_pool(pool, gcwq)
3470 WARN_ON(!list_empty(&pool->idle_list));
Tejun Heoe22bee72010-06-29 10:07:14 +02003471
3472 for_each_busy_worker(worker, i, pos, gcwq) {
3473 struct work_struct *rebind_work = &worker->rebind_work;
3474
3475 /*
3476 * Rebind_work may race with future cpu hotplug
3477 * operations. Use a separate flag to mark that
3478 * rebinding is scheduled.
3479 */
Tejun Heocb444762010-07-02 10:03:50 +02003480 worker->flags |= WORKER_REBIND;
3481 worker->flags &= ~WORKER_ROGUE;
Tejun Heoe22bee72010-06-29 10:07:14 +02003482
3483 /* queue rebind_work, wq doesn't matter, use the default one */
3484 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
3485 work_data_bits(rebind_work)))
3486 continue;
3487
3488 debug_work_activate(rebind_work);
Tejun Heod320c032010-06-29 10:07:14 +02003489 insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work,
Tejun Heoe22bee72010-06-29 10:07:14 +02003490 worker->scheduled.next,
3491 work_color_to_flags(WORK_NO_COLOR));
3492 }
3493
3494 /* relinquish manager role */
Tejun Heo0290d202012-07-13 22:16:44 -07003495 for_each_worker_pool(pool, gcwq)
3496 pool->flags &= ~POOL_MANAGING_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02003497
Tejun Heodb7bccf2010-06-29 10:07:12 +02003498 /* notify completion */
3499 gcwq->trustee = NULL;
3500 gcwq->trustee_state = TRUSTEE_DONE;
3501 wake_up_all(&gcwq->trustee_wait);
3502 spin_unlock_irq(&gcwq->lock);
3503 return 0;
3504}
3505
3506/**
3507 * wait_trustee_state - wait for trustee to enter the specified state
3508 * @gcwq: gcwq the trustee of interest belongs to
3509 * @state: target state to wait for
3510 *
3511 * Wait for the trustee to reach @state. DONE is already matched.
3512 *
3513 * CONTEXT:
3514 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3515 * multiple times. To be used by cpu_callback.
3516 */
3517static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09003518__releases(&gcwq->lock)
3519__acquires(&gcwq->lock)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003520{
3521 if (!(gcwq->trustee_state == state ||
3522 gcwq->trustee_state == TRUSTEE_DONE)) {
3523 spin_unlock_irq(&gcwq->lock);
3524 __wait_event(gcwq->trustee_wait,
3525 gcwq->trustee_state == state ||
3526 gcwq->trustee_state == TRUSTEE_DONE);
3527 spin_lock_irq(&gcwq->lock);
3528 }
3529}
3530
Oleg Nesterov3af244332007-05-09 02:34:09 -07003531static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
3532 unsigned long action,
3533 void *hcpu)
3534{
3535 unsigned int cpu = (unsigned long)hcpu;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003536 struct global_cwq *gcwq = get_gcwq(cpu);
3537 struct task_struct *new_trustee = NULL;
Tejun Heo0290d202012-07-13 22:16:44 -07003538 struct worker *new_workers[NR_WORKER_POOLS] = { };
3539 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003540 unsigned long flags;
Tejun Heo0290d202012-07-13 22:16:44 -07003541 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003543 action &= ~CPU_TASKS_FROZEN;
3544
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003546 case CPU_DOWN_PREPARE:
3547 new_trustee = kthread_create(trustee_thread, gcwq,
3548 "workqueue_trustee/%d\n", cpu);
3549 if (IS_ERR(new_trustee))
3550 return notifier_from_errno(PTR_ERR(new_trustee));
3551 kthread_bind(new_trustee, cpu);
Tejun Heoe22bee72010-06-29 10:07:14 +02003552 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 case CPU_UP_PREPARE:
Tejun Heo0290d202012-07-13 22:16:44 -07003554 i = 0;
3555 for_each_worker_pool(pool, gcwq) {
3556 BUG_ON(pool->first_idle);
3557 new_workers[i] = create_worker(pool, false);
3558 if (!new_workers[i++])
3559 goto err_destroy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 }
3562
Tejun Heodb7bccf2010-06-29 10:07:12 +02003563 /* some are called w/ irq disabled, don't disturb irq status */
3564 spin_lock_irqsave(&gcwq->lock, flags);
3565
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003566 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003567 case CPU_DOWN_PREPARE:
3568 /* initialize trustee and tell it to acquire the gcwq */
3569 BUG_ON(gcwq->trustee || gcwq->trustee_state != TRUSTEE_DONE);
3570 gcwq->trustee = new_trustee;
3571 gcwq->trustee_state = TRUSTEE_START;
3572 wake_up_process(gcwq->trustee);
3573 wait_trustee_state(gcwq, TRUSTEE_IN_CHARGE);
Tejun Heoe22bee72010-06-29 10:07:14 +02003574 /* fall through */
3575 case CPU_UP_PREPARE:
Tejun Heo0290d202012-07-13 22:16:44 -07003576 i = 0;
3577 for_each_worker_pool(pool, gcwq) {
3578 BUG_ON(pool->first_idle);
3579 pool->first_idle = new_workers[i++];
3580 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003581 break;
3582
3583 case CPU_DYING:
3584 /*
3585 * Before this, the trustee and all workers except for
3586 * the ones which are still executing works from
3587 * before the last CPU down must be on the cpu. After
3588 * this, they'll all be diasporas.
3589 */
3590 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003591 break;
3592
Oleg Nesterov3da1c842008-07-25 01:47:50 -07003593 case CPU_POST_DEAD:
Tejun Heodb7bccf2010-06-29 10:07:12 +02003594 gcwq->trustee_state = TRUSTEE_BUTCHER;
Tejun Heoe22bee72010-06-29 10:07:14 +02003595 /* fall through */
3596 case CPU_UP_CANCELED:
Tejun Heo0290d202012-07-13 22:16:44 -07003597 for_each_worker_pool(pool, gcwq) {
3598 destroy_worker(pool->first_idle);
3599 pool->first_idle = NULL;
3600 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003601 break;
3602
3603 case CPU_DOWN_FAILED:
3604 case CPU_ONLINE:
Tejun Heoe22bee72010-06-29 10:07:14 +02003605 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003606 if (gcwq->trustee_state != TRUSTEE_DONE) {
3607 gcwq->trustee_state = TRUSTEE_RELEASE;
3608 wake_up_process(gcwq->trustee);
3609 wait_trustee_state(gcwq, TRUSTEE_DONE);
3610 }
3611
Tejun Heoe22bee72010-06-29 10:07:14 +02003612 /*
3613 * Trustee is done and there might be no worker left.
3614 * Put the first_idle in and request a real manager to
3615 * take a look.
3616 */
Tejun Heo0290d202012-07-13 22:16:44 -07003617 for_each_worker_pool(pool, gcwq) {
3618 spin_unlock_irq(&gcwq->lock);
3619 kthread_bind(pool->first_idle->task, cpu);
3620 spin_lock_irq(&gcwq->lock);
3621 pool->flags |= POOL_MANAGE_WORKERS;
3622 start_worker(pool->first_idle);
3623 pool->first_idle = NULL;
3624 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003625 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003626 }
3627
Tejun Heodb7bccf2010-06-29 10:07:12 +02003628 spin_unlock_irqrestore(&gcwq->lock, flags);
3629
Tejun Heo15376632010-06-29 10:07:11 +02003630 return notifier_from_errno(0);
Tejun Heo0290d202012-07-13 22:16:44 -07003631
3632err_destroy:
3633 if (new_trustee)
3634 kthread_stop(new_trustee);
3635
3636 spin_lock_irqsave(&gcwq->lock, flags);
3637 for (i = 0; i < NR_WORKER_POOLS; i++)
3638 if (new_workers[i])
3639 destroy_worker(new_workers[i]);
3640 spin_unlock_irqrestore(&gcwq->lock, flags);
3641
3642 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644
Rusty Russell2d3854a2008-11-05 13:39:10 +11003645#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003646
Rusty Russell2d3854a2008-11-05 13:39:10 +11003647struct work_for_cpu {
Andrew Morton6b440032009-04-09 09:50:37 -06003648 struct completion completion;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003649 long (*fn)(void *);
3650 void *arg;
3651 long ret;
3652};
3653
Andrew Morton6b440032009-04-09 09:50:37 -06003654static int do_work_for_cpu(void *_wfc)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003655{
Andrew Morton6b440032009-04-09 09:50:37 -06003656 struct work_for_cpu *wfc = _wfc;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003657 wfc->ret = wfc->fn(wfc->arg);
Andrew Morton6b440032009-04-09 09:50:37 -06003658 complete(&wfc->completion);
3659 return 0;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003660}
3661
3662/**
3663 * work_on_cpu - run a function in user context on a particular cpu
3664 * @cpu: the cpu to run on
3665 * @fn: the function to run
3666 * @arg: the function arg
3667 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003668 * This will return the value @fn returns.
3669 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06003670 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003671 */
3672long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3673{
Andrew Morton6b440032009-04-09 09:50:37 -06003674 struct task_struct *sub_thread;
3675 struct work_for_cpu wfc = {
3676 .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
3677 .fn = fn,
3678 .arg = arg,
3679 };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003680
Andrew Morton6b440032009-04-09 09:50:37 -06003681 sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
3682 if (IS_ERR(sub_thread))
3683 return PTR_ERR(sub_thread);
3684 kthread_bind(sub_thread, cpu);
3685 wake_up_process(sub_thread);
3686 wait_for_completion(&wfc.completion);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003687 return wfc.ret;
3688}
3689EXPORT_SYMBOL_GPL(work_on_cpu);
3690#endif /* CONFIG_SMP */
3691
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003692#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303693
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003694/**
3695 * freeze_workqueues_begin - begin freezing workqueues
3696 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003697 * Start freezing workqueues. After this function returns, all freezable
3698 * workqueues will queue new works to their frozen_works list instead of
3699 * gcwq->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003700 *
3701 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003702 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003703 */
3704void freeze_workqueues_begin(void)
3705{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003706 unsigned int cpu;
3707
3708 spin_lock(&workqueue_lock);
3709
3710 BUG_ON(workqueue_freezing);
3711 workqueue_freezing = true;
3712
Tejun Heof3421792010-07-02 10:03:51 +02003713 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003714 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003715 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003716
3717 spin_lock_irq(&gcwq->lock);
3718
Tejun Heodb7bccf2010-06-29 10:07:12 +02003719 BUG_ON(gcwq->flags & GCWQ_FREEZING);
3720 gcwq->flags |= GCWQ_FREEZING;
3721
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003722 list_for_each_entry(wq, &workqueues, list) {
3723 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3724
Tejun Heo58a69cb2011-02-16 09:25:31 +01003725 if (cwq && wq->flags & WQ_FREEZABLE)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003726 cwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003727 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003728
3729 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003730 }
3731
3732 spin_unlock(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003734
3735/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003736 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003737 *
3738 * Check whether freezing is complete. This function must be called
3739 * between freeze_workqueues_begin() and thaw_workqueues().
3740 *
3741 * CONTEXT:
3742 * Grabs and releases workqueue_lock.
3743 *
3744 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01003745 * %true if some freezable workqueues are still busy. %false if freezing
3746 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003747 */
3748bool freeze_workqueues_busy(void)
3749{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003750 unsigned int cpu;
3751 bool busy = false;
3752
3753 spin_lock(&workqueue_lock);
3754
3755 BUG_ON(!workqueue_freezing);
3756
Tejun Heof3421792010-07-02 10:03:51 +02003757 for_each_gcwq_cpu(cpu) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003758 struct workqueue_struct *wq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003759 /*
3760 * nr_active is monotonically decreasing. It's safe
3761 * to peek without lock.
3762 */
3763 list_for_each_entry(wq, &workqueues, list) {
3764 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3765
Tejun Heo58a69cb2011-02-16 09:25:31 +01003766 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003767 continue;
3768
3769 BUG_ON(cwq->nr_active < 0);
3770 if (cwq->nr_active) {
3771 busy = true;
3772 goto out_unlock;
3773 }
3774 }
3775 }
3776out_unlock:
3777 spin_unlock(&workqueue_lock);
3778 return busy;
3779}
3780
3781/**
3782 * thaw_workqueues - thaw workqueues
3783 *
3784 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo7e116292010-06-29 10:07:13 +02003785 * frozen works are transferred to their respective gcwq worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003786 *
3787 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003788 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003789 */
3790void thaw_workqueues(void)
3791{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003792 unsigned int cpu;
3793
3794 spin_lock(&workqueue_lock);
3795
3796 if (!workqueue_freezing)
3797 goto out_unlock;
3798
Tejun Heof3421792010-07-02 10:03:51 +02003799 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003800 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo0290d202012-07-13 22:16:44 -07003801 struct worker_pool *pool;
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003802 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003803
3804 spin_lock_irq(&gcwq->lock);
3805
Tejun Heodb7bccf2010-06-29 10:07:12 +02003806 BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
3807 gcwq->flags &= ~GCWQ_FREEZING;
3808
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003809 list_for_each_entry(wq, &workqueues, list) {
3810 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3811
Tejun Heo58a69cb2011-02-16 09:25:31 +01003812 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003813 continue;
3814
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003815 /* restore max_active and repopulate worklist */
3816 cwq->max_active = wq->saved_max_active;
3817
3818 while (!list_empty(&cwq->delayed_works) &&
3819 cwq->nr_active < cwq->max_active)
3820 cwq_activate_first_delayed(cwq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003821 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003822
Tejun Heo0290d202012-07-13 22:16:44 -07003823 for_each_worker_pool(pool, gcwq)
3824 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02003825
Tejun Heo8b03ae32010-06-29 10:07:12 +02003826 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003827 }
3828
3829 workqueue_freezing = false;
3830out_unlock:
3831 spin_unlock(&workqueue_lock);
3832}
3833#endif /* CONFIG_FREEZER */
3834
Suresh Siddha6ee05782010-07-30 14:57:37 -07003835static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836{
Tejun Heoc34056a2010-06-29 10:07:11 +02003837 unsigned int cpu;
Tejun Heoc8e55f32010-06-29 10:07:12 +02003838 int i;
Tejun Heoc34056a2010-06-29 10:07:11 +02003839
Tejun Heof6500942010-08-09 11:50:34 +02003840 cpu_notifier(workqueue_cpu_callback, CPU_PRI_WORKQUEUE);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003841
3842 /* initialize gcwqs */
Tejun Heof3421792010-07-02 10:03:51 +02003843 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003844 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo0290d202012-07-13 22:16:44 -07003845 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003846
3847 spin_lock_init(&gcwq->lock);
3848 gcwq->cpu = cpu;
Tejun Heo477a3c32010-08-31 10:54:35 +02003849 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003850
Tejun Heoc8e55f32010-06-29 10:07:12 +02003851 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
3852 INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
3853
Tejun Heo0290d202012-07-13 22:16:44 -07003854 for_each_worker_pool(pool, gcwq) {
3855 pool->gcwq = gcwq;
3856 INIT_LIST_HEAD(&pool->worklist);
3857 INIT_LIST_HEAD(&pool->idle_list);
Tejun Heoe22bee72010-06-29 10:07:14 +02003858
Tejun Heo0290d202012-07-13 22:16:44 -07003859 init_timer_deferrable(&pool->idle_timer);
3860 pool->idle_timer.function = idle_worker_timeout;
3861 pool->idle_timer.data = (unsigned long)pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003862
Tejun Heo0290d202012-07-13 22:16:44 -07003863 setup_timer(&pool->mayday_timer, gcwq_mayday_timeout,
3864 (unsigned long)pool);
3865
3866 ida_init(&pool->worker_ida);
3867 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003868
3869 gcwq->trustee_state = TRUSTEE_DONE;
3870 init_waitqueue_head(&gcwq->trustee_wait);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003871 }
3872
Tejun Heoe22bee72010-06-29 10:07:14 +02003873 /* create the initial worker */
Tejun Heof3421792010-07-02 10:03:51 +02003874 for_each_online_gcwq_cpu(cpu) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003875 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo0290d202012-07-13 22:16:44 -07003876 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003877
Tejun Heo477a3c32010-08-31 10:54:35 +02003878 if (cpu != WORK_CPU_UNBOUND)
3879 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heo0290d202012-07-13 22:16:44 -07003880
3881 for_each_worker_pool(pool, gcwq) {
3882 struct worker *worker;
3883
3884 worker = create_worker(pool, true);
3885 BUG_ON(!worker);
3886 spin_lock_irq(&gcwq->lock);
3887 start_worker(worker);
3888 spin_unlock_irq(&gcwq->lock);
3889 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003890 }
3891
Tejun Heod320c032010-06-29 10:07:14 +02003892 system_wq = alloc_workqueue("events", 0, 0);
3893 system_long_wq = alloc_workqueue("events_long", 0, 0);
3894 system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0);
Tejun Heof3421792010-07-02 10:03:51 +02003895 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3896 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01003897 system_freezable_wq = alloc_workqueue("events_freezable",
3898 WQ_FREEZABLE, 0);
Alan Stern62d3c542012-03-02 10:51:00 +01003899 system_nrt_freezable_wq = alloc_workqueue("events_nrt_freezable",
3900 WQ_NON_REENTRANT | WQ_FREEZABLE, 0);
Hitoshi Mitakee5cba242010-11-26 12:06:44 +01003901 BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq ||
Alan Stern62d3c542012-03-02 10:51:00 +01003902 !system_unbound_wq || !system_freezable_wq ||
3903 !system_nrt_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07003904 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905}
Suresh Siddha6ee05782010-07-30 14:57:37 -07003906early_initcall(init_workqueues);