blob: 770c1a8128bfdfe9bb88dae8300427a5b71d4c0c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tejun Heoc54fce62010-09-10 16:51:36 +02002 * kernel/workqueue.c - generic async execution with shared worker pool
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Tejun Heoc54fce62010-09-10 16:51:36 +02004 * Copyright (C) 2002 Ingo Molnar
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Tejun Heoc54fce62010-09-10 16:51:36 +02006 * Derived from the taskqueue/keventd code by:
7 * David Woodhouse <dwmw2@infradead.org>
8 * Andrew Morton
9 * Kai Petzke <wpp@marie.physik.tu-berlin.de>
10 * Theodore Ts'o <tytso@mit.edu>
Christoph Lameter89ada672005-10-30 15:01:59 -080011 *
Christoph Lametercde53532008-07-04 09:59:22 -070012 * Made to use alloc_percpu by Christoph Lameter.
Tejun Heoc54fce62010-09-10 16:51:36 +020013 *
14 * Copyright (C) 2010 SUSE Linux Products GmbH
15 * Copyright (C) 2010 Tejun Heo <tj@kernel.org>
16 *
17 * This is the generic async execution mechanism. Work items as are
18 * executed in process context. The worker pool is shared and
19 * automatically managed. There is one worker pool for each CPU and
20 * one extra for works which are better served by workers which are
21 * not bound to any specific CPU.
22 *
23 * Please read Documentation/workqueue.txt for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 */
25
Paul Gortmaker9984de12011-05-23 14:51:41 -040026#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/kernel.h>
28#include <linux/sched.h>
29#include <linux/init.h>
30#include <linux/signal.h>
31#include <linux/completion.h>
32#include <linux/workqueue.h>
33#include <linux/slab.h>
34#include <linux/cpu.h>
35#include <linux/notifier.h>
36#include <linux/kthread.h>
James Bottomley1fa44ec2006-02-23 12:43:43 -060037#include <linux/hardirq.h>
Christoph Lameter46934022006-10-11 01:21:26 -070038#include <linux/mempolicy.h>
Rafael J. Wysocki341a5952006-12-06 20:34:49 -080039#include <linux/freezer.h>
Peter Zijlstrad5abe662006-12-06 20:37:26 -080040#include <linux/kallsyms.h>
41#include <linux/debug_locks.h>
Johannes Berg4e6045f2007-10-18 23:39:55 -070042#include <linux/lockdep.h>
Tejun Heoc34056a2010-06-29 10:07:11 +020043#include <linux/idr.h>
Tejun Heoe22bee72010-06-29 10:07:14 +020044
45#include "workqueue_sched.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Tejun Heoc8e55f32010-06-29 10:07:12 +020047enum {
Tejun Heobc2ae0f2012-07-17 12:39:27 -070048 /*
49 * global_cwq flags
50 *
51 * A bound gcwq is either associated or disassociated with its CPU.
52 * While associated (!DISASSOCIATED), all workers are bound to the
53 * CPU and none has %WORKER_UNBOUND set and concurrency management
54 * is in effect.
55 *
56 * While DISASSOCIATED, the cpu may be offline and all workers have
57 * %WORKER_UNBOUND set and concurrency management disabled, and may
58 * be executing on any CPU. The gcwq behaves as an unbound one.
59 *
60 * Note that DISASSOCIATED can be flipped only while holding
61 * managership of all pools on the gcwq to avoid changing binding
62 * state while create_worker() is in progress.
63 */
Tejun Heo11ebea52012-07-12 14:46:37 -070064 GCWQ_DISASSOCIATED = 1 << 0, /* cpu can't serve workers */
65 GCWQ_FREEZING = 1 << 1, /* freeze in progress */
66
67 /* pool flags */
68 POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
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 Heoc34056a2010-06-29 10:07:11 +0200129
Tejun Heoe22bee72010-06-29 10:07:14 +0200130/*
131 * The poor guys doing the actual heavy lifting. All on-duty workers
132 * are either serving the manager role, on idle list or on busy hash.
133 */
Tejun Heoc34056a2010-06-29 10:07:11 +0200134struct worker {
Tejun Heoc8e55f32010-06-29 10:07:12 +0200135 /* on idle list while idle, on busy hash table while busy */
136 union {
137 struct list_head entry; /* L: while idle */
138 struct hlist_node hentry; /* L: while busy */
139 };
140
Tejun Heoc34056a2010-06-29 10:07:11 +0200141 struct work_struct *current_work; /* L: work being processed */
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200142 struct cpu_workqueue_struct *current_cwq; /* L: current_work's cwq */
Tejun Heoaffee4b2010-06-29 10:07:12 +0200143 struct list_head scheduled; /* L: scheduled works */
Tejun Heoc34056a2010-06-29 10:07:11 +0200144 struct task_struct *task; /* I: worker task */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700145 struct worker_pool *pool; /* I: the associated pool */
Tejun Heoe22bee72010-06-29 10:07:14 +0200146 /* 64 bytes boundary on 64bit, 32 on 32bit */
147 unsigned long last_active; /* L: last active timestamp */
148 unsigned int flags; /* X: flags */
Tejun Heoc34056a2010-06-29 10:07:11 +0200149 int id; /* I: worker id */
Tejun Heo25511a42012-07-17 12:39:27 -0700150
151 /* for rebinding worker to CPU */
Tejun Heo25511a42012-07-17 12:39:27 -0700152 struct work_struct rebind_work; /* L: for busy worker */
Tejun Heoc34056a2010-06-29 10:07:11 +0200153};
154
Tejun Heobd7bdd42012-07-12 14:46:37 -0700155struct worker_pool {
156 struct global_cwq *gcwq; /* I: the owning gcwq */
Tejun Heo11ebea52012-07-12 14:46:37 -0700157 unsigned int flags; /* X: flags */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700158
159 struct list_head worklist; /* L: list of pending works */
160 int nr_workers; /* L: total number of workers */
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700161
162 /* nr_idle includes the ones off idle_list for rebinding */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700163 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 Heo8b03ae32010-06-29 10:07:12 +0200189} ____cacheline_aligned_in_smp;
190
191/*
Tejun Heo502ca9d2010-06-29 10:07:13 +0200192 * The per-CPU workqueue. The lower WORK_STRUCT_FLAG_BITS of
Tejun Heo0f900042010-06-29 10:07:11 +0200193 * work_struct->data are used for flags and thus cwqs need to be
194 * aligned at two's power of the number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 */
196struct cpu_workqueue_struct {
Tejun Heobd7bdd42012-07-12 14:46:37 -0700197 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200198 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200199 int work_color; /* L: current color */
200 int flush_color; /* L: flushing color */
201 int nr_in_flight[WORK_NR_COLORS];
202 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200203 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200204 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200205 struct list_head delayed_works; /* L: delayed works */
Tejun Heo0f900042010-06-29 10:07:11 +0200206};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200209 * Structure used to wait for workqueue flush.
210 */
211struct wq_flusher {
212 struct list_head list; /* F: list of flushers */
213 int flush_color; /* F: flush color waiting for */
214 struct completion done; /* flush completion */
215};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Tejun Heo73f53c42010-06-29 10:07:11 +0200217/*
Tejun Heof2e005a2010-07-20 15:59:09 +0200218 * All cpumasks are assumed to be always set on UP and thus can't be
219 * used to determine whether there's something to be done.
220 */
221#ifdef CONFIG_SMP
222typedef cpumask_var_t mayday_mask_t;
223#define mayday_test_and_set_cpu(cpu, mask) \
224 cpumask_test_and_set_cpu((cpu), (mask))
225#define mayday_clear_cpu(cpu, mask) cpumask_clear_cpu((cpu), (mask))
226#define for_each_mayday_cpu(cpu, mask) for_each_cpu((cpu), (mask))
Tejun Heo9c375472010-08-31 11:18:34 +0200227#define alloc_mayday_mask(maskp, gfp) zalloc_cpumask_var((maskp), (gfp))
Tejun Heof2e005a2010-07-20 15:59:09 +0200228#define free_mayday_mask(mask) free_cpumask_var((mask))
229#else
230typedef unsigned long mayday_mask_t;
231#define mayday_test_and_set_cpu(cpu, mask) test_and_set_bit(0, &(mask))
232#define mayday_clear_cpu(cpu, mask) clear_bit(0, &(mask))
233#define for_each_mayday_cpu(cpu, mask) if ((cpu) = 0, (mask))
234#define alloc_mayday_mask(maskp, gfp) true
235#define free_mayday_mask(mask) do { } while (0)
236#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238/*
239 * The externally visible workqueue abstraction is an array of
240 * per-CPU workqueues:
241 */
242struct workqueue_struct {
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200243 unsigned int flags; /* W: WQ_* flags */
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200244 union {
245 struct cpu_workqueue_struct __percpu *pcpu;
246 struct cpu_workqueue_struct *single;
247 unsigned long v;
248 } cpu_wq; /* I: cwq's */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200249 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200250
251 struct mutex flush_mutex; /* protects wq flushing */
252 int work_color; /* F: current work color */
253 int flush_color; /* F: current flush color */
254 atomic_t nr_cwqs_to_flush; /* flush in progress */
255 struct wq_flusher *first_flusher; /* F: first flusher */
256 struct list_head flusher_queue; /* F: flush waiters */
257 struct list_head flusher_overflow; /* F: flush overflow list */
258
Tejun Heof2e005a2010-07-20 15:59:09 +0200259 mayday_mask_t mayday_mask; /* cpus requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200260 struct worker *rescuer; /* I: rescue worker */
261
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200262 int nr_drainers; /* W: drain in progress */
Tejun Heodcd989c2010-06-29 10:07:14 +0200263 int saved_max_active; /* W: saved cwq max_active */
Johannes Berg4e6045f2007-10-18 23:39:55 -0700264#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200265 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700266#endif
Tejun Heob196be82012-01-10 15:11:35 -0800267 char name[]; /* I: workqueue name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268};
269
Tejun Heod320c032010-06-29 10:07:14 +0200270struct workqueue_struct *system_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200271EXPORT_SYMBOL_GPL(system_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300272struct workqueue_struct *system_highpri_wq __read_mostly;
Joonsoo Kim1aabe902012-08-15 23:25:39 +0900273EXPORT_SYMBOL_GPL(system_highpri_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300274struct workqueue_struct *system_long_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200275EXPORT_SYMBOL_GPL(system_long_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300276struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200277EXPORT_SYMBOL_GPL(system_unbound_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300278struct workqueue_struct *system_freezable_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100279EXPORT_SYMBOL_GPL(system_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200280
Tejun Heo97bd2342010-10-05 10:41:14 +0200281#define CREATE_TRACE_POINTS
282#include <trace/events/workqueue.h>
283
Tejun Heo4ce62e92012-07-13 22:16:44 -0700284#define for_each_worker_pool(pool, gcwq) \
Tejun Heo32704762012-07-13 22:16:45 -0700285 for ((pool) = &(gcwq)->pools[0]; \
286 (pool) < &(gcwq)->pools[NR_WORKER_POOLS]; (pool)++)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700287
Tejun Heodb7bccf2010-06-29 10:07:12 +0200288#define for_each_busy_worker(worker, i, pos, gcwq) \
289 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \
290 hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry)
291
Tejun Heof3421792010-07-02 10:03:51 +0200292static inline int __next_gcwq_cpu(int cpu, const struct cpumask *mask,
293 unsigned int sw)
294{
295 if (cpu < nr_cpu_ids) {
296 if (sw & 1) {
297 cpu = cpumask_next(cpu, mask);
298 if (cpu < nr_cpu_ids)
299 return cpu;
300 }
301 if (sw & 2)
302 return WORK_CPU_UNBOUND;
303 }
304 return WORK_CPU_NONE;
305}
306
307static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
308 struct workqueue_struct *wq)
309{
310 return __next_gcwq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2);
311}
312
Tejun Heo09884952010-08-01 11:50:12 +0200313/*
314 * CPU iterators
315 *
316 * An extra gcwq is defined for an invalid cpu number
317 * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any
318 * specific CPU. The following iterators are similar to
319 * for_each_*_cpu() iterators but also considers the unbound gcwq.
320 *
321 * for_each_gcwq_cpu() : possible CPUs + WORK_CPU_UNBOUND
322 * for_each_online_gcwq_cpu() : online CPUs + WORK_CPU_UNBOUND
323 * for_each_cwq_cpu() : possible CPUs for bound workqueues,
324 * WORK_CPU_UNBOUND for unbound workqueues
325 */
Tejun Heof3421792010-07-02 10:03:51 +0200326#define for_each_gcwq_cpu(cpu) \
327 for ((cpu) = __next_gcwq_cpu(-1, cpu_possible_mask, 3); \
328 (cpu) < WORK_CPU_NONE; \
329 (cpu) = __next_gcwq_cpu((cpu), cpu_possible_mask, 3))
330
331#define for_each_online_gcwq_cpu(cpu) \
332 for ((cpu) = __next_gcwq_cpu(-1, cpu_online_mask, 3); \
333 (cpu) < WORK_CPU_NONE; \
334 (cpu) = __next_gcwq_cpu((cpu), cpu_online_mask, 3))
335
336#define for_each_cwq_cpu(cpu, wq) \
337 for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, (wq)); \
338 (cpu) < WORK_CPU_NONE; \
339 (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq)))
340
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900341#ifdef CONFIG_DEBUG_OBJECTS_WORK
342
343static struct debug_obj_descr work_debug_descr;
344
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100345static void *work_debug_hint(void *addr)
346{
347 return ((struct work_struct *) addr)->func;
348}
349
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900350/*
351 * fixup_init is called when:
352 * - an active object is initialized
353 */
354static int work_fixup_init(void *addr, enum debug_obj_state state)
355{
356 struct work_struct *work = addr;
357
358 switch (state) {
359 case ODEBUG_STATE_ACTIVE:
360 cancel_work_sync(work);
361 debug_object_init(work, &work_debug_descr);
362 return 1;
363 default:
364 return 0;
365 }
366}
367
368/*
369 * fixup_activate is called when:
370 * - an active object is activated
371 * - an unknown object is activated (might be a statically initialized object)
372 */
373static int work_fixup_activate(void *addr, enum debug_obj_state state)
374{
375 struct work_struct *work = addr;
376
377 switch (state) {
378
379 case ODEBUG_STATE_NOTAVAILABLE:
380 /*
381 * This is not really a fixup. The work struct was
382 * statically initialized. We just make sure that it
383 * is tracked in the object tracker.
384 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200385 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900386 debug_object_init(work, &work_debug_descr);
387 debug_object_activate(work, &work_debug_descr);
388 return 0;
389 }
390 WARN_ON_ONCE(1);
391 return 0;
392
393 case ODEBUG_STATE_ACTIVE:
394 WARN_ON(1);
395
396 default:
397 return 0;
398 }
399}
400
401/*
402 * fixup_free is called when:
403 * - an active object is freed
404 */
405static int work_fixup_free(void *addr, enum debug_obj_state state)
406{
407 struct work_struct *work = addr;
408
409 switch (state) {
410 case ODEBUG_STATE_ACTIVE:
411 cancel_work_sync(work);
412 debug_object_free(work, &work_debug_descr);
413 return 1;
414 default:
415 return 0;
416 }
417}
418
419static struct debug_obj_descr work_debug_descr = {
420 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100421 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900422 .fixup_init = work_fixup_init,
423 .fixup_activate = work_fixup_activate,
424 .fixup_free = work_fixup_free,
425};
426
427static inline void debug_work_activate(struct work_struct *work)
428{
429 debug_object_activate(work, &work_debug_descr);
430}
431
432static inline void debug_work_deactivate(struct work_struct *work)
433{
434 debug_object_deactivate(work, &work_debug_descr);
435}
436
437void __init_work(struct work_struct *work, int onstack)
438{
439 if (onstack)
440 debug_object_init_on_stack(work, &work_debug_descr);
441 else
442 debug_object_init(work, &work_debug_descr);
443}
444EXPORT_SYMBOL_GPL(__init_work);
445
446void destroy_work_on_stack(struct work_struct *work)
447{
448 debug_object_free(work, &work_debug_descr);
449}
450EXPORT_SYMBOL_GPL(destroy_work_on_stack);
451
452#else
453static inline void debug_work_activate(struct work_struct *work) { }
454static inline void debug_work_deactivate(struct work_struct *work) { }
455#endif
456
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100457/* Serializes the accesses to the list of workqueues. */
458static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459static LIST_HEAD(workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200460static bool workqueue_freezing; /* W: have wqs started freezing? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Oleg Nesterov14441962007-05-23 13:57:57 -0700462/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200463 * The almighty global cpu workqueues. nr_running is the only field
464 * which is expected to be used frequently by other cpus via
465 * try_to_wake_up(). Put it in a separate cacheline.
Oleg Nesterov14441962007-05-23 13:57:57 -0700466 */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200467static DEFINE_PER_CPU(struct global_cwq, global_cwq);
Tejun Heo4ce62e92012-07-13 22:16:44 -0700468static DEFINE_PER_CPU_SHARED_ALIGNED(atomic_t, pool_nr_running[NR_WORKER_POOLS]);
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800469
Tejun Heof3421792010-07-02 10:03:51 +0200470/*
471 * Global cpu workqueue and nr_running counter for unbound gcwq. The
472 * gcwq is always online, has GCWQ_DISASSOCIATED set, and all its
473 * workers have WORKER_UNBOUND set.
474 */
475static struct global_cwq unbound_global_cwq;
Tejun Heo4ce62e92012-07-13 22:16:44 -0700476static atomic_t unbound_pool_nr_running[NR_WORKER_POOLS] = {
477 [0 ... NR_WORKER_POOLS - 1] = ATOMIC_INIT(0), /* always 0 */
478};
Tejun Heof3421792010-07-02 10:03:51 +0200479
Tejun Heoc34056a2010-06-29 10:07:11 +0200480static int worker_thread(void *__worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Tejun Heo32704762012-07-13 22:16:45 -0700482static int worker_pool_pri(struct worker_pool *pool)
483{
484 return pool - pool->gcwq->pools;
485}
486
Tejun Heo8b03ae32010-06-29 10:07:12 +0200487static struct global_cwq *get_gcwq(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Tejun Heof3421792010-07-02 10:03:51 +0200489 if (cpu != WORK_CPU_UNBOUND)
490 return &per_cpu(global_cwq, cpu);
491 else
492 return &unbound_global_cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
Tejun Heo63d95a92012-07-12 14:46:37 -0700495static atomic_t *get_pool_nr_running(struct worker_pool *pool)
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700496{
Tejun Heo63d95a92012-07-12 14:46:37 -0700497 int cpu = pool->gcwq->cpu;
Tejun Heo32704762012-07-13 22:16:45 -0700498 int idx = worker_pool_pri(pool);
Tejun Heo63d95a92012-07-12 14:46:37 -0700499
Tejun Heof3421792010-07-02 10:03:51 +0200500 if (cpu != WORK_CPU_UNBOUND)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700501 return &per_cpu(pool_nr_running, cpu)[idx];
Tejun Heof3421792010-07-02 10:03:51 +0200502 else
Tejun Heo4ce62e92012-07-13 22:16:44 -0700503 return &unbound_pool_nr_running[idx];
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700504}
505
Tejun Heo4690c4a2010-06-29 10:07:10 +0200506static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
507 struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700508{
Tejun Heof3421792010-07-02 10:03:51 +0200509 if (!(wq->flags & WQ_UNBOUND)) {
Lai Jiangshane06ffa12012-03-09 18:03:20 +0800510 if (likely(cpu < nr_cpu_ids))
Tejun Heof3421792010-07-02 10:03:51 +0200511 return per_cpu_ptr(wq->cpu_wq.pcpu, cpu);
Tejun Heof3421792010-07-02 10:03:51 +0200512 } else if (likely(cpu == WORK_CPU_UNBOUND))
513 return wq->cpu_wq.single;
514 return NULL;
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700515}
516
Tejun Heo73f53c42010-06-29 10:07:11 +0200517static unsigned int work_color_to_flags(int color)
518{
519 return color << WORK_STRUCT_COLOR_SHIFT;
520}
521
522static int get_work_color(struct work_struct *work)
523{
524 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
525 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
526}
527
528static int work_next_color(int color)
529{
530 return (color + 1) % WORK_NR_COLORS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
David Howells4594bf12006-12-07 11:33:26 +0000533/*
Tejun Heob5490072012-08-03 10:30:46 -0700534 * While queued, %WORK_STRUCT_CWQ is set and non flag bits of a work's data
535 * contain the pointer to the queued cwq. Once execution starts, the flag
536 * is cleared and the high bits contain OFFQ flags and CPU number.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200537 *
Tejun Heobbb68df2012-08-03 10:30:46 -0700538 * set_work_cwq(), set_work_cpu_and_clear_pending(), mark_work_canceling()
539 * and clear_work_data() can be used to set the cwq, cpu or clear
540 * work->data. These functions should only be called while the work is
541 * owned - ie. while the PENDING bit is set.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200542 *
Tejun Heobbb68df2012-08-03 10:30:46 -0700543 * get_work_[g]cwq() can be used to obtain the gcwq or cwq corresponding to
544 * a work. gcwq is available once the work has been queued anywhere after
545 * initialization until it is sync canceled. cwq is available only while
546 * the work item is queued.
547 *
548 * %WORK_OFFQ_CANCELING is used to mark a work item which is being
549 * canceled. While being canceled, a work item may have its PENDING set
550 * but stay off timer and worklist for arbitrarily long and nobody should
551 * try to steal the PENDING bit.
David Howells4594bf12006-12-07 11:33:26 +0000552 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200553static inline void set_work_data(struct work_struct *work, unsigned long data,
554 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000555{
David Howells4594bf12006-12-07 11:33:26 +0000556 BUG_ON(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200557 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000558}
David Howells365970a2006-11-22 14:54:49 +0000559
Tejun Heo7a22ad72010-06-29 10:07:13 +0200560static void set_work_cwq(struct work_struct *work,
561 struct cpu_workqueue_struct *cwq,
562 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200563{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200564 set_work_data(work, (unsigned long)cwq,
Tejun Heoe1201532010-07-22 14:14:25 +0200565 WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200566}
567
Tejun Heo8930cab2012-08-03 10:30:45 -0700568static void set_work_cpu_and_clear_pending(struct work_struct *work,
569 unsigned int cpu)
David Howells365970a2006-11-22 14:54:49 +0000570{
Tejun Heo23657bb2012-08-13 17:08:19 -0700571 /*
572 * The following wmb is paired with the implied mb in
573 * test_and_set_bit(PENDING) and ensures all updates to @work made
574 * here are visible to and precede any updates by the next PENDING
575 * owner.
576 */
577 smp_wmb();
Tejun Heob5490072012-08-03 10:30:46 -0700578 set_work_data(work, (unsigned long)cpu << WORK_OFFQ_CPU_SHIFT, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200579}
580
581static void clear_work_data(struct work_struct *work)
582{
Tejun Heo23657bb2012-08-13 17:08:19 -0700583 smp_wmb(); /* see set_work_cpu_and_clear_pending() */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200584 set_work_data(work, WORK_STRUCT_NO_CPU, 0);
585}
586
Tejun Heo7a22ad72010-06-29 10:07:13 +0200587static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work)
588{
Tejun Heoe1201532010-07-22 14:14:25 +0200589 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200590
Tejun Heoe1201532010-07-22 14:14:25 +0200591 if (data & WORK_STRUCT_CWQ)
592 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
593 else
594 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200595}
596
597static struct global_cwq *get_work_gcwq(struct work_struct *work)
598{
Tejun Heoe1201532010-07-22 14:14:25 +0200599 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200600 unsigned int cpu;
601
Tejun Heoe1201532010-07-22 14:14:25 +0200602 if (data & WORK_STRUCT_CWQ)
603 return ((struct cpu_workqueue_struct *)
Tejun Heobd7bdd42012-07-12 14:46:37 -0700604 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->gcwq;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200605
Tejun Heob5490072012-08-03 10:30:46 -0700606 cpu = data >> WORK_OFFQ_CPU_SHIFT;
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200607 if (cpu == WORK_CPU_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200608 return NULL;
609
Tejun Heof3421792010-07-02 10:03:51 +0200610 BUG_ON(cpu >= nr_cpu_ids && cpu != WORK_CPU_UNBOUND);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200611 return get_gcwq(cpu);
David Howells365970a2006-11-22 14:54:49 +0000612}
613
Tejun Heobbb68df2012-08-03 10:30:46 -0700614static void mark_work_canceling(struct work_struct *work)
615{
616 struct global_cwq *gcwq = get_work_gcwq(work);
617 unsigned long cpu = gcwq ? gcwq->cpu : WORK_CPU_NONE;
618
619 set_work_data(work, (cpu << WORK_OFFQ_CPU_SHIFT) | WORK_OFFQ_CANCELING,
620 WORK_STRUCT_PENDING);
621}
622
623static bool work_is_canceling(struct work_struct *work)
624{
625 unsigned long data = atomic_long_read(&work->data);
626
627 return !(data & WORK_STRUCT_CWQ) && (data & WORK_OFFQ_CANCELING);
628}
629
David Howells365970a2006-11-22 14:54:49 +0000630/*
Tejun Heo32704762012-07-13 22:16:45 -0700631 * Policy functions. These define the policies on how the global worker
632 * pools are managed. Unless noted otherwise, these functions assume that
633 * they're being called with gcwq->lock held.
David Howells365970a2006-11-22 14:54:49 +0000634 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200635
Tejun Heo63d95a92012-07-12 14:46:37 -0700636static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000637{
Tejun Heo32704762012-07-13 22:16:45 -0700638 return !atomic_read(get_pool_nr_running(pool));
David Howells365970a2006-11-22 14:54:49 +0000639}
640
Tejun Heoe22bee72010-06-29 10:07:14 +0200641/*
642 * Need to wake up a worker? Called from anything but currently
643 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700644 *
645 * Note that, because unbound workers never contribute to nr_running, this
646 * function will always return %true for unbound gcwq as long as the
647 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200648 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700649static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000650{
Tejun Heo63d95a92012-07-12 14:46:37 -0700651 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000652}
653
Tejun Heoe22bee72010-06-29 10:07:14 +0200654/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700655static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200656{
Tejun Heo63d95a92012-07-12 14:46:37 -0700657 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200658}
659
660/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700661static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200662{
Tejun Heo63d95a92012-07-12 14:46:37 -0700663 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200664
Tejun Heo32704762012-07-13 22:16:45 -0700665 return !list_empty(&pool->worklist) && atomic_read(nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200666}
667
668/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700669static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200670{
Tejun Heo63d95a92012-07-12 14:46:37 -0700671 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200672}
673
674/* Do I need to be the manager? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700675static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200676{
Tejun Heo63d95a92012-07-12 14:46:37 -0700677 return need_to_create_worker(pool) ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700678 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200679}
680
681/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700682static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200683{
Lai Jiangshan552a37e2012-09-10 10:03:33 -0700684 bool managing = pool->flags & POOL_MANAGING_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -0700685 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
686 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200687
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700688 /*
689 * nr_idle and idle_list may disagree if idle rebinding is in
690 * progress. Never return %true if idle_list is empty.
691 */
692 if (list_empty(&pool->idle_list))
693 return false;
694
Tejun Heoe22bee72010-06-29 10:07:14 +0200695 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
696}
697
698/*
699 * Wake up functions.
700 */
701
Tejun Heo7e116292010-06-29 10:07:13 +0200702/* Return the first worker. Safe with preemption disabled */
Tejun Heo63d95a92012-07-12 14:46:37 -0700703static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200704{
Tejun Heo63d95a92012-07-12 14:46:37 -0700705 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200706 return NULL;
707
Tejun Heo63d95a92012-07-12 14:46:37 -0700708 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200709}
710
711/**
712 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700713 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200714 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700715 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200716 *
717 * CONTEXT:
718 * spin_lock_irq(gcwq->lock).
719 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700720static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200721{
Tejun Heo63d95a92012-07-12 14:46:37 -0700722 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200723
724 if (likely(worker))
725 wake_up_process(worker->task);
726}
727
Tejun Heo4690c4a2010-06-29 10:07:10 +0200728/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200729 * wq_worker_waking_up - a worker is waking up
730 * @task: task waking up
731 * @cpu: CPU @task is waking up to
732 *
733 * This function is called during try_to_wake_up() when a worker is
734 * being awoken.
735 *
736 * CONTEXT:
737 * spin_lock_irq(rq->lock)
738 */
739void wq_worker_waking_up(struct task_struct *task, unsigned int cpu)
740{
741 struct worker *worker = kthread_data(task);
742
Steven Rostedt2d646722010-12-03 23:12:33 -0500743 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heo63d95a92012-07-12 14:46:37 -0700744 atomic_inc(get_pool_nr_running(worker->pool));
Tejun Heoe22bee72010-06-29 10:07:14 +0200745}
746
747/**
748 * wq_worker_sleeping - a worker is going to sleep
749 * @task: task going to sleep
750 * @cpu: CPU in question, must be the current CPU number
751 *
752 * This function is called during schedule() when a busy worker is
753 * going to sleep. Worker on the same cpu can be woken up by
754 * returning pointer to its task.
755 *
756 * CONTEXT:
757 * spin_lock_irq(rq->lock)
758 *
759 * RETURNS:
760 * Worker task on @cpu to wake up, %NULL if none.
761 */
762struct task_struct *wq_worker_sleeping(struct task_struct *task,
763 unsigned int cpu)
764{
765 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heobd7bdd42012-07-12 14:46:37 -0700766 struct worker_pool *pool = worker->pool;
Tejun Heo63d95a92012-07-12 14:46:37 -0700767 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200768
Steven Rostedt2d646722010-12-03 23:12:33 -0500769 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200770 return NULL;
771
772 /* this can only happen on the local cpu */
773 BUG_ON(cpu != raw_smp_processor_id());
774
775 /*
776 * The counterpart of the following dec_and_test, implied mb,
777 * worklist not empty test sequence is in insert_work().
778 * Please read comment there.
779 *
Tejun Heo628c78e2012-07-17 12:39:27 -0700780 * NOT_RUNNING is clear. This means that we're bound to and
781 * running on the local cpu w/ rq lock held and preemption
782 * disabled, which in turn means that none else could be
783 * manipulating idle_list, so dereferencing idle_list without gcwq
784 * lock is safe.
Tejun Heoe22bee72010-06-29 10:07:14 +0200785 */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700786 if (atomic_dec_and_test(nr_running) && !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700787 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200788 return to_wakeup ? to_wakeup->task : NULL;
789}
790
791/**
792 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200793 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200794 * @flags: flags to set
795 * @wakeup: wakeup an idle worker if necessary
796 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200797 * Set @flags in @worker->flags and adjust nr_running accordingly. If
798 * nr_running becomes zero and @wakeup is %true, an idle worker is
799 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200800 *
Tejun Heocb444762010-07-02 10:03:50 +0200801 * CONTEXT:
802 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200803 */
804static inline void worker_set_flags(struct worker *worker, unsigned int flags,
805 bool wakeup)
806{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700807 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200808
Tejun Heocb444762010-07-02 10:03:50 +0200809 WARN_ON_ONCE(worker->task != current);
810
Tejun Heoe22bee72010-06-29 10:07:14 +0200811 /*
812 * If transitioning into NOT_RUNNING, adjust nr_running and
813 * wake up an idle worker as necessary if requested by
814 * @wakeup.
815 */
816 if ((flags & WORKER_NOT_RUNNING) &&
817 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heo63d95a92012-07-12 14:46:37 -0700818 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200819
820 if (wakeup) {
821 if (atomic_dec_and_test(nr_running) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -0700822 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700823 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200824 } else
825 atomic_dec(nr_running);
826 }
827
Tejun Heod302f012010-06-29 10:07:13 +0200828 worker->flags |= flags;
829}
830
831/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200832 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200833 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200834 * @flags: flags to clear
835 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200836 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200837 *
Tejun Heocb444762010-07-02 10:03:50 +0200838 * CONTEXT:
839 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200840 */
841static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
842{
Tejun Heo63d95a92012-07-12 14:46:37 -0700843 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200844 unsigned int oflags = worker->flags;
845
Tejun Heocb444762010-07-02 10:03:50 +0200846 WARN_ON_ONCE(worker->task != current);
847
Tejun Heod302f012010-06-29 10:07:13 +0200848 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200849
Tejun Heo42c025f2011-01-11 15:58:49 +0100850 /*
851 * If transitioning out of NOT_RUNNING, increment nr_running. Note
852 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
853 * of multiple flags, not a single flag.
854 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200855 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
856 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heo63d95a92012-07-12 14:46:37 -0700857 atomic_inc(get_pool_nr_running(pool));
Tejun Heod302f012010-06-29 10:07:13 +0200858}
859
860/**
Tejun Heoc8e55f32010-06-29 10:07:12 +0200861 * busy_worker_head - return the busy hash head for a work
862 * @gcwq: gcwq of interest
863 * @work: work to be hashed
864 *
865 * Return hash head of @gcwq for @work.
866 *
867 * CONTEXT:
868 * spin_lock_irq(gcwq->lock).
869 *
870 * RETURNS:
871 * Pointer to the hash head.
872 */
873static struct hlist_head *busy_worker_head(struct global_cwq *gcwq,
874 struct work_struct *work)
875{
876 const int base_shift = ilog2(sizeof(struct work_struct));
877 unsigned long v = (unsigned long)work;
878
879 /* simple shift and fold hash, do we need something better? */
880 v >>= base_shift;
881 v += v >> BUSY_WORKER_HASH_ORDER;
882 v &= BUSY_WORKER_HASH_MASK;
883
884 return &gcwq->busy_hash[v];
885}
886
887/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200888 * __find_worker_executing_work - find worker which is executing a work
889 * @gcwq: gcwq of interest
890 * @bwh: hash head as returned by busy_worker_head()
891 * @work: work to find worker for
892 *
893 * Find a worker which is executing @work on @gcwq. @bwh should be
894 * the hash head obtained by calling busy_worker_head() with the same
895 * work.
896 *
897 * CONTEXT:
898 * spin_lock_irq(gcwq->lock).
899 *
900 * RETURNS:
901 * Pointer to worker which is executing @work if found, NULL
902 * otherwise.
903 */
904static struct worker *__find_worker_executing_work(struct global_cwq *gcwq,
905 struct hlist_head *bwh,
906 struct work_struct *work)
907{
908 struct worker *worker;
909 struct hlist_node *tmp;
910
911 hlist_for_each_entry(worker, tmp, bwh, hentry)
912 if (worker->current_work == work)
913 return worker;
914 return NULL;
915}
916
917/**
918 * find_worker_executing_work - find worker which is executing a work
919 * @gcwq: gcwq of interest
920 * @work: work to find worker for
921 *
922 * Find a worker which is executing @work on @gcwq. This function is
923 * identical to __find_worker_executing_work() except that this
924 * function calculates @bwh itself.
925 *
926 * CONTEXT:
927 * spin_lock_irq(gcwq->lock).
928 *
929 * RETURNS:
930 * Pointer to worker which is executing @work if found, NULL
931 * otherwise.
932 */
933static struct worker *find_worker_executing_work(struct global_cwq *gcwq,
934 struct work_struct *work)
935{
936 return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work),
937 work);
938}
939
940/**
Tejun Heobf4ede02012-08-03 10:30:46 -0700941 * move_linked_works - move linked works to a list
942 * @work: start of series of works to be scheduled
943 * @head: target list to append @work to
944 * @nextp: out paramter for nested worklist walking
945 *
946 * Schedule linked works starting from @work to @head. Work series to
947 * be scheduled starts at @work and includes any consecutive work with
948 * WORK_STRUCT_LINKED set in its predecessor.
949 *
950 * If @nextp is not NULL, it's updated to point to the next work of
951 * the last scheduled work. This allows move_linked_works() to be
952 * nested inside outer list_for_each_entry_safe().
953 *
954 * CONTEXT:
955 * spin_lock_irq(gcwq->lock).
956 */
957static void move_linked_works(struct work_struct *work, struct list_head *head,
958 struct work_struct **nextp)
959{
960 struct work_struct *n;
961
962 /*
963 * Linked worklist will always end before the end of the list,
964 * use NULL for list head.
965 */
966 list_for_each_entry_safe_from(work, n, NULL, entry) {
967 list_move_tail(&work->entry, head);
968 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
969 break;
970 }
971
972 /*
973 * If we're already inside safe list traversal and have moved
974 * multiple works to the scheduled queue, the next position
975 * needs to be updated.
976 */
977 if (nextp)
978 *nextp = n;
979}
980
981static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
982{
983 struct work_struct *work = list_first_entry(&cwq->delayed_works,
984 struct work_struct, entry);
985
986 trace_workqueue_activate_work(work);
987 move_linked_works(work, &cwq->pool->worklist, NULL);
988 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
989 cwq->nr_active++;
990}
991
992/**
993 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
994 * @cwq: cwq of interest
995 * @color: color of work which left the queue
996 * @delayed: for a delayed work
997 *
998 * A work either has completed or is removed from pending queue,
999 * decrement nr_in_flight of its cwq and handle workqueue flushing.
1000 *
1001 * CONTEXT:
1002 * spin_lock_irq(gcwq->lock).
1003 */
1004static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color,
1005 bool delayed)
1006{
1007 /* ignore uncolored works */
1008 if (color == WORK_NO_COLOR)
1009 return;
1010
1011 cwq->nr_in_flight[color]--;
1012
1013 if (!delayed) {
1014 cwq->nr_active--;
1015 if (!list_empty(&cwq->delayed_works)) {
1016 /* one down, submit a delayed one */
1017 if (cwq->nr_active < cwq->max_active)
1018 cwq_activate_first_delayed(cwq);
1019 }
1020 }
1021
1022 /* is flush in progress and are we at the flushing tip? */
1023 if (likely(cwq->flush_color != color))
1024 return;
1025
1026 /* are there still in-flight works? */
1027 if (cwq->nr_in_flight[color])
1028 return;
1029
1030 /* this cwq is done, clear flush_color */
1031 cwq->flush_color = -1;
1032
1033 /*
1034 * If this was the last cwq, wake up the first flusher. It
1035 * will handle the rest.
1036 */
1037 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
1038 complete(&cwq->wq->first_flusher->done);
1039}
1040
Tejun Heo36e227d2012-08-03 10:30:46 -07001041/**
Tejun Heobbb68df2012-08-03 10:30:46 -07001042 * try_to_grab_pending - steal work item from worklist and disable irq
Tejun Heo36e227d2012-08-03 10:30:46 -07001043 * @work: work item to steal
1044 * @is_dwork: @work is a delayed_work
Tejun Heobbb68df2012-08-03 10:30:46 -07001045 * @flags: place to store irq state
Tejun Heo36e227d2012-08-03 10:30:46 -07001046 *
1047 * Try to grab PENDING bit of @work. This function can handle @work in any
1048 * stable state - idle, on timer or on worklist. Return values are
1049 *
1050 * 1 if @work was pending and we successfully stole PENDING
1051 * 0 if @work was idle and we claimed PENDING
1052 * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry
Tejun Heobbb68df2012-08-03 10:30:46 -07001053 * -ENOENT if someone else is canceling @work, this state may persist
1054 * for arbitrarily long
Tejun Heo36e227d2012-08-03 10:30:46 -07001055 *
Tejun Heobbb68df2012-08-03 10:30:46 -07001056 * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001057 * interrupted while holding PENDING and @work off queue, irq must be
1058 * disabled on entry. This, combined with delayed_work->timer being
1059 * irqsafe, ensures that we return -EAGAIN for finite short period of time.
Tejun Heobbb68df2012-08-03 10:30:46 -07001060 *
1061 * On successful return, >= 0, irq is disabled and the caller is
1062 * responsible for releasing it using local_irq_restore(*@flags).
1063 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001064 * This function is safe to call from any context including IRQ handler.
Tejun Heobf4ede02012-08-03 10:30:46 -07001065 */
Tejun Heobbb68df2012-08-03 10:30:46 -07001066static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
1067 unsigned long *flags)
Tejun Heobf4ede02012-08-03 10:30:46 -07001068{
1069 struct global_cwq *gcwq;
Tejun Heobf4ede02012-08-03 10:30:46 -07001070
Tejun Heobbb68df2012-08-03 10:30:46 -07001071 WARN_ON_ONCE(in_irq());
1072
1073 local_irq_save(*flags);
1074
Tejun Heo36e227d2012-08-03 10:30:46 -07001075 /* try to steal the timer if it exists */
1076 if (is_dwork) {
1077 struct delayed_work *dwork = to_delayed_work(work);
1078
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001079 /*
1080 * dwork->timer is irqsafe. If del_timer() fails, it's
1081 * guaranteed that the timer is not queued anywhere and not
1082 * running on the local CPU.
1083 */
Tejun Heo36e227d2012-08-03 10:30:46 -07001084 if (likely(del_timer(&dwork->timer)))
1085 return 1;
1086 }
1087
1088 /* try to claim PENDING the normal way */
Tejun Heobf4ede02012-08-03 10:30:46 -07001089 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1090 return 0;
1091
1092 /*
1093 * The queueing is in progress, or it is already queued. Try to
1094 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1095 */
1096 gcwq = get_work_gcwq(work);
1097 if (!gcwq)
Tejun Heobbb68df2012-08-03 10:30:46 -07001098 goto fail;
Tejun Heobf4ede02012-08-03 10:30:46 -07001099
Tejun Heobbb68df2012-08-03 10:30:46 -07001100 spin_lock(&gcwq->lock);
Tejun Heobf4ede02012-08-03 10:30:46 -07001101 if (!list_empty(&work->entry)) {
1102 /*
1103 * This work is queued, but perhaps we locked the wrong gcwq.
1104 * In that case we must see the new value after rmb(), see
1105 * insert_work()->wmb().
1106 */
1107 smp_rmb();
1108 if (gcwq == get_work_gcwq(work)) {
1109 debug_work_deactivate(work);
1110 list_del_init(&work->entry);
1111 cwq_dec_nr_in_flight(get_work_cwq(work),
1112 get_work_color(work),
1113 *work_data_bits(work) & WORK_STRUCT_DELAYED);
Tejun Heo36e227d2012-08-03 10:30:46 -07001114
Tejun Heobbb68df2012-08-03 10:30:46 -07001115 spin_unlock(&gcwq->lock);
Tejun Heo36e227d2012-08-03 10:30:46 -07001116 return 1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001117 }
1118 }
Tejun Heobbb68df2012-08-03 10:30:46 -07001119 spin_unlock(&gcwq->lock);
1120fail:
1121 local_irq_restore(*flags);
1122 if (work_is_canceling(work))
1123 return -ENOENT;
1124 cpu_relax();
Tejun Heo36e227d2012-08-03 10:30:46 -07001125 return -EAGAIN;
Tejun Heobf4ede02012-08-03 10:30:46 -07001126}
1127
1128/**
Tejun Heo7e116292010-06-29 10:07:13 +02001129 * insert_work - insert a work into gcwq
Tejun Heo4690c4a2010-06-29 10:07:10 +02001130 * @cwq: cwq @work belongs to
1131 * @work: work to insert
1132 * @head: insertion point
1133 * @extra_flags: extra WORK_STRUCT_* flags to set
1134 *
Tejun Heo7e116292010-06-29 10:07:13 +02001135 * Insert @work which belongs to @cwq into @gcwq after @head.
1136 * @extra_flags is or'd to work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +02001137 *
1138 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001139 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02001140 */
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001141static void insert_work(struct cpu_workqueue_struct *cwq,
Tejun Heo4690c4a2010-06-29 10:07:10 +02001142 struct work_struct *work, struct list_head *head,
1143 unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001144{
Tejun Heo63d95a92012-07-12 14:46:37 -07001145 struct worker_pool *pool = cwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01001146
Tejun Heo4690c4a2010-06-29 10:07:10 +02001147 /* we own @work, set data and link */
Tejun Heo7a22ad72010-06-29 10:07:13 +02001148 set_work_cwq(work, cwq, extra_flags);
Tejun Heo4690c4a2010-06-29 10:07:10 +02001149
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001150 /*
1151 * Ensure that we get the right work->data if we see the
1152 * result of list_add() below, see try_to_grab_pending().
1153 */
1154 smp_wmb();
Tejun Heo4690c4a2010-06-29 10:07:10 +02001155
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -07001156 list_add_tail(&work->entry, head);
Tejun Heoe22bee72010-06-29 10:07:14 +02001157
1158 /*
1159 * Ensure either worker_sched_deactivated() sees the above
1160 * list_add_tail() or we see zero nr_running to avoid workers
1161 * lying around lazily while there are works to be processed.
1162 */
1163 smp_mb();
1164
Tejun Heo63d95a92012-07-12 14:46:37 -07001165 if (__need_more_worker(pool))
1166 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001167}
1168
Tejun Heoc8efcc22010-12-20 19:32:04 +01001169/*
1170 * Test whether @work is being queued from another work executing on the
1171 * same workqueue. This is rather expensive and should only be used from
1172 * cold paths.
1173 */
1174static bool is_chained_work(struct workqueue_struct *wq)
1175{
1176 unsigned long flags;
1177 unsigned int cpu;
1178
1179 for_each_gcwq_cpu(cpu) {
1180 struct global_cwq *gcwq = get_gcwq(cpu);
1181 struct worker *worker;
1182 struct hlist_node *pos;
1183 int i;
1184
1185 spin_lock_irqsave(&gcwq->lock, flags);
1186 for_each_busy_worker(worker, i, pos, gcwq) {
1187 if (worker->task != current)
1188 continue;
1189 spin_unlock_irqrestore(&gcwq->lock, flags);
1190 /*
1191 * I'm @worker, no locking necessary. See if @work
1192 * is headed to the same workqueue.
1193 */
1194 return worker->current_cwq->wq == wq;
1195 }
1196 spin_unlock_irqrestore(&gcwq->lock, flags);
1197 }
1198 return false;
1199}
1200
Tejun Heo4690c4a2010-06-29 10:07:10 +02001201static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 struct work_struct *work)
1203{
Tejun Heo502ca9d2010-06-29 10:07:13 +02001204 struct global_cwq *gcwq;
1205 struct cpu_workqueue_struct *cwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001206 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001207 unsigned int work_flags;
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001208 unsigned int req_cpu = cpu;
Tejun Heo8930cab2012-08-03 10:30:45 -07001209
1210 /*
1211 * While a work item is PENDING && off queue, a task trying to
1212 * steal the PENDING will busy-loop waiting for it to either get
1213 * queued or lose PENDING. Grabbing PENDING and queueing should
1214 * happen with IRQ disabled.
1215 */
1216 WARN_ON_ONCE(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001218 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001219
Tejun Heoc8efcc22010-12-20 19:32:04 +01001220 /* if dying, only works from the same workqueue are allowed */
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02001221 if (unlikely(wq->flags & WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001222 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001223 return;
1224
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001225 /* determine gcwq to use */
1226 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001227 struct global_cwq *last_gcwq;
1228
Tejun Heo57469822012-08-03 10:30:45 -07001229 if (cpu == WORK_CPU_UNBOUND)
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001230 cpu = raw_smp_processor_id();
1231
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001232 /*
Tejun Heodbf25762012-08-20 14:51:23 -07001233 * It's multi cpu. If @work was previously on a different
1234 * cpu, it might still be running there, in which case the
1235 * work needs to be queued on that cpu to guarantee
1236 * non-reentrancy.
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001237 */
Tejun Heo502ca9d2010-06-29 10:07:13 +02001238 gcwq = get_gcwq(cpu);
Tejun Heodbf25762012-08-20 14:51:23 -07001239 last_gcwq = get_work_gcwq(work);
1240
1241 if (last_gcwq && last_gcwq != gcwq) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001242 struct worker *worker;
1243
Tejun Heo8930cab2012-08-03 10:30:45 -07001244 spin_lock(&last_gcwq->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001245
1246 worker = find_worker_executing_work(last_gcwq, work);
1247
1248 if (worker && worker->current_cwq->wq == wq)
1249 gcwq = last_gcwq;
1250 else {
1251 /* meh... not running there, queue here */
Tejun Heo8930cab2012-08-03 10:30:45 -07001252 spin_unlock(&last_gcwq->lock);
1253 spin_lock(&gcwq->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001254 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001255 } else {
1256 spin_lock(&gcwq->lock);
1257 }
Tejun Heof3421792010-07-02 10:03:51 +02001258 } else {
1259 gcwq = get_gcwq(WORK_CPU_UNBOUND);
Tejun Heo8930cab2012-08-03 10:30:45 -07001260 spin_lock(&gcwq->lock);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001261 }
1262
1263 /* gcwq determined, get cwq and queue */
1264 cwq = get_cwq(gcwq->cpu, wq);
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001265 trace_workqueue_queue_work(req_cpu, cwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001266
Dan Carpenterf5b25522012-04-13 22:06:58 +03001267 if (WARN_ON(!list_empty(&work->entry))) {
Tejun Heo8930cab2012-08-03 10:30:45 -07001268 spin_unlock(&gcwq->lock);
Dan Carpenterf5b25522012-04-13 22:06:58 +03001269 return;
1270 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001271
Tejun Heo73f53c42010-06-29 10:07:11 +02001272 cwq->nr_in_flight[cwq->work_color]++;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001273 work_flags = work_color_to_flags(cwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001274
1275 if (likely(cwq->nr_active < cwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001276 trace_workqueue_activate_work(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001277 cwq->nr_active++;
Tejun Heo32704762012-07-13 22:16:45 -07001278 worklist = &cwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001279 } else {
1280 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001281 worklist = &cwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001282 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001283
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001284 insert_work(cwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001285
Tejun Heo8930cab2012-08-03 10:30:45 -07001286 spin_unlock(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287}
1288
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001289/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07001290 * queue_work_on - queue work on specific cpu
1291 * @cpu: CPU number to execute work on
1292 * @wq: workqueue to use
1293 * @work: work to queue
1294 *
Tejun Heod4283e92012-08-03 10:30:44 -07001295 * Returns %false if @work was already on a queue, %true otherwise.
Zhang Ruic1a220e2008-07-23 21:28:39 -07001296 *
1297 * We queue the work to a specific CPU, the caller must ensure it
1298 * can't go away.
1299 */
Tejun Heod4283e92012-08-03 10:30:44 -07001300bool queue_work_on(int cpu, struct workqueue_struct *wq,
1301 struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07001302{
Tejun Heod4283e92012-08-03 10:30:44 -07001303 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001304 unsigned long flags;
1305
1306 local_irq_save(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001307
Tejun Heo22df02b2010-06-29 10:07:10 +02001308 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001309 __queue_work(cpu, wq, work);
Tejun Heod4283e92012-08-03 10:30:44 -07001310 ret = true;
Zhang Ruic1a220e2008-07-23 21:28:39 -07001311 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001312
1313 local_irq_restore(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001314 return ret;
1315}
1316EXPORT_SYMBOL_GPL(queue_work_on);
1317
Tejun Heo0a13c002012-08-03 10:30:44 -07001318/**
1319 * queue_work - queue work on a workqueue
1320 * @wq: workqueue to use
1321 * @work: work to queue
1322 *
Tejun Heod4283e92012-08-03 10:30:44 -07001323 * Returns %false if @work was already on a queue, %true otherwise.
Tejun Heo0a13c002012-08-03 10:30:44 -07001324 *
1325 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1326 * it can be processed by another CPU.
1327 */
Tejun Heod4283e92012-08-03 10:30:44 -07001328bool queue_work(struct workqueue_struct *wq, struct work_struct *work)
Tejun Heo0a13c002012-08-03 10:30:44 -07001329{
Tejun Heo57469822012-08-03 10:30:45 -07001330 return queue_work_on(WORK_CPU_UNBOUND, wq, work);
Tejun Heo0a13c002012-08-03 10:30:44 -07001331}
1332EXPORT_SYMBOL_GPL(queue_work);
1333
Tejun Heod8e794d2012-08-03 10:30:45 -07001334void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
David Howells52bad642006-11-22 14:54:01 +00001336 struct delayed_work *dwork = (struct delayed_work *)__data;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001337 struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001339 /* should have been called from irqsafe timer with irq already off */
Tejun Heo12650572012-08-08 09:38:42 -07001340 __queue_work(dwork->cpu, cwq->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341}
Tejun Heod8e794d2012-08-03 10:30:45 -07001342EXPORT_SYMBOL_GPL(delayed_work_timer_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001344static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
1345 struct delayed_work *dwork, unsigned long delay)
1346{
1347 struct timer_list *timer = &dwork->timer;
1348 struct work_struct *work = &dwork->work;
1349 unsigned int lcpu;
1350
1351 WARN_ON_ONCE(timer->function != delayed_work_timer_fn ||
1352 timer->data != (unsigned long)dwork);
1353 BUG_ON(timer_pending(timer));
1354 BUG_ON(!list_empty(&work->entry));
1355
1356 timer_stats_timer_set_start_info(&dwork->timer);
1357
1358 /*
1359 * This stores cwq for the moment, for the timer_fn. Note that the
1360 * work's gcwq is preserved to allow reentrance detection for
1361 * delayed works.
1362 */
1363 if (!(wq->flags & WQ_UNBOUND)) {
1364 struct global_cwq *gcwq = get_work_gcwq(work);
1365
Joonsoo Kime42986d2012-08-15 23:25:38 +09001366 /*
1367 * If we cannot get the last gcwq from @work directly,
1368 * select the last CPU such that it avoids unnecessarily
1369 * triggering non-reentrancy check in __queue_work().
1370 */
1371 lcpu = cpu;
1372 if (gcwq)
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001373 lcpu = gcwq->cpu;
Joonsoo Kime42986d2012-08-15 23:25:38 +09001374 if (lcpu == WORK_CPU_UNBOUND)
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001375 lcpu = raw_smp_processor_id();
1376 } else {
1377 lcpu = WORK_CPU_UNBOUND;
1378 }
1379
1380 set_work_cwq(work, get_cwq(lcpu, wq), 0);
1381
Tejun Heo12650572012-08-08 09:38:42 -07001382 dwork->cpu = cpu;
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001383 timer->expires = jiffies + delay;
1384
1385 if (unlikely(cpu != WORK_CPU_UNBOUND))
1386 add_timer_on(timer, cpu);
1387 else
1388 add_timer(timer);
1389}
1390
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001391/**
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001392 * queue_delayed_work_on - queue work on specific CPU after delay
1393 * @cpu: CPU number to execute work on
1394 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001395 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001396 * @delay: number of jiffies to wait before queueing
1397 *
Tejun Heo715f1302012-08-03 10:30:46 -07001398 * Returns %false if @work was already on a queue, %true otherwise. If
1399 * @delay is zero and @dwork is idle, it will be scheduled for immediate
1400 * execution.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001401 */
Tejun Heod4283e92012-08-03 10:30:44 -07001402bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
1403 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001404{
David Howells52bad642006-11-22 14:54:01 +00001405 struct work_struct *work = &dwork->work;
Tejun Heod4283e92012-08-03 10:30:44 -07001406 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001407 unsigned long flags;
1408
Tejun Heo715f1302012-08-03 10:30:46 -07001409 if (!delay)
1410 return queue_work_on(cpu, wq, &dwork->work);
1411
Tejun Heo8930cab2012-08-03 10:30:45 -07001412 /* read the comment in __queue_work() */
1413 local_irq_save(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001414
Tejun Heo22df02b2010-06-29 10:07:10 +02001415 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001416 __queue_delayed_work(cpu, wq, dwork, delay);
Tejun Heod4283e92012-08-03 10:30:44 -07001417 ret = true;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001418 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001419
1420 local_irq_restore(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001421 return ret;
1422}
Dave Jonesae90dd52006-06-30 01:40:45 -04001423EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Tejun Heoc8e55f32010-06-29 10:07:12 +02001425/**
Tejun Heo0a13c002012-08-03 10:30:44 -07001426 * queue_delayed_work - queue work on a workqueue after delay
1427 * @wq: workqueue to use
1428 * @dwork: delayable work to queue
1429 * @delay: number of jiffies to wait before queueing
1430 *
Tejun Heo715f1302012-08-03 10:30:46 -07001431 * Equivalent to queue_delayed_work_on() but tries to use the local CPU.
Tejun Heo0a13c002012-08-03 10:30:44 -07001432 */
Tejun Heod4283e92012-08-03 10:30:44 -07001433bool queue_delayed_work(struct workqueue_struct *wq,
Tejun Heo0a13c002012-08-03 10:30:44 -07001434 struct delayed_work *dwork, unsigned long delay)
1435{
Tejun Heo57469822012-08-03 10:30:45 -07001436 return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
Tejun Heo0a13c002012-08-03 10:30:44 -07001437}
1438EXPORT_SYMBOL_GPL(queue_delayed_work);
1439
1440/**
Tejun Heo8376fe22012-08-03 10:30:47 -07001441 * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
1442 * @cpu: CPU number to execute work on
1443 * @wq: workqueue to use
1444 * @dwork: work to queue
1445 * @delay: number of jiffies to wait before queueing
1446 *
1447 * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
1448 * modify @dwork's timer so that it expires after @delay. If @delay is
1449 * zero, @work is guaranteed to be scheduled immediately regardless of its
1450 * current state.
1451 *
1452 * Returns %false if @dwork was idle and queued, %true if @dwork was
1453 * pending and its timer was modified.
1454 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001455 * This function is safe to call from any context including IRQ handler.
Tejun Heo8376fe22012-08-03 10:30:47 -07001456 * See try_to_grab_pending() for details.
1457 */
1458bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
1459 struct delayed_work *dwork, unsigned long delay)
1460{
1461 unsigned long flags;
1462 int ret;
1463
1464 do {
1465 ret = try_to_grab_pending(&dwork->work, true, &flags);
1466 } while (unlikely(ret == -EAGAIN));
1467
1468 if (likely(ret >= 0)) {
1469 __queue_delayed_work(cpu, wq, dwork, delay);
1470 local_irq_restore(flags);
1471 }
1472
1473 /* -ENOENT from try_to_grab_pending() becomes %true */
1474 return ret;
1475}
1476EXPORT_SYMBOL_GPL(mod_delayed_work_on);
1477
1478/**
1479 * mod_delayed_work - modify delay of or queue a delayed work
1480 * @wq: workqueue to use
1481 * @dwork: work to queue
1482 * @delay: number of jiffies to wait before queueing
1483 *
1484 * mod_delayed_work_on() on local CPU.
1485 */
1486bool mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dwork,
1487 unsigned long delay)
1488{
1489 return mod_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
1490}
1491EXPORT_SYMBOL_GPL(mod_delayed_work);
1492
1493/**
Tejun Heoc8e55f32010-06-29 10:07:12 +02001494 * worker_enter_idle - enter idle state
1495 * @worker: worker which is entering idle state
1496 *
1497 * @worker is entering idle state. Update stats and idle timer if
1498 * necessary.
1499 *
1500 * LOCKING:
1501 * spin_lock_irq(gcwq->lock).
1502 */
1503static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001505 struct worker_pool *pool = worker->pool;
1506 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Tejun Heoc8e55f32010-06-29 10:07:12 +02001508 BUG_ON(worker->flags & WORKER_IDLE);
1509 BUG_ON(!list_empty(&worker->entry) &&
1510 (worker->hentry.next || worker->hentry.pprev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Tejun Heocb444762010-07-02 10:03:50 +02001512 /* can't use worker_set_flags(), also called from start_worker() */
1513 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001514 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001515 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001516
Tejun Heoc8e55f32010-06-29 10:07:12 +02001517 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001518 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001519
Tejun Heo628c78e2012-07-17 12:39:27 -07001520 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1521 mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
Tejun Heocb444762010-07-02 10:03:50 +02001522
Tejun Heo544ecf32012-05-14 15:04:50 -07001523 /*
Tejun Heo628c78e2012-07-17 12:39:27 -07001524 * Sanity check nr_running. Because gcwq_unbind_fn() releases
1525 * gcwq->lock between setting %WORKER_UNBOUND and zapping
1526 * nr_running, the warning may trigger spuriously. Check iff
1527 * unbind is not in progress.
Tejun Heo544ecf32012-05-14 15:04:50 -07001528 */
Tejun Heo628c78e2012-07-17 12:39:27 -07001529 WARN_ON_ONCE(!(gcwq->flags & GCWQ_DISASSOCIATED) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001530 pool->nr_workers == pool->nr_idle &&
Tejun Heo63d95a92012-07-12 14:46:37 -07001531 atomic_read(get_pool_nr_running(pool)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
1533
Tejun Heoc8e55f32010-06-29 10:07:12 +02001534/**
1535 * worker_leave_idle - leave idle state
1536 * @worker: worker which is leaving idle state
1537 *
1538 * @worker is leaving idle state. Update stats.
1539 *
1540 * LOCKING:
1541 * spin_lock_irq(gcwq->lock).
1542 */
1543static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001545 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Tejun Heoc8e55f32010-06-29 10:07:12 +02001547 BUG_ON(!(worker->flags & WORKER_IDLE));
Tejun Heod302f012010-06-29 10:07:13 +02001548 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001549 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001550 list_del_init(&worker->entry);
1551}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Tejun Heoe22bee72010-06-29 10:07:14 +02001553/**
1554 * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq
1555 * @worker: self
1556 *
1557 * Works which are scheduled while the cpu is online must at least be
1558 * scheduled to a worker which is bound to the cpu so that if they are
1559 * flushed from cpu callbacks while cpu is going down, they are
1560 * guaranteed to execute on the cpu.
1561 *
1562 * This function is to be used by rogue workers and rescuers to bind
1563 * themselves to the target cpu and may race with cpu going down or
1564 * coming online. kthread_bind() can't be used because it may put the
1565 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1566 * verbatim as it's best effort and blocking and gcwq may be
1567 * [dis]associated in the meantime.
1568 *
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001569 * This function tries set_cpus_allowed() and locks gcwq and verifies the
1570 * binding against %GCWQ_DISASSOCIATED which is set during
1571 * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1572 * enters idle state or fetches works without dropping lock, it can
1573 * guarantee the scheduling requirement described in the first paragraph.
Tejun Heoe22bee72010-06-29 10:07:14 +02001574 *
1575 * CONTEXT:
1576 * Might sleep. Called without any lock but returns with gcwq->lock
1577 * held.
1578 *
1579 * RETURNS:
1580 * %true if the associated gcwq is online (@worker is successfully
1581 * bound), %false if offline.
1582 */
1583static bool worker_maybe_bind_and_lock(struct worker *worker)
Namhyung Kim972fa1c2010-08-22 23:19:43 +09001584__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001585{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001586 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001587 struct task_struct *task = worker->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Tejun Heoe22bee72010-06-29 10:07:14 +02001589 while (true) {
1590 /*
1591 * The following call may fail, succeed or succeed
1592 * without actually migrating the task to the cpu if
1593 * it races with cpu hotunplug operation. Verify
1594 * against GCWQ_DISASSOCIATED.
1595 */
Tejun Heof3421792010-07-02 10:03:51 +02001596 if (!(gcwq->flags & GCWQ_DISASSOCIATED))
1597 set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu));
Oleg Nesterov85f41862007-05-09 02:34:20 -07001598
Tejun Heoe22bee72010-06-29 10:07:14 +02001599 spin_lock_irq(&gcwq->lock);
1600 if (gcwq->flags & GCWQ_DISASSOCIATED)
1601 return false;
1602 if (task_cpu(task) == gcwq->cpu &&
1603 cpumask_equal(&current->cpus_allowed,
1604 get_cpu_mask(gcwq->cpu)))
1605 return true;
1606 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001607
Tejun Heo5035b202011-04-29 18:08:37 +02001608 /*
1609 * We've raced with CPU hot[un]plug. Give it a breather
1610 * and retry migration. cond_resched() is required here;
1611 * otherwise, we might deadlock against cpu_stop trying to
1612 * bring down the CPU on non-preemptive kernel.
1613 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001614 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001615 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001616 }
1617}
1618
1619/*
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001620 * Rebind an idle @worker to its CPU. worker_thread() will test
Tejun Heo25511a42012-07-17 12:39:27 -07001621 * %WORKER_REBIND before leaving idle and call this function.
1622 */
1623static void idle_worker_rebind(struct worker *worker)
1624{
1625 struct global_cwq *gcwq = worker->pool->gcwq;
1626
Tejun Heoec588152012-09-04 23:16:32 -07001627 /*
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001628 * CPU may go down again inbetween. If rebinding fails, reinstate
1629 * UNBOUND. We're off idle_list and nobody else can do it for us.
Tejun Heoec588152012-09-04 23:16:32 -07001630 */
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001631 if (!worker_maybe_bind_and_lock(worker))
1632 worker->flags |= WORKER_UNBOUND;
1633
1634 worker_clr_flags(worker, WORKER_REBIND);
1635
1636 /* rebind complete, become available again */
1637 list_add(&worker->entry, &worker->pool->idle_list);
1638 spin_unlock_irq(&gcwq->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07001639}
1640
1641/*
1642 * Function for @worker->rebind.work used to rebind unbound busy workers to
Tejun Heo403c8212012-07-17 12:39:27 -07001643 * the associated cpu which is coming back online. This is scheduled by
1644 * cpu up but can race with other cpu hotplug operations and may be
1645 * executed twice without intervening cpu down.
Tejun Heoe22bee72010-06-29 10:07:14 +02001646 */
Tejun Heo25511a42012-07-17 12:39:27 -07001647static void busy_worker_rebind_fn(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001648{
1649 struct worker *worker = container_of(work, struct worker, rebind_work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001650 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001651
Lai Jiangshan960bd112012-09-17 15:42:31 -07001652 worker_maybe_bind_and_lock(worker);
1653
1654 /*
1655 * %WORKER_REBIND must be cleared even if the above binding failed;
1656 * otherwise, we may confuse the next CPU_UP cycle or oops / get
1657 * stuck by calling idle_worker_rebind() prematurely. If CPU went
1658 * down again inbetween, %WORKER_UNBOUND would be set, so clearing
1659 * %WORKER_REBIND is always safe.
1660 */
1661 worker_clr_flags(worker, WORKER_REBIND);
Tejun Heoe22bee72010-06-29 10:07:14 +02001662
1663 spin_unlock_irq(&gcwq->lock);
1664}
1665
Tejun Heo25511a42012-07-17 12:39:27 -07001666/**
1667 * rebind_workers - rebind all workers of a gcwq to the associated CPU
1668 * @gcwq: gcwq of interest
1669 *
1670 * @gcwq->cpu is coming online. Rebind all workers to the CPU. Rebinding
1671 * is different for idle and busy ones.
1672 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001673 * Idle ones will be removed from the idle_list and woken up. They will
1674 * add themselves back after completing rebind. This ensures that the
1675 * idle_list doesn't contain any unbound workers when re-bound busy workers
1676 * try to perform local wake-ups for concurrency management.
Tejun Heo25511a42012-07-17 12:39:27 -07001677 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001678 * Busy workers can rebind after they finish their current work items.
1679 * Queueing the rebind work item at the head of the scheduled list is
1680 * enough. Note that nr_running will be properly bumped as busy workers
1681 * rebind.
Tejun Heo25511a42012-07-17 12:39:27 -07001682 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001683 * On return, all non-manager workers are scheduled for rebind - see
1684 * manage_workers() for the manager special case. Any idle worker
1685 * including the manager will not appear on @idle_list until rebind is
1686 * complete, making local wake-ups safe.
Tejun Heo25511a42012-07-17 12:39:27 -07001687 */
1688static void rebind_workers(struct global_cwq *gcwq)
Tejun Heo25511a42012-07-17 12:39:27 -07001689{
Tejun Heo25511a42012-07-17 12:39:27 -07001690 struct worker_pool *pool;
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001691 struct worker *worker, *n;
Tejun Heo25511a42012-07-17 12:39:27 -07001692 struct hlist_node *pos;
1693 int i;
1694
1695 lockdep_assert_held(&gcwq->lock);
1696
1697 for_each_worker_pool(pool, gcwq)
1698 lockdep_assert_held(&pool->manager_mutex);
1699
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001700 /* set REBIND and kick idle ones */
Tejun Heo25511a42012-07-17 12:39:27 -07001701 for_each_worker_pool(pool, gcwq) {
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001702 list_for_each_entry_safe(worker, n, &pool->idle_list, entry) {
Lai Jiangshan96e65302012-09-02 00:28:19 +08001703 unsigned long worker_flags = worker->flags;
1704
Lai Jiangshan96e65302012-09-02 00:28:19 +08001705 /* morph UNBOUND to REBIND atomically */
1706 worker_flags &= ~WORKER_UNBOUND;
1707 worker_flags |= WORKER_REBIND;
1708 ACCESS_ONCE(worker->flags) = worker_flags;
Tejun Heo25511a42012-07-17 12:39:27 -07001709
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001710 /*
1711 * idle workers should be off @pool->idle_list
1712 * until rebind is complete to avoid receiving
1713 * premature local wake-ups.
1714 */
1715 list_del_init(&worker->entry);
Tejun Heo25511a42012-07-17 12:39:27 -07001716
1717 /* worker_thread() will call idle_worker_rebind() */
1718 wake_up_process(worker->task);
1719 }
1720 }
1721
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001722 /* rebind busy workers */
Tejun Heo25511a42012-07-17 12:39:27 -07001723 for_each_busy_worker(worker, i, pos, gcwq) {
Lai Jiangshan96e65302012-09-02 00:28:19 +08001724 unsigned long worker_flags = worker->flags;
Tejun Heo25511a42012-07-17 12:39:27 -07001725 struct work_struct *rebind_work = &worker->rebind_work;
Joonsoo Kime2b6a6d2012-08-15 23:25:40 +09001726 struct workqueue_struct *wq;
Tejun Heo25511a42012-07-17 12:39:27 -07001727
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 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
1734 work_data_bits(rebind_work)))
1735 continue;
1736
Tejun Heo25511a42012-07-17 12:39:27 -07001737 debug_work_activate(rebind_work);
Joonsoo Kime2b6a6d2012-08-15 23:25:40 +09001738
1739 /*
1740 * wq doesn't really matter but let's keep @worker->pool
1741 * and @cwq->pool consistent for sanity.
1742 */
1743 if (worker_pool_pri(worker->pool))
1744 wq = system_highpri_wq;
1745 else
1746 wq = system_wq;
1747
1748 insert_work(get_cwq(gcwq->cpu, wq), rebind_work,
1749 worker->scheduled.next,
1750 work_color_to_flags(WORK_NO_COLOR));
Tejun Heo25511a42012-07-17 12:39:27 -07001751 }
1752}
1753
Tejun Heoc34056a2010-06-29 10:07:11 +02001754static struct worker *alloc_worker(void)
1755{
1756 struct worker *worker;
1757
1758 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001759 if (worker) {
1760 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001761 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heo25511a42012-07-17 12:39:27 -07001762 INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn);
Tejun Heoe22bee72010-06-29 10:07:14 +02001763 /* on creation a worker is in !idle && prep state */
1764 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001765 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001766 return worker;
1767}
1768
1769/**
1770 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001771 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001772 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001773 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001774 * can be started by calling start_worker() or destroyed using
1775 * destroy_worker().
1776 *
1777 * CONTEXT:
1778 * Might sleep. Does GFP_KERNEL allocations.
1779 *
1780 * RETURNS:
1781 * Pointer to the newly created worker.
1782 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001783static struct worker *create_worker(struct worker_pool *pool)
Tejun Heoc34056a2010-06-29 10:07:11 +02001784{
Tejun Heo63d95a92012-07-12 14:46:37 -07001785 struct global_cwq *gcwq = pool->gcwq;
Tejun Heo32704762012-07-13 22:16:45 -07001786 const char *pri = worker_pool_pri(pool) ? "H" : "";
Tejun Heoc34056a2010-06-29 10:07:11 +02001787 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001788 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001789
Tejun Heo8b03ae32010-06-29 10:07:12 +02001790 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001791 while (ida_get_new(&pool->worker_ida, &id)) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001792 spin_unlock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001793 if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001794 goto fail;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001795 spin_lock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001796 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02001797 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001798
1799 worker = alloc_worker();
1800 if (!worker)
1801 goto fail;
1802
Tejun Heobd7bdd42012-07-12 14:46:37 -07001803 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001804 worker->id = id;
1805
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001806 if (gcwq->cpu != WORK_CPU_UNBOUND)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001807 worker->task = kthread_create_on_node(worker_thread,
Tejun Heo32704762012-07-13 22:16:45 -07001808 worker, cpu_to_node(gcwq->cpu),
1809 "kworker/%u:%d%s", gcwq->cpu, id, pri);
Tejun Heof3421792010-07-02 10:03:51 +02001810 else
1811 worker->task = kthread_create(worker_thread, worker,
Tejun Heo32704762012-07-13 22:16:45 -07001812 "kworker/u:%d%s", id, pri);
Tejun Heoc34056a2010-06-29 10:07:11 +02001813 if (IS_ERR(worker->task))
1814 goto fail;
1815
Tejun Heo32704762012-07-13 22:16:45 -07001816 if (worker_pool_pri(pool))
1817 set_user_nice(worker->task, HIGHPRI_NICE_LEVEL);
1818
Tejun Heodb7bccf2010-06-29 10:07:12 +02001819 /*
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001820 * Determine CPU binding of the new worker depending on
1821 * %GCWQ_DISASSOCIATED. The caller is responsible for ensuring the
1822 * flag remains stable across this function. See the comments
1823 * above the flag definition for details.
1824 *
1825 * As an unbound worker may later become a regular one if CPU comes
1826 * online, make sure every worker has %PF_THREAD_BOUND set.
Tejun Heodb7bccf2010-06-29 10:07:12 +02001827 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001828 if (!(gcwq->flags & GCWQ_DISASSOCIATED)) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001829 kthread_bind(worker->task, gcwq->cpu);
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001830 } else {
Tejun Heodb7bccf2010-06-29 10:07:12 +02001831 worker->task->flags |= PF_THREAD_BOUND;
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001832 worker->flags |= WORKER_UNBOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001834
Tejun Heoc34056a2010-06-29 10:07:11 +02001835 return worker;
1836fail:
1837 if (id >= 0) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001838 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001839 ida_remove(&pool->worker_ida, id);
Tejun Heo8b03ae32010-06-29 10:07:12 +02001840 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001841 }
1842 kfree(worker);
1843 return NULL;
1844}
1845
1846/**
1847 * start_worker - start a newly created worker
1848 * @worker: worker to start
1849 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001850 * Make the gcwq aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001851 *
1852 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001853 * spin_lock_irq(gcwq->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001854 */
1855static void start_worker(struct worker *worker)
1856{
Tejun Heocb444762010-07-02 10:03:50 +02001857 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001858 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001859 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001860 wake_up_process(worker->task);
1861}
1862
1863/**
1864 * destroy_worker - destroy a workqueue worker
1865 * @worker: worker to be destroyed
1866 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001867 * Destroy @worker and adjust @gcwq stats accordingly.
1868 *
1869 * CONTEXT:
1870 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001871 */
1872static void destroy_worker(struct worker *worker)
1873{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001874 struct worker_pool *pool = worker->pool;
1875 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001876 int id = worker->id;
1877
1878 /* sanity check frenzy */
1879 BUG_ON(worker->current_work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001880 BUG_ON(!list_empty(&worker->scheduled));
Tejun Heoc34056a2010-06-29 10:07:11 +02001881
Tejun Heoc8e55f32010-06-29 10:07:12 +02001882 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001883 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001884 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001885 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001886
1887 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001888 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001889
1890 spin_unlock_irq(&gcwq->lock);
1891
Tejun Heoc34056a2010-06-29 10:07:11 +02001892 kthread_stop(worker->task);
1893 kfree(worker);
1894
Tejun Heo8b03ae32010-06-29 10:07:12 +02001895 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001896 ida_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001897}
1898
Tejun Heo63d95a92012-07-12 14:46:37 -07001899static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001900{
Tejun Heo63d95a92012-07-12 14:46:37 -07001901 struct worker_pool *pool = (void *)__pool;
1902 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001903
1904 spin_lock_irq(&gcwq->lock);
1905
Tejun Heo63d95a92012-07-12 14:46:37 -07001906 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001907 struct worker *worker;
1908 unsigned long expires;
1909
1910 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001911 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001912 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1913
1914 if (time_before(jiffies, expires))
Tejun Heo63d95a92012-07-12 14:46:37 -07001915 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001916 else {
1917 /* it's been idle for too long, wake up manager */
Tejun Heo11ebea52012-07-12 14:46:37 -07001918 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -07001919 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001920 }
1921 }
1922
1923 spin_unlock_irq(&gcwq->lock);
1924}
1925
1926static bool send_mayday(struct work_struct *work)
1927{
1928 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1929 struct workqueue_struct *wq = cwq->wq;
Tejun Heof3421792010-07-02 10:03:51 +02001930 unsigned int cpu;
Tejun Heoe22bee72010-06-29 10:07:14 +02001931
1932 if (!(wq->flags & WQ_RESCUER))
1933 return false;
1934
1935 /* mayday mayday mayday */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001936 cpu = cwq->pool->gcwq->cpu;
Tejun Heof3421792010-07-02 10:03:51 +02001937 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1938 if (cpu == WORK_CPU_UNBOUND)
1939 cpu = 0;
Tejun Heof2e005a2010-07-20 15:59:09 +02001940 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001941 wake_up_process(wq->rescuer->task);
1942 return true;
1943}
1944
Tejun Heo63d95a92012-07-12 14:46:37 -07001945static void gcwq_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001946{
Tejun Heo63d95a92012-07-12 14:46:37 -07001947 struct worker_pool *pool = (void *)__pool;
1948 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001949 struct work_struct *work;
1950
1951 spin_lock_irq(&gcwq->lock);
1952
Tejun Heo63d95a92012-07-12 14:46:37 -07001953 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001954 /*
1955 * We've been trying to create a new worker but
1956 * haven't been successful. We might be hitting an
1957 * allocation deadlock. Send distress signals to
1958 * rescuers.
1959 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001960 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001961 send_mayday(work);
1962 }
1963
1964 spin_unlock_irq(&gcwq->lock);
1965
Tejun Heo63d95a92012-07-12 14:46:37 -07001966 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001967}
1968
1969/**
1970 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001971 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001972 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001973 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001974 * have at least one idle worker on return from this function. If
1975 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001976 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001977 * possible allocation deadlock.
1978 *
1979 * On return, need_to_create_worker() is guaranteed to be false and
1980 * may_start_working() true.
1981 *
1982 * LOCKING:
1983 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1984 * multiple times. Does GFP_KERNEL allocations. Called only from
1985 * manager.
1986 *
1987 * RETURNS:
1988 * false if no action was taken and gcwq->lock stayed locked, true
1989 * otherwise.
1990 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001991static bool maybe_create_worker(struct worker_pool *pool)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001992__releases(&gcwq->lock)
1993__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001994{
Tejun Heo63d95a92012-07-12 14:46:37 -07001995 struct global_cwq *gcwq = pool->gcwq;
1996
1997 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001998 return false;
1999restart:
Tejun Heo9f9c2362010-07-14 11:31:20 +02002000 spin_unlock_irq(&gcwq->lock);
2001
Tejun Heoe22bee72010-06-29 10:07:14 +02002002 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07002003 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02002004
2005 while (true) {
2006 struct worker *worker;
2007
Tejun Heobc2ae0f2012-07-17 12:39:27 -07002008 worker = create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002009 if (worker) {
Tejun Heo63d95a92012-07-12 14:46:37 -07002010 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02002011 spin_lock_irq(&gcwq->lock);
2012 start_worker(worker);
Tejun Heo63d95a92012-07-12 14:46:37 -07002013 BUG_ON(need_to_create_worker(pool));
Tejun Heoe22bee72010-06-29 10:07:14 +02002014 return true;
2015 }
2016
Tejun Heo63d95a92012-07-12 14:46:37 -07002017 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002018 break;
2019
Tejun Heoe22bee72010-06-29 10:07:14 +02002020 __set_current_state(TASK_INTERRUPTIBLE);
2021 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02002022
Tejun Heo63d95a92012-07-12 14:46:37 -07002023 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002024 break;
2025 }
2026
Tejun Heo63d95a92012-07-12 14:46:37 -07002027 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02002028 spin_lock_irq(&gcwq->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07002029 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002030 goto restart;
2031 return true;
2032}
2033
2034/**
2035 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo63d95a92012-07-12 14:46:37 -07002036 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02002037 *
Tejun Heo63d95a92012-07-12 14:46:37 -07002038 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02002039 * IDLE_WORKER_TIMEOUT.
2040 *
2041 * LOCKING:
2042 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
2043 * multiple times. Called only from manager.
2044 *
2045 * RETURNS:
2046 * false if no action was taken and gcwq->lock stayed locked, true
2047 * otherwise.
2048 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002049static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02002050{
2051 bool ret = false;
2052
Tejun Heo63d95a92012-07-12 14:46:37 -07002053 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02002054 struct worker *worker;
2055 unsigned long expires;
2056
Tejun Heo63d95a92012-07-12 14:46:37 -07002057 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02002058 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
2059
2060 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07002061 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02002062 break;
2063 }
2064
2065 destroy_worker(worker);
2066 ret = true;
2067 }
2068
2069 return ret;
2070}
2071
2072/**
2073 * manage_workers - manage worker pool
2074 * @worker: self
2075 *
2076 * Assume the manager role and manage gcwq worker pool @worker belongs
2077 * to. At any given time, there can be only zero or one manager per
2078 * gcwq. The exclusion is handled automatically by this function.
2079 *
2080 * The caller can safely start processing works on false return. On
2081 * true return, it's guaranteed that need_to_create_worker() is false
2082 * and may_start_working() is true.
2083 *
2084 * CONTEXT:
2085 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
2086 * multiple times. Does GFP_KERNEL allocations.
2087 *
2088 * RETURNS:
2089 * false if no action was taken and gcwq->lock stayed locked, true if
2090 * some action was taken.
2091 */
2092static bool manage_workers(struct worker *worker)
2093{
Tejun Heo63d95a92012-07-12 14:46:37 -07002094 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002095 bool ret = false;
2096
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002097 if (pool->flags & POOL_MANAGING_WORKERS)
Tejun Heoe22bee72010-06-29 10:07:14 +02002098 return ret;
2099
Lai Jiangshan552a37e2012-09-10 10:03:33 -07002100 pool->flags |= POOL_MANAGING_WORKERS;
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002101
2102 /*
2103 * To simplify both worker management and CPU hotplug, hold off
2104 * management while hotplug is in progress. CPU hotplug path can't
2105 * grab %POOL_MANAGING_WORKERS to achieve this because that can
2106 * lead to idle worker depletion (all become busy thinking someone
2107 * else is managing) which in turn can result in deadlock under
2108 * extreme circumstances. Use @pool->manager_mutex to synchronize
2109 * manager against CPU hotplug.
2110 *
2111 * manager_mutex would always be free unless CPU hotplug is in
2112 * progress. trylock first without dropping @gcwq->lock.
2113 */
2114 if (unlikely(!mutex_trylock(&pool->manager_mutex))) {
2115 spin_unlock_irq(&pool->gcwq->lock);
2116 mutex_lock(&pool->manager_mutex);
2117 /*
2118 * CPU hotplug could have happened while we were waiting
2119 * for manager_mutex. Hotplug itself can't handle us
2120 * because manager isn't either on idle or busy list, and
2121 * @gcwq's state and ours could have deviated.
2122 *
2123 * As hotplug is now excluded via manager_mutex, we can
2124 * simply try to bind. It will succeed or fail depending
2125 * on @gcwq's current state. Try it and adjust
2126 * %WORKER_UNBOUND accordingly.
2127 */
2128 if (worker_maybe_bind_and_lock(worker))
2129 worker->flags &= ~WORKER_UNBOUND;
2130 else
2131 worker->flags |= WORKER_UNBOUND;
2132
2133 ret = true;
2134 }
2135
Tejun Heo11ebea52012-07-12 14:46:37 -07002136 pool->flags &= ~POOL_MANAGE_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02002137
2138 /*
2139 * Destroy and then create so that may_start_working() is true
2140 * on return.
2141 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002142 ret |= maybe_destroy_workers(pool);
2143 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002144
Lai Jiangshan552a37e2012-09-10 10:03:33 -07002145 pool->flags &= ~POOL_MANAGING_WORKERS;
Tejun Heo60373152012-07-17 12:39:27 -07002146 mutex_unlock(&pool->manager_mutex);
Tejun Heoe22bee72010-06-29 10:07:14 +02002147 return ret;
2148}
2149
Tejun Heoa62428c2010-06-29 10:07:10 +02002150/**
2151 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02002152 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02002153 * @work: work to process
2154 *
2155 * Process @work. This function contains all the logics necessary to
2156 * process a single work including synchronization against and
2157 * interaction with other workers on the same cpu, queueing and
2158 * flushing. As long as context requirement is met, any worker can
2159 * call this function to process a work.
2160 *
2161 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002162 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02002163 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002164static void process_one_work(struct worker *worker, struct work_struct *work)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09002165__releases(&gcwq->lock)
2166__acquires(&gcwq->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02002167{
Tejun Heo7e116292010-06-29 10:07:13 +02002168 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002169 struct worker_pool *pool = worker->pool;
2170 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002171 struct hlist_head *bwh = busy_worker_head(gcwq, work);
Tejun Heofb0e7be2010-06-29 10:07:15 +02002172 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heoa62428c2010-06-29 10:07:10 +02002173 work_func_t f = work->func;
Tejun Heo73f53c42010-06-29 10:07:11 +02002174 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02002175 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02002176#ifdef CONFIG_LOCKDEP
2177 /*
2178 * It is permissible to free the struct work_struct from
2179 * inside the function that is called from it, this we need to
2180 * take into account for lockdep too. To avoid bogus "held
2181 * lock freed" warnings as well as problems when looking into
2182 * work->lockdep_map, make a copy and use that here.
2183 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07002184 struct lockdep_map lockdep_map;
2185
2186 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002187#endif
Tejun Heo6fec10a2012-07-22 10:16:34 -07002188 /*
2189 * Ensure we're on the correct CPU. DISASSOCIATED test is
2190 * necessary to avoid spurious warnings from rescuers servicing the
2191 * unbound or a disassociated gcwq.
2192 */
Tejun Heo25511a42012-07-17 12:39:27 -07002193 WARN_ON_ONCE(!(worker->flags & (WORKER_UNBOUND | WORKER_REBIND)) &&
Tejun Heo6fec10a2012-07-22 10:16:34 -07002194 !(gcwq->flags & GCWQ_DISASSOCIATED) &&
Tejun Heo25511a42012-07-17 12:39:27 -07002195 raw_smp_processor_id() != gcwq->cpu);
2196
Tejun Heo7e116292010-06-29 10:07:13 +02002197 /*
2198 * A single work shouldn't be executed concurrently by
2199 * multiple workers on a single cpu. Check whether anyone is
2200 * already processing the work. If so, defer the work to the
2201 * currently executing one.
2202 */
2203 collision = __find_worker_executing_work(gcwq, bwh, work);
2204 if (unlikely(collision)) {
2205 move_linked_works(work, &collision->scheduled, NULL);
2206 return;
2207 }
2208
Tejun Heo8930cab2012-08-03 10:30:45 -07002209 /* claim and dequeue */
Tejun Heoa62428c2010-06-29 10:07:10 +02002210 debug_work_deactivate(work);
Tejun Heoc8e55f32010-06-29 10:07:12 +02002211 hlist_add_head(&worker->hentry, bwh);
Tejun Heoc34056a2010-06-29 10:07:11 +02002212 worker->current_work = work;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02002213 worker->current_cwq = cwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002214 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002215
Tejun Heoa62428c2010-06-29 10:07:10 +02002216 list_del_init(&work->entry);
2217
Tejun Heo649027d2010-06-29 10:07:14 +02002218 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02002219 * CPU intensive works don't participate in concurrency
2220 * management. They're the scheduler's responsibility.
2221 */
2222 if (unlikely(cpu_intensive))
2223 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
2224
Tejun Heo974271c2012-07-12 14:46:37 -07002225 /*
2226 * Unbound gcwq isn't concurrency managed and work items should be
2227 * executed ASAP. Wake up another worker if necessary.
2228 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002229 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
2230 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07002231
Tejun Heo8930cab2012-08-03 10:30:45 -07002232 /*
Tejun Heo23657bb2012-08-13 17:08:19 -07002233 * Record the last CPU and clear PENDING which should be the last
2234 * update to @work. Also, do this inside @gcwq->lock so that
2235 * PENDING and queued state changes happen together while IRQ is
2236 * disabled.
Tejun Heo8930cab2012-08-03 10:30:45 -07002237 */
Tejun Heo8930cab2012-08-03 10:30:45 -07002238 set_work_cpu_and_clear_pending(work, gcwq->cpu);
Tejun Heoa62428c2010-06-29 10:07:10 +02002239
Tejun Heo8930cab2012-08-03 10:30:45 -07002240 spin_unlock_irq(&gcwq->lock);
Tejun Heo959d1af2012-08-03 10:30:45 -07002241
Tejun Heoe1594892011-01-09 23:32:15 +01002242 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002243 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002244 trace_workqueue_execute_start(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002245 f(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002246 /*
2247 * While we must be careful to not use "work" after this, the trace
2248 * point will only record its address.
2249 */
2250 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002251 lock_map_release(&lockdep_map);
2252 lock_map_release(&cwq->wq->lockdep_map);
2253
2254 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Valentin Ilie044c7822012-08-19 00:52:42 +03002255 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
2256 " last function: %pf\n",
2257 current->comm, preempt_count(), task_pid_nr(current), f);
Tejun Heoa62428c2010-06-29 10:07:10 +02002258 debug_show_held_locks(current);
2259 dump_stack();
2260 }
2261
Tejun Heo8b03ae32010-06-29 10:07:12 +02002262 spin_lock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002263
Tejun Heofb0e7be2010-06-29 10:07:15 +02002264 /* clear cpu intensive status */
2265 if (unlikely(cpu_intensive))
2266 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2267
Tejun Heoa62428c2010-06-29 10:07:10 +02002268 /* we're done with it, release */
Tejun Heoc8e55f32010-06-29 10:07:12 +02002269 hlist_del_init(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002270 worker->current_work = NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02002271 worker->current_cwq = NULL;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02002272 cwq_dec_nr_in_flight(cwq, work_color, false);
Tejun Heoa62428c2010-06-29 10:07:10 +02002273}
2274
Tejun Heoaffee4b2010-06-29 10:07:12 +02002275/**
2276 * process_scheduled_works - process scheduled works
2277 * @worker: self
2278 *
2279 * Process all scheduled works. Please note that the scheduled list
2280 * may change while processing a work, so this function repeatedly
2281 * fetches a work from the top and executes it.
2282 *
2283 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002284 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02002285 * multiple times.
2286 */
2287static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002289 while (!list_empty(&worker->scheduled)) {
2290 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002292 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294}
2295
Tejun Heo4690c4a2010-06-29 10:07:10 +02002296/**
2297 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02002298 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02002299 *
Tejun Heoe22bee72010-06-29 10:07:14 +02002300 * The gcwq worker thread function. There's a single dynamic pool of
2301 * these per each cpu. These workers process all works regardless of
2302 * their specific target workqueue. The only exception is works which
2303 * belong to workqueues with a rescuer which will be explained in
2304 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02002305 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002306static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307{
Tejun Heoc34056a2010-06-29 10:07:11 +02002308 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07002309 struct worker_pool *pool = worker->pool;
2310 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Tejun Heoe22bee72010-06-29 10:07:14 +02002312 /* tell the scheduler that this is a workqueue worker */
2313 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002314woke_up:
Tejun Heoc8e55f32010-06-29 10:07:12 +02002315 spin_lock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Tejun Heo25511a42012-07-17 12:39:27 -07002317 /*
2318 * DIE can be set only while idle and REBIND set while busy has
2319 * @worker->rebind_work scheduled. Checking here is enough.
2320 */
2321 if (unlikely(worker->flags & (WORKER_REBIND | WORKER_DIE))) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02002322 spin_unlock_irq(&gcwq->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07002323
2324 if (worker->flags & WORKER_DIE) {
2325 worker->task->flags &= ~PF_WQ_WORKER;
2326 return 0;
2327 }
2328
2329 idle_worker_rebind(worker);
2330 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 }
2332
Tejun Heoc8e55f32010-06-29 10:07:12 +02002333 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002334recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002335 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002336 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002337 goto sleep;
2338
2339 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002340 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002341 goto recheck;
2342
Tejun Heoc8e55f32010-06-29 10:07:12 +02002343 /*
2344 * ->scheduled list can only be filled while a worker is
2345 * preparing to process a work or actually processing it.
2346 * Make sure nobody diddled with it while I was sleeping.
2347 */
2348 BUG_ON(!list_empty(&worker->scheduled));
2349
Tejun Heoe22bee72010-06-29 10:07:14 +02002350 /*
2351 * When control reaches this point, we're guaranteed to have
2352 * at least one idle worker or that someone else has already
2353 * assumed the manager role.
2354 */
2355 worker_clr_flags(worker, WORKER_PREP);
2356
2357 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002358 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002359 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002360 struct work_struct, entry);
2361
2362 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2363 /* optimization path, not strictly necessary */
2364 process_one_work(worker, work);
2365 if (unlikely(!list_empty(&worker->scheduled)))
2366 process_scheduled_works(worker);
2367 } else {
2368 move_linked_works(work, &worker->scheduled, NULL);
2369 process_scheduled_works(worker);
2370 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002371 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002372
Tejun Heoe22bee72010-06-29 10:07:14 +02002373 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002374sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07002375 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002376 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002377
Tejun Heoc8e55f32010-06-29 10:07:12 +02002378 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02002379 * gcwq->lock is held and there's no work to process and no
2380 * need to manage, sleep. Workers are woken up only while
2381 * holding gcwq->lock or from local cpu, so setting the
2382 * current state before releasing gcwq->lock is enough to
2383 * prevent losing any event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002384 */
2385 worker_enter_idle(worker);
2386 __set_current_state(TASK_INTERRUPTIBLE);
2387 spin_unlock_irq(&gcwq->lock);
2388 schedule();
2389 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390}
2391
Tejun Heoe22bee72010-06-29 10:07:14 +02002392/**
2393 * rescuer_thread - the rescuer thread function
2394 * @__wq: the associated workqueue
2395 *
2396 * Workqueue rescuer thread function. There's one rescuer for each
2397 * workqueue which has WQ_RESCUER set.
2398 *
2399 * Regular work processing on a gcwq may block trying to create a new
2400 * worker which uses GFP_KERNEL allocation which has slight chance of
2401 * developing into deadlock if some works currently on the same queue
2402 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2403 * the problem rescuer solves.
2404 *
2405 * When such condition is possible, the gcwq summons rescuers of all
2406 * workqueues which have works queued on the gcwq and let them process
2407 * those works so that forward progress can be guaranteed.
2408 *
2409 * This should happen rarely.
2410 */
2411static int rescuer_thread(void *__wq)
2412{
2413 struct workqueue_struct *wq = __wq;
2414 struct worker *rescuer = wq->rescuer;
2415 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heof3421792010-07-02 10:03:51 +02002416 bool is_unbound = wq->flags & WQ_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02002417 unsigned int cpu;
2418
2419 set_user_nice(current, RESCUER_NICE_LEVEL);
2420repeat:
2421 set_current_state(TASK_INTERRUPTIBLE);
2422
2423 if (kthread_should_stop())
2424 return 0;
2425
Tejun Heof3421792010-07-02 10:03:51 +02002426 /*
2427 * See whether any cpu is asking for help. Unbounded
2428 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
2429 */
Tejun Heof2e005a2010-07-20 15:59:09 +02002430 for_each_mayday_cpu(cpu, wq->mayday_mask) {
Tejun Heof3421792010-07-02 10:03:51 +02002431 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2432 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002433 struct worker_pool *pool = cwq->pool;
2434 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002435 struct work_struct *work, *n;
2436
2437 __set_current_state(TASK_RUNNING);
Tejun Heof2e005a2010-07-20 15:59:09 +02002438 mayday_clear_cpu(cpu, wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002439
2440 /* migrate to the target cpu if possible */
Tejun Heobd7bdd42012-07-12 14:46:37 -07002441 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002442 worker_maybe_bind_and_lock(rescuer);
2443
2444 /*
2445 * Slurp in all works issued via this workqueue and
2446 * process'em.
2447 */
2448 BUG_ON(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002449 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02002450 if (get_work_cwq(work) == cwq)
2451 move_linked_works(work, scheduled, &n);
2452
2453 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002454
2455 /*
2456 * Leave this gcwq. If keep_working() is %true, notify a
2457 * regular worker; otherwise, we end up with 0 concurrency
2458 * and stalling the execution.
2459 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002460 if (keep_working(pool))
2461 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002462
Tejun Heoe22bee72010-06-29 10:07:14 +02002463 spin_unlock_irq(&gcwq->lock);
2464 }
2465
2466 schedule();
2467 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468}
2469
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002470struct wq_barrier {
2471 struct work_struct work;
2472 struct completion done;
2473};
2474
2475static void wq_barrier_func(struct work_struct *work)
2476{
2477 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2478 complete(&barr->done);
2479}
2480
Tejun Heo4690c4a2010-06-29 10:07:10 +02002481/**
2482 * insert_wq_barrier - insert a barrier work
2483 * @cwq: cwq to insert barrier into
2484 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002485 * @target: target work to attach @barr to
2486 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002487 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002488 * @barr is linked to @target such that @barr is completed only after
2489 * @target finishes execution. Please note that the ordering
2490 * guarantee is observed only with respect to @target and on the local
2491 * cpu.
2492 *
2493 * Currently, a queued barrier can't be canceled. This is because
2494 * try_to_grab_pending() can't determine whether the work to be
2495 * grabbed is at the head of the queue and thus can't clear LINKED
2496 * flag of the previous work while there must be a valid next work
2497 * after a work with LINKED flag set.
2498 *
2499 * Note that when @worker is non-NULL, @target may be modified
2500 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002501 *
2502 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002503 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002504 */
Oleg Nesterov83c22522007-05-09 02:33:54 -07002505static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002506 struct wq_barrier *barr,
2507 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002508{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002509 struct list_head *head;
2510 unsigned int linked = 0;
2511
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002512 /*
Tejun Heo8b03ae32010-06-29 10:07:12 +02002513 * debugobject calls are safe here even with gcwq->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002514 * as we know for sure that this will not trigger any of the
2515 * checks and call back into the fixup functions where we
2516 * might deadlock.
2517 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002518 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002519 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002520 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002521
Tejun Heoaffee4b2010-06-29 10:07:12 +02002522 /*
2523 * If @target is currently being executed, schedule the
2524 * barrier to the worker; otherwise, put it after @target.
2525 */
2526 if (worker)
2527 head = worker->scheduled.next;
2528 else {
2529 unsigned long *bits = work_data_bits(target);
2530
2531 head = target->entry.next;
2532 /* there can already be other linked works, inherit and set */
2533 linked = *bits & WORK_STRUCT_LINKED;
2534 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2535 }
2536
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002537 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002538 insert_work(cwq, &barr->work, head,
2539 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002540}
2541
Tejun Heo73f53c42010-06-29 10:07:11 +02002542/**
2543 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2544 * @wq: workqueue being flushed
2545 * @flush_color: new flush color, < 0 for no-op
2546 * @work_color: new work color, < 0 for no-op
2547 *
2548 * Prepare cwqs for workqueue flushing.
2549 *
2550 * If @flush_color is non-negative, flush_color on all cwqs should be
2551 * -1. If no cwq has in-flight commands at the specified color, all
2552 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2553 * has in flight commands, its cwq->flush_color is set to
2554 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2555 * wakeup logic is armed and %true is returned.
2556 *
2557 * The caller should have initialized @wq->first_flusher prior to
2558 * calling this function with non-negative @flush_color. If
2559 * @flush_color is negative, no flush color update is done and %false
2560 * is returned.
2561 *
2562 * If @work_color is non-negative, all cwqs should have the same
2563 * work_color which is previous to @work_color and all will be
2564 * advanced to @work_color.
2565 *
2566 * CONTEXT:
2567 * mutex_lock(wq->flush_mutex).
2568 *
2569 * RETURNS:
2570 * %true if @flush_color >= 0 and there's something to flush. %false
2571 * otherwise.
2572 */
2573static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2574 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575{
Tejun Heo73f53c42010-06-29 10:07:11 +02002576 bool wait = false;
2577 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -07002578
Tejun Heo73f53c42010-06-29 10:07:11 +02002579 if (flush_color >= 0) {
2580 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2581 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002582 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002583
Tejun Heof3421792010-07-02 10:03:51 +02002584 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002585 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002586 struct global_cwq *gcwq = cwq->pool->gcwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002587
Tejun Heo8b03ae32010-06-29 10:07:12 +02002588 spin_lock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002589
2590 if (flush_color >= 0) {
2591 BUG_ON(cwq->flush_color != -1);
2592
2593 if (cwq->nr_in_flight[flush_color]) {
2594 cwq->flush_color = flush_color;
2595 atomic_inc(&wq->nr_cwqs_to_flush);
2596 wait = true;
2597 }
2598 }
2599
2600 if (work_color >= 0) {
2601 BUG_ON(work_color != work_next_color(cwq->work_color));
2602 cwq->work_color = work_color;
2603 }
2604
Tejun Heo8b03ae32010-06-29 10:07:12 +02002605 spin_unlock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002606 }
2607
2608 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2609 complete(&wq->first_flusher->done);
2610
2611 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612}
2613
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002614/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002616 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 *
2618 * Forces execution of the workqueue and blocks until its completion.
2619 * This is typically used in driver shutdown handlers.
2620 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002621 * We sleep until all works which were queued on entry have been handled,
2622 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002624void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625{
Tejun Heo73f53c42010-06-29 10:07:11 +02002626 struct wq_flusher this_flusher = {
2627 .list = LIST_HEAD_INIT(this_flusher.list),
2628 .flush_color = -1,
2629 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2630 };
2631 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002632
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002633 lock_map_acquire(&wq->lockdep_map);
2634 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002635
2636 mutex_lock(&wq->flush_mutex);
2637
2638 /*
2639 * Start-to-wait phase
2640 */
2641 next_color = work_next_color(wq->work_color);
2642
2643 if (next_color != wq->flush_color) {
2644 /*
2645 * Color space is not full. The current work_color
2646 * becomes our flush_color and work_color is advanced
2647 * by one.
2648 */
2649 BUG_ON(!list_empty(&wq->flusher_overflow));
2650 this_flusher.flush_color = wq->work_color;
2651 wq->work_color = next_color;
2652
2653 if (!wq->first_flusher) {
2654 /* no flush in progress, become the first flusher */
2655 BUG_ON(wq->flush_color != this_flusher.flush_color);
2656
2657 wq->first_flusher = &this_flusher;
2658
2659 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2660 wq->work_color)) {
2661 /* nothing to flush, done */
2662 wq->flush_color = next_color;
2663 wq->first_flusher = NULL;
2664 goto out_unlock;
2665 }
2666 } else {
2667 /* wait in queue */
2668 BUG_ON(wq->flush_color == this_flusher.flush_color);
2669 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2670 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2671 }
2672 } else {
2673 /*
2674 * Oops, color space is full, wait on overflow queue.
2675 * The next flush completion will assign us
2676 * flush_color and transfer to flusher_queue.
2677 */
2678 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2679 }
2680
2681 mutex_unlock(&wq->flush_mutex);
2682
2683 wait_for_completion(&this_flusher.done);
2684
2685 /*
2686 * Wake-up-and-cascade phase
2687 *
2688 * First flushers are responsible for cascading flushes and
2689 * handling overflow. Non-first flushers can simply return.
2690 */
2691 if (wq->first_flusher != &this_flusher)
2692 return;
2693
2694 mutex_lock(&wq->flush_mutex);
2695
Tejun Heo4ce48b32010-07-02 10:03:51 +02002696 /* we might have raced, check again with mutex held */
2697 if (wq->first_flusher != &this_flusher)
2698 goto out_unlock;
2699
Tejun Heo73f53c42010-06-29 10:07:11 +02002700 wq->first_flusher = NULL;
2701
2702 BUG_ON(!list_empty(&this_flusher.list));
2703 BUG_ON(wq->flush_color != this_flusher.flush_color);
2704
2705 while (true) {
2706 struct wq_flusher *next, *tmp;
2707
2708 /* complete all the flushers sharing the current flush color */
2709 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2710 if (next->flush_color != wq->flush_color)
2711 break;
2712 list_del_init(&next->list);
2713 complete(&next->done);
2714 }
2715
2716 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2717 wq->flush_color != work_next_color(wq->work_color));
2718
2719 /* this flush_color is finished, advance by one */
2720 wq->flush_color = work_next_color(wq->flush_color);
2721
2722 /* one color has been freed, handle overflow queue */
2723 if (!list_empty(&wq->flusher_overflow)) {
2724 /*
2725 * Assign the same color to all overflowed
2726 * flushers, advance work_color and append to
2727 * flusher_queue. This is the start-to-wait
2728 * phase for these overflowed flushers.
2729 */
2730 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2731 tmp->flush_color = wq->work_color;
2732
2733 wq->work_color = work_next_color(wq->work_color);
2734
2735 list_splice_tail_init(&wq->flusher_overflow,
2736 &wq->flusher_queue);
2737 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2738 }
2739
2740 if (list_empty(&wq->flusher_queue)) {
2741 BUG_ON(wq->flush_color != wq->work_color);
2742 break;
2743 }
2744
2745 /*
2746 * Need to flush more colors. Make the next flusher
2747 * the new first flusher and arm cwqs.
2748 */
2749 BUG_ON(wq->flush_color == wq->work_color);
2750 BUG_ON(wq->flush_color != next->flush_color);
2751
2752 list_del_init(&next->list);
2753 wq->first_flusher = next;
2754
2755 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2756 break;
2757
2758 /*
2759 * Meh... this color is already done, clear first
2760 * flusher and repeat cascading.
2761 */
2762 wq->first_flusher = NULL;
2763 }
2764
2765out_unlock:
2766 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767}
Dave Jonesae90dd52006-06-30 01:40:45 -04002768EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002770/**
2771 * drain_workqueue - drain a workqueue
2772 * @wq: workqueue to drain
2773 *
2774 * Wait until the workqueue becomes empty. While draining is in progress,
2775 * only chain queueing is allowed. IOW, only currently pending or running
2776 * work items on @wq can queue further work items on it. @wq is flushed
2777 * repeatedly until it becomes empty. The number of flushing is detemined
2778 * by the depth of chaining and should be relatively short. Whine if it
2779 * takes too long.
2780 */
2781void drain_workqueue(struct workqueue_struct *wq)
2782{
2783 unsigned int flush_cnt = 0;
2784 unsigned int cpu;
2785
2786 /*
2787 * __queue_work() needs to test whether there are drainers, is much
2788 * hotter than drain_workqueue() and already looks at @wq->flags.
2789 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2790 */
2791 spin_lock(&workqueue_lock);
2792 if (!wq->nr_drainers++)
2793 wq->flags |= WQ_DRAINING;
2794 spin_unlock(&workqueue_lock);
2795reflush:
2796 flush_workqueue(wq);
2797
2798 for_each_cwq_cpu(cpu, wq) {
2799 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002800 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002801
Tejun Heobd7bdd42012-07-12 14:46:37 -07002802 spin_lock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002803 drained = !cwq->nr_active && list_empty(&cwq->delayed_works);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002804 spin_unlock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002805
2806 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002807 continue;
2808
2809 if (++flush_cnt == 10 ||
2810 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
Valentin Ilie044c7822012-08-19 00:52:42 +03002811 pr_warn("workqueue %s: flush on destruction isn't complete after %u tries\n",
2812 wq->name, flush_cnt);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002813 goto reflush;
2814 }
2815
2816 spin_lock(&workqueue_lock);
2817 if (!--wq->nr_drainers)
2818 wq->flags &= ~WQ_DRAINING;
2819 spin_unlock(&workqueue_lock);
2820}
2821EXPORT_SYMBOL_GPL(drain_workqueue);
2822
Tejun Heo606a5022012-08-20 14:51:23 -07002823static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
Tejun Heobaf59022010-09-16 10:42:16 +02002824{
2825 struct worker *worker = NULL;
2826 struct global_cwq *gcwq;
2827 struct cpu_workqueue_struct *cwq;
2828
2829 might_sleep();
2830 gcwq = get_work_gcwq(work);
2831 if (!gcwq)
2832 return false;
2833
2834 spin_lock_irq(&gcwq->lock);
2835 if (!list_empty(&work->entry)) {
2836 /*
2837 * See the comment near try_to_grab_pending()->smp_rmb().
2838 * If it was re-queued to a different gcwq under us, we
2839 * are not going to wait.
2840 */
2841 smp_rmb();
2842 cwq = get_work_cwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002843 if (unlikely(!cwq || gcwq != cwq->pool->gcwq))
Tejun Heobaf59022010-09-16 10:42:16 +02002844 goto already_gone;
Tejun Heo606a5022012-08-20 14:51:23 -07002845 } else {
Tejun Heobaf59022010-09-16 10:42:16 +02002846 worker = find_worker_executing_work(gcwq, work);
2847 if (!worker)
2848 goto already_gone;
2849 cwq = worker->current_cwq;
Tejun Heo606a5022012-08-20 14:51:23 -07002850 }
Tejun Heobaf59022010-09-16 10:42:16 +02002851
2852 insert_wq_barrier(cwq, barr, work, worker);
2853 spin_unlock_irq(&gcwq->lock);
2854
Tejun Heoe1594892011-01-09 23:32:15 +01002855 /*
2856 * If @max_active is 1 or rescuer is in use, flushing another work
2857 * item on the same workqueue may lead to deadlock. Make sure the
2858 * flusher is not running on the same workqueue by verifying write
2859 * access.
2860 */
2861 if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER)
2862 lock_map_acquire(&cwq->wq->lockdep_map);
2863 else
2864 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heobaf59022010-09-16 10:42:16 +02002865 lock_map_release(&cwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002866
Tejun Heobaf59022010-09-16 10:42:16 +02002867 return true;
2868already_gone:
2869 spin_unlock_irq(&gcwq->lock);
2870 return false;
2871}
2872
Oleg Nesterovdb700892008-07-25 01:47:49 -07002873/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002874 * flush_work - wait for a work to finish executing the last queueing instance
2875 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002876 *
Tejun Heo606a5022012-08-20 14:51:23 -07002877 * Wait until @work has finished execution. @work is guaranteed to be idle
2878 * on return if it hasn't been requeued since flush started.
Tejun Heo401a8d02010-09-16 10:36:00 +02002879 *
2880 * RETURNS:
2881 * %true if flush_work() waited for the work to finish execution,
2882 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002883 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002884bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002885{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002886 struct wq_barrier barr;
2887
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002888 lock_map_acquire(&work->lockdep_map);
2889 lock_map_release(&work->lockdep_map);
2890
Tejun Heo606a5022012-08-20 14:51:23 -07002891 if (start_flush_work(work, &barr)) {
Tejun Heobaf59022010-09-16 10:42:16 +02002892 wait_for_completion(&barr.done);
2893 destroy_work_on_stack(&barr.work);
2894 return true;
Tejun Heo606a5022012-08-20 14:51:23 -07002895 } else {
Tejun Heobaf59022010-09-16 10:42:16 +02002896 return false;
Tejun Heo606a5022012-08-20 14:51:23 -07002897 }
Oleg Nesterovdb700892008-07-25 01:47:49 -07002898}
2899EXPORT_SYMBOL_GPL(flush_work);
2900
Tejun Heo36e227d2012-08-03 10:30:46 -07002901static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002902{
Tejun Heobbb68df2012-08-03 10:30:46 -07002903 unsigned long flags;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002904 int ret;
2905
2906 do {
Tejun Heobbb68df2012-08-03 10:30:46 -07002907 ret = try_to_grab_pending(work, is_dwork, &flags);
2908 /*
2909 * If someone else is canceling, wait for the same event it
2910 * would be waiting for before retrying.
2911 */
2912 if (unlikely(ret == -ENOENT))
Tejun Heo606a5022012-08-20 14:51:23 -07002913 flush_work(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002914 } while (unlikely(ret < 0));
2915
Tejun Heobbb68df2012-08-03 10:30:46 -07002916 /* tell other tasks trying to grab @work to back off */
2917 mark_work_canceling(work);
2918 local_irq_restore(flags);
2919
Tejun Heo606a5022012-08-20 14:51:23 -07002920 flush_work(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002921 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002922 return ret;
2923}
2924
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002925/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002926 * cancel_work_sync - cancel a work and wait for it to finish
2927 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002928 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002929 * Cancel @work and wait for its execution to finish. This function
2930 * can be used even if the work re-queues itself or migrates to
2931 * another workqueue. On return from this function, @work is
2932 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002933 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002934 * cancel_work_sync(&delayed_work->work) must not be used for
2935 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002936 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002937 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002938 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002939 *
2940 * RETURNS:
2941 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002942 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002943bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002944{
Tejun Heo36e227d2012-08-03 10:30:46 -07002945 return __cancel_work_timer(work, false);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002946}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002947EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002948
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002949/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002950 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2951 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002952 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002953 * Delayed timer is cancelled and the pending work is queued for
2954 * immediate execution. Like flush_work(), this function only
2955 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002956 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002957 * RETURNS:
2958 * %true if flush_work() waited for the work to finish execution,
2959 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002960 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002961bool flush_delayed_work(struct delayed_work *dwork)
2962{
Tejun Heo8930cab2012-08-03 10:30:45 -07002963 local_irq_disable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002964 if (del_timer_sync(&dwork->timer))
Tejun Heo12650572012-08-08 09:38:42 -07002965 __queue_work(dwork->cpu,
Tejun Heo401a8d02010-09-16 10:36:00 +02002966 get_work_cwq(&dwork->work)->wq, &dwork->work);
Tejun Heo8930cab2012-08-03 10:30:45 -07002967 local_irq_enable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002968 return flush_work(&dwork->work);
2969}
2970EXPORT_SYMBOL(flush_delayed_work);
2971
2972/**
Tejun Heo57b30ae2012-08-21 13:18:24 -07002973 * cancel_delayed_work - cancel a delayed work
2974 * @dwork: delayed_work to cancel
2975 *
2976 * Kill off a pending delayed_work. Returns %true if @dwork was pending
2977 * and canceled; %false if wasn't pending. Note that the work callback
2978 * function may still be running on return, unless it returns %true and the
2979 * work doesn't re-arm itself. Explicitly flush or use
2980 * cancel_delayed_work_sync() to wait on it.
2981 *
2982 * This function is safe to call from any context including IRQ handler.
2983 */
2984bool cancel_delayed_work(struct delayed_work *dwork)
2985{
2986 unsigned long flags;
2987 int ret;
2988
2989 do {
2990 ret = try_to_grab_pending(&dwork->work, true, &flags);
2991 } while (unlikely(ret == -EAGAIN));
2992
2993 if (unlikely(ret < 0))
2994 return false;
2995
2996 set_work_cpu_and_clear_pending(&dwork->work, work_cpu(&dwork->work));
2997 local_irq_restore(flags);
2998 return true;
2999}
3000EXPORT_SYMBOL(cancel_delayed_work);
3001
3002/**
Tejun Heo401a8d02010-09-16 10:36:00 +02003003 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
3004 * @dwork: the delayed work cancel
3005 *
3006 * This is cancel_work_sync() for delayed works.
3007 *
3008 * RETURNS:
3009 * %true if @dwork was pending, %false otherwise.
3010 */
3011bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07003012{
Tejun Heo36e227d2012-08-03 10:30:46 -07003013 return __cancel_work_timer(&dwork->work, true);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07003014}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07003015EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
Tejun Heod4283e92012-08-03 10:30:44 -07003017/**
Tejun Heo0a13c002012-08-03 10:30:44 -07003018 * schedule_work_on - put work task on a specific cpu
3019 * @cpu: cpu to put the work task on
3020 * @work: job to be done
3021 *
3022 * This puts a job on a specific cpu
3023 */
Tejun Heod4283e92012-08-03 10:30:44 -07003024bool schedule_work_on(int cpu, struct work_struct *work)
Tejun Heo0a13c002012-08-03 10:30:44 -07003025{
3026 return queue_work_on(cpu, system_wq, work);
3027}
3028EXPORT_SYMBOL(schedule_work_on);
3029
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003030/**
3031 * schedule_work - put work task in global workqueue
3032 * @work: job to be done
3033 *
Tejun Heod4283e92012-08-03 10:30:44 -07003034 * Returns %false if @work was already on the kernel-global workqueue and
3035 * %true otherwise.
Bart Van Assche5b0f437d2009-07-30 19:00:53 +02003036 *
3037 * This puts a job in the kernel-global workqueue if it was not already
3038 * queued and leaves it in the same position on the kernel-global
3039 * workqueue otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003040 */
Tejun Heod4283e92012-08-03 10:30:44 -07003041bool schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042{
Tejun Heod320c032010-06-29 10:07:14 +02003043 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044}
Dave Jonesae90dd52006-06-30 01:40:45 -04003045EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003047/**
3048 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
3049 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +00003050 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003051 * @delay: number of jiffies to wait
3052 *
3053 * After waiting for a given time this puts a job in the kernel-global
3054 * workqueue on the specified CPU.
3055 */
Tejun Heod4283e92012-08-03 10:30:44 -07003056bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork,
3057 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058{
Tejun Heod320c032010-06-29 10:07:14 +02003059 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060}
Dave Jonesae90dd52006-06-30 01:40:45 -04003061EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
Andrew Mortonb6136772006-06-25 05:47:49 -07003063/**
Tejun Heo0a13c002012-08-03 10:30:44 -07003064 * schedule_delayed_work - put work task in global workqueue after delay
3065 * @dwork: job to be done
3066 * @delay: number of jiffies to wait or 0 for immediate execution
3067 *
3068 * After waiting for a given time this puts a job in the kernel-global
3069 * workqueue.
3070 */
Tejun Heod4283e92012-08-03 10:30:44 -07003071bool schedule_delayed_work(struct delayed_work *dwork, unsigned long delay)
Tejun Heo0a13c002012-08-03 10:30:44 -07003072{
3073 return queue_delayed_work(system_wq, dwork, delay);
3074}
3075EXPORT_SYMBOL(schedule_delayed_work);
3076
3077/**
Tejun Heo31ddd872010-10-19 11:14:49 +02003078 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07003079 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07003080 *
Tejun Heo31ddd872010-10-19 11:14:49 +02003081 * schedule_on_each_cpu() executes @func on each online CPU using the
3082 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07003083 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02003084 *
3085 * RETURNS:
3086 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07003087 */
David Howells65f27f32006-11-22 14:55:48 +00003088int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08003089{
3090 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02003091 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08003092
Andrew Mortonb6136772006-06-25 05:47:49 -07003093 works = alloc_percpu(struct work_struct);
3094 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08003095 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07003096
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003097 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08003098
Christoph Lameter15316ba2006-01-08 01:00:43 -08003099 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01003100 struct work_struct *work = per_cpu_ptr(works, cpu);
3101
3102 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003103 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02003104 }
Tejun Heo93981802009-11-17 14:06:20 -08003105
3106 for_each_online_cpu(cpu)
3107 flush_work(per_cpu_ptr(works, cpu));
3108
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003109 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07003110 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08003111 return 0;
3112}
3113
Alan Sterneef6a7d2010-02-12 17:39:21 +09003114/**
3115 * flush_scheduled_work - ensure that any scheduled work has run to completion.
3116 *
3117 * Forces execution of the kernel-global workqueue and blocks until its
3118 * completion.
3119 *
3120 * Think twice before calling this function! It's very easy to get into
3121 * trouble if you don't take great care. Either of the following situations
3122 * will lead to deadlock:
3123 *
3124 * One of the work items currently on the workqueue needs to acquire
3125 * a lock held by your code or its caller.
3126 *
3127 * Your code is running in the context of a work routine.
3128 *
3129 * They will be detected by lockdep when they occur, but the first might not
3130 * occur very often. It depends on what work items are on the workqueue and
3131 * what locks they need, which you have no control over.
3132 *
3133 * In most situations flushing the entire workqueue is overkill; you merely
3134 * need to know that a particular work item isn't queued and isn't running.
3135 * In such cases you should use cancel_delayed_work_sync() or
3136 * cancel_work_sync() instead.
3137 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138void flush_scheduled_work(void)
3139{
Tejun Heod320c032010-06-29 10:07:14 +02003140 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141}
Dave Jonesae90dd52006-06-30 01:40:45 -04003142EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143
3144/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06003145 * execute_in_process_context - reliably execute the routine with user context
3146 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06003147 * @ew: guaranteed storage for the execute work structure (must
3148 * be available when the work executes)
3149 *
3150 * Executes the function immediately if process context is available,
3151 * otherwise schedules the function for delayed execution.
3152 *
3153 * Returns: 0 - function was executed
3154 * 1 - function was scheduled for execution
3155 */
David Howells65f27f32006-11-22 14:55:48 +00003156int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06003157{
3158 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00003159 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003160 return 0;
3161 }
3162
David Howells65f27f32006-11-22 14:55:48 +00003163 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003164 schedule_work(&ew->work);
3165
3166 return 1;
3167}
3168EXPORT_SYMBOL_GPL(execute_in_process_context);
3169
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170int keventd_up(void)
3171{
Tejun Heod320c032010-06-29 10:07:14 +02003172 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173}
3174
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003175static int alloc_cwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176{
Oleg Nesterov3af244332007-05-09 02:34:09 -07003177 /*
Tejun Heo0f900042010-06-29 10:07:11 +02003178 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
3179 * Make sure that the alignment isn't lower than that of
3180 * unsigned long long.
Oleg Nesterov3af244332007-05-09 02:34:09 -07003181 */
Tejun Heo0f900042010-06-29 10:07:11 +02003182 const size_t size = sizeof(struct cpu_workqueue_struct);
3183 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
3184 __alignof__(unsigned long long));
Oleg Nesterov3af244332007-05-09 02:34:09 -07003185
Lai Jiangshane06ffa12012-03-09 18:03:20 +08003186 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02003187 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
Tejun Heo931ac772010-07-20 11:07:48 +02003188 else {
Tejun Heof3421792010-07-02 10:03:51 +02003189 void *ptr;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01003190
Tejun Heof3421792010-07-02 10:03:51 +02003191 /*
3192 * Allocate enough room to align cwq and put an extra
3193 * pointer at the end pointing back to the originally
3194 * allocated pointer which will be used for free.
3195 */
3196 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
3197 if (ptr) {
3198 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
3199 *(void **)(wq->cpu_wq.single + 1) = ptr;
3200 }
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003201 }
Tejun Heof3421792010-07-02 10:03:51 +02003202
Tejun Heo0415b00d12011-03-24 18:50:09 +01003203 /* just in case, make sure it's actually aligned */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003204 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
3205 return wq->cpu_wq.v ? 0 : -ENOMEM;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003206}
3207
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003208static void free_cwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07003209{
Lai Jiangshane06ffa12012-03-09 18:03:20 +08003210 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02003211 free_percpu(wq->cpu_wq.pcpu);
3212 else if (wq->cpu_wq.single) {
3213 /* the pointer to free is stored right after the cwq */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003214 kfree(*(void **)(wq->cpu_wq.single + 1));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07003215 }
3216}
3217
Tejun Heof3421792010-07-02 10:03:51 +02003218static int wq_clamp_max_active(int max_active, unsigned int flags,
3219 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003220{
Tejun Heof3421792010-07-02 10:03:51 +02003221 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3222
3223 if (max_active < 1 || max_active > lim)
Valentin Ilie044c7822012-08-19 00:52:42 +03003224 pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
3225 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003226
Tejun Heof3421792010-07-02 10:03:51 +02003227 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003228}
3229
Tejun Heob196be82012-01-10 15:11:35 -08003230struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02003231 unsigned int flags,
3232 int max_active,
3233 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08003234 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003235{
Tejun Heob196be82012-01-10 15:11:35 -08003236 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003237 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02003238 unsigned int cpu;
Tejun Heob196be82012-01-10 15:11:35 -08003239 size_t namelen;
3240
3241 /* determine namelen, allocate wq and format name */
3242 va_start(args, lock_name);
3243 va_copy(args1, args);
3244 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3245
3246 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3247 if (!wq)
3248 goto err;
3249
3250 vsnprintf(wq->name, namelen, fmt, args1);
3251 va_end(args);
3252 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003253
Tejun Heof3421792010-07-02 10:03:51 +02003254 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02003255 * Workqueues which may be used during memory reclaim should
3256 * have a rescuer to guarantee forward progress.
3257 */
3258 if (flags & WQ_MEM_RECLAIM)
3259 flags |= WQ_RESCUER;
3260
Tejun Heod320c032010-06-29 10:07:14 +02003261 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003262 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003263
Tejun Heob196be82012-01-10 15:11:35 -08003264 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003265 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003266 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003267 mutex_init(&wq->flush_mutex);
3268 atomic_set(&wq->nr_cwqs_to_flush, 0);
3269 INIT_LIST_HEAD(&wq->flusher_queue);
3270 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003271
Johannes Bergeb13ba82008-01-16 09:51:58 +01003272 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003273 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003274
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003275 if (alloc_cwqs(wq) < 0)
3276 goto err;
3277
Tejun Heof3421792010-07-02 10:03:51 +02003278 for_each_cwq_cpu(cpu, wq) {
Tejun Heo15376632010-06-29 10:07:11 +02003279 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003280 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo32704762012-07-13 22:16:45 -07003281 int pool_idx = (bool)(flags & WQ_HIGHPRI);
Tejun Heo15376632010-06-29 10:07:11 +02003282
Tejun Heo0f900042010-06-29 10:07:11 +02003283 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heo32704762012-07-13 22:16:45 -07003284 cwq->pool = &gcwq->pools[pool_idx];
Tejun Heoc34056a2010-06-29 10:07:11 +02003285 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02003286 cwq->flush_color = -1;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003287 cwq->max_active = max_active;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003288 INIT_LIST_HEAD(&cwq->delayed_works);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003289 }
3290
Tejun Heoe22bee72010-06-29 10:07:14 +02003291 if (flags & WQ_RESCUER) {
3292 struct worker *rescuer;
3293
Tejun Heof2e005a2010-07-20 15:59:09 +02003294 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
Tejun Heoe22bee72010-06-29 10:07:14 +02003295 goto err;
3296
3297 wq->rescuer = rescuer = alloc_worker();
3298 if (!rescuer)
3299 goto err;
3300
Tejun Heob196be82012-01-10 15:11:35 -08003301 rescuer->task = kthread_create(rescuer_thread, wq, "%s",
3302 wq->name);
Tejun Heoe22bee72010-06-29 10:07:14 +02003303 if (IS_ERR(rescuer->task))
3304 goto err;
3305
Tejun Heoe22bee72010-06-29 10:07:14 +02003306 rescuer->task->flags |= PF_THREAD_BOUND;
3307 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003308 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003309
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003310 /*
3311 * workqueue_lock protects global freeze state and workqueues
3312 * list. Grab it, set max_active accordingly and add the new
3313 * workqueue to workqueues list.
3314 */
Tejun Heo15376632010-06-29 10:07:11 +02003315 spin_lock(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003316
Tejun Heo58a69cb2011-02-16 09:25:31 +01003317 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heof3421792010-07-02 10:03:51 +02003318 for_each_cwq_cpu(cpu, wq)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003319 get_cwq(cpu, wq)->max_active = 0;
3320
Tejun Heo15376632010-06-29 10:07:11 +02003321 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003322
Tejun Heo15376632010-06-29 10:07:11 +02003323 spin_unlock(&workqueue_lock);
3324
Oleg Nesterov3af244332007-05-09 02:34:09 -07003325 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02003326err:
3327 if (wq) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003328 free_cwqs(wq);
Tejun Heof2e005a2010-07-20 15:59:09 +02003329 free_mayday_mask(wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02003330 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003331 kfree(wq);
3332 }
3333 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003334}
Tejun Heod320c032010-06-29 10:07:14 +02003335EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003336
3337/**
3338 * destroy_workqueue - safely terminate a workqueue
3339 * @wq: target workqueue
3340 *
3341 * Safely destroy a workqueue. All work currently pending will be done first.
3342 */
3343void destroy_workqueue(struct workqueue_struct *wq)
3344{
Tejun Heoc8e55f32010-06-29 10:07:12 +02003345 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003346
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003347 /* drain it before proceeding with destruction */
3348 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003349
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003350 /*
3351 * wq list is used to freeze wq, remove from list after
3352 * flushing is complete in case freeze races us.
3353 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003354 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07003355 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003356 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003357
Tejun Heoe22bee72010-06-29 10:07:14 +02003358 /* sanity check */
Tejun Heof3421792010-07-02 10:03:51 +02003359 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02003360 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3361 int i;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003362
Tejun Heo73f53c42010-06-29 10:07:11 +02003363 for (i = 0; i < WORK_NR_COLORS; i++)
3364 BUG_ON(cwq->nr_in_flight[i]);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003365 BUG_ON(cwq->nr_active);
3366 BUG_ON(!list_empty(&cwq->delayed_works));
Tejun Heo73f53c42010-06-29 10:07:11 +02003367 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003368
Tejun Heoe22bee72010-06-29 10:07:14 +02003369 if (wq->flags & WQ_RESCUER) {
3370 kthread_stop(wq->rescuer->task);
Tejun Heof2e005a2010-07-20 15:59:09 +02003371 free_mayday_mask(wq->mayday_mask);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003372 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003373 }
3374
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003375 free_cwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003376 kfree(wq);
3377}
3378EXPORT_SYMBOL_GPL(destroy_workqueue);
3379
Tejun Heodcd989c2010-06-29 10:07:14 +02003380/**
3381 * workqueue_set_max_active - adjust max_active of a workqueue
3382 * @wq: target workqueue
3383 * @max_active: new max_active value.
3384 *
3385 * Set max_active of @wq to @max_active.
3386 *
3387 * CONTEXT:
3388 * Don't call from IRQ context.
3389 */
3390void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3391{
3392 unsigned int cpu;
3393
Tejun Heof3421792010-07-02 10:03:51 +02003394 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003395
3396 spin_lock(&workqueue_lock);
3397
3398 wq->saved_max_active = max_active;
3399
Tejun Heof3421792010-07-02 10:03:51 +02003400 for_each_cwq_cpu(cpu, wq) {
Tejun Heodcd989c2010-06-29 10:07:14 +02003401 struct global_cwq *gcwq = get_gcwq(cpu);
3402
3403 spin_lock_irq(&gcwq->lock);
3404
Tejun Heo58a69cb2011-02-16 09:25:31 +01003405 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heodcd989c2010-06-29 10:07:14 +02003406 !(gcwq->flags & GCWQ_FREEZING))
3407 get_cwq(gcwq->cpu, wq)->max_active = max_active;
3408
3409 spin_unlock_irq(&gcwq->lock);
3410 }
3411
3412 spin_unlock(&workqueue_lock);
3413}
3414EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3415
3416/**
3417 * workqueue_congested - test whether a workqueue is congested
3418 * @cpu: CPU in question
3419 * @wq: target workqueue
3420 *
3421 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3422 * no synchronization around this function and the test result is
3423 * unreliable and only useful as advisory hints or for debugging.
3424 *
3425 * RETURNS:
3426 * %true if congested, %false otherwise.
3427 */
3428bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3429{
3430 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3431
3432 return !list_empty(&cwq->delayed_works);
3433}
3434EXPORT_SYMBOL_GPL(workqueue_congested);
3435
3436/**
3437 * work_cpu - return the last known associated cpu for @work
3438 * @work: the work of interest
3439 *
3440 * RETURNS:
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003441 * CPU number if @work was ever queued. WORK_CPU_NONE otherwise.
Tejun Heodcd989c2010-06-29 10:07:14 +02003442 */
3443unsigned int work_cpu(struct work_struct *work)
3444{
3445 struct global_cwq *gcwq = get_work_gcwq(work);
3446
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003447 return gcwq ? gcwq->cpu : WORK_CPU_NONE;
Tejun Heodcd989c2010-06-29 10:07:14 +02003448}
3449EXPORT_SYMBOL_GPL(work_cpu);
3450
3451/**
3452 * work_busy - test whether a work is currently pending or running
3453 * @work: the work to be tested
3454 *
3455 * Test whether @work is currently pending or running. There is no
3456 * synchronization around this function and the test result is
3457 * unreliable and only useful as advisory hints or for debugging.
3458 * Especially for reentrant wqs, the pending state might hide the
3459 * running state.
3460 *
3461 * RETURNS:
3462 * OR'd bitmask of WORK_BUSY_* bits.
3463 */
3464unsigned int work_busy(struct work_struct *work)
3465{
3466 struct global_cwq *gcwq = get_work_gcwq(work);
3467 unsigned long flags;
3468 unsigned int ret = 0;
3469
3470 if (!gcwq)
3471 return false;
3472
3473 spin_lock_irqsave(&gcwq->lock, flags);
3474
3475 if (work_pending(work))
3476 ret |= WORK_BUSY_PENDING;
3477 if (find_worker_executing_work(gcwq, work))
3478 ret |= WORK_BUSY_RUNNING;
3479
3480 spin_unlock_irqrestore(&gcwq->lock, flags);
3481
3482 return ret;
3483}
3484EXPORT_SYMBOL_GPL(work_busy);
3485
Tejun Heodb7bccf2010-06-29 10:07:12 +02003486/*
3487 * CPU hotplug.
3488 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003489 * There are two challenges in supporting CPU hotplug. Firstly, there
3490 * are a lot of assumptions on strong associations among work, cwq and
3491 * gcwq which make migrating pending and scheduled works very
3492 * difficult to implement without impacting hot paths. Secondly,
3493 * gcwqs serve mix of short, long and very long running works making
3494 * blocked draining impractical.
3495 *
Tejun Heo628c78e2012-07-17 12:39:27 -07003496 * This is solved by allowing a gcwq to be disassociated from the CPU
3497 * running as an unbound one and allowing it to be reattached later if the
3498 * cpu comes back online.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003499 */
3500
Tejun Heo60373152012-07-17 12:39:27 -07003501/* claim manager positions of all pools */
Tejun Heo8db25e72012-07-17 12:39:28 -07003502static void gcwq_claim_management_and_lock(struct global_cwq *gcwq)
Tejun Heo60373152012-07-17 12:39:27 -07003503{
3504 struct worker_pool *pool;
3505
3506 for_each_worker_pool(pool, gcwq)
3507 mutex_lock_nested(&pool->manager_mutex, pool - gcwq->pools);
Tejun Heo8db25e72012-07-17 12:39:28 -07003508 spin_lock_irq(&gcwq->lock);
Tejun Heo60373152012-07-17 12:39:27 -07003509}
3510
3511/* release manager positions */
Tejun Heo8db25e72012-07-17 12:39:28 -07003512static void gcwq_release_management_and_unlock(struct global_cwq *gcwq)
Tejun Heo60373152012-07-17 12:39:27 -07003513{
3514 struct worker_pool *pool;
3515
Tejun Heo8db25e72012-07-17 12:39:28 -07003516 spin_unlock_irq(&gcwq->lock);
Tejun Heo60373152012-07-17 12:39:27 -07003517 for_each_worker_pool(pool, gcwq)
3518 mutex_unlock(&pool->manager_mutex);
3519}
3520
Tejun Heo628c78e2012-07-17 12:39:27 -07003521static void gcwq_unbind_fn(struct work_struct *work)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003522{
Tejun Heo628c78e2012-07-17 12:39:27 -07003523 struct global_cwq *gcwq = get_gcwq(smp_processor_id());
Tejun Heo4ce62e92012-07-13 22:16:44 -07003524 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003525 struct worker *worker;
3526 struct hlist_node *pos;
3527 int i;
3528
3529 BUG_ON(gcwq->cpu != smp_processor_id());
3530
Tejun Heo8db25e72012-07-17 12:39:28 -07003531 gcwq_claim_management_and_lock(gcwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02003532
Tejun Heof2d5a0e2012-07-17 12:39:26 -07003533 /*
3534 * We've claimed all manager positions. Make all workers unbound
3535 * and set DISASSOCIATED. Before this, all workers except for the
3536 * ones which are still executing works from before the last CPU
3537 * down must be on the cpu. After this, they may become diasporas.
3538 */
Tejun Heo60373152012-07-17 12:39:27 -07003539 for_each_worker_pool(pool, gcwq)
Tejun Heo4ce62e92012-07-13 22:16:44 -07003540 list_for_each_entry(worker, &pool->idle_list, entry)
Tejun Heo403c8212012-07-17 12:39:27 -07003541 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003542
3543 for_each_busy_worker(worker, i, pos, gcwq)
Tejun Heo403c8212012-07-17 12:39:27 -07003544 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003545
Tejun Heof2d5a0e2012-07-17 12:39:26 -07003546 gcwq->flags |= GCWQ_DISASSOCIATED;
3547
Tejun Heo8db25e72012-07-17 12:39:28 -07003548 gcwq_release_management_and_unlock(gcwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02003549
3550 /*
Tejun Heo628c78e2012-07-17 12:39:27 -07003551 * Call schedule() so that we cross rq->lock and thus can guarantee
3552 * sched callbacks see the %WORKER_UNBOUND flag. This is necessary
3553 * as scheduler callbacks may be invoked from other cpus.
3554 */
3555 schedule();
3556
3557 /*
3558 * Sched callbacks are disabled now. Zap nr_running. After this,
3559 * nr_running stays zero and need_more_worker() and keep_working()
3560 * are always true as long as the worklist is not empty. @gcwq now
3561 * behaves as unbound (in terms of concurrency management) gcwq
3562 * which is served by workers tied to the CPU.
3563 *
3564 * On return from this function, the current worker would trigger
3565 * unbound chain execution of pending work items if other workers
3566 * didn't already.
Tejun Heoe22bee72010-06-29 10:07:14 +02003567 */
Tejun Heo4ce62e92012-07-13 22:16:44 -07003568 for_each_worker_pool(pool, gcwq)
3569 atomic_set(get_pool_nr_running(pool), 0);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003570}
3571
Tejun Heo8db25e72012-07-17 12:39:28 -07003572/*
3573 * Workqueues should be brought up before normal priority CPU notifiers.
3574 * This will be registered high priority CPU notifier.
3575 */
3576static int __devinit workqueue_cpu_up_callback(struct notifier_block *nfb,
3577 unsigned long action,
3578 void *hcpu)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003579{
3580 unsigned int cpu = (unsigned long)hcpu;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003581 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003582 struct worker_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583
Tejun Heo8db25e72012-07-17 12:39:28 -07003584 switch (action & ~CPU_TASKS_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 case CPU_UP_PREPARE:
Tejun Heo4ce62e92012-07-13 22:16:44 -07003586 for_each_worker_pool(pool, gcwq) {
Tejun Heo3ce63372012-07-17 12:39:27 -07003587 struct worker *worker;
3588
3589 if (pool->nr_workers)
3590 continue;
3591
3592 worker = create_worker(pool);
3593 if (!worker)
3594 return NOTIFY_BAD;
3595
3596 spin_lock_irq(&gcwq->lock);
3597 start_worker(worker);
3598 spin_unlock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003600 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003601
Tejun Heo65758202012-07-17 12:39:26 -07003602 case CPU_DOWN_FAILED:
3603 case CPU_ONLINE:
Tejun Heo8db25e72012-07-17 12:39:28 -07003604 gcwq_claim_management_and_lock(gcwq);
3605 gcwq->flags &= ~GCWQ_DISASSOCIATED;
3606 rebind_workers(gcwq);
3607 gcwq_release_management_and_unlock(gcwq);
3608 break;
Tejun Heo65758202012-07-17 12:39:26 -07003609 }
3610 return NOTIFY_OK;
3611}
3612
3613/*
3614 * Workqueues should be brought down after normal priority CPU notifiers.
3615 * This will be registered as low priority CPU notifier.
3616 */
3617static int __devinit workqueue_cpu_down_callback(struct notifier_block *nfb,
3618 unsigned long action,
3619 void *hcpu)
3620{
Tejun Heo8db25e72012-07-17 12:39:28 -07003621 unsigned int cpu = (unsigned long)hcpu;
3622 struct work_struct unbind_work;
3623
Tejun Heo65758202012-07-17 12:39:26 -07003624 switch (action & ~CPU_TASKS_FROZEN) {
3625 case CPU_DOWN_PREPARE:
Tejun Heo8db25e72012-07-17 12:39:28 -07003626 /* unbinding should happen on the local CPU */
3627 INIT_WORK_ONSTACK(&unbind_work, gcwq_unbind_fn);
Joonsoo Kim7635d2f2012-08-15 23:25:41 +09003628 queue_work_on(cpu, system_highpri_wq, &unbind_work);
Tejun Heo8db25e72012-07-17 12:39:28 -07003629 flush_work(&unbind_work);
3630 break;
Tejun Heo65758202012-07-17 12:39:26 -07003631 }
3632 return NOTIFY_OK;
3633}
3634
Rusty Russell2d3854a2008-11-05 13:39:10 +11003635#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003636
Rusty Russell2d3854a2008-11-05 13:39:10 +11003637struct work_for_cpu {
Andrew Morton6b440032009-04-09 09:50:37 -06003638 struct completion completion;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003639 long (*fn)(void *);
3640 void *arg;
3641 long ret;
3642};
3643
Andrew Morton6b440032009-04-09 09:50:37 -06003644static int do_work_for_cpu(void *_wfc)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003645{
Andrew Morton6b440032009-04-09 09:50:37 -06003646 struct work_for_cpu *wfc = _wfc;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003647 wfc->ret = wfc->fn(wfc->arg);
Andrew Morton6b440032009-04-09 09:50:37 -06003648 complete(&wfc->completion);
3649 return 0;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003650}
3651
3652/**
3653 * work_on_cpu - run a function in user context on a particular cpu
3654 * @cpu: the cpu to run on
3655 * @fn: the function to run
3656 * @arg: the function arg
3657 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003658 * This will return the value @fn returns.
3659 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06003660 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003661 */
3662long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3663{
Andrew Morton6b440032009-04-09 09:50:37 -06003664 struct task_struct *sub_thread;
3665 struct work_for_cpu wfc = {
3666 .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
3667 .fn = fn,
3668 .arg = arg,
3669 };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003670
Andrew Morton6b440032009-04-09 09:50:37 -06003671 sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
3672 if (IS_ERR(sub_thread))
3673 return PTR_ERR(sub_thread);
3674 kthread_bind(sub_thread, cpu);
3675 wake_up_process(sub_thread);
3676 wait_for_completion(&wfc.completion);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003677 return wfc.ret;
3678}
3679EXPORT_SYMBOL_GPL(work_on_cpu);
3680#endif /* CONFIG_SMP */
3681
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003682#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303683
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003684/**
3685 * freeze_workqueues_begin - begin freezing workqueues
3686 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003687 * Start freezing workqueues. After this function returns, all freezable
3688 * workqueues will queue new works to their frozen_works list instead of
3689 * gcwq->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003690 *
3691 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003692 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003693 */
3694void freeze_workqueues_begin(void)
3695{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003696 unsigned int cpu;
3697
3698 spin_lock(&workqueue_lock);
3699
3700 BUG_ON(workqueue_freezing);
3701 workqueue_freezing = true;
3702
Tejun Heof3421792010-07-02 10:03:51 +02003703 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003704 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003705 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003706
3707 spin_lock_irq(&gcwq->lock);
3708
Tejun Heodb7bccf2010-06-29 10:07:12 +02003709 BUG_ON(gcwq->flags & GCWQ_FREEZING);
3710 gcwq->flags |= GCWQ_FREEZING;
3711
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003712 list_for_each_entry(wq, &workqueues, list) {
3713 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3714
Tejun Heo58a69cb2011-02-16 09:25:31 +01003715 if (cwq && wq->flags & WQ_FREEZABLE)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003716 cwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003717 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003718
3719 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003720 }
3721
3722 spin_unlock(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003724
3725/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003726 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003727 *
3728 * Check whether freezing is complete. This function must be called
3729 * between freeze_workqueues_begin() and thaw_workqueues().
3730 *
3731 * CONTEXT:
3732 * Grabs and releases workqueue_lock.
3733 *
3734 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01003735 * %true if some freezable workqueues are still busy. %false if freezing
3736 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003737 */
3738bool freeze_workqueues_busy(void)
3739{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003740 unsigned int cpu;
3741 bool busy = false;
3742
3743 spin_lock(&workqueue_lock);
3744
3745 BUG_ON(!workqueue_freezing);
3746
Tejun Heof3421792010-07-02 10:03:51 +02003747 for_each_gcwq_cpu(cpu) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003748 struct workqueue_struct *wq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003749 /*
3750 * nr_active is monotonically decreasing. It's safe
3751 * to peek without lock.
3752 */
3753 list_for_each_entry(wq, &workqueues, list) {
3754 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3755
Tejun Heo58a69cb2011-02-16 09:25:31 +01003756 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003757 continue;
3758
3759 BUG_ON(cwq->nr_active < 0);
3760 if (cwq->nr_active) {
3761 busy = true;
3762 goto out_unlock;
3763 }
3764 }
3765 }
3766out_unlock:
3767 spin_unlock(&workqueue_lock);
3768 return busy;
3769}
3770
3771/**
3772 * thaw_workqueues - thaw workqueues
3773 *
3774 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo7e116292010-06-29 10:07:13 +02003775 * frozen works are transferred to their respective gcwq worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003776 *
3777 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003778 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003779 */
3780void thaw_workqueues(void)
3781{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003782 unsigned int cpu;
3783
3784 spin_lock(&workqueue_lock);
3785
3786 if (!workqueue_freezing)
3787 goto out_unlock;
3788
Tejun Heof3421792010-07-02 10:03:51 +02003789 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003790 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003791 struct worker_pool *pool;
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003792 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003793
3794 spin_lock_irq(&gcwq->lock);
3795
Tejun Heodb7bccf2010-06-29 10:07:12 +02003796 BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
3797 gcwq->flags &= ~GCWQ_FREEZING;
3798
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003799 list_for_each_entry(wq, &workqueues, list) {
3800 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3801
Tejun Heo58a69cb2011-02-16 09:25:31 +01003802 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003803 continue;
3804
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003805 /* restore max_active and repopulate worklist */
3806 cwq->max_active = wq->saved_max_active;
3807
3808 while (!list_empty(&cwq->delayed_works) &&
3809 cwq->nr_active < cwq->max_active)
3810 cwq_activate_first_delayed(cwq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003811 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003812
Tejun Heo4ce62e92012-07-13 22:16:44 -07003813 for_each_worker_pool(pool, gcwq)
3814 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02003815
Tejun Heo8b03ae32010-06-29 10:07:12 +02003816 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003817 }
3818
3819 workqueue_freezing = false;
3820out_unlock:
3821 spin_unlock(&workqueue_lock);
3822}
3823#endif /* CONFIG_FREEZER */
3824
Suresh Siddha6ee05782010-07-30 14:57:37 -07003825static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826{
Tejun Heoc34056a2010-06-29 10:07:11 +02003827 unsigned int cpu;
Tejun Heoc8e55f32010-06-29 10:07:12 +02003828 int i;
Tejun Heoc34056a2010-06-29 10:07:11 +02003829
Tejun Heob5490072012-08-03 10:30:46 -07003830 /* make sure we have enough bits for OFFQ CPU number */
3831 BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_CPU_SHIFT)) <
3832 WORK_CPU_LAST);
3833
Tejun Heo65758202012-07-17 12:39:26 -07003834 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
3835 cpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003836
3837 /* initialize gcwqs */
Tejun Heof3421792010-07-02 10:03:51 +02003838 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003839 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003840 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003841
3842 spin_lock_init(&gcwq->lock);
3843 gcwq->cpu = cpu;
Tejun Heo477a3c32010-08-31 10:54:35 +02003844 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003845
Tejun Heoc8e55f32010-06-29 10:07:12 +02003846 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
3847 INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
3848
Tejun Heo4ce62e92012-07-13 22:16:44 -07003849 for_each_worker_pool(pool, gcwq) {
3850 pool->gcwq = gcwq;
3851 INIT_LIST_HEAD(&pool->worklist);
3852 INIT_LIST_HEAD(&pool->idle_list);
Tejun Heoe22bee72010-06-29 10:07:14 +02003853
Tejun Heo4ce62e92012-07-13 22:16:44 -07003854 init_timer_deferrable(&pool->idle_timer);
3855 pool->idle_timer.function = idle_worker_timeout;
3856 pool->idle_timer.data = (unsigned long)pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003857
Tejun Heo4ce62e92012-07-13 22:16:44 -07003858 setup_timer(&pool->mayday_timer, gcwq_mayday_timeout,
3859 (unsigned long)pool);
3860
Tejun Heo60373152012-07-17 12:39:27 -07003861 mutex_init(&pool->manager_mutex);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003862 ida_init(&pool->worker_ida);
3863 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003864 }
3865
Tejun Heoe22bee72010-06-29 10:07:14 +02003866 /* create the initial worker */
Tejun Heof3421792010-07-02 10:03:51 +02003867 for_each_online_gcwq_cpu(cpu) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003868 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003869 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003870
Tejun Heo477a3c32010-08-31 10:54:35 +02003871 if (cpu != WORK_CPU_UNBOUND)
3872 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003873
3874 for_each_worker_pool(pool, gcwq) {
3875 struct worker *worker;
3876
Tejun Heobc2ae0f2012-07-17 12:39:27 -07003877 worker = create_worker(pool);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003878 BUG_ON(!worker);
3879 spin_lock_irq(&gcwq->lock);
3880 start_worker(worker);
3881 spin_unlock_irq(&gcwq->lock);
3882 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003883 }
3884
Tejun Heod320c032010-06-29 10:07:14 +02003885 system_wq = alloc_workqueue("events", 0, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09003886 system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
Tejun Heod320c032010-06-29 10:07:14 +02003887 system_long_wq = alloc_workqueue("events_long", 0, 0);
Tejun Heof3421792010-07-02 10:03:51 +02003888 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3889 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01003890 system_freezable_wq = alloc_workqueue("events_freezable",
3891 WQ_FREEZABLE, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09003892 BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
Tejun Heoae930e02012-08-20 14:51:23 -07003893 !system_unbound_wq || !system_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07003894 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895}
Suresh Siddha6ee05782010-07-30 14:57:37 -07003896early_initcall(init_workqueues);