blob: 224580f7459ca3d12dc34f7fff2ee02fd758179d [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 Lameter469340232006-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>
Sasha Levin42f85702012-12-17 10:01:23 -050044#include <linux/hashtable.h>
Tejun Heoe22bee72010-06-29 10:07:14 +020045
Tejun Heoea138442013-01-18 14:05:55 -080046#include "workqueue_internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Tejun Heoc8e55f32010-06-29 10:07:12 +020048enum {
Tejun Heobc2ae0f2012-07-17 12:39:27 -070049 /*
Tejun Heo24647572013-01-24 11:01:33 -080050 * worker_pool flags
Tejun Heobc2ae0f2012-07-17 12:39:27 -070051 *
Tejun Heo24647572013-01-24 11:01:33 -080052 * A bound pool is either associated or disassociated with its CPU.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070053 * While associated (!DISASSOCIATED), all workers are bound to the
54 * CPU and none has %WORKER_UNBOUND set and concurrency management
55 * is in effect.
56 *
57 * While DISASSOCIATED, the cpu may be offline and all workers have
58 * %WORKER_UNBOUND set and concurrency management disabled, and may
Tejun Heo24647572013-01-24 11:01:33 -080059 * be executing on any CPU. The pool behaves as an unbound one.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070060 *
61 * Note that DISASSOCIATED can be flipped only while holding
Tejun Heo24647572013-01-24 11:01:33 -080062 * assoc_mutex to avoid changing binding state while
63 * create_worker() is in progress.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070064 */
Tejun Heo11ebea52012-07-12 14:46:37 -070065 POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
Lai Jiangshan552a37e2012-09-10 10:03:33 -070066 POOL_MANAGING_WORKERS = 1 << 1, /* managing workers */
Tejun Heo24647572013-01-24 11:01:33 -080067 POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */
Tejun Heo35b6bb62013-01-24 11:01:33 -080068 POOL_FREEZING = 1 << 3, /* freeze in progress */
Tejun Heodb7bccf2010-06-29 10:07:12 +020069
Tejun Heoc8e55f32010-06-29 10:07:12 +020070 /* worker flags */
71 WORKER_STARTED = 1 << 0, /* started */
72 WORKER_DIE = 1 << 1, /* die die die */
73 WORKER_IDLE = 1 << 2, /* is idle */
Tejun Heoe22bee72010-06-29 10:07:14 +020074 WORKER_PREP = 1 << 3, /* preparing to run works */
Tejun Heofb0e7be2010-06-29 10:07:15 +020075 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
Tejun Heof3421792010-07-02 10:03:51 +020076 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
Tejun Heoe22bee72010-06-29 10:07:14 +020077
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -070078 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_UNBOUND |
Tejun Heo403c8212012-07-17 12:39:27 -070079 WORKER_CPU_INTENSIVE,
Tejun Heodb7bccf2010-06-29 10:07:12 +020080
Tejun Heoe34cdddb2013-01-24 11:01:33 -080081 NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */
Tejun Heo4ce62e92012-07-13 22:16:44 -070082
Tejun Heoc8e55f32010-06-29 10:07:12 +020083 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
Tejun Heodb7bccf2010-06-29 10:07:12 +020084
Tejun Heoe22bee72010-06-29 10:07:14 +020085 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
86 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
87
Tejun Heo3233cdb2011-02-16 18:10:19 +010088 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
89 /* call for help after 10ms
90 (min two ticks) */
Tejun Heoe22bee72010-06-29 10:07:14 +020091 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
92 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heoe22bee72010-06-29 10:07:14 +020093
94 /*
95 * Rescue workers are used only on emergencies and shared by
96 * all cpus. Give -20.
97 */
98 RESCUER_NICE_LEVEL = -20,
Tejun Heo32704762012-07-13 22:16:45 -070099 HIGHPRI_NICE_LEVEL = -20,
Tejun Heoc8e55f32010-06-29 10:07:12 +0200100};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102/*
Tejun Heo4690c4a2010-06-29 10:07:10 +0200103 * Structure fields follow one of the following exclusion rules.
104 *
Tejun Heoe41e7042010-08-24 14:22:47 +0200105 * I: Modifiable by initialization/destruction paths and read-only for
106 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200107 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200108 * P: Preemption protected. Disabling preemption is enough and should
109 * only be modified and accessed from the local cpu.
110 *
Tejun Heod565ed62013-01-24 11:01:33 -0800111 * L: pool->lock protected. Access with pool->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200112 *
Tejun Heod565ed62013-01-24 11:01:33 -0800113 * X: During normal operation, modification requires pool->lock and should
114 * be done only from local cpu. Either disabling preemption on local
115 * cpu or grabbing pool->lock is enough for read access. If
116 * POOL_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200117 *
Tejun Heo73f53c42010-06-29 10:07:11 +0200118 * F: wq->flush_mutex protected.
119 *
Tejun Heo4690c4a2010-06-29 10:07:10 +0200120 * W: workqueue_lock protected.
121 */
122
Tejun Heo2eaebdb2013-01-18 14:05:55 -0800123/* struct worker is defined in workqueue_internal.h */
Tejun Heoc34056a2010-06-29 10:07:11 +0200124
Tejun Heobd7bdd42012-07-12 14:46:37 -0700125struct worker_pool {
Tejun Heod565ed62013-01-24 11:01:33 -0800126 spinlock_t lock; /* the pool lock */
Tejun Heoec22ca52013-01-24 11:01:33 -0800127 unsigned int cpu; /* I: the associated cpu */
Tejun Heo9daf9e62013-01-24 11:01:33 -0800128 int id; /* I: pool ID */
Tejun Heo11ebea52012-07-12 14:46:37 -0700129 unsigned int flags; /* X: flags */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700130
131 struct list_head worklist; /* L: list of pending works */
132 int nr_workers; /* L: total number of workers */
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700133
134 /* nr_idle includes the ones off idle_list for rebinding */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700135 int nr_idle; /* L: currently idle ones */
136
137 struct list_head idle_list; /* X: list of idle workers */
138 struct timer_list idle_timer; /* L: worker idle timeout */
139 struct timer_list mayday_timer; /* L: SOS timer for workers */
140
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800141 /* workers are chained either in busy_hash or idle_list */
142 DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER);
143 /* L: hash of busy workers */
144
Tejun Heo24647572013-01-24 11:01:33 -0800145 struct mutex assoc_mutex; /* protect POOL_DISASSOCIATED */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700146 struct ida worker_ida; /* L: for worker IDs */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200147} ____cacheline_aligned_in_smp;
148
149/*
Tejun Heo502ca9d2010-06-29 10:07:13 +0200150 * The per-CPU workqueue. The lower WORK_STRUCT_FLAG_BITS of
Tejun Heo0f900042010-06-29 10:07:11 +0200151 * work_struct->data are used for flags and thus cwqs need to be
152 * aligned at two's power of the number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 */
154struct cpu_workqueue_struct {
Tejun Heobd7bdd42012-07-12 14:46:37 -0700155 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200156 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200157 int work_color; /* L: current color */
158 int flush_color; /* L: flushing color */
159 int nr_in_flight[WORK_NR_COLORS];
160 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200161 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200162 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200163 struct list_head delayed_works; /* L: delayed works */
Tejun Heo0f900042010-06-29 10:07:11 +0200164};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200167 * Structure used to wait for workqueue flush.
168 */
169struct wq_flusher {
170 struct list_head list; /* F: list of flushers */
171 int flush_color; /* F: flush color waiting for */
172 struct completion done; /* flush completion */
173};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Tejun Heo73f53c42010-06-29 10:07:11 +0200175/*
Tejun Heof2e005a2010-07-20 15:59:09 +0200176 * All cpumasks are assumed to be always set on UP and thus can't be
177 * used to determine whether there's something to be done.
178 */
179#ifdef CONFIG_SMP
180typedef cpumask_var_t mayday_mask_t;
181#define mayday_test_and_set_cpu(cpu, mask) \
182 cpumask_test_and_set_cpu((cpu), (mask))
183#define mayday_clear_cpu(cpu, mask) cpumask_clear_cpu((cpu), (mask))
184#define for_each_mayday_cpu(cpu, mask) for_each_cpu((cpu), (mask))
Tejun Heo9c375472010-08-31 11:18:34 +0200185#define alloc_mayday_mask(maskp, gfp) zalloc_cpumask_var((maskp), (gfp))
Tejun Heof2e005a2010-07-20 15:59:09 +0200186#define free_mayday_mask(mask) free_cpumask_var((mask))
187#else
188typedef unsigned long mayday_mask_t;
189#define mayday_test_and_set_cpu(cpu, mask) test_and_set_bit(0, &(mask))
190#define mayday_clear_cpu(cpu, mask) clear_bit(0, &(mask))
191#define for_each_mayday_cpu(cpu, mask) if ((cpu) = 0, (mask))
192#define alloc_mayday_mask(maskp, gfp) true
193#define free_mayday_mask(mask) do { } while (0)
194#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196/*
197 * The externally visible workqueue abstraction is an array of
198 * per-CPU workqueues:
199 */
200struct workqueue_struct {
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200201 unsigned int flags; /* W: WQ_* flags */
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200202 union {
203 struct cpu_workqueue_struct __percpu *pcpu;
204 struct cpu_workqueue_struct *single;
205 unsigned long v;
206 } cpu_wq; /* I: cwq's */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200207 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200208
209 struct mutex flush_mutex; /* protects wq flushing */
210 int work_color; /* F: current work color */
211 int flush_color; /* F: current flush color */
212 atomic_t nr_cwqs_to_flush; /* flush in progress */
213 struct wq_flusher *first_flusher; /* F: first flusher */
214 struct list_head flusher_queue; /* F: flush waiters */
215 struct list_head flusher_overflow; /* F: flush overflow list */
216
Tejun Heof2e005a2010-07-20 15:59:09 +0200217 mayday_mask_t mayday_mask; /* cpus requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200218 struct worker *rescuer; /* I: rescue worker */
219
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200220 int nr_drainers; /* W: drain in progress */
Tejun Heodcd989c2010-06-29 10:07:14 +0200221 int saved_max_active; /* W: saved cwq max_active */
Johannes Berg4e6045f2007-10-18 23:39:55 -0700222#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200223 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700224#endif
Tejun Heob196be82012-01-10 15:11:35 -0800225 char name[]; /* I: workqueue name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226};
227
Tejun Heod320c032010-06-29 10:07:14 +0200228struct workqueue_struct *system_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200229EXPORT_SYMBOL_GPL(system_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300230struct workqueue_struct *system_highpri_wq __read_mostly;
Joonsoo Kim1aabe902012-08-15 23:25:39 +0900231EXPORT_SYMBOL_GPL(system_highpri_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300232struct workqueue_struct *system_long_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200233EXPORT_SYMBOL_GPL(system_long_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300234struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200235EXPORT_SYMBOL_GPL(system_unbound_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300236struct workqueue_struct *system_freezable_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100237EXPORT_SYMBOL_GPL(system_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200238
Tejun Heo97bd2342010-10-05 10:41:14 +0200239#define CREATE_TRACE_POINTS
240#include <trace/events/workqueue.h>
241
Tejun Heo38db41d2013-01-24 11:01:34 -0800242#define for_each_std_worker_pool(pool, cpu) \
Tejun Heoa60dc392013-01-24 11:01:34 -0800243 for ((pool) = &std_worker_pools(cpu)[0]; \
244 (pool) < &std_worker_pools(cpu)[NR_STD_WORKER_POOLS]; (pool)++)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700245
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800246#define for_each_busy_worker(worker, i, pos, pool) \
247 hash_for_each(pool->busy_hash, i, pos, worker, hentry)
Tejun Heodb7bccf2010-06-29 10:07:12 +0200248
Tejun Heof3421792010-07-02 10:03:51 +0200249static inline int __next_gcwq_cpu(int cpu, const struct cpumask *mask,
250 unsigned int sw)
251{
252 if (cpu < nr_cpu_ids) {
253 if (sw & 1) {
254 cpu = cpumask_next(cpu, mask);
255 if (cpu < nr_cpu_ids)
256 return cpu;
257 }
258 if (sw & 2)
259 return WORK_CPU_UNBOUND;
260 }
261 return WORK_CPU_NONE;
262}
263
264static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
265 struct workqueue_struct *wq)
266{
267 return __next_gcwq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2);
268}
269
Tejun Heo09884952010-08-01 11:50:12 +0200270/*
271 * CPU iterators
272 *
273 * An extra gcwq is defined for an invalid cpu number
274 * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any
275 * specific CPU. The following iterators are similar to
276 * for_each_*_cpu() iterators but also considers the unbound gcwq.
277 *
278 * for_each_gcwq_cpu() : possible CPUs + WORK_CPU_UNBOUND
279 * for_each_online_gcwq_cpu() : online CPUs + WORK_CPU_UNBOUND
280 * for_each_cwq_cpu() : possible CPUs for bound workqueues,
281 * WORK_CPU_UNBOUND for unbound workqueues
282 */
Tejun Heof3421792010-07-02 10:03:51 +0200283#define for_each_gcwq_cpu(cpu) \
284 for ((cpu) = __next_gcwq_cpu(-1, cpu_possible_mask, 3); \
285 (cpu) < WORK_CPU_NONE; \
286 (cpu) = __next_gcwq_cpu((cpu), cpu_possible_mask, 3))
287
288#define for_each_online_gcwq_cpu(cpu) \
289 for ((cpu) = __next_gcwq_cpu(-1, cpu_online_mask, 3); \
290 (cpu) < WORK_CPU_NONE; \
291 (cpu) = __next_gcwq_cpu((cpu), cpu_online_mask, 3))
292
293#define for_each_cwq_cpu(cpu, wq) \
294 for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, (wq)); \
295 (cpu) < WORK_CPU_NONE; \
296 (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq)))
297
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900298#ifdef CONFIG_DEBUG_OBJECTS_WORK
299
300static struct debug_obj_descr work_debug_descr;
301
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100302static void *work_debug_hint(void *addr)
303{
304 return ((struct work_struct *) addr)->func;
305}
306
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900307/*
308 * fixup_init is called when:
309 * - an active object is initialized
310 */
311static int work_fixup_init(void *addr, enum debug_obj_state state)
312{
313 struct work_struct *work = addr;
314
315 switch (state) {
316 case ODEBUG_STATE_ACTIVE:
317 cancel_work_sync(work);
318 debug_object_init(work, &work_debug_descr);
319 return 1;
320 default:
321 return 0;
322 }
323}
324
325/*
326 * fixup_activate is called when:
327 * - an active object is activated
328 * - an unknown object is activated (might be a statically initialized object)
329 */
330static int work_fixup_activate(void *addr, enum debug_obj_state state)
331{
332 struct work_struct *work = addr;
333
334 switch (state) {
335
336 case ODEBUG_STATE_NOTAVAILABLE:
337 /*
338 * This is not really a fixup. The work struct was
339 * statically initialized. We just make sure that it
340 * is tracked in the object tracker.
341 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200342 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900343 debug_object_init(work, &work_debug_descr);
344 debug_object_activate(work, &work_debug_descr);
345 return 0;
346 }
347 WARN_ON_ONCE(1);
348 return 0;
349
350 case ODEBUG_STATE_ACTIVE:
351 WARN_ON(1);
352
353 default:
354 return 0;
355 }
356}
357
358/*
359 * fixup_free is called when:
360 * - an active object is freed
361 */
362static int work_fixup_free(void *addr, enum debug_obj_state state)
363{
364 struct work_struct *work = addr;
365
366 switch (state) {
367 case ODEBUG_STATE_ACTIVE:
368 cancel_work_sync(work);
369 debug_object_free(work, &work_debug_descr);
370 return 1;
371 default:
372 return 0;
373 }
374}
375
376static struct debug_obj_descr work_debug_descr = {
377 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100378 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900379 .fixup_init = work_fixup_init,
380 .fixup_activate = work_fixup_activate,
381 .fixup_free = work_fixup_free,
382};
383
384static inline void debug_work_activate(struct work_struct *work)
385{
386 debug_object_activate(work, &work_debug_descr);
387}
388
389static inline void debug_work_deactivate(struct work_struct *work)
390{
391 debug_object_deactivate(work, &work_debug_descr);
392}
393
394void __init_work(struct work_struct *work, int onstack)
395{
396 if (onstack)
397 debug_object_init_on_stack(work, &work_debug_descr);
398 else
399 debug_object_init(work, &work_debug_descr);
400}
401EXPORT_SYMBOL_GPL(__init_work);
402
403void destroy_work_on_stack(struct work_struct *work)
404{
405 debug_object_free(work, &work_debug_descr);
406}
407EXPORT_SYMBOL_GPL(destroy_work_on_stack);
408
409#else
410static inline void debug_work_activate(struct work_struct *work) { }
411static inline void debug_work_deactivate(struct work_struct *work) { }
412#endif
413
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100414/* Serializes the accesses to the list of workqueues. */
415static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416static LIST_HEAD(workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200417static bool workqueue_freezing; /* W: have wqs started freezing? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Oleg Nesterov14441962007-05-23 13:57:57 -0700419/*
Tejun Heoa60dc392013-01-24 11:01:34 -0800420 * The CPU standard worker pools. nr_running is the only field which is
421 * expected to be used frequently by other cpus via try_to_wake_up(). Put
422 * it in a separate cacheline.
Oleg Nesterov14441962007-05-23 13:57:57 -0700423 */
Tejun Heoa60dc392013-01-24 11:01:34 -0800424static DEFINE_PER_CPU(struct worker_pool [NR_STD_WORKER_POOLS],
425 cpu_std_worker_pools);
Tejun Heoe34cdddb2013-01-24 11:01:33 -0800426static DEFINE_PER_CPU_SHARED_ALIGNED(atomic_t, pool_nr_running[NR_STD_WORKER_POOLS]);
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800427
Tejun Heof3421792010-07-02 10:03:51 +0200428/*
Tejun Heoa60dc392013-01-24 11:01:34 -0800429 * Standard worker pools and nr_running counter for unbound CPU. The pools
430 * have POOL_DISASSOCIATED set, and all workers have WORKER_UNBOUND set.
Tejun Heof3421792010-07-02 10:03:51 +0200431 */
Tejun Heoa60dc392013-01-24 11:01:34 -0800432static struct worker_pool unbound_std_worker_pools[NR_STD_WORKER_POOLS];
Tejun Heoe34cdddb2013-01-24 11:01:33 -0800433static atomic_t unbound_pool_nr_running[NR_STD_WORKER_POOLS] = {
434 [0 ... NR_STD_WORKER_POOLS - 1] = ATOMIC_INIT(0), /* always 0 */
Tejun Heo4ce62e92012-07-13 22:16:44 -0700435};
Tejun Heof3421792010-07-02 10:03:51 +0200436
Tejun Heo9daf9e62013-01-24 11:01:33 -0800437/* idr of all pools */
438static DEFINE_MUTEX(worker_pool_idr_mutex);
439static DEFINE_IDR(worker_pool_idr);
440
Tejun Heoc34056a2010-06-29 10:07:11 +0200441static int worker_thread(void *__worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Tejun Heoa60dc392013-01-24 11:01:34 -0800443static struct worker_pool *std_worker_pools(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Tejun Heof3421792010-07-02 10:03:51 +0200445 if (cpu != WORK_CPU_UNBOUND)
Tejun Heoa60dc392013-01-24 11:01:34 -0800446 return per_cpu(cpu_std_worker_pools, cpu);
Tejun Heof3421792010-07-02 10:03:51 +0200447 else
Tejun Heoa60dc392013-01-24 11:01:34 -0800448 return unbound_std_worker_pools;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
Tejun Heo4e8f0a62013-01-24 11:01:34 -0800451static int std_worker_pool_pri(struct worker_pool *pool)
452{
Tejun Heoa60dc392013-01-24 11:01:34 -0800453 return pool - std_worker_pools(pool->cpu);
Tejun Heo4e8f0a62013-01-24 11:01:34 -0800454}
455
Tejun Heo9daf9e62013-01-24 11:01:33 -0800456/* allocate ID and assign it to @pool */
457static int worker_pool_assign_id(struct worker_pool *pool)
458{
459 int ret;
460
461 mutex_lock(&worker_pool_idr_mutex);
462 idr_pre_get(&worker_pool_idr, GFP_KERNEL);
463 ret = idr_get_new(&worker_pool_idr, pool, &pool->id);
464 mutex_unlock(&worker_pool_idr_mutex);
465
466 return ret;
467}
468
Tejun Heo7c3eed52013-01-24 11:01:33 -0800469/*
470 * Lookup worker_pool by id. The idr currently is built during boot and
471 * never modified. Don't worry about locking for now.
472 */
473static struct worker_pool *worker_pool_by_id(int pool_id)
474{
475 return idr_find(&worker_pool_idr, pool_id);
476}
477
Tejun Heod565ed62013-01-24 11:01:33 -0800478static struct worker_pool *get_std_worker_pool(int cpu, bool highpri)
479{
Tejun Heoa60dc392013-01-24 11:01:34 -0800480 struct worker_pool *pools = std_worker_pools(cpu);
Tejun Heod565ed62013-01-24 11:01:33 -0800481
Tejun Heoa60dc392013-01-24 11:01:34 -0800482 return &pools[highpri];
Tejun Heod565ed62013-01-24 11:01:33 -0800483}
484
Tejun Heo63d95a92012-07-12 14:46:37 -0700485static atomic_t *get_pool_nr_running(struct worker_pool *pool)
Oleg Nesterovb1f4ec172007-05-09 02:34:12 -0700486{
Tejun Heoec22ca52013-01-24 11:01:33 -0800487 int cpu = pool->cpu;
Tejun Heoe34cdddb2013-01-24 11:01:33 -0800488 int idx = std_worker_pool_pri(pool);
Tejun Heo63d95a92012-07-12 14:46:37 -0700489
Tejun Heof3421792010-07-02 10:03:51 +0200490 if (cpu != WORK_CPU_UNBOUND)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700491 return &per_cpu(pool_nr_running, cpu)[idx];
Tejun Heof3421792010-07-02 10:03:51 +0200492 else
Tejun Heo4ce62e92012-07-13 22:16:44 -0700493 return &unbound_pool_nr_running[idx];
Oleg Nesterovb1f4ec172007-05-09 02:34:12 -0700494}
495
Tejun Heo4690c4a2010-06-29 10:07:10 +0200496static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
497 struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700498{
Tejun Heof3421792010-07-02 10:03:51 +0200499 if (!(wq->flags & WQ_UNBOUND)) {
Lai Jiangshane06ffa12012-03-09 18:03:20 +0800500 if (likely(cpu < nr_cpu_ids))
Tejun Heof3421792010-07-02 10:03:51 +0200501 return per_cpu_ptr(wq->cpu_wq.pcpu, cpu);
Tejun Heof3421792010-07-02 10:03:51 +0200502 } else if (likely(cpu == WORK_CPU_UNBOUND))
503 return wq->cpu_wq.single;
504 return NULL;
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700505}
506
Tejun Heo73f53c42010-06-29 10:07:11 +0200507static unsigned int work_color_to_flags(int color)
508{
509 return color << WORK_STRUCT_COLOR_SHIFT;
510}
511
512static int get_work_color(struct work_struct *work)
513{
514 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
515 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
516}
517
518static int work_next_color(int color)
519{
520 return (color + 1) % WORK_NR_COLORS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
David Howells4594bf12006-12-07 11:33:26 +0000523/*
Tejun Heob5490072012-08-03 10:30:46 -0700524 * While queued, %WORK_STRUCT_CWQ is set and non flag bits of a work's data
525 * contain the pointer to the queued cwq. Once execution starts, the flag
Tejun Heo7c3eed52013-01-24 11:01:33 -0800526 * is cleared and the high bits contain OFFQ flags and pool ID.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200527 *
Tejun Heo7c3eed52013-01-24 11:01:33 -0800528 * set_work_cwq(), set_work_pool_and_clear_pending(), mark_work_canceling()
529 * and clear_work_data() can be used to set the cwq, pool or clear
Tejun Heobbb68df2012-08-03 10:30:46 -0700530 * work->data. These functions should only be called while the work is
531 * owned - ie. while the PENDING bit is set.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200532 *
Tejun Heo7c3eed52013-01-24 11:01:33 -0800533 * get_work_pool() and get_work_cwq() can be used to obtain the pool or cwq
534 * corresponding to a work. Pool is available once the work has been
535 * queued anywhere after initialization until it is sync canceled. cwq is
536 * available only while the work item is queued.
Tejun Heobbb68df2012-08-03 10:30:46 -0700537 *
538 * %WORK_OFFQ_CANCELING is used to mark a work item which is being
539 * canceled. While being canceled, a work item may have its PENDING set
540 * but stay off timer and worklist for arbitrarily long and nobody should
541 * try to steal the PENDING bit.
David Howells4594bf12006-12-07 11:33:26 +0000542 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200543static inline void set_work_data(struct work_struct *work, unsigned long data,
544 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000545{
David Howells4594bf12006-12-07 11:33:26 +0000546 BUG_ON(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200547 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000548}
David Howells365970a2006-11-22 14:54:49 +0000549
Tejun Heo7a22ad72010-06-29 10:07:13 +0200550static void set_work_cwq(struct work_struct *work,
551 struct cpu_workqueue_struct *cwq,
552 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200553{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200554 set_work_data(work, (unsigned long)cwq,
Tejun Heoe1201532010-07-22 14:14:25 +0200555 WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200556}
557
Tejun Heo7c3eed52013-01-24 11:01:33 -0800558static void set_work_pool_and_clear_pending(struct work_struct *work,
559 int pool_id)
David Howells365970a2006-11-22 14:54:49 +0000560{
Tejun Heo23657bb2012-08-13 17:08:19 -0700561 /*
562 * The following wmb is paired with the implied mb in
563 * test_and_set_bit(PENDING) and ensures all updates to @work made
564 * here are visible to and precede any updates by the next PENDING
565 * owner.
566 */
567 smp_wmb();
Tejun Heo7c3eed52013-01-24 11:01:33 -0800568 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200569}
570
571static void clear_work_data(struct work_struct *work)
572{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800573 smp_wmb(); /* see set_work_pool_and_clear_pending() */
574 set_work_data(work, WORK_STRUCT_NO_POOL, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200575}
576
Tejun Heo7a22ad72010-06-29 10:07:13 +0200577static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work)
578{
Tejun Heoe1201532010-07-22 14:14:25 +0200579 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200580
Tejun Heoe1201532010-07-22 14:14:25 +0200581 if (data & WORK_STRUCT_CWQ)
582 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
583 else
584 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200585}
586
Tejun Heo7c3eed52013-01-24 11:01:33 -0800587/**
588 * get_work_pool - return the worker_pool a given work was associated with
589 * @work: the work item of interest
590 *
591 * Return the worker_pool @work was last associated with. %NULL if none.
592 */
593static struct worker_pool *get_work_pool(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200594{
Tejun Heoe1201532010-07-22 14:14:25 +0200595 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800596 struct worker_pool *pool;
597 int pool_id;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200598
Tejun Heoe1201532010-07-22 14:14:25 +0200599 if (data & WORK_STRUCT_CWQ)
600 return ((struct cpu_workqueue_struct *)
Tejun Heo7c3eed52013-01-24 11:01:33 -0800601 (data & WORK_STRUCT_WQ_DATA_MASK))->pool;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200602
Tejun Heo7c3eed52013-01-24 11:01:33 -0800603 pool_id = data >> WORK_OFFQ_POOL_SHIFT;
604 if (pool_id == WORK_OFFQ_POOL_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200605 return NULL;
606
Tejun Heo7c3eed52013-01-24 11:01:33 -0800607 pool = worker_pool_by_id(pool_id);
608 WARN_ON_ONCE(!pool);
609 return pool;
610}
611
612/**
613 * get_work_pool_id - return the worker pool ID a given work is associated with
614 * @work: the work item of interest
615 *
616 * Return the worker_pool ID @work was last associated with.
617 * %WORK_OFFQ_POOL_NONE if none.
618 */
619static int get_work_pool_id(struct work_struct *work)
620{
621 struct worker_pool *pool = get_work_pool(work);
622
623 return pool ? pool->id : WORK_OFFQ_POOL_NONE;
624}
625
Tejun Heobbb68df2012-08-03 10:30:46 -0700626static void mark_work_canceling(struct work_struct *work)
627{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800628 unsigned long pool_id = get_work_pool_id(work);
Tejun Heobbb68df2012-08-03 10:30:46 -0700629
Tejun Heo7c3eed52013-01-24 11:01:33 -0800630 pool_id <<= WORK_OFFQ_POOL_SHIFT;
631 set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700632}
633
634static bool work_is_canceling(struct work_struct *work)
635{
636 unsigned long data = atomic_long_read(&work->data);
637
638 return !(data & WORK_STRUCT_CWQ) && (data & WORK_OFFQ_CANCELING);
639}
640
David Howells365970a2006-11-22 14:54:49 +0000641/*
Tejun Heo32704762012-07-13 22:16:45 -0700642 * Policy functions. These define the policies on how the global worker
643 * pools are managed. Unless noted otherwise, these functions assume that
Tejun Heod565ed62013-01-24 11:01:33 -0800644 * they're being called with pool->lock held.
David Howells365970a2006-11-22 14:54:49 +0000645 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200646
Tejun Heo63d95a92012-07-12 14:46:37 -0700647static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000648{
Tejun Heo32704762012-07-13 22:16:45 -0700649 return !atomic_read(get_pool_nr_running(pool));
David Howells365970a2006-11-22 14:54:49 +0000650}
651
Tejun Heoe22bee72010-06-29 10:07:14 +0200652/*
653 * Need to wake up a worker? Called from anything but currently
654 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700655 *
656 * Note that, because unbound workers never contribute to nr_running, this
657 * function will always return %true for unbound gcwq as long as the
658 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200659 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700660static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000661{
Tejun Heo63d95a92012-07-12 14:46:37 -0700662 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000663}
664
Tejun Heoe22bee72010-06-29 10:07:14 +0200665/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700666static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200667{
Tejun Heo63d95a92012-07-12 14:46:37 -0700668 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200669}
670
671/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700672static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200673{
Tejun Heo63d95a92012-07-12 14:46:37 -0700674 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200675
Tejun Heo32704762012-07-13 22:16:45 -0700676 return !list_empty(&pool->worklist) && atomic_read(nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200677}
678
679/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700680static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200681{
Tejun Heo63d95a92012-07-12 14:46:37 -0700682 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200683}
684
685/* Do I need to be the manager? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700686static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200687{
Tejun Heo63d95a92012-07-12 14:46:37 -0700688 return need_to_create_worker(pool) ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700689 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200690}
691
692/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700693static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200694{
Lai Jiangshan552a37e2012-09-10 10:03:33 -0700695 bool managing = pool->flags & POOL_MANAGING_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -0700696 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
697 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200698
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700699 /*
700 * nr_idle and idle_list may disagree if idle rebinding is in
701 * progress. Never return %true if idle_list is empty.
702 */
703 if (list_empty(&pool->idle_list))
704 return false;
705
Tejun Heoe22bee72010-06-29 10:07:14 +0200706 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
707}
708
709/*
710 * Wake up functions.
711 */
712
Tejun Heo7e116292010-06-29 10:07:13 +0200713/* Return the first worker. Safe with preemption disabled */
Tejun Heo63d95a92012-07-12 14:46:37 -0700714static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200715{
Tejun Heo63d95a92012-07-12 14:46:37 -0700716 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200717 return NULL;
718
Tejun Heo63d95a92012-07-12 14:46:37 -0700719 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200720}
721
722/**
723 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700724 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200725 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700726 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200727 *
728 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800729 * spin_lock_irq(pool->lock).
Tejun Heo7e116292010-06-29 10:07:13 +0200730 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700731static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200732{
Tejun Heo63d95a92012-07-12 14:46:37 -0700733 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200734
735 if (likely(worker))
736 wake_up_process(worker->task);
737}
738
Tejun Heo4690c4a2010-06-29 10:07:10 +0200739/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200740 * wq_worker_waking_up - a worker is waking up
741 * @task: task waking up
742 * @cpu: CPU @task is waking up to
743 *
744 * This function is called during try_to_wake_up() when a worker is
745 * being awoken.
746 *
747 * CONTEXT:
748 * spin_lock_irq(rq->lock)
749 */
750void wq_worker_waking_up(struct task_struct *task, unsigned int cpu)
751{
752 struct worker *worker = kthread_data(task);
753
Joonsoo Kim36576002012-10-26 23:03:49 +0900754 if (!(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoec22ca52013-01-24 11:01:33 -0800755 WARN_ON_ONCE(worker->pool->cpu != cpu);
Tejun Heo63d95a92012-07-12 14:46:37 -0700756 atomic_inc(get_pool_nr_running(worker->pool));
Joonsoo Kim36576002012-10-26 23:03:49 +0900757 }
Tejun Heoe22bee72010-06-29 10:07:14 +0200758}
759
760/**
761 * wq_worker_sleeping - a worker is going to sleep
762 * @task: task going to sleep
763 * @cpu: CPU in question, must be the current CPU number
764 *
765 * This function is called during schedule() when a busy worker is
766 * going to sleep. Worker on the same cpu can be woken up by
767 * returning pointer to its task.
768 *
769 * CONTEXT:
770 * spin_lock_irq(rq->lock)
771 *
772 * RETURNS:
773 * Worker task on @cpu to wake up, %NULL if none.
774 */
775struct task_struct *wq_worker_sleeping(struct task_struct *task,
776 unsigned int cpu)
777{
778 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heo111c2252013-01-17 17:16:24 -0800779 struct worker_pool *pool;
780 atomic_t *nr_running;
Tejun Heoe22bee72010-06-29 10:07:14 +0200781
Tejun Heo111c2252013-01-17 17:16:24 -0800782 /*
783 * Rescuers, which may not have all the fields set up like normal
784 * workers, also reach here, let's not access anything before
785 * checking NOT_RUNNING.
786 */
Steven Rostedt2d646722010-12-03 23:12:33 -0500787 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200788 return NULL;
789
Tejun Heo111c2252013-01-17 17:16:24 -0800790 pool = worker->pool;
791 nr_running = get_pool_nr_running(pool);
792
Tejun Heoe22bee72010-06-29 10:07:14 +0200793 /* this can only happen on the local cpu */
794 BUG_ON(cpu != raw_smp_processor_id());
795
796 /*
797 * The counterpart of the following dec_and_test, implied mb,
798 * worklist not empty test sequence is in insert_work().
799 * Please read comment there.
800 *
Tejun Heo628c78e2012-07-17 12:39:27 -0700801 * NOT_RUNNING is clear. This means that we're bound to and
802 * running on the local cpu w/ rq lock held and preemption
803 * disabled, which in turn means that none else could be
Tejun Heod565ed62013-01-24 11:01:33 -0800804 * manipulating idle_list, so dereferencing idle_list without pool
Tejun Heo628c78e2012-07-17 12:39:27 -0700805 * lock is safe.
Tejun Heoe22bee72010-06-29 10:07:14 +0200806 */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700807 if (atomic_dec_and_test(nr_running) && !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700808 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200809 return to_wakeup ? to_wakeup->task : NULL;
810}
811
812/**
813 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200814 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200815 * @flags: flags to set
816 * @wakeup: wakeup an idle worker if necessary
817 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200818 * Set @flags in @worker->flags and adjust nr_running accordingly. If
819 * nr_running becomes zero and @wakeup is %true, an idle worker is
820 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200821 *
Tejun Heocb444762010-07-02 10:03:50 +0200822 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800823 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200824 */
825static inline void worker_set_flags(struct worker *worker, unsigned int flags,
826 bool wakeup)
827{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700828 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200829
Tejun Heocb444762010-07-02 10:03:50 +0200830 WARN_ON_ONCE(worker->task != current);
831
Tejun Heoe22bee72010-06-29 10:07:14 +0200832 /*
833 * If transitioning into NOT_RUNNING, adjust nr_running and
834 * wake up an idle worker as necessary if requested by
835 * @wakeup.
836 */
837 if ((flags & WORKER_NOT_RUNNING) &&
838 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heo63d95a92012-07-12 14:46:37 -0700839 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200840
841 if (wakeup) {
842 if (atomic_dec_and_test(nr_running) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -0700843 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700844 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200845 } else
846 atomic_dec(nr_running);
847 }
848
Tejun Heod302f012010-06-29 10:07:13 +0200849 worker->flags |= flags;
850}
851
852/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200853 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200854 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200855 * @flags: flags to clear
856 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200857 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200858 *
Tejun Heocb444762010-07-02 10:03:50 +0200859 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800860 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200861 */
862static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
863{
Tejun Heo63d95a92012-07-12 14:46:37 -0700864 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200865 unsigned int oflags = worker->flags;
866
Tejun Heocb444762010-07-02 10:03:50 +0200867 WARN_ON_ONCE(worker->task != current);
868
Tejun Heod302f012010-06-29 10:07:13 +0200869 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200870
Tejun Heo42c025f2011-01-11 15:58:49 +0100871 /*
872 * If transitioning out of NOT_RUNNING, increment nr_running. Note
873 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
874 * of multiple flags, not a single flag.
875 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200876 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
877 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heo63d95a92012-07-12 14:46:37 -0700878 atomic_inc(get_pool_nr_running(pool));
Tejun Heod302f012010-06-29 10:07:13 +0200879}
880
881/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200882 * find_worker_executing_work - find worker which is executing a work
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800883 * @pool: pool of interest
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200884 * @work: work to find worker for
885 *
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800886 * Find a worker which is executing @work on @pool by searching
887 * @pool->busy_hash which is keyed by the address of @work. For a worker
Tejun Heoa2c1c572012-12-18 10:35:02 -0800888 * to match, its current execution should match the address of @work and
889 * its work function. This is to avoid unwanted dependency between
890 * unrelated work executions through a work item being recycled while still
891 * being executed.
892 *
893 * This is a bit tricky. A work item may be freed once its execution
894 * starts and nothing prevents the freed area from being recycled for
895 * another work item. If the same work item address ends up being reused
896 * before the original execution finishes, workqueue will identify the
897 * recycled work item as currently executing and make it wait until the
898 * current execution finishes, introducing an unwanted dependency.
899 *
900 * This function checks the work item address, work function and workqueue
901 * to avoid false positives. Note that this isn't complete as one may
902 * construct a work function which can introduce dependency onto itself
903 * through a recycled work item. Well, if somebody wants to shoot oneself
904 * in the foot that badly, there's only so much we can do, and if such
905 * deadlock actually occurs, it should be easy to locate the culprit work
906 * function.
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200907 *
908 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800909 * spin_lock_irq(pool->lock).
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200910 *
911 * RETURNS:
912 * Pointer to worker which is executing @work if found, NULL
913 * otherwise.
914 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800915static struct worker *find_worker_executing_work(struct worker_pool *pool,
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200916 struct work_struct *work)
917{
Sasha Levin42f85702012-12-17 10:01:23 -0500918 struct worker *worker;
919 struct hlist_node *tmp;
920
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800921 hash_for_each_possible(pool->busy_hash, worker, tmp, hentry,
Tejun Heoa2c1c572012-12-18 10:35:02 -0800922 (unsigned long)work)
923 if (worker->current_work == work &&
924 worker->current_func == work->func)
Sasha Levin42f85702012-12-17 10:01:23 -0500925 return worker;
926
927 return NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200928}
929
930/**
Tejun Heobf4ede02012-08-03 10:30:46 -0700931 * move_linked_works - move linked works to a list
932 * @work: start of series of works to be scheduled
933 * @head: target list to append @work to
934 * @nextp: out paramter for nested worklist walking
935 *
936 * Schedule linked works starting from @work to @head. Work series to
937 * be scheduled starts at @work and includes any consecutive work with
938 * WORK_STRUCT_LINKED set in its predecessor.
939 *
940 * If @nextp is not NULL, it's updated to point to the next work of
941 * the last scheduled work. This allows move_linked_works() to be
942 * nested inside outer list_for_each_entry_safe().
943 *
944 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800945 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -0700946 */
947static void move_linked_works(struct work_struct *work, struct list_head *head,
948 struct work_struct **nextp)
949{
950 struct work_struct *n;
951
952 /*
953 * Linked worklist will always end before the end of the list,
954 * use NULL for list head.
955 */
956 list_for_each_entry_safe_from(work, n, NULL, entry) {
957 list_move_tail(&work->entry, head);
958 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
959 break;
960 }
961
962 /*
963 * If we're already inside safe list traversal and have moved
964 * multiple works to the scheduled queue, the next position
965 * needs to be updated.
966 */
967 if (nextp)
968 *nextp = n;
969}
970
Lai Jiangshan3aa62492012-09-18 10:40:00 -0700971static void cwq_activate_delayed_work(struct work_struct *work)
Tejun Heobf4ede02012-08-03 10:30:46 -0700972{
Lai Jiangshan3aa62492012-09-18 10:40:00 -0700973 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heobf4ede02012-08-03 10:30:46 -0700974
975 trace_workqueue_activate_work(work);
976 move_linked_works(work, &cwq->pool->worklist, NULL);
977 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
978 cwq->nr_active++;
979}
980
Lai Jiangshan3aa62492012-09-18 10:40:00 -0700981static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
982{
983 struct work_struct *work = list_first_entry(&cwq->delayed_works,
984 struct work_struct, entry);
985
986 cwq_activate_delayed_work(work);
987}
988
Tejun Heobf4ede02012-08-03 10:30:46 -0700989/**
990 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
991 * @cwq: cwq of interest
992 * @color: color of work which left the queue
Tejun Heobf4ede02012-08-03 10:30:46 -0700993 *
994 * A work either has completed or is removed from pending queue,
995 * decrement nr_in_flight of its cwq and handle workqueue flushing.
996 *
997 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800998 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -0700999 */
Lai Jiangshanb3f9f402012-09-18 10:40:00 -07001000static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color)
Tejun Heobf4ede02012-08-03 10:30:46 -07001001{
1002 /* ignore uncolored works */
1003 if (color == WORK_NO_COLOR)
1004 return;
1005
1006 cwq->nr_in_flight[color]--;
1007
Lai Jiangshanb3f9f402012-09-18 10:40:00 -07001008 cwq->nr_active--;
1009 if (!list_empty(&cwq->delayed_works)) {
1010 /* one down, submit a delayed one */
1011 if (cwq->nr_active < cwq->max_active)
1012 cwq_activate_first_delayed(cwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001013 }
1014
1015 /* is flush in progress and are we at the flushing tip? */
1016 if (likely(cwq->flush_color != color))
1017 return;
1018
1019 /* are there still in-flight works? */
1020 if (cwq->nr_in_flight[color])
1021 return;
1022
1023 /* this cwq is done, clear flush_color */
1024 cwq->flush_color = -1;
1025
1026 /*
1027 * If this was the last cwq, wake up the first flusher. It
1028 * will handle the rest.
1029 */
1030 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
1031 complete(&cwq->wq->first_flusher->done);
1032}
1033
Tejun Heo36e227d2012-08-03 10:30:46 -07001034/**
Tejun Heobbb68df2012-08-03 10:30:46 -07001035 * try_to_grab_pending - steal work item from worklist and disable irq
Tejun Heo36e227d2012-08-03 10:30:46 -07001036 * @work: work item to steal
1037 * @is_dwork: @work is a delayed_work
Tejun Heobbb68df2012-08-03 10:30:46 -07001038 * @flags: place to store irq state
Tejun Heo36e227d2012-08-03 10:30:46 -07001039 *
1040 * Try to grab PENDING bit of @work. This function can handle @work in any
1041 * stable state - idle, on timer or on worklist. Return values are
1042 *
1043 * 1 if @work was pending and we successfully stole PENDING
1044 * 0 if @work was idle and we claimed PENDING
1045 * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry
Tejun Heobbb68df2012-08-03 10:30:46 -07001046 * -ENOENT if someone else is canceling @work, this state may persist
1047 * for arbitrarily long
Tejun Heo36e227d2012-08-03 10:30:46 -07001048 *
Tejun Heobbb68df2012-08-03 10:30:46 -07001049 * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001050 * interrupted while holding PENDING and @work off queue, irq must be
1051 * disabled on entry. This, combined with delayed_work->timer being
1052 * irqsafe, ensures that we return -EAGAIN for finite short period of time.
Tejun Heobbb68df2012-08-03 10:30:46 -07001053 *
1054 * On successful return, >= 0, irq is disabled and the caller is
1055 * responsible for releasing it using local_irq_restore(*@flags).
1056 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001057 * This function is safe to call from any context including IRQ handler.
Tejun Heobf4ede02012-08-03 10:30:46 -07001058 */
Tejun Heobbb68df2012-08-03 10:30:46 -07001059static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
1060 unsigned long *flags)
Tejun Heobf4ede02012-08-03 10:30:46 -07001061{
Tejun Heod565ed62013-01-24 11:01:33 -08001062 struct worker_pool *pool;
Tejun Heobf4ede02012-08-03 10:30:46 -07001063
Tejun Heobbb68df2012-08-03 10:30:46 -07001064 local_irq_save(*flags);
1065
Tejun Heo36e227d2012-08-03 10:30:46 -07001066 /* try to steal the timer if it exists */
1067 if (is_dwork) {
1068 struct delayed_work *dwork = to_delayed_work(work);
1069
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001070 /*
1071 * dwork->timer is irqsafe. If del_timer() fails, it's
1072 * guaranteed that the timer is not queued anywhere and not
1073 * running on the local CPU.
1074 */
Tejun Heo36e227d2012-08-03 10:30:46 -07001075 if (likely(del_timer(&dwork->timer)))
1076 return 1;
1077 }
1078
1079 /* try to claim PENDING the normal way */
Tejun Heobf4ede02012-08-03 10:30:46 -07001080 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1081 return 0;
1082
1083 /*
1084 * The queueing is in progress, or it is already queued. Try to
1085 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1086 */
Tejun Heod565ed62013-01-24 11:01:33 -08001087 pool = get_work_pool(work);
1088 if (!pool)
Tejun Heobbb68df2012-08-03 10:30:46 -07001089 goto fail;
Tejun Heobf4ede02012-08-03 10:30:46 -07001090
Tejun Heod565ed62013-01-24 11:01:33 -08001091 spin_lock(&pool->lock);
Tejun Heobf4ede02012-08-03 10:30:46 -07001092 if (!list_empty(&work->entry)) {
1093 /*
Tejun Heod565ed62013-01-24 11:01:33 -08001094 * This work is queued, but perhaps we locked the wrong
1095 * pool. In that case we must see the new value after
1096 * rmb(), see insert_work()->wmb().
Tejun Heobf4ede02012-08-03 10:30:46 -07001097 */
1098 smp_rmb();
Tejun Heod565ed62013-01-24 11:01:33 -08001099 if (pool == get_work_pool(work)) {
Tejun Heobf4ede02012-08-03 10:30:46 -07001100 debug_work_deactivate(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001101
1102 /*
1103 * A delayed work item cannot be grabbed directly
1104 * because it might have linked NO_COLOR work items
1105 * which, if left on the delayed_list, will confuse
1106 * cwq->nr_active management later on and cause
1107 * stall. Make sure the work item is activated
1108 * before grabbing.
1109 */
1110 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
1111 cwq_activate_delayed_work(work);
1112
Tejun Heobf4ede02012-08-03 10:30:46 -07001113 list_del_init(&work->entry);
1114 cwq_dec_nr_in_flight(get_work_cwq(work),
Lai Jiangshanb3f9f402012-09-18 10:40:00 -07001115 get_work_color(work));
Tejun Heo36e227d2012-08-03 10:30:46 -07001116
Tejun Heod565ed62013-01-24 11:01:33 -08001117 spin_unlock(&pool->lock);
Tejun Heo36e227d2012-08-03 10:30:46 -07001118 return 1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001119 }
1120 }
Tejun Heod565ed62013-01-24 11:01:33 -08001121 spin_unlock(&pool->lock);
Tejun Heobbb68df2012-08-03 10:30:46 -07001122fail:
1123 local_irq_restore(*flags);
1124 if (work_is_canceling(work))
1125 return -ENOENT;
1126 cpu_relax();
Tejun Heo36e227d2012-08-03 10:30:46 -07001127 return -EAGAIN;
Tejun Heobf4ede02012-08-03 10:30:46 -07001128}
1129
1130/**
Tejun Heo7e116292010-06-29 10:07:13 +02001131 * insert_work - insert a work into gcwq
Tejun Heo4690c4a2010-06-29 10:07:10 +02001132 * @cwq: cwq @work belongs to
1133 * @work: work to insert
1134 * @head: insertion point
1135 * @extra_flags: extra WORK_STRUCT_* flags to set
1136 *
Tejun Heo7e116292010-06-29 10:07:13 +02001137 * Insert @work which belongs to @cwq into @gcwq after @head.
1138 * @extra_flags is or'd to work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +02001139 *
1140 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001141 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02001142 */
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001143static void insert_work(struct cpu_workqueue_struct *cwq,
Tejun Heo4690c4a2010-06-29 10:07:10 +02001144 struct work_struct *work, struct list_head *head,
1145 unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001146{
Tejun Heo63d95a92012-07-12 14:46:37 -07001147 struct worker_pool *pool = cwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01001148
Tejun Heo4690c4a2010-06-29 10:07:10 +02001149 /* we own @work, set data and link */
Tejun Heo7a22ad72010-06-29 10:07:13 +02001150 set_work_cwq(work, cwq, extra_flags);
Tejun Heo4690c4a2010-06-29 10:07:10 +02001151
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001152 /*
1153 * Ensure that we get the right work->data if we see the
1154 * result of list_add() below, see try_to_grab_pending().
1155 */
1156 smp_wmb();
Tejun Heo4690c4a2010-06-29 10:07:10 +02001157
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -07001158 list_add_tail(&work->entry, head);
Tejun Heoe22bee72010-06-29 10:07:14 +02001159
1160 /*
1161 * Ensure either worker_sched_deactivated() sees the above
1162 * list_add_tail() or we see zero nr_running to avoid workers
1163 * lying around lazily while there are works to be processed.
1164 */
1165 smp_mb();
1166
Tejun Heo63d95a92012-07-12 14:46:37 -07001167 if (__need_more_worker(pool))
1168 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001169}
1170
Tejun Heoc8efcc22010-12-20 19:32:04 +01001171/*
1172 * Test whether @work is being queued from another work executing on the
1173 * same workqueue. This is rather expensive and should only be used from
1174 * cold paths.
1175 */
1176static bool is_chained_work(struct workqueue_struct *wq)
1177{
1178 unsigned long flags;
1179 unsigned int cpu;
1180
1181 for_each_gcwq_cpu(cpu) {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001182 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
1183 struct worker_pool *pool = cwq->pool;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001184 struct worker *worker;
1185 struct hlist_node *pos;
1186 int i;
1187
Tejun Heod565ed62013-01-24 11:01:33 -08001188 spin_lock_irqsave(&pool->lock, flags);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001189 for_each_busy_worker(worker, i, pos, pool) {
Tejun Heoc8efcc22010-12-20 19:32:04 +01001190 if (worker->task != current)
1191 continue;
Tejun Heod565ed62013-01-24 11:01:33 -08001192 spin_unlock_irqrestore(&pool->lock, flags);
Tejun Heoc8efcc22010-12-20 19:32:04 +01001193 /*
1194 * I'm @worker, no locking necessary. See if @work
1195 * is headed to the same workqueue.
1196 */
1197 return worker->current_cwq->wq == wq;
1198 }
Tejun Heod565ed62013-01-24 11:01:33 -08001199 spin_unlock_irqrestore(&pool->lock, flags);
Tejun Heoc8efcc22010-12-20 19:32:04 +01001200 }
1201 return false;
1202}
1203
Tejun Heo4690c4a2010-06-29 10:07:10 +02001204static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 struct work_struct *work)
1206{
Tejun Heod565ed62013-01-24 11:01:33 -08001207 bool highpri = wq->flags & WQ_HIGHPRI;
1208 struct worker_pool *pool;
Tejun Heo502ca9d2010-06-29 10:07:13 +02001209 struct cpu_workqueue_struct *cwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001210 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001211 unsigned int work_flags;
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001212 unsigned int req_cpu = cpu;
Tejun Heo8930cab2012-08-03 10:30:45 -07001213
1214 /*
1215 * While a work item is PENDING && off queue, a task trying to
1216 * steal the PENDING will busy-loop waiting for it to either get
1217 * queued or lose PENDING. Grabbing PENDING and queueing should
1218 * happen with IRQ disabled.
1219 */
1220 WARN_ON_ONCE(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001222 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001223
Tejun Heoc8efcc22010-12-20 19:32:04 +01001224 /* if dying, only works from the same workqueue are allowed */
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02001225 if (unlikely(wq->flags & WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001226 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001227 return;
1228
Tejun Heod565ed62013-01-24 11:01:33 -08001229 /* determine pool to use */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001230 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001231 struct worker_pool *last_pool;
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001232
Tejun Heo57469822012-08-03 10:30:45 -07001233 if (cpu == WORK_CPU_UNBOUND)
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001234 cpu = raw_smp_processor_id();
1235
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001236 /*
Tejun Heodbf25762012-08-20 14:51:23 -07001237 * It's multi cpu. If @work was previously on a different
1238 * cpu, it might still be running there, in which case the
1239 * work needs to be queued on that cpu to guarantee
1240 * non-reentrancy.
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001241 */
Tejun Heod565ed62013-01-24 11:01:33 -08001242 pool = get_std_worker_pool(cpu, highpri);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001243 last_pool = get_work_pool(work);
Tejun Heodbf25762012-08-20 14:51:23 -07001244
Tejun Heod565ed62013-01-24 11:01:33 -08001245 if (last_pool && last_pool != pool) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001246 struct worker *worker;
1247
Tejun Heod565ed62013-01-24 11:01:33 -08001248 spin_lock(&last_pool->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001249
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001250 worker = find_worker_executing_work(last_pool, work);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001251
1252 if (worker && worker->current_cwq->wq == wq)
Tejun Heod565ed62013-01-24 11:01:33 -08001253 pool = last_pool;
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001254 else {
1255 /* meh... not running there, queue here */
Tejun Heod565ed62013-01-24 11:01:33 -08001256 spin_unlock(&last_pool->lock);
1257 spin_lock(&pool->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001258 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001259 } else {
Tejun Heod565ed62013-01-24 11:01:33 -08001260 spin_lock(&pool->lock);
Tejun Heo8930cab2012-08-03 10:30:45 -07001261 }
Tejun Heof3421792010-07-02 10:03:51 +02001262 } else {
Tejun Heod565ed62013-01-24 11:01:33 -08001263 pool = get_std_worker_pool(WORK_CPU_UNBOUND, highpri);
1264 spin_lock(&pool->lock);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001265 }
1266
Tejun Heod565ed62013-01-24 11:01:33 -08001267 /* pool determined, get cwq and queue */
1268 cwq = get_cwq(pool->cpu, wq);
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001269 trace_workqueue_queue_work(req_cpu, cwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001270
Dan Carpenterf5b25522012-04-13 22:06:58 +03001271 if (WARN_ON(!list_empty(&work->entry))) {
Tejun Heod565ed62013-01-24 11:01:33 -08001272 spin_unlock(&pool->lock);
Dan Carpenterf5b25522012-04-13 22:06:58 +03001273 return;
1274 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001275
Tejun Heo73f53c42010-06-29 10:07:11 +02001276 cwq->nr_in_flight[cwq->work_color]++;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001277 work_flags = work_color_to_flags(cwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001278
1279 if (likely(cwq->nr_active < cwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001280 trace_workqueue_activate_work(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001281 cwq->nr_active++;
Tejun Heo32704762012-07-13 22:16:45 -07001282 worklist = &cwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001283 } else {
1284 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001285 worklist = &cwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001286 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001287
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001288 insert_work(cwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001289
Tejun Heod565ed62013-01-24 11:01:33 -08001290 spin_unlock(&pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291}
1292
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001293/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07001294 * queue_work_on - queue work on specific cpu
1295 * @cpu: CPU number to execute work on
1296 * @wq: workqueue to use
1297 * @work: work to queue
1298 *
Tejun Heod4283e92012-08-03 10:30:44 -07001299 * Returns %false if @work was already on a queue, %true otherwise.
Zhang Ruic1a220e2008-07-23 21:28:39 -07001300 *
1301 * We queue the work to a specific CPU, the caller must ensure it
1302 * can't go away.
1303 */
Tejun Heod4283e92012-08-03 10:30:44 -07001304bool queue_work_on(int cpu, struct workqueue_struct *wq,
1305 struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07001306{
Tejun Heod4283e92012-08-03 10:30:44 -07001307 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001308 unsigned long flags;
1309
1310 local_irq_save(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001311
Tejun Heo22df02b2010-06-29 10:07:10 +02001312 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001313 __queue_work(cpu, wq, work);
Tejun Heod4283e92012-08-03 10:30:44 -07001314 ret = true;
Zhang Ruic1a220e2008-07-23 21:28:39 -07001315 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001316
1317 local_irq_restore(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001318 return ret;
1319}
1320EXPORT_SYMBOL_GPL(queue_work_on);
1321
Tejun Heo0a13c002012-08-03 10:30:44 -07001322/**
1323 * queue_work - queue work on a workqueue
1324 * @wq: workqueue to use
1325 * @work: work to queue
1326 *
Tejun Heod4283e92012-08-03 10:30:44 -07001327 * Returns %false if @work was already on a queue, %true otherwise.
Tejun Heo0a13c002012-08-03 10:30:44 -07001328 *
1329 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1330 * it can be processed by another CPU.
1331 */
Tejun Heod4283e92012-08-03 10:30:44 -07001332bool queue_work(struct workqueue_struct *wq, struct work_struct *work)
Tejun Heo0a13c002012-08-03 10:30:44 -07001333{
Tejun Heo57469822012-08-03 10:30:45 -07001334 return queue_work_on(WORK_CPU_UNBOUND, wq, work);
Tejun Heo0a13c002012-08-03 10:30:44 -07001335}
1336EXPORT_SYMBOL_GPL(queue_work);
1337
Tejun Heod8e794d2012-08-03 10:30:45 -07001338void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
David Howells52bad642006-11-22 14:54:01 +00001340 struct delayed_work *dwork = (struct delayed_work *)__data;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001341 struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001343 /* should have been called from irqsafe timer with irq already off */
Tejun Heo12650572012-08-08 09:38:42 -07001344 __queue_work(dwork->cpu, cwq->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345}
Tejun Heod8e794d2012-08-03 10:30:45 -07001346EXPORT_SYMBOL_GPL(delayed_work_timer_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001348static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
1349 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001351 struct timer_list *timer = &dwork->timer;
1352 struct work_struct *work = &dwork->work;
1353 unsigned int lcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001355 WARN_ON_ONCE(timer->function != delayed_work_timer_fn ||
1356 timer->data != (unsigned long)dwork);
Tejun Heofc4b5142012-12-04 07:40:39 -08001357 WARN_ON_ONCE(timer_pending(timer));
1358 WARN_ON_ONCE(!list_empty(&work->entry));
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001359
Tejun Heo8852aac2012-12-01 16:23:42 -08001360 /*
1361 * If @delay is 0, queue @dwork->work immediately. This is for
1362 * both optimization and correctness. The earliest @timer can
1363 * expire is on the closest next tick and delayed_work users depend
1364 * on that there's no such delay when @delay is 0.
1365 */
1366 if (!delay) {
1367 __queue_work(cpu, wq, &dwork->work);
1368 return;
1369 }
1370
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001371 timer_stats_timer_set_start_info(&dwork->timer);
1372
1373 /*
1374 * This stores cwq for the moment, for the timer_fn. Note that the
Tejun Heoec22ca52013-01-24 11:01:33 -08001375 * work's pool is preserved to allow reentrance detection for
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001376 * delayed works.
1377 */
1378 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heoec22ca52013-01-24 11:01:33 -08001379 struct worker_pool *pool = get_work_pool(work);
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001380
Joonsoo Kime42986d2012-08-15 23:25:38 +09001381 /*
Tejun Heoec22ca52013-01-24 11:01:33 -08001382 * If we cannot get the last pool from @work directly,
Joonsoo Kime42986d2012-08-15 23:25:38 +09001383 * select the last CPU such that it avoids unnecessarily
1384 * triggering non-reentrancy check in __queue_work().
1385 */
1386 lcpu = cpu;
Tejun Heoec22ca52013-01-24 11:01:33 -08001387 if (pool)
1388 lcpu = pool->cpu;
Joonsoo Kime42986d2012-08-15 23:25:38 +09001389 if (lcpu == WORK_CPU_UNBOUND)
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001390 lcpu = raw_smp_processor_id();
1391 } else {
1392 lcpu = WORK_CPU_UNBOUND;
1393 }
1394
1395 set_work_cwq(work, get_cwq(lcpu, wq), 0);
1396
Tejun Heo12650572012-08-08 09:38:42 -07001397 dwork->cpu = cpu;
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001398 timer->expires = jiffies + delay;
1399
1400 if (unlikely(cpu != WORK_CPU_UNBOUND))
1401 add_timer_on(timer, cpu);
1402 else
1403 add_timer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404}
1405
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001406/**
1407 * queue_delayed_work_on - queue work on specific CPU after delay
1408 * @cpu: CPU number to execute work on
1409 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001410 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001411 * @delay: number of jiffies to wait before queueing
1412 *
Tejun Heo715f1302012-08-03 10:30:46 -07001413 * Returns %false if @work was already on a queue, %true otherwise. If
1414 * @delay is zero and @dwork is idle, it will be scheduled for immediate
1415 * execution.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001416 */
Tejun Heod4283e92012-08-03 10:30:44 -07001417bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
1418 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001419{
David Howells52bad642006-11-22 14:54:01 +00001420 struct work_struct *work = &dwork->work;
Tejun Heod4283e92012-08-03 10:30:44 -07001421 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001422 unsigned long flags;
1423
1424 /* read the comment in __queue_work() */
1425 local_irq_save(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001426
Tejun Heo22df02b2010-06-29 10:07:10 +02001427 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001428 __queue_delayed_work(cpu, wq, dwork, delay);
Tejun Heod4283e92012-08-03 10:30:44 -07001429 ret = true;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001430 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001431
1432 local_irq_restore(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001433 return ret;
1434}
Dave Jonesae90dd52006-06-30 01:40:45 -04001435EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Tejun Heoc8e55f32010-06-29 10:07:12 +02001437/**
Tejun Heo0a13c002012-08-03 10:30:44 -07001438 * queue_delayed_work - queue work on a workqueue after delay
1439 * @wq: workqueue to use
1440 * @dwork: delayable work to queue
1441 * @delay: number of jiffies to wait before queueing
1442 *
Tejun Heo715f1302012-08-03 10:30:46 -07001443 * Equivalent to queue_delayed_work_on() but tries to use the local CPU.
Tejun Heo0a13c002012-08-03 10:30:44 -07001444 */
Tejun Heod4283e92012-08-03 10:30:44 -07001445bool queue_delayed_work(struct workqueue_struct *wq,
Tejun Heo0a13c002012-08-03 10:30:44 -07001446 struct delayed_work *dwork, unsigned long delay)
1447{
Tejun Heo57469822012-08-03 10:30:45 -07001448 return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
Tejun Heo0a13c002012-08-03 10:30:44 -07001449}
1450EXPORT_SYMBOL_GPL(queue_delayed_work);
1451
1452/**
Tejun Heo8376fe22012-08-03 10:30:47 -07001453 * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
1454 * @cpu: CPU number to execute work on
1455 * @wq: workqueue to use
1456 * @dwork: work to queue
1457 * @delay: number of jiffies to wait before queueing
1458 *
1459 * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
1460 * modify @dwork's timer so that it expires after @delay. If @delay is
1461 * zero, @work is guaranteed to be scheduled immediately regardless of its
1462 * current state.
1463 *
1464 * Returns %false if @dwork was idle and queued, %true if @dwork was
1465 * pending and its timer was modified.
1466 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001467 * This function is safe to call from any context including IRQ handler.
Tejun Heo8376fe22012-08-03 10:30:47 -07001468 * See try_to_grab_pending() for details.
1469 */
1470bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
1471 struct delayed_work *dwork, unsigned long delay)
1472{
1473 unsigned long flags;
1474 int ret;
1475
1476 do {
1477 ret = try_to_grab_pending(&dwork->work, true, &flags);
1478 } while (unlikely(ret == -EAGAIN));
1479
1480 if (likely(ret >= 0)) {
1481 __queue_delayed_work(cpu, wq, dwork, delay);
1482 local_irq_restore(flags);
1483 }
1484
1485 /* -ENOENT from try_to_grab_pending() becomes %true */
1486 return ret;
1487}
1488EXPORT_SYMBOL_GPL(mod_delayed_work_on);
1489
1490/**
1491 * mod_delayed_work - modify delay of or queue a delayed work
1492 * @wq: workqueue to use
1493 * @dwork: work to queue
1494 * @delay: number of jiffies to wait before queueing
1495 *
1496 * mod_delayed_work_on() on local CPU.
1497 */
1498bool mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dwork,
1499 unsigned long delay)
1500{
1501 return mod_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
1502}
1503EXPORT_SYMBOL_GPL(mod_delayed_work);
1504
1505/**
Tejun Heoc8e55f32010-06-29 10:07:12 +02001506 * worker_enter_idle - enter idle state
1507 * @worker: worker which is entering idle state
1508 *
1509 * @worker is entering idle state. Update stats and idle timer if
1510 * necessary.
1511 *
1512 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001513 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001514 */
1515static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001517 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Tejun Heoc8e55f32010-06-29 10:07:12 +02001519 BUG_ON(worker->flags & WORKER_IDLE);
1520 BUG_ON(!list_empty(&worker->entry) &&
1521 (worker->hentry.next || worker->hentry.pprev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Tejun Heocb444762010-07-02 10:03:50 +02001523 /* can't use worker_set_flags(), also called from start_worker() */
1524 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001525 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001526 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001527
Tejun Heoc8e55f32010-06-29 10:07:12 +02001528 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001529 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001530
Tejun Heo628c78e2012-07-17 12:39:27 -07001531 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1532 mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
Tejun Heocb444762010-07-02 10:03:50 +02001533
Tejun Heo544ecf32012-05-14 15:04:50 -07001534 /*
Tejun Heo628c78e2012-07-17 12:39:27 -07001535 * Sanity check nr_running. Because gcwq_unbind_fn() releases
Tejun Heod565ed62013-01-24 11:01:33 -08001536 * pool->lock between setting %WORKER_UNBOUND and zapping
Tejun Heo628c78e2012-07-17 12:39:27 -07001537 * nr_running, the warning may trigger spuriously. Check iff
1538 * unbind is not in progress.
Tejun Heo544ecf32012-05-14 15:04:50 -07001539 */
Tejun Heo24647572013-01-24 11:01:33 -08001540 WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001541 pool->nr_workers == pool->nr_idle &&
Tejun Heo63d95a92012-07-12 14:46:37 -07001542 atomic_read(get_pool_nr_running(pool)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543}
1544
Tejun Heoc8e55f32010-06-29 10:07:12 +02001545/**
1546 * worker_leave_idle - leave idle state
1547 * @worker: worker which is leaving idle state
1548 *
1549 * @worker is leaving idle state. Update stats.
1550 *
1551 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001552 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001553 */
1554static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001556 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Tejun Heoc8e55f32010-06-29 10:07:12 +02001558 BUG_ON(!(worker->flags & WORKER_IDLE));
Tejun Heod302f012010-06-29 10:07:13 +02001559 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001560 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001561 list_del_init(&worker->entry);
1562}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Tejun Heoe22bee72010-06-29 10:07:14 +02001564/**
1565 * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq
1566 * @worker: self
1567 *
1568 * Works which are scheduled while the cpu is online must at least be
1569 * scheduled to a worker which is bound to the cpu so that if they are
1570 * flushed from cpu callbacks while cpu is going down, they are
1571 * guaranteed to execute on the cpu.
1572 *
1573 * This function is to be used by rogue workers and rescuers to bind
1574 * themselves to the target cpu and may race with cpu going down or
1575 * coming online. kthread_bind() can't be used because it may put the
1576 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1577 * verbatim as it's best effort and blocking and gcwq may be
1578 * [dis]associated in the meantime.
1579 *
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001580 * This function tries set_cpus_allowed() and locks gcwq and verifies the
Tejun Heo24647572013-01-24 11:01:33 -08001581 * binding against %POOL_DISASSOCIATED which is set during
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001582 * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1583 * enters idle state or fetches works without dropping lock, it can
1584 * guarantee the scheduling requirement described in the first paragraph.
Tejun Heoe22bee72010-06-29 10:07:14 +02001585 *
1586 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001587 * Might sleep. Called without any lock but returns with pool->lock
Tejun Heoe22bee72010-06-29 10:07:14 +02001588 * held.
1589 *
1590 * RETURNS:
1591 * %true if the associated gcwq is online (@worker is successfully
1592 * bound), %false if offline.
1593 */
1594static bool worker_maybe_bind_and_lock(struct worker *worker)
Tejun Heod565ed62013-01-24 11:01:33 -08001595__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001596{
Tejun Heo24647572013-01-24 11:01:33 -08001597 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001598 struct task_struct *task = worker->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Tejun Heoe22bee72010-06-29 10:07:14 +02001600 while (true) {
1601 /*
1602 * The following call may fail, succeed or succeed
1603 * without actually migrating the task to the cpu if
1604 * it races with cpu hotunplug operation. Verify
Tejun Heo24647572013-01-24 11:01:33 -08001605 * against POOL_DISASSOCIATED.
Tejun Heoe22bee72010-06-29 10:07:14 +02001606 */
Tejun Heo24647572013-01-24 11:01:33 -08001607 if (!(pool->flags & POOL_DISASSOCIATED))
Tejun Heoec22ca52013-01-24 11:01:33 -08001608 set_cpus_allowed_ptr(task, get_cpu_mask(pool->cpu));
Oleg Nesterov85f41862007-05-09 02:34:20 -07001609
Tejun Heod565ed62013-01-24 11:01:33 -08001610 spin_lock_irq(&pool->lock);
Tejun Heo24647572013-01-24 11:01:33 -08001611 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heoe22bee72010-06-29 10:07:14 +02001612 return false;
Tejun Heoec22ca52013-01-24 11:01:33 -08001613 if (task_cpu(task) == pool->cpu &&
Tejun Heoe22bee72010-06-29 10:07:14 +02001614 cpumask_equal(&current->cpus_allowed,
Tejun Heoec22ca52013-01-24 11:01:33 -08001615 get_cpu_mask(pool->cpu)))
Tejun Heoe22bee72010-06-29 10:07:14 +02001616 return true;
Tejun Heod565ed62013-01-24 11:01:33 -08001617 spin_unlock_irq(&pool->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001618
Tejun Heo5035b202011-04-29 18:08:37 +02001619 /*
1620 * We've raced with CPU hot[un]plug. Give it a breather
1621 * and retry migration. cond_resched() is required here;
1622 * otherwise, we might deadlock against cpu_stop trying to
1623 * bring down the CPU on non-preemptive kernel.
1624 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001625 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001626 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001627 }
1628}
1629
1630/*
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001631 * Rebind an idle @worker to its CPU. worker_thread() will test
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001632 * list_empty(@worker->entry) before leaving idle and call this function.
Tejun Heo25511a42012-07-17 12:39:27 -07001633 */
1634static void idle_worker_rebind(struct worker *worker)
1635{
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001636 /* CPU may go down again inbetween, clear UNBOUND only on success */
1637 if (worker_maybe_bind_and_lock(worker))
1638 worker_clr_flags(worker, WORKER_UNBOUND);
Tejun Heo25511a42012-07-17 12:39:27 -07001639
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001640 /* rebind complete, become available again */
1641 list_add(&worker->entry, &worker->pool->idle_list);
Tejun Heod565ed62013-01-24 11:01:33 -08001642 spin_unlock_irq(&worker->pool->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07001643}
1644
1645/*
1646 * Function for @worker->rebind.work used to rebind unbound busy workers to
Tejun Heo403c8212012-07-17 12:39:27 -07001647 * the associated cpu which is coming back online. This is scheduled by
1648 * cpu up but can race with other cpu hotplug operations and may be
1649 * executed twice without intervening cpu down.
Tejun Heoe22bee72010-06-29 10:07:14 +02001650 */
Tejun Heo25511a42012-07-17 12:39:27 -07001651static void busy_worker_rebind_fn(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001652{
1653 struct worker *worker = container_of(work, struct worker, rebind_work);
Tejun Heoe22bee72010-06-29 10:07:14 +02001654
Lai Jiangshaneab6d822012-09-18 09:59:22 -07001655 if (worker_maybe_bind_and_lock(worker))
1656 worker_clr_flags(worker, WORKER_UNBOUND);
Tejun Heoe22bee72010-06-29 10:07:14 +02001657
Tejun Heod565ed62013-01-24 11:01:33 -08001658 spin_unlock_irq(&worker->pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001659}
1660
Tejun Heo25511a42012-07-17 12:39:27 -07001661/**
Tejun Heo94cf58b2013-01-24 11:01:33 -08001662 * rebind_workers - rebind all workers of a pool to the associated CPU
1663 * @pool: pool of interest
Tejun Heo25511a42012-07-17 12:39:27 -07001664 *
Tejun Heo94cf58b2013-01-24 11:01:33 -08001665 * @pool->cpu is coming online. Rebind all workers to the CPU. Rebinding
Tejun Heo25511a42012-07-17 12:39:27 -07001666 * is different for idle and busy ones.
1667 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001668 * Idle ones will be removed from the idle_list and woken up. They will
1669 * add themselves back after completing rebind. This ensures that the
1670 * idle_list doesn't contain any unbound workers when re-bound busy workers
1671 * try to perform local wake-ups for concurrency management.
Tejun Heo25511a42012-07-17 12:39:27 -07001672 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001673 * Busy workers can rebind after they finish their current work items.
1674 * Queueing the rebind work item at the head of the scheduled list is
1675 * enough. Note that nr_running will be properly bumped as busy workers
1676 * rebind.
Tejun Heo25511a42012-07-17 12:39:27 -07001677 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001678 * On return, all non-manager workers are scheduled for rebind - see
1679 * manage_workers() for the manager special case. Any idle worker
1680 * including the manager will not appear on @idle_list until rebind is
1681 * complete, making local wake-ups safe.
Tejun Heo25511a42012-07-17 12:39:27 -07001682 */
Tejun Heo94cf58b2013-01-24 11:01:33 -08001683static void rebind_workers(struct worker_pool *pool)
Tejun Heo25511a42012-07-17 12:39:27 -07001684{
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001685 struct worker *worker, *n;
Tejun Heo25511a42012-07-17 12:39:27 -07001686 struct hlist_node *pos;
1687 int i;
1688
Tejun Heo94cf58b2013-01-24 11:01:33 -08001689 lockdep_assert_held(&pool->assoc_mutex);
1690 lockdep_assert_held(&pool->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07001691
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001692 /* dequeue and kick idle ones */
Tejun Heo94cf58b2013-01-24 11:01:33 -08001693 list_for_each_entry_safe(worker, n, &pool->idle_list, entry) {
1694 /*
1695 * idle workers should be off @pool->idle_list until rebind
1696 * is complete to avoid receiving premature local wake-ups.
1697 */
1698 list_del_init(&worker->entry);
Lai Jiangshan96e65302012-09-02 00:28:19 +08001699
Tejun Heo94cf58b2013-01-24 11:01:33 -08001700 /*
1701 * worker_thread() will see the above dequeuing and call
1702 * idle_worker_rebind().
1703 */
1704 wake_up_process(worker->task);
1705 }
Tejun Heo25511a42012-07-17 12:39:27 -07001706
Tejun Heo94cf58b2013-01-24 11:01:33 -08001707 /* rebind busy workers */
1708 for_each_busy_worker(worker, i, pos, pool) {
1709 struct work_struct *rebind_work = &worker->rebind_work;
1710 struct workqueue_struct *wq;
Tejun Heo25511a42012-07-17 12:39:27 -07001711
Tejun Heo94cf58b2013-01-24 11:01:33 -08001712 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
1713 work_data_bits(rebind_work)))
1714 continue;
Tejun Heo25511a42012-07-17 12:39:27 -07001715
Tejun Heo94cf58b2013-01-24 11:01:33 -08001716 debug_work_activate(rebind_work);
Tejun Heo90beca52012-09-04 23:12:33 -07001717
Tejun Heo94cf58b2013-01-24 11:01:33 -08001718 /*
1719 * wq doesn't really matter but let's keep @worker->pool
1720 * and @cwq->pool consistent for sanity.
1721 */
1722 if (std_worker_pool_pri(worker->pool))
1723 wq = system_highpri_wq;
1724 else
1725 wq = system_wq;
Tejun Heoec588152012-09-04 23:16:32 -07001726
Tejun Heo94cf58b2013-01-24 11:01:33 -08001727 insert_work(get_cwq(pool->cpu, wq), rebind_work,
1728 worker->scheduled.next,
1729 work_color_to_flags(WORK_NO_COLOR));
Tejun Heoec588152012-09-04 23:16:32 -07001730 }
Tejun Heo25511a42012-07-17 12:39:27 -07001731}
1732
Tejun Heoc34056a2010-06-29 10:07:11 +02001733static struct worker *alloc_worker(void)
1734{
1735 struct worker *worker;
1736
1737 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001738 if (worker) {
1739 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001740 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heo25511a42012-07-17 12:39:27 -07001741 INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn);
Tejun Heoe22bee72010-06-29 10:07:14 +02001742 /* on creation a worker is in !idle && prep state */
1743 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001744 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001745 return worker;
1746}
1747
1748/**
1749 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001750 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001751 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001752 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001753 * can be started by calling start_worker() or destroyed using
1754 * destroy_worker().
1755 *
1756 * CONTEXT:
1757 * Might sleep. Does GFP_KERNEL allocations.
1758 *
1759 * RETURNS:
1760 * Pointer to the newly created worker.
1761 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001762static struct worker *create_worker(struct worker_pool *pool)
Tejun Heoc34056a2010-06-29 10:07:11 +02001763{
Tejun Heoe34cdddb2013-01-24 11:01:33 -08001764 const char *pri = std_worker_pool_pri(pool) ? "H" : "";
Tejun Heoc34056a2010-06-29 10:07:11 +02001765 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001766 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001767
Tejun Heod565ed62013-01-24 11:01:33 -08001768 spin_lock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001769 while (ida_get_new(&pool->worker_ida, &id)) {
Tejun Heod565ed62013-01-24 11:01:33 -08001770 spin_unlock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001771 if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001772 goto fail;
Tejun Heod565ed62013-01-24 11:01:33 -08001773 spin_lock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001774 }
Tejun Heod565ed62013-01-24 11:01:33 -08001775 spin_unlock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001776
1777 worker = alloc_worker();
1778 if (!worker)
1779 goto fail;
1780
Tejun Heobd7bdd42012-07-12 14:46:37 -07001781 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001782 worker->id = id;
1783
Tejun Heoec22ca52013-01-24 11:01:33 -08001784 if (pool->cpu != WORK_CPU_UNBOUND)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001785 worker->task = kthread_create_on_node(worker_thread,
Tejun Heoec22ca52013-01-24 11:01:33 -08001786 worker, cpu_to_node(pool->cpu),
1787 "kworker/%u:%d%s", pool->cpu, id, pri);
Tejun Heof3421792010-07-02 10:03:51 +02001788 else
1789 worker->task = kthread_create(worker_thread, worker,
Tejun Heo32704762012-07-13 22:16:45 -07001790 "kworker/u:%d%s", id, pri);
Tejun Heoc34056a2010-06-29 10:07:11 +02001791 if (IS_ERR(worker->task))
1792 goto fail;
1793
Tejun Heoe34cdddb2013-01-24 11:01:33 -08001794 if (std_worker_pool_pri(pool))
Tejun Heo32704762012-07-13 22:16:45 -07001795 set_user_nice(worker->task, HIGHPRI_NICE_LEVEL);
1796
Tejun Heodb7bccf2010-06-29 10:07:12 +02001797 /*
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001798 * Determine CPU binding of the new worker depending on
Tejun Heo24647572013-01-24 11:01:33 -08001799 * %POOL_DISASSOCIATED. The caller is responsible for ensuring the
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001800 * flag remains stable across this function. See the comments
1801 * above the flag definition for details.
1802 *
1803 * As an unbound worker may later become a regular one if CPU comes
1804 * online, make sure every worker has %PF_THREAD_BOUND set.
Tejun Heodb7bccf2010-06-29 10:07:12 +02001805 */
Tejun Heo24647572013-01-24 11:01:33 -08001806 if (!(pool->flags & POOL_DISASSOCIATED)) {
Tejun Heoec22ca52013-01-24 11:01:33 -08001807 kthread_bind(worker->task, pool->cpu);
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001808 } else {
Tejun Heodb7bccf2010-06-29 10:07:12 +02001809 worker->task->flags |= PF_THREAD_BOUND;
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001810 worker->flags |= WORKER_UNBOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001812
Tejun Heoc34056a2010-06-29 10:07:11 +02001813 return worker;
1814fail:
1815 if (id >= 0) {
Tejun Heod565ed62013-01-24 11:01:33 -08001816 spin_lock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001817 ida_remove(&pool->worker_ida, id);
Tejun Heod565ed62013-01-24 11:01:33 -08001818 spin_unlock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001819 }
1820 kfree(worker);
1821 return NULL;
1822}
1823
1824/**
1825 * start_worker - start a newly created worker
1826 * @worker: worker to start
1827 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001828 * Make the gcwq aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001829 *
1830 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001831 * spin_lock_irq(pool->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001832 */
1833static void start_worker(struct worker *worker)
1834{
Tejun Heocb444762010-07-02 10:03:50 +02001835 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001836 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001837 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001838 wake_up_process(worker->task);
1839}
1840
1841/**
1842 * destroy_worker - destroy a workqueue worker
1843 * @worker: worker to be destroyed
1844 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001845 * Destroy @worker and adjust @gcwq stats accordingly.
1846 *
1847 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001848 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001849 */
1850static void destroy_worker(struct worker *worker)
1851{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001852 struct worker_pool *pool = worker->pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001853 int id = worker->id;
1854
1855 /* sanity check frenzy */
1856 BUG_ON(worker->current_work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001857 BUG_ON(!list_empty(&worker->scheduled));
Tejun Heoc34056a2010-06-29 10:07:11 +02001858
Tejun Heoc8e55f32010-06-29 10:07:12 +02001859 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001860 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001861 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001862 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001863
1864 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001865 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001866
Tejun Heod565ed62013-01-24 11:01:33 -08001867 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001868
Tejun Heoc34056a2010-06-29 10:07:11 +02001869 kthread_stop(worker->task);
1870 kfree(worker);
1871
Tejun Heod565ed62013-01-24 11:01:33 -08001872 spin_lock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001873 ida_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001874}
1875
Tejun Heo63d95a92012-07-12 14:46:37 -07001876static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001877{
Tejun Heo63d95a92012-07-12 14:46:37 -07001878 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001879
Tejun Heod565ed62013-01-24 11:01:33 -08001880 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001881
Tejun Heo63d95a92012-07-12 14:46:37 -07001882 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001883 struct worker *worker;
1884 unsigned long expires;
1885
1886 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001887 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001888 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1889
1890 if (time_before(jiffies, expires))
Tejun Heo63d95a92012-07-12 14:46:37 -07001891 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001892 else {
1893 /* it's been idle for too long, wake up manager */
Tejun Heo11ebea52012-07-12 14:46:37 -07001894 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -07001895 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001896 }
1897 }
1898
Tejun Heod565ed62013-01-24 11:01:33 -08001899 spin_unlock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001900}
1901
1902static bool send_mayday(struct work_struct *work)
1903{
1904 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1905 struct workqueue_struct *wq = cwq->wq;
Tejun Heof3421792010-07-02 10:03:51 +02001906 unsigned int cpu;
Tejun Heoe22bee72010-06-29 10:07:14 +02001907
1908 if (!(wq->flags & WQ_RESCUER))
1909 return false;
1910
1911 /* mayday mayday mayday */
Tejun Heoec22ca52013-01-24 11:01:33 -08001912 cpu = cwq->pool->cpu;
Tejun Heof3421792010-07-02 10:03:51 +02001913 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1914 if (cpu == WORK_CPU_UNBOUND)
1915 cpu = 0;
Tejun Heof2e005a2010-07-20 15:59:09 +02001916 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001917 wake_up_process(wq->rescuer->task);
1918 return true;
1919}
1920
Tejun Heo63d95a92012-07-12 14:46:37 -07001921static void gcwq_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001922{
Tejun Heo63d95a92012-07-12 14:46:37 -07001923 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001924 struct work_struct *work;
1925
Tejun Heod565ed62013-01-24 11:01:33 -08001926 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001927
Tejun Heo63d95a92012-07-12 14:46:37 -07001928 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001929 /*
1930 * We've been trying to create a new worker but
1931 * haven't been successful. We might be hitting an
1932 * allocation deadlock. Send distress signals to
1933 * rescuers.
1934 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001935 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001936 send_mayday(work);
1937 }
1938
Tejun Heod565ed62013-01-24 11:01:33 -08001939 spin_unlock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001940
Tejun Heo63d95a92012-07-12 14:46:37 -07001941 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001942}
1943
1944/**
1945 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001946 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001947 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001948 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001949 * have at least one idle worker on return from this function. If
1950 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001951 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001952 * possible allocation deadlock.
1953 *
1954 * On return, need_to_create_worker() is guaranteed to be false and
1955 * may_start_working() true.
1956 *
1957 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001958 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001959 * multiple times. Does GFP_KERNEL allocations. Called only from
1960 * manager.
1961 *
1962 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08001963 * false if no action was taken and pool->lock stayed locked, true
Tejun Heoe22bee72010-06-29 10:07:14 +02001964 * otherwise.
1965 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001966static bool maybe_create_worker(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001967__releases(&pool->lock)
1968__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001969{
Tejun Heo63d95a92012-07-12 14:46:37 -07001970 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001971 return false;
1972restart:
Tejun Heod565ed62013-01-24 11:01:33 -08001973 spin_unlock_irq(&pool->lock);
Tejun Heo9f9c236442010-07-14 11:31:20 +02001974
Tejun Heoe22bee72010-06-29 10:07:14 +02001975 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07001976 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001977
1978 while (true) {
1979 struct worker *worker;
1980
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001981 worker = create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001982 if (worker) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001983 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001984 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001985 start_worker(worker);
Tejun Heo63d95a92012-07-12 14:46:37 -07001986 BUG_ON(need_to_create_worker(pool));
Tejun Heoe22bee72010-06-29 10:07:14 +02001987 return true;
1988 }
1989
Tejun Heo63d95a92012-07-12 14:46:37 -07001990 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001991 break;
1992
Tejun Heoe22bee72010-06-29 10:07:14 +02001993 __set_current_state(TASK_INTERRUPTIBLE);
1994 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c236442010-07-14 11:31:20 +02001995
Tejun Heo63d95a92012-07-12 14:46:37 -07001996 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001997 break;
1998 }
1999
Tejun Heo63d95a92012-07-12 14:46:37 -07002000 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08002001 spin_lock_irq(&pool->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07002002 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002003 goto restart;
2004 return true;
2005}
2006
2007/**
2008 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo63d95a92012-07-12 14:46:37 -07002009 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02002010 *
Tejun Heo63d95a92012-07-12 14:46:37 -07002011 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02002012 * IDLE_WORKER_TIMEOUT.
2013 *
2014 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08002015 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02002016 * multiple times. Called only from manager.
2017 *
2018 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08002019 * false if no action was taken and pool->lock stayed locked, true
Tejun Heoe22bee72010-06-29 10:07:14 +02002020 * otherwise.
2021 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002022static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02002023{
2024 bool ret = false;
2025
Tejun Heo63d95a92012-07-12 14:46:37 -07002026 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02002027 struct worker *worker;
2028 unsigned long expires;
2029
Tejun Heo63d95a92012-07-12 14:46:37 -07002030 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02002031 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
2032
2033 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07002034 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02002035 break;
2036 }
2037
2038 destroy_worker(worker);
2039 ret = true;
2040 }
2041
2042 return ret;
2043}
2044
2045/**
2046 * manage_workers - manage worker pool
2047 * @worker: self
2048 *
2049 * Assume the manager role and manage gcwq worker pool @worker belongs
2050 * to. At any given time, there can be only zero or one manager per
2051 * gcwq. The exclusion is handled automatically by this function.
2052 *
2053 * The caller can safely start processing works on false return. On
2054 * true return, it's guaranteed that need_to_create_worker() is false
2055 * and may_start_working() is true.
2056 *
2057 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002058 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02002059 * multiple times. Does GFP_KERNEL allocations.
2060 *
2061 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08002062 * spin_lock_irq(pool->lock) which may be released and regrabbed
2063 * multiple times. Does GFP_KERNEL allocations.
Tejun Heoe22bee72010-06-29 10:07:14 +02002064 */
2065static bool manage_workers(struct worker *worker)
2066{
Tejun Heo63d95a92012-07-12 14:46:37 -07002067 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002068 bool ret = false;
2069
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002070 if (pool->flags & POOL_MANAGING_WORKERS)
Tejun Heoe22bee72010-06-29 10:07:14 +02002071 return ret;
2072
Lai Jiangshan552a37e2012-09-10 10:03:33 -07002073 pool->flags |= POOL_MANAGING_WORKERS;
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002074
2075 /*
2076 * To simplify both worker management and CPU hotplug, hold off
2077 * management while hotplug is in progress. CPU hotplug path can't
2078 * grab %POOL_MANAGING_WORKERS to achieve this because that can
2079 * lead to idle worker depletion (all become busy thinking someone
2080 * else is managing) which in turn can result in deadlock under
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002081 * extreme circumstances. Use @pool->assoc_mutex to synchronize
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002082 * manager against CPU hotplug.
2083 *
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002084 * assoc_mutex would always be free unless CPU hotplug is in
Tejun Heod565ed62013-01-24 11:01:33 -08002085 * progress. trylock first without dropping @pool->lock.
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002086 */
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002087 if (unlikely(!mutex_trylock(&pool->assoc_mutex))) {
Tejun Heod565ed62013-01-24 11:01:33 -08002088 spin_unlock_irq(&pool->lock);
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002089 mutex_lock(&pool->assoc_mutex);
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002090 /*
2091 * CPU hotplug could have happened while we were waiting
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002092 * for assoc_mutex. Hotplug itself can't handle us
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002093 * because manager isn't either on idle or busy list, and
2094 * @gcwq's state and ours could have deviated.
2095 *
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002096 * As hotplug is now excluded via assoc_mutex, we can
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002097 * simply try to bind. It will succeed or fail depending
2098 * on @gcwq's current state. Try it and adjust
2099 * %WORKER_UNBOUND accordingly.
2100 */
2101 if (worker_maybe_bind_and_lock(worker))
2102 worker->flags &= ~WORKER_UNBOUND;
2103 else
2104 worker->flags |= WORKER_UNBOUND;
2105
2106 ret = true;
2107 }
2108
Tejun Heo11ebea52012-07-12 14:46:37 -07002109 pool->flags &= ~POOL_MANAGE_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02002110
2111 /*
2112 * Destroy and then create so that may_start_working() is true
2113 * on return.
2114 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002115 ret |= maybe_destroy_workers(pool);
2116 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002117
Lai Jiangshan552a37e2012-09-10 10:03:33 -07002118 pool->flags &= ~POOL_MANAGING_WORKERS;
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002119 mutex_unlock(&pool->assoc_mutex);
Tejun Heoe22bee72010-06-29 10:07:14 +02002120 return ret;
2121}
2122
Tejun Heoa62428c2010-06-29 10:07:10 +02002123/**
2124 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02002125 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02002126 * @work: work to process
2127 *
2128 * Process @work. This function contains all the logics necessary to
2129 * process a single work including synchronization against and
2130 * interaction with other workers on the same cpu, queueing and
2131 * flushing. As long as context requirement is met, any worker can
2132 * call this function to process a work.
2133 *
2134 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002135 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02002136 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002137static void process_one_work(struct worker *worker, struct work_struct *work)
Tejun Heod565ed62013-01-24 11:01:33 -08002138__releases(&pool->lock)
2139__acquires(&pool->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02002140{
Tejun Heo7e116292010-06-29 10:07:13 +02002141 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002142 struct worker_pool *pool = worker->pool;
Tejun Heofb0e7be2010-06-29 10:07:15 +02002143 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02002144 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02002145 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02002146#ifdef CONFIG_LOCKDEP
2147 /*
2148 * It is permissible to free the struct work_struct from
2149 * inside the function that is called from it, this we need to
2150 * take into account for lockdep too. To avoid bogus "held
2151 * lock freed" warnings as well as problems when looking into
2152 * work->lockdep_map, make a copy and use that here.
2153 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07002154 struct lockdep_map lockdep_map;
2155
2156 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002157#endif
Tejun Heo6fec10a2012-07-22 10:16:34 -07002158 /*
2159 * Ensure we're on the correct CPU. DISASSOCIATED test is
2160 * necessary to avoid spurious warnings from rescuers servicing the
Tejun Heo24647572013-01-24 11:01:33 -08002161 * unbound or a disassociated pool.
Tejun Heo6fec10a2012-07-22 10:16:34 -07002162 */
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002163 WARN_ON_ONCE(!(worker->flags & WORKER_UNBOUND) &&
Tejun Heo24647572013-01-24 11:01:33 -08002164 !(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heoec22ca52013-01-24 11:01:33 -08002165 raw_smp_processor_id() != pool->cpu);
Tejun Heo25511a42012-07-17 12:39:27 -07002166
Tejun Heo7e116292010-06-29 10:07:13 +02002167 /*
2168 * A single work shouldn't be executed concurrently by
2169 * multiple workers on a single cpu. Check whether anyone is
2170 * already processing the work. If so, defer the work to the
2171 * currently executing one.
2172 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002173 collision = find_worker_executing_work(pool, work);
Tejun Heo7e116292010-06-29 10:07:13 +02002174 if (unlikely(collision)) {
2175 move_linked_works(work, &collision->scheduled, NULL);
2176 return;
2177 }
2178
Tejun Heo8930cab2012-08-03 10:30:45 -07002179 /* claim and dequeue */
Tejun Heoa62428c2010-06-29 10:07:10 +02002180 debug_work_deactivate(work);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002181 hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work);
Tejun Heoc34056a2010-06-29 10:07:11 +02002182 worker->current_work = work;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002183 worker->current_func = work->func;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02002184 worker->current_cwq = cwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002185 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002186
Tejun Heoa62428c2010-06-29 10:07:10 +02002187 list_del_init(&work->entry);
2188
Tejun Heo649027d2010-06-29 10:07:14 +02002189 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02002190 * CPU intensive works don't participate in concurrency
2191 * management. They're the scheduler's responsibility.
2192 */
2193 if (unlikely(cpu_intensive))
2194 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
2195
Tejun Heo974271c2012-07-12 14:46:37 -07002196 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002197 * Unbound pool isn't concurrency managed and work items should be
Tejun Heo974271c2012-07-12 14:46:37 -07002198 * executed ASAP. Wake up another worker if necessary.
2199 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002200 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
2201 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07002202
Tejun Heo8930cab2012-08-03 10:30:45 -07002203 /*
Tejun Heo7c3eed52013-01-24 11:01:33 -08002204 * Record the last pool and clear PENDING which should be the last
Tejun Heod565ed62013-01-24 11:01:33 -08002205 * update to @work. Also, do this inside @pool->lock so that
Tejun Heo23657bb2012-08-13 17:08:19 -07002206 * PENDING and queued state changes happen together while IRQ is
2207 * disabled.
Tejun Heo8930cab2012-08-03 10:30:45 -07002208 */
Tejun Heo7c3eed52013-01-24 11:01:33 -08002209 set_work_pool_and_clear_pending(work, pool->id);
Tejun Heoa62428c2010-06-29 10:07:10 +02002210
Tejun Heod565ed62013-01-24 11:01:33 -08002211 spin_unlock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002212
Tejun Heoe159489b2011-01-09 23:32:15 +01002213 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002214 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002215 trace_workqueue_execute_start(work);
Tejun Heoa2c1c572012-12-18 10:35:02 -08002216 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002217 /*
2218 * While we must be careful to not use "work" after this, the trace
2219 * point will only record its address.
2220 */
2221 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002222 lock_map_release(&lockdep_map);
2223 lock_map_release(&cwq->wq->lockdep_map);
2224
2225 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Valentin Ilie044c7822012-08-19 00:52:42 +03002226 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
2227 " last function: %pf\n",
Tejun Heoa2c1c572012-12-18 10:35:02 -08002228 current->comm, preempt_count(), task_pid_nr(current),
2229 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02002230 debug_show_held_locks(current);
2231 dump_stack();
2232 }
2233
Tejun Heod565ed62013-01-24 11:01:33 -08002234 spin_lock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002235
Tejun Heofb0e7be2010-06-29 10:07:15 +02002236 /* clear cpu intensive status */
2237 if (unlikely(cpu_intensive))
2238 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2239
Tejun Heoa62428c2010-06-29 10:07:10 +02002240 /* we're done with it, release */
Sasha Levin42f85702012-12-17 10:01:23 -05002241 hash_del(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002242 worker->current_work = NULL;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002243 worker->current_func = NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02002244 worker->current_cwq = NULL;
Lai Jiangshanb3f9f402012-09-18 10:40:00 -07002245 cwq_dec_nr_in_flight(cwq, work_color);
Tejun Heoa62428c2010-06-29 10:07:10 +02002246}
2247
Tejun Heoaffee4b2010-06-29 10:07:12 +02002248/**
2249 * process_scheduled_works - process scheduled works
2250 * @worker: self
2251 *
2252 * Process all scheduled works. Please note that the scheduled list
2253 * may change while processing a work, so this function repeatedly
2254 * fetches a work from the top and executes it.
2255 *
2256 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002257 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02002258 * multiple times.
2259 */
2260static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002262 while (!list_empty(&worker->scheduled)) {
2263 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002265 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267}
2268
Tejun Heo4690c4a2010-06-29 10:07:10 +02002269/**
2270 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02002271 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02002272 *
Tejun Heoe22bee72010-06-29 10:07:14 +02002273 * The gcwq worker thread function. There's a single dynamic pool of
2274 * these per each cpu. These workers process all works regardless of
2275 * their specific target workqueue. The only exception is works which
2276 * belong to workqueues with a rescuer which will be explained in
2277 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02002278 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002279static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280{
Tejun Heoc34056a2010-06-29 10:07:11 +02002281 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07002282 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
Tejun Heoe22bee72010-06-29 10:07:14 +02002284 /* tell the scheduler that this is a workqueue worker */
2285 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002286woke_up:
Tejun Heod565ed62013-01-24 11:01:33 -08002287 spin_lock_irq(&pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002289 /* we are off idle list if destruction or rebind is requested */
2290 if (unlikely(list_empty(&worker->entry))) {
Tejun Heod565ed62013-01-24 11:01:33 -08002291 spin_unlock_irq(&pool->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07002292
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002293 /* if DIE is set, destruction is requested */
Tejun Heo25511a42012-07-17 12:39:27 -07002294 if (worker->flags & WORKER_DIE) {
2295 worker->task->flags &= ~PF_WQ_WORKER;
2296 return 0;
2297 }
2298
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002299 /* otherwise, rebind */
Tejun Heo25511a42012-07-17 12:39:27 -07002300 idle_worker_rebind(worker);
2301 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 }
2303
Tejun Heoc8e55f32010-06-29 10:07:12 +02002304 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002305recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002306 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002307 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002308 goto sleep;
2309
2310 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002311 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002312 goto recheck;
2313
Tejun Heoc8e55f32010-06-29 10:07:12 +02002314 /*
2315 * ->scheduled list can only be filled while a worker is
2316 * preparing to process a work or actually processing it.
2317 * Make sure nobody diddled with it while I was sleeping.
2318 */
2319 BUG_ON(!list_empty(&worker->scheduled));
2320
Tejun Heoe22bee72010-06-29 10:07:14 +02002321 /*
2322 * When control reaches this point, we're guaranteed to have
2323 * at least one idle worker or that someone else has already
2324 * assumed the manager role.
2325 */
2326 worker_clr_flags(worker, WORKER_PREP);
2327
2328 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002329 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002330 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002331 struct work_struct, entry);
2332
2333 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2334 /* optimization path, not strictly necessary */
2335 process_one_work(worker, work);
2336 if (unlikely(!list_empty(&worker->scheduled)))
2337 process_scheduled_works(worker);
2338 } else {
2339 move_linked_works(work, &worker->scheduled, NULL);
2340 process_scheduled_works(worker);
2341 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002342 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002343
Tejun Heoe22bee72010-06-29 10:07:14 +02002344 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002345sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07002346 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002347 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002348
Tejun Heoc8e55f32010-06-29 10:07:12 +02002349 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002350 * pool->lock is held and there's no work to process and no need to
2351 * manage, sleep. Workers are woken up only while holding
2352 * pool->lock or from local cpu, so setting the current state
2353 * before releasing pool->lock is enough to prevent losing any
2354 * event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002355 */
2356 worker_enter_idle(worker);
2357 __set_current_state(TASK_INTERRUPTIBLE);
Tejun Heod565ed62013-01-24 11:01:33 -08002358 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02002359 schedule();
2360 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361}
2362
Tejun Heoe22bee72010-06-29 10:07:14 +02002363/**
2364 * rescuer_thread - the rescuer thread function
Tejun Heo111c2252013-01-17 17:16:24 -08002365 * @__rescuer: self
Tejun Heoe22bee72010-06-29 10:07:14 +02002366 *
2367 * Workqueue rescuer thread function. There's one rescuer for each
2368 * workqueue which has WQ_RESCUER set.
2369 *
2370 * Regular work processing on a gcwq may block trying to create a new
2371 * worker which uses GFP_KERNEL allocation which has slight chance of
2372 * developing into deadlock if some works currently on the same queue
2373 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2374 * the problem rescuer solves.
2375 *
2376 * When such condition is possible, the gcwq summons rescuers of all
2377 * workqueues which have works queued on the gcwq and let them process
2378 * those works so that forward progress can be guaranteed.
2379 *
2380 * This should happen rarely.
2381 */
Tejun Heo111c2252013-01-17 17:16:24 -08002382static int rescuer_thread(void *__rescuer)
Tejun Heoe22bee72010-06-29 10:07:14 +02002383{
Tejun Heo111c2252013-01-17 17:16:24 -08002384 struct worker *rescuer = __rescuer;
2385 struct workqueue_struct *wq = rescuer->rescue_wq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002386 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heof3421792010-07-02 10:03:51 +02002387 bool is_unbound = wq->flags & WQ_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02002388 unsigned int cpu;
2389
2390 set_user_nice(current, RESCUER_NICE_LEVEL);
Tejun Heo111c2252013-01-17 17:16:24 -08002391
2392 /*
2393 * Mark rescuer as worker too. As WORKER_PREP is never cleared, it
2394 * doesn't participate in concurrency management.
2395 */
2396 rescuer->task->flags |= PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002397repeat:
2398 set_current_state(TASK_INTERRUPTIBLE);
2399
Mike Galbraith412d32e2012-11-28 07:17:18 +01002400 if (kthread_should_stop()) {
2401 __set_current_state(TASK_RUNNING);
Tejun Heo111c2252013-01-17 17:16:24 -08002402 rescuer->task->flags &= ~PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002403 return 0;
Mike Galbraith412d32e2012-11-28 07:17:18 +01002404 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002405
Tejun Heof3421792010-07-02 10:03:51 +02002406 /*
2407 * See whether any cpu is asking for help. Unbounded
2408 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
2409 */
Tejun Heof2e005a2010-07-20 15:59:09 +02002410 for_each_mayday_cpu(cpu, wq->mayday_mask) {
Tejun Heof3421792010-07-02 10:03:51 +02002411 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2412 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002413 struct worker_pool *pool = cwq->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002414 struct work_struct *work, *n;
2415
2416 __set_current_state(TASK_RUNNING);
Tejun Heof2e005a2010-07-20 15:59:09 +02002417 mayday_clear_cpu(cpu, wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002418
2419 /* migrate to the target cpu if possible */
Tejun Heobd7bdd42012-07-12 14:46:37 -07002420 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002421 worker_maybe_bind_and_lock(rescuer);
2422
2423 /*
2424 * Slurp in all works issued via this workqueue and
2425 * process'em.
2426 */
2427 BUG_ON(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002428 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02002429 if (get_work_cwq(work) == cwq)
2430 move_linked_works(work, scheduled, &n);
2431
2432 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002433
2434 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002435 * Leave this pool. If keep_working() is %true, notify a
Tejun Heo75769582011-02-14 14:04:46 +01002436 * regular worker; otherwise, we end up with 0 concurrency
2437 * and stalling the execution.
2438 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002439 if (keep_working(pool))
2440 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002441
Tejun Heod565ed62013-01-24 11:01:33 -08002442 spin_unlock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002443 }
2444
Tejun Heo111c2252013-01-17 17:16:24 -08002445 /* rescuers should never participate in concurrency management */
2446 WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
Tejun Heoe22bee72010-06-29 10:07:14 +02002447 schedule();
2448 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449}
2450
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002451struct wq_barrier {
2452 struct work_struct work;
2453 struct completion done;
2454};
2455
2456static void wq_barrier_func(struct work_struct *work)
2457{
2458 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2459 complete(&barr->done);
2460}
2461
Tejun Heo4690c4a2010-06-29 10:07:10 +02002462/**
2463 * insert_wq_barrier - insert a barrier work
2464 * @cwq: cwq to insert barrier into
2465 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002466 * @target: target work to attach @barr to
2467 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002468 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002469 * @barr is linked to @target such that @barr is completed only after
2470 * @target finishes execution. Please note that the ordering
2471 * guarantee is observed only with respect to @target and on the local
2472 * cpu.
2473 *
2474 * Currently, a queued barrier can't be canceled. This is because
2475 * try_to_grab_pending() can't determine whether the work to be
2476 * grabbed is at the head of the queue and thus can't clear LINKED
2477 * flag of the previous work while there must be a valid next work
2478 * after a work with LINKED flag set.
2479 *
2480 * Note that when @worker is non-NULL, @target may be modified
2481 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002482 *
2483 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002484 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002485 */
Oleg Nesterov83c22522007-05-09 02:33:54 -07002486static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002487 struct wq_barrier *barr,
2488 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002489{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002490 struct list_head *head;
2491 unsigned int linked = 0;
2492
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002493 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002494 * debugobject calls are safe here even with pool->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002495 * as we know for sure that this will not trigger any of the
2496 * checks and call back into the fixup functions where we
2497 * might deadlock.
2498 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002499 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002500 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002501 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002502
Tejun Heoaffee4b2010-06-29 10:07:12 +02002503 /*
2504 * If @target is currently being executed, schedule the
2505 * barrier to the worker; otherwise, put it after @target.
2506 */
2507 if (worker)
2508 head = worker->scheduled.next;
2509 else {
2510 unsigned long *bits = work_data_bits(target);
2511
2512 head = target->entry.next;
2513 /* there can already be other linked works, inherit and set */
2514 linked = *bits & WORK_STRUCT_LINKED;
2515 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2516 }
2517
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002518 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002519 insert_work(cwq, &barr->work, head,
2520 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002521}
2522
Tejun Heo73f53c42010-06-29 10:07:11 +02002523/**
2524 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2525 * @wq: workqueue being flushed
2526 * @flush_color: new flush color, < 0 for no-op
2527 * @work_color: new work color, < 0 for no-op
2528 *
2529 * Prepare cwqs for workqueue flushing.
2530 *
2531 * If @flush_color is non-negative, flush_color on all cwqs should be
2532 * -1. If no cwq has in-flight commands at the specified color, all
2533 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2534 * has in flight commands, its cwq->flush_color is set to
2535 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2536 * wakeup logic is armed and %true is returned.
2537 *
2538 * The caller should have initialized @wq->first_flusher prior to
2539 * calling this function with non-negative @flush_color. If
2540 * @flush_color is negative, no flush color update is done and %false
2541 * is returned.
2542 *
2543 * If @work_color is non-negative, all cwqs should have the same
2544 * work_color which is previous to @work_color and all will be
2545 * advanced to @work_color.
2546 *
2547 * CONTEXT:
2548 * mutex_lock(wq->flush_mutex).
2549 *
2550 * RETURNS:
2551 * %true if @flush_color >= 0 and there's something to flush. %false
2552 * otherwise.
2553 */
2554static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2555 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556{
Tejun Heo73f53c42010-06-29 10:07:11 +02002557 bool wait = false;
2558 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -07002559
Tejun Heo73f53c42010-06-29 10:07:11 +02002560 if (flush_color >= 0) {
2561 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2562 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002563 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002564
Tejun Heof3421792010-07-02 10:03:51 +02002565 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002566 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heod565ed62013-01-24 11:01:33 -08002567 struct worker_pool *pool = cwq->pool;
Tejun Heo73f53c42010-06-29 10:07:11 +02002568
Tejun Heod565ed62013-01-24 11:01:33 -08002569 spin_lock_irq(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002570
2571 if (flush_color >= 0) {
2572 BUG_ON(cwq->flush_color != -1);
2573
2574 if (cwq->nr_in_flight[flush_color]) {
2575 cwq->flush_color = flush_color;
2576 atomic_inc(&wq->nr_cwqs_to_flush);
2577 wait = true;
2578 }
2579 }
2580
2581 if (work_color >= 0) {
2582 BUG_ON(work_color != work_next_color(cwq->work_color));
2583 cwq->work_color = work_color;
2584 }
2585
Tejun Heod565ed62013-01-24 11:01:33 -08002586 spin_unlock_irq(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002587 }
2588
2589 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2590 complete(&wq->first_flusher->done);
2591
2592 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593}
2594
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002595/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002597 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 *
2599 * Forces execution of the workqueue and blocks until its completion.
2600 * This is typically used in driver shutdown handlers.
2601 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002602 * We sleep until all works which were queued on entry have been handled,
2603 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002605void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606{
Tejun Heo73f53c42010-06-29 10:07:11 +02002607 struct wq_flusher this_flusher = {
2608 .list = LIST_HEAD_INIT(this_flusher.list),
2609 .flush_color = -1,
2610 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2611 };
2612 int next_color;
Oleg Nesterovb1f4ec172007-05-09 02:34:12 -07002613
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002614 lock_map_acquire(&wq->lockdep_map);
2615 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002616
2617 mutex_lock(&wq->flush_mutex);
2618
2619 /*
2620 * Start-to-wait phase
2621 */
2622 next_color = work_next_color(wq->work_color);
2623
2624 if (next_color != wq->flush_color) {
2625 /*
2626 * Color space is not full. The current work_color
2627 * becomes our flush_color and work_color is advanced
2628 * by one.
2629 */
2630 BUG_ON(!list_empty(&wq->flusher_overflow));
2631 this_flusher.flush_color = wq->work_color;
2632 wq->work_color = next_color;
2633
2634 if (!wq->first_flusher) {
2635 /* no flush in progress, become the first flusher */
2636 BUG_ON(wq->flush_color != this_flusher.flush_color);
2637
2638 wq->first_flusher = &this_flusher;
2639
2640 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2641 wq->work_color)) {
2642 /* nothing to flush, done */
2643 wq->flush_color = next_color;
2644 wq->first_flusher = NULL;
2645 goto out_unlock;
2646 }
2647 } else {
2648 /* wait in queue */
2649 BUG_ON(wq->flush_color == this_flusher.flush_color);
2650 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2651 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2652 }
2653 } else {
2654 /*
2655 * Oops, color space is full, wait on overflow queue.
2656 * The next flush completion will assign us
2657 * flush_color and transfer to flusher_queue.
2658 */
2659 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2660 }
2661
2662 mutex_unlock(&wq->flush_mutex);
2663
2664 wait_for_completion(&this_flusher.done);
2665
2666 /*
2667 * Wake-up-and-cascade phase
2668 *
2669 * First flushers are responsible for cascading flushes and
2670 * handling overflow. Non-first flushers can simply return.
2671 */
2672 if (wq->first_flusher != &this_flusher)
2673 return;
2674
2675 mutex_lock(&wq->flush_mutex);
2676
Tejun Heo4ce48b32010-07-02 10:03:51 +02002677 /* we might have raced, check again with mutex held */
2678 if (wq->first_flusher != &this_flusher)
2679 goto out_unlock;
2680
Tejun Heo73f53c42010-06-29 10:07:11 +02002681 wq->first_flusher = NULL;
2682
2683 BUG_ON(!list_empty(&this_flusher.list));
2684 BUG_ON(wq->flush_color != this_flusher.flush_color);
2685
2686 while (true) {
2687 struct wq_flusher *next, *tmp;
2688
2689 /* complete all the flushers sharing the current flush color */
2690 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2691 if (next->flush_color != wq->flush_color)
2692 break;
2693 list_del_init(&next->list);
2694 complete(&next->done);
2695 }
2696
2697 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2698 wq->flush_color != work_next_color(wq->work_color));
2699
2700 /* this flush_color is finished, advance by one */
2701 wq->flush_color = work_next_color(wq->flush_color);
2702
2703 /* one color has been freed, handle overflow queue */
2704 if (!list_empty(&wq->flusher_overflow)) {
2705 /*
2706 * Assign the same color to all overflowed
2707 * flushers, advance work_color and append to
2708 * flusher_queue. This is the start-to-wait
2709 * phase for these overflowed flushers.
2710 */
2711 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2712 tmp->flush_color = wq->work_color;
2713
2714 wq->work_color = work_next_color(wq->work_color);
2715
2716 list_splice_tail_init(&wq->flusher_overflow,
2717 &wq->flusher_queue);
2718 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2719 }
2720
2721 if (list_empty(&wq->flusher_queue)) {
2722 BUG_ON(wq->flush_color != wq->work_color);
2723 break;
2724 }
2725
2726 /*
2727 * Need to flush more colors. Make the next flusher
2728 * the new first flusher and arm cwqs.
2729 */
2730 BUG_ON(wq->flush_color == wq->work_color);
2731 BUG_ON(wq->flush_color != next->flush_color);
2732
2733 list_del_init(&next->list);
2734 wq->first_flusher = next;
2735
2736 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2737 break;
2738
2739 /*
2740 * Meh... this color is already done, clear first
2741 * flusher and repeat cascading.
2742 */
2743 wq->first_flusher = NULL;
2744 }
2745
2746out_unlock:
2747 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748}
Dave Jonesae90dd52006-06-30 01:40:45 -04002749EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002751/**
2752 * drain_workqueue - drain a workqueue
2753 * @wq: workqueue to drain
2754 *
2755 * Wait until the workqueue becomes empty. While draining is in progress,
2756 * only chain queueing is allowed. IOW, only currently pending or running
2757 * work items on @wq can queue further work items on it. @wq is flushed
2758 * repeatedly until it becomes empty. The number of flushing is detemined
2759 * by the depth of chaining and should be relatively short. Whine if it
2760 * takes too long.
2761 */
2762void drain_workqueue(struct workqueue_struct *wq)
2763{
2764 unsigned int flush_cnt = 0;
2765 unsigned int cpu;
2766
2767 /*
2768 * __queue_work() needs to test whether there are drainers, is much
2769 * hotter than drain_workqueue() and already looks at @wq->flags.
2770 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2771 */
2772 spin_lock(&workqueue_lock);
2773 if (!wq->nr_drainers++)
2774 wq->flags |= WQ_DRAINING;
2775 spin_unlock(&workqueue_lock);
2776reflush:
2777 flush_workqueue(wq);
2778
2779 for_each_cwq_cpu(cpu, wq) {
2780 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002781 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002782
Tejun Heod565ed62013-01-24 11:01:33 -08002783 spin_lock_irq(&cwq->pool->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002784 drained = !cwq->nr_active && list_empty(&cwq->delayed_works);
Tejun Heod565ed62013-01-24 11:01:33 -08002785 spin_unlock_irq(&cwq->pool->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002786
2787 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002788 continue;
2789
2790 if (++flush_cnt == 10 ||
2791 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
Valentin Ilie044c7822012-08-19 00:52:42 +03002792 pr_warn("workqueue %s: flush on destruction isn't complete after %u tries\n",
2793 wq->name, flush_cnt);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002794 goto reflush;
2795 }
2796
2797 spin_lock(&workqueue_lock);
2798 if (!--wq->nr_drainers)
2799 wq->flags &= ~WQ_DRAINING;
2800 spin_unlock(&workqueue_lock);
2801}
2802EXPORT_SYMBOL_GPL(drain_workqueue);
2803
Tejun Heo606a5022012-08-20 14:51:23 -07002804static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
Tejun Heobaf59022010-09-16 10:42:16 +02002805{
2806 struct worker *worker = NULL;
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002807 struct worker_pool *pool;
Tejun Heobaf59022010-09-16 10:42:16 +02002808 struct cpu_workqueue_struct *cwq;
2809
2810 might_sleep();
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002811 pool = get_work_pool(work);
2812 if (!pool)
Tejun Heobaf59022010-09-16 10:42:16 +02002813 return false;
2814
Tejun Heod565ed62013-01-24 11:01:33 -08002815 spin_lock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002816 if (!list_empty(&work->entry)) {
2817 /*
2818 * See the comment near try_to_grab_pending()->smp_rmb().
Tejun Heod565ed62013-01-24 11:01:33 -08002819 * If it was re-queued to a different pool under us, we
Tejun Heobaf59022010-09-16 10:42:16 +02002820 * are not going to wait.
2821 */
2822 smp_rmb();
2823 cwq = get_work_cwq(work);
Tejun Heod565ed62013-01-24 11:01:33 -08002824 if (unlikely(!cwq || pool != cwq->pool))
Tejun Heobaf59022010-09-16 10:42:16 +02002825 goto already_gone;
Tejun Heo606a5022012-08-20 14:51:23 -07002826 } else {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002827 worker = find_worker_executing_work(pool, work);
Tejun Heobaf59022010-09-16 10:42:16 +02002828 if (!worker)
2829 goto already_gone;
2830 cwq = worker->current_cwq;
Tejun Heo606a5022012-08-20 14:51:23 -07002831 }
Tejun Heobaf59022010-09-16 10:42:16 +02002832
2833 insert_wq_barrier(cwq, barr, work, worker);
Tejun Heod565ed62013-01-24 11:01:33 -08002834 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002835
Tejun Heoe159489b2011-01-09 23:32:15 +01002836 /*
2837 * If @max_active is 1 or rescuer is in use, flushing another work
2838 * item on the same workqueue may lead to deadlock. Make sure the
2839 * flusher is not running on the same workqueue by verifying write
2840 * access.
2841 */
2842 if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER)
2843 lock_map_acquire(&cwq->wq->lockdep_map);
2844 else
2845 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heobaf59022010-09-16 10:42:16 +02002846 lock_map_release(&cwq->wq->lockdep_map);
Tejun Heoe159489b2011-01-09 23:32:15 +01002847
Tejun Heobaf59022010-09-16 10:42:16 +02002848 return true;
2849already_gone:
Tejun Heod565ed62013-01-24 11:01:33 -08002850 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002851 return false;
2852}
2853
Oleg Nesterovdb700892008-07-25 01:47:49 -07002854/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002855 * flush_work - wait for a work to finish executing the last queueing instance
2856 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002857 *
Tejun Heo606a5022012-08-20 14:51:23 -07002858 * Wait until @work has finished execution. @work is guaranteed to be idle
2859 * on return if it hasn't been requeued since flush started.
Tejun Heo401a8d02010-09-16 10:36:00 +02002860 *
2861 * RETURNS:
2862 * %true if flush_work() waited for the work to finish execution,
2863 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002864 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002865bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002866{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002867 struct wq_barrier barr;
2868
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002869 lock_map_acquire(&work->lockdep_map);
2870 lock_map_release(&work->lockdep_map);
2871
Tejun Heo606a5022012-08-20 14:51:23 -07002872 if (start_flush_work(work, &barr)) {
Tejun Heobaf59022010-09-16 10:42:16 +02002873 wait_for_completion(&barr.done);
2874 destroy_work_on_stack(&barr.work);
2875 return true;
Tejun Heo606a5022012-08-20 14:51:23 -07002876 } else {
Tejun Heobaf59022010-09-16 10:42:16 +02002877 return false;
Tejun Heo606a5022012-08-20 14:51:23 -07002878 }
Oleg Nesterovdb700892008-07-25 01:47:49 -07002879}
2880EXPORT_SYMBOL_GPL(flush_work);
2881
Tejun Heo36e227d2012-08-03 10:30:46 -07002882static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
Tejun Heo401a8d02010-09-16 10:36:00 +02002883{
Tejun Heobbb68df2012-08-03 10:30:46 -07002884 unsigned long flags;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002885 int ret;
2886
2887 do {
Tejun Heobbb68df2012-08-03 10:30:46 -07002888 ret = try_to_grab_pending(work, is_dwork, &flags);
2889 /*
2890 * If someone else is canceling, wait for the same event it
2891 * would be waiting for before retrying.
2892 */
2893 if (unlikely(ret == -ENOENT))
Tejun Heo606a5022012-08-20 14:51:23 -07002894 flush_work(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002895 } while (unlikely(ret < 0));
2896
Tejun Heobbb68df2012-08-03 10:30:46 -07002897 /* tell other tasks trying to grab @work to back off */
2898 mark_work_canceling(work);
2899 local_irq_restore(flags);
2900
Tejun Heo606a5022012-08-20 14:51:23 -07002901 flush_work(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002902 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002903 return ret;
2904}
2905
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002906/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002907 * cancel_work_sync - cancel a work and wait for it to finish
2908 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002909 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002910 * Cancel @work and wait for its execution to finish. This function
2911 * can be used even if the work re-queues itself or migrates to
2912 * another workqueue. On return from this function, @work is
2913 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002914 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002915 * cancel_work_sync(&delayed_work->work) must not be used for
2916 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002917 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002918 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002919 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002920 *
2921 * RETURNS:
2922 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002923 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002924bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002925{
Tejun Heo36e227d2012-08-03 10:30:46 -07002926 return __cancel_work_timer(work, false);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002927}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002928EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002929
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002930/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002931 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2932 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002933 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002934 * Delayed timer is cancelled and the pending work is queued for
2935 * immediate execution. Like flush_work(), this function only
2936 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002937 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002938 * RETURNS:
2939 * %true if flush_work() waited for the work to finish execution,
2940 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002941 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002942bool flush_delayed_work(struct delayed_work *dwork)
2943{
Tejun Heo8930cab2012-08-03 10:30:45 -07002944 local_irq_disable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002945 if (del_timer_sync(&dwork->timer))
Tejun Heo12650572012-08-08 09:38:42 -07002946 __queue_work(dwork->cpu,
Tejun Heo401a8d02010-09-16 10:36:00 +02002947 get_work_cwq(&dwork->work)->wq, &dwork->work);
Tejun Heo8930cab2012-08-03 10:30:45 -07002948 local_irq_enable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002949 return flush_work(&dwork->work);
2950}
2951EXPORT_SYMBOL(flush_delayed_work);
2952
2953/**
Tejun Heo57b30ae2012-08-21 13:18:24 -07002954 * cancel_delayed_work - cancel a delayed work
2955 * @dwork: delayed_work to cancel
Tejun Heo09383492010-09-16 10:48:29 +02002956 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002957 * Kill off a pending delayed_work. Returns %true if @dwork was pending
2958 * and canceled; %false if wasn't pending. Note that the work callback
2959 * function may still be running on return, unless it returns %true and the
2960 * work doesn't re-arm itself. Explicitly flush or use
2961 * cancel_delayed_work_sync() to wait on it.
Tejun Heo09383492010-09-16 10:48:29 +02002962 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002963 * This function is safe to call from any context including IRQ handler.
Tejun Heo09383492010-09-16 10:48:29 +02002964 */
Tejun Heo57b30ae2012-08-21 13:18:24 -07002965bool cancel_delayed_work(struct delayed_work *dwork)
Tejun Heo09383492010-09-16 10:48:29 +02002966{
Tejun Heo57b30ae2012-08-21 13:18:24 -07002967 unsigned long flags;
2968 int ret;
2969
2970 do {
2971 ret = try_to_grab_pending(&dwork->work, true, &flags);
2972 } while (unlikely(ret == -EAGAIN));
2973
2974 if (unlikely(ret < 0))
2975 return false;
2976
Tejun Heo7c3eed52013-01-24 11:01:33 -08002977 set_work_pool_and_clear_pending(&dwork->work,
2978 get_work_pool_id(&dwork->work));
Tejun Heo57b30ae2012-08-21 13:18:24 -07002979 local_irq_restore(flags);
Dan Magenheimerc0158ca2012-10-18 16:31:37 -07002980 return ret;
Tejun Heo09383492010-09-16 10:48:29 +02002981}
Tejun Heo57b30ae2012-08-21 13:18:24 -07002982EXPORT_SYMBOL(cancel_delayed_work);
Tejun Heo09383492010-09-16 10:48:29 +02002983
2984/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002985 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2986 * @dwork: the delayed work cancel
2987 *
2988 * This is cancel_work_sync() for delayed works.
2989 *
2990 * RETURNS:
2991 * %true if @dwork was pending, %false otherwise.
2992 */
2993bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002994{
Tejun Heo36e227d2012-08-03 10:30:46 -07002995 return __cancel_work_timer(&dwork->work, true);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002996}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002997EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002999/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07003000 * schedule_work_on - put work task on a specific cpu
3001 * @cpu: cpu to put the work task on
3002 * @work: job to be done
3003 *
3004 * This puts a job on a specific cpu
3005 */
Tejun Heod4283e92012-08-03 10:30:44 -07003006bool schedule_work_on(int cpu, struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07003007{
Tejun Heod320c032010-06-29 10:07:14 +02003008 return queue_work_on(cpu, system_wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07003009}
3010EXPORT_SYMBOL(schedule_work_on);
3011
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003012/**
Dave Jonesae90dd52006-06-30 01:40:45 -04003013 * schedule_work - put work task in global workqueue
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 * @work: job to be done
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 *
Tejun Heod4283e92012-08-03 10:30:44 -07003016 * Returns %false if @work was already on the kernel-global workqueue and
3017 * %true otherwise.
David Howells52bad642006-11-22 14:54:01 +00003018 *
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003019 * This puts a job in the kernel-global workqueue if it was not already
3020 * queued and leaves it in the same position on the kernel-global
3021 * workqueue otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 */
Tejun Heod4283e92012-08-03 10:30:44 -07003023bool schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024{
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003025 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026}
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003027EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003029/**
3030 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
3031 * @cpu: cpu to use
3032 * @dwork: job to be done
3033 * @delay: number of jiffies to wait
3034 *
3035 * After waiting for a given time this puts a job in the kernel-global
3036 * workqueue on the specified CPU.
3037 */
Tejun Heod4283e92012-08-03 10:30:44 -07003038bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork,
3039 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040{
Tejun Heod320c032010-06-29 10:07:14 +02003041 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042}
Dave Jonesae90dd52006-06-30 01:40:45 -04003043EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
Andrew Mortonb6136772006-06-25 05:47:49 -07003045/**
Tejun Heo0a13c002012-08-03 10:30:44 -07003046 * schedule_delayed_work - put work task in global workqueue after delay
3047 * @dwork: job to be done
3048 * @delay: number of jiffies to wait or 0 for immediate execution
3049 *
3050 * After waiting for a given time this puts a job in the kernel-global
3051 * workqueue.
3052 */
Tejun Heod4283e92012-08-03 10:30:44 -07003053bool schedule_delayed_work(struct delayed_work *dwork, unsigned long delay)
Tejun Heo0a13c002012-08-03 10:30:44 -07003054{
3055 return queue_delayed_work(system_wq, dwork, delay);
3056}
3057EXPORT_SYMBOL(schedule_delayed_work);
3058
3059/**
Tejun Heo31ddd872010-10-19 11:14:49 +02003060 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07003061 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07003062 *
Tejun Heo31ddd872010-10-19 11:14:49 +02003063 * schedule_on_each_cpu() executes @func on each online CPU using the
3064 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07003065 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02003066 *
3067 * RETURNS:
3068 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07003069 */
David Howells65f27f32006-11-22 14:55:48 +00003070int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08003071{
3072 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02003073 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08003074
Andrew Mortonb6136772006-06-25 05:47:49 -07003075 works = alloc_percpu(struct work_struct);
3076 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08003077 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07003078
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003079 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08003080
Christoph Lameter15316ba2006-01-08 01:00:43 -08003081 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01003082 struct work_struct *work = per_cpu_ptr(works, cpu);
3083
3084 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003085 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02003086 }
Tejun Heo93981802009-11-17 14:06:20 -08003087
3088 for_each_online_cpu(cpu)
3089 flush_work(per_cpu_ptr(works, cpu));
3090
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003091 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07003092 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08003093 return 0;
3094}
3095
Alan Sterneef6a7d2010-02-12 17:39:21 +09003096/**
3097 * flush_scheduled_work - ensure that any scheduled work has run to completion.
3098 *
3099 * Forces execution of the kernel-global workqueue and blocks until its
3100 * completion.
3101 *
3102 * Think twice before calling this function! It's very easy to get into
3103 * trouble if you don't take great care. Either of the following situations
3104 * will lead to deadlock:
3105 *
3106 * One of the work items currently on the workqueue needs to acquire
3107 * a lock held by your code or its caller.
3108 *
3109 * Your code is running in the context of a work routine.
3110 *
3111 * They will be detected by lockdep when they occur, but the first might not
3112 * occur very often. It depends on what work items are on the workqueue and
3113 * what locks they need, which you have no control over.
3114 *
3115 * In most situations flushing the entire workqueue is overkill; you merely
3116 * need to know that a particular work item isn't queued and isn't running.
3117 * In such cases you should use cancel_delayed_work_sync() or
3118 * cancel_work_sync() instead.
3119 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120void flush_scheduled_work(void)
3121{
Tejun Heod320c032010-06-29 10:07:14 +02003122 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123}
Dave Jonesae90dd52006-06-30 01:40:45 -04003124EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125
3126/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06003127 * execute_in_process_context - reliably execute the routine with user context
3128 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06003129 * @ew: guaranteed storage for the execute work structure (must
3130 * be available when the work executes)
3131 *
3132 * Executes the function immediately if process context is available,
3133 * otherwise schedules the function for delayed execution.
3134 *
3135 * Returns: 0 - function was executed
3136 * 1 - function was scheduled for execution
3137 */
David Howells65f27f32006-11-22 14:55:48 +00003138int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06003139{
3140 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00003141 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003142 return 0;
3143 }
3144
David Howells65f27f32006-11-22 14:55:48 +00003145 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003146 schedule_work(&ew->work);
3147
3148 return 1;
3149}
3150EXPORT_SYMBOL_GPL(execute_in_process_context);
3151
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152int keventd_up(void)
3153{
Tejun Heod320c032010-06-29 10:07:14 +02003154 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155}
3156
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003157static int alloc_cwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158{
Oleg Nesterov3af244332007-05-09 02:34:09 -07003159 /*
Tejun Heo0f900042010-06-29 10:07:11 +02003160 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
3161 * Make sure that the alignment isn't lower than that of
3162 * unsigned long long.
Oleg Nesterov3af244332007-05-09 02:34:09 -07003163 */
Tejun Heo0f900042010-06-29 10:07:11 +02003164 const size_t size = sizeof(struct cpu_workqueue_struct);
3165 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
3166 __alignof__(unsigned long long));
Oleg Nesterov3af244332007-05-09 02:34:09 -07003167
Lai Jiangshane06ffa12012-03-09 18:03:20 +08003168 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02003169 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
Tejun Heo931ac772010-07-20 11:07:48 +02003170 else {
Tejun Heof3421792010-07-02 10:03:51 +02003171 void *ptr;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01003172
Tejun Heof3421792010-07-02 10:03:51 +02003173 /*
3174 * Allocate enough room to align cwq and put an extra
3175 * pointer at the end pointing back to the originally
3176 * allocated pointer which will be used for free.
3177 */
3178 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
3179 if (ptr) {
3180 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
3181 *(void **)(wq->cpu_wq.single + 1) = ptr;
3182 }
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003183 }
Tejun Heof3421792010-07-02 10:03:51 +02003184
Tejun Heo0415b00d12011-03-24 18:50:09 +01003185 /* just in case, make sure it's actually aligned */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003186 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
3187 return wq->cpu_wq.v ? 0 : -ENOMEM;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003188}
3189
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003190static void free_cwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07003191{
Lai Jiangshane06ffa12012-03-09 18:03:20 +08003192 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02003193 free_percpu(wq->cpu_wq.pcpu);
3194 else if (wq->cpu_wq.single) {
3195 /* the pointer to free is stored right after the cwq */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003196 kfree(*(void **)(wq->cpu_wq.single + 1));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07003197 }
3198}
3199
Tejun Heof3421792010-07-02 10:03:51 +02003200static int wq_clamp_max_active(int max_active, unsigned int flags,
3201 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003202{
Tejun Heof3421792010-07-02 10:03:51 +02003203 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3204
3205 if (max_active < 1 || max_active > lim)
Valentin Ilie044c7822012-08-19 00:52:42 +03003206 pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
3207 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003208
Tejun Heof3421792010-07-02 10:03:51 +02003209 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003210}
3211
Tejun Heob196be82012-01-10 15:11:35 -08003212struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02003213 unsigned int flags,
3214 int max_active,
3215 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08003216 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003217{
Tejun Heob196be82012-01-10 15:11:35 -08003218 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003219 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02003220 unsigned int cpu;
Tejun Heob196be82012-01-10 15:11:35 -08003221 size_t namelen;
3222
3223 /* determine namelen, allocate wq and format name */
3224 va_start(args, lock_name);
3225 va_copy(args1, args);
3226 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3227
3228 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3229 if (!wq)
3230 goto err;
3231
3232 vsnprintf(wq->name, namelen, fmt, args1);
3233 va_end(args);
3234 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003235
Tejun Heof3421792010-07-02 10:03:51 +02003236 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02003237 * Workqueues which may be used during memory reclaim should
3238 * have a rescuer to guarantee forward progress.
3239 */
3240 if (flags & WQ_MEM_RECLAIM)
3241 flags |= WQ_RESCUER;
3242
Tejun Heod320c032010-06-29 10:07:14 +02003243 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003244 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003245
Tejun Heob196be82012-01-10 15:11:35 -08003246 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003247 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003248 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003249 mutex_init(&wq->flush_mutex);
3250 atomic_set(&wq->nr_cwqs_to_flush, 0);
3251 INIT_LIST_HEAD(&wq->flusher_queue);
3252 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003253
Johannes Bergeb13ba82008-01-16 09:51:58 +01003254 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003255 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003256
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003257 if (alloc_cwqs(wq) < 0)
3258 goto err;
3259
Tejun Heof3421792010-07-02 10:03:51 +02003260 for_each_cwq_cpu(cpu, wq) {
Tejun Heo15376632010-06-29 10:07:11 +02003261 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3262
Tejun Heo0f900042010-06-29 10:07:11 +02003263 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heoa60dc392013-01-24 11:01:34 -08003264 cwq->pool = get_std_worker_pool(cpu, flags & WQ_HIGHPRI);
Tejun Heoc34056a2010-06-29 10:07:11 +02003265 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02003266 cwq->flush_color = -1;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003267 cwq->max_active = max_active;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003268 INIT_LIST_HEAD(&cwq->delayed_works);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003269 }
3270
Tejun Heoe22bee72010-06-29 10:07:14 +02003271 if (flags & WQ_RESCUER) {
3272 struct worker *rescuer;
3273
Tejun Heof2e005a2010-07-20 15:59:09 +02003274 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
Tejun Heoe22bee72010-06-29 10:07:14 +02003275 goto err;
3276
3277 wq->rescuer = rescuer = alloc_worker();
3278 if (!rescuer)
3279 goto err;
3280
Tejun Heo111c2252013-01-17 17:16:24 -08003281 rescuer->rescue_wq = wq;
3282 rescuer->task = kthread_create(rescuer_thread, rescuer, "%s",
Tejun Heob196be82012-01-10 15:11:35 -08003283 wq->name);
Tejun Heoe22bee72010-06-29 10:07:14 +02003284 if (IS_ERR(rescuer->task))
3285 goto err;
3286
Tejun Heoe22bee72010-06-29 10:07:14 +02003287 rescuer->task->flags |= PF_THREAD_BOUND;
3288 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003289 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003290
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003291 /*
3292 * workqueue_lock protects global freeze state and workqueues
3293 * list. Grab it, set max_active accordingly and add the new
3294 * workqueue to workqueues list.
3295 */
Tejun Heo15376632010-06-29 10:07:11 +02003296 spin_lock(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003297
Tejun Heo58a69cb2011-02-16 09:25:31 +01003298 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heof3421792010-07-02 10:03:51 +02003299 for_each_cwq_cpu(cpu, wq)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003300 get_cwq(cpu, wq)->max_active = 0;
3301
Tejun Heo15376632010-06-29 10:07:11 +02003302 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003303
Tejun Heo15376632010-06-29 10:07:11 +02003304 spin_unlock(&workqueue_lock);
3305
Oleg Nesterov3af244332007-05-09 02:34:09 -07003306 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02003307err:
3308 if (wq) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003309 free_cwqs(wq);
Tejun Heof2e005a2010-07-20 15:59:09 +02003310 free_mayday_mask(wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02003311 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003312 kfree(wq);
3313 }
3314 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003315}
Tejun Heod320c032010-06-29 10:07:14 +02003316EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003317
3318/**
3319 * destroy_workqueue - safely terminate a workqueue
3320 * @wq: target workqueue
3321 *
3322 * Safely destroy a workqueue. All work currently pending will be done first.
3323 */
3324void destroy_workqueue(struct workqueue_struct *wq)
3325{
Tejun Heoc8e55f32010-06-29 10:07:12 +02003326 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003327
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003328 /* drain it before proceeding with destruction */
3329 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003330
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003331 /*
3332 * wq list is used to freeze wq, remove from list after
3333 * flushing is complete in case freeze races us.
3334 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003335 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec172007-05-09 02:34:12 -07003336 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003337 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003338
Tejun Heoe22bee72010-06-29 10:07:14 +02003339 /* sanity check */
Tejun Heof3421792010-07-02 10:03:51 +02003340 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02003341 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3342 int i;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003343
Tejun Heo73f53c42010-06-29 10:07:11 +02003344 for (i = 0; i < WORK_NR_COLORS; i++)
3345 BUG_ON(cwq->nr_in_flight[i]);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003346 BUG_ON(cwq->nr_active);
3347 BUG_ON(!list_empty(&cwq->delayed_works));
Tejun Heo73f53c42010-06-29 10:07:11 +02003348 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003349
Tejun Heoe22bee72010-06-29 10:07:14 +02003350 if (wq->flags & WQ_RESCUER) {
3351 kthread_stop(wq->rescuer->task);
Tejun Heof2e005a2010-07-20 15:59:09 +02003352 free_mayday_mask(wq->mayday_mask);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003353 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003354 }
3355
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003356 free_cwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003357 kfree(wq);
3358}
3359EXPORT_SYMBOL_GPL(destroy_workqueue);
3360
Tejun Heodcd989c2010-06-29 10:07:14 +02003361/**
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003362 * cwq_set_max_active - adjust max_active of a cwq
3363 * @cwq: target cpu_workqueue_struct
3364 * @max_active: new max_active value.
3365 *
3366 * Set @cwq->max_active to @max_active and activate delayed works if
3367 * increased.
3368 *
3369 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08003370 * spin_lock_irq(pool->lock).
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003371 */
3372static void cwq_set_max_active(struct cpu_workqueue_struct *cwq, int max_active)
3373{
3374 cwq->max_active = max_active;
3375
3376 while (!list_empty(&cwq->delayed_works) &&
3377 cwq->nr_active < cwq->max_active)
3378 cwq_activate_first_delayed(cwq);
3379}
3380
3381/**
Tejun Heodcd989c2010-06-29 10:07:14 +02003382 * workqueue_set_max_active - adjust max_active of a workqueue
3383 * @wq: target workqueue
3384 * @max_active: new max_active value.
3385 *
3386 * Set max_active of @wq to @max_active.
3387 *
3388 * CONTEXT:
3389 * Don't call from IRQ context.
3390 */
3391void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3392{
3393 unsigned int cpu;
3394
Tejun Heof3421792010-07-02 10:03:51 +02003395 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003396
3397 spin_lock(&workqueue_lock);
3398
3399 wq->saved_max_active = max_active;
3400
Tejun Heof3421792010-07-02 10:03:51 +02003401 for_each_cwq_cpu(cpu, wq) {
Tejun Heo35b6bb62013-01-24 11:01:33 -08003402 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3403 struct worker_pool *pool = cwq->pool;
Tejun Heodcd989c2010-06-29 10:07:14 +02003404
Tejun Heod565ed62013-01-24 11:01:33 -08003405 spin_lock_irq(&pool->lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02003406
Tejun Heo58a69cb2011-02-16 09:25:31 +01003407 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heo35b6bb62013-01-24 11:01:33 -08003408 !(pool->flags & POOL_FREEZING))
3409 cwq_set_max_active(cwq, max_active);
Tejun Heodcd989c2010-06-29 10:07:14 +02003410
Tejun Heod565ed62013-01-24 11:01:33 -08003411 spin_unlock_irq(&pool->lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02003412 }
3413
3414 spin_unlock(&workqueue_lock);
3415}
3416EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3417
3418/**
3419 * workqueue_congested - test whether a workqueue is congested
3420 * @cpu: CPU in question
3421 * @wq: target workqueue
3422 *
3423 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3424 * no synchronization around this function and the test result is
3425 * unreliable and only useful as advisory hints or for debugging.
3426 *
3427 * RETURNS:
3428 * %true if congested, %false otherwise.
3429 */
3430bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3431{
3432 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3433
3434 return !list_empty(&cwq->delayed_works);
3435}
3436EXPORT_SYMBOL_GPL(workqueue_congested);
3437
3438/**
Tejun Heodcd989c2010-06-29 10:07:14 +02003439 * work_busy - test whether a work is currently pending or running
3440 * @work: the work to be tested
3441 *
3442 * Test whether @work is currently pending or running. There is no
3443 * synchronization around this function and the test result is
3444 * unreliable and only useful as advisory hints or for debugging.
3445 * Especially for reentrant wqs, the pending state might hide the
3446 * running state.
3447 *
3448 * RETURNS:
3449 * OR'd bitmask of WORK_BUSY_* bits.
3450 */
3451unsigned int work_busy(struct work_struct *work)
3452{
Tejun Heoc9e7cf22013-01-24 11:01:33 -08003453 struct worker_pool *pool = get_work_pool(work);
Tejun Heodcd989c2010-06-29 10:07:14 +02003454 unsigned long flags;
3455 unsigned int ret = 0;
3456
Tejun Heoc9e7cf22013-01-24 11:01:33 -08003457 if (!pool)
Joonsoo Kim999767b2012-10-21 01:30:06 +09003458 return 0;
Tejun Heodcd989c2010-06-29 10:07:14 +02003459
Tejun Heod565ed62013-01-24 11:01:33 -08003460 spin_lock_irqsave(&pool->lock, flags);
Tejun Heodcd989c2010-06-29 10:07:14 +02003461
3462 if (work_pending(work))
3463 ret |= WORK_BUSY_PENDING;
Tejun Heoc9e7cf22013-01-24 11:01:33 -08003464 if (find_worker_executing_work(pool, work))
Tejun Heodcd989c2010-06-29 10:07:14 +02003465 ret |= WORK_BUSY_RUNNING;
3466
Tejun Heod565ed62013-01-24 11:01:33 -08003467 spin_unlock_irqrestore(&pool->lock, flags);
Tejun Heodcd989c2010-06-29 10:07:14 +02003468
3469 return ret;
3470}
3471EXPORT_SYMBOL_GPL(work_busy);
3472
Tejun Heodb7bccf2010-06-29 10:07:12 +02003473/*
3474 * CPU hotplug.
3475 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003476 * There are two challenges in supporting CPU hotplug. Firstly, there
3477 * are a lot of assumptions on strong associations among work, cwq and
3478 * gcwq which make migrating pending and scheduled works very
3479 * difficult to implement without impacting hot paths. Secondly,
Tejun Heo94cf58b2013-01-24 11:01:33 -08003480 * worker pools serve mix of short, long and very long running works making
Tejun Heoe22bee72010-06-29 10:07:14 +02003481 * blocked draining impractical.
3482 *
Tejun Heo24647572013-01-24 11:01:33 -08003483 * This is solved by allowing the pools to be disassociated from the CPU
Tejun Heo628c78e2012-07-17 12:39:27 -07003484 * running as an unbound one and allowing it to be reattached later if the
3485 * cpu comes back online.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003486 */
3487
Tejun Heo628c78e2012-07-17 12:39:27 -07003488static void gcwq_unbind_fn(struct work_struct *work)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003489{
Tejun Heo38db41d2013-01-24 11:01:34 -08003490 int cpu = smp_processor_id();
Tejun Heo4ce62e92012-07-13 22:16:44 -07003491 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003492 struct worker *worker;
3493 struct hlist_node *pos;
3494 int i;
3495
Tejun Heo38db41d2013-01-24 11:01:34 -08003496 for_each_std_worker_pool(pool, cpu) {
3497 BUG_ON(cpu != smp_processor_id());
Tejun Heo94cf58b2013-01-24 11:01:33 -08003498
3499 mutex_lock(&pool->assoc_mutex);
3500 spin_lock_irq(&pool->lock);
3501
3502 /*
3503 * We've claimed all manager positions. Make all workers
3504 * unbound and set DISASSOCIATED. Before this, all workers
3505 * except for the ones which are still executing works from
3506 * before the last CPU down must be on the cpu. After
3507 * this, they may become diasporas.
3508 */
Tejun Heo4ce62e92012-07-13 22:16:44 -07003509 list_for_each_entry(worker, &pool->idle_list, entry)
Tejun Heo403c8212012-07-17 12:39:27 -07003510 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003511
Tejun Heoc9e7cf22013-01-24 11:01:33 -08003512 for_each_busy_worker(worker, i, pos, pool)
3513 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003514
Tejun Heo24647572013-01-24 11:01:33 -08003515 pool->flags |= POOL_DISASSOCIATED;
Tejun Heof2d5a0e2012-07-17 12:39:26 -07003516
Tejun Heo94cf58b2013-01-24 11:01:33 -08003517 spin_unlock_irq(&pool->lock);
3518 mutex_unlock(&pool->assoc_mutex);
3519 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003520
3521 /*
Tejun Heo628c78e2012-07-17 12:39:27 -07003522 * Call schedule() so that we cross rq->lock and thus can guarantee
3523 * sched callbacks see the %WORKER_UNBOUND flag. This is necessary
3524 * as scheduler callbacks may be invoked from other cpus.
3525 */
3526 schedule();
3527
3528 /*
3529 * Sched callbacks are disabled now. Zap nr_running. After this,
3530 * nr_running stays zero and need_more_worker() and keep_working()
Tejun Heo38db41d2013-01-24 11:01:34 -08003531 * are always true as long as the worklist is not empty. Pools on
3532 * @cpu now behave as unbound (in terms of concurrency management)
3533 * pools which are served by workers tied to the CPU.
Tejun Heo628c78e2012-07-17 12:39:27 -07003534 *
3535 * On return from this function, the current worker would trigger
3536 * unbound chain execution of pending work items if other workers
3537 * didn't already.
Tejun Heoe22bee72010-06-29 10:07:14 +02003538 */
Tejun Heo38db41d2013-01-24 11:01:34 -08003539 for_each_std_worker_pool(pool, cpu)
Tejun Heo4ce62e92012-07-13 22:16:44 -07003540 atomic_set(get_pool_nr_running(pool), 0);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003541}
3542
Tejun Heo8db25e72012-07-17 12:39:28 -07003543/*
3544 * Workqueues should be brought up before normal priority CPU notifiers.
3545 * This will be registered high priority CPU notifier.
3546 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07003547static int __cpuinit workqueue_cpu_up_callback(struct notifier_block *nfb,
Tejun Heo8db25e72012-07-17 12:39:28 -07003548 unsigned long action,
3549 void *hcpu)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003550{
3551 unsigned int cpu = (unsigned long)hcpu;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003552 struct worker_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553
Tejun Heo8db25e72012-07-17 12:39:28 -07003554 switch (action & ~CPU_TASKS_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 case CPU_UP_PREPARE:
Tejun Heo38db41d2013-01-24 11:01:34 -08003556 for_each_std_worker_pool(pool, cpu) {
Tejun Heo3ce63372012-07-17 12:39:27 -07003557 struct worker *worker;
3558
3559 if (pool->nr_workers)
3560 continue;
3561
3562 worker = create_worker(pool);
3563 if (!worker)
3564 return NOTIFY_BAD;
3565
Tejun Heod565ed62013-01-24 11:01:33 -08003566 spin_lock_irq(&pool->lock);
Tejun Heo3ce63372012-07-17 12:39:27 -07003567 start_worker(worker);
Tejun Heod565ed62013-01-24 11:01:33 -08003568 spin_unlock_irq(&pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003570 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003571
Tejun Heo65758202012-07-17 12:39:26 -07003572 case CPU_DOWN_FAILED:
3573 case CPU_ONLINE:
Tejun Heo38db41d2013-01-24 11:01:34 -08003574 for_each_std_worker_pool(pool, cpu) {
Tejun Heo94cf58b2013-01-24 11:01:33 -08003575 mutex_lock(&pool->assoc_mutex);
3576 spin_lock_irq(&pool->lock);
3577
Tejun Heo24647572013-01-24 11:01:33 -08003578 pool->flags &= ~POOL_DISASSOCIATED;
Tejun Heo94cf58b2013-01-24 11:01:33 -08003579 rebind_workers(pool);
3580
3581 spin_unlock_irq(&pool->lock);
3582 mutex_unlock(&pool->assoc_mutex);
3583 }
Tejun Heo8db25e72012-07-17 12:39:28 -07003584 break;
Tejun Heo65758202012-07-17 12:39:26 -07003585 }
3586 return NOTIFY_OK;
3587}
3588
3589/*
3590 * Workqueues should be brought down after normal priority CPU notifiers.
3591 * This will be registered as low priority CPU notifier.
3592 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07003593static int __cpuinit workqueue_cpu_down_callback(struct notifier_block *nfb,
Tejun Heo65758202012-07-17 12:39:26 -07003594 unsigned long action,
3595 void *hcpu)
3596{
Tejun Heo8db25e72012-07-17 12:39:28 -07003597 unsigned int cpu = (unsigned long)hcpu;
3598 struct work_struct unbind_work;
3599
Tejun Heo65758202012-07-17 12:39:26 -07003600 switch (action & ~CPU_TASKS_FROZEN) {
3601 case CPU_DOWN_PREPARE:
Tejun Heo8db25e72012-07-17 12:39:28 -07003602 /* unbinding should happen on the local CPU */
3603 INIT_WORK_ONSTACK(&unbind_work, gcwq_unbind_fn);
Joonsoo Kim7635d2f2012-08-15 23:25:41 +09003604 queue_work_on(cpu, system_highpri_wq, &unbind_work);
Tejun Heo8db25e72012-07-17 12:39:28 -07003605 flush_work(&unbind_work);
3606 break;
Tejun Heo65758202012-07-17 12:39:26 -07003607 }
3608 return NOTIFY_OK;
3609}
3610
Rusty Russell2d3854a2008-11-05 13:39:10 +11003611#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003612
Rusty Russell2d3854a2008-11-05 13:39:10 +11003613struct work_for_cpu {
Tejun Heoed48ece2012-09-18 12:48:43 -07003614 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003615 long (*fn)(void *);
3616 void *arg;
3617 long ret;
3618};
3619
Tejun Heoed48ece2012-09-18 12:48:43 -07003620static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003621{
Tejun Heoed48ece2012-09-18 12:48:43 -07003622 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
3623
Rusty Russell2d3854a2008-11-05 13:39:10 +11003624 wfc->ret = wfc->fn(wfc->arg);
3625}
3626
3627/**
3628 * work_on_cpu - run a function in user context on a particular cpu
3629 * @cpu: the cpu to run on
3630 * @fn: the function to run
3631 * @arg: the function arg
3632 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003633 * This will return the value @fn returns.
3634 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b44003e2009-04-09 09:50:37 -06003635 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003636 */
3637long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3638{
Tejun Heoed48ece2012-09-18 12:48:43 -07003639 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003640
Tejun Heoed48ece2012-09-18 12:48:43 -07003641 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
3642 schedule_work_on(cpu, &wfc.work);
3643 flush_work(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003644 return wfc.ret;
3645}
3646EXPORT_SYMBOL_GPL(work_on_cpu);
3647#endif /* CONFIG_SMP */
3648
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003649#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303650
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003651/**
3652 * freeze_workqueues_begin - begin freezing workqueues
3653 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003654 * Start freezing workqueues. After this function returns, all freezable
3655 * workqueues will queue new works to their frozen_works list instead of
3656 * gcwq->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003657 *
3658 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08003659 * Grabs and releases workqueue_lock and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003660 */
3661void freeze_workqueues_begin(void)
3662{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003663 unsigned int cpu;
3664
3665 spin_lock(&workqueue_lock);
3666
3667 BUG_ON(workqueue_freezing);
3668 workqueue_freezing = true;
3669
Tejun Heof3421792010-07-02 10:03:51 +02003670 for_each_gcwq_cpu(cpu) {
Tejun Heo35b6bb62013-01-24 11:01:33 -08003671 struct worker_pool *pool;
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003672 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003673
Tejun Heo38db41d2013-01-24 11:01:34 -08003674 for_each_std_worker_pool(pool, cpu) {
Tejun Heoa1056302013-01-24 11:01:33 -08003675 spin_lock_irq(&pool->lock);
Tejun Heod565ed62013-01-24 11:01:33 -08003676
Tejun Heo35b6bb62013-01-24 11:01:33 -08003677 WARN_ON_ONCE(pool->flags & POOL_FREEZING);
3678 pool->flags |= POOL_FREEZING;
Tejun Heoa1056302013-01-24 11:01:33 -08003679
3680 list_for_each_entry(wq, &workqueues, list) {
3681 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3682
3683 if (cwq && cwq->pool == pool &&
3684 (wq->flags & WQ_FREEZABLE))
3685 cwq->max_active = 0;
3686 }
3687
3688 spin_unlock_irq(&pool->lock);
Tejun Heo35b6bb62013-01-24 11:01:33 -08003689 }
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003690 }
3691
3692 spin_unlock(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003694
3695/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003696 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003697 *
3698 * Check whether freezing is complete. This function must be called
3699 * between freeze_workqueues_begin() and thaw_workqueues().
3700 *
3701 * CONTEXT:
3702 * Grabs and releases workqueue_lock.
3703 *
3704 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01003705 * %true if some freezable workqueues are still busy. %false if freezing
3706 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003707 */
3708bool freeze_workqueues_busy(void)
3709{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003710 unsigned int cpu;
3711 bool busy = false;
3712
3713 spin_lock(&workqueue_lock);
3714
3715 BUG_ON(!workqueue_freezing);
3716
Tejun Heof3421792010-07-02 10:03:51 +02003717 for_each_gcwq_cpu(cpu) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003718 struct workqueue_struct *wq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003719 /*
3720 * nr_active is monotonically decreasing. It's safe
3721 * to peek without lock.
3722 */
3723 list_for_each_entry(wq, &workqueues, list) {
3724 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3725
Tejun Heo58a69cb2011-02-16 09:25:31 +01003726 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003727 continue;
3728
3729 BUG_ON(cwq->nr_active < 0);
3730 if (cwq->nr_active) {
3731 busy = true;
3732 goto out_unlock;
3733 }
3734 }
3735 }
3736out_unlock:
3737 spin_unlock(&workqueue_lock);
3738 return busy;
3739}
3740
3741/**
3742 * thaw_workqueues - thaw workqueues
3743 *
3744 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo7e116292010-06-29 10:07:13 +02003745 * frozen works are transferred to their respective gcwq worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003746 *
3747 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08003748 * Grabs and releases workqueue_lock and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003749 */
3750void thaw_workqueues(void)
3751{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003752 unsigned int cpu;
3753
3754 spin_lock(&workqueue_lock);
3755
3756 if (!workqueue_freezing)
3757 goto out_unlock;
3758
Tejun Heof3421792010-07-02 10:03:51 +02003759 for_each_gcwq_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07003760 struct worker_pool *pool;
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003761 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003762
Tejun Heo38db41d2013-01-24 11:01:34 -08003763 for_each_std_worker_pool(pool, cpu) {
Tejun Heoa1056302013-01-24 11:01:33 -08003764 spin_lock_irq(&pool->lock);
Tejun Heod565ed62013-01-24 11:01:33 -08003765
Tejun Heo35b6bb62013-01-24 11:01:33 -08003766 WARN_ON_ONCE(!(pool->flags & POOL_FREEZING));
3767 pool->flags &= ~POOL_FREEZING;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003768
Tejun Heoa1056302013-01-24 11:01:33 -08003769 list_for_each_entry(wq, &workqueues, list) {
3770 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003771
Tejun Heoa1056302013-01-24 11:01:33 -08003772 if (!cwq || cwq->pool != pool ||
3773 !(wq->flags & WQ_FREEZABLE))
3774 continue;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003775
Tejun Heoa1056302013-01-24 11:01:33 -08003776 /* restore max_active and repopulate worklist */
3777 cwq_set_max_active(cwq, wq->saved_max_active);
3778 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003779
Tejun Heo4ce62e92012-07-13 22:16:44 -07003780 wake_up_worker(pool);
Tejun Heoa1056302013-01-24 11:01:33 -08003781
3782 spin_unlock_irq(&pool->lock);
Tejun Heod565ed62013-01-24 11:01:33 -08003783 }
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003784 }
3785
3786 workqueue_freezing = false;
3787out_unlock:
3788 spin_unlock(&workqueue_lock);
3789}
3790#endif /* CONFIG_FREEZER */
3791
Suresh Siddha6ee05782010-07-30 14:57:37 -07003792static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793{
Tejun Heoc34056a2010-06-29 10:07:11 +02003794 unsigned int cpu;
3795
Tejun Heo7c3eed52013-01-24 11:01:33 -08003796 /* make sure we have enough bits for OFFQ pool ID */
3797 BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) <
3798 WORK_CPU_LAST * NR_STD_WORKER_POOLS);
Tejun Heob5490072012-08-03 10:30:46 -07003799
Tejun Heo65758202012-07-17 12:39:26 -07003800 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
Lai Jiangshana5b4e572012-09-18 09:59:23 -07003801 hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003802
3803 /* initialize gcwqs */
Tejun Heof3421792010-07-02 10:03:51 +02003804 for_each_gcwq_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07003805 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003806
Tejun Heo38db41d2013-01-24 11:01:34 -08003807 for_each_std_worker_pool(pool, cpu) {
Tejun Heod565ed62013-01-24 11:01:33 -08003808 spin_lock_init(&pool->lock);
Tejun Heoec22ca52013-01-24 11:01:33 -08003809 pool->cpu = cpu;
Tejun Heo24647572013-01-24 11:01:33 -08003810 pool->flags |= POOL_DISASSOCIATED;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003811 INIT_LIST_HEAD(&pool->worklist);
3812 INIT_LIST_HEAD(&pool->idle_list);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08003813 hash_init(pool->busy_hash);
Tejun Heoe22bee72010-06-29 10:07:14 +02003814
Tejun Heo4ce62e92012-07-13 22:16:44 -07003815 init_timer_deferrable(&pool->idle_timer);
3816 pool->idle_timer.function = idle_worker_timeout;
3817 pool->idle_timer.data = (unsigned long)pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003818
Tejun Heo4ce62e92012-07-13 22:16:44 -07003819 setup_timer(&pool->mayday_timer, gcwq_mayday_timeout,
3820 (unsigned long)pool);
3821
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07003822 mutex_init(&pool->assoc_mutex);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003823 ida_init(&pool->worker_ida);
Tejun Heo9daf9e62013-01-24 11:01:33 -08003824
3825 /* alloc pool ID */
3826 BUG_ON(worker_pool_assign_id(pool));
Tejun Heo4ce62e92012-07-13 22:16:44 -07003827 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003828 }
3829
Tejun Heoe22bee72010-06-29 10:07:14 +02003830 /* create the initial worker */
Tejun Heof3421792010-07-02 10:03:51 +02003831 for_each_online_gcwq_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07003832 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003833
Tejun Heo38db41d2013-01-24 11:01:34 -08003834 for_each_std_worker_pool(pool, cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07003835 struct worker *worker;
3836
Tejun Heo24647572013-01-24 11:01:33 -08003837 if (cpu != WORK_CPU_UNBOUND)
3838 pool->flags &= ~POOL_DISASSOCIATED;
3839
Tejun Heobc2ae0f2012-07-17 12:39:27 -07003840 worker = create_worker(pool);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003841 BUG_ON(!worker);
Tejun Heod565ed62013-01-24 11:01:33 -08003842 spin_lock_irq(&pool->lock);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003843 start_worker(worker);
Tejun Heod565ed62013-01-24 11:01:33 -08003844 spin_unlock_irq(&pool->lock);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003845 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003846 }
3847
Tejun Heod320c032010-06-29 10:07:14 +02003848 system_wq = alloc_workqueue("events", 0, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09003849 system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
Tejun Heod320c032010-06-29 10:07:14 +02003850 system_long_wq = alloc_workqueue("events_long", 0, 0);
Tejun Heof3421792010-07-02 10:03:51 +02003851 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3852 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01003853 system_freezable_wq = alloc_workqueue("events_freezable",
3854 WQ_FREEZABLE, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09003855 BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
Tejun Heoae930e02012-08-20 14:51:23 -07003856 !system_unbound_wq || !system_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07003857 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858}
Suresh Siddha6ee05782010-07-30 14:57:37 -07003859early_initcall(init_workqueues);