blob: e1d05e51a80adcec89f51c6f68f7823e1a7fdd29 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tejun Heoc54fce62010-09-10 16:51:36 +02002 * kernel/workqueue.c - generic async execution with shared worker pool
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Tejun Heoc54fce62010-09-10 16:51:36 +02004 * Copyright (C) 2002 Ingo Molnar
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Tejun Heoc54fce62010-09-10 16:51:36 +02006 * Derived from the taskqueue/keventd code by:
7 * David Woodhouse <dwmw2@infradead.org>
8 * Andrew Morton
9 * Kai Petzke <wpp@marie.physik.tu-berlin.de>
10 * Theodore Ts'o <tytso@mit.edu>
Christoph Lameter89ada672005-10-30 15:01:59 -080011 *
Christoph Lametercde53532008-07-04 09:59:22 -070012 * Made to use alloc_percpu by Christoph Lameter.
Tejun Heoc54fce62010-09-10 16:51:36 +020013 *
14 * Copyright (C) 2010 SUSE Linux Products GmbH
15 * Copyright (C) 2010 Tejun Heo <tj@kernel.org>
16 *
17 * This is the generic async execution mechanism. Work items as are
18 * executed in process context. The worker pool is shared and
19 * automatically managed. There is one worker pool for each CPU and
20 * one extra for works which are better served by workers which are
21 * not bound to any specific CPU.
22 *
23 * Please read Documentation/workqueue.txt for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 */
25
Paul Gortmaker9984de12011-05-23 14:51:41 -040026#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/kernel.h>
28#include <linux/sched.h>
29#include <linux/init.h>
30#include <linux/signal.h>
31#include <linux/completion.h>
32#include <linux/workqueue.h>
33#include <linux/slab.h>
34#include <linux/cpu.h>
35#include <linux/notifier.h>
36#include <linux/kthread.h>
James Bottomley1fa44ec2006-02-23 12:43:43 -060037#include <linux/hardirq.h>
Christoph Lameter46934022006-10-11 01:21:26 -070038#include <linux/mempolicy.h>
Rafael J. Wysocki341a5952006-12-06 20:34:49 -080039#include <linux/freezer.h>
Peter Zijlstrad5abe662006-12-06 20:37:26 -080040#include <linux/kallsyms.h>
41#include <linux/debug_locks.h>
Johannes Berg4e6045f2007-10-18 23:39:55 -070042#include <linux/lockdep.h>
Tejun Heoc34056a2010-06-29 10:07:11 +020043#include <linux/idr.h>
Tejun Heoe22bee72010-06-29 10:07:14 +020044
45#include "workqueue_sched.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Tejun Heoc8e55f32010-06-29 10:07:12 +020047enum {
Tejun Heobc2ae0f2012-07-17 12:39:27 -070048 /*
49 * global_cwq flags
50 *
51 * A bound gcwq is either associated or disassociated with its CPU.
52 * While associated (!DISASSOCIATED), all workers are bound to the
53 * CPU and none has %WORKER_UNBOUND set and concurrency management
54 * is in effect.
55 *
56 * While DISASSOCIATED, the cpu may be offline and all workers have
57 * %WORKER_UNBOUND set and concurrency management disabled, and may
58 * be executing on any CPU. The gcwq behaves as an unbound one.
59 *
60 * Note that DISASSOCIATED can be flipped only while holding
61 * managership of all pools on the gcwq to avoid changing binding
62 * state while create_worker() is in progress.
63 */
Tejun Heo11ebea52012-07-12 14:46:37 -070064 GCWQ_DISASSOCIATED = 1 << 0, /* cpu can't serve workers */
65 GCWQ_FREEZING = 1 << 1, /* freeze in progress */
66
67 /* pool flags */
68 POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
Tejun Heodb7bccf2010-06-29 10:07:12 +020069
Tejun Heoc8e55f32010-06-29 10:07:12 +020070 /* worker flags */
71 WORKER_STARTED = 1 << 0, /* started */
72 WORKER_DIE = 1 << 1, /* die die die */
73 WORKER_IDLE = 1 << 2, /* is idle */
Tejun Heoe22bee72010-06-29 10:07:14 +020074 WORKER_PREP = 1 << 3, /* preparing to run works */
Tejun Heoe22bee72010-06-29 10:07:14 +020075 WORKER_REBIND = 1 << 5, /* mom is home, come back */
Tejun Heofb0e7be2010-06-29 10:07:15 +020076 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
Tejun Heof3421792010-07-02 10:03:51 +020077 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
Tejun Heoe22bee72010-06-29 10:07:14 +020078
Tejun Heo403c8212012-07-17 12:39:27 -070079 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_REBIND | WORKER_UNBOUND |
80 WORKER_CPU_INTENSIVE,
Tejun Heodb7bccf2010-06-29 10:07:12 +020081
82 /* gcwq->trustee_state */
83 TRUSTEE_START = 0, /* start */
84 TRUSTEE_IN_CHARGE = 1, /* trustee in charge of gcwq */
85 TRUSTEE_BUTCHER = 2, /* butcher workers */
86 TRUSTEE_RELEASE = 3, /* release workers */
87 TRUSTEE_DONE = 4, /* trustee is done */
Tejun Heoc8e55f32010-06-29 10:07:12 +020088
Tejun Heo32704762012-07-13 22:16:45 -070089 NR_WORKER_POOLS = 2, /* # worker pools per gcwq */
Tejun Heo4ce62e92012-07-13 22:16:44 -070090
Tejun Heoc8e55f32010-06-29 10:07:12 +020091 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
92 BUSY_WORKER_HASH_SIZE = 1 << BUSY_WORKER_HASH_ORDER,
93 BUSY_WORKER_HASH_MASK = BUSY_WORKER_HASH_SIZE - 1,
Tejun Heodb7bccf2010-06-29 10:07:12 +020094
Tejun Heoe22bee72010-06-29 10:07:14 +020095 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
96 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
97
Tejun Heo3233cdb2011-02-16 18:10:19 +010098 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
99 /* call for help after 10ms
100 (min two ticks) */
Tejun Heoe22bee72010-06-29 10:07:14 +0200101 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
102 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200103 TRUSTEE_COOLDOWN = HZ / 10, /* for trustee draining */
Tejun Heoe22bee72010-06-29 10:07:14 +0200104
105 /*
106 * Rescue workers are used only on emergencies and shared by
107 * all cpus. Give -20.
108 */
109 RESCUER_NICE_LEVEL = -20,
Tejun Heo32704762012-07-13 22:16:45 -0700110 HIGHPRI_NICE_LEVEL = -20,
Tejun Heoc8e55f32010-06-29 10:07:12 +0200111};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113/*
Tejun Heo4690c4a2010-06-29 10:07:10 +0200114 * Structure fields follow one of the following exclusion rules.
115 *
Tejun Heoe41e7042010-08-24 14:22:47 +0200116 * I: Modifiable by initialization/destruction paths and read-only for
117 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200118 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200119 * P: Preemption protected. Disabling preemption is enough and should
120 * only be modified and accessed from the local cpu.
121 *
Tejun Heo8b03ae32010-06-29 10:07:12 +0200122 * L: gcwq->lock protected. Access with gcwq->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200123 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200124 * X: During normal operation, modification requires gcwq->lock and
125 * should be done only from local cpu. Either disabling preemption
126 * on local cpu or grabbing gcwq->lock is enough for read access.
Tejun Heof3421792010-07-02 10:03:51 +0200127 * If GCWQ_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200128 *
Tejun Heo73f53c42010-06-29 10:07:11 +0200129 * F: wq->flush_mutex protected.
130 *
Tejun Heo4690c4a2010-06-29 10:07:10 +0200131 * W: workqueue_lock protected.
132 */
133
Tejun Heo8b03ae32010-06-29 10:07:12 +0200134struct global_cwq;
Tejun Heobd7bdd42012-07-12 14:46:37 -0700135struct worker_pool;
Tejun Heoc34056a2010-06-29 10:07:11 +0200136
Tejun Heoe22bee72010-06-29 10:07:14 +0200137/*
138 * The poor guys doing the actual heavy lifting. All on-duty workers
139 * are either serving the manager role, on idle list or on busy hash.
140 */
Tejun Heoc34056a2010-06-29 10:07:11 +0200141struct worker {
Tejun Heoc8e55f32010-06-29 10:07:12 +0200142 /* on idle list while idle, on busy hash table while busy */
143 union {
144 struct list_head entry; /* L: while idle */
145 struct hlist_node hentry; /* L: while busy */
146 };
147
Tejun Heoc34056a2010-06-29 10:07:11 +0200148 struct work_struct *current_work; /* L: work being processed */
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200149 struct cpu_workqueue_struct *current_cwq; /* L: current_work's cwq */
Tejun Heoaffee4b2010-06-29 10:07:12 +0200150 struct list_head scheduled; /* L: scheduled works */
Tejun Heoc34056a2010-06-29 10:07:11 +0200151 struct task_struct *task; /* I: worker task */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700152 struct worker_pool *pool; /* I: the associated pool */
Tejun Heoe22bee72010-06-29 10:07:14 +0200153 /* 64 bytes boundary on 64bit, 32 on 32bit */
154 unsigned long last_active; /* L: last active timestamp */
155 unsigned int flags; /* X: flags */
Tejun Heoc34056a2010-06-29 10:07:11 +0200156 int id; /* I: worker id */
Tejun Heoe22bee72010-06-29 10:07:14 +0200157 struct work_struct rebind_work; /* L: rebind worker to cpu */
Tejun Heoc34056a2010-06-29 10:07:11 +0200158};
159
Tejun Heobd7bdd42012-07-12 14:46:37 -0700160struct worker_pool {
161 struct global_cwq *gcwq; /* I: the owning gcwq */
Tejun Heo11ebea52012-07-12 14:46:37 -0700162 unsigned int flags; /* X: flags */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700163
164 struct list_head worklist; /* L: list of pending works */
165 int nr_workers; /* L: total number of workers */
166 int nr_idle; /* L: currently idle ones */
167
168 struct list_head idle_list; /* X: list of idle workers */
169 struct timer_list idle_timer; /* L: worker idle timeout */
170 struct timer_list mayday_timer; /* L: SOS timer for workers */
171
Tejun Heo60373152012-07-17 12:39:27 -0700172 struct mutex manager_mutex; /* mutex manager should hold */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700173 struct ida worker_ida; /* L: for worker IDs */
174 struct worker *first_idle; /* L: first idle worker */
175};
176
Tejun Heo4690c4a2010-06-29 10:07:10 +0200177/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200178 * Global per-cpu workqueue. There's one and only one for each cpu
179 * and all works are queued and processed here regardless of their
180 * target workqueues.
Tejun Heo8b03ae32010-06-29 10:07:12 +0200181 */
182struct global_cwq {
183 spinlock_t lock; /* the gcwq lock */
184 unsigned int cpu; /* I: the associated cpu */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200185 unsigned int flags; /* L: GCWQ_* flags */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200186
Tejun Heobd7bdd42012-07-12 14:46:37 -0700187 /* workers are chained either in busy_hash or pool idle_list */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200188 struct hlist_head busy_hash[BUSY_WORKER_HASH_SIZE];
189 /* L: hash of busy workers */
190
Tejun Heo32704762012-07-13 22:16:45 -0700191 struct worker_pool pools[2]; /* normal and highpri pools */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200192
193 struct task_struct *trustee; /* L: for gcwq shutdown */
194 unsigned int trustee_state; /* L: trustee state */
195 wait_queue_head_t trustee_wait; /* trustee wait */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200196} ____cacheline_aligned_in_smp;
197
198/*
Tejun Heo502ca9d2010-06-29 10:07:13 +0200199 * The per-CPU workqueue. The lower WORK_STRUCT_FLAG_BITS of
Tejun Heo0f900042010-06-29 10:07:11 +0200200 * work_struct->data are used for flags and thus cwqs need to be
201 * aligned at two's power of the number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 */
203struct cpu_workqueue_struct {
Tejun Heobd7bdd42012-07-12 14:46:37 -0700204 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200205 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200206 int work_color; /* L: current color */
207 int flush_color; /* L: flushing color */
208 int nr_in_flight[WORK_NR_COLORS];
209 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200210 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200211 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200212 struct list_head delayed_works; /* L: delayed works */
Tejun Heo0f900042010-06-29 10:07:11 +0200213};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200216 * Structure used to wait for workqueue flush.
217 */
218struct wq_flusher {
219 struct list_head list; /* F: list of flushers */
220 int flush_color; /* F: flush color waiting for */
221 struct completion done; /* flush completion */
222};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Tejun Heo73f53c42010-06-29 10:07:11 +0200224/*
Tejun Heof2e005a2010-07-20 15:59:09 +0200225 * All cpumasks are assumed to be always set on UP and thus can't be
226 * used to determine whether there's something to be done.
227 */
228#ifdef CONFIG_SMP
229typedef cpumask_var_t mayday_mask_t;
230#define mayday_test_and_set_cpu(cpu, mask) \
231 cpumask_test_and_set_cpu((cpu), (mask))
232#define mayday_clear_cpu(cpu, mask) cpumask_clear_cpu((cpu), (mask))
233#define for_each_mayday_cpu(cpu, mask) for_each_cpu((cpu), (mask))
Tejun Heo9c375472010-08-31 11:18:34 +0200234#define alloc_mayday_mask(maskp, gfp) zalloc_cpumask_var((maskp), (gfp))
Tejun Heof2e005a2010-07-20 15:59:09 +0200235#define free_mayday_mask(mask) free_cpumask_var((mask))
236#else
237typedef unsigned long mayday_mask_t;
238#define mayday_test_and_set_cpu(cpu, mask) test_and_set_bit(0, &(mask))
239#define mayday_clear_cpu(cpu, mask) clear_bit(0, &(mask))
240#define for_each_mayday_cpu(cpu, mask) if ((cpu) = 0, (mask))
241#define alloc_mayday_mask(maskp, gfp) true
242#define free_mayday_mask(mask) do { } while (0)
243#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245/*
246 * The externally visible workqueue abstraction is an array of
247 * per-CPU workqueues:
248 */
249struct workqueue_struct {
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200250 unsigned int flags; /* W: WQ_* flags */
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200251 union {
252 struct cpu_workqueue_struct __percpu *pcpu;
253 struct cpu_workqueue_struct *single;
254 unsigned long v;
255 } cpu_wq; /* I: cwq's */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200256 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200257
258 struct mutex flush_mutex; /* protects wq flushing */
259 int work_color; /* F: current work color */
260 int flush_color; /* F: current flush color */
261 atomic_t nr_cwqs_to_flush; /* flush in progress */
262 struct wq_flusher *first_flusher; /* F: first flusher */
263 struct list_head flusher_queue; /* F: flush waiters */
264 struct list_head flusher_overflow; /* F: flush overflow list */
265
Tejun Heof2e005a2010-07-20 15:59:09 +0200266 mayday_mask_t mayday_mask; /* cpus requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200267 struct worker *rescuer; /* I: rescue worker */
268
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200269 int nr_drainers; /* W: drain in progress */
Tejun Heodcd989c2010-06-29 10:07:14 +0200270 int saved_max_active; /* W: saved cwq max_active */
Johannes Berg4e6045f2007-10-18 23:39:55 -0700271#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200272 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700273#endif
Tejun Heob196be82012-01-10 15:11:35 -0800274 char name[]; /* I: workqueue name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275};
276
Tejun Heod320c032010-06-29 10:07:14 +0200277struct workqueue_struct *system_wq __read_mostly;
278struct workqueue_struct *system_long_wq __read_mostly;
279struct workqueue_struct *system_nrt_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200280struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100281struct workqueue_struct *system_freezable_wq __read_mostly;
Alan Stern62d3c542012-03-02 10:51:00 +0100282struct workqueue_struct *system_nrt_freezable_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200283EXPORT_SYMBOL_GPL(system_wq);
284EXPORT_SYMBOL_GPL(system_long_wq);
285EXPORT_SYMBOL_GPL(system_nrt_wq);
Tejun Heof3421792010-07-02 10:03:51 +0200286EXPORT_SYMBOL_GPL(system_unbound_wq);
Tejun Heo24d51ad2011-02-21 09:52:50 +0100287EXPORT_SYMBOL_GPL(system_freezable_wq);
Alan Stern62d3c542012-03-02 10:51:00 +0100288EXPORT_SYMBOL_GPL(system_nrt_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200289
Tejun Heo97bd2342010-10-05 10:41:14 +0200290#define CREATE_TRACE_POINTS
291#include <trace/events/workqueue.h>
292
Tejun Heo4ce62e92012-07-13 22:16:44 -0700293#define for_each_worker_pool(pool, gcwq) \
Tejun Heo32704762012-07-13 22:16:45 -0700294 for ((pool) = &(gcwq)->pools[0]; \
295 (pool) < &(gcwq)->pools[NR_WORKER_POOLS]; (pool)++)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700296
Tejun Heodb7bccf2010-06-29 10:07:12 +0200297#define for_each_busy_worker(worker, i, pos, gcwq) \
298 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \
299 hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry)
300
Tejun Heof3421792010-07-02 10:03:51 +0200301static inline int __next_gcwq_cpu(int cpu, const struct cpumask *mask,
302 unsigned int sw)
303{
304 if (cpu < nr_cpu_ids) {
305 if (sw & 1) {
306 cpu = cpumask_next(cpu, mask);
307 if (cpu < nr_cpu_ids)
308 return cpu;
309 }
310 if (sw & 2)
311 return WORK_CPU_UNBOUND;
312 }
313 return WORK_CPU_NONE;
314}
315
316static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
317 struct workqueue_struct *wq)
318{
319 return __next_gcwq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2);
320}
321
Tejun Heo09884952010-08-01 11:50:12 +0200322/*
323 * CPU iterators
324 *
325 * An extra gcwq is defined for an invalid cpu number
326 * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any
327 * specific CPU. The following iterators are similar to
328 * for_each_*_cpu() iterators but also considers the unbound gcwq.
329 *
330 * for_each_gcwq_cpu() : possible CPUs + WORK_CPU_UNBOUND
331 * for_each_online_gcwq_cpu() : online CPUs + WORK_CPU_UNBOUND
332 * for_each_cwq_cpu() : possible CPUs for bound workqueues,
333 * WORK_CPU_UNBOUND for unbound workqueues
334 */
Tejun Heof3421792010-07-02 10:03:51 +0200335#define for_each_gcwq_cpu(cpu) \
336 for ((cpu) = __next_gcwq_cpu(-1, cpu_possible_mask, 3); \
337 (cpu) < WORK_CPU_NONE; \
338 (cpu) = __next_gcwq_cpu((cpu), cpu_possible_mask, 3))
339
340#define for_each_online_gcwq_cpu(cpu) \
341 for ((cpu) = __next_gcwq_cpu(-1, cpu_online_mask, 3); \
342 (cpu) < WORK_CPU_NONE; \
343 (cpu) = __next_gcwq_cpu((cpu), cpu_online_mask, 3))
344
345#define for_each_cwq_cpu(cpu, wq) \
346 for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, (wq)); \
347 (cpu) < WORK_CPU_NONE; \
348 (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq)))
349
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900350#ifdef CONFIG_DEBUG_OBJECTS_WORK
351
352static struct debug_obj_descr work_debug_descr;
353
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100354static void *work_debug_hint(void *addr)
355{
356 return ((struct work_struct *) addr)->func;
357}
358
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900359/*
360 * fixup_init is called when:
361 * - an active object is initialized
362 */
363static int work_fixup_init(void *addr, enum debug_obj_state state)
364{
365 struct work_struct *work = addr;
366
367 switch (state) {
368 case ODEBUG_STATE_ACTIVE:
369 cancel_work_sync(work);
370 debug_object_init(work, &work_debug_descr);
371 return 1;
372 default:
373 return 0;
374 }
375}
376
377/*
378 * fixup_activate is called when:
379 * - an active object is activated
380 * - an unknown object is activated (might be a statically initialized object)
381 */
382static int work_fixup_activate(void *addr, enum debug_obj_state state)
383{
384 struct work_struct *work = addr;
385
386 switch (state) {
387
388 case ODEBUG_STATE_NOTAVAILABLE:
389 /*
390 * This is not really a fixup. The work struct was
391 * statically initialized. We just make sure that it
392 * is tracked in the object tracker.
393 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200394 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900395 debug_object_init(work, &work_debug_descr);
396 debug_object_activate(work, &work_debug_descr);
397 return 0;
398 }
399 WARN_ON_ONCE(1);
400 return 0;
401
402 case ODEBUG_STATE_ACTIVE:
403 WARN_ON(1);
404
405 default:
406 return 0;
407 }
408}
409
410/*
411 * fixup_free is called when:
412 * - an active object is freed
413 */
414static int work_fixup_free(void *addr, enum debug_obj_state state)
415{
416 struct work_struct *work = addr;
417
418 switch (state) {
419 case ODEBUG_STATE_ACTIVE:
420 cancel_work_sync(work);
421 debug_object_free(work, &work_debug_descr);
422 return 1;
423 default:
424 return 0;
425 }
426}
427
428static struct debug_obj_descr work_debug_descr = {
429 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100430 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900431 .fixup_init = work_fixup_init,
432 .fixup_activate = work_fixup_activate,
433 .fixup_free = work_fixup_free,
434};
435
436static inline void debug_work_activate(struct work_struct *work)
437{
438 debug_object_activate(work, &work_debug_descr);
439}
440
441static inline void debug_work_deactivate(struct work_struct *work)
442{
443 debug_object_deactivate(work, &work_debug_descr);
444}
445
446void __init_work(struct work_struct *work, int onstack)
447{
448 if (onstack)
449 debug_object_init_on_stack(work, &work_debug_descr);
450 else
451 debug_object_init(work, &work_debug_descr);
452}
453EXPORT_SYMBOL_GPL(__init_work);
454
455void destroy_work_on_stack(struct work_struct *work)
456{
457 debug_object_free(work, &work_debug_descr);
458}
459EXPORT_SYMBOL_GPL(destroy_work_on_stack);
460
461#else
462static inline void debug_work_activate(struct work_struct *work) { }
463static inline void debug_work_deactivate(struct work_struct *work) { }
464#endif
465
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100466/* Serializes the accesses to the list of workqueues. */
467static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468static LIST_HEAD(workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200469static bool workqueue_freezing; /* W: have wqs started freezing? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Oleg Nesterov14441962007-05-23 13:57:57 -0700471/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200472 * The almighty global cpu workqueues. nr_running is the only field
473 * which is expected to be used frequently by other cpus via
474 * try_to_wake_up(). Put it in a separate cacheline.
Oleg Nesterov14441962007-05-23 13:57:57 -0700475 */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200476static DEFINE_PER_CPU(struct global_cwq, global_cwq);
Tejun Heo4ce62e92012-07-13 22:16:44 -0700477static DEFINE_PER_CPU_SHARED_ALIGNED(atomic_t, pool_nr_running[NR_WORKER_POOLS]);
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800478
Tejun Heof3421792010-07-02 10:03:51 +0200479/*
480 * Global cpu workqueue and nr_running counter for unbound gcwq. The
481 * gcwq is always online, has GCWQ_DISASSOCIATED set, and all its
482 * workers have WORKER_UNBOUND set.
483 */
484static struct global_cwq unbound_global_cwq;
Tejun Heo4ce62e92012-07-13 22:16:44 -0700485static atomic_t unbound_pool_nr_running[NR_WORKER_POOLS] = {
486 [0 ... NR_WORKER_POOLS - 1] = ATOMIC_INIT(0), /* always 0 */
487};
Tejun Heof3421792010-07-02 10:03:51 +0200488
Tejun Heoc34056a2010-06-29 10:07:11 +0200489static int worker_thread(void *__worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Tejun Heo32704762012-07-13 22:16:45 -0700491static int worker_pool_pri(struct worker_pool *pool)
492{
493 return pool - pool->gcwq->pools;
494}
495
Tejun Heo8b03ae32010-06-29 10:07:12 +0200496static struct global_cwq *get_gcwq(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Tejun Heof3421792010-07-02 10:03:51 +0200498 if (cpu != WORK_CPU_UNBOUND)
499 return &per_cpu(global_cwq, cpu);
500 else
501 return &unbound_global_cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
Tejun Heo63d95a92012-07-12 14:46:37 -0700504static atomic_t *get_pool_nr_running(struct worker_pool *pool)
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700505{
Tejun Heo63d95a92012-07-12 14:46:37 -0700506 int cpu = pool->gcwq->cpu;
Tejun Heo32704762012-07-13 22:16:45 -0700507 int idx = worker_pool_pri(pool);
Tejun Heo63d95a92012-07-12 14:46:37 -0700508
Tejun Heof3421792010-07-02 10:03:51 +0200509 if (cpu != WORK_CPU_UNBOUND)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700510 return &per_cpu(pool_nr_running, cpu)[idx];
Tejun Heof3421792010-07-02 10:03:51 +0200511 else
Tejun Heo4ce62e92012-07-13 22:16:44 -0700512 return &unbound_pool_nr_running[idx];
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700513}
514
Tejun Heo4690c4a2010-06-29 10:07:10 +0200515static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
516 struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700517{
Tejun Heof3421792010-07-02 10:03:51 +0200518 if (!(wq->flags & WQ_UNBOUND)) {
Lai Jiangshane06ffa12012-03-09 18:03:20 +0800519 if (likely(cpu < nr_cpu_ids))
Tejun Heof3421792010-07-02 10:03:51 +0200520 return per_cpu_ptr(wq->cpu_wq.pcpu, cpu);
Tejun Heof3421792010-07-02 10:03:51 +0200521 } else if (likely(cpu == WORK_CPU_UNBOUND))
522 return wq->cpu_wq.single;
523 return NULL;
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700524}
525
Tejun Heo73f53c42010-06-29 10:07:11 +0200526static unsigned int work_color_to_flags(int color)
527{
528 return color << WORK_STRUCT_COLOR_SHIFT;
529}
530
531static int get_work_color(struct work_struct *work)
532{
533 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
534 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
535}
536
537static int work_next_color(int color)
538{
539 return (color + 1) % WORK_NR_COLORS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
David Howells4594bf12006-12-07 11:33:26 +0000542/*
Tejun Heoe1201532010-07-22 14:14:25 +0200543 * A work's data points to the cwq with WORK_STRUCT_CWQ set while the
544 * work is on queue. Once execution starts, WORK_STRUCT_CWQ is
545 * cleared and the work data contains the cpu number it was last on.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200546 *
547 * set_work_{cwq|cpu}() and clear_work_data() can be used to set the
548 * cwq, cpu or clear work->data. These functions should only be
549 * called while the work is owned - ie. while the PENDING bit is set.
550 *
551 * get_work_[g]cwq() can be used to obtain the gcwq or cwq
552 * corresponding to a work. gcwq is available once the work has been
553 * queued anywhere after initialization. cwq is available only from
554 * queueing until execution starts.
David Howells4594bf12006-12-07 11:33:26 +0000555 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200556static inline void set_work_data(struct work_struct *work, unsigned long data,
557 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000558{
David Howells4594bf12006-12-07 11:33:26 +0000559 BUG_ON(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200560 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000561}
David Howells365970a2006-11-22 14:54:49 +0000562
Tejun Heo7a22ad72010-06-29 10:07:13 +0200563static void set_work_cwq(struct work_struct *work,
564 struct cpu_workqueue_struct *cwq,
565 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200566{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200567 set_work_data(work, (unsigned long)cwq,
Tejun Heoe1201532010-07-22 14:14:25 +0200568 WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200569}
570
Tejun Heo7a22ad72010-06-29 10:07:13 +0200571static void set_work_cpu(struct work_struct *work, unsigned int cpu)
David Howells365970a2006-11-22 14:54:49 +0000572{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200573 set_work_data(work, cpu << WORK_STRUCT_FLAG_BITS, WORK_STRUCT_PENDING);
574}
575
576static void clear_work_data(struct work_struct *work)
577{
578 set_work_data(work, WORK_STRUCT_NO_CPU, 0);
579}
580
Tejun Heo7a22ad72010-06-29 10:07:13 +0200581static struct cpu_workqueue_struct *get_work_cwq(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
Tejun Heoe1201532010-07-22 14:14:25 +0200585 if (data & WORK_STRUCT_CWQ)
586 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
587 else
588 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200589}
590
591static struct global_cwq *get_work_gcwq(struct work_struct *work)
592{
Tejun Heoe1201532010-07-22 14:14:25 +0200593 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200594 unsigned int cpu;
595
Tejun Heoe1201532010-07-22 14:14:25 +0200596 if (data & WORK_STRUCT_CWQ)
597 return ((struct cpu_workqueue_struct *)
Tejun Heobd7bdd42012-07-12 14:46:37 -0700598 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->gcwq;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200599
600 cpu = data >> WORK_STRUCT_FLAG_BITS;
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200601 if (cpu == WORK_CPU_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200602 return NULL;
603
Tejun Heof3421792010-07-02 10:03:51 +0200604 BUG_ON(cpu >= nr_cpu_ids && cpu != WORK_CPU_UNBOUND);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200605 return get_gcwq(cpu);
David Howells365970a2006-11-22 14:54:49 +0000606}
607
608/*
Tejun Heo32704762012-07-13 22:16:45 -0700609 * Policy functions. These define the policies on how the global worker
610 * pools are managed. Unless noted otherwise, these functions assume that
611 * they're being called with gcwq->lock held.
David Howells365970a2006-11-22 14:54:49 +0000612 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200613
Tejun Heo63d95a92012-07-12 14:46:37 -0700614static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000615{
Tejun Heo32704762012-07-13 22:16:45 -0700616 return !atomic_read(get_pool_nr_running(pool));
David Howells365970a2006-11-22 14:54:49 +0000617}
618
Tejun Heoe22bee72010-06-29 10:07:14 +0200619/*
620 * Need to wake up a worker? Called from anything but currently
621 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700622 *
623 * Note that, because unbound workers never contribute to nr_running, this
624 * function will always return %true for unbound gcwq as long as the
625 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200626 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700627static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000628{
Tejun Heo63d95a92012-07-12 14:46:37 -0700629 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000630}
631
Tejun Heoe22bee72010-06-29 10:07:14 +0200632/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700633static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200634{
Tejun Heo63d95a92012-07-12 14:46:37 -0700635 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200636}
637
638/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700639static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200640{
Tejun Heo63d95a92012-07-12 14:46:37 -0700641 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200642
Tejun Heo32704762012-07-13 22:16:45 -0700643 return !list_empty(&pool->worklist) && atomic_read(nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200644}
645
646/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700647static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200648{
Tejun Heo63d95a92012-07-12 14:46:37 -0700649 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200650}
651
652/* Do I need to be the manager? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700653static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200654{
Tejun Heo63d95a92012-07-12 14:46:37 -0700655 return need_to_create_worker(pool) ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700656 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200657}
658
659/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700660static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200661{
Tejun Heo60373152012-07-17 12:39:27 -0700662 bool managing = mutex_is_locked(&pool->manager_mutex);
Tejun Heo63d95a92012-07-12 14:46:37 -0700663 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
664 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200665
666 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
667}
668
669/*
670 * Wake up functions.
671 */
672
Tejun Heo7e116292010-06-29 10:07:13 +0200673/* Return the first worker. Safe with preemption disabled */
Tejun Heo63d95a92012-07-12 14:46:37 -0700674static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200675{
Tejun Heo63d95a92012-07-12 14:46:37 -0700676 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200677 return NULL;
678
Tejun Heo63d95a92012-07-12 14:46:37 -0700679 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200680}
681
682/**
683 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700684 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200685 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700686 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200687 *
688 * CONTEXT:
689 * spin_lock_irq(gcwq->lock).
690 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700691static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200692{
Tejun Heo63d95a92012-07-12 14:46:37 -0700693 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200694
695 if (likely(worker))
696 wake_up_process(worker->task);
697}
698
Tejun Heo4690c4a2010-06-29 10:07:10 +0200699/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200700 * wq_worker_waking_up - a worker is waking up
701 * @task: task waking up
702 * @cpu: CPU @task is waking up to
703 *
704 * This function is called during try_to_wake_up() when a worker is
705 * being awoken.
706 *
707 * CONTEXT:
708 * spin_lock_irq(rq->lock)
709 */
710void wq_worker_waking_up(struct task_struct *task, unsigned int cpu)
711{
712 struct worker *worker = kthread_data(task);
713
Steven Rostedt2d646722010-12-03 23:12:33 -0500714 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heo63d95a92012-07-12 14:46:37 -0700715 atomic_inc(get_pool_nr_running(worker->pool));
Tejun Heoe22bee72010-06-29 10:07:14 +0200716}
717
718/**
719 * wq_worker_sleeping - a worker is going to sleep
720 * @task: task going to sleep
721 * @cpu: CPU in question, must be the current CPU number
722 *
723 * This function is called during schedule() when a busy worker is
724 * going to sleep. Worker on the same cpu can be woken up by
725 * returning pointer to its task.
726 *
727 * CONTEXT:
728 * spin_lock_irq(rq->lock)
729 *
730 * RETURNS:
731 * Worker task on @cpu to wake up, %NULL if none.
732 */
733struct task_struct *wq_worker_sleeping(struct task_struct *task,
734 unsigned int cpu)
735{
736 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heobd7bdd42012-07-12 14:46:37 -0700737 struct worker_pool *pool = worker->pool;
Tejun Heo63d95a92012-07-12 14:46:37 -0700738 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200739
Steven Rostedt2d646722010-12-03 23:12:33 -0500740 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200741 return NULL;
742
743 /* this can only happen on the local cpu */
744 BUG_ON(cpu != raw_smp_processor_id());
745
746 /*
747 * The counterpart of the following dec_and_test, implied mb,
748 * worklist not empty test sequence is in insert_work().
749 * Please read comment there.
750 *
751 * NOT_RUNNING is clear. This means that trustee is not in
752 * charge and we're running on the local cpu w/ rq lock held
753 * and preemption disabled, which in turn means that none else
754 * could be manipulating idle_list, so dereferencing idle_list
755 * without gcwq lock is safe.
756 */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700757 if (atomic_dec_and_test(nr_running) && !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700758 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200759 return to_wakeup ? to_wakeup->task : NULL;
760}
761
762/**
763 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200764 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200765 * @flags: flags to set
766 * @wakeup: wakeup an idle worker if necessary
767 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200768 * Set @flags in @worker->flags and adjust nr_running accordingly. If
769 * nr_running becomes zero and @wakeup is %true, an idle worker is
770 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200771 *
Tejun Heocb444762010-07-02 10:03:50 +0200772 * CONTEXT:
773 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200774 */
775static inline void worker_set_flags(struct worker *worker, unsigned int flags,
776 bool wakeup)
777{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700778 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200779
Tejun Heocb444762010-07-02 10:03:50 +0200780 WARN_ON_ONCE(worker->task != current);
781
Tejun Heoe22bee72010-06-29 10:07:14 +0200782 /*
783 * If transitioning into NOT_RUNNING, adjust nr_running and
784 * wake up an idle worker as necessary if requested by
785 * @wakeup.
786 */
787 if ((flags & WORKER_NOT_RUNNING) &&
788 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heo63d95a92012-07-12 14:46:37 -0700789 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200790
791 if (wakeup) {
792 if (atomic_dec_and_test(nr_running) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -0700793 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700794 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200795 } else
796 atomic_dec(nr_running);
797 }
798
Tejun Heod302f012010-06-29 10:07:13 +0200799 worker->flags |= flags;
800}
801
802/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200803 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200804 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200805 * @flags: flags to clear
806 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200807 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200808 *
Tejun Heocb444762010-07-02 10:03:50 +0200809 * CONTEXT:
810 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200811 */
812static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
813{
Tejun Heo63d95a92012-07-12 14:46:37 -0700814 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200815 unsigned int oflags = worker->flags;
816
Tejun Heocb444762010-07-02 10:03:50 +0200817 WARN_ON_ONCE(worker->task != current);
818
Tejun Heod302f012010-06-29 10:07:13 +0200819 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200820
Tejun Heo42c025f2011-01-11 15:58:49 +0100821 /*
822 * If transitioning out of NOT_RUNNING, increment nr_running. Note
823 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
824 * of multiple flags, not a single flag.
825 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200826 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
827 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heo63d95a92012-07-12 14:46:37 -0700828 atomic_inc(get_pool_nr_running(pool));
Tejun Heod302f012010-06-29 10:07:13 +0200829}
830
831/**
Tejun Heoc8e55f32010-06-29 10:07:12 +0200832 * busy_worker_head - return the busy hash head for a work
833 * @gcwq: gcwq of interest
834 * @work: work to be hashed
835 *
836 * Return hash head of @gcwq for @work.
837 *
838 * CONTEXT:
839 * spin_lock_irq(gcwq->lock).
840 *
841 * RETURNS:
842 * Pointer to the hash head.
843 */
844static struct hlist_head *busy_worker_head(struct global_cwq *gcwq,
845 struct work_struct *work)
846{
847 const int base_shift = ilog2(sizeof(struct work_struct));
848 unsigned long v = (unsigned long)work;
849
850 /* simple shift and fold hash, do we need something better? */
851 v >>= base_shift;
852 v += v >> BUSY_WORKER_HASH_ORDER;
853 v &= BUSY_WORKER_HASH_MASK;
854
855 return &gcwq->busy_hash[v];
856}
857
858/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200859 * __find_worker_executing_work - find worker which is executing a work
860 * @gcwq: gcwq of interest
861 * @bwh: hash head as returned by busy_worker_head()
862 * @work: work to find worker for
863 *
864 * Find a worker which is executing @work on @gcwq. @bwh should be
865 * the hash head obtained by calling busy_worker_head() with the same
866 * work.
867 *
868 * CONTEXT:
869 * spin_lock_irq(gcwq->lock).
870 *
871 * RETURNS:
872 * Pointer to worker which is executing @work if found, NULL
873 * otherwise.
874 */
875static struct worker *__find_worker_executing_work(struct global_cwq *gcwq,
876 struct hlist_head *bwh,
877 struct work_struct *work)
878{
879 struct worker *worker;
880 struct hlist_node *tmp;
881
882 hlist_for_each_entry(worker, tmp, bwh, hentry)
883 if (worker->current_work == work)
884 return worker;
885 return NULL;
886}
887
888/**
889 * find_worker_executing_work - find worker which is executing a work
890 * @gcwq: gcwq of interest
891 * @work: work to find worker for
892 *
893 * Find a worker which is executing @work on @gcwq. This function is
894 * identical to __find_worker_executing_work() except that this
895 * function calculates @bwh itself.
896 *
897 * CONTEXT:
898 * spin_lock_irq(gcwq->lock).
899 *
900 * RETURNS:
901 * Pointer to worker which is executing @work if found, NULL
902 * otherwise.
903 */
904static struct worker *find_worker_executing_work(struct global_cwq *gcwq,
905 struct work_struct *work)
906{
907 return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work),
908 work);
909}
910
911/**
Tejun Heo7e116292010-06-29 10:07:13 +0200912 * insert_work - insert a work into gcwq
Tejun Heo4690c4a2010-06-29 10:07:10 +0200913 * @cwq: cwq @work belongs to
914 * @work: work to insert
915 * @head: insertion point
916 * @extra_flags: extra WORK_STRUCT_* flags to set
917 *
Tejun Heo7e116292010-06-29 10:07:13 +0200918 * Insert @work which belongs to @cwq into @gcwq after @head.
919 * @extra_flags is or'd to work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200920 *
921 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +0200922 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +0200923 */
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700924static void insert_work(struct cpu_workqueue_struct *cwq,
Tejun Heo4690c4a2010-06-29 10:07:10 +0200925 struct work_struct *work, struct list_head *head,
926 unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700927{
Tejun Heo63d95a92012-07-12 14:46:37 -0700928 struct worker_pool *pool = cwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100929
Tejun Heo4690c4a2010-06-29 10:07:10 +0200930 /* we own @work, set data and link */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200931 set_work_cwq(work, cwq, extra_flags);
Tejun Heo4690c4a2010-06-29 10:07:10 +0200932
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700933 /*
934 * Ensure that we get the right work->data if we see the
935 * result of list_add() below, see try_to_grab_pending().
936 */
937 smp_wmb();
Tejun Heo4690c4a2010-06-29 10:07:10 +0200938
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700939 list_add_tail(&work->entry, head);
Tejun Heoe22bee72010-06-29 10:07:14 +0200940
941 /*
942 * Ensure either worker_sched_deactivated() sees the above
943 * list_add_tail() or we see zero nr_running to avoid workers
944 * lying around lazily while there are works to be processed.
945 */
946 smp_mb();
947
Tejun Heo63d95a92012-07-12 14:46:37 -0700948 if (__need_more_worker(pool))
949 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700950}
951
Tejun Heoc8efcc22010-12-20 19:32:04 +0100952/*
953 * Test whether @work is being queued from another work executing on the
954 * same workqueue. This is rather expensive and should only be used from
955 * cold paths.
956 */
957static bool is_chained_work(struct workqueue_struct *wq)
958{
959 unsigned long flags;
960 unsigned int cpu;
961
962 for_each_gcwq_cpu(cpu) {
963 struct global_cwq *gcwq = get_gcwq(cpu);
964 struct worker *worker;
965 struct hlist_node *pos;
966 int i;
967
968 spin_lock_irqsave(&gcwq->lock, flags);
969 for_each_busy_worker(worker, i, pos, gcwq) {
970 if (worker->task != current)
971 continue;
972 spin_unlock_irqrestore(&gcwq->lock, flags);
973 /*
974 * I'm @worker, no locking necessary. See if @work
975 * is headed to the same workqueue.
976 */
977 return worker->current_cwq->wq == wq;
978 }
979 spin_unlock_irqrestore(&gcwq->lock, flags);
980 }
981 return false;
982}
983
Tejun Heo4690c4a2010-06-29 10:07:10 +0200984static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 struct work_struct *work)
986{
Tejun Heo502ca9d2010-06-29 10:07:13 +0200987 struct global_cwq *gcwq;
988 struct cpu_workqueue_struct *cwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200989 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +0200990 unsigned int work_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 unsigned long flags;
992
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900993 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200994
Tejun Heoc8efcc22010-12-20 19:32:04 +0100995 /* if dying, only works from the same workqueue are allowed */
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200996 if (unlikely(wq->flags & WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +0100997 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +0200998 return;
999
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001000 /* determine gcwq to use */
1001 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001002 struct global_cwq *last_gcwq;
1003
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001004 if (unlikely(cpu == WORK_CPU_UNBOUND))
1005 cpu = raw_smp_processor_id();
1006
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001007 /*
1008 * It's multi cpu. If @wq is non-reentrant and @work
1009 * was previously on a different cpu, it might still
1010 * be running there, in which case the work needs to
1011 * be queued on that cpu to guarantee non-reentrance.
1012 */
Tejun Heo502ca9d2010-06-29 10:07:13 +02001013 gcwq = get_gcwq(cpu);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001014 if (wq->flags & WQ_NON_REENTRANT &&
1015 (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) {
1016 struct worker *worker;
1017
1018 spin_lock_irqsave(&last_gcwq->lock, flags);
1019
1020 worker = find_worker_executing_work(last_gcwq, work);
1021
1022 if (worker && worker->current_cwq->wq == wq)
1023 gcwq = last_gcwq;
1024 else {
1025 /* meh... not running there, queue here */
1026 spin_unlock_irqrestore(&last_gcwq->lock, flags);
1027 spin_lock_irqsave(&gcwq->lock, flags);
1028 }
1029 } else
1030 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heof3421792010-07-02 10:03:51 +02001031 } else {
1032 gcwq = get_gcwq(WORK_CPU_UNBOUND);
1033 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001034 }
1035
1036 /* gcwq determined, get cwq and queue */
1037 cwq = get_cwq(gcwq->cpu, wq);
Tejun Heocdadf002010-10-05 10:49:55 +02001038 trace_workqueue_queue_work(cpu, cwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001039
Dan Carpenterf5b25522012-04-13 22:06:58 +03001040 if (WARN_ON(!list_empty(&work->entry))) {
1041 spin_unlock_irqrestore(&gcwq->lock, flags);
1042 return;
1043 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001044
Tejun Heo73f53c42010-06-29 10:07:11 +02001045 cwq->nr_in_flight[cwq->work_color]++;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001046 work_flags = work_color_to_flags(cwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001047
1048 if (likely(cwq->nr_active < cwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001049 trace_workqueue_activate_work(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001050 cwq->nr_active++;
Tejun Heo32704762012-07-13 22:16:45 -07001051 worklist = &cwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001052 } else {
1053 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001054 worklist = &cwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001055 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001056
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001057 insert_work(cwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001058
Tejun Heo8b03ae32010-06-29 10:07:12 +02001059 spin_unlock_irqrestore(&gcwq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001062/**
1063 * queue_work - queue work on a workqueue
1064 * @wq: workqueue to use
1065 * @work: work to queue
1066 *
Alan Stern057647f2006-10-28 10:38:58 -07001067 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 *
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07001069 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1070 * it can be processed by another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001072int queue_work(struct workqueue_struct *wq, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001074 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001076 ret = queue_work_on(get_cpu(), wq, work);
1077 put_cpu();
1078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 return ret;
1080}
Dave Jonesae90dd52006-06-30 01:40:45 -04001081EXPORT_SYMBOL_GPL(queue_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Zhang Ruic1a220e2008-07-23 21:28:39 -07001083/**
1084 * queue_work_on - queue work on specific cpu
1085 * @cpu: CPU number to execute work on
1086 * @wq: workqueue to use
1087 * @work: work to queue
1088 *
1089 * Returns 0 if @work was already on a queue, non-zero otherwise.
1090 *
1091 * We queue the work to a specific CPU, the caller must ensure it
1092 * can't go away.
1093 */
1094int
1095queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
1096{
1097 int ret = 0;
1098
Tejun Heo22df02b2010-06-29 10:07:10 +02001099 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001100 __queue_work(cpu, wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001101 ret = 1;
1102 }
1103 return ret;
1104}
1105EXPORT_SYMBOL_GPL(queue_work_on);
1106
Li Zefan6d141c32008-02-08 04:21:09 -08001107static void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
David Howells52bad642006-11-22 14:54:01 +00001109 struct delayed_work *dwork = (struct delayed_work *)__data;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001110 struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Tejun Heo4690c4a2010-06-29 10:07:10 +02001112 __queue_work(smp_processor_id(), cwq->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113}
1114
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001115/**
1116 * queue_delayed_work - queue work on a workqueue after delay
1117 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001118 * @dwork: delayable work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001119 * @delay: number of jiffies to wait before queueing
1120 *
Alan Stern057647f2006-10-28 10:38:58 -07001121 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001122 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001123int queue_delayed_work(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001124 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
David Howells52bad642006-11-22 14:54:01 +00001126 if (delay == 0)
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001127 return queue_work(wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001129 return queue_delayed_work_on(-1, wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130}
Dave Jonesae90dd52006-06-30 01:40:45 -04001131EXPORT_SYMBOL_GPL(queue_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001133/**
1134 * queue_delayed_work_on - queue work on specific CPU after delay
1135 * @cpu: CPU number to execute work on
1136 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001137 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001138 * @delay: number of jiffies to wait before queueing
1139 *
Alan Stern057647f2006-10-28 10:38:58 -07001140 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001141 */
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001142int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001143 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001144{
1145 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +00001146 struct timer_list *timer = &dwork->timer;
1147 struct work_struct *work = &dwork->work;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001148
Tejun Heo22df02b2010-06-29 10:07:10 +02001149 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001150 unsigned int lcpu;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001151
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001152 BUG_ON(timer_pending(timer));
1153 BUG_ON(!list_empty(&work->entry));
1154
Andrew Liu8a3e77c2008-05-01 04:35:14 -07001155 timer_stats_timer_set_start_info(&dwork->timer);
1156
Tejun Heo7a22ad72010-06-29 10:07:13 +02001157 /*
1158 * This stores cwq for the moment, for the timer_fn.
1159 * Note that the work's gcwq is preserved to allow
1160 * reentrance detection for delayed works.
1161 */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001162 if (!(wq->flags & WQ_UNBOUND)) {
1163 struct global_cwq *gcwq = get_work_gcwq(work);
1164
1165 if (gcwq && gcwq->cpu != WORK_CPU_UNBOUND)
1166 lcpu = gcwq->cpu;
1167 else
1168 lcpu = raw_smp_processor_id();
1169 } else
1170 lcpu = WORK_CPU_UNBOUND;
1171
Tejun Heo7a22ad72010-06-29 10:07:13 +02001172 set_work_cwq(work, get_cwq(lcpu, wq), 0);
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001173
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001174 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +00001175 timer->data = (unsigned long)dwork;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001176 timer->function = delayed_work_timer_fn;
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001177
1178 if (unlikely(cpu >= 0))
1179 add_timer_on(timer, cpu);
1180 else
1181 add_timer(timer);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001182 ret = 1;
1183 }
1184 return ret;
1185}
Dave Jonesae90dd52006-06-30 01:40:45 -04001186EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Tejun Heoc8e55f32010-06-29 10:07:12 +02001188/**
1189 * worker_enter_idle - enter idle state
1190 * @worker: worker which is entering idle state
1191 *
1192 * @worker is entering idle state. Update stats and idle timer if
1193 * necessary.
1194 *
1195 * LOCKING:
1196 * spin_lock_irq(gcwq->lock).
1197 */
1198static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001200 struct worker_pool *pool = worker->pool;
1201 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Tejun Heoc8e55f32010-06-29 10:07:12 +02001203 BUG_ON(worker->flags & WORKER_IDLE);
1204 BUG_ON(!list_empty(&worker->entry) &&
1205 (worker->hentry.next || worker->hentry.pprev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Tejun Heocb444762010-07-02 10:03:50 +02001207 /* can't use worker_set_flags(), also called from start_worker() */
1208 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001209 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001210 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001211
Tejun Heoc8e55f32010-06-29 10:07:12 +02001212 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001213 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001214
Tejun Heo403c8212012-07-17 12:39:27 -07001215 if (likely(gcwq->trustee_state != TRUSTEE_DONE)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001216 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
Tejun Heobd7bdd42012-07-12 14:46:37 -07001217 mod_timer(&pool->idle_timer,
Tejun Heoe22bee72010-06-29 10:07:14 +02001218 jiffies + IDLE_WORKER_TIMEOUT);
1219 } else
Tejun Heodb7bccf2010-06-29 10:07:12 +02001220 wake_up_all(&gcwq->trustee_wait);
Tejun Heocb444762010-07-02 10:03:50 +02001221
Tejun Heo544ecf32012-05-14 15:04:50 -07001222 /*
1223 * Sanity check nr_running. Because trustee releases gcwq->lock
Tejun Heo403c8212012-07-17 12:39:27 -07001224 * between setting %WORKER_UNBOUND and zapping nr_running, the
Tejun Heo544ecf32012-05-14 15:04:50 -07001225 * warning may trigger spuriously. Check iff trustee is idle.
1226 */
1227 WARN_ON_ONCE(gcwq->trustee_state == TRUSTEE_DONE &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001228 pool->nr_workers == pool->nr_idle &&
Tejun Heo63d95a92012-07-12 14:46:37 -07001229 atomic_read(get_pool_nr_running(pool)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230}
1231
Tejun Heoc8e55f32010-06-29 10:07:12 +02001232/**
1233 * worker_leave_idle - leave idle state
1234 * @worker: worker which is leaving idle state
1235 *
1236 * @worker is leaving idle state. Update stats.
1237 *
1238 * LOCKING:
1239 * spin_lock_irq(gcwq->lock).
1240 */
1241static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001243 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Tejun Heoc8e55f32010-06-29 10:07:12 +02001245 BUG_ON(!(worker->flags & WORKER_IDLE));
Tejun Heod302f012010-06-29 10:07:13 +02001246 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001247 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001248 list_del_init(&worker->entry);
1249}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Tejun Heoe22bee72010-06-29 10:07:14 +02001251/**
1252 * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq
1253 * @worker: self
1254 *
1255 * Works which are scheduled while the cpu is online must at least be
1256 * scheduled to a worker which is bound to the cpu so that if they are
1257 * flushed from cpu callbacks while cpu is going down, they are
1258 * guaranteed to execute on the cpu.
1259 *
1260 * This function is to be used by rogue workers and rescuers to bind
1261 * themselves to the target cpu and may race with cpu going down or
1262 * coming online. kthread_bind() can't be used because it may put the
1263 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1264 * verbatim as it's best effort and blocking and gcwq may be
1265 * [dis]associated in the meantime.
1266 *
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001267 * This function tries set_cpus_allowed() and locks gcwq and verifies the
1268 * binding against %GCWQ_DISASSOCIATED which is set during
1269 * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1270 * enters idle state or fetches works without dropping lock, it can
1271 * guarantee the scheduling requirement described in the first paragraph.
Tejun Heoe22bee72010-06-29 10:07:14 +02001272 *
1273 * CONTEXT:
1274 * Might sleep. Called without any lock but returns with gcwq->lock
1275 * held.
1276 *
1277 * RETURNS:
1278 * %true if the associated gcwq is online (@worker is successfully
1279 * bound), %false if offline.
1280 */
1281static bool worker_maybe_bind_and_lock(struct worker *worker)
Namhyung Kim972fa1c2010-08-22 23:19:43 +09001282__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001283{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001284 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001285 struct task_struct *task = worker->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Tejun Heoe22bee72010-06-29 10:07:14 +02001287 while (true) {
1288 /*
1289 * The following call may fail, succeed or succeed
1290 * without actually migrating the task to the cpu if
1291 * it races with cpu hotunplug operation. Verify
1292 * against GCWQ_DISASSOCIATED.
1293 */
Tejun Heof3421792010-07-02 10:03:51 +02001294 if (!(gcwq->flags & GCWQ_DISASSOCIATED))
1295 set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu));
Oleg Nesterov85f41862007-05-09 02:34:20 -07001296
Tejun Heoe22bee72010-06-29 10:07:14 +02001297 spin_lock_irq(&gcwq->lock);
1298 if (gcwq->flags & GCWQ_DISASSOCIATED)
1299 return false;
1300 if (task_cpu(task) == gcwq->cpu &&
1301 cpumask_equal(&current->cpus_allowed,
1302 get_cpu_mask(gcwq->cpu)))
1303 return true;
1304 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001305
Tejun Heo5035b202011-04-29 18:08:37 +02001306 /*
1307 * We've raced with CPU hot[un]plug. Give it a breather
1308 * and retry migration. cond_resched() is required here;
1309 * otherwise, we might deadlock against cpu_stop trying to
1310 * bring down the CPU on non-preemptive kernel.
1311 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001312 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001313 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001314 }
1315}
1316
1317/*
Tejun Heo403c8212012-07-17 12:39:27 -07001318 * Function for worker->rebind_work used to rebind unbound busy workers to
1319 * the associated cpu which is coming back online. This is scheduled by
1320 * cpu up but can race with other cpu hotplug operations and may be
1321 * executed twice without intervening cpu down.
Tejun Heoe22bee72010-06-29 10:07:14 +02001322 */
1323static void worker_rebind_fn(struct work_struct *work)
1324{
1325 struct worker *worker = container_of(work, struct worker, rebind_work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001326 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001327
1328 if (worker_maybe_bind_and_lock(worker))
1329 worker_clr_flags(worker, WORKER_REBIND);
1330
1331 spin_unlock_irq(&gcwq->lock);
1332}
1333
Tejun Heoc34056a2010-06-29 10:07:11 +02001334static struct worker *alloc_worker(void)
1335{
1336 struct worker *worker;
1337
1338 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001339 if (worker) {
1340 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001341 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heoe22bee72010-06-29 10:07:14 +02001342 INIT_WORK(&worker->rebind_work, worker_rebind_fn);
1343 /* on creation a worker is in !idle && prep state */
1344 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001345 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001346 return worker;
1347}
1348
1349/**
1350 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001351 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001352 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001353 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001354 * can be started by calling start_worker() or destroyed using
1355 * destroy_worker().
1356 *
1357 * CONTEXT:
1358 * Might sleep. Does GFP_KERNEL allocations.
1359 *
1360 * RETURNS:
1361 * Pointer to the newly created worker.
1362 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001363static struct worker *create_worker(struct worker_pool *pool)
Tejun Heoc34056a2010-06-29 10:07:11 +02001364{
Tejun Heo63d95a92012-07-12 14:46:37 -07001365 struct global_cwq *gcwq = pool->gcwq;
Tejun Heo32704762012-07-13 22:16:45 -07001366 const char *pri = worker_pool_pri(pool) ? "H" : "";
Tejun Heoc34056a2010-06-29 10:07:11 +02001367 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001368 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001369
Tejun Heo8b03ae32010-06-29 10:07:12 +02001370 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001371 while (ida_get_new(&pool->worker_ida, &id)) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001372 spin_unlock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001373 if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001374 goto fail;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001375 spin_lock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001376 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02001377 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001378
1379 worker = alloc_worker();
1380 if (!worker)
1381 goto fail;
1382
Tejun Heobd7bdd42012-07-12 14:46:37 -07001383 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001384 worker->id = id;
1385
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001386 if (gcwq->cpu != WORK_CPU_UNBOUND)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001387 worker->task = kthread_create_on_node(worker_thread,
Tejun Heo32704762012-07-13 22:16:45 -07001388 worker, cpu_to_node(gcwq->cpu),
1389 "kworker/%u:%d%s", gcwq->cpu, id, pri);
Tejun Heof3421792010-07-02 10:03:51 +02001390 else
1391 worker->task = kthread_create(worker_thread, worker,
Tejun Heo32704762012-07-13 22:16:45 -07001392 "kworker/u:%d%s", id, pri);
Tejun Heoc34056a2010-06-29 10:07:11 +02001393 if (IS_ERR(worker->task))
1394 goto fail;
1395
Tejun Heo32704762012-07-13 22:16:45 -07001396 if (worker_pool_pri(pool))
1397 set_user_nice(worker->task, HIGHPRI_NICE_LEVEL);
1398
Tejun Heodb7bccf2010-06-29 10:07:12 +02001399 /*
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001400 * Determine CPU binding of the new worker depending on
1401 * %GCWQ_DISASSOCIATED. The caller is responsible for ensuring the
1402 * flag remains stable across this function. See the comments
1403 * above the flag definition for details.
1404 *
1405 * As an unbound worker may later become a regular one if CPU comes
1406 * online, make sure every worker has %PF_THREAD_BOUND set.
Tejun Heodb7bccf2010-06-29 10:07:12 +02001407 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001408 if (!(gcwq->flags & GCWQ_DISASSOCIATED)) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001409 kthread_bind(worker->task, gcwq->cpu);
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001410 } else {
Tejun Heodb7bccf2010-06-29 10:07:12 +02001411 worker->task->flags |= PF_THREAD_BOUND;
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001412 worker->flags |= WORKER_UNBOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001414
Tejun Heoc34056a2010-06-29 10:07:11 +02001415 return worker;
1416fail:
1417 if (id >= 0) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001418 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001419 ida_remove(&pool->worker_ida, id);
Tejun Heo8b03ae32010-06-29 10:07:12 +02001420 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001421 }
1422 kfree(worker);
1423 return NULL;
1424}
1425
1426/**
1427 * start_worker - start a newly created worker
1428 * @worker: worker to start
1429 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001430 * Make the gcwq aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001431 *
1432 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001433 * spin_lock_irq(gcwq->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001434 */
1435static void start_worker(struct worker *worker)
1436{
Tejun Heocb444762010-07-02 10:03:50 +02001437 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001438 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001439 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001440 wake_up_process(worker->task);
1441}
1442
1443/**
1444 * destroy_worker - destroy a workqueue worker
1445 * @worker: worker to be destroyed
1446 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001447 * Destroy @worker and adjust @gcwq stats accordingly.
1448 *
1449 * CONTEXT:
1450 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001451 */
1452static void destroy_worker(struct worker *worker)
1453{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001454 struct worker_pool *pool = worker->pool;
1455 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001456 int id = worker->id;
1457
1458 /* sanity check frenzy */
1459 BUG_ON(worker->current_work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001460 BUG_ON(!list_empty(&worker->scheduled));
Tejun Heoc34056a2010-06-29 10:07:11 +02001461
Tejun Heoc8e55f32010-06-29 10:07:12 +02001462 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001463 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001464 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001465 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001466
1467 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001468 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001469
1470 spin_unlock_irq(&gcwq->lock);
1471
Tejun Heoc34056a2010-06-29 10:07:11 +02001472 kthread_stop(worker->task);
1473 kfree(worker);
1474
Tejun Heo8b03ae32010-06-29 10:07:12 +02001475 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001476 ida_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001477}
1478
Tejun Heo63d95a92012-07-12 14:46:37 -07001479static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001480{
Tejun Heo63d95a92012-07-12 14:46:37 -07001481 struct worker_pool *pool = (void *)__pool;
1482 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001483
1484 spin_lock_irq(&gcwq->lock);
1485
Tejun Heo63d95a92012-07-12 14:46:37 -07001486 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001487 struct worker *worker;
1488 unsigned long expires;
1489
1490 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001491 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001492 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1493
1494 if (time_before(jiffies, expires))
Tejun Heo63d95a92012-07-12 14:46:37 -07001495 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001496 else {
1497 /* it's been idle for too long, wake up manager */
Tejun Heo11ebea52012-07-12 14:46:37 -07001498 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -07001499 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001500 }
1501 }
1502
1503 spin_unlock_irq(&gcwq->lock);
1504}
1505
1506static bool send_mayday(struct work_struct *work)
1507{
1508 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1509 struct workqueue_struct *wq = cwq->wq;
Tejun Heof3421792010-07-02 10:03:51 +02001510 unsigned int cpu;
Tejun Heoe22bee72010-06-29 10:07:14 +02001511
1512 if (!(wq->flags & WQ_RESCUER))
1513 return false;
1514
1515 /* mayday mayday mayday */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001516 cpu = cwq->pool->gcwq->cpu;
Tejun Heof3421792010-07-02 10:03:51 +02001517 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1518 if (cpu == WORK_CPU_UNBOUND)
1519 cpu = 0;
Tejun Heof2e005a2010-07-20 15:59:09 +02001520 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001521 wake_up_process(wq->rescuer->task);
1522 return true;
1523}
1524
Tejun Heo63d95a92012-07-12 14:46:37 -07001525static void gcwq_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001526{
Tejun Heo63d95a92012-07-12 14:46:37 -07001527 struct worker_pool *pool = (void *)__pool;
1528 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001529 struct work_struct *work;
1530
1531 spin_lock_irq(&gcwq->lock);
1532
Tejun Heo63d95a92012-07-12 14:46:37 -07001533 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001534 /*
1535 * We've been trying to create a new worker but
1536 * haven't been successful. We might be hitting an
1537 * allocation deadlock. Send distress signals to
1538 * rescuers.
1539 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001540 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001541 send_mayday(work);
1542 }
1543
1544 spin_unlock_irq(&gcwq->lock);
1545
Tejun Heo63d95a92012-07-12 14:46:37 -07001546 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001547}
1548
1549/**
1550 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001551 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001552 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001553 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001554 * have at least one idle worker on return from this function. If
1555 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001556 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001557 * possible allocation deadlock.
1558 *
1559 * On return, need_to_create_worker() is guaranteed to be false and
1560 * may_start_working() true.
1561 *
1562 * LOCKING:
1563 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1564 * multiple times. Does GFP_KERNEL allocations. Called only from
1565 * manager.
1566 *
1567 * RETURNS:
1568 * false if no action was taken and gcwq->lock stayed locked, true
1569 * otherwise.
1570 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001571static bool maybe_create_worker(struct worker_pool *pool)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001572__releases(&gcwq->lock)
1573__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001574{
Tejun Heo63d95a92012-07-12 14:46:37 -07001575 struct global_cwq *gcwq = pool->gcwq;
1576
1577 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001578 return false;
1579restart:
Tejun Heo9f9c2362010-07-14 11:31:20 +02001580 spin_unlock_irq(&gcwq->lock);
1581
Tejun Heoe22bee72010-06-29 10:07:14 +02001582 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07001583 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001584
1585 while (true) {
1586 struct worker *worker;
1587
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001588 worker = create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001589 if (worker) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001590 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001591 spin_lock_irq(&gcwq->lock);
1592 start_worker(worker);
Tejun Heo63d95a92012-07-12 14:46:37 -07001593 BUG_ON(need_to_create_worker(pool));
Tejun Heoe22bee72010-06-29 10:07:14 +02001594 return true;
1595 }
1596
Tejun Heo63d95a92012-07-12 14:46:37 -07001597 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001598 break;
1599
Tejun Heoe22bee72010-06-29 10:07:14 +02001600 __set_current_state(TASK_INTERRUPTIBLE);
1601 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001602
Tejun Heo63d95a92012-07-12 14:46:37 -07001603 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001604 break;
1605 }
1606
Tejun Heo63d95a92012-07-12 14:46:37 -07001607 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001608 spin_lock_irq(&gcwq->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07001609 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001610 goto restart;
1611 return true;
1612}
1613
1614/**
1615 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo63d95a92012-07-12 14:46:37 -07001616 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001617 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001618 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02001619 * IDLE_WORKER_TIMEOUT.
1620 *
1621 * LOCKING:
1622 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1623 * multiple times. Called only from manager.
1624 *
1625 * RETURNS:
1626 * false if no action was taken and gcwq->lock stayed locked, true
1627 * otherwise.
1628 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001629static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001630{
1631 bool ret = false;
1632
Tejun Heo63d95a92012-07-12 14:46:37 -07001633 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001634 struct worker *worker;
1635 unsigned long expires;
1636
Tejun Heo63d95a92012-07-12 14:46:37 -07001637 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001638 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1639
1640 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001641 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001642 break;
1643 }
1644
1645 destroy_worker(worker);
1646 ret = true;
1647 }
1648
1649 return ret;
1650}
1651
1652/**
1653 * manage_workers - manage worker pool
1654 * @worker: self
1655 *
1656 * Assume the manager role and manage gcwq worker pool @worker belongs
1657 * to. At any given time, there can be only zero or one manager per
1658 * gcwq. The exclusion is handled automatically by this function.
1659 *
1660 * The caller can safely start processing works on false return. On
1661 * true return, it's guaranteed that need_to_create_worker() is false
1662 * and may_start_working() is true.
1663 *
1664 * CONTEXT:
1665 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1666 * multiple times. Does GFP_KERNEL allocations.
1667 *
1668 * RETURNS:
1669 * false if no action was taken and gcwq->lock stayed locked, true if
1670 * some action was taken.
1671 */
1672static bool manage_workers(struct worker *worker)
1673{
Tejun Heo63d95a92012-07-12 14:46:37 -07001674 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001675 bool ret = false;
1676
Tejun Heo60373152012-07-17 12:39:27 -07001677 if (!mutex_trylock(&pool->manager_mutex))
Tejun Heoe22bee72010-06-29 10:07:14 +02001678 return ret;
1679
Tejun Heo11ebea52012-07-12 14:46:37 -07001680 pool->flags &= ~POOL_MANAGE_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02001681
1682 /*
1683 * Destroy and then create so that may_start_working() is true
1684 * on return.
1685 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001686 ret |= maybe_destroy_workers(pool);
1687 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001688
Tejun Heo60373152012-07-17 12:39:27 -07001689 mutex_unlock(&pool->manager_mutex);
Tejun Heoe22bee72010-06-29 10:07:14 +02001690 return ret;
1691}
1692
Tejun Heoa62428c2010-06-29 10:07:10 +02001693/**
Tejun Heoaffee4b2010-06-29 10:07:12 +02001694 * move_linked_works - move linked works to a list
1695 * @work: start of series of works to be scheduled
1696 * @head: target list to append @work to
1697 * @nextp: out paramter for nested worklist walking
1698 *
1699 * Schedule linked works starting from @work to @head. Work series to
1700 * be scheduled starts at @work and includes any consecutive work with
1701 * WORK_STRUCT_LINKED set in its predecessor.
1702 *
1703 * If @nextp is not NULL, it's updated to point to the next work of
1704 * the last scheduled work. This allows move_linked_works() to be
1705 * nested inside outer list_for_each_entry_safe().
1706 *
1707 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001708 * spin_lock_irq(gcwq->lock).
Tejun Heoaffee4b2010-06-29 10:07:12 +02001709 */
1710static void move_linked_works(struct work_struct *work, struct list_head *head,
1711 struct work_struct **nextp)
1712{
1713 struct work_struct *n;
1714
1715 /*
1716 * Linked worklist will always end before the end of the list,
1717 * use NULL for list head.
1718 */
1719 list_for_each_entry_safe_from(work, n, NULL, entry) {
1720 list_move_tail(&work->entry, head);
1721 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1722 break;
1723 }
1724
1725 /*
1726 * If we're already inside safe list traversal and have moved
1727 * multiple works to the scheduled queue, the next position
1728 * needs to be updated.
1729 */
1730 if (nextp)
1731 *nextp = n;
1732}
1733
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001734static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
1735{
1736 struct work_struct *work = list_first_entry(&cwq->delayed_works,
1737 struct work_struct, entry);
1738
Tejun Heocdadf002010-10-05 10:49:55 +02001739 trace_workqueue_activate_work(work);
Tejun Heo32704762012-07-13 22:16:45 -07001740 move_linked_works(work, &cwq->pool->worklist, NULL);
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001741 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001742 cwq->nr_active++;
1743}
1744
Tejun Heoaffee4b2010-06-29 10:07:12 +02001745/**
Tejun Heo73f53c42010-06-29 10:07:11 +02001746 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
1747 * @cwq: cwq of interest
1748 * @color: color of work which left the queue
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001749 * @delayed: for a delayed work
Tejun Heo73f53c42010-06-29 10:07:11 +02001750 *
1751 * A work either has completed or is removed from pending queue,
1752 * decrement nr_in_flight of its cwq and handle workqueue flushing.
1753 *
1754 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001755 * spin_lock_irq(gcwq->lock).
Tejun Heo73f53c42010-06-29 10:07:11 +02001756 */
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001757static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color,
1758 bool delayed)
Tejun Heo73f53c42010-06-29 10:07:11 +02001759{
1760 /* ignore uncolored works */
1761 if (color == WORK_NO_COLOR)
1762 return;
1763
1764 cwq->nr_in_flight[color]--;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001765
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001766 if (!delayed) {
1767 cwq->nr_active--;
1768 if (!list_empty(&cwq->delayed_works)) {
1769 /* one down, submit a delayed one */
1770 if (cwq->nr_active < cwq->max_active)
1771 cwq_activate_first_delayed(cwq);
1772 }
Tejun Heo502ca9d2010-06-29 10:07:13 +02001773 }
Tejun Heo73f53c42010-06-29 10:07:11 +02001774
1775 /* is flush in progress and are we at the flushing tip? */
1776 if (likely(cwq->flush_color != color))
1777 return;
1778
1779 /* are there still in-flight works? */
1780 if (cwq->nr_in_flight[color])
1781 return;
1782
1783 /* this cwq is done, clear flush_color */
1784 cwq->flush_color = -1;
1785
1786 /*
1787 * If this was the last cwq, wake up the first flusher. It
1788 * will handle the rest.
1789 */
1790 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
1791 complete(&cwq->wq->first_flusher->done);
1792}
1793
1794/**
Tejun Heoa62428c2010-06-29 10:07:10 +02001795 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02001796 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02001797 * @work: work to process
1798 *
1799 * Process @work. This function contains all the logics necessary to
1800 * process a single work including synchronization against and
1801 * interaction with other workers on the same cpu, queueing and
1802 * flushing. As long as context requirement is met, any worker can
1803 * call this function to process a work.
1804 *
1805 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001806 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02001807 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001808static void process_one_work(struct worker *worker, struct work_struct *work)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001809__releases(&gcwq->lock)
1810__acquires(&gcwq->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02001811{
Tejun Heo7e116292010-06-29 10:07:13 +02001812 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001813 struct worker_pool *pool = worker->pool;
1814 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001815 struct hlist_head *bwh = busy_worker_head(gcwq, work);
Tejun Heofb0e7be2010-06-29 10:07:15 +02001816 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heoa62428c2010-06-29 10:07:10 +02001817 work_func_t f = work->func;
Tejun Heo73f53c42010-06-29 10:07:11 +02001818 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02001819 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02001820#ifdef CONFIG_LOCKDEP
1821 /*
1822 * It is permissible to free the struct work_struct from
1823 * inside the function that is called from it, this we need to
1824 * take into account for lockdep too. To avoid bogus "held
1825 * lock freed" warnings as well as problems when looking into
1826 * work->lockdep_map, make a copy and use that here.
1827 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07001828 struct lockdep_map lockdep_map;
1829
1830 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02001831#endif
Tejun Heo7e116292010-06-29 10:07:13 +02001832 /*
1833 * A single work shouldn't be executed concurrently by
1834 * multiple workers on a single cpu. Check whether anyone is
1835 * already processing the work. If so, defer the work to the
1836 * currently executing one.
1837 */
1838 collision = __find_worker_executing_work(gcwq, bwh, work);
1839 if (unlikely(collision)) {
1840 move_linked_works(work, &collision->scheduled, NULL);
1841 return;
1842 }
1843
Tejun Heoa62428c2010-06-29 10:07:10 +02001844 /* claim and process */
Tejun Heoa62428c2010-06-29 10:07:10 +02001845 debug_work_deactivate(work);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001846 hlist_add_head(&worker->hentry, bwh);
Tejun Heoc34056a2010-06-29 10:07:11 +02001847 worker->current_work = work;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001848 worker->current_cwq = cwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02001849 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02001850
Tejun Heo7a22ad72010-06-29 10:07:13 +02001851 /* record the current cpu number in the work data and dequeue */
1852 set_work_cpu(work, gcwq->cpu);
Tejun Heoa62428c2010-06-29 10:07:10 +02001853 list_del_init(&work->entry);
1854
Tejun Heo649027d2010-06-29 10:07:14 +02001855 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02001856 * CPU intensive works don't participate in concurrency
1857 * management. They're the scheduler's responsibility.
1858 */
1859 if (unlikely(cpu_intensive))
1860 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
1861
Tejun Heo974271c2012-07-12 14:46:37 -07001862 /*
1863 * Unbound gcwq isn't concurrency managed and work items should be
1864 * executed ASAP. Wake up another worker if necessary.
1865 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001866 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
1867 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07001868
Tejun Heo8b03ae32010-06-29 10:07:12 +02001869 spin_unlock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001870
Tejun Heoa62428c2010-06-29 10:07:10 +02001871 work_clear_pending(work);
Tejun Heoe1594892011-01-09 23:32:15 +01001872 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02001873 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001874 trace_workqueue_execute_start(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02001875 f(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001876 /*
1877 * While we must be careful to not use "work" after this, the trace
1878 * point will only record its address.
1879 */
1880 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02001881 lock_map_release(&lockdep_map);
1882 lock_map_release(&cwq->wq->lockdep_map);
1883
1884 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
1885 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
1886 "%s/0x%08x/%d\n",
1887 current->comm, preempt_count(), task_pid_nr(current));
1888 printk(KERN_ERR " last function: ");
1889 print_symbol("%s\n", (unsigned long)f);
1890 debug_show_held_locks(current);
1891 dump_stack();
1892 }
1893
Tejun Heo8b03ae32010-06-29 10:07:12 +02001894 spin_lock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001895
Tejun Heofb0e7be2010-06-29 10:07:15 +02001896 /* clear cpu intensive status */
1897 if (unlikely(cpu_intensive))
1898 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
1899
Tejun Heoa62428c2010-06-29 10:07:10 +02001900 /* we're done with it, release */
Tejun Heoc8e55f32010-06-29 10:07:12 +02001901 hlist_del_init(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001902 worker->current_work = NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001903 worker->current_cwq = NULL;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001904 cwq_dec_nr_in_flight(cwq, work_color, false);
Tejun Heoa62428c2010-06-29 10:07:10 +02001905}
1906
Tejun Heoaffee4b2010-06-29 10:07:12 +02001907/**
1908 * process_scheduled_works - process scheduled works
1909 * @worker: self
1910 *
1911 * Process all scheduled works. Please note that the scheduled list
1912 * may change while processing a work, so this function repeatedly
1913 * fetches a work from the top and executes it.
1914 *
1915 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001916 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02001917 * multiple times.
1918 */
1919static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920{
Tejun Heoaffee4b2010-06-29 10:07:12 +02001921 while (!list_empty(&worker->scheduled)) {
1922 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001924 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926}
1927
Tejun Heo4690c4a2010-06-29 10:07:10 +02001928/**
1929 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02001930 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02001931 *
Tejun Heoe22bee72010-06-29 10:07:14 +02001932 * The gcwq worker thread function. There's a single dynamic pool of
1933 * these per each cpu. These workers process all works regardless of
1934 * their specific target workqueue. The only exception is works which
1935 * belong to workqueues with a rescuer which will be explained in
1936 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02001937 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001938static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
Tejun Heoc34056a2010-06-29 10:07:11 +02001940 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001941 struct worker_pool *pool = worker->pool;
1942 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Tejun Heoe22bee72010-06-29 10:07:14 +02001944 /* tell the scheduler that this is a workqueue worker */
1945 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001946woke_up:
Tejun Heoc8e55f32010-06-29 10:07:12 +02001947 spin_lock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Tejun Heoc8e55f32010-06-29 10:07:12 +02001949 /* DIE can be set only while we're idle, checking here is enough */
1950 if (worker->flags & WORKER_DIE) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001951 spin_unlock_irq(&gcwq->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001952 worker->task->flags &= ~PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001953 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 }
1955
Tejun Heoc8e55f32010-06-29 10:07:12 +02001956 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001957recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02001958 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07001959 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001960 goto sleep;
1961
1962 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07001963 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02001964 goto recheck;
1965
Tejun Heoc8e55f32010-06-29 10:07:12 +02001966 /*
1967 * ->scheduled list can only be filled while a worker is
1968 * preparing to process a work or actually processing it.
1969 * Make sure nobody diddled with it while I was sleeping.
1970 */
1971 BUG_ON(!list_empty(&worker->scheduled));
1972
Tejun Heoe22bee72010-06-29 10:07:14 +02001973 /*
1974 * When control reaches this point, we're guaranteed to have
1975 * at least one idle worker or that someone else has already
1976 * assumed the manager role.
1977 */
1978 worker_clr_flags(worker, WORKER_PREP);
1979
1980 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02001981 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07001982 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02001983 struct work_struct, entry);
1984
1985 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
1986 /* optimization path, not strictly necessary */
1987 process_one_work(worker, work);
1988 if (unlikely(!list_empty(&worker->scheduled)))
1989 process_scheduled_works(worker);
1990 } else {
1991 move_linked_works(work, &worker->scheduled, NULL);
1992 process_scheduled_works(worker);
1993 }
Tejun Heo63d95a92012-07-12 14:46:37 -07001994 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02001995
Tejun Heoe22bee72010-06-29 10:07:14 +02001996 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02001997sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07001998 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02001999 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002000
Tejun Heoc8e55f32010-06-29 10:07:12 +02002001 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02002002 * gcwq->lock is held and there's no work to process and no
2003 * need to manage, sleep. Workers are woken up only while
2004 * holding gcwq->lock or from local cpu, so setting the
2005 * current state before releasing gcwq->lock is enough to
2006 * prevent losing any event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002007 */
2008 worker_enter_idle(worker);
2009 __set_current_state(TASK_INTERRUPTIBLE);
2010 spin_unlock_irq(&gcwq->lock);
2011 schedule();
2012 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013}
2014
Tejun Heoe22bee72010-06-29 10:07:14 +02002015/**
2016 * rescuer_thread - the rescuer thread function
2017 * @__wq: the associated workqueue
2018 *
2019 * Workqueue rescuer thread function. There's one rescuer for each
2020 * workqueue which has WQ_RESCUER set.
2021 *
2022 * Regular work processing on a gcwq may block trying to create a new
2023 * worker which uses GFP_KERNEL allocation which has slight chance of
2024 * developing into deadlock if some works currently on the same queue
2025 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2026 * the problem rescuer solves.
2027 *
2028 * When such condition is possible, the gcwq summons rescuers of all
2029 * workqueues which have works queued on the gcwq and let them process
2030 * those works so that forward progress can be guaranteed.
2031 *
2032 * This should happen rarely.
2033 */
2034static int rescuer_thread(void *__wq)
2035{
2036 struct workqueue_struct *wq = __wq;
2037 struct worker *rescuer = wq->rescuer;
2038 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heof3421792010-07-02 10:03:51 +02002039 bool is_unbound = wq->flags & WQ_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02002040 unsigned int cpu;
2041
2042 set_user_nice(current, RESCUER_NICE_LEVEL);
2043repeat:
2044 set_current_state(TASK_INTERRUPTIBLE);
2045
2046 if (kthread_should_stop())
2047 return 0;
2048
Tejun Heof3421792010-07-02 10:03:51 +02002049 /*
2050 * See whether any cpu is asking for help. Unbounded
2051 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
2052 */
Tejun Heof2e005a2010-07-20 15:59:09 +02002053 for_each_mayday_cpu(cpu, wq->mayday_mask) {
Tejun Heof3421792010-07-02 10:03:51 +02002054 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2055 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002056 struct worker_pool *pool = cwq->pool;
2057 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002058 struct work_struct *work, *n;
2059
2060 __set_current_state(TASK_RUNNING);
Tejun Heof2e005a2010-07-20 15:59:09 +02002061 mayday_clear_cpu(cpu, wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002062
2063 /* migrate to the target cpu if possible */
Tejun Heobd7bdd42012-07-12 14:46:37 -07002064 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002065 worker_maybe_bind_and_lock(rescuer);
2066
2067 /*
2068 * Slurp in all works issued via this workqueue and
2069 * process'em.
2070 */
2071 BUG_ON(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002072 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02002073 if (get_work_cwq(work) == cwq)
2074 move_linked_works(work, scheduled, &n);
2075
2076 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002077
2078 /*
2079 * Leave this gcwq. If keep_working() is %true, notify a
2080 * regular worker; otherwise, we end up with 0 concurrency
2081 * and stalling the execution.
2082 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002083 if (keep_working(pool))
2084 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002085
Tejun Heoe22bee72010-06-29 10:07:14 +02002086 spin_unlock_irq(&gcwq->lock);
2087 }
2088
2089 schedule();
2090 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091}
2092
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002093struct wq_barrier {
2094 struct work_struct work;
2095 struct completion done;
2096};
2097
2098static void wq_barrier_func(struct work_struct *work)
2099{
2100 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2101 complete(&barr->done);
2102}
2103
Tejun Heo4690c4a2010-06-29 10:07:10 +02002104/**
2105 * insert_wq_barrier - insert a barrier work
2106 * @cwq: cwq to insert barrier into
2107 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002108 * @target: target work to attach @barr to
2109 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002110 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002111 * @barr is linked to @target such that @barr is completed only after
2112 * @target finishes execution. Please note that the ordering
2113 * guarantee is observed only with respect to @target and on the local
2114 * cpu.
2115 *
2116 * Currently, a queued barrier can't be canceled. This is because
2117 * try_to_grab_pending() can't determine whether the work to be
2118 * grabbed is at the head of the queue and thus can't clear LINKED
2119 * flag of the previous work while there must be a valid next work
2120 * after a work with LINKED flag set.
2121 *
2122 * Note that when @worker is non-NULL, @target may be modified
2123 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002124 *
2125 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002126 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002127 */
Oleg Nesterov83c22522007-05-09 02:33:54 -07002128static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002129 struct wq_barrier *barr,
2130 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002131{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002132 struct list_head *head;
2133 unsigned int linked = 0;
2134
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002135 /*
Tejun Heo8b03ae32010-06-29 10:07:12 +02002136 * debugobject calls are safe here even with gcwq->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002137 * as we know for sure that this will not trigger any of the
2138 * checks and call back into the fixup functions where we
2139 * might deadlock.
2140 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002141 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002142 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002143 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002144
Tejun Heoaffee4b2010-06-29 10:07:12 +02002145 /*
2146 * If @target is currently being executed, schedule the
2147 * barrier to the worker; otherwise, put it after @target.
2148 */
2149 if (worker)
2150 head = worker->scheduled.next;
2151 else {
2152 unsigned long *bits = work_data_bits(target);
2153
2154 head = target->entry.next;
2155 /* there can already be other linked works, inherit and set */
2156 linked = *bits & WORK_STRUCT_LINKED;
2157 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2158 }
2159
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002160 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002161 insert_work(cwq, &barr->work, head,
2162 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002163}
2164
Tejun Heo73f53c42010-06-29 10:07:11 +02002165/**
2166 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2167 * @wq: workqueue being flushed
2168 * @flush_color: new flush color, < 0 for no-op
2169 * @work_color: new work color, < 0 for no-op
2170 *
2171 * Prepare cwqs for workqueue flushing.
2172 *
2173 * If @flush_color is non-negative, flush_color on all cwqs should be
2174 * -1. If no cwq has in-flight commands at the specified color, all
2175 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2176 * has in flight commands, its cwq->flush_color is set to
2177 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2178 * wakeup logic is armed and %true is returned.
2179 *
2180 * The caller should have initialized @wq->first_flusher prior to
2181 * calling this function with non-negative @flush_color. If
2182 * @flush_color is negative, no flush color update is done and %false
2183 * is returned.
2184 *
2185 * If @work_color is non-negative, all cwqs should have the same
2186 * work_color which is previous to @work_color and all will be
2187 * advanced to @work_color.
2188 *
2189 * CONTEXT:
2190 * mutex_lock(wq->flush_mutex).
2191 *
2192 * RETURNS:
2193 * %true if @flush_color >= 0 and there's something to flush. %false
2194 * otherwise.
2195 */
2196static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2197 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198{
Tejun Heo73f53c42010-06-29 10:07:11 +02002199 bool wait = false;
2200 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -07002201
Tejun Heo73f53c42010-06-29 10:07:11 +02002202 if (flush_color >= 0) {
2203 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2204 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002205 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002206
Tejun Heof3421792010-07-02 10:03:51 +02002207 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002208 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002209 struct global_cwq *gcwq = cwq->pool->gcwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002210
Tejun Heo8b03ae32010-06-29 10:07:12 +02002211 spin_lock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002212
2213 if (flush_color >= 0) {
2214 BUG_ON(cwq->flush_color != -1);
2215
2216 if (cwq->nr_in_flight[flush_color]) {
2217 cwq->flush_color = flush_color;
2218 atomic_inc(&wq->nr_cwqs_to_flush);
2219 wait = true;
2220 }
2221 }
2222
2223 if (work_color >= 0) {
2224 BUG_ON(work_color != work_next_color(cwq->work_color));
2225 cwq->work_color = work_color;
2226 }
2227
Tejun Heo8b03ae32010-06-29 10:07:12 +02002228 spin_unlock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002229 }
2230
2231 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2232 complete(&wq->first_flusher->done);
2233
2234 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235}
2236
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002237/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002239 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 *
2241 * Forces execution of the workqueue and blocks until its completion.
2242 * This is typically used in driver shutdown handlers.
2243 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002244 * We sleep until all works which were queued on entry have been handled,
2245 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002247void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248{
Tejun Heo73f53c42010-06-29 10:07:11 +02002249 struct wq_flusher this_flusher = {
2250 .list = LIST_HEAD_INIT(this_flusher.list),
2251 .flush_color = -1,
2252 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2253 };
2254 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002255
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002256 lock_map_acquire(&wq->lockdep_map);
2257 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002258
2259 mutex_lock(&wq->flush_mutex);
2260
2261 /*
2262 * Start-to-wait phase
2263 */
2264 next_color = work_next_color(wq->work_color);
2265
2266 if (next_color != wq->flush_color) {
2267 /*
2268 * Color space is not full. The current work_color
2269 * becomes our flush_color and work_color is advanced
2270 * by one.
2271 */
2272 BUG_ON(!list_empty(&wq->flusher_overflow));
2273 this_flusher.flush_color = wq->work_color;
2274 wq->work_color = next_color;
2275
2276 if (!wq->first_flusher) {
2277 /* no flush in progress, become the first flusher */
2278 BUG_ON(wq->flush_color != this_flusher.flush_color);
2279
2280 wq->first_flusher = &this_flusher;
2281
2282 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2283 wq->work_color)) {
2284 /* nothing to flush, done */
2285 wq->flush_color = next_color;
2286 wq->first_flusher = NULL;
2287 goto out_unlock;
2288 }
2289 } else {
2290 /* wait in queue */
2291 BUG_ON(wq->flush_color == this_flusher.flush_color);
2292 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2293 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2294 }
2295 } else {
2296 /*
2297 * Oops, color space is full, wait on overflow queue.
2298 * The next flush completion will assign us
2299 * flush_color and transfer to flusher_queue.
2300 */
2301 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2302 }
2303
2304 mutex_unlock(&wq->flush_mutex);
2305
2306 wait_for_completion(&this_flusher.done);
2307
2308 /*
2309 * Wake-up-and-cascade phase
2310 *
2311 * First flushers are responsible for cascading flushes and
2312 * handling overflow. Non-first flushers can simply return.
2313 */
2314 if (wq->first_flusher != &this_flusher)
2315 return;
2316
2317 mutex_lock(&wq->flush_mutex);
2318
Tejun Heo4ce48b32010-07-02 10:03:51 +02002319 /* we might have raced, check again with mutex held */
2320 if (wq->first_flusher != &this_flusher)
2321 goto out_unlock;
2322
Tejun Heo73f53c42010-06-29 10:07:11 +02002323 wq->first_flusher = NULL;
2324
2325 BUG_ON(!list_empty(&this_flusher.list));
2326 BUG_ON(wq->flush_color != this_flusher.flush_color);
2327
2328 while (true) {
2329 struct wq_flusher *next, *tmp;
2330
2331 /* complete all the flushers sharing the current flush color */
2332 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2333 if (next->flush_color != wq->flush_color)
2334 break;
2335 list_del_init(&next->list);
2336 complete(&next->done);
2337 }
2338
2339 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2340 wq->flush_color != work_next_color(wq->work_color));
2341
2342 /* this flush_color is finished, advance by one */
2343 wq->flush_color = work_next_color(wq->flush_color);
2344
2345 /* one color has been freed, handle overflow queue */
2346 if (!list_empty(&wq->flusher_overflow)) {
2347 /*
2348 * Assign the same color to all overflowed
2349 * flushers, advance work_color and append to
2350 * flusher_queue. This is the start-to-wait
2351 * phase for these overflowed flushers.
2352 */
2353 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2354 tmp->flush_color = wq->work_color;
2355
2356 wq->work_color = work_next_color(wq->work_color);
2357
2358 list_splice_tail_init(&wq->flusher_overflow,
2359 &wq->flusher_queue);
2360 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2361 }
2362
2363 if (list_empty(&wq->flusher_queue)) {
2364 BUG_ON(wq->flush_color != wq->work_color);
2365 break;
2366 }
2367
2368 /*
2369 * Need to flush more colors. Make the next flusher
2370 * the new first flusher and arm cwqs.
2371 */
2372 BUG_ON(wq->flush_color == wq->work_color);
2373 BUG_ON(wq->flush_color != next->flush_color);
2374
2375 list_del_init(&next->list);
2376 wq->first_flusher = next;
2377
2378 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2379 break;
2380
2381 /*
2382 * Meh... this color is already done, clear first
2383 * flusher and repeat cascading.
2384 */
2385 wq->first_flusher = NULL;
2386 }
2387
2388out_unlock:
2389 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390}
Dave Jonesae90dd52006-06-30 01:40:45 -04002391EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002393/**
2394 * drain_workqueue - drain a workqueue
2395 * @wq: workqueue to drain
2396 *
2397 * Wait until the workqueue becomes empty. While draining is in progress,
2398 * only chain queueing is allowed. IOW, only currently pending or running
2399 * work items on @wq can queue further work items on it. @wq is flushed
2400 * repeatedly until it becomes empty. The number of flushing is detemined
2401 * by the depth of chaining and should be relatively short. Whine if it
2402 * takes too long.
2403 */
2404void drain_workqueue(struct workqueue_struct *wq)
2405{
2406 unsigned int flush_cnt = 0;
2407 unsigned int cpu;
2408
2409 /*
2410 * __queue_work() needs to test whether there are drainers, is much
2411 * hotter than drain_workqueue() and already looks at @wq->flags.
2412 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2413 */
2414 spin_lock(&workqueue_lock);
2415 if (!wq->nr_drainers++)
2416 wq->flags |= WQ_DRAINING;
2417 spin_unlock(&workqueue_lock);
2418reflush:
2419 flush_workqueue(wq);
2420
2421 for_each_cwq_cpu(cpu, wq) {
2422 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002423 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002424
Tejun Heobd7bdd42012-07-12 14:46:37 -07002425 spin_lock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002426 drained = !cwq->nr_active && list_empty(&cwq->delayed_works);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002427 spin_unlock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002428
2429 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002430 continue;
2431
2432 if (++flush_cnt == 10 ||
2433 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
2434 pr_warning("workqueue %s: flush on destruction isn't complete after %u tries\n",
2435 wq->name, flush_cnt);
2436 goto reflush;
2437 }
2438
2439 spin_lock(&workqueue_lock);
2440 if (!--wq->nr_drainers)
2441 wq->flags &= ~WQ_DRAINING;
2442 spin_unlock(&workqueue_lock);
2443}
2444EXPORT_SYMBOL_GPL(drain_workqueue);
2445
Tejun Heobaf59022010-09-16 10:42:16 +02002446static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
2447 bool wait_executing)
2448{
2449 struct worker *worker = NULL;
2450 struct global_cwq *gcwq;
2451 struct cpu_workqueue_struct *cwq;
2452
2453 might_sleep();
2454 gcwq = get_work_gcwq(work);
2455 if (!gcwq)
2456 return false;
2457
2458 spin_lock_irq(&gcwq->lock);
2459 if (!list_empty(&work->entry)) {
2460 /*
2461 * See the comment near try_to_grab_pending()->smp_rmb().
2462 * If it was re-queued to a different gcwq under us, we
2463 * are not going to wait.
2464 */
2465 smp_rmb();
2466 cwq = get_work_cwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002467 if (unlikely(!cwq || gcwq != cwq->pool->gcwq))
Tejun Heobaf59022010-09-16 10:42:16 +02002468 goto already_gone;
2469 } else if (wait_executing) {
2470 worker = find_worker_executing_work(gcwq, work);
2471 if (!worker)
2472 goto already_gone;
2473 cwq = worker->current_cwq;
2474 } else
2475 goto already_gone;
2476
2477 insert_wq_barrier(cwq, barr, work, worker);
2478 spin_unlock_irq(&gcwq->lock);
2479
Tejun Heoe1594892011-01-09 23:32:15 +01002480 /*
2481 * If @max_active is 1 or rescuer is in use, flushing another work
2482 * item on the same workqueue may lead to deadlock. Make sure the
2483 * flusher is not running on the same workqueue by verifying write
2484 * access.
2485 */
2486 if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER)
2487 lock_map_acquire(&cwq->wq->lockdep_map);
2488 else
2489 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heobaf59022010-09-16 10:42:16 +02002490 lock_map_release(&cwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002491
Tejun Heobaf59022010-09-16 10:42:16 +02002492 return true;
2493already_gone:
2494 spin_unlock_irq(&gcwq->lock);
2495 return false;
2496}
2497
Oleg Nesterovdb700892008-07-25 01:47:49 -07002498/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002499 * flush_work - wait for a work to finish executing the last queueing instance
2500 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002501 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002502 * Wait until @work has finished execution. This function considers
2503 * only the last queueing instance of @work. If @work has been
2504 * enqueued across different CPUs on a non-reentrant workqueue or on
2505 * multiple workqueues, @work might still be executing on return on
2506 * some of the CPUs from earlier queueing.
Oleg Nesterova67da702008-07-25 01:47:52 -07002507 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002508 * If @work was queued only on a non-reentrant, ordered or unbound
2509 * workqueue, @work is guaranteed to be idle on return if it hasn't
2510 * been requeued since flush started.
2511 *
2512 * RETURNS:
2513 * %true if flush_work() waited for the work to finish execution,
2514 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002515 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002516bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002517{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002518 struct wq_barrier barr;
2519
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002520 lock_map_acquire(&work->lockdep_map);
2521 lock_map_release(&work->lockdep_map);
2522
Tejun Heobaf59022010-09-16 10:42:16 +02002523 if (start_flush_work(work, &barr, true)) {
2524 wait_for_completion(&barr.done);
2525 destroy_work_on_stack(&barr.work);
2526 return true;
2527 } else
2528 return false;
Oleg Nesterovdb700892008-07-25 01:47:49 -07002529}
2530EXPORT_SYMBOL_GPL(flush_work);
2531
Tejun Heo401a8d02010-09-16 10:36:00 +02002532static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work)
2533{
2534 struct wq_barrier barr;
2535 struct worker *worker;
2536
2537 spin_lock_irq(&gcwq->lock);
2538
2539 worker = find_worker_executing_work(gcwq, work);
2540 if (unlikely(worker))
2541 insert_wq_barrier(worker->current_cwq, &barr, work, worker);
2542
2543 spin_unlock_irq(&gcwq->lock);
2544
2545 if (unlikely(worker)) {
2546 wait_for_completion(&barr.done);
2547 destroy_work_on_stack(&barr.work);
2548 return true;
2549 } else
2550 return false;
2551}
2552
2553static bool wait_on_work(struct work_struct *work)
2554{
2555 bool ret = false;
2556 int cpu;
2557
2558 might_sleep();
2559
2560 lock_map_acquire(&work->lockdep_map);
2561 lock_map_release(&work->lockdep_map);
2562
2563 for_each_gcwq_cpu(cpu)
2564 ret |= wait_on_cpu_work(get_gcwq(cpu), work);
2565 return ret;
2566}
2567
Tejun Heo09383492010-09-16 10:48:29 +02002568/**
2569 * flush_work_sync - wait until a work has finished execution
2570 * @work: the work to flush
2571 *
2572 * Wait until @work has finished execution. On return, it's
2573 * guaranteed that all queueing instances of @work which happened
2574 * before this function is called are finished. In other words, if
2575 * @work hasn't been requeued since this function was called, @work is
2576 * guaranteed to be idle on return.
2577 *
2578 * RETURNS:
2579 * %true if flush_work_sync() waited for the work to finish execution,
2580 * %false if it was already idle.
2581 */
2582bool flush_work_sync(struct work_struct *work)
2583{
2584 struct wq_barrier barr;
2585 bool pending, waited;
2586
2587 /* we'll wait for executions separately, queue barr only if pending */
2588 pending = start_flush_work(work, &barr, false);
2589
2590 /* wait for executions to finish */
2591 waited = wait_on_work(work);
2592
2593 /* wait for the pending one */
2594 if (pending) {
2595 wait_for_completion(&barr.done);
2596 destroy_work_on_stack(&barr.work);
2597 }
2598
2599 return pending || waited;
2600}
2601EXPORT_SYMBOL_GPL(flush_work_sync);
2602
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002603/*
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002604 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002605 * so this work can't be re-armed in any way.
2606 */
2607static int try_to_grab_pending(struct work_struct *work)
2608{
Tejun Heo8b03ae32010-06-29 10:07:12 +02002609 struct global_cwq *gcwq;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002610 int ret = -1;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002611
Tejun Heo22df02b2010-06-29 10:07:10 +02002612 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002613 return 0;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002614
2615 /*
2616 * The queueing is in progress, or it is already queued. Try to
2617 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
2618 */
Tejun Heo7a22ad72010-06-29 10:07:13 +02002619 gcwq = get_work_gcwq(work);
2620 if (!gcwq)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002621 return ret;
2622
Tejun Heo8b03ae32010-06-29 10:07:12 +02002623 spin_lock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002624 if (!list_empty(&work->entry)) {
2625 /*
Tejun Heo7a22ad72010-06-29 10:07:13 +02002626 * This work is queued, but perhaps we locked the wrong gcwq.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002627 * In that case we must see the new value after rmb(), see
2628 * insert_work()->wmb().
2629 */
2630 smp_rmb();
Tejun Heo7a22ad72010-06-29 10:07:13 +02002631 if (gcwq == get_work_gcwq(work)) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002632 debug_work_deactivate(work);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002633 list_del_init(&work->entry);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002634 cwq_dec_nr_in_flight(get_work_cwq(work),
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02002635 get_work_color(work),
2636 *work_data_bits(work) & WORK_STRUCT_DELAYED);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002637 ret = 1;
2638 }
2639 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02002640 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002641
2642 return ret;
2643}
2644
Tejun Heo401a8d02010-09-16 10:36:00 +02002645static bool __cancel_work_timer(struct work_struct *work,
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002646 struct timer_list* timer)
2647{
2648 int ret;
2649
2650 do {
2651 ret = (timer && likely(del_timer(timer)));
2652 if (!ret)
2653 ret = try_to_grab_pending(work);
2654 wait_on_work(work);
2655 } while (unlikely(ret < 0));
2656
Tejun Heo7a22ad72010-06-29 10:07:13 +02002657 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002658 return ret;
2659}
2660
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002661/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002662 * cancel_work_sync - cancel a work and wait for it to finish
2663 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002664 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002665 * Cancel @work and wait for its execution to finish. This function
2666 * can be used even if the work re-queues itself or migrates to
2667 * another workqueue. On return from this function, @work is
2668 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002669 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002670 * cancel_work_sync(&delayed_work->work) must not be used for
2671 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002672 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002673 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002674 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002675 *
2676 * RETURNS:
2677 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002678 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002679bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002680{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002681 return __cancel_work_timer(work, NULL);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002682}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002683EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002684
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002685/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002686 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2687 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002688 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002689 * Delayed timer is cancelled and the pending work is queued for
2690 * immediate execution. Like flush_work(), this function only
2691 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002692 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002693 * RETURNS:
2694 * %true if flush_work() waited for the work to finish execution,
2695 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002696 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002697bool flush_delayed_work(struct delayed_work *dwork)
2698{
2699 if (del_timer_sync(&dwork->timer))
2700 __queue_work(raw_smp_processor_id(),
2701 get_work_cwq(&dwork->work)->wq, &dwork->work);
2702 return flush_work(&dwork->work);
2703}
2704EXPORT_SYMBOL(flush_delayed_work);
2705
2706/**
Tejun Heo09383492010-09-16 10:48:29 +02002707 * flush_delayed_work_sync - wait for a dwork to finish
2708 * @dwork: the delayed work to flush
2709 *
2710 * Delayed timer is cancelled and the pending work is queued for
2711 * execution immediately. Other than timer handling, its behavior
2712 * is identical to flush_work_sync().
2713 *
2714 * RETURNS:
2715 * %true if flush_work_sync() waited for the work to finish execution,
2716 * %false if it was already idle.
2717 */
2718bool flush_delayed_work_sync(struct delayed_work *dwork)
2719{
2720 if (del_timer_sync(&dwork->timer))
2721 __queue_work(raw_smp_processor_id(),
2722 get_work_cwq(&dwork->work)->wq, &dwork->work);
2723 return flush_work_sync(&dwork->work);
2724}
2725EXPORT_SYMBOL(flush_delayed_work_sync);
2726
2727/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002728 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2729 * @dwork: the delayed work cancel
2730 *
2731 * This is cancel_work_sync() for delayed works.
2732 *
2733 * RETURNS:
2734 * %true if @dwork was pending, %false otherwise.
2735 */
2736bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002737{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002738 return __cancel_work_timer(&dwork->work, &dwork->timer);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002739}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002740EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002742/**
2743 * schedule_work - put work task in global workqueue
2744 * @work: job to be done
2745 *
Bart Van Assche5b0f437d2009-07-30 19:00:53 +02002746 * Returns zero if @work was already on the kernel-global workqueue and
2747 * non-zero otherwise.
2748 *
2749 * This puts a job in the kernel-global workqueue if it was not already
2750 * queued and leaves it in the same position on the kernel-global
2751 * workqueue otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002752 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002753int schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754{
Tejun Heod320c032010-06-29 10:07:14 +02002755 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756}
Dave Jonesae90dd52006-06-30 01:40:45 -04002757EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
Zhang Ruic1a220e2008-07-23 21:28:39 -07002759/*
2760 * schedule_work_on - put work task on a specific cpu
2761 * @cpu: cpu to put the work task on
2762 * @work: job to be done
2763 *
2764 * This puts a job on a specific cpu
2765 */
2766int schedule_work_on(int cpu, struct work_struct *work)
2767{
Tejun Heod320c032010-06-29 10:07:14 +02002768 return queue_work_on(cpu, system_wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07002769}
2770EXPORT_SYMBOL(schedule_work_on);
2771
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002772/**
2773 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +00002774 * @dwork: job to be done
2775 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002776 *
2777 * After waiting for a given time this puts a job in the kernel-global
2778 * workqueue.
2779 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002780int schedule_delayed_work(struct delayed_work *dwork,
Ingo Molnar82f67cd2007-02-16 01:28:13 -08002781 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782{
Tejun Heod320c032010-06-29 10:07:14 +02002783 return queue_delayed_work(system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784}
Dave Jonesae90dd52006-06-30 01:40:45 -04002785EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002787/**
2788 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
2789 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +00002790 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002791 * @delay: number of jiffies to wait
2792 *
2793 * After waiting for a given time this puts a job in the kernel-global
2794 * workqueue on the specified CPU.
2795 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +00002797 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798{
Tejun Heod320c032010-06-29 10:07:14 +02002799 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800}
Dave Jonesae90dd52006-06-30 01:40:45 -04002801EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
Andrew Mortonb6136772006-06-25 05:47:49 -07002803/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002804 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002805 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002806 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002807 * schedule_on_each_cpu() executes @func on each online CPU using the
2808 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002809 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002810 *
2811 * RETURNS:
2812 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002813 */
David Howells65f27f32006-11-22 14:55:48 +00002814int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002815{
2816 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002817 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002818
Andrew Mortonb6136772006-06-25 05:47:49 -07002819 works = alloc_percpu(struct work_struct);
2820 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002821 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002822
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002823 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002824
Christoph Lameter15316ba2006-01-08 01:00:43 -08002825 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002826 struct work_struct *work = per_cpu_ptr(works, cpu);
2827
2828 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002829 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002830 }
Tejun Heo93981802009-11-17 14:06:20 -08002831
2832 for_each_online_cpu(cpu)
2833 flush_work(per_cpu_ptr(works, cpu));
2834
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002835 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002836 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002837 return 0;
2838}
2839
Alan Sterneef6a7d2010-02-12 17:39:21 +09002840/**
2841 * flush_scheduled_work - ensure that any scheduled work has run to completion.
2842 *
2843 * Forces execution of the kernel-global workqueue and blocks until its
2844 * completion.
2845 *
2846 * Think twice before calling this function! It's very easy to get into
2847 * trouble if you don't take great care. Either of the following situations
2848 * will lead to deadlock:
2849 *
2850 * One of the work items currently on the workqueue needs to acquire
2851 * a lock held by your code or its caller.
2852 *
2853 * Your code is running in the context of a work routine.
2854 *
2855 * They will be detected by lockdep when they occur, but the first might not
2856 * occur very often. It depends on what work items are on the workqueue and
2857 * what locks they need, which you have no control over.
2858 *
2859 * In most situations flushing the entire workqueue is overkill; you merely
2860 * need to know that a particular work item isn't queued and isn't running.
2861 * In such cases you should use cancel_delayed_work_sync() or
2862 * cancel_work_sync() instead.
2863 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864void flush_scheduled_work(void)
2865{
Tejun Heod320c032010-06-29 10:07:14 +02002866 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867}
Dave Jonesae90dd52006-06-30 01:40:45 -04002868EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
2870/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06002871 * execute_in_process_context - reliably execute the routine with user context
2872 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06002873 * @ew: guaranteed storage for the execute work structure (must
2874 * be available when the work executes)
2875 *
2876 * Executes the function immediately if process context is available,
2877 * otherwise schedules the function for delayed execution.
2878 *
2879 * Returns: 0 - function was executed
2880 * 1 - function was scheduled for execution
2881 */
David Howells65f27f32006-11-22 14:55:48 +00002882int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06002883{
2884 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00002885 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002886 return 0;
2887 }
2888
David Howells65f27f32006-11-22 14:55:48 +00002889 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002890 schedule_work(&ew->work);
2891
2892 return 1;
2893}
2894EXPORT_SYMBOL_GPL(execute_in_process_context);
2895
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896int keventd_up(void)
2897{
Tejun Heod320c032010-06-29 10:07:14 +02002898 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899}
2900
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002901static int alloc_cwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902{
Oleg Nesterov3af244332007-05-09 02:34:09 -07002903 /*
Tejun Heo0f900042010-06-29 10:07:11 +02002904 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
2905 * Make sure that the alignment isn't lower than that of
2906 * unsigned long long.
Oleg Nesterov3af244332007-05-09 02:34:09 -07002907 */
Tejun Heo0f900042010-06-29 10:07:11 +02002908 const size_t size = sizeof(struct cpu_workqueue_struct);
2909 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
2910 __alignof__(unsigned long long));
Oleg Nesterov3af244332007-05-09 02:34:09 -07002911
Lai Jiangshane06ffa12012-03-09 18:03:20 +08002912 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02002913 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
Tejun Heo931ac772010-07-20 11:07:48 +02002914 else {
Tejun Heof3421792010-07-02 10:03:51 +02002915 void *ptr;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01002916
Tejun Heof3421792010-07-02 10:03:51 +02002917 /*
2918 * Allocate enough room to align cwq and put an extra
2919 * pointer at the end pointing back to the originally
2920 * allocated pointer which will be used for free.
2921 */
2922 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
2923 if (ptr) {
2924 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
2925 *(void **)(wq->cpu_wq.single + 1) = ptr;
2926 }
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002927 }
Tejun Heof3421792010-07-02 10:03:51 +02002928
Tejun Heo0415b00d12011-03-24 18:50:09 +01002929 /* just in case, make sure it's actually aligned */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002930 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
2931 return wq->cpu_wq.v ? 0 : -ENOMEM;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002932}
2933
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002934static void free_cwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002935{
Lai Jiangshane06ffa12012-03-09 18:03:20 +08002936 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02002937 free_percpu(wq->cpu_wq.pcpu);
2938 else if (wq->cpu_wq.single) {
2939 /* the pointer to free is stored right after the cwq */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002940 kfree(*(void **)(wq->cpu_wq.single + 1));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002941 }
2942}
2943
Tejun Heof3421792010-07-02 10:03:51 +02002944static int wq_clamp_max_active(int max_active, unsigned int flags,
2945 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002946{
Tejun Heof3421792010-07-02 10:03:51 +02002947 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
2948
2949 if (max_active < 1 || max_active > lim)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002950 printk(KERN_WARNING "workqueue: max_active %d requested for %s "
2951 "is out of range, clamping between %d and %d\n",
Tejun Heof3421792010-07-02 10:03:51 +02002952 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002953
Tejun Heof3421792010-07-02 10:03:51 +02002954 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002955}
2956
Tejun Heob196be82012-01-10 15:11:35 -08002957struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02002958 unsigned int flags,
2959 int max_active,
2960 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08002961 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07002962{
Tejun Heob196be82012-01-10 15:11:35 -08002963 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002964 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02002965 unsigned int cpu;
Tejun Heob196be82012-01-10 15:11:35 -08002966 size_t namelen;
2967
2968 /* determine namelen, allocate wq and format name */
2969 va_start(args, lock_name);
2970 va_copy(args1, args);
2971 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
2972
2973 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
2974 if (!wq)
2975 goto err;
2976
2977 vsnprintf(wq->name, namelen, fmt, args1);
2978 va_end(args);
2979 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002980
Tejun Heof3421792010-07-02 10:03:51 +02002981 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02002982 * Workqueues which may be used during memory reclaim should
2983 * have a rescuer to guarantee forward progress.
2984 */
2985 if (flags & WQ_MEM_RECLAIM)
2986 flags |= WQ_RESCUER;
2987
Tejun Heod320c032010-06-29 10:07:14 +02002988 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08002989 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002990
Tejun Heob196be82012-01-10 15:11:35 -08002991 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02002992 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02002993 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02002994 mutex_init(&wq->flush_mutex);
2995 atomic_set(&wq->nr_cwqs_to_flush, 0);
2996 INIT_LIST_HEAD(&wq->flusher_queue);
2997 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07002998
Johannes Bergeb13ba82008-01-16 09:51:58 +01002999 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003000 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003001
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003002 if (alloc_cwqs(wq) < 0)
3003 goto err;
3004
Tejun Heof3421792010-07-02 10:03:51 +02003005 for_each_cwq_cpu(cpu, wq) {
Tejun Heo15376632010-06-29 10:07:11 +02003006 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003007 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo32704762012-07-13 22:16:45 -07003008 int pool_idx = (bool)(flags & WQ_HIGHPRI);
Tejun Heo15376632010-06-29 10:07:11 +02003009
Tejun Heo0f900042010-06-29 10:07:11 +02003010 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heo32704762012-07-13 22:16:45 -07003011 cwq->pool = &gcwq->pools[pool_idx];
Tejun Heoc34056a2010-06-29 10:07:11 +02003012 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02003013 cwq->flush_color = -1;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003014 cwq->max_active = max_active;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003015 INIT_LIST_HEAD(&cwq->delayed_works);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003016 }
3017
Tejun Heoe22bee72010-06-29 10:07:14 +02003018 if (flags & WQ_RESCUER) {
3019 struct worker *rescuer;
3020
Tejun Heof2e005a2010-07-20 15:59:09 +02003021 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
Tejun Heoe22bee72010-06-29 10:07:14 +02003022 goto err;
3023
3024 wq->rescuer = rescuer = alloc_worker();
3025 if (!rescuer)
3026 goto err;
3027
Tejun Heob196be82012-01-10 15:11:35 -08003028 rescuer->task = kthread_create(rescuer_thread, wq, "%s",
3029 wq->name);
Tejun Heoe22bee72010-06-29 10:07:14 +02003030 if (IS_ERR(rescuer->task))
3031 goto err;
3032
Tejun Heoe22bee72010-06-29 10:07:14 +02003033 rescuer->task->flags |= PF_THREAD_BOUND;
3034 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003035 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003036
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003037 /*
3038 * workqueue_lock protects global freeze state and workqueues
3039 * list. Grab it, set max_active accordingly and add the new
3040 * workqueue to workqueues list.
3041 */
Tejun Heo15376632010-06-29 10:07:11 +02003042 spin_lock(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003043
Tejun Heo58a69cb2011-02-16 09:25:31 +01003044 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heof3421792010-07-02 10:03:51 +02003045 for_each_cwq_cpu(cpu, wq)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003046 get_cwq(cpu, wq)->max_active = 0;
3047
Tejun Heo15376632010-06-29 10:07:11 +02003048 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003049
Tejun Heo15376632010-06-29 10:07:11 +02003050 spin_unlock(&workqueue_lock);
3051
Oleg Nesterov3af244332007-05-09 02:34:09 -07003052 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02003053err:
3054 if (wq) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003055 free_cwqs(wq);
Tejun Heof2e005a2010-07-20 15:59:09 +02003056 free_mayday_mask(wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02003057 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003058 kfree(wq);
3059 }
3060 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003061}
Tejun Heod320c032010-06-29 10:07:14 +02003062EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003063
3064/**
3065 * destroy_workqueue - safely terminate a workqueue
3066 * @wq: target workqueue
3067 *
3068 * Safely destroy a workqueue. All work currently pending will be done first.
3069 */
3070void destroy_workqueue(struct workqueue_struct *wq)
3071{
Tejun Heoc8e55f32010-06-29 10:07:12 +02003072 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003073
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003074 /* drain it before proceeding with destruction */
3075 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003076
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003077 /*
3078 * wq list is used to freeze wq, remove from list after
3079 * flushing is complete in case freeze races us.
3080 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003081 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07003082 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003083 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003084
Tejun Heoe22bee72010-06-29 10:07:14 +02003085 /* sanity check */
Tejun Heof3421792010-07-02 10:03:51 +02003086 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02003087 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3088 int i;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003089
Tejun Heo73f53c42010-06-29 10:07:11 +02003090 for (i = 0; i < WORK_NR_COLORS; i++)
3091 BUG_ON(cwq->nr_in_flight[i]);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003092 BUG_ON(cwq->nr_active);
3093 BUG_ON(!list_empty(&cwq->delayed_works));
Tejun Heo73f53c42010-06-29 10:07:11 +02003094 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003095
Tejun Heoe22bee72010-06-29 10:07:14 +02003096 if (wq->flags & WQ_RESCUER) {
3097 kthread_stop(wq->rescuer->task);
Tejun Heof2e005a2010-07-20 15:59:09 +02003098 free_mayday_mask(wq->mayday_mask);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003099 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003100 }
3101
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003102 free_cwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003103 kfree(wq);
3104}
3105EXPORT_SYMBOL_GPL(destroy_workqueue);
3106
Tejun Heodcd989c2010-06-29 10:07:14 +02003107/**
3108 * workqueue_set_max_active - adjust max_active of a workqueue
3109 * @wq: target workqueue
3110 * @max_active: new max_active value.
3111 *
3112 * Set max_active of @wq to @max_active.
3113 *
3114 * CONTEXT:
3115 * Don't call from IRQ context.
3116 */
3117void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3118{
3119 unsigned int cpu;
3120
Tejun Heof3421792010-07-02 10:03:51 +02003121 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003122
3123 spin_lock(&workqueue_lock);
3124
3125 wq->saved_max_active = max_active;
3126
Tejun Heof3421792010-07-02 10:03:51 +02003127 for_each_cwq_cpu(cpu, wq) {
Tejun Heodcd989c2010-06-29 10:07:14 +02003128 struct global_cwq *gcwq = get_gcwq(cpu);
3129
3130 spin_lock_irq(&gcwq->lock);
3131
Tejun Heo58a69cb2011-02-16 09:25:31 +01003132 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heodcd989c2010-06-29 10:07:14 +02003133 !(gcwq->flags & GCWQ_FREEZING))
3134 get_cwq(gcwq->cpu, wq)->max_active = max_active;
3135
3136 spin_unlock_irq(&gcwq->lock);
3137 }
3138
3139 spin_unlock(&workqueue_lock);
3140}
3141EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3142
3143/**
3144 * workqueue_congested - test whether a workqueue is congested
3145 * @cpu: CPU in question
3146 * @wq: target workqueue
3147 *
3148 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3149 * no synchronization around this function and the test result is
3150 * unreliable and only useful as advisory hints or for debugging.
3151 *
3152 * RETURNS:
3153 * %true if congested, %false otherwise.
3154 */
3155bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3156{
3157 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3158
3159 return !list_empty(&cwq->delayed_works);
3160}
3161EXPORT_SYMBOL_GPL(workqueue_congested);
3162
3163/**
3164 * work_cpu - return the last known associated cpu for @work
3165 * @work: the work of interest
3166 *
3167 * RETURNS:
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003168 * CPU number if @work was ever queued. WORK_CPU_NONE otherwise.
Tejun Heodcd989c2010-06-29 10:07:14 +02003169 */
3170unsigned int work_cpu(struct work_struct *work)
3171{
3172 struct global_cwq *gcwq = get_work_gcwq(work);
3173
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003174 return gcwq ? gcwq->cpu : WORK_CPU_NONE;
Tejun Heodcd989c2010-06-29 10:07:14 +02003175}
3176EXPORT_SYMBOL_GPL(work_cpu);
3177
3178/**
3179 * work_busy - test whether a work is currently pending or running
3180 * @work: the work to be tested
3181 *
3182 * Test whether @work is currently pending or running. There is no
3183 * synchronization around this function and the test result is
3184 * unreliable and only useful as advisory hints or for debugging.
3185 * Especially for reentrant wqs, the pending state might hide the
3186 * running state.
3187 *
3188 * RETURNS:
3189 * OR'd bitmask of WORK_BUSY_* bits.
3190 */
3191unsigned int work_busy(struct work_struct *work)
3192{
3193 struct global_cwq *gcwq = get_work_gcwq(work);
3194 unsigned long flags;
3195 unsigned int ret = 0;
3196
3197 if (!gcwq)
3198 return false;
3199
3200 spin_lock_irqsave(&gcwq->lock, flags);
3201
3202 if (work_pending(work))
3203 ret |= WORK_BUSY_PENDING;
3204 if (find_worker_executing_work(gcwq, work))
3205 ret |= WORK_BUSY_RUNNING;
3206
3207 spin_unlock_irqrestore(&gcwq->lock, flags);
3208
3209 return ret;
3210}
3211EXPORT_SYMBOL_GPL(work_busy);
3212
Tejun Heodb7bccf2010-06-29 10:07:12 +02003213/*
3214 * CPU hotplug.
3215 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003216 * There are two challenges in supporting CPU hotplug. Firstly, there
3217 * are a lot of assumptions on strong associations among work, cwq and
3218 * gcwq which make migrating pending and scheduled works very
3219 * difficult to implement without impacting hot paths. Secondly,
3220 * gcwqs serve mix of short, long and very long running works making
3221 * blocked draining impractical.
3222 *
Tejun Heo403c8212012-07-17 12:39:27 -07003223 * This is solved by allowing a gcwq to be detached from CPU, running it
3224 * with unbound workers and allowing it to be reattached later if the cpu
3225 * comes back online. A separate thread is created to govern a gcwq in
3226 * such state and is called the trustee of the gcwq.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003227 *
3228 * Trustee states and their descriptions.
3229 *
3230 * START Command state used on startup. On CPU_DOWN_PREPARE, a
3231 * new trustee is started with this state.
3232 *
3233 * IN_CHARGE Once started, trustee will enter this state after
Tejun Heoe22bee72010-06-29 10:07:14 +02003234 * assuming the manager role and making all existing
3235 * workers rogue. DOWN_PREPARE waits for trustee to
3236 * enter this state. After reaching IN_CHARGE, trustee
3237 * tries to execute the pending worklist until it's empty
3238 * and the state is set to BUTCHER, or the state is set
3239 * to RELEASE.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003240 *
3241 * BUTCHER Command state which is set by the cpu callback after
3242 * the cpu has went down. Once this state is set trustee
3243 * knows that there will be no new works on the worklist
3244 * and once the worklist is empty it can proceed to
3245 * killing idle workers.
3246 *
3247 * RELEASE Command state which is set by the cpu callback if the
3248 * cpu down has been canceled or it has come online
3249 * again. After recognizing this state, trustee stops
Tejun Heoe22bee72010-06-29 10:07:14 +02003250 * trying to drain or butcher and clears ROGUE, rebinds
3251 * all remaining workers back to the cpu and releases
3252 * manager role.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003253 *
3254 * DONE Trustee will enter this state after BUTCHER or RELEASE
3255 * is complete.
3256 *
3257 * trustee CPU draining
3258 * took over down complete
3259 * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE
3260 * | | ^
3261 * | CPU is back online v return workers |
3262 * ----------------> RELEASE --------------
3263 */
3264
Tejun Heo60373152012-07-17 12:39:27 -07003265/* claim manager positions of all pools */
3266static void gcwq_claim_management(struct global_cwq *gcwq)
3267{
3268 struct worker_pool *pool;
3269
3270 for_each_worker_pool(pool, gcwq)
3271 mutex_lock_nested(&pool->manager_mutex, pool - gcwq->pools);
3272}
3273
3274/* release manager positions */
3275static void gcwq_release_management(struct global_cwq *gcwq)
3276{
3277 struct worker_pool *pool;
3278
3279 for_each_worker_pool(pool, gcwq)
3280 mutex_unlock(&pool->manager_mutex);
3281}
3282
Tejun Heodb7bccf2010-06-29 10:07:12 +02003283/**
3284 * trustee_wait_event_timeout - timed event wait for trustee
3285 * @cond: condition to wait for
3286 * @timeout: timeout in jiffies
3287 *
3288 * wait_event_timeout() for trustee to use. Handles locking and
3289 * checks for RELEASE request.
3290 *
3291 * CONTEXT:
3292 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3293 * multiple times. To be used by trustee.
3294 *
3295 * RETURNS:
3296 * Positive indicating left time if @cond is satisfied, 0 if timed
3297 * out, -1 if canceled.
3298 */
3299#define trustee_wait_event_timeout(cond, timeout) ({ \
3300 long __ret = (timeout); \
3301 while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) && \
3302 __ret) { \
3303 spin_unlock_irq(&gcwq->lock); \
3304 __wait_event_timeout(gcwq->trustee_wait, (cond) || \
3305 (gcwq->trustee_state == TRUSTEE_RELEASE), \
3306 __ret); \
3307 spin_lock_irq(&gcwq->lock); \
3308 } \
3309 gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret); \
3310})
3311
3312/**
3313 * trustee_wait_event - event wait for trustee
3314 * @cond: condition to wait for
3315 *
3316 * wait_event() for trustee to use. Automatically handles locking and
3317 * checks for CANCEL request.
3318 *
3319 * CONTEXT:
3320 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3321 * multiple times. To be used by trustee.
3322 *
3323 * RETURNS:
3324 * 0 if @cond is satisfied, -1 if canceled.
3325 */
3326#define trustee_wait_event(cond) ({ \
3327 long __ret1; \
3328 __ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\
3329 __ret1 < 0 ? -1 : 0; \
3330})
3331
Tejun Heo4ce62e92012-07-13 22:16:44 -07003332static bool gcwq_has_idle_workers(struct global_cwq *gcwq)
3333{
3334 struct worker_pool *pool;
3335
3336 for_each_worker_pool(pool, gcwq)
3337 if (!list_empty(&pool->idle_list))
3338 return true;
3339 return false;
3340}
3341
Tejun Heodb7bccf2010-06-29 10:07:12 +02003342static int __cpuinit trustee_thread(void *__gcwq)
3343{
3344 struct global_cwq *gcwq = __gcwq;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003345 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003346 struct worker *worker;
Tejun Heoe22bee72010-06-29 10:07:14 +02003347 struct work_struct *work;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003348 struct hlist_node *pos;
Tejun Heoe22bee72010-06-29 10:07:14 +02003349 long rc;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003350 int i;
3351
3352 BUG_ON(gcwq->cpu != smp_processor_id());
3353
Tejun Heo60373152012-07-17 12:39:27 -07003354 gcwq_claim_management(gcwq);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003355 spin_lock_irq(&gcwq->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02003356
Tejun Heof2d5a0e2012-07-17 12:39:26 -07003357 /*
3358 * We've claimed all manager positions. Make all workers unbound
3359 * and set DISASSOCIATED. Before this, all workers except for the
3360 * ones which are still executing works from before the last CPU
3361 * down must be on the cpu. After this, they may become diasporas.
3362 */
Tejun Heo60373152012-07-17 12:39:27 -07003363 for_each_worker_pool(pool, gcwq)
Tejun Heo4ce62e92012-07-13 22:16:44 -07003364 list_for_each_entry(worker, &pool->idle_list, entry)
Tejun Heo403c8212012-07-17 12:39:27 -07003365 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003366
3367 for_each_busy_worker(worker, i, pos, gcwq)
Tejun Heo403c8212012-07-17 12:39:27 -07003368 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003369
Tejun Heof2d5a0e2012-07-17 12:39:26 -07003370 gcwq->flags |= GCWQ_DISASSOCIATED;
3371
Tejun Heodb7bccf2010-06-29 10:07:12 +02003372 /*
Tejun Heo403c8212012-07-17 12:39:27 -07003373 * Call schedule() so that we cross rq->lock and thus can guarantee
3374 * sched callbacks see the unbound flag. This is necessary as
3375 * scheduler callbacks may be invoked from other cpus.
Tejun Heoe22bee72010-06-29 10:07:14 +02003376 */
3377 spin_unlock_irq(&gcwq->lock);
3378 schedule();
3379 spin_lock_irq(&gcwq->lock);
3380
3381 /*
Tejun Heocb444762010-07-02 10:03:50 +02003382 * Sched callbacks are disabled now. Zap nr_running. After
3383 * this, nr_running stays zero and need_more_worker() and
3384 * keep_working() are always true as long as the worklist is
3385 * not empty.
Tejun Heoe22bee72010-06-29 10:07:14 +02003386 */
Tejun Heo4ce62e92012-07-13 22:16:44 -07003387 for_each_worker_pool(pool, gcwq)
3388 atomic_set(get_pool_nr_running(pool), 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003389
3390 spin_unlock_irq(&gcwq->lock);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003391 for_each_worker_pool(pool, gcwq)
3392 del_timer_sync(&pool->idle_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003393 spin_lock_irq(&gcwq->lock);
3394
3395 /*
Tejun Heodb7bccf2010-06-29 10:07:12 +02003396 * We're now in charge. Notify and proceed to drain. We need
3397 * to keep the gcwq running during the whole CPU down
3398 * procedure as other cpu hotunplug callbacks may need to
3399 * flush currently running tasks.
3400 */
3401 gcwq->trustee_state = TRUSTEE_IN_CHARGE;
3402 wake_up_all(&gcwq->trustee_wait);
3403
3404 /*
3405 * The original cpu is in the process of dying and may go away
3406 * anytime now. When that happens, we and all workers would
Tejun Heoe22bee72010-06-29 10:07:14 +02003407 * be migrated to other cpus. Try draining any left work. We
3408 * want to get it over with ASAP - spam rescuers, wake up as
3409 * many idlers as necessary and create new ones till the
3410 * worklist is empty. Note that if the gcwq is frozen, there
Tejun Heo58a69cb2011-02-16 09:25:31 +01003411 * may be frozen works in freezable cwqs. Don't declare
Tejun Heoe22bee72010-06-29 10:07:14 +02003412 * completion while frozen.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003413 */
Tejun Heo4ce62e92012-07-13 22:16:44 -07003414 while (true) {
3415 bool busy = false;
Tejun Heoe22bee72010-06-29 10:07:14 +02003416
Tejun Heo4ce62e92012-07-13 22:16:44 -07003417 for_each_worker_pool(pool, gcwq)
3418 busy |= pool->nr_workers != pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +02003419
Tejun Heo4ce62e92012-07-13 22:16:44 -07003420 if (!busy && !(gcwq->flags & GCWQ_FREEZING) &&
3421 gcwq->trustee_state != TRUSTEE_IN_CHARGE)
3422 break;
Tejun Heoe22bee72010-06-29 10:07:14 +02003423
Tejun Heo4ce62e92012-07-13 22:16:44 -07003424 for_each_worker_pool(pool, gcwq) {
3425 int nr_works = 0;
3426
3427 list_for_each_entry(work, &pool->worklist, entry) {
3428 send_mayday(work);
3429 nr_works++;
3430 }
3431
3432 list_for_each_entry(worker, &pool->idle_list, entry) {
3433 if (!nr_works--)
3434 break;
3435 wake_up_process(worker->task);
3436 }
3437
3438 if (need_to_create_worker(pool)) {
3439 spin_unlock_irq(&gcwq->lock);
Tejun Heobc2ae0f2012-07-17 12:39:27 -07003440 worker = create_worker(pool);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003441 spin_lock_irq(&gcwq->lock);
Tejun Heobc2ae0f2012-07-17 12:39:27 -07003442 if (worker)
Tejun Heo4ce62e92012-07-13 22:16:44 -07003443 start_worker(worker);
Tejun Heoe22bee72010-06-29 10:07:14 +02003444 }
3445 }
3446
Tejun Heodb7bccf2010-06-29 10:07:12 +02003447 /* give a breather */
3448 if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN) < 0)
3449 break;
3450 }
3451
Tejun Heoe22bee72010-06-29 10:07:14 +02003452 /*
3453 * Either all works have been scheduled and cpu is down, or
3454 * cpu down has already been canceled. Wait for and butcher
3455 * all workers till we're canceled.
3456 */
3457 do {
Tejun Heo4ce62e92012-07-13 22:16:44 -07003458 rc = trustee_wait_event(gcwq_has_idle_workers(gcwq));
3459
3460 i = 0;
3461 for_each_worker_pool(pool, gcwq) {
3462 while (!list_empty(&pool->idle_list)) {
3463 worker = list_first_entry(&pool->idle_list,
3464 struct worker, entry);
3465 destroy_worker(worker);
3466 }
3467 i |= pool->nr_workers;
3468 }
3469 } while (i && rc >= 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003470
3471 /*
3472 * At this point, either draining has completed and no worker
3473 * is left, or cpu down has been canceled or the cpu is being
3474 * brought back up. There shouldn't be any idle one left.
3475 * Tell the remaining busy ones to rebind once it finishes the
3476 * currently scheduled works by scheduling the rebind_work.
3477 */
Tejun Heo4ce62e92012-07-13 22:16:44 -07003478 for_each_worker_pool(pool, gcwq)
3479 WARN_ON(!list_empty(&pool->idle_list));
Tejun Heoe22bee72010-06-29 10:07:14 +02003480
Tejun Heobc2ae0f2012-07-17 12:39:27 -07003481 /* if we're reassociating, clear DISASSOCIATED */
3482 if (gcwq->trustee_state == TRUSTEE_RELEASE)
3483 gcwq->flags &= ~GCWQ_DISASSOCIATED;
3484
Tejun Heoe22bee72010-06-29 10:07:14 +02003485 for_each_busy_worker(worker, i, pos, gcwq) {
3486 struct work_struct *rebind_work = &worker->rebind_work;
3487
3488 /*
3489 * Rebind_work may race with future cpu hotplug
3490 * operations. Use a separate flag to mark that
3491 * rebinding is scheduled.
3492 */
Tejun Heocb444762010-07-02 10:03:50 +02003493 worker->flags |= WORKER_REBIND;
Tejun Heo403c8212012-07-17 12:39:27 -07003494 worker->flags &= ~WORKER_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02003495
3496 /* queue rebind_work, wq doesn't matter, use the default one */
3497 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
3498 work_data_bits(rebind_work)))
3499 continue;
3500
3501 debug_work_activate(rebind_work);
Tejun Heod320c032010-06-29 10:07:14 +02003502 insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work,
Tejun Heoe22bee72010-06-29 10:07:14 +02003503 worker->scheduled.next,
3504 work_color_to_flags(WORK_NO_COLOR));
3505 }
3506
Tejun Heo60373152012-07-17 12:39:27 -07003507 gcwq_release_management(gcwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02003508
Tejun Heodb7bccf2010-06-29 10:07:12 +02003509 /* notify completion */
3510 gcwq->trustee = NULL;
3511 gcwq->trustee_state = TRUSTEE_DONE;
3512 wake_up_all(&gcwq->trustee_wait);
3513 spin_unlock_irq(&gcwq->lock);
3514 return 0;
3515}
3516
3517/**
3518 * wait_trustee_state - wait for trustee to enter the specified state
3519 * @gcwq: gcwq the trustee of interest belongs to
3520 * @state: target state to wait for
3521 *
3522 * Wait for the trustee to reach @state. DONE is already matched.
3523 *
3524 * CONTEXT:
3525 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3526 * multiple times. To be used by cpu_callback.
3527 */
3528static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09003529__releases(&gcwq->lock)
3530__acquires(&gcwq->lock)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003531{
3532 if (!(gcwq->trustee_state == state ||
3533 gcwq->trustee_state == TRUSTEE_DONE)) {
3534 spin_unlock_irq(&gcwq->lock);
3535 __wait_event(gcwq->trustee_wait,
3536 gcwq->trustee_state == state ||
3537 gcwq->trustee_state == TRUSTEE_DONE);
3538 spin_lock_irq(&gcwq->lock);
3539 }
3540}
3541
Oleg Nesterov3af244332007-05-09 02:34:09 -07003542static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
3543 unsigned long action,
3544 void *hcpu)
3545{
3546 unsigned int cpu = (unsigned long)hcpu;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003547 struct global_cwq *gcwq = get_gcwq(cpu);
3548 struct task_struct *new_trustee = NULL;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003549 struct worker *new_workers[NR_WORKER_POOLS] = { };
3550 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003551 unsigned long flags;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003552 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003554 action &= ~CPU_TASKS_FROZEN;
3555
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003557 case CPU_DOWN_PREPARE:
3558 new_trustee = kthread_create(trustee_thread, gcwq,
3559 "workqueue_trustee/%d\n", cpu);
3560 if (IS_ERR(new_trustee))
3561 return notifier_from_errno(PTR_ERR(new_trustee));
3562 kthread_bind(new_trustee, cpu);
Tejun Heoe22bee72010-06-29 10:07:14 +02003563 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 case CPU_UP_PREPARE:
Tejun Heo4ce62e92012-07-13 22:16:44 -07003565 i = 0;
3566 for_each_worker_pool(pool, gcwq) {
3567 BUG_ON(pool->first_idle);
Tejun Heobc2ae0f2012-07-17 12:39:27 -07003568 new_workers[i] = create_worker(pool);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003569 if (!new_workers[i++])
3570 goto err_destroy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 }
3573
Tejun Heodb7bccf2010-06-29 10:07:12 +02003574 /* some are called w/ irq disabled, don't disturb irq status */
3575 spin_lock_irqsave(&gcwq->lock, flags);
3576
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003577 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003578 case CPU_DOWN_PREPARE:
3579 /* initialize trustee and tell it to acquire the gcwq */
3580 BUG_ON(gcwq->trustee || gcwq->trustee_state != TRUSTEE_DONE);
3581 gcwq->trustee = new_trustee;
3582 gcwq->trustee_state = TRUSTEE_START;
3583 wake_up_process(gcwq->trustee);
3584 wait_trustee_state(gcwq, TRUSTEE_IN_CHARGE);
Tejun Heoe22bee72010-06-29 10:07:14 +02003585 /* fall through */
3586 case CPU_UP_PREPARE:
Tejun Heo4ce62e92012-07-13 22:16:44 -07003587 i = 0;
3588 for_each_worker_pool(pool, gcwq) {
3589 BUG_ON(pool->first_idle);
3590 pool->first_idle = new_workers[i++];
3591 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003592 break;
3593
Oleg Nesterov3da1c842008-07-25 01:47:50 -07003594 case CPU_POST_DEAD:
Tejun Heodb7bccf2010-06-29 10:07:12 +02003595 gcwq->trustee_state = TRUSTEE_BUTCHER;
Tejun Heoe22bee72010-06-29 10:07:14 +02003596 /* fall through */
3597 case CPU_UP_CANCELED:
Tejun Heo4ce62e92012-07-13 22:16:44 -07003598 for_each_worker_pool(pool, gcwq) {
3599 destroy_worker(pool->first_idle);
3600 pool->first_idle = NULL;
3601 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003602 break;
3603
3604 case CPU_DOWN_FAILED:
3605 case CPU_ONLINE:
3606 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 /*
Tejun Heobc2ae0f2012-07-17 12:39:27 -07003613 * Either DISASSOCIATED is already cleared or no worker is
3614 * left on the gcwq. Safe to clear DISASSOCIATED without
3615 * claiming managers.
3616 */
3617 gcwq->flags &= ~GCWQ_DISASSOCIATED;
3618
3619 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003620 * Trustee is done and there might be no worker left.
3621 * Put the first_idle in and request a real manager to
3622 * take a look.
3623 */
Tejun Heo4ce62e92012-07-13 22:16:44 -07003624 for_each_worker_pool(pool, gcwq) {
3625 spin_unlock_irq(&gcwq->lock);
3626 kthread_bind(pool->first_idle->task, cpu);
3627 spin_lock_irq(&gcwq->lock);
3628 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heobc2ae0f2012-07-17 12:39:27 -07003629 pool->first_idle->flags &= ~WORKER_UNBOUND;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003630 start_worker(pool->first_idle);
3631 pool->first_idle = NULL;
3632 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003633 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003634 }
3635
Tejun Heodb7bccf2010-06-29 10:07:12 +02003636 spin_unlock_irqrestore(&gcwq->lock, flags);
3637
Tejun Heo15376632010-06-29 10:07:11 +02003638 return notifier_from_errno(0);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003639
3640err_destroy:
3641 if (new_trustee)
3642 kthread_stop(new_trustee);
3643
3644 spin_lock_irqsave(&gcwq->lock, flags);
3645 for (i = 0; i < NR_WORKER_POOLS; i++)
3646 if (new_workers[i])
3647 destroy_worker(new_workers[i]);
3648 spin_unlock_irqrestore(&gcwq->lock, flags);
3649
3650 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652
Tejun Heo65758202012-07-17 12:39:26 -07003653/*
3654 * Workqueues should be brought up before normal priority CPU notifiers.
3655 * This will be registered high priority CPU notifier.
3656 */
3657static int __devinit workqueue_cpu_up_callback(struct notifier_block *nfb,
3658 unsigned long action,
3659 void *hcpu)
3660{
3661 switch (action & ~CPU_TASKS_FROZEN) {
3662 case CPU_UP_PREPARE:
3663 case CPU_UP_CANCELED:
3664 case CPU_DOWN_FAILED:
3665 case CPU_ONLINE:
3666 return workqueue_cpu_callback(nfb, action, hcpu);
3667 }
3668 return NOTIFY_OK;
3669}
3670
3671/*
3672 * Workqueues should be brought down after normal priority CPU notifiers.
3673 * This will be registered as low priority CPU notifier.
3674 */
3675static int __devinit workqueue_cpu_down_callback(struct notifier_block *nfb,
3676 unsigned long action,
3677 void *hcpu)
3678{
3679 switch (action & ~CPU_TASKS_FROZEN) {
3680 case CPU_DOWN_PREPARE:
Tejun Heo65758202012-07-17 12:39:26 -07003681 case CPU_POST_DEAD:
3682 return workqueue_cpu_callback(nfb, action, hcpu);
3683 }
3684 return NOTIFY_OK;
3685}
3686
Rusty Russell2d3854a2008-11-05 13:39:10 +11003687#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003688
Rusty Russell2d3854a2008-11-05 13:39:10 +11003689struct work_for_cpu {
Andrew Morton6b440032009-04-09 09:50:37 -06003690 struct completion completion;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003691 long (*fn)(void *);
3692 void *arg;
3693 long ret;
3694};
3695
Andrew Morton6b440032009-04-09 09:50:37 -06003696static int do_work_for_cpu(void *_wfc)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003697{
Andrew Morton6b440032009-04-09 09:50:37 -06003698 struct work_for_cpu *wfc = _wfc;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003699 wfc->ret = wfc->fn(wfc->arg);
Andrew Morton6b440032009-04-09 09:50:37 -06003700 complete(&wfc->completion);
3701 return 0;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003702}
3703
3704/**
3705 * work_on_cpu - run a function in user context on a particular cpu
3706 * @cpu: the cpu to run on
3707 * @fn: the function to run
3708 * @arg: the function arg
3709 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003710 * This will return the value @fn returns.
3711 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06003712 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003713 */
3714long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3715{
Andrew Morton6b440032009-04-09 09:50:37 -06003716 struct task_struct *sub_thread;
3717 struct work_for_cpu wfc = {
3718 .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
3719 .fn = fn,
3720 .arg = arg,
3721 };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003722
Andrew Morton6b440032009-04-09 09:50:37 -06003723 sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
3724 if (IS_ERR(sub_thread))
3725 return PTR_ERR(sub_thread);
3726 kthread_bind(sub_thread, cpu);
3727 wake_up_process(sub_thread);
3728 wait_for_completion(&wfc.completion);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003729 return wfc.ret;
3730}
3731EXPORT_SYMBOL_GPL(work_on_cpu);
3732#endif /* CONFIG_SMP */
3733
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003734#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303735
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003736/**
3737 * freeze_workqueues_begin - begin freezing workqueues
3738 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003739 * Start freezing workqueues. After this function returns, all freezable
3740 * workqueues will queue new works to their frozen_works list instead of
3741 * gcwq->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003742 *
3743 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003744 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003745 */
3746void freeze_workqueues_begin(void)
3747{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003748 unsigned int cpu;
3749
3750 spin_lock(&workqueue_lock);
3751
3752 BUG_ON(workqueue_freezing);
3753 workqueue_freezing = true;
3754
Tejun Heof3421792010-07-02 10:03:51 +02003755 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003756 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003757 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003758
3759 spin_lock_irq(&gcwq->lock);
3760
Tejun Heodb7bccf2010-06-29 10:07:12 +02003761 BUG_ON(gcwq->flags & GCWQ_FREEZING);
3762 gcwq->flags |= GCWQ_FREEZING;
3763
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003764 list_for_each_entry(wq, &workqueues, list) {
3765 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3766
Tejun Heo58a69cb2011-02-16 09:25:31 +01003767 if (cwq && wq->flags & WQ_FREEZABLE)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003768 cwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003769 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003770
3771 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003772 }
3773
3774 spin_unlock(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003776
3777/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003778 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003779 *
3780 * Check whether freezing is complete. This function must be called
3781 * between freeze_workqueues_begin() and thaw_workqueues().
3782 *
3783 * CONTEXT:
3784 * Grabs and releases workqueue_lock.
3785 *
3786 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01003787 * %true if some freezable workqueues are still busy. %false if freezing
3788 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003789 */
3790bool freeze_workqueues_busy(void)
3791{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003792 unsigned int cpu;
3793 bool busy = false;
3794
3795 spin_lock(&workqueue_lock);
3796
3797 BUG_ON(!workqueue_freezing);
3798
Tejun Heof3421792010-07-02 10:03:51 +02003799 for_each_gcwq_cpu(cpu) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003800 struct workqueue_struct *wq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003801 /*
3802 * nr_active is monotonically decreasing. It's safe
3803 * to peek without lock.
3804 */
3805 list_for_each_entry(wq, &workqueues, list) {
3806 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3807
Tejun Heo58a69cb2011-02-16 09:25:31 +01003808 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003809 continue;
3810
3811 BUG_ON(cwq->nr_active < 0);
3812 if (cwq->nr_active) {
3813 busy = true;
3814 goto out_unlock;
3815 }
3816 }
3817 }
3818out_unlock:
3819 spin_unlock(&workqueue_lock);
3820 return busy;
3821}
3822
3823/**
3824 * thaw_workqueues - thaw workqueues
3825 *
3826 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo7e116292010-06-29 10:07:13 +02003827 * frozen works are transferred to their respective gcwq worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003828 *
3829 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003830 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003831 */
3832void thaw_workqueues(void)
3833{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003834 unsigned int cpu;
3835
3836 spin_lock(&workqueue_lock);
3837
3838 if (!workqueue_freezing)
3839 goto out_unlock;
3840
Tejun Heof3421792010-07-02 10:03:51 +02003841 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003842 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003843 struct worker_pool *pool;
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003844 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003845
3846 spin_lock_irq(&gcwq->lock);
3847
Tejun Heodb7bccf2010-06-29 10:07:12 +02003848 BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
3849 gcwq->flags &= ~GCWQ_FREEZING;
3850
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003851 list_for_each_entry(wq, &workqueues, list) {
3852 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3853
Tejun Heo58a69cb2011-02-16 09:25:31 +01003854 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003855 continue;
3856
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003857 /* restore max_active and repopulate worklist */
3858 cwq->max_active = wq->saved_max_active;
3859
3860 while (!list_empty(&cwq->delayed_works) &&
3861 cwq->nr_active < cwq->max_active)
3862 cwq_activate_first_delayed(cwq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003863 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003864
Tejun Heo4ce62e92012-07-13 22:16:44 -07003865 for_each_worker_pool(pool, gcwq)
3866 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02003867
Tejun Heo8b03ae32010-06-29 10:07:12 +02003868 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003869 }
3870
3871 workqueue_freezing = false;
3872out_unlock:
3873 spin_unlock(&workqueue_lock);
3874}
3875#endif /* CONFIG_FREEZER */
3876
Suresh Siddha6ee05782010-07-30 14:57:37 -07003877static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878{
Tejun Heoc34056a2010-06-29 10:07:11 +02003879 unsigned int cpu;
Tejun Heoc8e55f32010-06-29 10:07:12 +02003880 int i;
Tejun Heoc34056a2010-06-29 10:07:11 +02003881
Tejun Heo65758202012-07-17 12:39:26 -07003882 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
3883 cpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003884
3885 /* initialize gcwqs */
Tejun Heof3421792010-07-02 10:03:51 +02003886 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003887 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003888 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003889
3890 spin_lock_init(&gcwq->lock);
3891 gcwq->cpu = cpu;
Tejun Heo477a3c32010-08-31 10:54:35 +02003892 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003893
Tejun Heoc8e55f32010-06-29 10:07:12 +02003894 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
3895 INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
3896
Tejun Heo4ce62e92012-07-13 22:16:44 -07003897 for_each_worker_pool(pool, gcwq) {
3898 pool->gcwq = gcwq;
3899 INIT_LIST_HEAD(&pool->worklist);
3900 INIT_LIST_HEAD(&pool->idle_list);
Tejun Heoe22bee72010-06-29 10:07:14 +02003901
Tejun Heo4ce62e92012-07-13 22:16:44 -07003902 init_timer_deferrable(&pool->idle_timer);
3903 pool->idle_timer.function = idle_worker_timeout;
3904 pool->idle_timer.data = (unsigned long)pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003905
Tejun Heo4ce62e92012-07-13 22:16:44 -07003906 setup_timer(&pool->mayday_timer, gcwq_mayday_timeout,
3907 (unsigned long)pool);
3908
Tejun Heo60373152012-07-17 12:39:27 -07003909 mutex_init(&pool->manager_mutex);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003910 ida_init(&pool->worker_ida);
3911 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003912
3913 gcwq->trustee_state = TRUSTEE_DONE;
3914 init_waitqueue_head(&gcwq->trustee_wait);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003915 }
3916
Tejun Heoe22bee72010-06-29 10:07:14 +02003917 /* create the initial worker */
Tejun Heof3421792010-07-02 10:03:51 +02003918 for_each_online_gcwq_cpu(cpu) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003919 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003920 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003921
Tejun Heo477a3c32010-08-31 10:54:35 +02003922 if (cpu != WORK_CPU_UNBOUND)
3923 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003924
3925 for_each_worker_pool(pool, gcwq) {
3926 struct worker *worker;
3927
Tejun Heobc2ae0f2012-07-17 12:39:27 -07003928 worker = create_worker(pool);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003929 BUG_ON(!worker);
3930 spin_lock_irq(&gcwq->lock);
3931 start_worker(worker);
3932 spin_unlock_irq(&gcwq->lock);
3933 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003934 }
3935
Tejun Heod320c032010-06-29 10:07:14 +02003936 system_wq = alloc_workqueue("events", 0, 0);
3937 system_long_wq = alloc_workqueue("events_long", 0, 0);
3938 system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0);
Tejun Heof3421792010-07-02 10:03:51 +02003939 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3940 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01003941 system_freezable_wq = alloc_workqueue("events_freezable",
3942 WQ_FREEZABLE, 0);
Alan Stern62d3c542012-03-02 10:51:00 +01003943 system_nrt_freezable_wq = alloc_workqueue("events_nrt_freezable",
3944 WQ_NON_REENTRANT | WQ_FREEZABLE, 0);
Hitoshi Mitakee5cba242010-11-26 12:06:44 +01003945 BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq ||
Alan Stern62d3c542012-03-02 10:51:00 +01003946 !system_unbound_wq || !system_freezable_wq ||
3947 !system_nrt_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07003948 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949}
Suresh Siddha6ee05782010-07-30 14:57:37 -07003950early_initcall(init_workqueues);