blob: 31d8a4586d4c719d2335555817ae528e9cc2427d [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>
Tejun Heoe22bee72010-06-29 10:07:14 +020044
45#include "workqueue_sched.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Tejun Heoc8e55f32010-06-29 10:07:12 +020047enum {
Tejun Heobc2ae0f2012-07-17 12:39:27 -070048 /*
49 * global_cwq flags
50 *
51 * A bound gcwq is either associated or disassociated with its CPU.
52 * While associated (!DISASSOCIATED), all workers are bound to the
53 * CPU and none has %WORKER_UNBOUND set and concurrency management
54 * is in effect.
55 *
56 * While DISASSOCIATED, the cpu may be offline and all workers have
57 * %WORKER_UNBOUND set and concurrency management disabled, and may
58 * be executing on any CPU. The gcwq behaves as an unbound one.
59 *
60 * Note that DISASSOCIATED can be flipped only while holding
61 * managership of all pools on the gcwq to avoid changing binding
62 * state while create_worker() is in progress.
63 */
Tejun Heo11ebea52012-07-12 14:46:37 -070064 GCWQ_DISASSOCIATED = 1 << 0, /* cpu can't serve workers */
65 GCWQ_FREEZING = 1 << 1, /* freeze in progress */
66
67 /* pool flags */
68 POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
Lai Jiangshan552a37e2012-09-10 10:03:33 -070069 POOL_MANAGING_WORKERS = 1 << 1, /* managing workers */
Tejun Heodb7bccf2010-06-29 10:07:12 +020070
Tejun Heoc8e55f32010-06-29 10:07:12 +020071 /* worker flags */
72 WORKER_STARTED = 1 << 0, /* started */
73 WORKER_DIE = 1 << 1, /* die die die */
74 WORKER_IDLE = 1 << 2, /* is idle */
Tejun Heoe22bee72010-06-29 10:07:14 +020075 WORKER_PREP = 1 << 3, /* preparing to run works */
Tejun Heoe22bee72010-06-29 10:07:14 +020076 WORKER_REBIND = 1 << 5, /* mom is home, come back */
Tejun Heofb0e7be2010-06-29 10:07:15 +020077 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
Tejun Heof3421792010-07-02 10:03:51 +020078 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
Tejun Heoe22bee72010-06-29 10:07:14 +020079
Tejun Heo403c8212012-07-17 12:39:27 -070080 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_REBIND | WORKER_UNBOUND |
81 WORKER_CPU_INTENSIVE,
Tejun Heodb7bccf2010-06-29 10:07:12 +020082
Tejun Heo32704762012-07-13 22:16:45 -070083 NR_WORKER_POOLS = 2, /* # worker pools per gcwq */
Tejun Heo4ce62e92012-07-13 22:16:44 -070084
Tejun Heoc8e55f32010-06-29 10:07:12 +020085 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
86 BUSY_WORKER_HASH_SIZE = 1 << BUSY_WORKER_HASH_ORDER,
87 BUSY_WORKER_HASH_MASK = BUSY_WORKER_HASH_SIZE - 1,
Tejun Heodb7bccf2010-06-29 10:07:12 +020088
Tejun Heoe22bee72010-06-29 10:07:14 +020089 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
90 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
91
Tejun Heo3233cdb2011-02-16 18:10:19 +010092 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
93 /* call for help after 10ms
94 (min two ticks) */
Tejun Heoe22bee72010-06-29 10:07:14 +020095 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
96 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heoe22bee72010-06-29 10:07:14 +020097
98 /*
99 * Rescue workers are used only on emergencies and shared by
100 * all cpus. Give -20.
101 */
102 RESCUER_NICE_LEVEL = -20,
Tejun Heo32704762012-07-13 22:16:45 -0700103 HIGHPRI_NICE_LEVEL = -20,
Tejun Heoc8e55f32010-06-29 10:07:12 +0200104};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106/*
Tejun Heo4690c4a2010-06-29 10:07:10 +0200107 * Structure fields follow one of the following exclusion rules.
108 *
Tejun Heoe41e7042010-08-24 14:22:47 +0200109 * I: Modifiable by initialization/destruction paths and read-only for
110 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200111 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200112 * P: Preemption protected. Disabling preemption is enough and should
113 * only be modified and accessed from the local cpu.
114 *
Tejun Heo8b03ae32010-06-29 10:07:12 +0200115 * L: gcwq->lock protected. Access with gcwq->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200116 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200117 * X: During normal operation, modification requires gcwq->lock and
118 * should be done only from local cpu. Either disabling preemption
119 * on local cpu or grabbing gcwq->lock is enough for read access.
Tejun Heof3421792010-07-02 10:03:51 +0200120 * If GCWQ_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200121 *
Tejun Heo73f53c42010-06-29 10:07:11 +0200122 * F: wq->flush_mutex protected.
123 *
Tejun Heo4690c4a2010-06-29 10:07:10 +0200124 * W: workqueue_lock protected.
125 */
126
Tejun Heo8b03ae32010-06-29 10:07:12 +0200127struct global_cwq;
Tejun Heobd7bdd42012-07-12 14:46:37 -0700128struct worker_pool;
Tejun Heo25511a42012-07-17 12:39:27 -0700129struct idle_rebind;
Tejun Heoc34056a2010-06-29 10:07:11 +0200130
Tejun Heoe22bee72010-06-29 10:07:14 +0200131/*
132 * The poor guys doing the actual heavy lifting. All on-duty workers
133 * are either serving the manager role, on idle list or on busy hash.
134 */
Tejun Heoc34056a2010-06-29 10:07:11 +0200135struct worker {
Tejun Heoc8e55f32010-06-29 10:07:12 +0200136 /* on idle list while idle, on busy hash table while busy */
137 union {
138 struct list_head entry; /* L: while idle */
139 struct hlist_node hentry; /* L: while busy */
140 };
141
Tejun Heoc34056a2010-06-29 10:07:11 +0200142 struct work_struct *current_work; /* L: work being processed */
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200143 struct cpu_workqueue_struct *current_cwq; /* L: current_work's cwq */
Tejun Heoaffee4b2010-06-29 10:07:12 +0200144 struct list_head scheduled; /* L: scheduled works */
Tejun Heoc34056a2010-06-29 10:07:11 +0200145 struct task_struct *task; /* I: worker task */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700146 struct worker_pool *pool; /* I: the associated pool */
Tejun Heoe22bee72010-06-29 10:07:14 +0200147 /* 64 bytes boundary on 64bit, 32 on 32bit */
148 unsigned long last_active; /* L: last active timestamp */
149 unsigned int flags; /* X: flags */
Tejun Heoc34056a2010-06-29 10:07:11 +0200150 int id; /* I: worker id */
Tejun Heo25511a42012-07-17 12:39:27 -0700151
152 /* for rebinding worker to CPU */
153 struct idle_rebind *idle_rebind; /* L: for idle worker */
154 struct work_struct rebind_work; /* L: for busy worker */
Tejun Heoc34056a2010-06-29 10:07:11 +0200155};
156
Tejun Heobd7bdd42012-07-12 14:46:37 -0700157struct worker_pool {
158 struct global_cwq *gcwq; /* I: the owning gcwq */
Tejun Heo11ebea52012-07-12 14:46:37 -0700159 unsigned int flags; /* X: flags */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700160
161 struct list_head worklist; /* L: list of pending works */
162 int nr_workers; /* L: total number of workers */
163 int nr_idle; /* L: currently idle ones */
164
165 struct list_head idle_list; /* X: list of idle workers */
166 struct timer_list idle_timer; /* L: worker idle timeout */
167 struct timer_list mayday_timer; /* L: SOS timer for workers */
168
Tejun Heo60373152012-07-17 12:39:27 -0700169 struct mutex manager_mutex; /* mutex manager should hold */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700170 struct ida worker_ida; /* L: for worker IDs */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700171};
172
Tejun Heo4690c4a2010-06-29 10:07:10 +0200173/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200174 * Global per-cpu workqueue. There's one and only one for each cpu
175 * and all works are queued and processed here regardless of their
176 * target workqueues.
Tejun Heo8b03ae32010-06-29 10:07:12 +0200177 */
178struct global_cwq {
179 spinlock_t lock; /* the gcwq lock */
180 unsigned int cpu; /* I: the associated cpu */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200181 unsigned int flags; /* L: GCWQ_* flags */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200182
Tejun Heobd7bdd42012-07-12 14:46:37 -0700183 /* workers are chained either in busy_hash or pool idle_list */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200184 struct hlist_head busy_hash[BUSY_WORKER_HASH_SIZE];
185 /* L: hash of busy workers */
186
Joonsoo Kim330dad52012-08-15 23:25:36 +0900187 struct worker_pool pools[NR_WORKER_POOLS];
188 /* normal and highpri pools */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200189
Tejun Heo25511a42012-07-17 12:39:27 -0700190 wait_queue_head_t rebind_hold; /* rebind hold wait */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200191} ____cacheline_aligned_in_smp;
192
193/*
Tejun Heo502ca9d2010-06-29 10:07:13 +0200194 * The per-CPU workqueue. The lower WORK_STRUCT_FLAG_BITS of
Tejun Heo0f900042010-06-29 10:07:11 +0200195 * work_struct->data are used for flags and thus cwqs need to be
196 * aligned at two's power of the number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 */
198struct cpu_workqueue_struct {
Tejun Heobd7bdd42012-07-12 14:46:37 -0700199 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200200 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200201 int work_color; /* L: current color */
202 int flush_color; /* L: flushing color */
203 int nr_in_flight[WORK_NR_COLORS];
204 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200205 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200206 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200207 struct list_head delayed_works; /* L: delayed works */
Tejun Heo0f900042010-06-29 10:07:11 +0200208};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200211 * Structure used to wait for workqueue flush.
212 */
213struct wq_flusher {
214 struct list_head list; /* F: list of flushers */
215 int flush_color; /* F: flush color waiting for */
216 struct completion done; /* flush completion */
217};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Tejun Heo73f53c42010-06-29 10:07:11 +0200219/*
Tejun Heof2e005a2010-07-20 15:59:09 +0200220 * All cpumasks are assumed to be always set on UP and thus can't be
221 * used to determine whether there's something to be done.
222 */
223#ifdef CONFIG_SMP
224typedef cpumask_var_t mayday_mask_t;
225#define mayday_test_and_set_cpu(cpu, mask) \
226 cpumask_test_and_set_cpu((cpu), (mask))
227#define mayday_clear_cpu(cpu, mask) cpumask_clear_cpu((cpu), (mask))
228#define for_each_mayday_cpu(cpu, mask) for_each_cpu((cpu), (mask))
Tejun Heo9c375472010-08-31 11:18:34 +0200229#define alloc_mayday_mask(maskp, gfp) zalloc_cpumask_var((maskp), (gfp))
Tejun Heof2e005a2010-07-20 15:59:09 +0200230#define free_mayday_mask(mask) free_cpumask_var((mask))
231#else
232typedef unsigned long mayday_mask_t;
233#define mayday_test_and_set_cpu(cpu, mask) test_and_set_bit(0, &(mask))
234#define mayday_clear_cpu(cpu, mask) clear_bit(0, &(mask))
235#define for_each_mayday_cpu(cpu, mask) if ((cpu) = 0, (mask))
236#define alloc_mayday_mask(maskp, gfp) true
237#define free_mayday_mask(mask) do { } while (0)
238#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240/*
241 * The externally visible workqueue abstraction is an array of
242 * per-CPU workqueues:
243 */
244struct workqueue_struct {
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200245 unsigned int flags; /* W: WQ_* flags */
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200246 union {
247 struct cpu_workqueue_struct __percpu *pcpu;
248 struct cpu_workqueue_struct *single;
249 unsigned long v;
250 } cpu_wq; /* I: cwq's */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200251 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200252
253 struct mutex flush_mutex; /* protects wq flushing */
254 int work_color; /* F: current work color */
255 int flush_color; /* F: current flush color */
256 atomic_t nr_cwqs_to_flush; /* flush in progress */
257 struct wq_flusher *first_flusher; /* F: first flusher */
258 struct list_head flusher_queue; /* F: flush waiters */
259 struct list_head flusher_overflow; /* F: flush overflow list */
260
Tejun Heof2e005a2010-07-20 15:59:09 +0200261 mayday_mask_t mayday_mask; /* cpus requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200262 struct worker *rescuer; /* I: rescue worker */
263
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200264 int nr_drainers; /* W: drain in progress */
Tejun Heodcd989c2010-06-29 10:07:14 +0200265 int saved_max_active; /* W: saved cwq max_active */
Johannes Berg4e6045f2007-10-18 23:39:55 -0700266#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200267 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700268#endif
Tejun Heob196be82012-01-10 15:11:35 -0800269 char name[]; /* I: workqueue name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270};
271
Tejun Heod320c032010-06-29 10:07:14 +0200272struct workqueue_struct *system_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200273EXPORT_SYMBOL_GPL(system_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300274struct workqueue_struct *system_highpri_wq __read_mostly;
Joonsoo Kim1aabe902012-08-15 23:25:39 +0900275EXPORT_SYMBOL_GPL(system_highpri_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300276struct workqueue_struct *system_long_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200277EXPORT_SYMBOL_GPL(system_long_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300278struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200279EXPORT_SYMBOL_GPL(system_unbound_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300280struct workqueue_struct *system_freezable_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100281EXPORT_SYMBOL_GPL(system_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200282
Tejun Heo97bd2342010-10-05 10:41:14 +0200283#define CREATE_TRACE_POINTS
284#include <trace/events/workqueue.h>
285
Tejun Heo4ce62e92012-07-13 22:16:44 -0700286#define for_each_worker_pool(pool, gcwq) \
Tejun Heo32704762012-07-13 22:16:45 -0700287 for ((pool) = &(gcwq)->pools[0]; \
288 (pool) < &(gcwq)->pools[NR_WORKER_POOLS]; (pool)++)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700289
Tejun Heodb7bccf2010-06-29 10:07:12 +0200290#define for_each_busy_worker(worker, i, pos, gcwq) \
291 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \
292 hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry)
293
Tejun Heof3421792010-07-02 10:03:51 +0200294static inline int __next_gcwq_cpu(int cpu, const struct cpumask *mask,
295 unsigned int sw)
296{
297 if (cpu < nr_cpu_ids) {
298 if (sw & 1) {
299 cpu = cpumask_next(cpu, mask);
300 if (cpu < nr_cpu_ids)
301 return cpu;
302 }
303 if (sw & 2)
304 return WORK_CPU_UNBOUND;
305 }
306 return WORK_CPU_NONE;
307}
308
309static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
310 struct workqueue_struct *wq)
311{
312 return __next_gcwq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2);
313}
314
Tejun Heo09884952010-08-01 11:50:12 +0200315/*
316 * CPU iterators
317 *
318 * An extra gcwq is defined for an invalid cpu number
319 * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any
320 * specific CPU. The following iterators are similar to
321 * for_each_*_cpu() iterators but also considers the unbound gcwq.
322 *
323 * for_each_gcwq_cpu() : possible CPUs + WORK_CPU_UNBOUND
324 * for_each_online_gcwq_cpu() : online CPUs + WORK_CPU_UNBOUND
325 * for_each_cwq_cpu() : possible CPUs for bound workqueues,
326 * WORK_CPU_UNBOUND for unbound workqueues
327 */
Tejun Heof3421792010-07-02 10:03:51 +0200328#define for_each_gcwq_cpu(cpu) \
329 for ((cpu) = __next_gcwq_cpu(-1, cpu_possible_mask, 3); \
330 (cpu) < WORK_CPU_NONE; \
331 (cpu) = __next_gcwq_cpu((cpu), cpu_possible_mask, 3))
332
333#define for_each_online_gcwq_cpu(cpu) \
334 for ((cpu) = __next_gcwq_cpu(-1, cpu_online_mask, 3); \
335 (cpu) < WORK_CPU_NONE; \
336 (cpu) = __next_gcwq_cpu((cpu), cpu_online_mask, 3))
337
338#define for_each_cwq_cpu(cpu, wq) \
339 for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, (wq)); \
340 (cpu) < WORK_CPU_NONE; \
341 (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq)))
342
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900343#ifdef CONFIG_DEBUG_OBJECTS_WORK
344
345static struct debug_obj_descr work_debug_descr;
346
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100347static void *work_debug_hint(void *addr)
348{
349 return ((struct work_struct *) addr)->func;
350}
351
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900352/*
353 * fixup_init is called when:
354 * - an active object is initialized
355 */
356static int work_fixup_init(void *addr, enum debug_obj_state state)
357{
358 struct work_struct *work = addr;
359
360 switch (state) {
361 case ODEBUG_STATE_ACTIVE:
362 cancel_work_sync(work);
363 debug_object_init(work, &work_debug_descr);
364 return 1;
365 default:
366 return 0;
367 }
368}
369
370/*
371 * fixup_activate is called when:
372 * - an active object is activated
373 * - an unknown object is activated (might be a statically initialized object)
374 */
375static int work_fixup_activate(void *addr, enum debug_obj_state state)
376{
377 struct work_struct *work = addr;
378
379 switch (state) {
380
381 case ODEBUG_STATE_NOTAVAILABLE:
382 /*
383 * This is not really a fixup. The work struct was
384 * statically initialized. We just make sure that it
385 * is tracked in the object tracker.
386 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200387 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900388 debug_object_init(work, &work_debug_descr);
389 debug_object_activate(work, &work_debug_descr);
390 return 0;
391 }
392 WARN_ON_ONCE(1);
393 return 0;
394
395 case ODEBUG_STATE_ACTIVE:
396 WARN_ON(1);
397
398 default:
399 return 0;
400 }
401}
402
403/*
404 * fixup_free is called when:
405 * - an active object is freed
406 */
407static int work_fixup_free(void *addr, enum debug_obj_state state)
408{
409 struct work_struct *work = addr;
410
411 switch (state) {
412 case ODEBUG_STATE_ACTIVE:
413 cancel_work_sync(work);
414 debug_object_free(work, &work_debug_descr);
415 return 1;
416 default:
417 return 0;
418 }
419}
420
421static struct debug_obj_descr work_debug_descr = {
422 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100423 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900424 .fixup_init = work_fixup_init,
425 .fixup_activate = work_fixup_activate,
426 .fixup_free = work_fixup_free,
427};
428
429static inline void debug_work_activate(struct work_struct *work)
430{
431 debug_object_activate(work, &work_debug_descr);
432}
433
434static inline void debug_work_deactivate(struct work_struct *work)
435{
436 debug_object_deactivate(work, &work_debug_descr);
437}
438
439void __init_work(struct work_struct *work, int onstack)
440{
441 if (onstack)
442 debug_object_init_on_stack(work, &work_debug_descr);
443 else
444 debug_object_init(work, &work_debug_descr);
445}
446EXPORT_SYMBOL_GPL(__init_work);
447
448void destroy_work_on_stack(struct work_struct *work)
449{
450 debug_object_free(work, &work_debug_descr);
451}
452EXPORT_SYMBOL_GPL(destroy_work_on_stack);
453
454#else
455static inline void debug_work_activate(struct work_struct *work) { }
456static inline void debug_work_deactivate(struct work_struct *work) { }
457#endif
458
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100459/* Serializes the accesses to the list of workqueues. */
460static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461static LIST_HEAD(workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200462static bool workqueue_freezing; /* W: have wqs started freezing? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Oleg Nesterov14441962007-05-23 13:57:57 -0700464/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200465 * The almighty global cpu workqueues. nr_running is the only field
466 * which is expected to be used frequently by other cpus via
467 * try_to_wake_up(). Put it in a separate cacheline.
Oleg Nesterov14441962007-05-23 13:57:57 -0700468 */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200469static DEFINE_PER_CPU(struct global_cwq, global_cwq);
Tejun Heo4ce62e92012-07-13 22:16:44 -0700470static DEFINE_PER_CPU_SHARED_ALIGNED(atomic_t, pool_nr_running[NR_WORKER_POOLS]);
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800471
Tejun Heof3421792010-07-02 10:03:51 +0200472/*
473 * Global cpu workqueue and nr_running counter for unbound gcwq. The
474 * gcwq is always online, has GCWQ_DISASSOCIATED set, and all its
475 * workers have WORKER_UNBOUND set.
476 */
477static struct global_cwq unbound_global_cwq;
Tejun Heo4ce62e92012-07-13 22:16:44 -0700478static atomic_t unbound_pool_nr_running[NR_WORKER_POOLS] = {
479 [0 ... NR_WORKER_POOLS - 1] = ATOMIC_INIT(0), /* always 0 */
480};
Tejun Heof3421792010-07-02 10:03:51 +0200481
Tejun Heoc34056a2010-06-29 10:07:11 +0200482static int worker_thread(void *__worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Tejun Heo32704762012-07-13 22:16:45 -0700484static int worker_pool_pri(struct worker_pool *pool)
485{
486 return pool - pool->gcwq->pools;
487}
488
Tejun Heo8b03ae32010-06-29 10:07:12 +0200489static struct global_cwq *get_gcwq(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Tejun Heof3421792010-07-02 10:03:51 +0200491 if (cpu != WORK_CPU_UNBOUND)
492 return &per_cpu(global_cwq, cpu);
493 else
494 return &unbound_global_cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
Tejun Heo63d95a92012-07-12 14:46:37 -0700497static atomic_t *get_pool_nr_running(struct worker_pool *pool)
Oleg Nesterovb1f4ec172007-05-09 02:34:12 -0700498{
Tejun Heo63d95a92012-07-12 14:46:37 -0700499 int cpu = pool->gcwq->cpu;
Tejun Heo32704762012-07-13 22:16:45 -0700500 int idx = worker_pool_pri(pool);
Tejun Heo63d95a92012-07-12 14:46:37 -0700501
Tejun Heof3421792010-07-02 10:03:51 +0200502 if (cpu != WORK_CPU_UNBOUND)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700503 return &per_cpu(pool_nr_running, cpu)[idx];
Tejun Heof3421792010-07-02 10:03:51 +0200504 else
Tejun Heo4ce62e92012-07-13 22:16:44 -0700505 return &unbound_pool_nr_running[idx];
Oleg Nesterovb1f4ec172007-05-09 02:34:12 -0700506}
507
Tejun Heo4690c4a2010-06-29 10:07:10 +0200508static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
509 struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700510{
Tejun Heof3421792010-07-02 10:03:51 +0200511 if (!(wq->flags & WQ_UNBOUND)) {
Lai Jiangshane06ffa12012-03-09 18:03:20 +0800512 if (likely(cpu < nr_cpu_ids))
Tejun Heof3421792010-07-02 10:03:51 +0200513 return per_cpu_ptr(wq->cpu_wq.pcpu, cpu);
Tejun Heof3421792010-07-02 10:03:51 +0200514 } else if (likely(cpu == WORK_CPU_UNBOUND))
515 return wq->cpu_wq.single;
516 return NULL;
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700517}
518
Tejun Heo73f53c42010-06-29 10:07:11 +0200519static unsigned int work_color_to_flags(int color)
520{
521 return color << WORK_STRUCT_COLOR_SHIFT;
522}
523
524static int get_work_color(struct work_struct *work)
525{
526 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
527 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
528}
529
530static int work_next_color(int color)
531{
532 return (color + 1) % WORK_NR_COLORS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
David Howells4594bf12006-12-07 11:33:26 +0000535/*
Tejun Heob5490072012-08-03 10:30:46 -0700536 * While queued, %WORK_STRUCT_CWQ is set and non flag bits of a work's data
537 * contain the pointer to the queued cwq. Once execution starts, the flag
538 * is cleared and the high bits contain OFFQ flags and CPU number.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200539 *
Tejun Heobbb68df2012-08-03 10:30:46 -0700540 * set_work_cwq(), set_work_cpu_and_clear_pending(), mark_work_canceling()
541 * and clear_work_data() can be used to set the cwq, cpu or clear
542 * work->data. These functions should only be called while the work is
543 * owned - ie. while the PENDING bit is set.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200544 *
Tejun Heobbb68df2012-08-03 10:30:46 -0700545 * get_work_[g]cwq() can be used to obtain the gcwq or cwq corresponding to
546 * a work. gcwq is available once the work has been queued anywhere after
547 * initialization until it is sync canceled. cwq is available only while
548 * the work item is queued.
549 *
550 * %WORK_OFFQ_CANCELING is used to mark a work item which is being
551 * canceled. While being canceled, a work item may have its PENDING set
552 * but stay off timer and worklist for arbitrarily long and nobody should
553 * try to steal the PENDING bit.
David Howells4594bf12006-12-07 11:33:26 +0000554 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200555static inline void set_work_data(struct work_struct *work, unsigned long data,
556 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000557{
David Howells4594bf12006-12-07 11:33:26 +0000558 BUG_ON(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200559 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000560}
David Howells365970a2006-11-22 14:54:49 +0000561
Tejun Heo7a22ad72010-06-29 10:07:13 +0200562static void set_work_cwq(struct work_struct *work,
563 struct cpu_workqueue_struct *cwq,
564 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200565{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200566 set_work_data(work, (unsigned long)cwq,
Tejun Heoe1201532010-07-22 14:14:25 +0200567 WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200568}
569
Tejun Heo8930cab2012-08-03 10:30:45 -0700570static void set_work_cpu_and_clear_pending(struct work_struct *work,
571 unsigned int cpu)
David Howells365970a2006-11-22 14:54:49 +0000572{
Tejun Heo23657bb2012-08-13 17:08:19 -0700573 /*
574 * The following wmb is paired with the implied mb in
575 * test_and_set_bit(PENDING) and ensures all updates to @work made
576 * here are visible to and precede any updates by the next PENDING
577 * owner.
578 */
579 smp_wmb();
Tejun Heob5490072012-08-03 10:30:46 -0700580 set_work_data(work, (unsigned long)cpu << WORK_OFFQ_CPU_SHIFT, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200581}
582
583static void clear_work_data(struct work_struct *work)
584{
Tejun Heo23657bb2012-08-13 17:08:19 -0700585 smp_wmb(); /* see set_work_cpu_and_clear_pending() */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200586 set_work_data(work, WORK_STRUCT_NO_CPU, 0);
587}
588
Tejun Heo7a22ad72010-06-29 10:07:13 +0200589static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work)
590{
Tejun Heoe1201532010-07-22 14:14:25 +0200591 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200592
Tejun Heoe1201532010-07-22 14:14:25 +0200593 if (data & WORK_STRUCT_CWQ)
594 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
595 else
596 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200597}
598
599static struct global_cwq *get_work_gcwq(struct work_struct *work)
600{
Tejun Heoe1201532010-07-22 14:14:25 +0200601 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200602 unsigned int cpu;
603
Tejun Heoe1201532010-07-22 14:14:25 +0200604 if (data & WORK_STRUCT_CWQ)
605 return ((struct cpu_workqueue_struct *)
Tejun Heobd7bdd42012-07-12 14:46:37 -0700606 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->gcwq;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200607
Tejun Heob5490072012-08-03 10:30:46 -0700608 cpu = data >> WORK_OFFQ_CPU_SHIFT;
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200609 if (cpu == WORK_CPU_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200610 return NULL;
611
Tejun Heof3421792010-07-02 10:03:51 +0200612 BUG_ON(cpu >= nr_cpu_ids && cpu != WORK_CPU_UNBOUND);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200613 return get_gcwq(cpu);
David Howells365970a2006-11-22 14:54:49 +0000614}
615
Tejun Heobbb68df2012-08-03 10:30:46 -0700616static void mark_work_canceling(struct work_struct *work)
617{
618 struct global_cwq *gcwq = get_work_gcwq(work);
619 unsigned long cpu = gcwq ? gcwq->cpu : WORK_CPU_NONE;
620
621 set_work_data(work, (cpu << WORK_OFFQ_CPU_SHIFT) | WORK_OFFQ_CANCELING,
622 WORK_STRUCT_PENDING);
623}
624
625static bool work_is_canceling(struct work_struct *work)
626{
627 unsigned long data = atomic_long_read(&work->data);
628
629 return !(data & WORK_STRUCT_CWQ) && (data & WORK_OFFQ_CANCELING);
630}
631
David Howells365970a2006-11-22 14:54:49 +0000632/*
Tejun Heo32704762012-07-13 22:16:45 -0700633 * Policy functions. These define the policies on how the global worker
634 * pools are managed. Unless noted otherwise, these functions assume that
635 * they're being called with gcwq->lock held.
David Howells365970a2006-11-22 14:54:49 +0000636 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200637
Tejun Heo63d95a92012-07-12 14:46:37 -0700638static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000639{
Tejun Heo32704762012-07-13 22:16:45 -0700640 return !atomic_read(get_pool_nr_running(pool));
David Howells365970a2006-11-22 14:54:49 +0000641}
642
Tejun Heoe22bee72010-06-29 10:07:14 +0200643/*
644 * Need to wake up a worker? Called from anything but currently
645 * running workers.
Tejun Heo974271c42012-07-12 14:46:37 -0700646 *
647 * Note that, because unbound workers never contribute to nr_running, this
648 * function will always return %true for unbound gcwq as long as the
649 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200650 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700651static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000652{
Tejun Heo63d95a92012-07-12 14:46:37 -0700653 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000654}
655
Tejun Heoe22bee72010-06-29 10:07:14 +0200656/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700657static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200658{
Tejun Heo63d95a92012-07-12 14:46:37 -0700659 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200660}
661
662/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700663static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200664{
Tejun Heo63d95a92012-07-12 14:46:37 -0700665 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200666
Tejun Heo32704762012-07-13 22:16:45 -0700667 return !list_empty(&pool->worklist) && atomic_read(nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200668}
669
670/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700671static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200672{
Tejun Heo63d95a92012-07-12 14:46:37 -0700673 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200674}
675
676/* Do I need to be the manager? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700677static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200678{
Tejun Heo63d95a92012-07-12 14:46:37 -0700679 return need_to_create_worker(pool) ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700680 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200681}
682
683/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700684static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200685{
Lai Jiangshan552a37e2012-09-10 10:03:33 -0700686 bool managing = pool->flags & POOL_MANAGING_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -0700687 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
688 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200689
690 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
691}
692
693/*
694 * Wake up functions.
695 */
696
Tejun Heo7e116292010-06-29 10:07:13 +0200697/* Return the first worker. Safe with preemption disabled */
Tejun Heo63d95a92012-07-12 14:46:37 -0700698static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200699{
Tejun Heo63d95a92012-07-12 14:46:37 -0700700 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200701 return NULL;
702
Tejun Heo63d95a92012-07-12 14:46:37 -0700703 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200704}
705
706/**
707 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700708 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200709 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700710 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200711 *
712 * CONTEXT:
713 * spin_lock_irq(gcwq->lock).
714 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700715static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200716{
Tejun Heo63d95a92012-07-12 14:46:37 -0700717 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200718
719 if (likely(worker))
720 wake_up_process(worker->task);
721}
722
Tejun Heo4690c4a2010-06-29 10:07:10 +0200723/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200724 * wq_worker_waking_up - a worker is waking up
725 * @task: task waking up
726 * @cpu: CPU @task is waking up to
727 *
728 * This function is called during try_to_wake_up() when a worker is
729 * being awoken.
730 *
731 * CONTEXT:
732 * spin_lock_irq(rq->lock)
733 */
734void wq_worker_waking_up(struct task_struct *task, unsigned int cpu)
735{
736 struct worker *worker = kthread_data(task);
737
Steven Rostedt2d646722010-12-03 23:12:33 -0500738 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heo63d95a92012-07-12 14:46:37 -0700739 atomic_inc(get_pool_nr_running(worker->pool));
Tejun Heoe22bee72010-06-29 10:07:14 +0200740}
741
742/**
743 * wq_worker_sleeping - a worker is going to sleep
744 * @task: task going to sleep
745 * @cpu: CPU in question, must be the current CPU number
746 *
747 * This function is called during schedule() when a busy worker is
748 * going to sleep. Worker on the same cpu can be woken up by
749 * returning pointer to its task.
750 *
751 * CONTEXT:
752 * spin_lock_irq(rq->lock)
753 *
754 * RETURNS:
755 * Worker task on @cpu to wake up, %NULL if none.
756 */
757struct task_struct *wq_worker_sleeping(struct task_struct *task,
758 unsigned int cpu)
759{
760 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heobd7bdd42012-07-12 14:46:37 -0700761 struct worker_pool *pool = worker->pool;
Tejun Heo63d95a92012-07-12 14:46:37 -0700762 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200763
Steven Rostedt2d646722010-12-03 23:12:33 -0500764 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200765 return NULL;
766
767 /* this can only happen on the local cpu */
768 BUG_ON(cpu != raw_smp_processor_id());
769
770 /*
771 * The counterpart of the following dec_and_test, implied mb,
772 * worklist not empty test sequence is in insert_work().
773 * Please read comment there.
774 *
Tejun Heo628c78e2012-07-17 12:39:27 -0700775 * NOT_RUNNING is clear. This means that we're bound to and
776 * running on the local cpu w/ rq lock held and preemption
777 * disabled, which in turn means that none else could be
778 * manipulating idle_list, so dereferencing idle_list without gcwq
779 * lock is safe.
Tejun Heoe22bee72010-06-29 10:07:14 +0200780 */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700781 if (atomic_dec_and_test(nr_running) && !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700782 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200783 return to_wakeup ? to_wakeup->task : NULL;
784}
785
786/**
787 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200788 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200789 * @flags: flags to set
790 * @wakeup: wakeup an idle worker if necessary
791 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200792 * Set @flags in @worker->flags and adjust nr_running accordingly. If
793 * nr_running becomes zero and @wakeup is %true, an idle worker is
794 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200795 *
Tejun Heocb444762010-07-02 10:03:50 +0200796 * CONTEXT:
797 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200798 */
799static inline void worker_set_flags(struct worker *worker, unsigned int flags,
800 bool wakeup)
801{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700802 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200803
Tejun Heocb444762010-07-02 10:03:50 +0200804 WARN_ON_ONCE(worker->task != current);
805
Tejun Heoe22bee72010-06-29 10:07:14 +0200806 /*
807 * If transitioning into NOT_RUNNING, adjust nr_running and
808 * wake up an idle worker as necessary if requested by
809 * @wakeup.
810 */
811 if ((flags & WORKER_NOT_RUNNING) &&
812 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heo63d95a92012-07-12 14:46:37 -0700813 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200814
815 if (wakeup) {
816 if (atomic_dec_and_test(nr_running) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -0700817 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700818 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200819 } else
820 atomic_dec(nr_running);
821 }
822
Tejun Heod302f012010-06-29 10:07:13 +0200823 worker->flags |= flags;
824}
825
826/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200827 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200828 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200829 * @flags: flags to clear
830 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200831 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200832 *
Tejun Heocb444762010-07-02 10:03:50 +0200833 * CONTEXT:
834 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200835 */
836static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
837{
Tejun Heo63d95a92012-07-12 14:46:37 -0700838 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200839 unsigned int oflags = worker->flags;
840
Tejun Heocb444762010-07-02 10:03:50 +0200841 WARN_ON_ONCE(worker->task != current);
842
Tejun Heod302f012010-06-29 10:07:13 +0200843 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200844
Tejun Heo42c025f2011-01-11 15:58:49 +0100845 /*
846 * If transitioning out of NOT_RUNNING, increment nr_running. Note
847 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
848 * of multiple flags, not a single flag.
849 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200850 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
851 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heo63d95a92012-07-12 14:46:37 -0700852 atomic_inc(get_pool_nr_running(pool));
Tejun Heod302f012010-06-29 10:07:13 +0200853}
854
855/**
Tejun Heoc8e55f32010-06-29 10:07:12 +0200856 * busy_worker_head - return the busy hash head for a work
857 * @gcwq: gcwq of interest
858 * @work: work to be hashed
859 *
860 * Return hash head of @gcwq for @work.
861 *
862 * CONTEXT:
863 * spin_lock_irq(gcwq->lock).
864 *
865 * RETURNS:
866 * Pointer to the hash head.
867 */
868static struct hlist_head *busy_worker_head(struct global_cwq *gcwq,
869 struct work_struct *work)
870{
871 const int base_shift = ilog2(sizeof(struct work_struct));
872 unsigned long v = (unsigned long)work;
873
874 /* simple shift and fold hash, do we need something better? */
875 v >>= base_shift;
876 v += v >> BUSY_WORKER_HASH_ORDER;
877 v &= BUSY_WORKER_HASH_MASK;
878
879 return &gcwq->busy_hash[v];
880}
881
882/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200883 * __find_worker_executing_work - find worker which is executing a work
884 * @gcwq: gcwq of interest
885 * @bwh: hash head as returned by busy_worker_head()
886 * @work: work to find worker for
887 *
888 * Find a worker which is executing @work on @gcwq. @bwh should be
889 * the hash head obtained by calling busy_worker_head() with the same
890 * work.
891 *
892 * CONTEXT:
893 * spin_lock_irq(gcwq->lock).
894 *
895 * RETURNS:
896 * Pointer to worker which is executing @work if found, NULL
897 * otherwise.
898 */
899static struct worker *__find_worker_executing_work(struct global_cwq *gcwq,
900 struct hlist_head *bwh,
901 struct work_struct *work)
902{
903 struct worker *worker;
904 struct hlist_node *tmp;
905
906 hlist_for_each_entry(worker, tmp, bwh, hentry)
907 if (worker->current_work == work)
908 return worker;
909 return NULL;
910}
911
912/**
913 * find_worker_executing_work - find worker which is executing a work
914 * @gcwq: gcwq of interest
915 * @work: work to find worker for
916 *
917 * Find a worker which is executing @work on @gcwq. This function is
918 * identical to __find_worker_executing_work() except that this
919 * function calculates @bwh itself.
920 *
921 * CONTEXT:
922 * spin_lock_irq(gcwq->lock).
923 *
924 * RETURNS:
925 * Pointer to worker which is executing @work if found, NULL
926 * otherwise.
927 */
928static struct worker *find_worker_executing_work(struct global_cwq *gcwq,
929 struct work_struct *work)
930{
931 return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work),
932 work);
933}
934
935/**
Tejun Heobf4ede02012-08-03 10:30:46 -0700936 * move_linked_works - move linked works to a list
937 * @work: start of series of works to be scheduled
938 * @head: target list to append @work to
939 * @nextp: out paramter for nested worklist walking
940 *
941 * Schedule linked works starting from @work to @head. Work series to
942 * be scheduled starts at @work and includes any consecutive work with
943 * WORK_STRUCT_LINKED set in its predecessor.
944 *
945 * If @nextp is not NULL, it's updated to point to the next work of
946 * the last scheduled work. This allows move_linked_works() to be
947 * nested inside outer list_for_each_entry_safe().
948 *
949 * CONTEXT:
950 * spin_lock_irq(gcwq->lock).
951 */
952static void move_linked_works(struct work_struct *work, struct list_head *head,
953 struct work_struct **nextp)
954{
955 struct work_struct *n;
956
957 /*
958 * Linked worklist will always end before the end of the list,
959 * use NULL for list head.
960 */
961 list_for_each_entry_safe_from(work, n, NULL, entry) {
962 list_move_tail(&work->entry, head);
963 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
964 break;
965 }
966
967 /*
968 * If we're already inside safe list traversal and have moved
969 * multiple works to the scheduled queue, the next position
970 * needs to be updated.
971 */
972 if (nextp)
973 *nextp = n;
974}
975
976static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
977{
978 struct work_struct *work = list_first_entry(&cwq->delayed_works,
979 struct work_struct, entry);
980
981 trace_workqueue_activate_work(work);
982 move_linked_works(work, &cwq->pool->worklist, NULL);
983 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
984 cwq->nr_active++;
985}
986
987/**
988 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
989 * @cwq: cwq of interest
990 * @color: color of work which left the queue
991 * @delayed: for a delayed work
992 *
993 * A work either has completed or is removed from pending queue,
994 * decrement nr_in_flight of its cwq and handle workqueue flushing.
995 *
996 * CONTEXT:
997 * spin_lock_irq(gcwq->lock).
998 */
999static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color,
1000 bool delayed)
1001{
1002 /* ignore uncolored works */
1003 if (color == WORK_NO_COLOR)
1004 return;
1005
1006 cwq->nr_in_flight[color]--;
1007
1008 if (!delayed) {
1009 cwq->nr_active--;
1010 if (!list_empty(&cwq->delayed_works)) {
1011 /* one down, submit a delayed one */
1012 if (cwq->nr_active < cwq->max_active)
1013 cwq_activate_first_delayed(cwq);
1014 }
1015 }
1016
1017 /* is flush in progress and are we at the flushing tip? */
1018 if (likely(cwq->flush_color != color))
1019 return;
1020
1021 /* are there still in-flight works? */
1022 if (cwq->nr_in_flight[color])
1023 return;
1024
1025 /* this cwq is done, clear flush_color */
1026 cwq->flush_color = -1;
1027
1028 /*
1029 * If this was the last cwq, wake up the first flusher. It
1030 * will handle the rest.
1031 */
1032 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
1033 complete(&cwq->wq->first_flusher->done);
1034}
1035
Tejun Heo36e227d2012-08-03 10:30:46 -07001036/**
Tejun Heobbb68df2012-08-03 10:30:46 -07001037 * try_to_grab_pending - steal work item from worklist and disable irq
Tejun Heo36e227d2012-08-03 10:30:46 -07001038 * @work: work item to steal
1039 * @is_dwork: @work is a delayed_work
Tejun Heobbb68df2012-08-03 10:30:46 -07001040 * @flags: place to store irq state
Tejun Heo36e227d2012-08-03 10:30:46 -07001041 *
1042 * Try to grab PENDING bit of @work. This function can handle @work in any
1043 * stable state - idle, on timer or on worklist. Return values are
1044 *
1045 * 1 if @work was pending and we successfully stole PENDING
1046 * 0 if @work was idle and we claimed PENDING
1047 * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry
Tejun Heobbb68df2012-08-03 10:30:46 -07001048 * -ENOENT if someone else is canceling @work, this state may persist
1049 * for arbitrarily long
Tejun Heo36e227d2012-08-03 10:30:46 -07001050 *
Tejun Heobbb68df2012-08-03 10:30:46 -07001051 * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001052 * interrupted while holding PENDING and @work off queue, irq must be
1053 * disabled on entry. This, combined with delayed_work->timer being
1054 * irqsafe, ensures that we return -EAGAIN for finite short period of time.
Tejun Heobbb68df2012-08-03 10:30:46 -07001055 *
1056 * On successful return, >= 0, irq is disabled and the caller is
1057 * responsible for releasing it using local_irq_restore(*@flags).
1058 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001059 * This function is safe to call from any context including IRQ handler.
Tejun Heobf4ede02012-08-03 10:30:46 -07001060 */
Tejun Heobbb68df2012-08-03 10:30:46 -07001061static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
1062 unsigned long *flags)
Tejun Heobf4ede02012-08-03 10:30:46 -07001063{
1064 struct global_cwq *gcwq;
Tejun Heobf4ede02012-08-03 10:30:46 -07001065
Tejun Heobbb68df2012-08-03 10:30:46 -07001066 WARN_ON_ONCE(in_irq());
1067
1068 local_irq_save(*flags);
1069
Tejun Heo36e227d2012-08-03 10:30:46 -07001070 /* try to steal the timer if it exists */
1071 if (is_dwork) {
1072 struct delayed_work *dwork = to_delayed_work(work);
1073
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001074 /*
1075 * dwork->timer is irqsafe. If del_timer() fails, it's
1076 * guaranteed that the timer is not queued anywhere and not
1077 * running on the local CPU.
1078 */
Tejun Heo36e227d2012-08-03 10:30:46 -07001079 if (likely(del_timer(&dwork->timer)))
1080 return 1;
1081 }
1082
1083 /* try to claim PENDING the normal way */
Tejun Heobf4ede02012-08-03 10:30:46 -07001084 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1085 return 0;
1086
1087 /*
1088 * The queueing is in progress, or it is already queued. Try to
1089 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1090 */
1091 gcwq = get_work_gcwq(work);
1092 if (!gcwq)
Tejun Heobbb68df2012-08-03 10:30:46 -07001093 goto fail;
Tejun Heobf4ede02012-08-03 10:30:46 -07001094
Tejun Heobbb68df2012-08-03 10:30:46 -07001095 spin_lock(&gcwq->lock);
Tejun Heobf4ede02012-08-03 10:30:46 -07001096 if (!list_empty(&work->entry)) {
1097 /*
1098 * This work is queued, but perhaps we locked the wrong gcwq.
1099 * In that case we must see the new value after rmb(), see
1100 * insert_work()->wmb().
1101 */
1102 smp_rmb();
1103 if (gcwq == get_work_gcwq(work)) {
1104 debug_work_deactivate(work);
1105 list_del_init(&work->entry);
1106 cwq_dec_nr_in_flight(get_work_cwq(work),
1107 get_work_color(work),
1108 *work_data_bits(work) & WORK_STRUCT_DELAYED);
Tejun Heo36e227d2012-08-03 10:30:46 -07001109
Tejun Heobbb68df2012-08-03 10:30:46 -07001110 spin_unlock(&gcwq->lock);
Tejun Heo36e227d2012-08-03 10:30:46 -07001111 return 1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001112 }
1113 }
Tejun Heobbb68df2012-08-03 10:30:46 -07001114 spin_unlock(&gcwq->lock);
1115fail:
1116 local_irq_restore(*flags);
1117 if (work_is_canceling(work))
1118 return -ENOENT;
1119 cpu_relax();
Tejun Heo36e227d2012-08-03 10:30:46 -07001120 return -EAGAIN;
Tejun Heobf4ede02012-08-03 10:30:46 -07001121}
1122
1123/**
Tejun Heo7e116292010-06-29 10:07:13 +02001124 * insert_work - insert a work into gcwq
Tejun Heo4690c4a2010-06-29 10:07:10 +02001125 * @cwq: cwq @work belongs to
1126 * @work: work to insert
1127 * @head: insertion point
1128 * @extra_flags: extra WORK_STRUCT_* flags to set
1129 *
Tejun Heo7e116292010-06-29 10:07:13 +02001130 * Insert @work which belongs to @cwq into @gcwq after @head.
1131 * @extra_flags is or'd to work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +02001132 *
1133 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001134 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02001135 */
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001136static void insert_work(struct cpu_workqueue_struct *cwq,
Tejun Heo4690c4a2010-06-29 10:07:10 +02001137 struct work_struct *work, struct list_head *head,
1138 unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001139{
Tejun Heo63d95a92012-07-12 14:46:37 -07001140 struct worker_pool *pool = cwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01001141
Tejun Heo4690c4a2010-06-29 10:07:10 +02001142 /* we own @work, set data and link */
Tejun Heo7a22ad72010-06-29 10:07:13 +02001143 set_work_cwq(work, cwq, extra_flags);
Tejun Heo4690c4a2010-06-29 10:07:10 +02001144
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001145 /*
1146 * Ensure that we get the right work->data if we see the
1147 * result of list_add() below, see try_to_grab_pending().
1148 */
1149 smp_wmb();
Tejun Heo4690c4a2010-06-29 10:07:10 +02001150
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -07001151 list_add_tail(&work->entry, head);
Tejun Heoe22bee72010-06-29 10:07:14 +02001152
1153 /*
1154 * Ensure either worker_sched_deactivated() sees the above
1155 * list_add_tail() or we see zero nr_running to avoid workers
1156 * lying around lazily while there are works to be processed.
1157 */
1158 smp_mb();
1159
Tejun Heo63d95a92012-07-12 14:46:37 -07001160 if (__need_more_worker(pool))
1161 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001162}
1163
Tejun Heoc8efcc22010-12-20 19:32:04 +01001164/*
1165 * Test whether @work is being queued from another work executing on the
1166 * same workqueue. This is rather expensive and should only be used from
1167 * cold paths.
1168 */
1169static bool is_chained_work(struct workqueue_struct *wq)
1170{
1171 unsigned long flags;
1172 unsigned int cpu;
1173
1174 for_each_gcwq_cpu(cpu) {
1175 struct global_cwq *gcwq = get_gcwq(cpu);
1176 struct worker *worker;
1177 struct hlist_node *pos;
1178 int i;
1179
1180 spin_lock_irqsave(&gcwq->lock, flags);
1181 for_each_busy_worker(worker, i, pos, gcwq) {
1182 if (worker->task != current)
1183 continue;
1184 spin_unlock_irqrestore(&gcwq->lock, flags);
1185 /*
1186 * I'm @worker, no locking necessary. See if @work
1187 * is headed to the same workqueue.
1188 */
1189 return worker->current_cwq->wq == wq;
1190 }
1191 spin_unlock_irqrestore(&gcwq->lock, flags);
1192 }
1193 return false;
1194}
1195
Tejun Heo4690c4a2010-06-29 10:07:10 +02001196static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 struct work_struct *work)
1198{
Tejun Heo502ca9d2010-06-29 10:07:13 +02001199 struct global_cwq *gcwq;
1200 struct cpu_workqueue_struct *cwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001201 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001202 unsigned int work_flags;
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001203 unsigned int req_cpu = cpu;
Tejun Heo8930cab2012-08-03 10:30:45 -07001204
1205 /*
1206 * While a work item is PENDING && off queue, a task trying to
1207 * steal the PENDING will busy-loop waiting for it to either get
1208 * queued or lose PENDING. Grabbing PENDING and queueing should
1209 * happen with IRQ disabled.
1210 */
1211 WARN_ON_ONCE(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001213 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001214
Tejun Heoc8efcc22010-12-20 19:32:04 +01001215 /* if dying, only works from the same workqueue are allowed */
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02001216 if (unlikely(wq->flags & WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001217 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001218 return;
1219
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001220 /* determine gcwq to use */
1221 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001222 struct global_cwq *last_gcwq;
1223
Tejun Heo57469822012-08-03 10:30:45 -07001224 if (cpu == WORK_CPU_UNBOUND)
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001225 cpu = raw_smp_processor_id();
1226
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001227 /*
Tejun Heodbf25762012-08-20 14:51:23 -07001228 * It's multi cpu. If @work was previously on a different
1229 * cpu, it might still be running there, in which case the
1230 * work needs to be queued on that cpu to guarantee
1231 * non-reentrancy.
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001232 */
Tejun Heo502ca9d2010-06-29 10:07:13 +02001233 gcwq = get_gcwq(cpu);
Tejun Heodbf25762012-08-20 14:51:23 -07001234 last_gcwq = get_work_gcwq(work);
1235
1236 if (last_gcwq && last_gcwq != gcwq) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001237 struct worker *worker;
1238
Tejun Heo8930cab2012-08-03 10:30:45 -07001239 spin_lock(&last_gcwq->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001240
1241 worker = find_worker_executing_work(last_gcwq, work);
1242
1243 if (worker && worker->current_cwq->wq == wq)
1244 gcwq = last_gcwq;
1245 else {
1246 /* meh... not running there, queue here */
Tejun Heo8930cab2012-08-03 10:30:45 -07001247 spin_unlock(&last_gcwq->lock);
1248 spin_lock(&gcwq->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001249 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001250 } else {
1251 spin_lock(&gcwq->lock);
1252 }
Tejun Heof3421792010-07-02 10:03:51 +02001253 } else {
1254 gcwq = get_gcwq(WORK_CPU_UNBOUND);
Tejun Heo8930cab2012-08-03 10:30:45 -07001255 spin_lock(&gcwq->lock);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001256 }
1257
1258 /* gcwq determined, get cwq and queue */
1259 cwq = get_cwq(gcwq->cpu, wq);
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001260 trace_workqueue_queue_work(req_cpu, cwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001261
Dan Carpenterf5b25522012-04-13 22:06:58 +03001262 if (WARN_ON(!list_empty(&work->entry))) {
Tejun Heo8930cab2012-08-03 10:30:45 -07001263 spin_unlock(&gcwq->lock);
Dan Carpenterf5b25522012-04-13 22:06:58 +03001264 return;
1265 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001266
Tejun Heo73f53c42010-06-29 10:07:11 +02001267 cwq->nr_in_flight[cwq->work_color]++;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001268 work_flags = work_color_to_flags(cwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001269
1270 if (likely(cwq->nr_active < cwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001271 trace_workqueue_activate_work(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001272 cwq->nr_active++;
Tejun Heo32704762012-07-13 22:16:45 -07001273 worklist = &cwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001274 } else {
1275 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001276 worklist = &cwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001277 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001278
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001279 insert_work(cwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001280
Tejun Heo8930cab2012-08-03 10:30:45 -07001281 spin_unlock(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282}
1283
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001284/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07001285 * queue_work_on - queue work on specific cpu
1286 * @cpu: CPU number to execute work on
1287 * @wq: workqueue to use
1288 * @work: work to queue
1289 *
Tejun Heod4283e92012-08-03 10:30:44 -07001290 * Returns %false if @work was already on a queue, %true otherwise.
Zhang Ruic1a220e2008-07-23 21:28:39 -07001291 *
1292 * We queue the work to a specific CPU, the caller must ensure it
1293 * can't go away.
1294 */
Tejun Heod4283e92012-08-03 10:30:44 -07001295bool queue_work_on(int cpu, struct workqueue_struct *wq,
1296 struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07001297{
Tejun Heod4283e92012-08-03 10:30:44 -07001298 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001299 unsigned long flags;
1300
1301 local_irq_save(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001302
Tejun Heo22df02b2010-06-29 10:07:10 +02001303 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001304 __queue_work(cpu, wq, work);
Tejun Heod4283e92012-08-03 10:30:44 -07001305 ret = true;
Zhang Ruic1a220e2008-07-23 21:28:39 -07001306 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001307
1308 local_irq_restore(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001309 return ret;
1310}
1311EXPORT_SYMBOL_GPL(queue_work_on);
1312
Tejun Heo0a13c002012-08-03 10:30:44 -07001313/**
1314 * queue_work - queue work on a workqueue
1315 * @wq: workqueue to use
1316 * @work: work to queue
1317 *
Tejun Heod4283e92012-08-03 10:30:44 -07001318 * Returns %false if @work was already on a queue, %true otherwise.
Tejun Heo0a13c002012-08-03 10:30:44 -07001319 *
1320 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1321 * it can be processed by another CPU.
1322 */
Tejun Heod4283e92012-08-03 10:30:44 -07001323bool queue_work(struct workqueue_struct *wq, struct work_struct *work)
Tejun Heo0a13c002012-08-03 10:30:44 -07001324{
Tejun Heo57469822012-08-03 10:30:45 -07001325 return queue_work_on(WORK_CPU_UNBOUND, wq, work);
Tejun Heo0a13c002012-08-03 10:30:44 -07001326}
1327EXPORT_SYMBOL_GPL(queue_work);
1328
Tejun Heod8e794d2012-08-03 10:30:45 -07001329void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
David Howells52bad642006-11-22 14:54:01 +00001331 struct delayed_work *dwork = (struct delayed_work *)__data;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001332 struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001334 /* should have been called from irqsafe timer with irq already off */
Tejun Heo12650572012-08-08 09:38:42 -07001335 __queue_work(dwork->cpu, cwq->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336}
Tejun Heod8e794d2012-08-03 10:30:45 -07001337EXPORT_SYMBOL_GPL(delayed_work_timer_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001339static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
1340 struct delayed_work *dwork, unsigned long delay)
1341{
1342 struct timer_list *timer = &dwork->timer;
1343 struct work_struct *work = &dwork->work;
1344 unsigned int lcpu;
1345
1346 WARN_ON_ONCE(timer->function != delayed_work_timer_fn ||
1347 timer->data != (unsigned long)dwork);
1348 BUG_ON(timer_pending(timer));
1349 BUG_ON(!list_empty(&work->entry));
1350
1351 timer_stats_timer_set_start_info(&dwork->timer);
1352
1353 /*
1354 * This stores cwq for the moment, for the timer_fn. Note that the
1355 * work's gcwq is preserved to allow reentrance detection for
1356 * delayed works.
1357 */
1358 if (!(wq->flags & WQ_UNBOUND)) {
1359 struct global_cwq *gcwq = get_work_gcwq(work);
1360
Joonsoo Kime42986d2012-08-15 23:25:38 +09001361 /*
1362 * If we cannot get the last gcwq from @work directly,
1363 * select the last CPU such that it avoids unnecessarily
1364 * triggering non-reentrancy check in __queue_work().
1365 */
1366 lcpu = cpu;
1367 if (gcwq)
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001368 lcpu = gcwq->cpu;
Joonsoo Kime42986d2012-08-15 23:25:38 +09001369 if (lcpu == WORK_CPU_UNBOUND)
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001370 lcpu = raw_smp_processor_id();
1371 } else {
1372 lcpu = WORK_CPU_UNBOUND;
1373 }
1374
1375 set_work_cwq(work, get_cwq(lcpu, wq), 0);
1376
Tejun Heo12650572012-08-08 09:38:42 -07001377 dwork->cpu = cpu;
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001378 timer->expires = jiffies + delay;
1379
1380 if (unlikely(cpu != WORK_CPU_UNBOUND))
1381 add_timer_on(timer, cpu);
1382 else
1383 add_timer(timer);
1384}
1385
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001386/**
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001387 * queue_delayed_work_on - queue work on specific CPU after delay
1388 * @cpu: CPU number to execute work on
1389 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001390 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001391 * @delay: number of jiffies to wait before queueing
1392 *
Tejun Heo715f1302012-08-03 10:30:46 -07001393 * Returns %false if @work was already on a queue, %true otherwise. If
1394 * @delay is zero and @dwork is idle, it will be scheduled for immediate
1395 * execution.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001396 */
Tejun Heod4283e92012-08-03 10:30:44 -07001397bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
1398 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001399{
David Howells52bad642006-11-22 14:54:01 +00001400 struct work_struct *work = &dwork->work;
Tejun Heod4283e92012-08-03 10:30:44 -07001401 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001402 unsigned long flags;
1403
Tejun Heo715f1302012-08-03 10:30:46 -07001404 if (!delay)
1405 return queue_work_on(cpu, wq, &dwork->work);
1406
Tejun Heo8930cab2012-08-03 10:30:45 -07001407 /* read the comment in __queue_work() */
1408 local_irq_save(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001409
Tejun Heo22df02b2010-06-29 10:07:10 +02001410 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001411 __queue_delayed_work(cpu, wq, dwork, delay);
Tejun Heod4283e92012-08-03 10:30:44 -07001412 ret = true;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001413 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001414
1415 local_irq_restore(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001416 return ret;
1417}
Dave Jonesae90dd52006-06-30 01:40:45 -04001418EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Tejun Heoc8e55f32010-06-29 10:07:12 +02001420/**
Tejun Heo0a13c002012-08-03 10:30:44 -07001421 * queue_delayed_work - queue work on a workqueue after delay
1422 * @wq: workqueue to use
1423 * @dwork: delayable work to queue
1424 * @delay: number of jiffies to wait before queueing
1425 *
Tejun Heo715f1302012-08-03 10:30:46 -07001426 * Equivalent to queue_delayed_work_on() but tries to use the local CPU.
Tejun Heo0a13c002012-08-03 10:30:44 -07001427 */
Tejun Heod4283e92012-08-03 10:30:44 -07001428bool queue_delayed_work(struct workqueue_struct *wq,
Tejun Heo0a13c002012-08-03 10:30:44 -07001429 struct delayed_work *dwork, unsigned long delay)
1430{
Tejun Heo57469822012-08-03 10:30:45 -07001431 return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
Tejun Heo0a13c002012-08-03 10:30:44 -07001432}
1433EXPORT_SYMBOL_GPL(queue_delayed_work);
1434
1435/**
Tejun Heo8376fe22012-08-03 10:30:47 -07001436 * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
1437 * @cpu: CPU number to execute work on
1438 * @wq: workqueue to use
1439 * @dwork: work to queue
1440 * @delay: number of jiffies to wait before queueing
1441 *
1442 * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
1443 * modify @dwork's timer so that it expires after @delay. If @delay is
1444 * zero, @work is guaranteed to be scheduled immediately regardless of its
1445 * current state.
1446 *
1447 * Returns %false if @dwork was idle and queued, %true if @dwork was
1448 * pending and its timer was modified.
1449 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001450 * This function is safe to call from any context including IRQ handler.
Tejun Heo8376fe22012-08-03 10:30:47 -07001451 * See try_to_grab_pending() for details.
1452 */
1453bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
1454 struct delayed_work *dwork, unsigned long delay)
1455{
1456 unsigned long flags;
1457 int ret;
1458
1459 do {
1460 ret = try_to_grab_pending(&dwork->work, true, &flags);
1461 } while (unlikely(ret == -EAGAIN));
1462
1463 if (likely(ret >= 0)) {
1464 __queue_delayed_work(cpu, wq, dwork, delay);
1465 local_irq_restore(flags);
1466 }
1467
1468 /* -ENOENT from try_to_grab_pending() becomes %true */
1469 return ret;
1470}
1471EXPORT_SYMBOL_GPL(mod_delayed_work_on);
1472
1473/**
1474 * mod_delayed_work - modify delay of or queue a delayed work
1475 * @wq: workqueue to use
1476 * @dwork: work to queue
1477 * @delay: number of jiffies to wait before queueing
1478 *
1479 * mod_delayed_work_on() on local CPU.
1480 */
1481bool mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dwork,
1482 unsigned long delay)
1483{
1484 return mod_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
1485}
1486EXPORT_SYMBOL_GPL(mod_delayed_work);
1487
1488/**
Tejun Heoc8e55f32010-06-29 10:07:12 +02001489 * worker_enter_idle - enter idle state
1490 * @worker: worker which is entering idle state
1491 *
1492 * @worker is entering idle state. Update stats and idle timer if
1493 * necessary.
1494 *
1495 * LOCKING:
1496 * spin_lock_irq(gcwq->lock).
1497 */
1498static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001500 struct worker_pool *pool = worker->pool;
1501 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Tejun Heoc8e55f32010-06-29 10:07:12 +02001503 BUG_ON(worker->flags & WORKER_IDLE);
1504 BUG_ON(!list_empty(&worker->entry) &&
1505 (worker->hentry.next || worker->hentry.pprev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Tejun Heocb444762010-07-02 10:03:50 +02001507 /* can't use worker_set_flags(), also called from start_worker() */
1508 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001509 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001510 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001511
Tejun Heoc8e55f32010-06-29 10:07:12 +02001512 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001513 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001514
Tejun Heo628c78e2012-07-17 12:39:27 -07001515 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1516 mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
Tejun Heocb444762010-07-02 10:03:50 +02001517
Tejun Heo544ecf32012-05-14 15:04:50 -07001518 /*
Tejun Heo628c78e2012-07-17 12:39:27 -07001519 * Sanity check nr_running. Because gcwq_unbind_fn() releases
1520 * gcwq->lock between setting %WORKER_UNBOUND and zapping
1521 * nr_running, the warning may trigger spuriously. Check iff
1522 * unbind is not in progress.
Tejun Heo544ecf32012-05-14 15:04:50 -07001523 */
Tejun Heo628c78e2012-07-17 12:39:27 -07001524 WARN_ON_ONCE(!(gcwq->flags & GCWQ_DISASSOCIATED) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001525 pool->nr_workers == pool->nr_idle &&
Tejun Heo63d95a92012-07-12 14:46:37 -07001526 atomic_read(get_pool_nr_running(pool)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527}
1528
Tejun Heoc8e55f32010-06-29 10:07:12 +02001529/**
1530 * worker_leave_idle - leave idle state
1531 * @worker: worker which is leaving idle state
1532 *
1533 * @worker is leaving idle state. Update stats.
1534 *
1535 * LOCKING:
1536 * spin_lock_irq(gcwq->lock).
1537 */
1538static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001540 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Tejun Heoc8e55f32010-06-29 10:07:12 +02001542 BUG_ON(!(worker->flags & WORKER_IDLE));
Tejun Heod302f012010-06-29 10:07:13 +02001543 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001544 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001545 list_del_init(&worker->entry);
1546}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Tejun Heoe22bee72010-06-29 10:07:14 +02001548/**
1549 * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq
1550 * @worker: self
1551 *
1552 * Works which are scheduled while the cpu is online must at least be
1553 * scheduled to a worker which is bound to the cpu so that if they are
1554 * flushed from cpu callbacks while cpu is going down, they are
1555 * guaranteed to execute on the cpu.
1556 *
1557 * This function is to be used by rogue workers and rescuers to bind
1558 * themselves to the target cpu and may race with cpu going down or
1559 * coming online. kthread_bind() can't be used because it may put the
1560 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1561 * verbatim as it's best effort and blocking and gcwq may be
1562 * [dis]associated in the meantime.
1563 *
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001564 * This function tries set_cpus_allowed() and locks gcwq and verifies the
1565 * binding against %GCWQ_DISASSOCIATED which is set during
1566 * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1567 * enters idle state or fetches works without dropping lock, it can
1568 * guarantee the scheduling requirement described in the first paragraph.
Tejun Heoe22bee72010-06-29 10:07:14 +02001569 *
1570 * CONTEXT:
1571 * Might sleep. Called without any lock but returns with gcwq->lock
1572 * held.
1573 *
1574 * RETURNS:
1575 * %true if the associated gcwq is online (@worker is successfully
1576 * bound), %false if offline.
1577 */
1578static bool worker_maybe_bind_and_lock(struct worker *worker)
Namhyung Kim972fa1c2010-08-22 23:19:43 +09001579__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001580{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001581 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001582 struct task_struct *task = worker->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Tejun Heoe22bee72010-06-29 10:07:14 +02001584 while (true) {
1585 /*
1586 * The following call may fail, succeed or succeed
1587 * without actually migrating the task to the cpu if
1588 * it races with cpu hotunplug operation. Verify
1589 * against GCWQ_DISASSOCIATED.
1590 */
Tejun Heof3421792010-07-02 10:03:51 +02001591 if (!(gcwq->flags & GCWQ_DISASSOCIATED))
1592 set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu));
Oleg Nesterov85f41862007-05-09 02:34:20 -07001593
Tejun Heoe22bee72010-06-29 10:07:14 +02001594 spin_lock_irq(&gcwq->lock);
1595 if (gcwq->flags & GCWQ_DISASSOCIATED)
1596 return false;
1597 if (task_cpu(task) == gcwq->cpu &&
1598 cpumask_equal(&current->cpus_allowed,
1599 get_cpu_mask(gcwq->cpu)))
1600 return true;
1601 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001602
Tejun Heo5035b202011-04-29 18:08:37 +02001603 /*
1604 * We've raced with CPU hot[un]plug. Give it a breather
1605 * and retry migration. cond_resched() is required here;
1606 * otherwise, we might deadlock against cpu_stop trying to
1607 * bring down the CPU on non-preemptive kernel.
1608 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001609 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001610 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001611 }
1612}
1613
Tejun Heo25511a42012-07-17 12:39:27 -07001614struct idle_rebind {
1615 int cnt; /* # workers to be rebound */
1616 struct completion done; /* all workers rebound */
1617};
1618
Tejun Heoe22bee72010-06-29 10:07:14 +02001619/*
Tejun Heo25511a42012-07-17 12:39:27 -07001620 * Rebind an idle @worker to its CPU. During CPU onlining, this has to
1621 * happen synchronously for idle workers. worker_thread() will test
1622 * %WORKER_REBIND before leaving idle and call this function.
1623 */
1624static void idle_worker_rebind(struct worker *worker)
1625{
1626 struct global_cwq *gcwq = worker->pool->gcwq;
1627
1628 /* CPU must be online at this point */
1629 WARN_ON(!worker_maybe_bind_and_lock(worker));
1630 if (!--worker->idle_rebind->cnt)
1631 complete(&worker->idle_rebind->done);
1632 spin_unlock_irq(&worker->pool->gcwq->lock);
1633
1634 /* we did our part, wait for rebind_workers() to finish up */
1635 wait_event(gcwq->rebind_hold, !(worker->flags & WORKER_REBIND));
Tejun Heoec588152012-09-04 23:16:32 -07001636
1637 /*
1638 * rebind_workers() shouldn't finish until all workers passed the
1639 * above WORKER_REBIND wait. Tell it when done.
1640 */
1641 spin_lock_irq(&worker->pool->gcwq->lock);
1642 if (!--worker->idle_rebind->cnt)
1643 complete(&worker->idle_rebind->done);
1644 spin_unlock_irq(&worker->pool->gcwq->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07001645}
1646
1647/*
1648 * Function for @worker->rebind.work used to rebind unbound busy workers to
Tejun Heo403c8212012-07-17 12:39:27 -07001649 * the associated cpu which is coming back online. This is scheduled by
1650 * cpu up but can race with other cpu hotplug operations and may be
1651 * executed twice without intervening cpu down.
Tejun Heoe22bee72010-06-29 10:07:14 +02001652 */
Tejun Heo25511a42012-07-17 12:39:27 -07001653static void busy_worker_rebind_fn(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001654{
1655 struct worker *worker = container_of(work, struct worker, rebind_work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001656 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001657
Lai Jiangshan960bd112012-09-17 15:42:31 -07001658 worker_maybe_bind_and_lock(worker);
1659
1660 /*
1661 * %WORKER_REBIND must be cleared even if the above binding failed;
1662 * otherwise, we may confuse the next CPU_UP cycle or oops / get
1663 * stuck by calling idle_worker_rebind() prematurely. If CPU went
1664 * down again inbetween, %WORKER_UNBOUND would be set, so clearing
1665 * %WORKER_REBIND is always safe.
1666 */
1667 worker_clr_flags(worker, WORKER_REBIND);
Tejun Heoe22bee72010-06-29 10:07:14 +02001668
1669 spin_unlock_irq(&gcwq->lock);
1670}
1671
Tejun Heo25511a42012-07-17 12:39:27 -07001672/**
1673 * rebind_workers - rebind all workers of a gcwq to the associated CPU
1674 * @gcwq: gcwq of interest
1675 *
1676 * @gcwq->cpu is coming online. Rebind all workers to the CPU. Rebinding
1677 * is different for idle and busy ones.
1678 *
1679 * The idle ones should be rebound synchronously and idle rebinding should
1680 * be complete before any worker starts executing work items with
1681 * concurrency management enabled; otherwise, scheduler may oops trying to
1682 * wake up non-local idle worker from wq_worker_sleeping().
1683 *
1684 * This is achieved by repeatedly requesting rebinding until all idle
1685 * workers are known to have been rebound under @gcwq->lock and holding all
1686 * idle workers from becoming busy until idle rebinding is complete.
1687 *
1688 * Once idle workers are rebound, busy workers can be rebound as they
1689 * finish executing their current work items. Queueing the rebind work at
1690 * the head of their scheduled lists is enough. Note that nr_running will
1691 * be properbly bumped as busy workers rebind.
1692 *
1693 * On return, all workers are guaranteed to either be bound or have rebind
1694 * work item scheduled.
1695 */
1696static void rebind_workers(struct global_cwq *gcwq)
1697 __releases(&gcwq->lock) __acquires(&gcwq->lock)
1698{
1699 struct idle_rebind idle_rebind;
1700 struct worker_pool *pool;
1701 struct worker *worker;
1702 struct hlist_node *pos;
1703 int i;
1704
1705 lockdep_assert_held(&gcwq->lock);
1706
1707 for_each_worker_pool(pool, gcwq)
1708 lockdep_assert_held(&pool->manager_mutex);
1709
1710 /*
1711 * Rebind idle workers. Interlocked both ways. We wait for
1712 * workers to rebind via @idle_rebind.done. Workers will wait for
1713 * us to finish up by watching %WORKER_REBIND.
1714 */
1715 init_completion(&idle_rebind.done);
1716retry:
1717 idle_rebind.cnt = 1;
1718 INIT_COMPLETION(idle_rebind.done);
1719
1720 /* set REBIND and kick idle ones, we'll wait for these later */
1721 for_each_worker_pool(pool, gcwq) {
1722 list_for_each_entry(worker, &pool->idle_list, entry) {
Lai Jiangshan96e65302012-09-02 00:28:19 +08001723 unsigned long worker_flags = worker->flags;
1724
Tejun Heo25511a42012-07-17 12:39:27 -07001725 if (worker->flags & WORKER_REBIND)
1726 continue;
1727
Lai Jiangshan96e65302012-09-02 00:28:19 +08001728 /* morph UNBOUND to REBIND atomically */
1729 worker_flags &= ~WORKER_UNBOUND;
1730 worker_flags |= WORKER_REBIND;
1731 ACCESS_ONCE(worker->flags) = worker_flags;
Tejun Heo25511a42012-07-17 12:39:27 -07001732
1733 idle_rebind.cnt++;
1734 worker->idle_rebind = &idle_rebind;
1735
1736 /* worker_thread() will call idle_worker_rebind() */
1737 wake_up_process(worker->task);
1738 }
1739 }
1740
1741 if (--idle_rebind.cnt) {
1742 spin_unlock_irq(&gcwq->lock);
1743 wait_for_completion(&idle_rebind.done);
1744 spin_lock_irq(&gcwq->lock);
1745 /* busy ones might have become idle while waiting, retry */
1746 goto retry;
1747 }
1748
Tejun Heo90beca52012-09-04 23:12:33 -07001749 /* all idle workers are rebound, rebind busy workers */
Tejun Heo25511a42012-07-17 12:39:27 -07001750 for_each_busy_worker(worker, i, pos, gcwq) {
Lai Jiangshan96e65302012-09-02 00:28:19 +08001751 unsigned long worker_flags = worker->flags;
Tejun Heo25511a42012-07-17 12:39:27 -07001752 struct work_struct *rebind_work = &worker->rebind_work;
Joonsoo Kime2b6a6d2012-08-15 23:25:40 +09001753 struct workqueue_struct *wq;
Tejun Heo25511a42012-07-17 12:39:27 -07001754
Lai Jiangshan96e65302012-09-02 00:28:19 +08001755 /* morph UNBOUND to REBIND atomically */
1756 worker_flags &= ~WORKER_UNBOUND;
1757 worker_flags |= WORKER_REBIND;
1758 ACCESS_ONCE(worker->flags) = worker_flags;
Tejun Heo25511a42012-07-17 12:39:27 -07001759
1760 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
1761 work_data_bits(rebind_work)))
1762 continue;
1763
Tejun Heo25511a42012-07-17 12:39:27 -07001764 debug_work_activate(rebind_work);
Joonsoo Kime2b6a6d2012-08-15 23:25:40 +09001765
1766 /*
1767 * wq doesn't really matter but let's keep @worker->pool
1768 * and @cwq->pool consistent for sanity.
1769 */
1770 if (worker_pool_pri(worker->pool))
1771 wq = system_highpri_wq;
1772 else
1773 wq = system_wq;
1774
1775 insert_work(get_cwq(gcwq->cpu, wq), rebind_work,
1776 worker->scheduled.next,
1777 work_color_to_flags(WORK_NO_COLOR));
Tejun Heo25511a42012-07-17 12:39:27 -07001778 }
Tejun Heo90beca52012-09-04 23:12:33 -07001779
1780 /*
1781 * All idle workers are rebound and waiting for %WORKER_REBIND to
1782 * be cleared inside idle_worker_rebind(). Clear and release.
1783 * Clearing %WORKER_REBIND from this foreign context is safe
1784 * because these workers are still guaranteed to be idle.
Tejun Heoec588152012-09-04 23:16:32 -07001785 *
1786 * We need to make sure all idle workers passed WORKER_REBIND wait
1787 * in idle_worker_rebind() before returning; otherwise, workers can
1788 * get stuck at the wait if hotplug cycle repeats.
Tejun Heo90beca52012-09-04 23:12:33 -07001789 */
Tejun Heoec588152012-09-04 23:16:32 -07001790 idle_rebind.cnt = 1;
1791 INIT_COMPLETION(idle_rebind.done);
1792
1793 for_each_worker_pool(pool, gcwq) {
1794 list_for_each_entry(worker, &pool->idle_list, entry) {
Tejun Heo90beca52012-09-04 23:12:33 -07001795 worker->flags &= ~WORKER_REBIND;
Tejun Heoec588152012-09-04 23:16:32 -07001796 idle_rebind.cnt++;
1797 }
1798 }
Tejun Heo90beca52012-09-04 23:12:33 -07001799
1800 wake_up_all(&gcwq->rebind_hold);
Tejun Heoec588152012-09-04 23:16:32 -07001801
1802 if (--idle_rebind.cnt) {
1803 spin_unlock_irq(&gcwq->lock);
1804 wait_for_completion(&idle_rebind.done);
1805 spin_lock_irq(&gcwq->lock);
1806 }
Tejun Heo25511a42012-07-17 12:39:27 -07001807}
1808
Tejun Heoc34056a2010-06-29 10:07:11 +02001809static struct worker *alloc_worker(void)
1810{
1811 struct worker *worker;
1812
1813 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001814 if (worker) {
1815 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001816 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heo25511a42012-07-17 12:39:27 -07001817 INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn);
Tejun Heoe22bee72010-06-29 10:07:14 +02001818 /* on creation a worker is in !idle && prep state */
1819 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001820 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001821 return worker;
1822}
1823
1824/**
1825 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001826 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001827 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001828 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001829 * can be started by calling start_worker() or destroyed using
1830 * destroy_worker().
1831 *
1832 * CONTEXT:
1833 * Might sleep. Does GFP_KERNEL allocations.
1834 *
1835 * RETURNS:
1836 * Pointer to the newly created worker.
1837 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001838static struct worker *create_worker(struct worker_pool *pool)
Tejun Heoc34056a2010-06-29 10:07:11 +02001839{
Tejun Heo63d95a92012-07-12 14:46:37 -07001840 struct global_cwq *gcwq = pool->gcwq;
Tejun Heo32704762012-07-13 22:16:45 -07001841 const char *pri = worker_pool_pri(pool) ? "H" : "";
Tejun Heoc34056a2010-06-29 10:07:11 +02001842 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001843 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001844
Tejun Heo8b03ae32010-06-29 10:07:12 +02001845 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001846 while (ida_get_new(&pool->worker_ida, &id)) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001847 spin_unlock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001848 if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001849 goto fail;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001850 spin_lock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001851 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02001852 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001853
1854 worker = alloc_worker();
1855 if (!worker)
1856 goto fail;
1857
Tejun Heobd7bdd42012-07-12 14:46:37 -07001858 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001859 worker->id = id;
1860
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001861 if (gcwq->cpu != WORK_CPU_UNBOUND)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001862 worker->task = kthread_create_on_node(worker_thread,
Tejun Heo32704762012-07-13 22:16:45 -07001863 worker, cpu_to_node(gcwq->cpu),
1864 "kworker/%u:%d%s", gcwq->cpu, id, pri);
Tejun Heof3421792010-07-02 10:03:51 +02001865 else
1866 worker->task = kthread_create(worker_thread, worker,
Tejun Heo32704762012-07-13 22:16:45 -07001867 "kworker/u:%d%s", id, pri);
Tejun Heoc34056a2010-06-29 10:07:11 +02001868 if (IS_ERR(worker->task))
1869 goto fail;
1870
Tejun Heo32704762012-07-13 22:16:45 -07001871 if (worker_pool_pri(pool))
1872 set_user_nice(worker->task, HIGHPRI_NICE_LEVEL);
1873
Tejun Heodb7bccf2010-06-29 10:07:12 +02001874 /*
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001875 * Determine CPU binding of the new worker depending on
1876 * %GCWQ_DISASSOCIATED. The caller is responsible for ensuring the
1877 * flag remains stable across this function. See the comments
1878 * above the flag definition for details.
1879 *
1880 * As an unbound worker may later become a regular one if CPU comes
1881 * online, make sure every worker has %PF_THREAD_BOUND set.
Tejun Heodb7bccf2010-06-29 10:07:12 +02001882 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001883 if (!(gcwq->flags & GCWQ_DISASSOCIATED)) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001884 kthread_bind(worker->task, gcwq->cpu);
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001885 } else {
Tejun Heodb7bccf2010-06-29 10:07:12 +02001886 worker->task->flags |= PF_THREAD_BOUND;
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001887 worker->flags |= WORKER_UNBOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001889
Tejun Heoc34056a2010-06-29 10:07:11 +02001890 return worker;
1891fail:
1892 if (id >= 0) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001893 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001894 ida_remove(&pool->worker_ida, id);
Tejun Heo8b03ae32010-06-29 10:07:12 +02001895 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001896 }
1897 kfree(worker);
1898 return NULL;
1899}
1900
1901/**
1902 * start_worker - start a newly created worker
1903 * @worker: worker to start
1904 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001905 * Make the gcwq aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001906 *
1907 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001908 * spin_lock_irq(gcwq->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001909 */
1910static void start_worker(struct worker *worker)
1911{
Tejun Heocb444762010-07-02 10:03:50 +02001912 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001913 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001914 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001915 wake_up_process(worker->task);
1916}
1917
1918/**
1919 * destroy_worker - destroy a workqueue worker
1920 * @worker: worker to be destroyed
1921 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001922 * Destroy @worker and adjust @gcwq stats accordingly.
1923 *
1924 * CONTEXT:
1925 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001926 */
1927static void destroy_worker(struct worker *worker)
1928{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001929 struct worker_pool *pool = worker->pool;
1930 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001931 int id = worker->id;
1932
1933 /* sanity check frenzy */
1934 BUG_ON(worker->current_work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001935 BUG_ON(!list_empty(&worker->scheduled));
Tejun Heoc34056a2010-06-29 10:07:11 +02001936
Tejun Heoc8e55f32010-06-29 10:07:12 +02001937 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001938 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001939 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001940 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001941
1942 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001943 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001944
1945 spin_unlock_irq(&gcwq->lock);
1946
Tejun Heoc34056a2010-06-29 10:07:11 +02001947 kthread_stop(worker->task);
1948 kfree(worker);
1949
Tejun Heo8b03ae32010-06-29 10:07:12 +02001950 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001951 ida_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001952}
1953
Tejun Heo63d95a92012-07-12 14:46:37 -07001954static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001955{
Tejun Heo63d95a92012-07-12 14:46:37 -07001956 struct worker_pool *pool = (void *)__pool;
1957 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001958
1959 spin_lock_irq(&gcwq->lock);
1960
Tejun Heo63d95a92012-07-12 14:46:37 -07001961 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001962 struct worker *worker;
1963 unsigned long expires;
1964
1965 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001966 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001967 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1968
1969 if (time_before(jiffies, expires))
Tejun Heo63d95a92012-07-12 14:46:37 -07001970 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001971 else {
1972 /* it's been idle for too long, wake up manager */
Tejun Heo11ebea52012-07-12 14:46:37 -07001973 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -07001974 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001975 }
1976 }
1977
1978 spin_unlock_irq(&gcwq->lock);
1979}
1980
1981static bool send_mayday(struct work_struct *work)
1982{
1983 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1984 struct workqueue_struct *wq = cwq->wq;
Tejun Heof3421792010-07-02 10:03:51 +02001985 unsigned int cpu;
Tejun Heoe22bee72010-06-29 10:07:14 +02001986
1987 if (!(wq->flags & WQ_RESCUER))
1988 return false;
1989
1990 /* mayday mayday mayday */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001991 cpu = cwq->pool->gcwq->cpu;
Tejun Heof3421792010-07-02 10:03:51 +02001992 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1993 if (cpu == WORK_CPU_UNBOUND)
1994 cpu = 0;
Tejun Heof2e005a2010-07-20 15:59:09 +02001995 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001996 wake_up_process(wq->rescuer->task);
1997 return true;
1998}
1999
Tejun Heo63d95a92012-07-12 14:46:37 -07002000static void gcwq_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02002001{
Tejun Heo63d95a92012-07-12 14:46:37 -07002002 struct worker_pool *pool = (void *)__pool;
2003 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002004 struct work_struct *work;
2005
2006 spin_lock_irq(&gcwq->lock);
2007
Tejun Heo63d95a92012-07-12 14:46:37 -07002008 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02002009 /*
2010 * We've been trying to create a new worker but
2011 * haven't been successful. We might be hitting an
2012 * allocation deadlock. Send distress signals to
2013 * rescuers.
2014 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002015 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02002016 send_mayday(work);
2017 }
2018
2019 spin_unlock_irq(&gcwq->lock);
2020
Tejun Heo63d95a92012-07-12 14:46:37 -07002021 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02002022}
2023
2024/**
2025 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07002026 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02002027 *
Tejun Heo63d95a92012-07-12 14:46:37 -07002028 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02002029 * have at least one idle worker on return from this function. If
2030 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07002031 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02002032 * possible allocation deadlock.
2033 *
2034 * On return, need_to_create_worker() is guaranteed to be false and
2035 * may_start_working() true.
2036 *
2037 * LOCKING:
2038 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
2039 * multiple times. Does GFP_KERNEL allocations. Called only from
2040 * manager.
2041 *
2042 * RETURNS:
2043 * false if no action was taken and gcwq->lock stayed locked, true
2044 * otherwise.
2045 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002046static bool maybe_create_worker(struct worker_pool *pool)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09002047__releases(&gcwq->lock)
2048__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02002049{
Tejun Heo63d95a92012-07-12 14:46:37 -07002050 struct global_cwq *gcwq = pool->gcwq;
2051
2052 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002053 return false;
2054restart:
Tejun Heo9f9c236442010-07-14 11:31:20 +02002055 spin_unlock_irq(&gcwq->lock);
2056
Tejun Heoe22bee72010-06-29 10:07:14 +02002057 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07002058 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02002059
2060 while (true) {
2061 struct worker *worker;
2062
Tejun Heobc2ae0f2012-07-17 12:39:27 -07002063 worker = create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002064 if (worker) {
Tejun Heo63d95a92012-07-12 14:46:37 -07002065 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02002066 spin_lock_irq(&gcwq->lock);
2067 start_worker(worker);
Tejun Heo63d95a92012-07-12 14:46:37 -07002068 BUG_ON(need_to_create_worker(pool));
Tejun Heoe22bee72010-06-29 10:07:14 +02002069 return true;
2070 }
2071
Tejun Heo63d95a92012-07-12 14:46:37 -07002072 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002073 break;
2074
Tejun Heoe22bee72010-06-29 10:07:14 +02002075 __set_current_state(TASK_INTERRUPTIBLE);
2076 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c236442010-07-14 11:31:20 +02002077
Tejun Heo63d95a92012-07-12 14:46:37 -07002078 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002079 break;
2080 }
2081
Tejun Heo63d95a92012-07-12 14:46:37 -07002082 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02002083 spin_lock_irq(&gcwq->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07002084 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002085 goto restart;
2086 return true;
2087}
2088
2089/**
2090 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo63d95a92012-07-12 14:46:37 -07002091 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02002092 *
Tejun Heo63d95a92012-07-12 14:46:37 -07002093 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02002094 * IDLE_WORKER_TIMEOUT.
2095 *
2096 * LOCKING:
2097 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
2098 * multiple times. Called only from manager.
2099 *
2100 * RETURNS:
2101 * false if no action was taken and gcwq->lock stayed locked, true
2102 * otherwise.
2103 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002104static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02002105{
2106 bool ret = false;
2107
Tejun Heo63d95a92012-07-12 14:46:37 -07002108 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02002109 struct worker *worker;
2110 unsigned long expires;
2111
Tejun Heo63d95a92012-07-12 14:46:37 -07002112 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02002113 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
2114
2115 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07002116 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02002117 break;
2118 }
2119
2120 destroy_worker(worker);
2121 ret = true;
2122 }
2123
2124 return ret;
2125}
2126
2127/**
2128 * manage_workers - manage worker pool
2129 * @worker: self
2130 *
2131 * Assume the manager role and manage gcwq worker pool @worker belongs
2132 * to. At any given time, there can be only zero or one manager per
2133 * gcwq. The exclusion is handled automatically by this function.
2134 *
2135 * The caller can safely start processing works on false return. On
2136 * true return, it's guaranteed that need_to_create_worker() is false
2137 * and may_start_working() is true.
2138 *
2139 * CONTEXT:
2140 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
2141 * multiple times. Does GFP_KERNEL allocations.
2142 *
2143 * RETURNS:
2144 * false if no action was taken and gcwq->lock stayed locked, true if
2145 * some action was taken.
2146 */
2147static bool manage_workers(struct worker *worker)
2148{
Tejun Heo63d95a92012-07-12 14:46:37 -07002149 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002150 bool ret = false;
2151
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002152 if (pool->flags & POOL_MANAGING_WORKERS)
Tejun Heoe22bee72010-06-29 10:07:14 +02002153 return ret;
2154
Lai Jiangshan552a37e2012-09-10 10:03:33 -07002155 pool->flags |= POOL_MANAGING_WORKERS;
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002156
2157 /*
2158 * To simplify both worker management and CPU hotplug, hold off
2159 * management while hotplug is in progress. CPU hotplug path can't
2160 * grab %POOL_MANAGING_WORKERS to achieve this because that can
2161 * lead to idle worker depletion (all become busy thinking someone
2162 * else is managing) which in turn can result in deadlock under
2163 * extreme circumstances. Use @pool->manager_mutex to synchronize
2164 * manager against CPU hotplug.
2165 *
2166 * manager_mutex would always be free unless CPU hotplug is in
2167 * progress. trylock first without dropping @gcwq->lock.
2168 */
2169 if (unlikely(!mutex_trylock(&pool->manager_mutex))) {
2170 spin_unlock_irq(&pool->gcwq->lock);
2171 mutex_lock(&pool->manager_mutex);
2172 /*
2173 * CPU hotplug could have happened while we were waiting
2174 * for manager_mutex. Hotplug itself can't handle us
2175 * because manager isn't either on idle or busy list, and
2176 * @gcwq's state and ours could have deviated.
2177 *
2178 * As hotplug is now excluded via manager_mutex, we can
2179 * simply try to bind. It will succeed or fail depending
2180 * on @gcwq's current state. Try it and adjust
2181 * %WORKER_UNBOUND accordingly.
2182 */
2183 if (worker_maybe_bind_and_lock(worker))
2184 worker->flags &= ~WORKER_UNBOUND;
2185 else
2186 worker->flags |= WORKER_UNBOUND;
2187
2188 ret = true;
2189 }
2190
Tejun Heo11ebea52012-07-12 14:46:37 -07002191 pool->flags &= ~POOL_MANAGE_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02002192
2193 /*
2194 * Destroy and then create so that may_start_working() is true
2195 * on return.
2196 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002197 ret |= maybe_destroy_workers(pool);
2198 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002199
Lai Jiangshan552a37e2012-09-10 10:03:33 -07002200 pool->flags &= ~POOL_MANAGING_WORKERS;
Tejun Heo60373152012-07-17 12:39:27 -07002201 mutex_unlock(&pool->manager_mutex);
Tejun Heoe22bee72010-06-29 10:07:14 +02002202 return ret;
2203}
2204
Tejun Heoa62428c2010-06-29 10:07:10 +02002205/**
2206 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02002207 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02002208 * @work: work to process
2209 *
2210 * Process @work. This function contains all the logics necessary to
2211 * process a single work including synchronization against and
2212 * interaction with other workers on the same cpu, queueing and
2213 * flushing. As long as context requirement is met, any worker can
2214 * call this function to process a work.
2215 *
2216 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002217 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02002218 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002219static void process_one_work(struct worker *worker, struct work_struct *work)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09002220__releases(&gcwq->lock)
2221__acquires(&gcwq->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02002222{
Tejun Heo7e116292010-06-29 10:07:13 +02002223 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002224 struct worker_pool *pool = worker->pool;
2225 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002226 struct hlist_head *bwh = busy_worker_head(gcwq, work);
Tejun Heofb0e7be2010-06-29 10:07:15 +02002227 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heoa62428c2010-06-29 10:07:10 +02002228 work_func_t f = work->func;
Tejun Heo73f53c42010-06-29 10:07:11 +02002229 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02002230 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02002231#ifdef CONFIG_LOCKDEP
2232 /*
2233 * It is permissible to free the struct work_struct from
2234 * inside the function that is called from it, this we need to
2235 * take into account for lockdep too. To avoid bogus "held
2236 * lock freed" warnings as well as problems when looking into
2237 * work->lockdep_map, make a copy and use that here.
2238 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07002239 struct lockdep_map lockdep_map;
2240
2241 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002242#endif
Tejun Heo6fec10a2012-07-22 10:16:34 -07002243 /*
2244 * Ensure we're on the correct CPU. DISASSOCIATED test is
2245 * necessary to avoid spurious warnings from rescuers servicing the
2246 * unbound or a disassociated gcwq.
2247 */
Tejun Heo25511a42012-07-17 12:39:27 -07002248 WARN_ON_ONCE(!(worker->flags & (WORKER_UNBOUND | WORKER_REBIND)) &&
Tejun Heo6fec10a2012-07-22 10:16:34 -07002249 !(gcwq->flags & GCWQ_DISASSOCIATED) &&
Tejun Heo25511a42012-07-17 12:39:27 -07002250 raw_smp_processor_id() != gcwq->cpu);
2251
Tejun Heo7e116292010-06-29 10:07:13 +02002252 /*
2253 * A single work shouldn't be executed concurrently by
2254 * multiple workers on a single cpu. Check whether anyone is
2255 * already processing the work. If so, defer the work to the
2256 * currently executing one.
2257 */
2258 collision = __find_worker_executing_work(gcwq, bwh, work);
2259 if (unlikely(collision)) {
2260 move_linked_works(work, &collision->scheduled, NULL);
2261 return;
2262 }
2263
Tejun Heo8930cab2012-08-03 10:30:45 -07002264 /* claim and dequeue */
Tejun Heoa62428c2010-06-29 10:07:10 +02002265 debug_work_deactivate(work);
Tejun Heoc8e55f32010-06-29 10:07:12 +02002266 hlist_add_head(&worker->hentry, bwh);
Tejun Heoc34056a2010-06-29 10:07:11 +02002267 worker->current_work = work;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02002268 worker->current_cwq = cwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002269 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002270
Tejun Heoa62428c2010-06-29 10:07:10 +02002271 list_del_init(&work->entry);
2272
Tejun Heo649027d2010-06-29 10:07:14 +02002273 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02002274 * CPU intensive works don't participate in concurrency
2275 * management. They're the scheduler's responsibility.
2276 */
2277 if (unlikely(cpu_intensive))
2278 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
2279
Tejun Heo974271c42012-07-12 14:46:37 -07002280 /*
2281 * Unbound gcwq isn't concurrency managed and work items should be
2282 * executed ASAP. Wake up another worker if necessary.
2283 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002284 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
2285 wake_up_worker(pool);
Tejun Heo974271c42012-07-12 14:46:37 -07002286
Tejun Heo8930cab2012-08-03 10:30:45 -07002287 /*
Tejun Heo23657bb2012-08-13 17:08:19 -07002288 * Record the last CPU and clear PENDING which should be the last
2289 * update to @work. Also, do this inside @gcwq->lock so that
2290 * PENDING and queued state changes happen together while IRQ is
2291 * disabled.
Tejun Heo8930cab2012-08-03 10:30:45 -07002292 */
Tejun Heo8930cab2012-08-03 10:30:45 -07002293 set_work_cpu_and_clear_pending(work, gcwq->cpu);
Tejun Heoa62428c2010-06-29 10:07:10 +02002294
Tejun Heo8930cab2012-08-03 10:30:45 -07002295 spin_unlock_irq(&gcwq->lock);
Tejun Heo959d1af2012-08-03 10:30:45 -07002296
Tejun Heoe159489b2011-01-09 23:32:15 +01002297 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002298 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002299 trace_workqueue_execute_start(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002300 f(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002301 /*
2302 * While we must be careful to not use "work" after this, the trace
2303 * point will only record its address.
2304 */
2305 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002306 lock_map_release(&lockdep_map);
2307 lock_map_release(&cwq->wq->lockdep_map);
2308
2309 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Valentin Ilie044c7822012-08-19 00:52:42 +03002310 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
2311 " last function: %pf\n",
2312 current->comm, preempt_count(), task_pid_nr(current), f);
Tejun Heoa62428c2010-06-29 10:07:10 +02002313 debug_show_held_locks(current);
2314 dump_stack();
2315 }
2316
Tejun Heo8b03ae32010-06-29 10:07:12 +02002317 spin_lock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002318
Tejun Heofb0e7be2010-06-29 10:07:15 +02002319 /* clear cpu intensive status */
2320 if (unlikely(cpu_intensive))
2321 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2322
Tejun Heoa62428c2010-06-29 10:07:10 +02002323 /* we're done with it, release */
Tejun Heoc8e55f32010-06-29 10:07:12 +02002324 hlist_del_init(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002325 worker->current_work = NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02002326 worker->current_cwq = NULL;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02002327 cwq_dec_nr_in_flight(cwq, work_color, false);
Tejun Heoa62428c2010-06-29 10:07:10 +02002328}
2329
Tejun Heoaffee4b2010-06-29 10:07:12 +02002330/**
2331 * process_scheduled_works - process scheduled works
2332 * @worker: self
2333 *
2334 * Process all scheduled works. Please note that the scheduled list
2335 * may change while processing a work, so this function repeatedly
2336 * fetches a work from the top and executes it.
2337 *
2338 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002339 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02002340 * multiple times.
2341 */
2342static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002344 while (!list_empty(&worker->scheduled)) {
2345 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002347 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349}
2350
Tejun Heo4690c4a2010-06-29 10:07:10 +02002351/**
2352 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02002353 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02002354 *
Tejun Heoe22bee72010-06-29 10:07:14 +02002355 * The gcwq worker thread function. There's a single dynamic pool of
2356 * these per each cpu. These workers process all works regardless of
2357 * their specific target workqueue. The only exception is works which
2358 * belong to workqueues with a rescuer which will be explained in
2359 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02002360 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002361static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362{
Tejun Heoc34056a2010-06-29 10:07:11 +02002363 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07002364 struct worker_pool *pool = worker->pool;
2365 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
Tejun Heoe22bee72010-06-29 10:07:14 +02002367 /* tell the scheduler that this is a workqueue worker */
2368 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002369woke_up:
Tejun Heoc8e55f32010-06-29 10:07:12 +02002370 spin_lock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Tejun Heo25511a42012-07-17 12:39:27 -07002372 /*
2373 * DIE can be set only while idle and REBIND set while busy has
2374 * @worker->rebind_work scheduled. Checking here is enough.
2375 */
2376 if (unlikely(worker->flags & (WORKER_REBIND | WORKER_DIE))) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02002377 spin_unlock_irq(&gcwq->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07002378
2379 if (worker->flags & WORKER_DIE) {
2380 worker->task->flags &= ~PF_WQ_WORKER;
2381 return 0;
2382 }
2383
2384 idle_worker_rebind(worker);
2385 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 }
2387
Tejun Heoc8e55f32010-06-29 10:07:12 +02002388 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002389recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002390 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002391 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002392 goto sleep;
2393
2394 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002395 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002396 goto recheck;
2397
Tejun Heoc8e55f32010-06-29 10:07:12 +02002398 /*
2399 * ->scheduled list can only be filled while a worker is
2400 * preparing to process a work or actually processing it.
2401 * Make sure nobody diddled with it while I was sleeping.
2402 */
2403 BUG_ON(!list_empty(&worker->scheduled));
2404
Tejun Heoe22bee72010-06-29 10:07:14 +02002405 /*
2406 * When control reaches this point, we're guaranteed to have
2407 * at least one idle worker or that someone else has already
2408 * assumed the manager role.
2409 */
2410 worker_clr_flags(worker, WORKER_PREP);
2411
2412 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002413 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002414 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002415 struct work_struct, entry);
2416
2417 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2418 /* optimization path, not strictly necessary */
2419 process_one_work(worker, work);
2420 if (unlikely(!list_empty(&worker->scheduled)))
2421 process_scheduled_works(worker);
2422 } else {
2423 move_linked_works(work, &worker->scheduled, NULL);
2424 process_scheduled_works(worker);
2425 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002426 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002427
Tejun Heoe22bee72010-06-29 10:07:14 +02002428 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002429sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07002430 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002431 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002432
Tejun Heoc8e55f32010-06-29 10:07:12 +02002433 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02002434 * gcwq->lock is held and there's no work to process and no
2435 * need to manage, sleep. Workers are woken up only while
2436 * holding gcwq->lock or from local cpu, so setting the
2437 * current state before releasing gcwq->lock is enough to
2438 * prevent losing any event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002439 */
2440 worker_enter_idle(worker);
2441 __set_current_state(TASK_INTERRUPTIBLE);
2442 spin_unlock_irq(&gcwq->lock);
2443 schedule();
2444 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445}
2446
Tejun Heoe22bee72010-06-29 10:07:14 +02002447/**
2448 * rescuer_thread - the rescuer thread function
2449 * @__wq: the associated workqueue
2450 *
2451 * Workqueue rescuer thread function. There's one rescuer for each
2452 * workqueue which has WQ_RESCUER set.
2453 *
2454 * Regular work processing on a gcwq may block trying to create a new
2455 * worker which uses GFP_KERNEL allocation which has slight chance of
2456 * developing into deadlock if some works currently on the same queue
2457 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2458 * the problem rescuer solves.
2459 *
2460 * When such condition is possible, the gcwq summons rescuers of all
2461 * workqueues which have works queued on the gcwq and let them process
2462 * those works so that forward progress can be guaranteed.
2463 *
2464 * This should happen rarely.
2465 */
2466static int rescuer_thread(void *__wq)
2467{
2468 struct workqueue_struct *wq = __wq;
2469 struct worker *rescuer = wq->rescuer;
2470 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heof3421792010-07-02 10:03:51 +02002471 bool is_unbound = wq->flags & WQ_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02002472 unsigned int cpu;
2473
2474 set_user_nice(current, RESCUER_NICE_LEVEL);
2475repeat:
2476 set_current_state(TASK_INTERRUPTIBLE);
2477
2478 if (kthread_should_stop())
2479 return 0;
2480
Tejun Heof3421792010-07-02 10:03:51 +02002481 /*
2482 * See whether any cpu is asking for help. Unbounded
2483 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
2484 */
Tejun Heof2e005a2010-07-20 15:59:09 +02002485 for_each_mayday_cpu(cpu, wq->mayday_mask) {
Tejun Heof3421792010-07-02 10:03:51 +02002486 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2487 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002488 struct worker_pool *pool = cwq->pool;
2489 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002490 struct work_struct *work, *n;
2491
2492 __set_current_state(TASK_RUNNING);
Tejun Heof2e005a2010-07-20 15:59:09 +02002493 mayday_clear_cpu(cpu, wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002494
2495 /* migrate to the target cpu if possible */
Tejun Heobd7bdd42012-07-12 14:46:37 -07002496 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002497 worker_maybe_bind_and_lock(rescuer);
2498
2499 /*
2500 * Slurp in all works issued via this workqueue and
2501 * process'em.
2502 */
2503 BUG_ON(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002504 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02002505 if (get_work_cwq(work) == cwq)
2506 move_linked_works(work, scheduled, &n);
2507
2508 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002509
2510 /*
2511 * Leave this gcwq. If keep_working() is %true, notify a
2512 * regular worker; otherwise, we end up with 0 concurrency
2513 * and stalling the execution.
2514 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002515 if (keep_working(pool))
2516 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002517
Tejun Heoe22bee72010-06-29 10:07:14 +02002518 spin_unlock_irq(&gcwq->lock);
2519 }
2520
2521 schedule();
2522 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523}
2524
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002525struct wq_barrier {
2526 struct work_struct work;
2527 struct completion done;
2528};
2529
2530static void wq_barrier_func(struct work_struct *work)
2531{
2532 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2533 complete(&barr->done);
2534}
2535
Tejun Heo4690c4a2010-06-29 10:07:10 +02002536/**
2537 * insert_wq_barrier - insert a barrier work
2538 * @cwq: cwq to insert barrier into
2539 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002540 * @target: target work to attach @barr to
2541 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002542 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002543 * @barr is linked to @target such that @barr is completed only after
2544 * @target finishes execution. Please note that the ordering
2545 * guarantee is observed only with respect to @target and on the local
2546 * cpu.
2547 *
2548 * Currently, a queued barrier can't be canceled. This is because
2549 * try_to_grab_pending() can't determine whether the work to be
2550 * grabbed is at the head of the queue and thus can't clear LINKED
2551 * flag of the previous work while there must be a valid next work
2552 * after a work with LINKED flag set.
2553 *
2554 * Note that when @worker is non-NULL, @target may be modified
2555 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002556 *
2557 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002558 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002559 */
Oleg Nesterov83c22522007-05-09 02:33:54 -07002560static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002561 struct wq_barrier *barr,
2562 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002563{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002564 struct list_head *head;
2565 unsigned int linked = 0;
2566
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002567 /*
Tejun Heo8b03ae32010-06-29 10:07:12 +02002568 * debugobject calls are safe here even with gcwq->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002569 * as we know for sure that this will not trigger any of the
2570 * checks and call back into the fixup functions where we
2571 * might deadlock.
2572 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002573 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002574 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002575 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002576
Tejun Heoaffee4b2010-06-29 10:07:12 +02002577 /*
2578 * If @target is currently being executed, schedule the
2579 * barrier to the worker; otherwise, put it after @target.
2580 */
2581 if (worker)
2582 head = worker->scheduled.next;
2583 else {
2584 unsigned long *bits = work_data_bits(target);
2585
2586 head = target->entry.next;
2587 /* there can already be other linked works, inherit and set */
2588 linked = *bits & WORK_STRUCT_LINKED;
2589 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2590 }
2591
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002592 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002593 insert_work(cwq, &barr->work, head,
2594 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002595}
2596
Tejun Heo73f53c42010-06-29 10:07:11 +02002597/**
2598 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2599 * @wq: workqueue being flushed
2600 * @flush_color: new flush color, < 0 for no-op
2601 * @work_color: new work color, < 0 for no-op
2602 *
2603 * Prepare cwqs for workqueue flushing.
2604 *
2605 * If @flush_color is non-negative, flush_color on all cwqs should be
2606 * -1. If no cwq has in-flight commands at the specified color, all
2607 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2608 * has in flight commands, its cwq->flush_color is set to
2609 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2610 * wakeup logic is armed and %true is returned.
2611 *
2612 * The caller should have initialized @wq->first_flusher prior to
2613 * calling this function with non-negative @flush_color. If
2614 * @flush_color is negative, no flush color update is done and %false
2615 * is returned.
2616 *
2617 * If @work_color is non-negative, all cwqs should have the same
2618 * work_color which is previous to @work_color and all will be
2619 * advanced to @work_color.
2620 *
2621 * CONTEXT:
2622 * mutex_lock(wq->flush_mutex).
2623 *
2624 * RETURNS:
2625 * %true if @flush_color >= 0 and there's something to flush. %false
2626 * otherwise.
2627 */
2628static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2629 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630{
Tejun Heo73f53c42010-06-29 10:07:11 +02002631 bool wait = false;
2632 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -07002633
Tejun Heo73f53c42010-06-29 10:07:11 +02002634 if (flush_color >= 0) {
2635 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2636 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002637 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002638
Tejun Heof3421792010-07-02 10:03:51 +02002639 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002640 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002641 struct global_cwq *gcwq = cwq->pool->gcwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002642
Tejun Heo8b03ae32010-06-29 10:07:12 +02002643 spin_lock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002644
2645 if (flush_color >= 0) {
2646 BUG_ON(cwq->flush_color != -1);
2647
2648 if (cwq->nr_in_flight[flush_color]) {
2649 cwq->flush_color = flush_color;
2650 atomic_inc(&wq->nr_cwqs_to_flush);
2651 wait = true;
2652 }
2653 }
2654
2655 if (work_color >= 0) {
2656 BUG_ON(work_color != work_next_color(cwq->work_color));
2657 cwq->work_color = work_color;
2658 }
2659
Tejun Heo8b03ae32010-06-29 10:07:12 +02002660 spin_unlock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002661 }
2662
2663 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2664 complete(&wq->first_flusher->done);
2665
2666 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667}
2668
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002669/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002671 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 *
2673 * Forces execution of the workqueue and blocks until its completion.
2674 * This is typically used in driver shutdown handlers.
2675 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002676 * We sleep until all works which were queued on entry have been handled,
2677 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002679void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680{
Tejun Heo73f53c42010-06-29 10:07:11 +02002681 struct wq_flusher this_flusher = {
2682 .list = LIST_HEAD_INIT(this_flusher.list),
2683 .flush_color = -1,
2684 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2685 };
2686 int next_color;
Oleg Nesterovb1f4ec172007-05-09 02:34:12 -07002687
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002688 lock_map_acquire(&wq->lockdep_map);
2689 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002690
2691 mutex_lock(&wq->flush_mutex);
2692
2693 /*
2694 * Start-to-wait phase
2695 */
2696 next_color = work_next_color(wq->work_color);
2697
2698 if (next_color != wq->flush_color) {
2699 /*
2700 * Color space is not full. The current work_color
2701 * becomes our flush_color and work_color is advanced
2702 * by one.
2703 */
2704 BUG_ON(!list_empty(&wq->flusher_overflow));
2705 this_flusher.flush_color = wq->work_color;
2706 wq->work_color = next_color;
2707
2708 if (!wq->first_flusher) {
2709 /* no flush in progress, become the first flusher */
2710 BUG_ON(wq->flush_color != this_flusher.flush_color);
2711
2712 wq->first_flusher = &this_flusher;
2713
2714 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2715 wq->work_color)) {
2716 /* nothing to flush, done */
2717 wq->flush_color = next_color;
2718 wq->first_flusher = NULL;
2719 goto out_unlock;
2720 }
2721 } else {
2722 /* wait in queue */
2723 BUG_ON(wq->flush_color == this_flusher.flush_color);
2724 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2725 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2726 }
2727 } else {
2728 /*
2729 * Oops, color space is full, wait on overflow queue.
2730 * The next flush completion will assign us
2731 * flush_color and transfer to flusher_queue.
2732 */
2733 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2734 }
2735
2736 mutex_unlock(&wq->flush_mutex);
2737
2738 wait_for_completion(&this_flusher.done);
2739
2740 /*
2741 * Wake-up-and-cascade phase
2742 *
2743 * First flushers are responsible for cascading flushes and
2744 * handling overflow. Non-first flushers can simply return.
2745 */
2746 if (wq->first_flusher != &this_flusher)
2747 return;
2748
2749 mutex_lock(&wq->flush_mutex);
2750
Tejun Heo4ce48b32010-07-02 10:03:51 +02002751 /* we might have raced, check again with mutex held */
2752 if (wq->first_flusher != &this_flusher)
2753 goto out_unlock;
2754
Tejun Heo73f53c42010-06-29 10:07:11 +02002755 wq->first_flusher = NULL;
2756
2757 BUG_ON(!list_empty(&this_flusher.list));
2758 BUG_ON(wq->flush_color != this_flusher.flush_color);
2759
2760 while (true) {
2761 struct wq_flusher *next, *tmp;
2762
2763 /* complete all the flushers sharing the current flush color */
2764 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2765 if (next->flush_color != wq->flush_color)
2766 break;
2767 list_del_init(&next->list);
2768 complete(&next->done);
2769 }
2770
2771 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2772 wq->flush_color != work_next_color(wq->work_color));
2773
2774 /* this flush_color is finished, advance by one */
2775 wq->flush_color = work_next_color(wq->flush_color);
2776
2777 /* one color has been freed, handle overflow queue */
2778 if (!list_empty(&wq->flusher_overflow)) {
2779 /*
2780 * Assign the same color to all overflowed
2781 * flushers, advance work_color and append to
2782 * flusher_queue. This is the start-to-wait
2783 * phase for these overflowed flushers.
2784 */
2785 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2786 tmp->flush_color = wq->work_color;
2787
2788 wq->work_color = work_next_color(wq->work_color);
2789
2790 list_splice_tail_init(&wq->flusher_overflow,
2791 &wq->flusher_queue);
2792 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2793 }
2794
2795 if (list_empty(&wq->flusher_queue)) {
2796 BUG_ON(wq->flush_color != wq->work_color);
2797 break;
2798 }
2799
2800 /*
2801 * Need to flush more colors. Make the next flusher
2802 * the new first flusher and arm cwqs.
2803 */
2804 BUG_ON(wq->flush_color == wq->work_color);
2805 BUG_ON(wq->flush_color != next->flush_color);
2806
2807 list_del_init(&next->list);
2808 wq->first_flusher = next;
2809
2810 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2811 break;
2812
2813 /*
2814 * Meh... this color is already done, clear first
2815 * flusher and repeat cascading.
2816 */
2817 wq->first_flusher = NULL;
2818 }
2819
2820out_unlock:
2821 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822}
Dave Jonesae90dd52006-06-30 01:40:45 -04002823EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002825/**
2826 * drain_workqueue - drain a workqueue
2827 * @wq: workqueue to drain
2828 *
2829 * Wait until the workqueue becomes empty. While draining is in progress,
2830 * only chain queueing is allowed. IOW, only currently pending or running
2831 * work items on @wq can queue further work items on it. @wq is flushed
2832 * repeatedly until it becomes empty. The number of flushing is detemined
2833 * by the depth of chaining and should be relatively short. Whine if it
2834 * takes too long.
2835 */
2836void drain_workqueue(struct workqueue_struct *wq)
2837{
2838 unsigned int flush_cnt = 0;
2839 unsigned int cpu;
2840
2841 /*
2842 * __queue_work() needs to test whether there are drainers, is much
2843 * hotter than drain_workqueue() and already looks at @wq->flags.
2844 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2845 */
2846 spin_lock(&workqueue_lock);
2847 if (!wq->nr_drainers++)
2848 wq->flags |= WQ_DRAINING;
2849 spin_unlock(&workqueue_lock);
2850reflush:
2851 flush_workqueue(wq);
2852
2853 for_each_cwq_cpu(cpu, wq) {
2854 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002855 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002856
Tejun Heobd7bdd42012-07-12 14:46:37 -07002857 spin_lock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002858 drained = !cwq->nr_active && list_empty(&cwq->delayed_works);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002859 spin_unlock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002860
2861 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002862 continue;
2863
2864 if (++flush_cnt == 10 ||
2865 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
Valentin Ilie044c7822012-08-19 00:52:42 +03002866 pr_warn("workqueue %s: flush on destruction isn't complete after %u tries\n",
2867 wq->name, flush_cnt);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002868 goto reflush;
2869 }
2870
2871 spin_lock(&workqueue_lock);
2872 if (!--wq->nr_drainers)
2873 wq->flags &= ~WQ_DRAINING;
2874 spin_unlock(&workqueue_lock);
2875}
2876EXPORT_SYMBOL_GPL(drain_workqueue);
2877
Tejun Heo606a5022012-08-20 14:51:23 -07002878static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
Tejun Heobaf59022010-09-16 10:42:16 +02002879{
2880 struct worker *worker = NULL;
2881 struct global_cwq *gcwq;
2882 struct cpu_workqueue_struct *cwq;
2883
2884 might_sleep();
2885 gcwq = get_work_gcwq(work);
2886 if (!gcwq)
2887 return false;
2888
2889 spin_lock_irq(&gcwq->lock);
2890 if (!list_empty(&work->entry)) {
2891 /*
2892 * See the comment near try_to_grab_pending()->smp_rmb().
2893 * If it was re-queued to a different gcwq under us, we
2894 * are not going to wait.
2895 */
2896 smp_rmb();
2897 cwq = get_work_cwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002898 if (unlikely(!cwq || gcwq != cwq->pool->gcwq))
Tejun Heobaf59022010-09-16 10:42:16 +02002899 goto already_gone;
Tejun Heo606a5022012-08-20 14:51:23 -07002900 } else {
Tejun Heobaf59022010-09-16 10:42:16 +02002901 worker = find_worker_executing_work(gcwq, work);
2902 if (!worker)
2903 goto already_gone;
2904 cwq = worker->current_cwq;
Tejun Heo606a5022012-08-20 14:51:23 -07002905 }
Tejun Heobaf59022010-09-16 10:42:16 +02002906
2907 insert_wq_barrier(cwq, barr, work, worker);
2908 spin_unlock_irq(&gcwq->lock);
2909
Tejun Heoe159489b2011-01-09 23:32:15 +01002910 /*
2911 * If @max_active is 1 or rescuer is in use, flushing another work
2912 * item on the same workqueue may lead to deadlock. Make sure the
2913 * flusher is not running on the same workqueue by verifying write
2914 * access.
2915 */
2916 if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER)
2917 lock_map_acquire(&cwq->wq->lockdep_map);
2918 else
2919 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heobaf59022010-09-16 10:42:16 +02002920 lock_map_release(&cwq->wq->lockdep_map);
Tejun Heoe159489b2011-01-09 23:32:15 +01002921
Tejun Heobaf59022010-09-16 10:42:16 +02002922 return true;
2923already_gone:
2924 spin_unlock_irq(&gcwq->lock);
2925 return false;
2926}
2927
Oleg Nesterovdb700892008-07-25 01:47:49 -07002928/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002929 * flush_work - wait for a work to finish executing the last queueing instance
2930 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002931 *
Tejun Heo606a5022012-08-20 14:51:23 -07002932 * Wait until @work has finished execution. @work is guaranteed to be idle
2933 * on return if it hasn't been requeued since flush started.
Tejun Heo401a8d02010-09-16 10:36:00 +02002934 *
2935 * RETURNS:
2936 * %true if flush_work() waited for the work to finish execution,
2937 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002938 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002939bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002940{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002941 struct wq_barrier barr;
2942
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002943 lock_map_acquire(&work->lockdep_map);
2944 lock_map_release(&work->lockdep_map);
2945
Tejun Heo606a5022012-08-20 14:51:23 -07002946 if (start_flush_work(work, &barr)) {
Tejun Heobaf59022010-09-16 10:42:16 +02002947 wait_for_completion(&barr.done);
2948 destroy_work_on_stack(&barr.work);
2949 return true;
Tejun Heo606a5022012-08-20 14:51:23 -07002950 } else {
Tejun Heobaf59022010-09-16 10:42:16 +02002951 return false;
Tejun Heo606a5022012-08-20 14:51:23 -07002952 }
Oleg Nesterovdb700892008-07-25 01:47:49 -07002953}
2954EXPORT_SYMBOL_GPL(flush_work);
2955
Tejun Heo36e227d2012-08-03 10:30:46 -07002956static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002957{
Tejun Heobbb68df2012-08-03 10:30:46 -07002958 unsigned long flags;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002959 int ret;
2960
2961 do {
Tejun Heobbb68df2012-08-03 10:30:46 -07002962 ret = try_to_grab_pending(work, is_dwork, &flags);
2963 /*
2964 * If someone else is canceling, wait for the same event it
2965 * would be waiting for before retrying.
2966 */
2967 if (unlikely(ret == -ENOENT))
Tejun Heo606a5022012-08-20 14:51:23 -07002968 flush_work(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002969 } while (unlikely(ret < 0));
2970
Tejun Heobbb68df2012-08-03 10:30:46 -07002971 /* tell other tasks trying to grab @work to back off */
2972 mark_work_canceling(work);
2973 local_irq_restore(flags);
2974
Tejun Heo606a5022012-08-20 14:51:23 -07002975 flush_work(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002976 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002977 return ret;
2978}
2979
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002980/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002981 * cancel_work_sync - cancel a work and wait for it to finish
2982 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002983 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002984 * Cancel @work and wait for its execution to finish. This function
2985 * can be used even if the work re-queues itself or migrates to
2986 * another workqueue. On return from this function, @work is
2987 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002988 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002989 * cancel_work_sync(&delayed_work->work) must not be used for
2990 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002991 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002992 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002993 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002994 *
2995 * RETURNS:
2996 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002997 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002998bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002999{
Tejun Heo36e227d2012-08-03 10:30:46 -07003000 return __cancel_work_timer(work, false);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07003001}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07003002EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07003003
Oleg Nesterov6e84d642007-05-09 02:34:46 -07003004/**
Tejun Heo401a8d02010-09-16 10:36:00 +02003005 * flush_delayed_work - wait for a dwork to finish executing the last queueing
3006 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07003007 *
Tejun Heo401a8d02010-09-16 10:36:00 +02003008 * Delayed timer is cancelled and the pending work is queued for
3009 * immediate execution. Like flush_work(), this function only
3010 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07003011 *
Tejun Heo401a8d02010-09-16 10:36:00 +02003012 * RETURNS:
3013 * %true if flush_work() waited for the work to finish execution,
3014 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07003015 */
Tejun Heo401a8d02010-09-16 10:36:00 +02003016bool flush_delayed_work(struct delayed_work *dwork)
3017{
Tejun Heo8930cab2012-08-03 10:30:45 -07003018 local_irq_disable();
Tejun Heo401a8d02010-09-16 10:36:00 +02003019 if (del_timer_sync(&dwork->timer))
Tejun Heo12650572012-08-08 09:38:42 -07003020 __queue_work(dwork->cpu,
Tejun Heo401a8d02010-09-16 10:36:00 +02003021 get_work_cwq(&dwork->work)->wq, &dwork->work);
Tejun Heo8930cab2012-08-03 10:30:45 -07003022 local_irq_enable();
Tejun Heo401a8d02010-09-16 10:36:00 +02003023 return flush_work(&dwork->work);
3024}
3025EXPORT_SYMBOL(flush_delayed_work);
3026
3027/**
Tejun Heo57b30ae2012-08-21 13:18:24 -07003028 * cancel_delayed_work - cancel a delayed work
3029 * @dwork: delayed_work to cancel
3030 *
3031 * Kill off a pending delayed_work. Returns %true if @dwork was pending
3032 * and canceled; %false if wasn't pending. Note that the work callback
3033 * function may still be running on return, unless it returns %true and the
3034 * work doesn't re-arm itself. Explicitly flush or use
3035 * cancel_delayed_work_sync() to wait on it.
3036 *
3037 * This function is safe to call from any context including IRQ handler.
3038 */
3039bool cancel_delayed_work(struct delayed_work *dwork)
3040{
3041 unsigned long flags;
3042 int ret;
3043
3044 do {
3045 ret = try_to_grab_pending(&dwork->work, true, &flags);
3046 } while (unlikely(ret == -EAGAIN));
3047
3048 if (unlikely(ret < 0))
3049 return false;
3050
3051 set_work_cpu_and_clear_pending(&dwork->work, work_cpu(&dwork->work));
3052 local_irq_restore(flags);
3053 return true;
3054}
3055EXPORT_SYMBOL(cancel_delayed_work);
3056
3057/**
Tejun Heo401a8d02010-09-16 10:36:00 +02003058 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
3059 * @dwork: the delayed work cancel
3060 *
3061 * This is cancel_work_sync() for delayed works.
3062 *
3063 * RETURNS:
3064 * %true if @dwork was pending, %false otherwise.
3065 */
3066bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07003067{
Tejun Heo36e227d2012-08-03 10:30:46 -07003068 return __cancel_work_timer(&dwork->work, true);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07003069}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07003070EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
Tejun Heod4283e92012-08-03 10:30:44 -07003072/**
Tejun Heo0a13c002012-08-03 10:30:44 -07003073 * schedule_work_on - put work task on a specific cpu
3074 * @cpu: cpu to put the work task on
3075 * @work: job to be done
3076 *
3077 * This puts a job on a specific cpu
3078 */
Tejun Heod4283e92012-08-03 10:30:44 -07003079bool schedule_work_on(int cpu, struct work_struct *work)
Tejun Heo0a13c002012-08-03 10:30:44 -07003080{
3081 return queue_work_on(cpu, system_wq, work);
3082}
3083EXPORT_SYMBOL(schedule_work_on);
3084
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003085/**
3086 * schedule_work - put work task in global workqueue
3087 * @work: job to be done
3088 *
Tejun Heod4283e92012-08-03 10:30:44 -07003089 * Returns %false if @work was already on the kernel-global workqueue and
3090 * %true otherwise.
Bart Van Assche5b0f437d2009-07-30 19:00:53 +02003091 *
3092 * This puts a job in the kernel-global workqueue if it was not already
3093 * queued and leaves it in the same position on the kernel-global
3094 * workqueue otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003095 */
Tejun Heod4283e92012-08-03 10:30:44 -07003096bool schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097{
Tejun Heod320c032010-06-29 10:07:14 +02003098 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099}
Dave Jonesae90dd52006-06-30 01:40:45 -04003100EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003102/**
3103 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
3104 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +00003105 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003106 * @delay: number of jiffies to wait
3107 *
3108 * After waiting for a given time this puts a job in the kernel-global
3109 * workqueue on the specified CPU.
3110 */
Tejun Heod4283e92012-08-03 10:30:44 -07003111bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork,
3112 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113{
Tejun Heod320c032010-06-29 10:07:14 +02003114 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115}
Dave Jonesae90dd52006-06-30 01:40:45 -04003116EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
Andrew Mortonb6136772006-06-25 05:47:49 -07003118/**
Tejun Heo0a13c002012-08-03 10:30:44 -07003119 * schedule_delayed_work - put work task in global workqueue after delay
3120 * @dwork: job to be done
3121 * @delay: number of jiffies to wait or 0 for immediate execution
3122 *
3123 * After waiting for a given time this puts a job in the kernel-global
3124 * workqueue.
3125 */
Tejun Heod4283e92012-08-03 10:30:44 -07003126bool schedule_delayed_work(struct delayed_work *dwork, unsigned long delay)
Tejun Heo0a13c002012-08-03 10:30:44 -07003127{
3128 return queue_delayed_work(system_wq, dwork, delay);
3129}
3130EXPORT_SYMBOL(schedule_delayed_work);
3131
3132/**
Tejun Heo31ddd872010-10-19 11:14:49 +02003133 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07003134 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07003135 *
Tejun Heo31ddd872010-10-19 11:14:49 +02003136 * schedule_on_each_cpu() executes @func on each online CPU using the
3137 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07003138 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02003139 *
3140 * RETURNS:
3141 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07003142 */
David Howells65f27f32006-11-22 14:55:48 +00003143int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba82006-01-08 01:00:43 -08003144{
3145 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02003146 struct work_struct __percpu *works;
Christoph Lameter15316ba82006-01-08 01:00:43 -08003147
Andrew Mortonb6136772006-06-25 05:47:49 -07003148 works = alloc_percpu(struct work_struct);
3149 if (!works)
Christoph Lameter15316ba82006-01-08 01:00:43 -08003150 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07003151
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003152 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08003153
Christoph Lameter15316ba82006-01-08 01:00:43 -08003154 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01003155 struct work_struct *work = per_cpu_ptr(works, cpu);
3156
3157 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003158 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02003159 }
Tejun Heo93981802009-11-17 14:06:20 -08003160
3161 for_each_online_cpu(cpu)
3162 flush_work(per_cpu_ptr(works, cpu));
3163
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003164 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07003165 free_percpu(works);
Christoph Lameter15316ba82006-01-08 01:00:43 -08003166 return 0;
3167}
3168
Alan Sterneef6a7d2010-02-12 17:39:21 +09003169/**
3170 * flush_scheduled_work - ensure that any scheduled work has run to completion.
3171 *
3172 * Forces execution of the kernel-global workqueue and blocks until its
3173 * completion.
3174 *
3175 * Think twice before calling this function! It's very easy to get into
3176 * trouble if you don't take great care. Either of the following situations
3177 * will lead to deadlock:
3178 *
3179 * One of the work items currently on the workqueue needs to acquire
3180 * a lock held by your code or its caller.
3181 *
3182 * Your code is running in the context of a work routine.
3183 *
3184 * They will be detected by lockdep when they occur, but the first might not
3185 * occur very often. It depends on what work items are on the workqueue and
3186 * what locks they need, which you have no control over.
3187 *
3188 * In most situations flushing the entire workqueue is overkill; you merely
3189 * need to know that a particular work item isn't queued and isn't running.
3190 * In such cases you should use cancel_delayed_work_sync() or
3191 * cancel_work_sync() instead.
3192 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193void flush_scheduled_work(void)
3194{
Tejun Heod320c032010-06-29 10:07:14 +02003195 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196}
Dave Jonesae90dd52006-06-30 01:40:45 -04003197EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198
3199/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06003200 * execute_in_process_context - reliably execute the routine with user context
3201 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06003202 * @ew: guaranteed storage for the execute work structure (must
3203 * be available when the work executes)
3204 *
3205 * Executes the function immediately if process context is available,
3206 * otherwise schedules the function for delayed execution.
3207 *
3208 * Returns: 0 - function was executed
3209 * 1 - function was scheduled for execution
3210 */
David Howells65f27f32006-11-22 14:55:48 +00003211int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06003212{
3213 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00003214 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003215 return 0;
3216 }
3217
David Howells65f27f32006-11-22 14:55:48 +00003218 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003219 schedule_work(&ew->work);
3220
3221 return 1;
3222}
3223EXPORT_SYMBOL_GPL(execute_in_process_context);
3224
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225int keventd_up(void)
3226{
Tejun Heod320c032010-06-29 10:07:14 +02003227 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228}
3229
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003230static int alloc_cwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231{
Oleg Nesterov3af244332007-05-09 02:34:09 -07003232 /*
Tejun Heo0f900042010-06-29 10:07:11 +02003233 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
3234 * Make sure that the alignment isn't lower than that of
3235 * unsigned long long.
Oleg Nesterov3af244332007-05-09 02:34:09 -07003236 */
Tejun Heo0f900042010-06-29 10:07:11 +02003237 const size_t size = sizeof(struct cpu_workqueue_struct);
3238 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
3239 __alignof__(unsigned long long));
Oleg Nesterov3af244332007-05-09 02:34:09 -07003240
Lai Jiangshane06ffa12012-03-09 18:03:20 +08003241 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02003242 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
Tejun Heo931ac772010-07-20 11:07:48 +02003243 else {
Tejun Heof3421792010-07-02 10:03:51 +02003244 void *ptr;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01003245
Tejun Heof3421792010-07-02 10:03:51 +02003246 /*
3247 * Allocate enough room to align cwq and put an extra
3248 * pointer at the end pointing back to the originally
3249 * allocated pointer which will be used for free.
3250 */
3251 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
3252 if (ptr) {
3253 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
3254 *(void **)(wq->cpu_wq.single + 1) = ptr;
3255 }
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003256 }
Tejun Heof3421792010-07-02 10:03:51 +02003257
Tejun Heo0415b00d12011-03-24 18:50:09 +01003258 /* just in case, make sure it's actually aligned */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003259 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
3260 return wq->cpu_wq.v ? 0 : -ENOMEM;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003261}
3262
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003263static void free_cwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07003264{
Lai Jiangshane06ffa12012-03-09 18:03:20 +08003265 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02003266 free_percpu(wq->cpu_wq.pcpu);
3267 else if (wq->cpu_wq.single) {
3268 /* the pointer to free is stored right after the cwq */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003269 kfree(*(void **)(wq->cpu_wq.single + 1));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07003270 }
3271}
3272
Tejun Heof3421792010-07-02 10:03:51 +02003273static int wq_clamp_max_active(int max_active, unsigned int flags,
3274 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003275{
Tejun Heof3421792010-07-02 10:03:51 +02003276 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3277
3278 if (max_active < 1 || max_active > lim)
Valentin Ilie044c7822012-08-19 00:52:42 +03003279 pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
3280 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003281
Tejun Heof3421792010-07-02 10:03:51 +02003282 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003283}
3284
Tejun Heob196be82012-01-10 15:11:35 -08003285struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02003286 unsigned int flags,
3287 int max_active,
3288 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08003289 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003290{
Tejun Heob196be82012-01-10 15:11:35 -08003291 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003292 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02003293 unsigned int cpu;
Tejun Heob196be82012-01-10 15:11:35 -08003294 size_t namelen;
3295
3296 /* determine namelen, allocate wq and format name */
3297 va_start(args, lock_name);
3298 va_copy(args1, args);
3299 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3300
3301 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3302 if (!wq)
3303 goto err;
3304
3305 vsnprintf(wq->name, namelen, fmt, args1);
3306 va_end(args);
3307 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003308
Tejun Heof3421792010-07-02 10:03:51 +02003309 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02003310 * Workqueues which may be used during memory reclaim should
3311 * have a rescuer to guarantee forward progress.
3312 */
3313 if (flags & WQ_MEM_RECLAIM)
3314 flags |= WQ_RESCUER;
3315
Tejun Heod320c032010-06-29 10:07:14 +02003316 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003317 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003318
Tejun Heob196be82012-01-10 15:11:35 -08003319 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003320 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003321 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003322 mutex_init(&wq->flush_mutex);
3323 atomic_set(&wq->nr_cwqs_to_flush, 0);
3324 INIT_LIST_HEAD(&wq->flusher_queue);
3325 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003326
Johannes Bergeb13ba82008-01-16 09:51:58 +01003327 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003328 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003329
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003330 if (alloc_cwqs(wq) < 0)
3331 goto err;
3332
Tejun Heof3421792010-07-02 10:03:51 +02003333 for_each_cwq_cpu(cpu, wq) {
Tejun Heo15376632010-06-29 10:07:11 +02003334 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003335 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo32704762012-07-13 22:16:45 -07003336 int pool_idx = (bool)(flags & WQ_HIGHPRI);
Tejun Heo15376632010-06-29 10:07:11 +02003337
Tejun Heo0f900042010-06-29 10:07:11 +02003338 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heo32704762012-07-13 22:16:45 -07003339 cwq->pool = &gcwq->pools[pool_idx];
Tejun Heoc34056a2010-06-29 10:07:11 +02003340 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02003341 cwq->flush_color = -1;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003342 cwq->max_active = max_active;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003343 INIT_LIST_HEAD(&cwq->delayed_works);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003344 }
3345
Tejun Heoe22bee72010-06-29 10:07:14 +02003346 if (flags & WQ_RESCUER) {
3347 struct worker *rescuer;
3348
Tejun Heof2e005a2010-07-20 15:59:09 +02003349 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
Tejun Heoe22bee72010-06-29 10:07:14 +02003350 goto err;
3351
3352 wq->rescuer = rescuer = alloc_worker();
3353 if (!rescuer)
3354 goto err;
3355
Tejun Heob196be82012-01-10 15:11:35 -08003356 rescuer->task = kthread_create(rescuer_thread, wq, "%s",
3357 wq->name);
Tejun Heoe22bee72010-06-29 10:07:14 +02003358 if (IS_ERR(rescuer->task))
3359 goto err;
3360
Tejun Heoe22bee72010-06-29 10:07:14 +02003361 rescuer->task->flags |= PF_THREAD_BOUND;
3362 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003363 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003364
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003365 /*
3366 * workqueue_lock protects global freeze state and workqueues
3367 * list. Grab it, set max_active accordingly and add the new
3368 * workqueue to workqueues list.
3369 */
Tejun Heo15376632010-06-29 10:07:11 +02003370 spin_lock(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003371
Tejun Heo58a69cb2011-02-16 09:25:31 +01003372 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heof3421792010-07-02 10:03:51 +02003373 for_each_cwq_cpu(cpu, wq)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003374 get_cwq(cpu, wq)->max_active = 0;
3375
Tejun Heo15376632010-06-29 10:07:11 +02003376 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003377
Tejun Heo15376632010-06-29 10:07:11 +02003378 spin_unlock(&workqueue_lock);
3379
Oleg Nesterov3af244332007-05-09 02:34:09 -07003380 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02003381err:
3382 if (wq) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003383 free_cwqs(wq);
Tejun Heof2e005a2010-07-20 15:59:09 +02003384 free_mayday_mask(wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02003385 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003386 kfree(wq);
3387 }
3388 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003389}
Tejun Heod320c032010-06-29 10:07:14 +02003390EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003391
3392/**
3393 * destroy_workqueue - safely terminate a workqueue
3394 * @wq: target workqueue
3395 *
3396 * Safely destroy a workqueue. All work currently pending will be done first.
3397 */
3398void destroy_workqueue(struct workqueue_struct *wq)
3399{
Tejun Heoc8e55f32010-06-29 10:07:12 +02003400 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003401
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003402 /* drain it before proceeding with destruction */
3403 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003404
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003405 /*
3406 * wq list is used to freeze wq, remove from list after
3407 * flushing is complete in case freeze races us.
3408 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003409 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec172007-05-09 02:34:12 -07003410 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003411 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003412
Tejun Heoe22bee72010-06-29 10:07:14 +02003413 /* sanity check */
Tejun Heof3421792010-07-02 10:03:51 +02003414 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02003415 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3416 int i;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003417
Tejun Heo73f53c42010-06-29 10:07:11 +02003418 for (i = 0; i < WORK_NR_COLORS; i++)
3419 BUG_ON(cwq->nr_in_flight[i]);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003420 BUG_ON(cwq->nr_active);
3421 BUG_ON(!list_empty(&cwq->delayed_works));
Tejun Heo73f53c42010-06-29 10:07:11 +02003422 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003423
Tejun Heoe22bee72010-06-29 10:07:14 +02003424 if (wq->flags & WQ_RESCUER) {
3425 kthread_stop(wq->rescuer->task);
Tejun Heof2e005a2010-07-20 15:59:09 +02003426 free_mayday_mask(wq->mayday_mask);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003427 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003428 }
3429
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003430 free_cwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003431 kfree(wq);
3432}
3433EXPORT_SYMBOL_GPL(destroy_workqueue);
3434
Tejun Heodcd989c2010-06-29 10:07:14 +02003435/**
3436 * workqueue_set_max_active - adjust max_active of a workqueue
3437 * @wq: target workqueue
3438 * @max_active: new max_active value.
3439 *
3440 * Set max_active of @wq to @max_active.
3441 *
3442 * CONTEXT:
3443 * Don't call from IRQ context.
3444 */
3445void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3446{
3447 unsigned int cpu;
3448
Tejun Heof3421792010-07-02 10:03:51 +02003449 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003450
3451 spin_lock(&workqueue_lock);
3452
3453 wq->saved_max_active = max_active;
3454
Tejun Heof3421792010-07-02 10:03:51 +02003455 for_each_cwq_cpu(cpu, wq) {
Tejun Heodcd989c2010-06-29 10:07:14 +02003456 struct global_cwq *gcwq = get_gcwq(cpu);
3457
3458 spin_lock_irq(&gcwq->lock);
3459
Tejun Heo58a69cb2011-02-16 09:25:31 +01003460 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heodcd989c2010-06-29 10:07:14 +02003461 !(gcwq->flags & GCWQ_FREEZING))
3462 get_cwq(gcwq->cpu, wq)->max_active = max_active;
3463
3464 spin_unlock_irq(&gcwq->lock);
3465 }
3466
3467 spin_unlock(&workqueue_lock);
3468}
3469EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3470
3471/**
3472 * workqueue_congested - test whether a workqueue is congested
3473 * @cpu: CPU in question
3474 * @wq: target workqueue
3475 *
3476 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3477 * no synchronization around this function and the test result is
3478 * unreliable and only useful as advisory hints or for debugging.
3479 *
3480 * RETURNS:
3481 * %true if congested, %false otherwise.
3482 */
3483bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3484{
3485 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3486
3487 return !list_empty(&cwq->delayed_works);
3488}
3489EXPORT_SYMBOL_GPL(workqueue_congested);
3490
3491/**
3492 * work_cpu - return the last known associated cpu for @work
3493 * @work: the work of interest
3494 *
3495 * RETURNS:
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003496 * CPU number if @work was ever queued. WORK_CPU_NONE otherwise.
Tejun Heodcd989c2010-06-29 10:07:14 +02003497 */
3498unsigned int work_cpu(struct work_struct *work)
3499{
3500 struct global_cwq *gcwq = get_work_gcwq(work);
3501
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003502 return gcwq ? gcwq->cpu : WORK_CPU_NONE;
Tejun Heodcd989c2010-06-29 10:07:14 +02003503}
3504EXPORT_SYMBOL_GPL(work_cpu);
3505
3506/**
3507 * work_busy - test whether a work is currently pending or running
3508 * @work: the work to be tested
3509 *
3510 * Test whether @work is currently pending or running. There is no
3511 * synchronization around this function and the test result is
3512 * unreliable and only useful as advisory hints or for debugging.
3513 * Especially for reentrant wqs, the pending state might hide the
3514 * running state.
3515 *
3516 * RETURNS:
3517 * OR'd bitmask of WORK_BUSY_* bits.
3518 */
3519unsigned int work_busy(struct work_struct *work)
3520{
3521 struct global_cwq *gcwq = get_work_gcwq(work);
3522 unsigned long flags;
3523 unsigned int ret = 0;
3524
3525 if (!gcwq)
3526 return false;
3527
3528 spin_lock_irqsave(&gcwq->lock, flags);
3529
3530 if (work_pending(work))
3531 ret |= WORK_BUSY_PENDING;
3532 if (find_worker_executing_work(gcwq, work))
3533 ret |= WORK_BUSY_RUNNING;
3534
3535 spin_unlock_irqrestore(&gcwq->lock, flags);
3536
3537 return ret;
3538}
3539EXPORT_SYMBOL_GPL(work_busy);
3540
Tejun Heodb7bccf2010-06-29 10:07:12 +02003541/*
3542 * CPU hotplug.
3543 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003544 * There are two challenges in supporting CPU hotplug. Firstly, there
3545 * are a lot of assumptions on strong associations among work, cwq and
3546 * gcwq which make migrating pending and scheduled works very
3547 * difficult to implement without impacting hot paths. Secondly,
3548 * gcwqs serve mix of short, long and very long running works making
3549 * blocked draining impractical.
3550 *
Tejun Heo628c78e2012-07-17 12:39:27 -07003551 * This is solved by allowing a gcwq to be disassociated from the CPU
3552 * running as an unbound one and allowing it to be reattached later if the
3553 * cpu comes back online.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003554 */
3555
Tejun Heo60373152012-07-17 12:39:27 -07003556/* claim manager positions of all pools */
Tejun Heo8db25e72012-07-17 12:39:28 -07003557static void gcwq_claim_management_and_lock(struct global_cwq *gcwq)
Tejun Heo60373152012-07-17 12:39:27 -07003558{
3559 struct worker_pool *pool;
3560
3561 for_each_worker_pool(pool, gcwq)
3562 mutex_lock_nested(&pool->manager_mutex, pool - gcwq->pools);
Tejun Heo8db25e72012-07-17 12:39:28 -07003563 spin_lock_irq(&gcwq->lock);
Tejun Heo60373152012-07-17 12:39:27 -07003564}
3565
3566/* release manager positions */
Tejun Heo8db25e72012-07-17 12:39:28 -07003567static void gcwq_release_management_and_unlock(struct global_cwq *gcwq)
Tejun Heo60373152012-07-17 12:39:27 -07003568{
3569 struct worker_pool *pool;
3570
Tejun Heo8db25e72012-07-17 12:39:28 -07003571 spin_unlock_irq(&gcwq->lock);
Tejun Heo60373152012-07-17 12:39:27 -07003572 for_each_worker_pool(pool, gcwq)
3573 mutex_unlock(&pool->manager_mutex);
3574}
3575
Tejun Heo628c78e2012-07-17 12:39:27 -07003576static void gcwq_unbind_fn(struct work_struct *work)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003577{
Tejun Heo628c78e2012-07-17 12:39:27 -07003578 struct global_cwq *gcwq = get_gcwq(smp_processor_id());
Tejun Heo4ce62e92012-07-13 22:16:44 -07003579 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003580 struct worker *worker;
3581 struct hlist_node *pos;
3582 int i;
3583
3584 BUG_ON(gcwq->cpu != smp_processor_id());
3585
Tejun Heo8db25e72012-07-17 12:39:28 -07003586 gcwq_claim_management_and_lock(gcwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02003587
Tejun Heof2d5a0e2012-07-17 12:39:26 -07003588 /*
3589 * We've claimed all manager positions. Make all workers unbound
3590 * and set DISASSOCIATED. Before this, all workers except for the
3591 * ones which are still executing works from before the last CPU
3592 * down must be on the cpu. After this, they may become diasporas.
3593 */
Tejun Heo60373152012-07-17 12:39:27 -07003594 for_each_worker_pool(pool, gcwq)
Tejun Heo4ce62e92012-07-13 22:16:44 -07003595 list_for_each_entry(worker, &pool->idle_list, entry)
Tejun Heo403c8212012-07-17 12:39:27 -07003596 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003597
3598 for_each_busy_worker(worker, i, pos, gcwq)
Tejun Heo403c8212012-07-17 12:39:27 -07003599 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003600
Tejun Heof2d5a0e2012-07-17 12:39:26 -07003601 gcwq->flags |= GCWQ_DISASSOCIATED;
3602
Tejun Heo8db25e72012-07-17 12:39:28 -07003603 gcwq_release_management_and_unlock(gcwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02003604
3605 /*
Tejun Heo628c78e2012-07-17 12:39:27 -07003606 * Call schedule() so that we cross rq->lock and thus can guarantee
3607 * sched callbacks see the %WORKER_UNBOUND flag. This is necessary
3608 * as scheduler callbacks may be invoked from other cpus.
3609 */
3610 schedule();
3611
3612 /*
3613 * Sched callbacks are disabled now. Zap nr_running. After this,
3614 * nr_running stays zero and need_more_worker() and keep_working()
3615 * are always true as long as the worklist is not empty. @gcwq now
3616 * behaves as unbound (in terms of concurrency management) gcwq
3617 * which is served by workers tied to the CPU.
3618 *
3619 * On return from this function, the current worker would trigger
3620 * unbound chain execution of pending work items if other workers
3621 * didn't already.
Tejun Heoe22bee72010-06-29 10:07:14 +02003622 */
Tejun Heo4ce62e92012-07-13 22:16:44 -07003623 for_each_worker_pool(pool, gcwq)
3624 atomic_set(get_pool_nr_running(pool), 0);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003625}
3626
Tejun Heo8db25e72012-07-17 12:39:28 -07003627/*
3628 * Workqueues should be brought up before normal priority CPU notifiers.
3629 * This will be registered high priority CPU notifier.
3630 */
3631static int __devinit workqueue_cpu_up_callback(struct notifier_block *nfb,
3632 unsigned long action,
3633 void *hcpu)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003634{
3635 unsigned int cpu = (unsigned long)hcpu;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003636 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003637 struct worker_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638
Tejun Heo8db25e72012-07-17 12:39:28 -07003639 switch (action & ~CPU_TASKS_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 case CPU_UP_PREPARE:
Tejun Heo4ce62e92012-07-13 22:16:44 -07003641 for_each_worker_pool(pool, gcwq) {
Tejun Heo3ce63372012-07-17 12:39:27 -07003642 struct worker *worker;
3643
3644 if (pool->nr_workers)
3645 continue;
3646
3647 worker = create_worker(pool);
3648 if (!worker)
3649 return NOTIFY_BAD;
3650
3651 spin_lock_irq(&gcwq->lock);
3652 start_worker(worker);
3653 spin_unlock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003655 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003656
Tejun Heo65758202012-07-17 12:39:26 -07003657 case CPU_DOWN_FAILED:
3658 case CPU_ONLINE:
Tejun Heo8db25e72012-07-17 12:39:28 -07003659 gcwq_claim_management_and_lock(gcwq);
3660 gcwq->flags &= ~GCWQ_DISASSOCIATED;
3661 rebind_workers(gcwq);
3662 gcwq_release_management_and_unlock(gcwq);
3663 break;
Tejun Heo65758202012-07-17 12:39:26 -07003664 }
3665 return NOTIFY_OK;
3666}
3667
3668/*
3669 * Workqueues should be brought down after normal priority CPU notifiers.
3670 * This will be registered as low priority CPU notifier.
3671 */
3672static int __devinit workqueue_cpu_down_callback(struct notifier_block *nfb,
3673 unsigned long action,
3674 void *hcpu)
3675{
Tejun Heo8db25e72012-07-17 12:39:28 -07003676 unsigned int cpu = (unsigned long)hcpu;
3677 struct work_struct unbind_work;
3678
Tejun Heo65758202012-07-17 12:39:26 -07003679 switch (action & ~CPU_TASKS_FROZEN) {
3680 case CPU_DOWN_PREPARE:
Tejun Heo8db25e72012-07-17 12:39:28 -07003681 /* unbinding should happen on the local CPU */
3682 INIT_WORK_ONSTACK(&unbind_work, gcwq_unbind_fn);
Joonsoo Kim7635d2f2012-08-15 23:25:41 +09003683 queue_work_on(cpu, system_highpri_wq, &unbind_work);
Tejun Heo8db25e72012-07-17 12:39:28 -07003684 flush_work(&unbind_work);
3685 break;
Tejun Heo65758202012-07-17 12:39:26 -07003686 }
3687 return NOTIFY_OK;
3688}
3689
Rusty Russell2d3854a2008-11-05 13:39:10 +11003690#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003691
Rusty Russell2d3854a2008-11-05 13:39:10 +11003692struct work_for_cpu {
Andrew Morton6b44003e2009-04-09 09:50:37 -06003693 struct completion completion;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003694 long (*fn)(void *);
3695 void *arg;
3696 long ret;
3697};
3698
Andrew Morton6b44003e2009-04-09 09:50:37 -06003699static int do_work_for_cpu(void *_wfc)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003700{
Andrew Morton6b44003e2009-04-09 09:50:37 -06003701 struct work_for_cpu *wfc = _wfc;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003702 wfc->ret = wfc->fn(wfc->arg);
Andrew Morton6b44003e2009-04-09 09:50:37 -06003703 complete(&wfc->completion);
3704 return 0;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003705}
3706
3707/**
3708 * work_on_cpu - run a function in user context on a particular cpu
3709 * @cpu: the cpu to run on
3710 * @fn: the function to run
3711 * @arg: the function arg
3712 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003713 * This will return the value @fn returns.
3714 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b44003e2009-04-09 09:50:37 -06003715 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003716 */
3717long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3718{
Andrew Morton6b44003e2009-04-09 09:50:37 -06003719 struct task_struct *sub_thread;
3720 struct work_for_cpu wfc = {
3721 .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
3722 .fn = fn,
3723 .arg = arg,
3724 };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003725
Andrew Morton6b44003e2009-04-09 09:50:37 -06003726 sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
3727 if (IS_ERR(sub_thread))
3728 return PTR_ERR(sub_thread);
3729 kthread_bind(sub_thread, cpu);
3730 wake_up_process(sub_thread);
3731 wait_for_completion(&wfc.completion);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003732 return wfc.ret;
3733}
3734EXPORT_SYMBOL_GPL(work_on_cpu);
3735#endif /* CONFIG_SMP */
3736
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003737#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303738
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003739/**
3740 * freeze_workqueues_begin - begin freezing workqueues
3741 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003742 * Start freezing workqueues. After this function returns, all freezable
3743 * workqueues will queue new works to their frozen_works list instead of
3744 * gcwq->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003745 *
3746 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003747 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003748 */
3749void freeze_workqueues_begin(void)
3750{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003751 unsigned int cpu;
3752
3753 spin_lock(&workqueue_lock);
3754
3755 BUG_ON(workqueue_freezing);
3756 workqueue_freezing = true;
3757
Tejun Heof3421792010-07-02 10:03:51 +02003758 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003759 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003760 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003761
3762 spin_lock_irq(&gcwq->lock);
3763
Tejun Heodb7bccf2010-06-29 10:07:12 +02003764 BUG_ON(gcwq->flags & GCWQ_FREEZING);
3765 gcwq->flags |= GCWQ_FREEZING;
3766
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003767 list_for_each_entry(wq, &workqueues, list) {
3768 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3769
Tejun Heo58a69cb2011-02-16 09:25:31 +01003770 if (cwq && wq->flags & WQ_FREEZABLE)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003771 cwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003772 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003773
3774 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003775 }
3776
3777 spin_unlock(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003779
3780/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003781 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003782 *
3783 * Check whether freezing is complete. This function must be called
3784 * between freeze_workqueues_begin() and thaw_workqueues().
3785 *
3786 * CONTEXT:
3787 * Grabs and releases workqueue_lock.
3788 *
3789 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01003790 * %true if some freezable workqueues are still busy. %false if freezing
3791 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003792 */
3793bool freeze_workqueues_busy(void)
3794{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003795 unsigned int cpu;
3796 bool busy = false;
3797
3798 spin_lock(&workqueue_lock);
3799
3800 BUG_ON(!workqueue_freezing);
3801
Tejun Heof3421792010-07-02 10:03:51 +02003802 for_each_gcwq_cpu(cpu) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003803 struct workqueue_struct *wq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003804 /*
3805 * nr_active is monotonically decreasing. It's safe
3806 * to peek without lock.
3807 */
3808 list_for_each_entry(wq, &workqueues, list) {
3809 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3810
Tejun Heo58a69cb2011-02-16 09:25:31 +01003811 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003812 continue;
3813
3814 BUG_ON(cwq->nr_active < 0);
3815 if (cwq->nr_active) {
3816 busy = true;
3817 goto out_unlock;
3818 }
3819 }
3820 }
3821out_unlock:
3822 spin_unlock(&workqueue_lock);
3823 return busy;
3824}
3825
3826/**
3827 * thaw_workqueues - thaw workqueues
3828 *
3829 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo7e116292010-06-29 10:07:13 +02003830 * frozen works are transferred to their respective gcwq worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003831 *
3832 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003833 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003834 */
3835void thaw_workqueues(void)
3836{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003837 unsigned int cpu;
3838
3839 spin_lock(&workqueue_lock);
3840
3841 if (!workqueue_freezing)
3842 goto out_unlock;
3843
Tejun Heof3421792010-07-02 10:03:51 +02003844 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003845 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003846 struct worker_pool *pool;
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003847 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003848
3849 spin_lock_irq(&gcwq->lock);
3850
Tejun Heodb7bccf2010-06-29 10:07:12 +02003851 BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
3852 gcwq->flags &= ~GCWQ_FREEZING;
3853
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003854 list_for_each_entry(wq, &workqueues, list) {
3855 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3856
Tejun Heo58a69cb2011-02-16 09:25:31 +01003857 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003858 continue;
3859
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003860 /* restore max_active and repopulate worklist */
3861 cwq->max_active = wq->saved_max_active;
3862
3863 while (!list_empty(&cwq->delayed_works) &&
3864 cwq->nr_active < cwq->max_active)
3865 cwq_activate_first_delayed(cwq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003866 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003867
Tejun Heo4ce62e92012-07-13 22:16:44 -07003868 for_each_worker_pool(pool, gcwq)
3869 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02003870
Tejun Heo8b03ae32010-06-29 10:07:12 +02003871 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003872 }
3873
3874 workqueue_freezing = false;
3875out_unlock:
3876 spin_unlock(&workqueue_lock);
3877}
3878#endif /* CONFIG_FREEZER */
3879
Suresh Siddha6ee05782010-07-30 14:57:37 -07003880static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881{
Tejun Heoc34056a2010-06-29 10:07:11 +02003882 unsigned int cpu;
Tejun Heoc8e55f32010-06-29 10:07:12 +02003883 int i;
Tejun Heoc34056a2010-06-29 10:07:11 +02003884
Tejun Heob5490072012-08-03 10:30:46 -07003885 /* make sure we have enough bits for OFFQ CPU number */
3886 BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_CPU_SHIFT)) <
3887 WORK_CPU_LAST);
3888
Tejun Heo65758202012-07-17 12:39:26 -07003889 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
3890 cpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003891
3892 /* initialize gcwqs */
Tejun Heof3421792010-07-02 10:03:51 +02003893 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003894 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003895 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003896
3897 spin_lock_init(&gcwq->lock);
3898 gcwq->cpu = cpu;
Tejun Heo477a3c32010-08-31 10:54:35 +02003899 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003900
Tejun Heoc8e55f32010-06-29 10:07:12 +02003901 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
3902 INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
3903
Tejun Heo4ce62e92012-07-13 22:16:44 -07003904 for_each_worker_pool(pool, gcwq) {
3905 pool->gcwq = gcwq;
3906 INIT_LIST_HEAD(&pool->worklist);
3907 INIT_LIST_HEAD(&pool->idle_list);
Tejun Heoe22bee72010-06-29 10:07:14 +02003908
Tejun Heo4ce62e92012-07-13 22:16:44 -07003909 init_timer_deferrable(&pool->idle_timer);
3910 pool->idle_timer.function = idle_worker_timeout;
3911 pool->idle_timer.data = (unsigned long)pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003912
Tejun Heo4ce62e92012-07-13 22:16:44 -07003913 setup_timer(&pool->mayday_timer, gcwq_mayday_timeout,
3914 (unsigned long)pool);
3915
Tejun Heo60373152012-07-17 12:39:27 -07003916 mutex_init(&pool->manager_mutex);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003917 ida_init(&pool->worker_ida);
3918 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003919
Tejun Heo25511a42012-07-17 12:39:27 -07003920 init_waitqueue_head(&gcwq->rebind_hold);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003921 }
3922
Tejun Heoe22bee72010-06-29 10:07:14 +02003923 /* create the initial worker */
Tejun Heof3421792010-07-02 10:03:51 +02003924 for_each_online_gcwq_cpu(cpu) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003925 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003926 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003927
Tejun Heo477a3c32010-08-31 10:54:35 +02003928 if (cpu != WORK_CPU_UNBOUND)
3929 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003930
3931 for_each_worker_pool(pool, gcwq) {
3932 struct worker *worker;
3933
Tejun Heobc2ae0f2012-07-17 12:39:27 -07003934 worker = create_worker(pool);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003935 BUG_ON(!worker);
3936 spin_lock_irq(&gcwq->lock);
3937 start_worker(worker);
3938 spin_unlock_irq(&gcwq->lock);
3939 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003940 }
3941
Tejun Heod320c032010-06-29 10:07:14 +02003942 system_wq = alloc_workqueue("events", 0, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09003943 system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
Tejun Heod320c032010-06-29 10:07:14 +02003944 system_long_wq = alloc_workqueue("events_long", 0, 0);
Tejun Heof3421792010-07-02 10:03:51 +02003945 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3946 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01003947 system_freezable_wq = alloc_workqueue("events_freezable",
3948 WQ_FREEZABLE, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09003949 BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
Tejun Heoae930e02012-08-20 14:51:23 -07003950 !system_unbound_wq || !system_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07003951 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952}
Suresh Siddha6ee05782010-07-30 14:57:37 -07003953early_initcall(init_workqueues);