blob: 7a98bae635facf6c18374f9595333d6642e1dceb [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 Heodb7bccf2010-06-29 10:07:12 +020048 /* global_cwq flags */
Tejun Heo11ebea52012-07-12 14:46:37 -070049 GCWQ_DISASSOCIATED = 1 << 0, /* cpu can't serve workers */
50 GCWQ_FREEZING = 1 << 1, /* freeze in progress */
51
52 /* pool flags */
53 POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
54 POOL_MANAGING_WORKERS = 1 << 1, /* managing workers */
55 POOL_HIGHPRI_PENDING = 1 << 2, /* highpri works on queue */
Tejun Heodb7bccf2010-06-29 10:07:12 +020056
Tejun Heoc8e55f32010-06-29 10:07:12 +020057 /* worker flags */
58 WORKER_STARTED = 1 << 0, /* started */
59 WORKER_DIE = 1 << 1, /* die die die */
60 WORKER_IDLE = 1 << 2, /* is idle */
Tejun Heoe22bee72010-06-29 10:07:14 +020061 WORKER_PREP = 1 << 3, /* preparing to run works */
Tejun Heodb7bccf2010-06-29 10:07:12 +020062 WORKER_ROGUE = 1 << 4, /* not bound to any cpu */
Tejun Heoe22bee72010-06-29 10:07:14 +020063 WORKER_REBIND = 1 << 5, /* mom is home, come back */
Tejun Heofb0e7be2010-06-29 10:07:15 +020064 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
Tejun Heof3421792010-07-02 10:03:51 +020065 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
Tejun Heoe22bee72010-06-29 10:07:14 +020066
Tejun Heofb0e7be2010-06-29 10:07:15 +020067 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_ROGUE | WORKER_REBIND |
Tejun Heof3421792010-07-02 10:03:51 +020068 WORKER_CPU_INTENSIVE | WORKER_UNBOUND,
Tejun Heodb7bccf2010-06-29 10:07:12 +020069
70 /* gcwq->trustee_state */
71 TRUSTEE_START = 0, /* start */
72 TRUSTEE_IN_CHARGE = 1, /* trustee in charge of gcwq */
73 TRUSTEE_BUTCHER = 2, /* butcher workers */
74 TRUSTEE_RELEASE = 3, /* release workers */
75 TRUSTEE_DONE = 4, /* trustee is done */
Tejun Heoc8e55f32010-06-29 10:07:12 +020076
77 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
78 BUSY_WORKER_HASH_SIZE = 1 << BUSY_WORKER_HASH_ORDER,
79 BUSY_WORKER_HASH_MASK = BUSY_WORKER_HASH_SIZE - 1,
Tejun Heodb7bccf2010-06-29 10:07:12 +020080
Tejun Heoe22bee72010-06-29 10:07:14 +020081 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
82 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
83
Tejun Heo3233cdb2011-02-16 18:10:19 +010084 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
85 /* call for help after 10ms
86 (min two ticks) */
Tejun Heoe22bee72010-06-29 10:07:14 +020087 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
88 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heodb7bccf2010-06-29 10:07:12 +020089 TRUSTEE_COOLDOWN = HZ / 10, /* for trustee draining */
Tejun Heoe22bee72010-06-29 10:07:14 +020090
91 /*
92 * Rescue workers are used only on emergencies and shared by
93 * all cpus. Give -20.
94 */
95 RESCUER_NICE_LEVEL = -20,
Tejun Heoc8e55f32010-06-29 10:07:12 +020096};
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98/*
Tejun Heo4690c4a2010-06-29 10:07:10 +020099 * Structure fields follow one of the following exclusion rules.
100 *
Tejun Heoe41e7042010-08-24 14:22:47 +0200101 * I: Modifiable by initialization/destruction paths and read-only for
102 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200103 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200104 * P: Preemption protected. Disabling preemption is enough and should
105 * only be modified and accessed from the local cpu.
106 *
Tejun Heo8b03ae32010-06-29 10:07:12 +0200107 * L: gcwq->lock protected. Access with gcwq->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200108 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200109 * X: During normal operation, modification requires gcwq->lock and
110 * should be done only from local cpu. Either disabling preemption
111 * on local cpu or grabbing gcwq->lock is enough for read access.
Tejun Heof3421792010-07-02 10:03:51 +0200112 * If GCWQ_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200113 *
Tejun Heo73f53c42010-06-29 10:07:11 +0200114 * F: wq->flush_mutex protected.
115 *
Tejun Heo4690c4a2010-06-29 10:07:10 +0200116 * W: workqueue_lock protected.
117 */
118
Tejun Heo8b03ae32010-06-29 10:07:12 +0200119struct global_cwq;
Tejun Heobd7bdd42012-07-12 14:46:37 -0700120struct worker_pool;
Tejun Heoc34056a2010-06-29 10:07:11 +0200121
Tejun Heoe22bee72010-06-29 10:07:14 +0200122/*
123 * The poor guys doing the actual heavy lifting. All on-duty workers
124 * are either serving the manager role, on idle list or on busy hash.
125 */
Tejun Heoc34056a2010-06-29 10:07:11 +0200126struct worker {
Tejun Heoc8e55f32010-06-29 10:07:12 +0200127 /* on idle list while idle, on busy hash table while busy */
128 union {
129 struct list_head entry; /* L: while idle */
130 struct hlist_node hentry; /* L: while busy */
131 };
132
Tejun Heoc34056a2010-06-29 10:07:11 +0200133 struct work_struct *current_work; /* L: work being processed */
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200134 struct cpu_workqueue_struct *current_cwq; /* L: current_work's cwq */
Tejun Heoaffee4b2010-06-29 10:07:12 +0200135 struct list_head scheduled; /* L: scheduled works */
Tejun Heoc34056a2010-06-29 10:07:11 +0200136 struct task_struct *task; /* I: worker task */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700137 struct worker_pool *pool; /* I: the associated pool */
Tejun Heoe22bee72010-06-29 10:07:14 +0200138 /* 64 bytes boundary on 64bit, 32 on 32bit */
139 unsigned long last_active; /* L: last active timestamp */
140 unsigned int flags; /* X: flags */
Tejun Heoc34056a2010-06-29 10:07:11 +0200141 int id; /* I: worker id */
Tejun Heoe22bee72010-06-29 10:07:14 +0200142 struct work_struct rebind_work; /* L: rebind worker to cpu */
Tejun Heoc34056a2010-06-29 10:07:11 +0200143};
144
Tejun Heobd7bdd42012-07-12 14:46:37 -0700145struct worker_pool {
146 struct global_cwq *gcwq; /* I: the owning gcwq */
Tejun Heo11ebea52012-07-12 14:46:37 -0700147 unsigned int flags; /* X: flags */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700148
149 struct list_head worklist; /* L: list of pending works */
150 int nr_workers; /* L: total number of workers */
151 int nr_idle; /* L: currently idle ones */
152
153 struct list_head idle_list; /* X: list of idle workers */
154 struct timer_list idle_timer; /* L: worker idle timeout */
155 struct timer_list mayday_timer; /* L: SOS timer for workers */
156
157 struct ida worker_ida; /* L: for worker IDs */
158 struct worker *first_idle; /* L: first idle worker */
159};
160
Tejun Heo4690c4a2010-06-29 10:07:10 +0200161/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200162 * Global per-cpu workqueue. There's one and only one for each cpu
163 * and all works are queued and processed here regardless of their
164 * target workqueues.
Tejun Heo8b03ae32010-06-29 10:07:12 +0200165 */
166struct global_cwq {
167 spinlock_t lock; /* the gcwq lock */
168 unsigned int cpu; /* I: the associated cpu */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200169 unsigned int flags; /* L: GCWQ_* flags */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200170
Tejun Heobd7bdd42012-07-12 14:46:37 -0700171 /* workers are chained either in busy_hash or pool idle_list */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200172 struct hlist_head busy_hash[BUSY_WORKER_HASH_SIZE];
173 /* L: hash of busy workers */
174
Tejun Heobd7bdd42012-07-12 14:46:37 -0700175 struct worker_pool pool; /* the worker pools */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200176
177 struct task_struct *trustee; /* L: for gcwq shutdown */
178 unsigned int trustee_state; /* L: trustee state */
179 wait_queue_head_t trustee_wait; /* trustee wait */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200180} ____cacheline_aligned_in_smp;
181
182/*
Tejun Heo502ca9d2010-06-29 10:07:13 +0200183 * The per-CPU workqueue. The lower WORK_STRUCT_FLAG_BITS of
Tejun Heo0f900042010-06-29 10:07:11 +0200184 * work_struct->data are used for flags and thus cwqs need to be
185 * aligned at two's power of the number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 */
187struct cpu_workqueue_struct {
Tejun Heobd7bdd42012-07-12 14:46:37 -0700188 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200189 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200190 int work_color; /* L: current color */
191 int flush_color; /* L: flushing color */
192 int nr_in_flight[WORK_NR_COLORS];
193 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200194 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200195 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200196 struct list_head delayed_works; /* L: delayed works */
Tejun Heo0f900042010-06-29 10:07:11 +0200197};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200200 * Structure used to wait for workqueue flush.
201 */
202struct wq_flusher {
203 struct list_head list; /* F: list of flushers */
204 int flush_color; /* F: flush color waiting for */
205 struct completion done; /* flush completion */
206};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Tejun Heo73f53c42010-06-29 10:07:11 +0200208/*
Tejun Heof2e005a2010-07-20 15:59:09 +0200209 * All cpumasks are assumed to be always set on UP and thus can't be
210 * used to determine whether there's something to be done.
211 */
212#ifdef CONFIG_SMP
213typedef cpumask_var_t mayday_mask_t;
214#define mayday_test_and_set_cpu(cpu, mask) \
215 cpumask_test_and_set_cpu((cpu), (mask))
216#define mayday_clear_cpu(cpu, mask) cpumask_clear_cpu((cpu), (mask))
217#define for_each_mayday_cpu(cpu, mask) for_each_cpu((cpu), (mask))
Tejun Heo9c375472010-08-31 11:18:34 +0200218#define alloc_mayday_mask(maskp, gfp) zalloc_cpumask_var((maskp), (gfp))
Tejun Heof2e005a2010-07-20 15:59:09 +0200219#define free_mayday_mask(mask) free_cpumask_var((mask))
220#else
221typedef unsigned long mayday_mask_t;
222#define mayday_test_and_set_cpu(cpu, mask) test_and_set_bit(0, &(mask))
223#define mayday_clear_cpu(cpu, mask) clear_bit(0, &(mask))
224#define for_each_mayday_cpu(cpu, mask) if ((cpu) = 0, (mask))
225#define alloc_mayday_mask(maskp, gfp) true
226#define free_mayday_mask(mask) do { } while (0)
227#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229/*
230 * The externally visible workqueue abstraction is an array of
231 * per-CPU workqueues:
232 */
233struct workqueue_struct {
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200234 unsigned int flags; /* W: WQ_* flags */
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200235 union {
236 struct cpu_workqueue_struct __percpu *pcpu;
237 struct cpu_workqueue_struct *single;
238 unsigned long v;
239 } cpu_wq; /* I: cwq's */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200240 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200241
242 struct mutex flush_mutex; /* protects wq flushing */
243 int work_color; /* F: current work color */
244 int flush_color; /* F: current flush color */
245 atomic_t nr_cwqs_to_flush; /* flush in progress */
246 struct wq_flusher *first_flusher; /* F: first flusher */
247 struct list_head flusher_queue; /* F: flush waiters */
248 struct list_head flusher_overflow; /* F: flush overflow list */
249
Tejun Heof2e005a2010-07-20 15:59:09 +0200250 mayday_mask_t mayday_mask; /* cpus requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200251 struct worker *rescuer; /* I: rescue worker */
252
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200253 int nr_drainers; /* W: drain in progress */
Tejun Heodcd989c2010-06-29 10:07:14 +0200254 int saved_max_active; /* W: saved cwq max_active */
Johannes Berg4e6045f2007-10-18 23:39:55 -0700255#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200256 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700257#endif
Tejun Heob196be82012-01-10 15:11:35 -0800258 char name[]; /* I: workqueue name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259};
260
Tejun Heod320c032010-06-29 10:07:14 +0200261struct workqueue_struct *system_wq __read_mostly;
262struct workqueue_struct *system_long_wq __read_mostly;
263struct workqueue_struct *system_nrt_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200264struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100265struct workqueue_struct *system_freezable_wq __read_mostly;
Alan Stern62d3c542012-03-02 10:51:00 +0100266struct workqueue_struct *system_nrt_freezable_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200267EXPORT_SYMBOL_GPL(system_wq);
268EXPORT_SYMBOL_GPL(system_long_wq);
269EXPORT_SYMBOL_GPL(system_nrt_wq);
Tejun Heof3421792010-07-02 10:03:51 +0200270EXPORT_SYMBOL_GPL(system_unbound_wq);
Tejun Heo24d51ad2011-02-21 09:52:50 +0100271EXPORT_SYMBOL_GPL(system_freezable_wq);
Alan Stern62d3c542012-03-02 10:51:00 +0100272EXPORT_SYMBOL_GPL(system_nrt_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200273
Tejun Heo97bd2342010-10-05 10:41:14 +0200274#define CREATE_TRACE_POINTS
275#include <trace/events/workqueue.h>
276
Tejun Heodb7bccf2010-06-29 10:07:12 +0200277#define for_each_busy_worker(worker, i, pos, gcwq) \
278 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \
279 hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry)
280
Tejun Heof3421792010-07-02 10:03:51 +0200281static inline int __next_gcwq_cpu(int cpu, const struct cpumask *mask,
282 unsigned int sw)
283{
284 if (cpu < nr_cpu_ids) {
285 if (sw & 1) {
286 cpu = cpumask_next(cpu, mask);
287 if (cpu < nr_cpu_ids)
288 return cpu;
289 }
290 if (sw & 2)
291 return WORK_CPU_UNBOUND;
292 }
293 return WORK_CPU_NONE;
294}
295
296static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
297 struct workqueue_struct *wq)
298{
299 return __next_gcwq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2);
300}
301
Tejun Heo09884952010-08-01 11:50:12 +0200302/*
303 * CPU iterators
304 *
305 * An extra gcwq is defined for an invalid cpu number
306 * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any
307 * specific CPU. The following iterators are similar to
308 * for_each_*_cpu() iterators but also considers the unbound gcwq.
309 *
310 * for_each_gcwq_cpu() : possible CPUs + WORK_CPU_UNBOUND
311 * for_each_online_gcwq_cpu() : online CPUs + WORK_CPU_UNBOUND
312 * for_each_cwq_cpu() : possible CPUs for bound workqueues,
313 * WORK_CPU_UNBOUND for unbound workqueues
314 */
Tejun Heof3421792010-07-02 10:03:51 +0200315#define for_each_gcwq_cpu(cpu) \
316 for ((cpu) = __next_gcwq_cpu(-1, cpu_possible_mask, 3); \
317 (cpu) < WORK_CPU_NONE; \
318 (cpu) = __next_gcwq_cpu((cpu), cpu_possible_mask, 3))
319
320#define for_each_online_gcwq_cpu(cpu) \
321 for ((cpu) = __next_gcwq_cpu(-1, cpu_online_mask, 3); \
322 (cpu) < WORK_CPU_NONE; \
323 (cpu) = __next_gcwq_cpu((cpu), cpu_online_mask, 3))
324
325#define for_each_cwq_cpu(cpu, wq) \
326 for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, (wq)); \
327 (cpu) < WORK_CPU_NONE; \
328 (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq)))
329
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900330#ifdef CONFIG_DEBUG_OBJECTS_WORK
331
332static struct debug_obj_descr work_debug_descr;
333
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100334static void *work_debug_hint(void *addr)
335{
336 return ((struct work_struct *) addr)->func;
337}
338
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900339/*
340 * fixup_init is called when:
341 * - an active object is initialized
342 */
343static int work_fixup_init(void *addr, enum debug_obj_state state)
344{
345 struct work_struct *work = addr;
346
347 switch (state) {
348 case ODEBUG_STATE_ACTIVE:
349 cancel_work_sync(work);
350 debug_object_init(work, &work_debug_descr);
351 return 1;
352 default:
353 return 0;
354 }
355}
356
357/*
358 * fixup_activate is called when:
359 * - an active object is activated
360 * - an unknown object is activated (might be a statically initialized object)
361 */
362static int work_fixup_activate(void *addr, enum debug_obj_state state)
363{
364 struct work_struct *work = addr;
365
366 switch (state) {
367
368 case ODEBUG_STATE_NOTAVAILABLE:
369 /*
370 * This is not really a fixup. The work struct was
371 * statically initialized. We just make sure that it
372 * is tracked in the object tracker.
373 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200374 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900375 debug_object_init(work, &work_debug_descr);
376 debug_object_activate(work, &work_debug_descr);
377 return 0;
378 }
379 WARN_ON_ONCE(1);
380 return 0;
381
382 case ODEBUG_STATE_ACTIVE:
383 WARN_ON(1);
384
385 default:
386 return 0;
387 }
388}
389
390/*
391 * fixup_free is called when:
392 * - an active object is freed
393 */
394static int work_fixup_free(void *addr, enum debug_obj_state state)
395{
396 struct work_struct *work = addr;
397
398 switch (state) {
399 case ODEBUG_STATE_ACTIVE:
400 cancel_work_sync(work);
401 debug_object_free(work, &work_debug_descr);
402 return 1;
403 default:
404 return 0;
405 }
406}
407
408static struct debug_obj_descr work_debug_descr = {
409 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100410 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900411 .fixup_init = work_fixup_init,
412 .fixup_activate = work_fixup_activate,
413 .fixup_free = work_fixup_free,
414};
415
416static inline void debug_work_activate(struct work_struct *work)
417{
418 debug_object_activate(work, &work_debug_descr);
419}
420
421static inline void debug_work_deactivate(struct work_struct *work)
422{
423 debug_object_deactivate(work, &work_debug_descr);
424}
425
426void __init_work(struct work_struct *work, int onstack)
427{
428 if (onstack)
429 debug_object_init_on_stack(work, &work_debug_descr);
430 else
431 debug_object_init(work, &work_debug_descr);
432}
433EXPORT_SYMBOL_GPL(__init_work);
434
435void destroy_work_on_stack(struct work_struct *work)
436{
437 debug_object_free(work, &work_debug_descr);
438}
439EXPORT_SYMBOL_GPL(destroy_work_on_stack);
440
441#else
442static inline void debug_work_activate(struct work_struct *work) { }
443static inline void debug_work_deactivate(struct work_struct *work) { }
444#endif
445
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100446/* Serializes the accesses to the list of workqueues. */
447static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448static LIST_HEAD(workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200449static bool workqueue_freezing; /* W: have wqs started freezing? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Oleg Nesterov14441962007-05-23 13:57:57 -0700451/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200452 * The almighty global cpu workqueues. nr_running is the only field
453 * which is expected to be used frequently by other cpus via
454 * try_to_wake_up(). Put it in a separate cacheline.
Oleg Nesterov14441962007-05-23 13:57:57 -0700455 */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200456static DEFINE_PER_CPU(struct global_cwq, global_cwq);
Tejun Heoe22bee72010-06-29 10:07:14 +0200457static DEFINE_PER_CPU_SHARED_ALIGNED(atomic_t, gcwq_nr_running);
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800458
Tejun Heof3421792010-07-02 10:03:51 +0200459/*
460 * Global cpu workqueue and nr_running counter for unbound gcwq. The
461 * gcwq is always online, has GCWQ_DISASSOCIATED set, and all its
462 * workers have WORKER_UNBOUND set.
463 */
464static struct global_cwq unbound_global_cwq;
465static atomic_t unbound_gcwq_nr_running = ATOMIC_INIT(0); /* always 0 */
466
Tejun Heoc34056a2010-06-29 10:07:11 +0200467static int worker_thread(void *__worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Tejun Heo8b03ae32010-06-29 10:07:12 +0200469static struct global_cwq *get_gcwq(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Tejun Heof3421792010-07-02 10:03:51 +0200471 if (cpu != WORK_CPU_UNBOUND)
472 return &per_cpu(global_cwq, cpu);
473 else
474 return &unbound_global_cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
Tejun Heo63d95a92012-07-12 14:46:37 -0700477static atomic_t *get_pool_nr_running(struct worker_pool *pool)
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700478{
Tejun Heo63d95a92012-07-12 14:46:37 -0700479 int cpu = pool->gcwq->cpu;
480
Tejun Heof3421792010-07-02 10:03:51 +0200481 if (cpu != WORK_CPU_UNBOUND)
482 return &per_cpu(gcwq_nr_running, cpu);
483 else
484 return &unbound_gcwq_nr_running;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700485}
486
Tejun Heo4690c4a2010-06-29 10:07:10 +0200487static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
488 struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700489{
Tejun Heof3421792010-07-02 10:03:51 +0200490 if (!(wq->flags & WQ_UNBOUND)) {
Lai Jiangshane06ffa12012-03-09 18:03:20 +0800491 if (likely(cpu < nr_cpu_ids))
Tejun Heof3421792010-07-02 10:03:51 +0200492 return per_cpu_ptr(wq->cpu_wq.pcpu, cpu);
Tejun Heof3421792010-07-02 10:03:51 +0200493 } else if (likely(cpu == WORK_CPU_UNBOUND))
494 return wq->cpu_wq.single;
495 return NULL;
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700496}
497
Tejun Heo73f53c42010-06-29 10:07:11 +0200498static unsigned int work_color_to_flags(int color)
499{
500 return color << WORK_STRUCT_COLOR_SHIFT;
501}
502
503static int get_work_color(struct work_struct *work)
504{
505 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
506 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
507}
508
509static int work_next_color(int color)
510{
511 return (color + 1) % WORK_NR_COLORS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
David Howells4594bf12006-12-07 11:33:26 +0000514/*
Tejun Heoe1201532010-07-22 14:14:25 +0200515 * A work's data points to the cwq with WORK_STRUCT_CWQ set while the
516 * work is on queue. Once execution starts, WORK_STRUCT_CWQ is
517 * cleared and the work data contains the cpu number it was last on.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200518 *
519 * set_work_{cwq|cpu}() and clear_work_data() can be used to set the
520 * cwq, cpu or clear work->data. These functions should only be
521 * called while the work is owned - ie. while the PENDING bit is set.
522 *
523 * get_work_[g]cwq() can be used to obtain the gcwq or cwq
524 * corresponding to a work. gcwq is available once the work has been
525 * queued anywhere after initialization. cwq is available only from
526 * queueing until execution starts.
David Howells4594bf12006-12-07 11:33:26 +0000527 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200528static inline void set_work_data(struct work_struct *work, unsigned long data,
529 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000530{
David Howells4594bf12006-12-07 11:33:26 +0000531 BUG_ON(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200532 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000533}
David Howells365970a2006-11-22 14:54:49 +0000534
Tejun Heo7a22ad72010-06-29 10:07:13 +0200535static void set_work_cwq(struct work_struct *work,
536 struct cpu_workqueue_struct *cwq,
537 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200538{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200539 set_work_data(work, (unsigned long)cwq,
Tejun Heoe1201532010-07-22 14:14:25 +0200540 WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200541}
542
Tejun Heo7a22ad72010-06-29 10:07:13 +0200543static void set_work_cpu(struct work_struct *work, unsigned int cpu)
David Howells365970a2006-11-22 14:54:49 +0000544{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200545 set_work_data(work, cpu << WORK_STRUCT_FLAG_BITS, WORK_STRUCT_PENDING);
546}
547
548static void clear_work_data(struct work_struct *work)
549{
550 set_work_data(work, WORK_STRUCT_NO_CPU, 0);
551}
552
Tejun Heo7a22ad72010-06-29 10:07:13 +0200553static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work)
554{
Tejun Heoe1201532010-07-22 14:14:25 +0200555 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200556
Tejun Heoe1201532010-07-22 14:14:25 +0200557 if (data & WORK_STRUCT_CWQ)
558 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
559 else
560 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200561}
562
563static struct global_cwq *get_work_gcwq(struct work_struct *work)
564{
Tejun Heoe1201532010-07-22 14:14:25 +0200565 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200566 unsigned int cpu;
567
Tejun Heoe1201532010-07-22 14:14:25 +0200568 if (data & WORK_STRUCT_CWQ)
569 return ((struct cpu_workqueue_struct *)
Tejun Heobd7bdd42012-07-12 14:46:37 -0700570 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->gcwq;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200571
572 cpu = data >> WORK_STRUCT_FLAG_BITS;
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200573 if (cpu == WORK_CPU_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200574 return NULL;
575
Tejun Heof3421792010-07-02 10:03:51 +0200576 BUG_ON(cpu >= nr_cpu_ids && cpu != WORK_CPU_UNBOUND);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200577 return get_gcwq(cpu);
David Howells365970a2006-11-22 14:54:49 +0000578}
579
580/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200581 * Policy functions. These define the policies on how the global
582 * worker pool is managed. Unless noted otherwise, these functions
583 * assume that they're being called with gcwq->lock held.
David Howells365970a2006-11-22 14:54:49 +0000584 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200585
Tejun Heo63d95a92012-07-12 14:46:37 -0700586static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000587{
Tejun Heo63d95a92012-07-12 14:46:37 -0700588 return !atomic_read(get_pool_nr_running(pool)) ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700589 (pool->flags & POOL_HIGHPRI_PENDING);
David Howells365970a2006-11-22 14:54:49 +0000590}
591
Tejun Heoe22bee72010-06-29 10:07:14 +0200592/*
593 * Need to wake up a worker? Called from anything but currently
594 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700595 *
596 * Note that, because unbound workers never contribute to nr_running, this
597 * function will always return %true for unbound gcwq as long as the
598 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200599 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700600static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000601{
Tejun Heo63d95a92012-07-12 14:46:37 -0700602 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000603}
604
Tejun Heoe22bee72010-06-29 10:07:14 +0200605/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700606static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200607{
Tejun Heo63d95a92012-07-12 14:46:37 -0700608 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200609}
610
611/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700612static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200613{
Tejun Heo63d95a92012-07-12 14:46:37 -0700614 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200615
Tejun Heo63d95a92012-07-12 14:46:37 -0700616 return !list_empty(&pool->worklist) &&
Tejun Heo30310042010-10-11 11:51:57 +0200617 (atomic_read(nr_running) <= 1 ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700618 (pool->flags & POOL_HIGHPRI_PENDING));
Tejun Heoe22bee72010-06-29 10:07:14 +0200619}
620
621/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700622static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200623{
Tejun Heo63d95a92012-07-12 14:46:37 -0700624 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200625}
626
627/* Do I need to be the manager? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700628static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200629{
Tejun Heo63d95a92012-07-12 14:46:37 -0700630 return need_to_create_worker(pool) ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700631 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200632}
633
634/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700635static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200636{
Tejun Heo11ebea52012-07-12 14:46:37 -0700637 bool managing = pool->flags & POOL_MANAGING_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -0700638 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
639 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200640
641 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
642}
643
644/*
645 * Wake up functions.
646 */
647
Tejun Heo7e116292010-06-29 10:07:13 +0200648/* Return the first worker. Safe with preemption disabled */
Tejun Heo63d95a92012-07-12 14:46:37 -0700649static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200650{
Tejun Heo63d95a92012-07-12 14:46:37 -0700651 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200652 return NULL;
653
Tejun Heo63d95a92012-07-12 14:46:37 -0700654 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200655}
656
657/**
658 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700659 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200660 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700661 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200662 *
663 * CONTEXT:
664 * spin_lock_irq(gcwq->lock).
665 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700666static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200667{
Tejun Heo63d95a92012-07-12 14:46:37 -0700668 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200669
670 if (likely(worker))
671 wake_up_process(worker->task);
672}
673
Tejun Heo4690c4a2010-06-29 10:07:10 +0200674/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200675 * wq_worker_waking_up - a worker is waking up
676 * @task: task waking up
677 * @cpu: CPU @task is waking up to
678 *
679 * This function is called during try_to_wake_up() when a worker is
680 * being awoken.
681 *
682 * CONTEXT:
683 * spin_lock_irq(rq->lock)
684 */
685void wq_worker_waking_up(struct task_struct *task, unsigned int cpu)
686{
687 struct worker *worker = kthread_data(task);
688
Steven Rostedt2d646722010-12-03 23:12:33 -0500689 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heo63d95a92012-07-12 14:46:37 -0700690 atomic_inc(get_pool_nr_running(worker->pool));
Tejun Heoe22bee72010-06-29 10:07:14 +0200691}
692
693/**
694 * wq_worker_sleeping - a worker is going to sleep
695 * @task: task going to sleep
696 * @cpu: CPU in question, must be the current CPU number
697 *
698 * This function is called during schedule() when a busy worker is
699 * going to sleep. Worker on the same cpu can be woken up by
700 * returning pointer to its task.
701 *
702 * CONTEXT:
703 * spin_lock_irq(rq->lock)
704 *
705 * RETURNS:
706 * Worker task on @cpu to wake up, %NULL if none.
707 */
708struct task_struct *wq_worker_sleeping(struct task_struct *task,
709 unsigned int cpu)
710{
711 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heobd7bdd42012-07-12 14:46:37 -0700712 struct worker_pool *pool = worker->pool;
Tejun Heo63d95a92012-07-12 14:46:37 -0700713 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200714
Steven Rostedt2d646722010-12-03 23:12:33 -0500715 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200716 return NULL;
717
718 /* this can only happen on the local cpu */
719 BUG_ON(cpu != raw_smp_processor_id());
720
721 /*
722 * The counterpart of the following dec_and_test, implied mb,
723 * worklist not empty test sequence is in insert_work().
724 * Please read comment there.
725 *
726 * NOT_RUNNING is clear. This means that trustee is not in
727 * charge and we're running on the local cpu w/ rq lock held
728 * and preemption disabled, which in turn means that none else
729 * could be manipulating idle_list, so dereferencing idle_list
730 * without gcwq lock is safe.
731 */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700732 if (atomic_dec_and_test(nr_running) && !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700733 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200734 return to_wakeup ? to_wakeup->task : NULL;
735}
736
737/**
738 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200739 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200740 * @flags: flags to set
741 * @wakeup: wakeup an idle worker if necessary
742 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200743 * Set @flags in @worker->flags and adjust nr_running accordingly. If
744 * nr_running becomes zero and @wakeup is %true, an idle worker is
745 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200746 *
Tejun Heocb444762010-07-02 10:03:50 +0200747 * CONTEXT:
748 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200749 */
750static inline void worker_set_flags(struct worker *worker, unsigned int flags,
751 bool wakeup)
752{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700753 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200754
Tejun Heocb444762010-07-02 10:03:50 +0200755 WARN_ON_ONCE(worker->task != current);
756
Tejun Heoe22bee72010-06-29 10:07:14 +0200757 /*
758 * If transitioning into NOT_RUNNING, adjust nr_running and
759 * wake up an idle worker as necessary if requested by
760 * @wakeup.
761 */
762 if ((flags & WORKER_NOT_RUNNING) &&
763 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heo63d95a92012-07-12 14:46:37 -0700764 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200765
766 if (wakeup) {
767 if (atomic_dec_and_test(nr_running) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -0700768 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700769 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200770 } else
771 atomic_dec(nr_running);
772 }
773
Tejun Heod302f012010-06-29 10:07:13 +0200774 worker->flags |= flags;
775}
776
777/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200778 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200779 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200780 * @flags: flags to clear
781 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200782 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200783 *
Tejun Heocb444762010-07-02 10:03:50 +0200784 * CONTEXT:
785 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200786 */
787static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
788{
Tejun Heo63d95a92012-07-12 14:46:37 -0700789 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200790 unsigned int oflags = worker->flags;
791
Tejun Heocb444762010-07-02 10:03:50 +0200792 WARN_ON_ONCE(worker->task != current);
793
Tejun Heod302f012010-06-29 10:07:13 +0200794 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200795
Tejun Heo42c025f2011-01-11 15:58:49 +0100796 /*
797 * If transitioning out of NOT_RUNNING, increment nr_running. Note
798 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
799 * of multiple flags, not a single flag.
800 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200801 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
802 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heo63d95a92012-07-12 14:46:37 -0700803 atomic_inc(get_pool_nr_running(pool));
Tejun Heod302f012010-06-29 10:07:13 +0200804}
805
806/**
Tejun Heoc8e55f32010-06-29 10:07:12 +0200807 * busy_worker_head - return the busy hash head for a work
808 * @gcwq: gcwq of interest
809 * @work: work to be hashed
810 *
811 * Return hash head of @gcwq for @work.
812 *
813 * CONTEXT:
814 * spin_lock_irq(gcwq->lock).
815 *
816 * RETURNS:
817 * Pointer to the hash head.
818 */
819static struct hlist_head *busy_worker_head(struct global_cwq *gcwq,
820 struct work_struct *work)
821{
822 const int base_shift = ilog2(sizeof(struct work_struct));
823 unsigned long v = (unsigned long)work;
824
825 /* simple shift and fold hash, do we need something better? */
826 v >>= base_shift;
827 v += v >> BUSY_WORKER_HASH_ORDER;
828 v &= BUSY_WORKER_HASH_MASK;
829
830 return &gcwq->busy_hash[v];
831}
832
833/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200834 * __find_worker_executing_work - find worker which is executing a work
835 * @gcwq: gcwq of interest
836 * @bwh: hash head as returned by busy_worker_head()
837 * @work: work to find worker for
838 *
839 * Find a worker which is executing @work on @gcwq. @bwh should be
840 * the hash head obtained by calling busy_worker_head() with the same
841 * work.
842 *
843 * CONTEXT:
844 * spin_lock_irq(gcwq->lock).
845 *
846 * RETURNS:
847 * Pointer to worker which is executing @work if found, NULL
848 * otherwise.
849 */
850static struct worker *__find_worker_executing_work(struct global_cwq *gcwq,
851 struct hlist_head *bwh,
852 struct work_struct *work)
853{
854 struct worker *worker;
855 struct hlist_node *tmp;
856
857 hlist_for_each_entry(worker, tmp, bwh, hentry)
858 if (worker->current_work == work)
859 return worker;
860 return NULL;
861}
862
863/**
864 * find_worker_executing_work - find worker which is executing a work
865 * @gcwq: gcwq of interest
866 * @work: work to find worker for
867 *
868 * Find a worker which is executing @work on @gcwq. This function is
869 * identical to __find_worker_executing_work() except that this
870 * function calculates @bwh itself.
871 *
872 * CONTEXT:
873 * spin_lock_irq(gcwq->lock).
874 *
875 * RETURNS:
876 * Pointer to worker which is executing @work if found, NULL
877 * otherwise.
878 */
879static struct worker *find_worker_executing_work(struct global_cwq *gcwq,
880 struct work_struct *work)
881{
882 return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work),
883 work);
884}
885
886/**
Tejun Heo63d95a92012-07-12 14:46:37 -0700887 * pool_determine_ins_pos - find insertion position
888 * @pool: pool of interest
Tejun Heo649027d2010-06-29 10:07:14 +0200889 * @cwq: cwq a work is being queued for
890 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700891 * A work for @cwq is about to be queued on @pool, determine insertion
Tejun Heo649027d2010-06-29 10:07:14 +0200892 * position for the work. If @cwq is for HIGHPRI wq, the work is
893 * queued at the head of the queue but in FIFO order with respect to
894 * other HIGHPRI works; otherwise, at the end of the queue. This
Tejun Heo11ebea52012-07-12 14:46:37 -0700895 * function also sets POOL_HIGHPRI_PENDING flag to hint @pool that
Tejun Heo649027d2010-06-29 10:07:14 +0200896 * there are HIGHPRI works pending.
897 *
898 * CONTEXT:
899 * spin_lock_irq(gcwq->lock).
900 *
901 * RETURNS:
902 * Pointer to inserstion position.
903 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700904static inline struct list_head *pool_determine_ins_pos(struct worker_pool *pool,
Tejun Heo649027d2010-06-29 10:07:14 +0200905 struct cpu_workqueue_struct *cwq)
906{
907 struct work_struct *twork;
908
909 if (likely(!(cwq->wq->flags & WQ_HIGHPRI)))
Tejun Heo63d95a92012-07-12 14:46:37 -0700910 return &pool->worklist;
Tejun Heo649027d2010-06-29 10:07:14 +0200911
Tejun Heo63d95a92012-07-12 14:46:37 -0700912 list_for_each_entry(twork, &pool->worklist, entry) {
Tejun Heo649027d2010-06-29 10:07:14 +0200913 struct cpu_workqueue_struct *tcwq = get_work_cwq(twork);
914
915 if (!(tcwq->wq->flags & WQ_HIGHPRI))
916 break;
917 }
918
Tejun Heo11ebea52012-07-12 14:46:37 -0700919 pool->flags |= POOL_HIGHPRI_PENDING;
Tejun Heo649027d2010-06-29 10:07:14 +0200920 return &twork->entry;
921}
922
923/**
Tejun Heo7e116292010-06-29 10:07:13 +0200924 * insert_work - insert a work into gcwq
Tejun Heo4690c4a2010-06-29 10:07:10 +0200925 * @cwq: cwq @work belongs to
926 * @work: work to insert
927 * @head: insertion point
928 * @extra_flags: extra WORK_STRUCT_* flags to set
929 *
Tejun Heo7e116292010-06-29 10:07:13 +0200930 * Insert @work which belongs to @cwq into @gcwq after @head.
931 * @extra_flags is or'd to work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200932 *
933 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +0200934 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +0200935 */
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700936static void insert_work(struct cpu_workqueue_struct *cwq,
Tejun Heo4690c4a2010-06-29 10:07:10 +0200937 struct work_struct *work, struct list_head *head,
938 unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700939{
Tejun Heo63d95a92012-07-12 14:46:37 -0700940 struct worker_pool *pool = cwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100941
Tejun Heo4690c4a2010-06-29 10:07:10 +0200942 /* we own @work, set data and link */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200943 set_work_cwq(work, cwq, extra_flags);
Tejun Heo4690c4a2010-06-29 10:07:10 +0200944
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700945 /*
946 * Ensure that we get the right work->data if we see the
947 * result of list_add() below, see try_to_grab_pending().
948 */
949 smp_wmb();
Tejun Heo4690c4a2010-06-29 10:07:10 +0200950
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700951 list_add_tail(&work->entry, head);
Tejun Heoe22bee72010-06-29 10:07:14 +0200952
953 /*
954 * Ensure either worker_sched_deactivated() sees the above
955 * list_add_tail() or we see zero nr_running to avoid workers
956 * lying around lazily while there are works to be processed.
957 */
958 smp_mb();
959
Tejun Heo63d95a92012-07-12 14:46:37 -0700960 if (__need_more_worker(pool))
961 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700962}
963
Tejun Heoc8efcc22010-12-20 19:32:04 +0100964/*
965 * Test whether @work is being queued from another work executing on the
966 * same workqueue. This is rather expensive and should only be used from
967 * cold paths.
968 */
969static bool is_chained_work(struct workqueue_struct *wq)
970{
971 unsigned long flags;
972 unsigned int cpu;
973
974 for_each_gcwq_cpu(cpu) {
975 struct global_cwq *gcwq = get_gcwq(cpu);
976 struct worker *worker;
977 struct hlist_node *pos;
978 int i;
979
980 spin_lock_irqsave(&gcwq->lock, flags);
981 for_each_busy_worker(worker, i, pos, gcwq) {
982 if (worker->task != current)
983 continue;
984 spin_unlock_irqrestore(&gcwq->lock, flags);
985 /*
986 * I'm @worker, no locking necessary. See if @work
987 * is headed to the same workqueue.
988 */
989 return worker->current_cwq->wq == wq;
990 }
991 spin_unlock_irqrestore(&gcwq->lock, flags);
992 }
993 return false;
994}
995
Tejun Heo4690c4a2010-06-29 10:07:10 +0200996static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 struct work_struct *work)
998{
Tejun Heo502ca9d2010-06-29 10:07:13 +0200999 struct global_cwq *gcwq;
1000 struct cpu_workqueue_struct *cwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001001 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001002 unsigned int work_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 unsigned long flags;
1004
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001005 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001006
Tejun Heoc8efcc22010-12-20 19:32:04 +01001007 /* if dying, only works from the same workqueue are allowed */
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02001008 if (unlikely(wq->flags & WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001009 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001010 return;
1011
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001012 /* determine gcwq to use */
1013 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001014 struct global_cwq *last_gcwq;
1015
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001016 if (unlikely(cpu == WORK_CPU_UNBOUND))
1017 cpu = raw_smp_processor_id();
1018
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001019 /*
1020 * It's multi cpu. If @wq is non-reentrant and @work
1021 * was previously on a different cpu, it might still
1022 * be running there, in which case the work needs to
1023 * be queued on that cpu to guarantee non-reentrance.
1024 */
Tejun Heo502ca9d2010-06-29 10:07:13 +02001025 gcwq = get_gcwq(cpu);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001026 if (wq->flags & WQ_NON_REENTRANT &&
1027 (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) {
1028 struct worker *worker;
1029
1030 spin_lock_irqsave(&last_gcwq->lock, flags);
1031
1032 worker = find_worker_executing_work(last_gcwq, work);
1033
1034 if (worker && worker->current_cwq->wq == wq)
1035 gcwq = last_gcwq;
1036 else {
1037 /* meh... not running there, queue here */
1038 spin_unlock_irqrestore(&last_gcwq->lock, flags);
1039 spin_lock_irqsave(&gcwq->lock, flags);
1040 }
1041 } else
1042 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heof3421792010-07-02 10:03:51 +02001043 } else {
1044 gcwq = get_gcwq(WORK_CPU_UNBOUND);
1045 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001046 }
1047
1048 /* gcwq determined, get cwq and queue */
1049 cwq = get_cwq(gcwq->cpu, wq);
Tejun Heocdadf002010-10-05 10:49:55 +02001050 trace_workqueue_queue_work(cpu, cwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001051
Dan Carpenterf5b25522012-04-13 22:06:58 +03001052 if (WARN_ON(!list_empty(&work->entry))) {
1053 spin_unlock_irqrestore(&gcwq->lock, flags);
1054 return;
1055 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001056
Tejun Heo73f53c42010-06-29 10:07:11 +02001057 cwq->nr_in_flight[cwq->work_color]++;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001058 work_flags = work_color_to_flags(cwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001059
1060 if (likely(cwq->nr_active < cwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001061 trace_workqueue_activate_work(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001062 cwq->nr_active++;
Tejun Heo63d95a92012-07-12 14:46:37 -07001063 worklist = pool_determine_ins_pos(cwq->pool, cwq);
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001064 } else {
1065 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001066 worklist = &cwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001067 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001068
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001069 insert_work(cwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001070
Tejun Heo8b03ae32010-06-29 10:07:12 +02001071 spin_unlock_irqrestore(&gcwq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001074/**
1075 * queue_work - queue work on a workqueue
1076 * @wq: workqueue to use
1077 * @work: work to queue
1078 *
Alan Stern057647f2006-10-28 10:38:58 -07001079 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 *
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07001081 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1082 * it can be processed by another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001084int queue_work(struct workqueue_struct *wq, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001086 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001088 ret = queue_work_on(get_cpu(), wq, work);
1089 put_cpu();
1090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 return ret;
1092}
Dave Jonesae90dd52006-06-30 01:40:45 -04001093EXPORT_SYMBOL_GPL(queue_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Zhang Ruic1a220e2008-07-23 21:28:39 -07001095/**
1096 * queue_work_on - queue work on specific cpu
1097 * @cpu: CPU number to execute work on
1098 * @wq: workqueue to use
1099 * @work: work to queue
1100 *
1101 * Returns 0 if @work was already on a queue, non-zero otherwise.
1102 *
1103 * We queue the work to a specific CPU, the caller must ensure it
1104 * can't go away.
1105 */
1106int
1107queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
1108{
1109 int ret = 0;
1110
Tejun Heo22df02b2010-06-29 10:07:10 +02001111 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001112 __queue_work(cpu, wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001113 ret = 1;
1114 }
1115 return ret;
1116}
1117EXPORT_SYMBOL_GPL(queue_work_on);
1118
Li Zefan6d141c32008-02-08 04:21:09 -08001119static void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
David Howells52bad642006-11-22 14:54:01 +00001121 struct delayed_work *dwork = (struct delayed_work *)__data;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001122 struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Tejun Heo4690c4a2010-06-29 10:07:10 +02001124 __queue_work(smp_processor_id(), cwq->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125}
1126
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001127/**
1128 * queue_delayed_work - queue work on a workqueue after delay
1129 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001130 * @dwork: delayable work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001131 * @delay: number of jiffies to wait before queueing
1132 *
Alan Stern057647f2006-10-28 10:38:58 -07001133 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001134 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001135int queue_delayed_work(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001136 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
David Howells52bad642006-11-22 14:54:01 +00001138 if (delay == 0)
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001139 return queue_work(wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001141 return queue_delayed_work_on(-1, wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
Dave Jonesae90dd52006-06-30 01:40:45 -04001143EXPORT_SYMBOL_GPL(queue_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001145/**
1146 * queue_delayed_work_on - queue work on specific CPU after delay
1147 * @cpu: CPU number to execute work on
1148 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001149 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001150 * @delay: number of jiffies to wait before queueing
1151 *
Alan Stern057647f2006-10-28 10:38:58 -07001152 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001153 */
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001154int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001155 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001156{
1157 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +00001158 struct timer_list *timer = &dwork->timer;
1159 struct work_struct *work = &dwork->work;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001160
Tejun Heo22df02b2010-06-29 10:07:10 +02001161 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001162 unsigned int lcpu;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001163
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001164 BUG_ON(timer_pending(timer));
1165 BUG_ON(!list_empty(&work->entry));
1166
Andrew Liu8a3e77c2008-05-01 04:35:14 -07001167 timer_stats_timer_set_start_info(&dwork->timer);
1168
Tejun Heo7a22ad72010-06-29 10:07:13 +02001169 /*
1170 * This stores cwq for the moment, for the timer_fn.
1171 * Note that the work's gcwq is preserved to allow
1172 * reentrance detection for delayed works.
1173 */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001174 if (!(wq->flags & WQ_UNBOUND)) {
1175 struct global_cwq *gcwq = get_work_gcwq(work);
1176
1177 if (gcwq && gcwq->cpu != WORK_CPU_UNBOUND)
1178 lcpu = gcwq->cpu;
1179 else
1180 lcpu = raw_smp_processor_id();
1181 } else
1182 lcpu = WORK_CPU_UNBOUND;
1183
Tejun Heo7a22ad72010-06-29 10:07:13 +02001184 set_work_cwq(work, get_cwq(lcpu, wq), 0);
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001185
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001186 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +00001187 timer->data = (unsigned long)dwork;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001188 timer->function = delayed_work_timer_fn;
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001189
1190 if (unlikely(cpu >= 0))
1191 add_timer_on(timer, cpu);
1192 else
1193 add_timer(timer);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001194 ret = 1;
1195 }
1196 return ret;
1197}
Dave Jonesae90dd52006-06-30 01:40:45 -04001198EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Tejun Heoc8e55f32010-06-29 10:07:12 +02001200/**
1201 * worker_enter_idle - enter idle state
1202 * @worker: worker which is entering idle state
1203 *
1204 * @worker is entering idle state. Update stats and idle timer if
1205 * necessary.
1206 *
1207 * LOCKING:
1208 * spin_lock_irq(gcwq->lock).
1209 */
1210static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001212 struct worker_pool *pool = worker->pool;
1213 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Tejun Heoc8e55f32010-06-29 10:07:12 +02001215 BUG_ON(worker->flags & WORKER_IDLE);
1216 BUG_ON(!list_empty(&worker->entry) &&
1217 (worker->hentry.next || worker->hentry.pprev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Tejun Heocb444762010-07-02 10:03:50 +02001219 /* can't use worker_set_flags(), also called from start_worker() */
1220 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001221 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001222 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001223
Tejun Heoc8e55f32010-06-29 10:07:12 +02001224 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001225 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001226
Tejun Heoe22bee72010-06-29 10:07:14 +02001227 if (likely(!(worker->flags & WORKER_ROGUE))) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001228 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
Tejun Heobd7bdd42012-07-12 14:46:37 -07001229 mod_timer(&pool->idle_timer,
Tejun Heoe22bee72010-06-29 10:07:14 +02001230 jiffies + IDLE_WORKER_TIMEOUT);
1231 } else
Tejun Heodb7bccf2010-06-29 10:07:12 +02001232 wake_up_all(&gcwq->trustee_wait);
Tejun Heocb444762010-07-02 10:03:50 +02001233
Tejun Heo544ecf32012-05-14 15:04:50 -07001234 /*
1235 * Sanity check nr_running. Because trustee releases gcwq->lock
1236 * between setting %WORKER_ROGUE and zapping nr_running, the
1237 * warning may trigger spuriously. Check iff trustee is idle.
1238 */
1239 WARN_ON_ONCE(gcwq->trustee_state == TRUSTEE_DONE &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001240 pool->nr_workers == pool->nr_idle &&
Tejun Heo63d95a92012-07-12 14:46:37 -07001241 atomic_read(get_pool_nr_running(pool)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242}
1243
Tejun Heoc8e55f32010-06-29 10:07:12 +02001244/**
1245 * worker_leave_idle - leave idle state
1246 * @worker: worker which is leaving idle state
1247 *
1248 * @worker is leaving idle state. Update stats.
1249 *
1250 * LOCKING:
1251 * spin_lock_irq(gcwq->lock).
1252 */
1253static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001255 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Tejun Heoc8e55f32010-06-29 10:07:12 +02001257 BUG_ON(!(worker->flags & WORKER_IDLE));
Tejun Heod302f012010-06-29 10:07:13 +02001258 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001259 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001260 list_del_init(&worker->entry);
1261}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Tejun Heoe22bee72010-06-29 10:07:14 +02001263/**
1264 * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq
1265 * @worker: self
1266 *
1267 * Works which are scheduled while the cpu is online must at least be
1268 * scheduled to a worker which is bound to the cpu so that if they are
1269 * flushed from cpu callbacks while cpu is going down, they are
1270 * guaranteed to execute on the cpu.
1271 *
1272 * This function is to be used by rogue workers and rescuers to bind
1273 * themselves to the target cpu and may race with cpu going down or
1274 * coming online. kthread_bind() can't be used because it may put the
1275 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1276 * verbatim as it's best effort and blocking and gcwq may be
1277 * [dis]associated in the meantime.
1278 *
1279 * This function tries set_cpus_allowed() and locks gcwq and verifies
1280 * the binding against GCWQ_DISASSOCIATED which is set during
1281 * CPU_DYING and cleared during CPU_ONLINE, so if the worker enters
1282 * idle state or fetches works without dropping lock, it can guarantee
1283 * the scheduling requirement described in the first paragraph.
1284 *
1285 * CONTEXT:
1286 * Might sleep. Called without any lock but returns with gcwq->lock
1287 * held.
1288 *
1289 * RETURNS:
1290 * %true if the associated gcwq is online (@worker is successfully
1291 * bound), %false if offline.
1292 */
1293static bool worker_maybe_bind_and_lock(struct worker *worker)
Namhyung Kim972fa1c2010-08-22 23:19:43 +09001294__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001295{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001296 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001297 struct task_struct *task = worker->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Tejun Heoe22bee72010-06-29 10:07:14 +02001299 while (true) {
1300 /*
1301 * The following call may fail, succeed or succeed
1302 * without actually migrating the task to the cpu if
1303 * it races with cpu hotunplug operation. Verify
1304 * against GCWQ_DISASSOCIATED.
1305 */
Tejun Heof3421792010-07-02 10:03:51 +02001306 if (!(gcwq->flags & GCWQ_DISASSOCIATED))
1307 set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu));
Oleg Nesterov85f41862007-05-09 02:34:20 -07001308
Tejun Heoe22bee72010-06-29 10:07:14 +02001309 spin_lock_irq(&gcwq->lock);
1310 if (gcwq->flags & GCWQ_DISASSOCIATED)
1311 return false;
1312 if (task_cpu(task) == gcwq->cpu &&
1313 cpumask_equal(&current->cpus_allowed,
1314 get_cpu_mask(gcwq->cpu)))
1315 return true;
1316 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001317
Tejun Heo5035b202011-04-29 18:08:37 +02001318 /*
1319 * We've raced with CPU hot[un]plug. Give it a breather
1320 * and retry migration. cond_resched() is required here;
1321 * otherwise, we might deadlock against cpu_stop trying to
1322 * bring down the CPU on non-preemptive kernel.
1323 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001324 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001325 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001326 }
1327}
1328
1329/*
1330 * Function for worker->rebind_work used to rebind rogue busy workers
1331 * to the associated cpu which is coming back online. This is
1332 * scheduled by cpu up but can race with other cpu hotplug operations
1333 * and may be executed twice without intervening cpu down.
1334 */
1335static void worker_rebind_fn(struct work_struct *work)
1336{
1337 struct worker *worker = container_of(work, struct worker, rebind_work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001338 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001339
1340 if (worker_maybe_bind_and_lock(worker))
1341 worker_clr_flags(worker, WORKER_REBIND);
1342
1343 spin_unlock_irq(&gcwq->lock);
1344}
1345
Tejun Heoc34056a2010-06-29 10:07:11 +02001346static struct worker *alloc_worker(void)
1347{
1348 struct worker *worker;
1349
1350 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001351 if (worker) {
1352 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001353 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heoe22bee72010-06-29 10:07:14 +02001354 INIT_WORK(&worker->rebind_work, worker_rebind_fn);
1355 /* on creation a worker is in !idle && prep state */
1356 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001357 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001358 return worker;
1359}
1360
1361/**
1362 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001363 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001364 * @bind: whether to set affinity to @cpu or not
1365 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001366 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001367 * can be started by calling start_worker() or destroyed using
1368 * destroy_worker().
1369 *
1370 * CONTEXT:
1371 * Might sleep. Does GFP_KERNEL allocations.
1372 *
1373 * RETURNS:
1374 * Pointer to the newly created worker.
1375 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001376static struct worker *create_worker(struct worker_pool *pool, bool bind)
Tejun Heoc34056a2010-06-29 10:07:11 +02001377{
Tejun Heo63d95a92012-07-12 14:46:37 -07001378 struct global_cwq *gcwq = pool->gcwq;
Tejun Heof3421792010-07-02 10:03:51 +02001379 bool on_unbound_cpu = gcwq->cpu == WORK_CPU_UNBOUND;
Tejun Heoc34056a2010-06-29 10:07:11 +02001380 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001381 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001382
Tejun Heo8b03ae32010-06-29 10:07:12 +02001383 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001384 while (ida_get_new(&pool->worker_ida, &id)) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001385 spin_unlock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001386 if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001387 goto fail;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001388 spin_lock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001389 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02001390 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001391
1392 worker = alloc_worker();
1393 if (!worker)
1394 goto fail;
1395
Tejun Heobd7bdd42012-07-12 14:46:37 -07001396 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001397 worker->id = id;
1398
Tejun Heof3421792010-07-02 10:03:51 +02001399 if (!on_unbound_cpu)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001400 worker->task = kthread_create_on_node(worker_thread,
1401 worker,
1402 cpu_to_node(gcwq->cpu),
1403 "kworker/%u:%d", gcwq->cpu, id);
Tejun Heof3421792010-07-02 10:03:51 +02001404 else
1405 worker->task = kthread_create(worker_thread, worker,
1406 "kworker/u:%d", id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001407 if (IS_ERR(worker->task))
1408 goto fail;
1409
Tejun Heodb7bccf2010-06-29 10:07:12 +02001410 /*
1411 * A rogue worker will become a regular one if CPU comes
1412 * online later on. Make sure every worker has
1413 * PF_THREAD_BOUND set.
1414 */
Tejun Heof3421792010-07-02 10:03:51 +02001415 if (bind && !on_unbound_cpu)
Tejun Heo8b03ae32010-06-29 10:07:12 +02001416 kthread_bind(worker->task, gcwq->cpu);
Tejun Heof3421792010-07-02 10:03:51 +02001417 else {
Tejun Heodb7bccf2010-06-29 10:07:12 +02001418 worker->task->flags |= PF_THREAD_BOUND;
Tejun Heof3421792010-07-02 10:03:51 +02001419 if (on_unbound_cpu)
1420 worker->flags |= WORKER_UNBOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001422
Tejun Heoc34056a2010-06-29 10:07:11 +02001423 return worker;
1424fail:
1425 if (id >= 0) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001426 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001427 ida_remove(&pool->worker_ida, id);
Tejun Heo8b03ae32010-06-29 10:07:12 +02001428 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001429 }
1430 kfree(worker);
1431 return NULL;
1432}
1433
1434/**
1435 * start_worker - start a newly created worker
1436 * @worker: worker to start
1437 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001438 * Make the gcwq aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001439 *
1440 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001441 * spin_lock_irq(gcwq->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001442 */
1443static void start_worker(struct worker *worker)
1444{
Tejun Heocb444762010-07-02 10:03:50 +02001445 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001446 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001447 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001448 wake_up_process(worker->task);
1449}
1450
1451/**
1452 * destroy_worker - destroy a workqueue worker
1453 * @worker: worker to be destroyed
1454 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001455 * Destroy @worker and adjust @gcwq stats accordingly.
1456 *
1457 * CONTEXT:
1458 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001459 */
1460static void destroy_worker(struct worker *worker)
1461{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001462 struct worker_pool *pool = worker->pool;
1463 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001464 int id = worker->id;
1465
1466 /* sanity check frenzy */
1467 BUG_ON(worker->current_work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001468 BUG_ON(!list_empty(&worker->scheduled));
Tejun Heoc34056a2010-06-29 10:07:11 +02001469
Tejun Heoc8e55f32010-06-29 10:07:12 +02001470 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001471 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001472 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001473 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001474
1475 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001476 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001477
1478 spin_unlock_irq(&gcwq->lock);
1479
Tejun Heoc34056a2010-06-29 10:07:11 +02001480 kthread_stop(worker->task);
1481 kfree(worker);
1482
Tejun Heo8b03ae32010-06-29 10:07:12 +02001483 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001484 ida_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001485}
1486
Tejun Heo63d95a92012-07-12 14:46:37 -07001487static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001488{
Tejun Heo63d95a92012-07-12 14:46:37 -07001489 struct worker_pool *pool = (void *)__pool;
1490 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001491
1492 spin_lock_irq(&gcwq->lock);
1493
Tejun Heo63d95a92012-07-12 14:46:37 -07001494 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001495 struct worker *worker;
1496 unsigned long expires;
1497
1498 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001499 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001500 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1501
1502 if (time_before(jiffies, expires))
Tejun Heo63d95a92012-07-12 14:46:37 -07001503 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001504 else {
1505 /* it's been idle for too long, wake up manager */
Tejun Heo11ebea52012-07-12 14:46:37 -07001506 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -07001507 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001508 }
1509 }
1510
1511 spin_unlock_irq(&gcwq->lock);
1512}
1513
1514static bool send_mayday(struct work_struct *work)
1515{
1516 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1517 struct workqueue_struct *wq = cwq->wq;
Tejun Heof3421792010-07-02 10:03:51 +02001518 unsigned int cpu;
Tejun Heoe22bee72010-06-29 10:07:14 +02001519
1520 if (!(wq->flags & WQ_RESCUER))
1521 return false;
1522
1523 /* mayday mayday mayday */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001524 cpu = cwq->pool->gcwq->cpu;
Tejun Heof3421792010-07-02 10:03:51 +02001525 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1526 if (cpu == WORK_CPU_UNBOUND)
1527 cpu = 0;
Tejun Heof2e005a2010-07-20 15:59:09 +02001528 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001529 wake_up_process(wq->rescuer->task);
1530 return true;
1531}
1532
Tejun Heo63d95a92012-07-12 14:46:37 -07001533static void gcwq_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001534{
Tejun Heo63d95a92012-07-12 14:46:37 -07001535 struct worker_pool *pool = (void *)__pool;
1536 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001537 struct work_struct *work;
1538
1539 spin_lock_irq(&gcwq->lock);
1540
Tejun Heo63d95a92012-07-12 14:46:37 -07001541 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001542 /*
1543 * We've been trying to create a new worker but
1544 * haven't been successful. We might be hitting an
1545 * allocation deadlock. Send distress signals to
1546 * rescuers.
1547 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001548 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001549 send_mayday(work);
1550 }
1551
1552 spin_unlock_irq(&gcwq->lock);
1553
Tejun Heo63d95a92012-07-12 14:46:37 -07001554 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001555}
1556
1557/**
1558 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001559 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001560 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001561 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001562 * have at least one idle worker on return from this function. If
1563 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001564 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001565 * possible allocation deadlock.
1566 *
1567 * On return, need_to_create_worker() is guaranteed to be false and
1568 * may_start_working() true.
1569 *
1570 * LOCKING:
1571 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1572 * multiple times. Does GFP_KERNEL allocations. Called only from
1573 * manager.
1574 *
1575 * RETURNS:
1576 * false if no action was taken and gcwq->lock stayed locked, true
1577 * otherwise.
1578 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001579static bool maybe_create_worker(struct worker_pool *pool)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001580__releases(&gcwq->lock)
1581__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001582{
Tejun Heo63d95a92012-07-12 14:46:37 -07001583 struct global_cwq *gcwq = pool->gcwq;
1584
1585 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001586 return false;
1587restart:
Tejun Heo9f9c2362010-07-14 11:31:20 +02001588 spin_unlock_irq(&gcwq->lock);
1589
Tejun Heoe22bee72010-06-29 10:07:14 +02001590 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07001591 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001592
1593 while (true) {
1594 struct worker *worker;
1595
Tejun Heo63d95a92012-07-12 14:46:37 -07001596 worker = create_worker(pool, true);
Tejun Heoe22bee72010-06-29 10:07:14 +02001597 if (worker) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001598 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001599 spin_lock_irq(&gcwq->lock);
1600 start_worker(worker);
Tejun Heo63d95a92012-07-12 14:46:37 -07001601 BUG_ON(need_to_create_worker(pool));
Tejun Heoe22bee72010-06-29 10:07:14 +02001602 return true;
1603 }
1604
Tejun Heo63d95a92012-07-12 14:46:37 -07001605 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001606 break;
1607
Tejun Heoe22bee72010-06-29 10:07:14 +02001608 __set_current_state(TASK_INTERRUPTIBLE);
1609 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001610
Tejun Heo63d95a92012-07-12 14:46:37 -07001611 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001612 break;
1613 }
1614
Tejun Heo63d95a92012-07-12 14:46:37 -07001615 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001616 spin_lock_irq(&gcwq->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07001617 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001618 goto restart;
1619 return true;
1620}
1621
1622/**
1623 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo63d95a92012-07-12 14:46:37 -07001624 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001625 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001626 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02001627 * IDLE_WORKER_TIMEOUT.
1628 *
1629 * LOCKING:
1630 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1631 * multiple times. Called only from manager.
1632 *
1633 * RETURNS:
1634 * false if no action was taken and gcwq->lock stayed locked, true
1635 * otherwise.
1636 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001637static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001638{
1639 bool ret = false;
1640
Tejun Heo63d95a92012-07-12 14:46:37 -07001641 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001642 struct worker *worker;
1643 unsigned long expires;
1644
Tejun Heo63d95a92012-07-12 14:46:37 -07001645 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001646 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1647
1648 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001649 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001650 break;
1651 }
1652
1653 destroy_worker(worker);
1654 ret = true;
1655 }
1656
1657 return ret;
1658}
1659
1660/**
1661 * manage_workers - manage worker pool
1662 * @worker: self
1663 *
1664 * Assume the manager role and manage gcwq worker pool @worker belongs
1665 * to. At any given time, there can be only zero or one manager per
1666 * gcwq. The exclusion is handled automatically by this function.
1667 *
1668 * The caller can safely start processing works on false return. On
1669 * true return, it's guaranteed that need_to_create_worker() is false
1670 * and may_start_working() is true.
1671 *
1672 * CONTEXT:
1673 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1674 * multiple times. Does GFP_KERNEL allocations.
1675 *
1676 * RETURNS:
1677 * false if no action was taken and gcwq->lock stayed locked, true if
1678 * some action was taken.
1679 */
1680static bool manage_workers(struct worker *worker)
1681{
Tejun Heo63d95a92012-07-12 14:46:37 -07001682 struct worker_pool *pool = worker->pool;
1683 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001684 bool ret = false;
1685
Tejun Heo11ebea52012-07-12 14:46:37 -07001686 if (pool->flags & POOL_MANAGING_WORKERS)
Tejun Heoe22bee72010-06-29 10:07:14 +02001687 return ret;
1688
Tejun Heo11ebea52012-07-12 14:46:37 -07001689 pool->flags &= ~POOL_MANAGE_WORKERS;
1690 pool->flags |= POOL_MANAGING_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02001691
1692 /*
1693 * Destroy and then create so that may_start_working() is true
1694 * on return.
1695 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001696 ret |= maybe_destroy_workers(pool);
1697 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001698
Tejun Heo11ebea52012-07-12 14:46:37 -07001699 pool->flags &= ~POOL_MANAGING_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02001700
1701 /*
1702 * The trustee might be waiting to take over the manager
1703 * position, tell it we're done.
1704 */
1705 if (unlikely(gcwq->trustee))
1706 wake_up_all(&gcwq->trustee_wait);
1707
1708 return ret;
1709}
1710
Tejun Heoa62428c2010-06-29 10:07:10 +02001711/**
Tejun Heoaffee4b2010-06-29 10:07:12 +02001712 * move_linked_works - move linked works to a list
1713 * @work: start of series of works to be scheduled
1714 * @head: target list to append @work to
1715 * @nextp: out paramter for nested worklist walking
1716 *
1717 * Schedule linked works starting from @work to @head. Work series to
1718 * be scheduled starts at @work and includes any consecutive work with
1719 * WORK_STRUCT_LINKED set in its predecessor.
1720 *
1721 * If @nextp is not NULL, it's updated to point to the next work of
1722 * the last scheduled work. This allows move_linked_works() to be
1723 * nested inside outer list_for_each_entry_safe().
1724 *
1725 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001726 * spin_lock_irq(gcwq->lock).
Tejun Heoaffee4b2010-06-29 10:07:12 +02001727 */
1728static void move_linked_works(struct work_struct *work, struct list_head *head,
1729 struct work_struct **nextp)
1730{
1731 struct work_struct *n;
1732
1733 /*
1734 * Linked worklist will always end before the end of the list,
1735 * use NULL for list head.
1736 */
1737 list_for_each_entry_safe_from(work, n, NULL, entry) {
1738 list_move_tail(&work->entry, head);
1739 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1740 break;
1741 }
1742
1743 /*
1744 * If we're already inside safe list traversal and have moved
1745 * multiple works to the scheduled queue, the next position
1746 * needs to be updated.
1747 */
1748 if (nextp)
1749 *nextp = n;
1750}
1751
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001752static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
1753{
1754 struct work_struct *work = list_first_entry(&cwq->delayed_works,
1755 struct work_struct, entry);
Tejun Heo63d95a92012-07-12 14:46:37 -07001756 struct list_head *pos = pool_determine_ins_pos(cwq->pool, cwq);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001757
Tejun Heocdadf002010-10-05 10:49:55 +02001758 trace_workqueue_activate_work(work);
Tejun Heo649027d2010-06-29 10:07:14 +02001759 move_linked_works(work, pos, NULL);
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001760 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001761 cwq->nr_active++;
1762}
1763
Tejun Heoaffee4b2010-06-29 10:07:12 +02001764/**
Tejun Heo73f53c42010-06-29 10:07:11 +02001765 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
1766 * @cwq: cwq of interest
1767 * @color: color of work which left the queue
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001768 * @delayed: for a delayed work
Tejun Heo73f53c42010-06-29 10:07:11 +02001769 *
1770 * A work either has completed or is removed from pending queue,
1771 * decrement nr_in_flight of its cwq and handle workqueue flushing.
1772 *
1773 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001774 * spin_lock_irq(gcwq->lock).
Tejun Heo73f53c42010-06-29 10:07:11 +02001775 */
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001776static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color,
1777 bool delayed)
Tejun Heo73f53c42010-06-29 10:07:11 +02001778{
1779 /* ignore uncolored works */
1780 if (color == WORK_NO_COLOR)
1781 return;
1782
1783 cwq->nr_in_flight[color]--;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001784
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001785 if (!delayed) {
1786 cwq->nr_active--;
1787 if (!list_empty(&cwq->delayed_works)) {
1788 /* one down, submit a delayed one */
1789 if (cwq->nr_active < cwq->max_active)
1790 cwq_activate_first_delayed(cwq);
1791 }
Tejun Heo502ca9d2010-06-29 10:07:13 +02001792 }
Tejun Heo73f53c42010-06-29 10:07:11 +02001793
1794 /* is flush in progress and are we at the flushing tip? */
1795 if (likely(cwq->flush_color != color))
1796 return;
1797
1798 /* are there still in-flight works? */
1799 if (cwq->nr_in_flight[color])
1800 return;
1801
1802 /* this cwq is done, clear flush_color */
1803 cwq->flush_color = -1;
1804
1805 /*
1806 * If this was the last cwq, wake up the first flusher. It
1807 * will handle the rest.
1808 */
1809 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
1810 complete(&cwq->wq->first_flusher->done);
1811}
1812
1813/**
Tejun Heoa62428c2010-06-29 10:07:10 +02001814 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02001815 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02001816 * @work: work to process
1817 *
1818 * Process @work. This function contains all the logics necessary to
1819 * process a single work including synchronization against and
1820 * interaction with other workers on the same cpu, queueing and
1821 * flushing. As long as context requirement is met, any worker can
1822 * call this function to process a work.
1823 *
1824 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001825 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02001826 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001827static void process_one_work(struct worker *worker, struct work_struct *work)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001828__releases(&gcwq->lock)
1829__acquires(&gcwq->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02001830{
Tejun Heo7e116292010-06-29 10:07:13 +02001831 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001832 struct worker_pool *pool = worker->pool;
1833 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001834 struct hlist_head *bwh = busy_worker_head(gcwq, work);
Tejun Heofb0e7be2010-06-29 10:07:15 +02001835 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heoa62428c2010-06-29 10:07:10 +02001836 work_func_t f = work->func;
Tejun Heo73f53c42010-06-29 10:07:11 +02001837 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02001838 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02001839#ifdef CONFIG_LOCKDEP
1840 /*
1841 * It is permissible to free the struct work_struct from
1842 * inside the function that is called from it, this we need to
1843 * take into account for lockdep too. To avoid bogus "held
1844 * lock freed" warnings as well as problems when looking into
1845 * work->lockdep_map, make a copy and use that here.
1846 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07001847 struct lockdep_map lockdep_map;
1848
1849 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02001850#endif
Tejun Heo7e116292010-06-29 10:07:13 +02001851 /*
1852 * A single work shouldn't be executed concurrently by
1853 * multiple workers on a single cpu. Check whether anyone is
1854 * already processing the work. If so, defer the work to the
1855 * currently executing one.
1856 */
1857 collision = __find_worker_executing_work(gcwq, bwh, work);
1858 if (unlikely(collision)) {
1859 move_linked_works(work, &collision->scheduled, NULL);
1860 return;
1861 }
1862
Tejun Heoa62428c2010-06-29 10:07:10 +02001863 /* claim and process */
Tejun Heoa62428c2010-06-29 10:07:10 +02001864 debug_work_deactivate(work);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001865 hlist_add_head(&worker->hentry, bwh);
Tejun Heoc34056a2010-06-29 10:07:11 +02001866 worker->current_work = work;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001867 worker->current_cwq = cwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02001868 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02001869
Tejun Heo7a22ad72010-06-29 10:07:13 +02001870 /* record the current cpu number in the work data and dequeue */
1871 set_work_cpu(work, gcwq->cpu);
Tejun Heoa62428c2010-06-29 10:07:10 +02001872 list_del_init(&work->entry);
1873
Tejun Heo649027d2010-06-29 10:07:14 +02001874 /*
1875 * If HIGHPRI_PENDING, check the next work, and, if HIGHPRI,
1876 * wake up another worker; otherwise, clear HIGHPRI_PENDING.
1877 */
Tejun Heo11ebea52012-07-12 14:46:37 -07001878 if (unlikely(pool->flags & POOL_HIGHPRI_PENDING)) {
Tejun Heobd7bdd42012-07-12 14:46:37 -07001879 struct work_struct *nwork = list_first_entry(&pool->worklist,
1880 struct work_struct, entry);
Tejun Heo649027d2010-06-29 10:07:14 +02001881
Tejun Heobd7bdd42012-07-12 14:46:37 -07001882 if (!list_empty(&pool->worklist) &&
Tejun Heo649027d2010-06-29 10:07:14 +02001883 get_work_cwq(nwork)->wq->flags & WQ_HIGHPRI)
Tejun Heo63d95a92012-07-12 14:46:37 -07001884 wake_up_worker(pool);
Tejun Heo649027d2010-06-29 10:07:14 +02001885 else
Tejun Heo11ebea52012-07-12 14:46:37 -07001886 pool->flags &= ~POOL_HIGHPRI_PENDING;
Tejun Heo649027d2010-06-29 10:07:14 +02001887 }
1888
Tejun Heofb0e7be2010-06-29 10:07:15 +02001889 /*
1890 * CPU intensive works don't participate in concurrency
1891 * management. They're the scheduler's responsibility.
1892 */
1893 if (unlikely(cpu_intensive))
1894 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
1895
Tejun Heo974271c2012-07-12 14:46:37 -07001896 /*
1897 * Unbound gcwq isn't concurrency managed and work items should be
1898 * executed ASAP. Wake up another worker if necessary.
1899 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001900 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
1901 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07001902
Tejun Heo8b03ae32010-06-29 10:07:12 +02001903 spin_unlock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001904
Tejun Heoa62428c2010-06-29 10:07:10 +02001905 work_clear_pending(work);
Tejun Heoe1594892011-01-09 23:32:15 +01001906 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02001907 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001908 trace_workqueue_execute_start(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02001909 f(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001910 /*
1911 * While we must be careful to not use "work" after this, the trace
1912 * point will only record its address.
1913 */
1914 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02001915 lock_map_release(&lockdep_map);
1916 lock_map_release(&cwq->wq->lockdep_map);
1917
1918 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
1919 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
1920 "%s/0x%08x/%d\n",
1921 current->comm, preempt_count(), task_pid_nr(current));
1922 printk(KERN_ERR " last function: ");
1923 print_symbol("%s\n", (unsigned long)f);
1924 debug_show_held_locks(current);
1925 dump_stack();
1926 }
1927
Tejun Heo8b03ae32010-06-29 10:07:12 +02001928 spin_lock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001929
Tejun Heofb0e7be2010-06-29 10:07:15 +02001930 /* clear cpu intensive status */
1931 if (unlikely(cpu_intensive))
1932 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
1933
Tejun Heoa62428c2010-06-29 10:07:10 +02001934 /* we're done with it, release */
Tejun Heoc8e55f32010-06-29 10:07:12 +02001935 hlist_del_init(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001936 worker->current_work = NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001937 worker->current_cwq = NULL;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001938 cwq_dec_nr_in_flight(cwq, work_color, false);
Tejun Heoa62428c2010-06-29 10:07:10 +02001939}
1940
Tejun Heoaffee4b2010-06-29 10:07:12 +02001941/**
1942 * process_scheduled_works - process scheduled works
1943 * @worker: self
1944 *
1945 * Process all scheduled works. Please note that the scheduled list
1946 * may change while processing a work, so this function repeatedly
1947 * fetches a work from the top and executes it.
1948 *
1949 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001950 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02001951 * multiple times.
1952 */
1953static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954{
Tejun Heoaffee4b2010-06-29 10:07:12 +02001955 while (!list_empty(&worker->scheduled)) {
1956 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001958 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960}
1961
Tejun Heo4690c4a2010-06-29 10:07:10 +02001962/**
1963 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02001964 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02001965 *
Tejun Heoe22bee72010-06-29 10:07:14 +02001966 * The gcwq worker thread function. There's a single dynamic pool of
1967 * these per each cpu. These workers process all works regardless of
1968 * their specific target workqueue. The only exception is works which
1969 * belong to workqueues with a rescuer which will be explained in
1970 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02001971 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001972static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
Tejun Heoc34056a2010-06-29 10:07:11 +02001974 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001975 struct worker_pool *pool = worker->pool;
1976 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Tejun Heoe22bee72010-06-29 10:07:14 +02001978 /* tell the scheduler that this is a workqueue worker */
1979 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001980woke_up:
Tejun Heoc8e55f32010-06-29 10:07:12 +02001981 spin_lock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
Tejun Heoc8e55f32010-06-29 10:07:12 +02001983 /* DIE can be set only while we're idle, checking here is enough */
1984 if (worker->flags & WORKER_DIE) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001985 spin_unlock_irq(&gcwq->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001986 worker->task->flags &= ~PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001987 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 }
1989
Tejun Heoc8e55f32010-06-29 10:07:12 +02001990 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001991recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02001992 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07001993 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001994 goto sleep;
1995
1996 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07001997 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02001998 goto recheck;
1999
Tejun Heoc8e55f32010-06-29 10:07:12 +02002000 /*
2001 * ->scheduled list can only be filled while a worker is
2002 * preparing to process a work or actually processing it.
2003 * Make sure nobody diddled with it while I was sleeping.
2004 */
2005 BUG_ON(!list_empty(&worker->scheduled));
2006
Tejun Heoe22bee72010-06-29 10:07:14 +02002007 /*
2008 * When control reaches this point, we're guaranteed to have
2009 * at least one idle worker or that someone else has already
2010 * assumed the manager role.
2011 */
2012 worker_clr_flags(worker, WORKER_PREP);
2013
2014 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002015 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002016 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002017 struct work_struct, entry);
2018
2019 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2020 /* optimization path, not strictly necessary */
2021 process_one_work(worker, work);
2022 if (unlikely(!list_empty(&worker->scheduled)))
2023 process_scheduled_works(worker);
2024 } else {
2025 move_linked_works(work, &worker->scheduled, NULL);
2026 process_scheduled_works(worker);
2027 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002028 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002029
Tejun Heoe22bee72010-06-29 10:07:14 +02002030 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002031sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07002032 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002033 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002034
Tejun Heoc8e55f32010-06-29 10:07:12 +02002035 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02002036 * gcwq->lock is held and there's no work to process and no
2037 * need to manage, sleep. Workers are woken up only while
2038 * holding gcwq->lock or from local cpu, so setting the
2039 * current state before releasing gcwq->lock is enough to
2040 * prevent losing any event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002041 */
2042 worker_enter_idle(worker);
2043 __set_current_state(TASK_INTERRUPTIBLE);
2044 spin_unlock_irq(&gcwq->lock);
2045 schedule();
2046 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047}
2048
Tejun Heoe22bee72010-06-29 10:07:14 +02002049/**
2050 * rescuer_thread - the rescuer thread function
2051 * @__wq: the associated workqueue
2052 *
2053 * Workqueue rescuer thread function. There's one rescuer for each
2054 * workqueue which has WQ_RESCUER set.
2055 *
2056 * Regular work processing on a gcwq may block trying to create a new
2057 * worker which uses GFP_KERNEL allocation which has slight chance of
2058 * developing into deadlock if some works currently on the same queue
2059 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2060 * the problem rescuer solves.
2061 *
2062 * When such condition is possible, the gcwq summons rescuers of all
2063 * workqueues which have works queued on the gcwq and let them process
2064 * those works so that forward progress can be guaranteed.
2065 *
2066 * This should happen rarely.
2067 */
2068static int rescuer_thread(void *__wq)
2069{
2070 struct workqueue_struct *wq = __wq;
2071 struct worker *rescuer = wq->rescuer;
2072 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heof3421792010-07-02 10:03:51 +02002073 bool is_unbound = wq->flags & WQ_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02002074 unsigned int cpu;
2075
2076 set_user_nice(current, RESCUER_NICE_LEVEL);
2077repeat:
2078 set_current_state(TASK_INTERRUPTIBLE);
2079
2080 if (kthread_should_stop())
2081 return 0;
2082
Tejun Heof3421792010-07-02 10:03:51 +02002083 /*
2084 * See whether any cpu is asking for help. Unbounded
2085 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
2086 */
Tejun Heof2e005a2010-07-20 15:59:09 +02002087 for_each_mayday_cpu(cpu, wq->mayday_mask) {
Tejun Heof3421792010-07-02 10:03:51 +02002088 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2089 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002090 struct worker_pool *pool = cwq->pool;
2091 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002092 struct work_struct *work, *n;
2093
2094 __set_current_state(TASK_RUNNING);
Tejun Heof2e005a2010-07-20 15:59:09 +02002095 mayday_clear_cpu(cpu, wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002096
2097 /* migrate to the target cpu if possible */
Tejun Heobd7bdd42012-07-12 14:46:37 -07002098 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002099 worker_maybe_bind_and_lock(rescuer);
2100
2101 /*
2102 * Slurp in all works issued via this workqueue and
2103 * process'em.
2104 */
2105 BUG_ON(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002106 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02002107 if (get_work_cwq(work) == cwq)
2108 move_linked_works(work, scheduled, &n);
2109
2110 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002111
2112 /*
2113 * Leave this gcwq. If keep_working() is %true, notify a
2114 * regular worker; otherwise, we end up with 0 concurrency
2115 * and stalling the execution.
2116 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002117 if (keep_working(pool))
2118 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002119
Tejun Heoe22bee72010-06-29 10:07:14 +02002120 spin_unlock_irq(&gcwq->lock);
2121 }
2122
2123 schedule();
2124 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125}
2126
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002127struct wq_barrier {
2128 struct work_struct work;
2129 struct completion done;
2130};
2131
2132static void wq_barrier_func(struct work_struct *work)
2133{
2134 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2135 complete(&barr->done);
2136}
2137
Tejun Heo4690c4a2010-06-29 10:07:10 +02002138/**
2139 * insert_wq_barrier - insert a barrier work
2140 * @cwq: cwq to insert barrier into
2141 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002142 * @target: target work to attach @barr to
2143 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002144 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002145 * @barr is linked to @target such that @barr is completed only after
2146 * @target finishes execution. Please note that the ordering
2147 * guarantee is observed only with respect to @target and on the local
2148 * cpu.
2149 *
2150 * Currently, a queued barrier can't be canceled. This is because
2151 * try_to_grab_pending() can't determine whether the work to be
2152 * grabbed is at the head of the queue and thus can't clear LINKED
2153 * flag of the previous work while there must be a valid next work
2154 * after a work with LINKED flag set.
2155 *
2156 * Note that when @worker is non-NULL, @target may be modified
2157 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002158 *
2159 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002160 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002161 */
Oleg Nesterov83c22522007-05-09 02:33:54 -07002162static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002163 struct wq_barrier *barr,
2164 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002165{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002166 struct list_head *head;
2167 unsigned int linked = 0;
2168
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002169 /*
Tejun Heo8b03ae32010-06-29 10:07:12 +02002170 * debugobject calls are safe here even with gcwq->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002171 * as we know for sure that this will not trigger any of the
2172 * checks and call back into the fixup functions where we
2173 * might deadlock.
2174 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002175 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002176 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002177 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002178
Tejun Heoaffee4b2010-06-29 10:07:12 +02002179 /*
2180 * If @target is currently being executed, schedule the
2181 * barrier to the worker; otherwise, put it after @target.
2182 */
2183 if (worker)
2184 head = worker->scheduled.next;
2185 else {
2186 unsigned long *bits = work_data_bits(target);
2187
2188 head = target->entry.next;
2189 /* there can already be other linked works, inherit and set */
2190 linked = *bits & WORK_STRUCT_LINKED;
2191 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2192 }
2193
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002194 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002195 insert_work(cwq, &barr->work, head,
2196 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002197}
2198
Tejun Heo73f53c42010-06-29 10:07:11 +02002199/**
2200 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2201 * @wq: workqueue being flushed
2202 * @flush_color: new flush color, < 0 for no-op
2203 * @work_color: new work color, < 0 for no-op
2204 *
2205 * Prepare cwqs for workqueue flushing.
2206 *
2207 * If @flush_color is non-negative, flush_color on all cwqs should be
2208 * -1. If no cwq has in-flight commands at the specified color, all
2209 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2210 * has in flight commands, its cwq->flush_color is set to
2211 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2212 * wakeup logic is armed and %true is returned.
2213 *
2214 * The caller should have initialized @wq->first_flusher prior to
2215 * calling this function with non-negative @flush_color. If
2216 * @flush_color is negative, no flush color update is done and %false
2217 * is returned.
2218 *
2219 * If @work_color is non-negative, all cwqs should have the same
2220 * work_color which is previous to @work_color and all will be
2221 * advanced to @work_color.
2222 *
2223 * CONTEXT:
2224 * mutex_lock(wq->flush_mutex).
2225 *
2226 * RETURNS:
2227 * %true if @flush_color >= 0 and there's something to flush. %false
2228 * otherwise.
2229 */
2230static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2231 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232{
Tejun Heo73f53c42010-06-29 10:07:11 +02002233 bool wait = false;
2234 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -07002235
Tejun Heo73f53c42010-06-29 10:07:11 +02002236 if (flush_color >= 0) {
2237 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2238 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002239 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002240
Tejun Heof3421792010-07-02 10:03:51 +02002241 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002242 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002243 struct global_cwq *gcwq = cwq->pool->gcwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002244
Tejun Heo8b03ae32010-06-29 10:07:12 +02002245 spin_lock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002246
2247 if (flush_color >= 0) {
2248 BUG_ON(cwq->flush_color != -1);
2249
2250 if (cwq->nr_in_flight[flush_color]) {
2251 cwq->flush_color = flush_color;
2252 atomic_inc(&wq->nr_cwqs_to_flush);
2253 wait = true;
2254 }
2255 }
2256
2257 if (work_color >= 0) {
2258 BUG_ON(work_color != work_next_color(cwq->work_color));
2259 cwq->work_color = work_color;
2260 }
2261
Tejun Heo8b03ae32010-06-29 10:07:12 +02002262 spin_unlock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002263 }
2264
2265 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2266 complete(&wq->first_flusher->done);
2267
2268 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269}
2270
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002271/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002273 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 *
2275 * Forces execution of the workqueue and blocks until its completion.
2276 * This is typically used in driver shutdown handlers.
2277 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002278 * We sleep until all works which were queued on entry have been handled,
2279 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002281void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282{
Tejun Heo73f53c42010-06-29 10:07:11 +02002283 struct wq_flusher this_flusher = {
2284 .list = LIST_HEAD_INIT(this_flusher.list),
2285 .flush_color = -1,
2286 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2287 };
2288 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002289
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002290 lock_map_acquire(&wq->lockdep_map);
2291 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002292
2293 mutex_lock(&wq->flush_mutex);
2294
2295 /*
2296 * Start-to-wait phase
2297 */
2298 next_color = work_next_color(wq->work_color);
2299
2300 if (next_color != wq->flush_color) {
2301 /*
2302 * Color space is not full. The current work_color
2303 * becomes our flush_color and work_color is advanced
2304 * by one.
2305 */
2306 BUG_ON(!list_empty(&wq->flusher_overflow));
2307 this_flusher.flush_color = wq->work_color;
2308 wq->work_color = next_color;
2309
2310 if (!wq->first_flusher) {
2311 /* no flush in progress, become the first flusher */
2312 BUG_ON(wq->flush_color != this_flusher.flush_color);
2313
2314 wq->first_flusher = &this_flusher;
2315
2316 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2317 wq->work_color)) {
2318 /* nothing to flush, done */
2319 wq->flush_color = next_color;
2320 wq->first_flusher = NULL;
2321 goto out_unlock;
2322 }
2323 } else {
2324 /* wait in queue */
2325 BUG_ON(wq->flush_color == this_flusher.flush_color);
2326 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2327 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2328 }
2329 } else {
2330 /*
2331 * Oops, color space is full, wait on overflow queue.
2332 * The next flush completion will assign us
2333 * flush_color and transfer to flusher_queue.
2334 */
2335 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2336 }
2337
2338 mutex_unlock(&wq->flush_mutex);
2339
2340 wait_for_completion(&this_flusher.done);
2341
2342 /*
2343 * Wake-up-and-cascade phase
2344 *
2345 * First flushers are responsible for cascading flushes and
2346 * handling overflow. Non-first flushers can simply return.
2347 */
2348 if (wq->first_flusher != &this_flusher)
2349 return;
2350
2351 mutex_lock(&wq->flush_mutex);
2352
Tejun Heo4ce48b32010-07-02 10:03:51 +02002353 /* we might have raced, check again with mutex held */
2354 if (wq->first_flusher != &this_flusher)
2355 goto out_unlock;
2356
Tejun Heo73f53c42010-06-29 10:07:11 +02002357 wq->first_flusher = NULL;
2358
2359 BUG_ON(!list_empty(&this_flusher.list));
2360 BUG_ON(wq->flush_color != this_flusher.flush_color);
2361
2362 while (true) {
2363 struct wq_flusher *next, *tmp;
2364
2365 /* complete all the flushers sharing the current flush color */
2366 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2367 if (next->flush_color != wq->flush_color)
2368 break;
2369 list_del_init(&next->list);
2370 complete(&next->done);
2371 }
2372
2373 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2374 wq->flush_color != work_next_color(wq->work_color));
2375
2376 /* this flush_color is finished, advance by one */
2377 wq->flush_color = work_next_color(wq->flush_color);
2378
2379 /* one color has been freed, handle overflow queue */
2380 if (!list_empty(&wq->flusher_overflow)) {
2381 /*
2382 * Assign the same color to all overflowed
2383 * flushers, advance work_color and append to
2384 * flusher_queue. This is the start-to-wait
2385 * phase for these overflowed flushers.
2386 */
2387 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2388 tmp->flush_color = wq->work_color;
2389
2390 wq->work_color = work_next_color(wq->work_color);
2391
2392 list_splice_tail_init(&wq->flusher_overflow,
2393 &wq->flusher_queue);
2394 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2395 }
2396
2397 if (list_empty(&wq->flusher_queue)) {
2398 BUG_ON(wq->flush_color != wq->work_color);
2399 break;
2400 }
2401
2402 /*
2403 * Need to flush more colors. Make the next flusher
2404 * the new first flusher and arm cwqs.
2405 */
2406 BUG_ON(wq->flush_color == wq->work_color);
2407 BUG_ON(wq->flush_color != next->flush_color);
2408
2409 list_del_init(&next->list);
2410 wq->first_flusher = next;
2411
2412 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2413 break;
2414
2415 /*
2416 * Meh... this color is already done, clear first
2417 * flusher and repeat cascading.
2418 */
2419 wq->first_flusher = NULL;
2420 }
2421
2422out_unlock:
2423 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424}
Dave Jonesae90dd52006-06-30 01:40:45 -04002425EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002427/**
2428 * drain_workqueue - drain a workqueue
2429 * @wq: workqueue to drain
2430 *
2431 * Wait until the workqueue becomes empty. While draining is in progress,
2432 * only chain queueing is allowed. IOW, only currently pending or running
2433 * work items on @wq can queue further work items on it. @wq is flushed
2434 * repeatedly until it becomes empty. The number of flushing is detemined
2435 * by the depth of chaining and should be relatively short. Whine if it
2436 * takes too long.
2437 */
2438void drain_workqueue(struct workqueue_struct *wq)
2439{
2440 unsigned int flush_cnt = 0;
2441 unsigned int cpu;
2442
2443 /*
2444 * __queue_work() needs to test whether there are drainers, is much
2445 * hotter than drain_workqueue() and already looks at @wq->flags.
2446 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2447 */
2448 spin_lock(&workqueue_lock);
2449 if (!wq->nr_drainers++)
2450 wq->flags |= WQ_DRAINING;
2451 spin_unlock(&workqueue_lock);
2452reflush:
2453 flush_workqueue(wq);
2454
2455 for_each_cwq_cpu(cpu, wq) {
2456 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002457 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002458
Tejun Heobd7bdd42012-07-12 14:46:37 -07002459 spin_lock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002460 drained = !cwq->nr_active && list_empty(&cwq->delayed_works);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002461 spin_unlock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002462
2463 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002464 continue;
2465
2466 if (++flush_cnt == 10 ||
2467 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
2468 pr_warning("workqueue %s: flush on destruction isn't complete after %u tries\n",
2469 wq->name, flush_cnt);
2470 goto reflush;
2471 }
2472
2473 spin_lock(&workqueue_lock);
2474 if (!--wq->nr_drainers)
2475 wq->flags &= ~WQ_DRAINING;
2476 spin_unlock(&workqueue_lock);
2477}
2478EXPORT_SYMBOL_GPL(drain_workqueue);
2479
Tejun Heobaf59022010-09-16 10:42:16 +02002480static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
2481 bool wait_executing)
2482{
2483 struct worker *worker = NULL;
2484 struct global_cwq *gcwq;
2485 struct cpu_workqueue_struct *cwq;
2486
2487 might_sleep();
2488 gcwq = get_work_gcwq(work);
2489 if (!gcwq)
2490 return false;
2491
2492 spin_lock_irq(&gcwq->lock);
2493 if (!list_empty(&work->entry)) {
2494 /*
2495 * See the comment near try_to_grab_pending()->smp_rmb().
2496 * If it was re-queued to a different gcwq under us, we
2497 * are not going to wait.
2498 */
2499 smp_rmb();
2500 cwq = get_work_cwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002501 if (unlikely(!cwq || gcwq != cwq->pool->gcwq))
Tejun Heobaf59022010-09-16 10:42:16 +02002502 goto already_gone;
2503 } else if (wait_executing) {
2504 worker = find_worker_executing_work(gcwq, work);
2505 if (!worker)
2506 goto already_gone;
2507 cwq = worker->current_cwq;
2508 } else
2509 goto already_gone;
2510
2511 insert_wq_barrier(cwq, barr, work, worker);
2512 spin_unlock_irq(&gcwq->lock);
2513
Tejun Heoe1594892011-01-09 23:32:15 +01002514 /*
2515 * If @max_active is 1 or rescuer is in use, flushing another work
2516 * item on the same workqueue may lead to deadlock. Make sure the
2517 * flusher is not running on the same workqueue by verifying write
2518 * access.
2519 */
2520 if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER)
2521 lock_map_acquire(&cwq->wq->lockdep_map);
2522 else
2523 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heobaf59022010-09-16 10:42:16 +02002524 lock_map_release(&cwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002525
Tejun Heobaf59022010-09-16 10:42:16 +02002526 return true;
2527already_gone:
2528 spin_unlock_irq(&gcwq->lock);
2529 return false;
2530}
2531
Oleg Nesterovdb700892008-07-25 01:47:49 -07002532/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002533 * flush_work - wait for a work to finish executing the last queueing instance
2534 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002535 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002536 * Wait until @work has finished execution. This function considers
2537 * only the last queueing instance of @work. If @work has been
2538 * enqueued across different CPUs on a non-reentrant workqueue or on
2539 * multiple workqueues, @work might still be executing on return on
2540 * some of the CPUs from earlier queueing.
Oleg Nesterova67da702008-07-25 01:47:52 -07002541 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002542 * If @work was queued only on a non-reentrant, ordered or unbound
2543 * workqueue, @work is guaranteed to be idle on return if it hasn't
2544 * been requeued since flush started.
2545 *
2546 * RETURNS:
2547 * %true if flush_work() waited for the work to finish execution,
2548 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002549 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002550bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002551{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002552 struct wq_barrier barr;
2553
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002554 lock_map_acquire(&work->lockdep_map);
2555 lock_map_release(&work->lockdep_map);
2556
Tejun Heobaf59022010-09-16 10:42:16 +02002557 if (start_flush_work(work, &barr, true)) {
2558 wait_for_completion(&barr.done);
2559 destroy_work_on_stack(&barr.work);
2560 return true;
2561 } else
2562 return false;
Oleg Nesterovdb700892008-07-25 01:47:49 -07002563}
2564EXPORT_SYMBOL_GPL(flush_work);
2565
Tejun Heo401a8d02010-09-16 10:36:00 +02002566static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work)
2567{
2568 struct wq_barrier barr;
2569 struct worker *worker;
2570
2571 spin_lock_irq(&gcwq->lock);
2572
2573 worker = find_worker_executing_work(gcwq, work);
2574 if (unlikely(worker))
2575 insert_wq_barrier(worker->current_cwq, &barr, work, worker);
2576
2577 spin_unlock_irq(&gcwq->lock);
2578
2579 if (unlikely(worker)) {
2580 wait_for_completion(&barr.done);
2581 destroy_work_on_stack(&barr.work);
2582 return true;
2583 } else
2584 return false;
2585}
2586
2587static bool wait_on_work(struct work_struct *work)
2588{
2589 bool ret = false;
2590 int cpu;
2591
2592 might_sleep();
2593
2594 lock_map_acquire(&work->lockdep_map);
2595 lock_map_release(&work->lockdep_map);
2596
2597 for_each_gcwq_cpu(cpu)
2598 ret |= wait_on_cpu_work(get_gcwq(cpu), work);
2599 return ret;
2600}
2601
Tejun Heo09383492010-09-16 10:48:29 +02002602/**
2603 * flush_work_sync - wait until a work has finished execution
2604 * @work: the work to flush
2605 *
2606 * Wait until @work has finished execution. On return, it's
2607 * guaranteed that all queueing instances of @work which happened
2608 * before this function is called are finished. In other words, if
2609 * @work hasn't been requeued since this function was called, @work is
2610 * guaranteed to be idle on return.
2611 *
2612 * RETURNS:
2613 * %true if flush_work_sync() waited for the work to finish execution,
2614 * %false if it was already idle.
2615 */
2616bool flush_work_sync(struct work_struct *work)
2617{
2618 struct wq_barrier barr;
2619 bool pending, waited;
2620
2621 /* we'll wait for executions separately, queue barr only if pending */
2622 pending = start_flush_work(work, &barr, false);
2623
2624 /* wait for executions to finish */
2625 waited = wait_on_work(work);
2626
2627 /* wait for the pending one */
2628 if (pending) {
2629 wait_for_completion(&barr.done);
2630 destroy_work_on_stack(&barr.work);
2631 }
2632
2633 return pending || waited;
2634}
2635EXPORT_SYMBOL_GPL(flush_work_sync);
2636
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002637/*
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002638 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002639 * so this work can't be re-armed in any way.
2640 */
2641static int try_to_grab_pending(struct work_struct *work)
2642{
Tejun Heo8b03ae32010-06-29 10:07:12 +02002643 struct global_cwq *gcwq;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002644 int ret = -1;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002645
Tejun Heo22df02b2010-06-29 10:07:10 +02002646 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002647 return 0;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002648
2649 /*
2650 * The queueing is in progress, or it is already queued. Try to
2651 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
2652 */
Tejun Heo7a22ad72010-06-29 10:07:13 +02002653 gcwq = get_work_gcwq(work);
2654 if (!gcwq)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002655 return ret;
2656
Tejun Heo8b03ae32010-06-29 10:07:12 +02002657 spin_lock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002658 if (!list_empty(&work->entry)) {
2659 /*
Tejun Heo7a22ad72010-06-29 10:07:13 +02002660 * This work is queued, but perhaps we locked the wrong gcwq.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002661 * In that case we must see the new value after rmb(), see
2662 * insert_work()->wmb().
2663 */
2664 smp_rmb();
Tejun Heo7a22ad72010-06-29 10:07:13 +02002665 if (gcwq == get_work_gcwq(work)) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002666 debug_work_deactivate(work);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002667 list_del_init(&work->entry);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002668 cwq_dec_nr_in_flight(get_work_cwq(work),
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02002669 get_work_color(work),
2670 *work_data_bits(work) & WORK_STRUCT_DELAYED);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002671 ret = 1;
2672 }
2673 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02002674 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002675
2676 return ret;
2677}
2678
Tejun Heo401a8d02010-09-16 10:36:00 +02002679static bool __cancel_work_timer(struct work_struct *work,
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002680 struct timer_list* timer)
2681{
2682 int ret;
2683
2684 do {
2685 ret = (timer && likely(del_timer(timer)));
2686 if (!ret)
2687 ret = try_to_grab_pending(work);
2688 wait_on_work(work);
2689 } while (unlikely(ret < 0));
2690
Tejun Heo7a22ad72010-06-29 10:07:13 +02002691 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002692 return ret;
2693}
2694
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002695/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002696 * cancel_work_sync - cancel a work and wait for it to finish
2697 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002698 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002699 * Cancel @work and wait for its execution to finish. This function
2700 * can be used even if the work re-queues itself or migrates to
2701 * another workqueue. On return from this function, @work is
2702 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002703 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002704 * cancel_work_sync(&delayed_work->work) must not be used for
2705 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002706 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002707 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002708 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002709 *
2710 * RETURNS:
2711 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002712 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002713bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002714{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002715 return __cancel_work_timer(work, NULL);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002716}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002717EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002718
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002719/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002720 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2721 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002722 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002723 * Delayed timer is cancelled and the pending work is queued for
2724 * immediate execution. Like flush_work(), this function only
2725 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002726 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002727 * RETURNS:
2728 * %true if flush_work() waited for the work to finish execution,
2729 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002730 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002731bool flush_delayed_work(struct delayed_work *dwork)
2732{
2733 if (del_timer_sync(&dwork->timer))
2734 __queue_work(raw_smp_processor_id(),
2735 get_work_cwq(&dwork->work)->wq, &dwork->work);
2736 return flush_work(&dwork->work);
2737}
2738EXPORT_SYMBOL(flush_delayed_work);
2739
2740/**
Tejun Heo09383492010-09-16 10:48:29 +02002741 * flush_delayed_work_sync - wait for a dwork to finish
2742 * @dwork: the delayed work to flush
2743 *
2744 * Delayed timer is cancelled and the pending work is queued for
2745 * execution immediately. Other than timer handling, its behavior
2746 * is identical to flush_work_sync().
2747 *
2748 * RETURNS:
2749 * %true if flush_work_sync() waited for the work to finish execution,
2750 * %false if it was already idle.
2751 */
2752bool flush_delayed_work_sync(struct delayed_work *dwork)
2753{
2754 if (del_timer_sync(&dwork->timer))
2755 __queue_work(raw_smp_processor_id(),
2756 get_work_cwq(&dwork->work)->wq, &dwork->work);
2757 return flush_work_sync(&dwork->work);
2758}
2759EXPORT_SYMBOL(flush_delayed_work_sync);
2760
2761/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002762 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2763 * @dwork: the delayed work cancel
2764 *
2765 * This is cancel_work_sync() for delayed works.
2766 *
2767 * RETURNS:
2768 * %true if @dwork was pending, %false otherwise.
2769 */
2770bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002771{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002772 return __cancel_work_timer(&dwork->work, &dwork->timer);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002773}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002774EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002776/**
2777 * schedule_work - put work task in global workqueue
2778 * @work: job to be done
2779 *
Bart Van Assche5b0f437d2009-07-30 19:00:53 +02002780 * Returns zero if @work was already on the kernel-global workqueue and
2781 * non-zero otherwise.
2782 *
2783 * This puts a job in the kernel-global workqueue if it was not already
2784 * queued and leaves it in the same position on the kernel-global
2785 * workqueue otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002786 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002787int schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788{
Tejun Heod320c032010-06-29 10:07:14 +02002789 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790}
Dave Jonesae90dd52006-06-30 01:40:45 -04002791EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
Zhang Ruic1a220e2008-07-23 21:28:39 -07002793/*
2794 * schedule_work_on - put work task on a specific cpu
2795 * @cpu: cpu to put the work task on
2796 * @work: job to be done
2797 *
2798 * This puts a job on a specific cpu
2799 */
2800int schedule_work_on(int cpu, struct work_struct *work)
2801{
Tejun Heod320c032010-06-29 10:07:14 +02002802 return queue_work_on(cpu, system_wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07002803}
2804EXPORT_SYMBOL(schedule_work_on);
2805
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002806/**
2807 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +00002808 * @dwork: job to be done
2809 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002810 *
2811 * After waiting for a given time this puts a job in the kernel-global
2812 * workqueue.
2813 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002814int schedule_delayed_work(struct delayed_work *dwork,
Ingo Molnar82f67cd2007-02-16 01:28:13 -08002815 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816{
Tejun Heod320c032010-06-29 10:07:14 +02002817 return queue_delayed_work(system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818}
Dave Jonesae90dd52006-06-30 01:40:45 -04002819EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002821/**
2822 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
2823 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +00002824 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002825 * @delay: number of jiffies to wait
2826 *
2827 * After waiting for a given time this puts a job in the kernel-global
2828 * workqueue on the specified CPU.
2829 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +00002831 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832{
Tejun Heod320c032010-06-29 10:07:14 +02002833 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834}
Dave Jonesae90dd52006-06-30 01:40:45 -04002835EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836
Andrew Mortonb6136772006-06-25 05:47:49 -07002837/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002838 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002839 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002840 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002841 * schedule_on_each_cpu() executes @func on each online CPU using the
2842 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002843 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002844 *
2845 * RETURNS:
2846 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002847 */
David Howells65f27f32006-11-22 14:55:48 +00002848int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002849{
2850 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002851 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002852
Andrew Mortonb6136772006-06-25 05:47:49 -07002853 works = alloc_percpu(struct work_struct);
2854 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002855 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002856
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002857 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002858
Christoph Lameter15316ba2006-01-08 01:00:43 -08002859 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002860 struct work_struct *work = per_cpu_ptr(works, cpu);
2861
2862 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002863 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002864 }
Tejun Heo93981802009-11-17 14:06:20 -08002865
2866 for_each_online_cpu(cpu)
2867 flush_work(per_cpu_ptr(works, cpu));
2868
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002869 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002870 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002871 return 0;
2872}
2873
Alan Sterneef6a7d2010-02-12 17:39:21 +09002874/**
2875 * flush_scheduled_work - ensure that any scheduled work has run to completion.
2876 *
2877 * Forces execution of the kernel-global workqueue and blocks until its
2878 * completion.
2879 *
2880 * Think twice before calling this function! It's very easy to get into
2881 * trouble if you don't take great care. Either of the following situations
2882 * will lead to deadlock:
2883 *
2884 * One of the work items currently on the workqueue needs to acquire
2885 * a lock held by your code or its caller.
2886 *
2887 * Your code is running in the context of a work routine.
2888 *
2889 * They will be detected by lockdep when they occur, but the first might not
2890 * occur very often. It depends on what work items are on the workqueue and
2891 * what locks they need, which you have no control over.
2892 *
2893 * In most situations flushing the entire workqueue is overkill; you merely
2894 * need to know that a particular work item isn't queued and isn't running.
2895 * In such cases you should use cancel_delayed_work_sync() or
2896 * cancel_work_sync() instead.
2897 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898void flush_scheduled_work(void)
2899{
Tejun Heod320c032010-06-29 10:07:14 +02002900 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901}
Dave Jonesae90dd52006-06-30 01:40:45 -04002902EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
2904/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06002905 * execute_in_process_context - reliably execute the routine with user context
2906 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06002907 * @ew: guaranteed storage for the execute work structure (must
2908 * be available when the work executes)
2909 *
2910 * Executes the function immediately if process context is available,
2911 * otherwise schedules the function for delayed execution.
2912 *
2913 * Returns: 0 - function was executed
2914 * 1 - function was scheduled for execution
2915 */
David Howells65f27f32006-11-22 14:55:48 +00002916int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06002917{
2918 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00002919 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002920 return 0;
2921 }
2922
David Howells65f27f32006-11-22 14:55:48 +00002923 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002924 schedule_work(&ew->work);
2925
2926 return 1;
2927}
2928EXPORT_SYMBOL_GPL(execute_in_process_context);
2929
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930int keventd_up(void)
2931{
Tejun Heod320c032010-06-29 10:07:14 +02002932 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933}
2934
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002935static int alloc_cwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936{
Oleg Nesterov3af244332007-05-09 02:34:09 -07002937 /*
Tejun Heo0f900042010-06-29 10:07:11 +02002938 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
2939 * Make sure that the alignment isn't lower than that of
2940 * unsigned long long.
Oleg Nesterov3af244332007-05-09 02:34:09 -07002941 */
Tejun Heo0f900042010-06-29 10:07:11 +02002942 const size_t size = sizeof(struct cpu_workqueue_struct);
2943 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
2944 __alignof__(unsigned long long));
Oleg Nesterov3af244332007-05-09 02:34:09 -07002945
Lai Jiangshane06ffa12012-03-09 18:03:20 +08002946 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02002947 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
Tejun Heo931ac772010-07-20 11:07:48 +02002948 else {
Tejun Heof3421792010-07-02 10:03:51 +02002949 void *ptr;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01002950
Tejun Heof3421792010-07-02 10:03:51 +02002951 /*
2952 * Allocate enough room to align cwq and put an extra
2953 * pointer at the end pointing back to the originally
2954 * allocated pointer which will be used for free.
2955 */
2956 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
2957 if (ptr) {
2958 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
2959 *(void **)(wq->cpu_wq.single + 1) = ptr;
2960 }
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002961 }
Tejun Heof3421792010-07-02 10:03:51 +02002962
Tejun Heo0415b00d12011-03-24 18:50:09 +01002963 /* just in case, make sure it's actually aligned */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002964 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
2965 return wq->cpu_wq.v ? 0 : -ENOMEM;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002966}
2967
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002968static void free_cwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002969{
Lai Jiangshane06ffa12012-03-09 18:03:20 +08002970 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02002971 free_percpu(wq->cpu_wq.pcpu);
2972 else if (wq->cpu_wq.single) {
2973 /* the pointer to free is stored right after the cwq */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002974 kfree(*(void **)(wq->cpu_wq.single + 1));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002975 }
2976}
2977
Tejun Heof3421792010-07-02 10:03:51 +02002978static int wq_clamp_max_active(int max_active, unsigned int flags,
2979 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002980{
Tejun Heof3421792010-07-02 10:03:51 +02002981 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
2982
2983 if (max_active < 1 || max_active > lim)
Tejun Heob71ab8c2010-06-29 10:07:14 +02002984 printk(KERN_WARNING "workqueue: max_active %d requested for %s "
2985 "is out of range, clamping between %d and %d\n",
Tejun Heof3421792010-07-02 10:03:51 +02002986 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002987
Tejun Heof3421792010-07-02 10:03:51 +02002988 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002989}
2990
Tejun Heob196be82012-01-10 15:11:35 -08002991struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02002992 unsigned int flags,
2993 int max_active,
2994 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08002995 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07002996{
Tejun Heob196be82012-01-10 15:11:35 -08002997 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002998 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02002999 unsigned int cpu;
Tejun Heob196be82012-01-10 15:11:35 -08003000 size_t namelen;
3001
3002 /* determine namelen, allocate wq and format name */
3003 va_start(args, lock_name);
3004 va_copy(args1, args);
3005 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3006
3007 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3008 if (!wq)
3009 goto err;
3010
3011 vsnprintf(wq->name, namelen, fmt, args1);
3012 va_end(args);
3013 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003014
Tejun Heof3421792010-07-02 10:03:51 +02003015 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02003016 * Workqueues which may be used during memory reclaim should
3017 * have a rescuer to guarantee forward progress.
3018 */
3019 if (flags & WQ_MEM_RECLAIM)
3020 flags |= WQ_RESCUER;
3021
Tejun Heod320c032010-06-29 10:07:14 +02003022 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003023 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003024
Tejun Heob196be82012-01-10 15:11:35 -08003025 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003026 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003027 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003028 mutex_init(&wq->flush_mutex);
3029 atomic_set(&wq->nr_cwqs_to_flush, 0);
3030 INIT_LIST_HEAD(&wq->flusher_queue);
3031 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003032
Johannes Bergeb13ba82008-01-16 09:51:58 +01003033 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003034 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003035
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003036 if (alloc_cwqs(wq) < 0)
3037 goto err;
3038
Tejun Heof3421792010-07-02 10:03:51 +02003039 for_each_cwq_cpu(cpu, wq) {
Tejun Heo15376632010-06-29 10:07:11 +02003040 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003041 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo15376632010-06-29 10:07:11 +02003042
Tejun Heo0f900042010-06-29 10:07:11 +02003043 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heobd7bdd42012-07-12 14:46:37 -07003044 cwq->pool = &gcwq->pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02003045 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02003046 cwq->flush_color = -1;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003047 cwq->max_active = max_active;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003048 INIT_LIST_HEAD(&cwq->delayed_works);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003049 }
3050
Tejun Heoe22bee72010-06-29 10:07:14 +02003051 if (flags & WQ_RESCUER) {
3052 struct worker *rescuer;
3053
Tejun Heof2e005a2010-07-20 15:59:09 +02003054 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
Tejun Heoe22bee72010-06-29 10:07:14 +02003055 goto err;
3056
3057 wq->rescuer = rescuer = alloc_worker();
3058 if (!rescuer)
3059 goto err;
3060
Tejun Heob196be82012-01-10 15:11:35 -08003061 rescuer->task = kthread_create(rescuer_thread, wq, "%s",
3062 wq->name);
Tejun Heoe22bee72010-06-29 10:07:14 +02003063 if (IS_ERR(rescuer->task))
3064 goto err;
3065
Tejun Heoe22bee72010-06-29 10:07:14 +02003066 rescuer->task->flags |= PF_THREAD_BOUND;
3067 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003068 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003069
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003070 /*
3071 * workqueue_lock protects global freeze state and workqueues
3072 * list. Grab it, set max_active accordingly and add the new
3073 * workqueue to workqueues list.
3074 */
Tejun Heo15376632010-06-29 10:07:11 +02003075 spin_lock(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003076
Tejun Heo58a69cb2011-02-16 09:25:31 +01003077 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heof3421792010-07-02 10:03:51 +02003078 for_each_cwq_cpu(cpu, wq)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003079 get_cwq(cpu, wq)->max_active = 0;
3080
Tejun Heo15376632010-06-29 10:07:11 +02003081 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003082
Tejun Heo15376632010-06-29 10:07:11 +02003083 spin_unlock(&workqueue_lock);
3084
Oleg Nesterov3af244332007-05-09 02:34:09 -07003085 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02003086err:
3087 if (wq) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003088 free_cwqs(wq);
Tejun Heof2e005a2010-07-20 15:59:09 +02003089 free_mayday_mask(wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02003090 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003091 kfree(wq);
3092 }
3093 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003094}
Tejun Heod320c032010-06-29 10:07:14 +02003095EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003096
3097/**
3098 * destroy_workqueue - safely terminate a workqueue
3099 * @wq: target workqueue
3100 *
3101 * Safely destroy a workqueue. All work currently pending will be done first.
3102 */
3103void destroy_workqueue(struct workqueue_struct *wq)
3104{
Tejun Heoc8e55f32010-06-29 10:07:12 +02003105 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003106
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003107 /* drain it before proceeding with destruction */
3108 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003109
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003110 /*
3111 * wq list is used to freeze wq, remove from list after
3112 * flushing is complete in case freeze races us.
3113 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003114 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07003115 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003116 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003117
Tejun Heoe22bee72010-06-29 10:07:14 +02003118 /* sanity check */
Tejun Heof3421792010-07-02 10:03:51 +02003119 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02003120 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3121 int i;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003122
Tejun Heo73f53c42010-06-29 10:07:11 +02003123 for (i = 0; i < WORK_NR_COLORS; i++)
3124 BUG_ON(cwq->nr_in_flight[i]);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003125 BUG_ON(cwq->nr_active);
3126 BUG_ON(!list_empty(&cwq->delayed_works));
Tejun Heo73f53c42010-06-29 10:07:11 +02003127 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003128
Tejun Heoe22bee72010-06-29 10:07:14 +02003129 if (wq->flags & WQ_RESCUER) {
3130 kthread_stop(wq->rescuer->task);
Tejun Heof2e005a2010-07-20 15:59:09 +02003131 free_mayday_mask(wq->mayday_mask);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003132 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003133 }
3134
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003135 free_cwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003136 kfree(wq);
3137}
3138EXPORT_SYMBOL_GPL(destroy_workqueue);
3139
Tejun Heodcd989c2010-06-29 10:07:14 +02003140/**
3141 * workqueue_set_max_active - adjust max_active of a workqueue
3142 * @wq: target workqueue
3143 * @max_active: new max_active value.
3144 *
3145 * Set max_active of @wq to @max_active.
3146 *
3147 * CONTEXT:
3148 * Don't call from IRQ context.
3149 */
3150void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3151{
3152 unsigned int cpu;
3153
Tejun Heof3421792010-07-02 10:03:51 +02003154 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003155
3156 spin_lock(&workqueue_lock);
3157
3158 wq->saved_max_active = max_active;
3159
Tejun Heof3421792010-07-02 10:03:51 +02003160 for_each_cwq_cpu(cpu, wq) {
Tejun Heodcd989c2010-06-29 10:07:14 +02003161 struct global_cwq *gcwq = get_gcwq(cpu);
3162
3163 spin_lock_irq(&gcwq->lock);
3164
Tejun Heo58a69cb2011-02-16 09:25:31 +01003165 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heodcd989c2010-06-29 10:07:14 +02003166 !(gcwq->flags & GCWQ_FREEZING))
3167 get_cwq(gcwq->cpu, wq)->max_active = max_active;
3168
3169 spin_unlock_irq(&gcwq->lock);
3170 }
3171
3172 spin_unlock(&workqueue_lock);
3173}
3174EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3175
3176/**
3177 * workqueue_congested - test whether a workqueue is congested
3178 * @cpu: CPU in question
3179 * @wq: target workqueue
3180 *
3181 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3182 * no synchronization around this function and the test result is
3183 * unreliable and only useful as advisory hints or for debugging.
3184 *
3185 * RETURNS:
3186 * %true if congested, %false otherwise.
3187 */
3188bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3189{
3190 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3191
3192 return !list_empty(&cwq->delayed_works);
3193}
3194EXPORT_SYMBOL_GPL(workqueue_congested);
3195
3196/**
3197 * work_cpu - return the last known associated cpu for @work
3198 * @work: the work of interest
3199 *
3200 * RETURNS:
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003201 * CPU number if @work was ever queued. WORK_CPU_NONE otherwise.
Tejun Heodcd989c2010-06-29 10:07:14 +02003202 */
3203unsigned int work_cpu(struct work_struct *work)
3204{
3205 struct global_cwq *gcwq = get_work_gcwq(work);
3206
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003207 return gcwq ? gcwq->cpu : WORK_CPU_NONE;
Tejun Heodcd989c2010-06-29 10:07:14 +02003208}
3209EXPORT_SYMBOL_GPL(work_cpu);
3210
3211/**
3212 * work_busy - test whether a work is currently pending or running
3213 * @work: the work to be tested
3214 *
3215 * Test whether @work is currently pending or running. There is no
3216 * synchronization around this function and the test result is
3217 * unreliable and only useful as advisory hints or for debugging.
3218 * Especially for reentrant wqs, the pending state might hide the
3219 * running state.
3220 *
3221 * RETURNS:
3222 * OR'd bitmask of WORK_BUSY_* bits.
3223 */
3224unsigned int work_busy(struct work_struct *work)
3225{
3226 struct global_cwq *gcwq = get_work_gcwq(work);
3227 unsigned long flags;
3228 unsigned int ret = 0;
3229
3230 if (!gcwq)
3231 return false;
3232
3233 spin_lock_irqsave(&gcwq->lock, flags);
3234
3235 if (work_pending(work))
3236 ret |= WORK_BUSY_PENDING;
3237 if (find_worker_executing_work(gcwq, work))
3238 ret |= WORK_BUSY_RUNNING;
3239
3240 spin_unlock_irqrestore(&gcwq->lock, flags);
3241
3242 return ret;
3243}
3244EXPORT_SYMBOL_GPL(work_busy);
3245
Tejun Heodb7bccf2010-06-29 10:07:12 +02003246/*
3247 * CPU hotplug.
3248 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003249 * There are two challenges in supporting CPU hotplug. Firstly, there
3250 * are a lot of assumptions on strong associations among work, cwq and
3251 * gcwq which make migrating pending and scheduled works very
3252 * difficult to implement without impacting hot paths. Secondly,
3253 * gcwqs serve mix of short, long and very long running works making
3254 * blocked draining impractical.
3255 *
3256 * This is solved by allowing a gcwq to be detached from CPU, running
3257 * it with unbound (rogue) workers and allowing it to be reattached
3258 * later if the cpu comes back online. A separate thread is created
3259 * to govern a gcwq in such state and is called the trustee of the
3260 * gcwq.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003261 *
3262 * Trustee states and their descriptions.
3263 *
3264 * START Command state used on startup. On CPU_DOWN_PREPARE, a
3265 * new trustee is started with this state.
3266 *
3267 * IN_CHARGE Once started, trustee will enter this state after
Tejun Heoe22bee72010-06-29 10:07:14 +02003268 * assuming the manager role and making all existing
3269 * workers rogue. DOWN_PREPARE waits for trustee to
3270 * enter this state. After reaching IN_CHARGE, trustee
3271 * tries to execute the pending worklist until it's empty
3272 * and the state is set to BUTCHER, or the state is set
3273 * to RELEASE.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003274 *
3275 * BUTCHER Command state which is set by the cpu callback after
3276 * the cpu has went down. Once this state is set trustee
3277 * knows that there will be no new works on the worklist
3278 * and once the worklist is empty it can proceed to
3279 * killing idle workers.
3280 *
3281 * RELEASE Command state which is set by the cpu callback if the
3282 * cpu down has been canceled or it has come online
3283 * again. After recognizing this state, trustee stops
Tejun Heoe22bee72010-06-29 10:07:14 +02003284 * trying to drain or butcher and clears ROGUE, rebinds
3285 * all remaining workers back to the cpu and releases
3286 * manager role.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003287 *
3288 * DONE Trustee will enter this state after BUTCHER or RELEASE
3289 * is complete.
3290 *
3291 * trustee CPU draining
3292 * took over down complete
3293 * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE
3294 * | | ^
3295 * | CPU is back online v return workers |
3296 * ----------------> RELEASE --------------
3297 */
3298
3299/**
3300 * trustee_wait_event_timeout - timed event wait for trustee
3301 * @cond: condition to wait for
3302 * @timeout: timeout in jiffies
3303 *
3304 * wait_event_timeout() for trustee to use. Handles locking and
3305 * checks for RELEASE request.
3306 *
3307 * CONTEXT:
3308 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3309 * multiple times. To be used by trustee.
3310 *
3311 * RETURNS:
3312 * Positive indicating left time if @cond is satisfied, 0 if timed
3313 * out, -1 if canceled.
3314 */
3315#define trustee_wait_event_timeout(cond, timeout) ({ \
3316 long __ret = (timeout); \
3317 while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) && \
3318 __ret) { \
3319 spin_unlock_irq(&gcwq->lock); \
3320 __wait_event_timeout(gcwq->trustee_wait, (cond) || \
3321 (gcwq->trustee_state == TRUSTEE_RELEASE), \
3322 __ret); \
3323 spin_lock_irq(&gcwq->lock); \
3324 } \
3325 gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret); \
3326})
3327
3328/**
3329 * trustee_wait_event - event wait for trustee
3330 * @cond: condition to wait for
3331 *
3332 * wait_event() for trustee to use. Automatically handles locking and
3333 * checks for CANCEL request.
3334 *
3335 * CONTEXT:
3336 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3337 * multiple times. To be used by trustee.
3338 *
3339 * RETURNS:
3340 * 0 if @cond is satisfied, -1 if canceled.
3341 */
3342#define trustee_wait_event(cond) ({ \
3343 long __ret1; \
3344 __ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\
3345 __ret1 < 0 ? -1 : 0; \
3346})
3347
3348static int __cpuinit trustee_thread(void *__gcwq)
3349{
3350 struct global_cwq *gcwq = __gcwq;
3351 struct worker *worker;
Tejun Heoe22bee72010-06-29 10:07:14 +02003352 struct work_struct *work;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003353 struct hlist_node *pos;
Tejun Heoe22bee72010-06-29 10:07:14 +02003354 long rc;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003355 int i;
3356
3357 BUG_ON(gcwq->cpu != smp_processor_id());
3358
3359 spin_lock_irq(&gcwq->lock);
3360 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003361 * Claim the manager position and make all workers rogue.
3362 * Trustee must be bound to the target cpu and can't be
3363 * cancelled.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003364 */
3365 BUG_ON(gcwq->cpu != smp_processor_id());
Tejun Heo11ebea52012-07-12 14:46:37 -07003366 rc = trustee_wait_event(!(gcwq->pool.flags & POOL_MANAGING_WORKERS));
Tejun Heoe22bee72010-06-29 10:07:14 +02003367 BUG_ON(rc < 0);
3368
Tejun Heo11ebea52012-07-12 14:46:37 -07003369 gcwq->pool.flags |= POOL_MANAGING_WORKERS;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003370
Tejun Heobd7bdd42012-07-12 14:46:37 -07003371 list_for_each_entry(worker, &gcwq->pool.idle_list, entry)
Tejun Heocb444762010-07-02 10:03:50 +02003372 worker->flags |= WORKER_ROGUE;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003373
3374 for_each_busy_worker(worker, i, pos, gcwq)
Tejun Heocb444762010-07-02 10:03:50 +02003375 worker->flags |= WORKER_ROGUE;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003376
3377 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003378 * Call schedule() so that we cross rq->lock and thus can
3379 * guarantee sched callbacks see the rogue flag. This is
3380 * necessary as scheduler callbacks may be invoked from other
3381 * cpus.
3382 */
3383 spin_unlock_irq(&gcwq->lock);
3384 schedule();
3385 spin_lock_irq(&gcwq->lock);
3386
3387 /*
Tejun Heocb444762010-07-02 10:03:50 +02003388 * Sched callbacks are disabled now. Zap nr_running. After
3389 * this, nr_running stays zero and need_more_worker() and
3390 * keep_working() are always true as long as the worklist is
3391 * not empty.
Tejun Heoe22bee72010-06-29 10:07:14 +02003392 */
Tejun Heo63d95a92012-07-12 14:46:37 -07003393 atomic_set(get_pool_nr_running(&gcwq->pool), 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003394
3395 spin_unlock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07003396 del_timer_sync(&gcwq->pool.idle_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003397 spin_lock_irq(&gcwq->lock);
3398
3399 /*
Tejun Heodb7bccf2010-06-29 10:07:12 +02003400 * We're now in charge. Notify and proceed to drain. We need
3401 * to keep the gcwq running during the whole CPU down
3402 * procedure as other cpu hotunplug callbacks may need to
3403 * flush currently running tasks.
3404 */
3405 gcwq->trustee_state = TRUSTEE_IN_CHARGE;
3406 wake_up_all(&gcwq->trustee_wait);
3407
3408 /*
3409 * The original cpu is in the process of dying and may go away
3410 * anytime now. When that happens, we and all workers would
Tejun Heoe22bee72010-06-29 10:07:14 +02003411 * be migrated to other cpus. Try draining any left work. We
3412 * want to get it over with ASAP - spam rescuers, wake up as
3413 * many idlers as necessary and create new ones till the
3414 * worklist is empty. Note that if the gcwq is frozen, there
Tejun Heo58a69cb2011-02-16 09:25:31 +01003415 * may be frozen works in freezable cwqs. Don't declare
Tejun Heoe22bee72010-06-29 10:07:14 +02003416 * completion while frozen.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003417 */
Tejun Heobd7bdd42012-07-12 14:46:37 -07003418 while (gcwq->pool.nr_workers != gcwq->pool.nr_idle ||
Tejun Heodb7bccf2010-06-29 10:07:12 +02003419 gcwq->flags & GCWQ_FREEZING ||
3420 gcwq->trustee_state == TRUSTEE_IN_CHARGE) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003421 int nr_works = 0;
3422
Tejun Heobd7bdd42012-07-12 14:46:37 -07003423 list_for_each_entry(work, &gcwq->pool.worklist, entry) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003424 send_mayday(work);
3425 nr_works++;
3426 }
3427
Tejun Heobd7bdd42012-07-12 14:46:37 -07003428 list_for_each_entry(worker, &gcwq->pool.idle_list, entry) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003429 if (!nr_works--)
3430 break;
3431 wake_up_process(worker->task);
3432 }
3433
Tejun Heo63d95a92012-07-12 14:46:37 -07003434 if (need_to_create_worker(&gcwq->pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003435 spin_unlock_irq(&gcwq->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07003436 worker = create_worker(&gcwq->pool, false);
Tejun Heoe22bee72010-06-29 10:07:14 +02003437 spin_lock_irq(&gcwq->lock);
3438 if (worker) {
Tejun Heocb444762010-07-02 10:03:50 +02003439 worker->flags |= WORKER_ROGUE;
Tejun Heoe22bee72010-06-29 10:07:14 +02003440 start_worker(worker);
3441 }
3442 }
3443
Tejun Heodb7bccf2010-06-29 10:07:12 +02003444 /* give a breather */
3445 if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN) < 0)
3446 break;
3447 }
3448
Tejun Heoe22bee72010-06-29 10:07:14 +02003449 /*
3450 * Either all works have been scheduled and cpu is down, or
3451 * cpu down has already been canceled. Wait for and butcher
3452 * all workers till we're canceled.
3453 */
3454 do {
Tejun Heobd7bdd42012-07-12 14:46:37 -07003455 rc = trustee_wait_event(!list_empty(&gcwq->pool.idle_list));
3456 while (!list_empty(&gcwq->pool.idle_list))
3457 destroy_worker(list_first_entry(&gcwq->pool.idle_list,
Tejun Heoe22bee72010-06-29 10:07:14 +02003458 struct worker, entry));
Tejun Heobd7bdd42012-07-12 14:46:37 -07003459 } while (gcwq->pool.nr_workers && rc >= 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003460
3461 /*
3462 * At this point, either draining has completed and no worker
3463 * is left, or cpu down has been canceled or the cpu is being
3464 * brought back up. There shouldn't be any idle one left.
3465 * Tell the remaining busy ones to rebind once it finishes the
3466 * currently scheduled works by scheduling the rebind_work.
3467 */
Tejun Heobd7bdd42012-07-12 14:46:37 -07003468 WARN_ON(!list_empty(&gcwq->pool.idle_list));
Tejun Heoe22bee72010-06-29 10:07:14 +02003469
3470 for_each_busy_worker(worker, i, pos, gcwq) {
3471 struct work_struct *rebind_work = &worker->rebind_work;
3472
3473 /*
3474 * Rebind_work may race with future cpu hotplug
3475 * operations. Use a separate flag to mark that
3476 * rebinding is scheduled.
3477 */
Tejun Heocb444762010-07-02 10:03:50 +02003478 worker->flags |= WORKER_REBIND;
3479 worker->flags &= ~WORKER_ROGUE;
Tejun Heoe22bee72010-06-29 10:07:14 +02003480
3481 /* queue rebind_work, wq doesn't matter, use the default one */
3482 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
3483 work_data_bits(rebind_work)))
3484 continue;
3485
3486 debug_work_activate(rebind_work);
Tejun Heod320c032010-06-29 10:07:14 +02003487 insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work,
Tejun Heoe22bee72010-06-29 10:07:14 +02003488 worker->scheduled.next,
3489 work_color_to_flags(WORK_NO_COLOR));
3490 }
3491
3492 /* relinquish manager role */
Tejun Heo11ebea52012-07-12 14:46:37 -07003493 gcwq->pool.flags &= ~POOL_MANAGING_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02003494
Tejun Heodb7bccf2010-06-29 10:07:12 +02003495 /* notify completion */
3496 gcwq->trustee = NULL;
3497 gcwq->trustee_state = TRUSTEE_DONE;
3498 wake_up_all(&gcwq->trustee_wait);
3499 spin_unlock_irq(&gcwq->lock);
3500 return 0;
3501}
3502
3503/**
3504 * wait_trustee_state - wait for trustee to enter the specified state
3505 * @gcwq: gcwq the trustee of interest belongs to
3506 * @state: target state to wait for
3507 *
3508 * Wait for the trustee to reach @state. DONE is already matched.
3509 *
3510 * CONTEXT:
3511 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3512 * multiple times. To be used by cpu_callback.
3513 */
3514static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09003515__releases(&gcwq->lock)
3516__acquires(&gcwq->lock)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003517{
3518 if (!(gcwq->trustee_state == state ||
3519 gcwq->trustee_state == TRUSTEE_DONE)) {
3520 spin_unlock_irq(&gcwq->lock);
3521 __wait_event(gcwq->trustee_wait,
3522 gcwq->trustee_state == state ||
3523 gcwq->trustee_state == TRUSTEE_DONE);
3524 spin_lock_irq(&gcwq->lock);
3525 }
3526}
3527
Oleg Nesterov3af244332007-05-09 02:34:09 -07003528static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
3529 unsigned long action,
3530 void *hcpu)
3531{
3532 unsigned int cpu = (unsigned long)hcpu;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003533 struct global_cwq *gcwq = get_gcwq(cpu);
3534 struct task_struct *new_trustee = NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +02003535 struct worker *uninitialized_var(new_worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003536 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003538 action &= ~CPU_TASKS_FROZEN;
3539
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003541 case CPU_DOWN_PREPARE:
3542 new_trustee = kthread_create(trustee_thread, gcwq,
3543 "workqueue_trustee/%d\n", cpu);
3544 if (IS_ERR(new_trustee))
3545 return notifier_from_errno(PTR_ERR(new_trustee));
3546 kthread_bind(new_trustee, cpu);
Tejun Heoe22bee72010-06-29 10:07:14 +02003547 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 case CPU_UP_PREPARE:
Tejun Heobd7bdd42012-07-12 14:46:37 -07003549 BUG_ON(gcwq->pool.first_idle);
Tejun Heo63d95a92012-07-12 14:46:37 -07003550 new_worker = create_worker(&gcwq->pool, false);
Tejun Heoe22bee72010-06-29 10:07:14 +02003551 if (!new_worker) {
3552 if (new_trustee)
3553 kthread_stop(new_trustee);
3554 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 }
3557
Tejun Heodb7bccf2010-06-29 10:07:12 +02003558 /* some are called w/ irq disabled, don't disturb irq status */
3559 spin_lock_irqsave(&gcwq->lock, flags);
3560
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003561 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003562 case CPU_DOWN_PREPARE:
3563 /* initialize trustee and tell it to acquire the gcwq */
3564 BUG_ON(gcwq->trustee || gcwq->trustee_state != TRUSTEE_DONE);
3565 gcwq->trustee = new_trustee;
3566 gcwq->trustee_state = TRUSTEE_START;
3567 wake_up_process(gcwq->trustee);
3568 wait_trustee_state(gcwq, TRUSTEE_IN_CHARGE);
Tejun Heoe22bee72010-06-29 10:07:14 +02003569 /* fall through */
3570 case CPU_UP_PREPARE:
Tejun Heobd7bdd42012-07-12 14:46:37 -07003571 BUG_ON(gcwq->pool.first_idle);
3572 gcwq->pool.first_idle = new_worker;
Tejun Heoe22bee72010-06-29 10:07:14 +02003573 break;
3574
3575 case CPU_DYING:
3576 /*
3577 * Before this, the trustee and all workers except for
3578 * the ones which are still executing works from
3579 * before the last CPU down must be on the cpu. After
3580 * this, they'll all be diasporas.
3581 */
3582 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003583 break;
3584
Oleg Nesterov3da1c842008-07-25 01:47:50 -07003585 case CPU_POST_DEAD:
Tejun Heodb7bccf2010-06-29 10:07:12 +02003586 gcwq->trustee_state = TRUSTEE_BUTCHER;
Tejun Heoe22bee72010-06-29 10:07:14 +02003587 /* fall through */
3588 case CPU_UP_CANCELED:
Tejun Heobd7bdd42012-07-12 14:46:37 -07003589 destroy_worker(gcwq->pool.first_idle);
3590 gcwq->pool.first_idle = NULL;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003591 break;
3592
3593 case CPU_DOWN_FAILED:
3594 case CPU_ONLINE:
Tejun Heoe22bee72010-06-29 10:07:14 +02003595 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003596 if (gcwq->trustee_state != TRUSTEE_DONE) {
3597 gcwq->trustee_state = TRUSTEE_RELEASE;
3598 wake_up_process(gcwq->trustee);
3599 wait_trustee_state(gcwq, TRUSTEE_DONE);
3600 }
3601
Tejun Heoe22bee72010-06-29 10:07:14 +02003602 /*
3603 * Trustee is done and there might be no worker left.
3604 * Put the first_idle in and request a real manager to
3605 * take a look.
3606 */
3607 spin_unlock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07003608 kthread_bind(gcwq->pool.first_idle->task, cpu);
Tejun Heoe22bee72010-06-29 10:07:14 +02003609 spin_lock_irq(&gcwq->lock);
Tejun Heo11ebea52012-07-12 14:46:37 -07003610 gcwq->pool.flags |= POOL_MANAGE_WORKERS;
Tejun Heobd7bdd42012-07-12 14:46:37 -07003611 start_worker(gcwq->pool.first_idle);
3612 gcwq->pool.first_idle = NULL;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003613 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003614 }
3615
Tejun Heodb7bccf2010-06-29 10:07:12 +02003616 spin_unlock_irqrestore(&gcwq->lock, flags);
3617
Tejun Heo15376632010-06-29 10:07:11 +02003618 return notifier_from_errno(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620
Rusty Russell2d3854a2008-11-05 13:39:10 +11003621#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003622
Rusty Russell2d3854a2008-11-05 13:39:10 +11003623struct work_for_cpu {
Andrew Morton6b44003e2009-04-09 09:50:37 -06003624 struct completion completion;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003625 long (*fn)(void *);
3626 void *arg;
3627 long ret;
3628};
3629
Andrew Morton6b44003e2009-04-09 09:50:37 -06003630static int do_work_for_cpu(void *_wfc)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003631{
Andrew Morton6b44003e2009-04-09 09:50:37 -06003632 struct work_for_cpu *wfc = _wfc;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003633 wfc->ret = wfc->fn(wfc->arg);
Andrew Morton6b44003e2009-04-09 09:50:37 -06003634 complete(&wfc->completion);
3635 return 0;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003636}
3637
3638/**
3639 * work_on_cpu - run a function in user context on a particular cpu
3640 * @cpu: the cpu to run on
3641 * @fn: the function to run
3642 * @arg: the function arg
3643 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003644 * This will return the value @fn returns.
3645 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b44003e2009-04-09 09:50:37 -06003646 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003647 */
3648long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3649{
Andrew Morton6b44003e2009-04-09 09:50:37 -06003650 struct task_struct *sub_thread;
3651 struct work_for_cpu wfc = {
3652 .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
3653 .fn = fn,
3654 .arg = arg,
3655 };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003656
Andrew Morton6b44003e2009-04-09 09:50:37 -06003657 sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
3658 if (IS_ERR(sub_thread))
3659 return PTR_ERR(sub_thread);
3660 kthread_bind(sub_thread, cpu);
3661 wake_up_process(sub_thread);
3662 wait_for_completion(&wfc.completion);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003663 return wfc.ret;
3664}
3665EXPORT_SYMBOL_GPL(work_on_cpu);
3666#endif /* CONFIG_SMP */
3667
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003668#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303669
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003670/**
3671 * freeze_workqueues_begin - begin freezing workqueues
3672 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003673 * Start freezing workqueues. After this function returns, all freezable
3674 * workqueues will queue new works to their frozen_works list instead of
3675 * gcwq->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003676 *
3677 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003678 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003679 */
3680void freeze_workqueues_begin(void)
3681{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003682 unsigned int cpu;
3683
3684 spin_lock(&workqueue_lock);
3685
3686 BUG_ON(workqueue_freezing);
3687 workqueue_freezing = true;
3688
Tejun Heof3421792010-07-02 10:03:51 +02003689 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003690 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003691 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003692
3693 spin_lock_irq(&gcwq->lock);
3694
Tejun Heodb7bccf2010-06-29 10:07:12 +02003695 BUG_ON(gcwq->flags & GCWQ_FREEZING);
3696 gcwq->flags |= GCWQ_FREEZING;
3697
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003698 list_for_each_entry(wq, &workqueues, list) {
3699 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3700
Tejun Heo58a69cb2011-02-16 09:25:31 +01003701 if (cwq && wq->flags & WQ_FREEZABLE)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003702 cwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003703 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003704
3705 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003706 }
3707
3708 spin_unlock(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003710
3711/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003712 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003713 *
3714 * Check whether freezing is complete. This function must be called
3715 * between freeze_workqueues_begin() and thaw_workqueues().
3716 *
3717 * CONTEXT:
3718 * Grabs and releases workqueue_lock.
3719 *
3720 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01003721 * %true if some freezable workqueues are still busy. %false if freezing
3722 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003723 */
3724bool freeze_workqueues_busy(void)
3725{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003726 unsigned int cpu;
3727 bool busy = false;
3728
3729 spin_lock(&workqueue_lock);
3730
3731 BUG_ON(!workqueue_freezing);
3732
Tejun Heof3421792010-07-02 10:03:51 +02003733 for_each_gcwq_cpu(cpu) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003734 struct workqueue_struct *wq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003735 /*
3736 * nr_active is monotonically decreasing. It's safe
3737 * to peek without lock.
3738 */
3739 list_for_each_entry(wq, &workqueues, list) {
3740 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3741
Tejun Heo58a69cb2011-02-16 09:25:31 +01003742 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003743 continue;
3744
3745 BUG_ON(cwq->nr_active < 0);
3746 if (cwq->nr_active) {
3747 busy = true;
3748 goto out_unlock;
3749 }
3750 }
3751 }
3752out_unlock:
3753 spin_unlock(&workqueue_lock);
3754 return busy;
3755}
3756
3757/**
3758 * thaw_workqueues - thaw workqueues
3759 *
3760 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo7e116292010-06-29 10:07:13 +02003761 * frozen works are transferred to their respective gcwq worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003762 *
3763 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003764 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003765 */
3766void thaw_workqueues(void)
3767{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003768 unsigned int cpu;
3769
3770 spin_lock(&workqueue_lock);
3771
3772 if (!workqueue_freezing)
3773 goto out_unlock;
3774
Tejun Heof3421792010-07-02 10:03:51 +02003775 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003776 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003777 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003778
3779 spin_lock_irq(&gcwq->lock);
3780
Tejun Heodb7bccf2010-06-29 10:07:12 +02003781 BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
3782 gcwq->flags &= ~GCWQ_FREEZING;
3783
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003784 list_for_each_entry(wq, &workqueues, list) {
3785 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3786
Tejun Heo58a69cb2011-02-16 09:25:31 +01003787 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003788 continue;
3789
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003790 /* restore max_active and repopulate worklist */
3791 cwq->max_active = wq->saved_max_active;
3792
3793 while (!list_empty(&cwq->delayed_works) &&
3794 cwq->nr_active < cwq->max_active)
3795 cwq_activate_first_delayed(cwq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003796 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003797
Tejun Heo63d95a92012-07-12 14:46:37 -07003798 wake_up_worker(&gcwq->pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02003799
Tejun Heo8b03ae32010-06-29 10:07:12 +02003800 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003801 }
3802
3803 workqueue_freezing = false;
3804out_unlock:
3805 spin_unlock(&workqueue_lock);
3806}
3807#endif /* CONFIG_FREEZER */
3808
Suresh Siddha6ee05782010-07-30 14:57:37 -07003809static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810{
Tejun Heoc34056a2010-06-29 10:07:11 +02003811 unsigned int cpu;
Tejun Heoc8e55f32010-06-29 10:07:12 +02003812 int i;
Tejun Heoc34056a2010-06-29 10:07:11 +02003813
Tejun Heof6500942010-08-09 11:50:34 +02003814 cpu_notifier(workqueue_cpu_callback, CPU_PRI_WORKQUEUE);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003815
3816 /* initialize gcwqs */
Tejun Heof3421792010-07-02 10:03:51 +02003817 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003818 struct global_cwq *gcwq = get_gcwq(cpu);
3819
3820 spin_lock_init(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07003821 gcwq->pool.gcwq = gcwq;
3822 INIT_LIST_HEAD(&gcwq->pool.worklist);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003823 gcwq->cpu = cpu;
Tejun Heo477a3c32010-08-31 10:54:35 +02003824 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003825
Tejun Heobd7bdd42012-07-12 14:46:37 -07003826 INIT_LIST_HEAD(&gcwq->pool.idle_list);
Tejun Heoc8e55f32010-06-29 10:07:12 +02003827 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
3828 INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
3829
Tejun Heobd7bdd42012-07-12 14:46:37 -07003830 init_timer_deferrable(&gcwq->pool.idle_timer);
3831 gcwq->pool.idle_timer.function = idle_worker_timeout;
Tejun Heo63d95a92012-07-12 14:46:37 -07003832 gcwq->pool.idle_timer.data = (unsigned long)&gcwq->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003833
Tejun Heobd7bdd42012-07-12 14:46:37 -07003834 setup_timer(&gcwq->pool.mayday_timer, gcwq_mayday_timeout,
Tejun Heo63d95a92012-07-12 14:46:37 -07003835 (unsigned long)&gcwq->pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02003836
Tejun Heobd7bdd42012-07-12 14:46:37 -07003837 ida_init(&gcwq->pool.worker_ida);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003838
3839 gcwq->trustee_state = TRUSTEE_DONE;
3840 init_waitqueue_head(&gcwq->trustee_wait);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003841 }
3842
Tejun Heoe22bee72010-06-29 10:07:14 +02003843 /* create the initial worker */
Tejun Heof3421792010-07-02 10:03:51 +02003844 for_each_online_gcwq_cpu(cpu) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003845 struct global_cwq *gcwq = get_gcwq(cpu);
3846 struct worker *worker;
3847
Tejun Heo477a3c32010-08-31 10:54:35 +02003848 if (cpu != WORK_CPU_UNBOUND)
3849 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heo63d95a92012-07-12 14:46:37 -07003850 worker = create_worker(&gcwq->pool, true);
Tejun Heoe22bee72010-06-29 10:07:14 +02003851 BUG_ON(!worker);
3852 spin_lock_irq(&gcwq->lock);
3853 start_worker(worker);
3854 spin_unlock_irq(&gcwq->lock);
3855 }
3856
Tejun Heod320c032010-06-29 10:07:14 +02003857 system_wq = alloc_workqueue("events", 0, 0);
3858 system_long_wq = alloc_workqueue("events_long", 0, 0);
3859 system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0);
Tejun Heof3421792010-07-02 10:03:51 +02003860 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3861 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01003862 system_freezable_wq = alloc_workqueue("events_freezable",
3863 WQ_FREEZABLE, 0);
Alan Stern62d3c542012-03-02 10:51:00 +01003864 system_nrt_freezable_wq = alloc_workqueue("events_nrt_freezable",
3865 WQ_NON_REENTRANT | WQ_FREEZABLE, 0);
Hitoshi Mitakee5cba242010-11-26 12:06:44 +01003866 BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq ||
Alan Stern62d3c542012-03-02 10:51:00 +01003867 !system_unbound_wq || !system_freezable_wq ||
3868 !system_nrt_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07003869 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870}
Suresh Siddha6ee05782010-07-30 14:57:37 -07003871early_initcall(init_workqueues);