blob: 329c404b68c27e22653a582af042afddfa2b0949 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tejun Heoc54fce62010-09-10 16:51:36 +02002 * kernel/workqueue.c - generic async execution with shared worker pool
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Tejun Heoc54fce62010-09-10 16:51:36 +02004 * Copyright (C) 2002 Ingo Molnar
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Tejun Heoc54fce62010-09-10 16:51:36 +02006 * Derived from the taskqueue/keventd code by:
7 * David Woodhouse <dwmw2@infradead.org>
8 * Andrew Morton
9 * Kai Petzke <wpp@marie.physik.tu-berlin.de>
10 * Theodore Ts'o <tytso@mit.edu>
Christoph Lameter89ada672005-10-30 15:01:59 -080011 *
Christoph Lametercde53532008-07-04 09:59:22 -070012 * Made to use alloc_percpu by Christoph Lameter.
Tejun Heoc54fce62010-09-10 16:51:36 +020013 *
14 * Copyright (C) 2010 SUSE Linux Products GmbH
15 * Copyright (C) 2010 Tejun Heo <tj@kernel.org>
16 *
17 * This is the generic async execution mechanism. Work items as are
18 * executed in process context. The worker pool is shared and
19 * automatically managed. There is one worker pool for each CPU and
20 * one extra for works which are better served by workers which are
21 * not bound to any specific CPU.
22 *
23 * Please read Documentation/workqueue.txt for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 */
25
Paul Gortmaker9984de12011-05-23 14:51:41 -040026#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/kernel.h>
28#include <linux/sched.h>
29#include <linux/init.h>
30#include <linux/signal.h>
31#include <linux/completion.h>
32#include <linux/workqueue.h>
33#include <linux/slab.h>
34#include <linux/cpu.h>
35#include <linux/notifier.h>
36#include <linux/kthread.h>
James Bottomley1fa44ec2006-02-23 12:43:43 -060037#include <linux/hardirq.h>
Christoph Lameter46934022006-10-11 01:21:26 -070038#include <linux/mempolicy.h>
Rafael J. Wysocki341a5952006-12-06 20:34:49 -080039#include <linux/freezer.h>
Peter Zijlstrad5abe662006-12-06 20:37:26 -080040#include <linux/kallsyms.h>
41#include <linux/debug_locks.h>
Johannes Berg4e6045f2007-10-18 23:39:55 -070042#include <linux/lockdep.h>
Tejun Heoc34056a2010-06-29 10:07:11 +020043#include <linux/idr.h>
Tejun Heoe22bee72010-06-29 10:07:14 +020044
45#include "workqueue_sched.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Tejun Heoc8e55f32010-06-29 10:07:12 +020047enum {
Tejun Heobc2ae0f2012-07-17 12:39:27 -070048 /*
49 * global_cwq flags
50 *
51 * A bound gcwq is either associated or disassociated with its CPU.
52 * While associated (!DISASSOCIATED), all workers are bound to the
53 * CPU and none has %WORKER_UNBOUND set and concurrency management
54 * is in effect.
55 *
56 * While DISASSOCIATED, the cpu may be offline and all workers have
57 * %WORKER_UNBOUND set and concurrency management disabled, and may
58 * be executing on any CPU. The gcwq behaves as an unbound one.
59 *
60 * Note that DISASSOCIATED can be flipped only while holding
61 * managership of all pools on the gcwq to avoid changing binding
62 * state while create_worker() is in progress.
63 */
Tejun Heo11ebea52012-07-12 14:46:37 -070064 GCWQ_DISASSOCIATED = 1 << 0, /* cpu can't serve workers */
65 GCWQ_FREEZING = 1 << 1, /* freeze in progress */
66
67 /* pool flags */
68 POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
Tejun Heodb7bccf2010-06-29 10:07:12 +020069
Tejun Heoc8e55f32010-06-29 10:07:12 +020070 /* worker flags */
71 WORKER_STARTED = 1 << 0, /* started */
72 WORKER_DIE = 1 << 1, /* die die die */
73 WORKER_IDLE = 1 << 2, /* is idle */
Tejun Heoe22bee72010-06-29 10:07:14 +020074 WORKER_PREP = 1 << 3, /* preparing to run works */
Tejun Heoe22bee72010-06-29 10:07:14 +020075 WORKER_REBIND = 1 << 5, /* mom is home, come back */
Tejun Heofb0e7be2010-06-29 10:07:15 +020076 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
Tejun Heof3421792010-07-02 10:03:51 +020077 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
Tejun Heoe22bee72010-06-29 10:07:14 +020078
Tejun Heo403c8212012-07-17 12:39:27 -070079 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_REBIND | WORKER_UNBOUND |
80 WORKER_CPU_INTENSIVE,
Tejun Heodb7bccf2010-06-29 10:07:12 +020081
Tejun Heo32704762012-07-13 22:16:45 -070082 NR_WORKER_POOLS = 2, /* # worker pools per gcwq */
Tejun Heo4ce62e92012-07-13 22:16:44 -070083
Tejun Heoc8e55f32010-06-29 10:07:12 +020084 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
85 BUSY_WORKER_HASH_SIZE = 1 << BUSY_WORKER_HASH_ORDER,
86 BUSY_WORKER_HASH_MASK = BUSY_WORKER_HASH_SIZE - 1,
Tejun Heodb7bccf2010-06-29 10:07:12 +020087
Tejun Heoe22bee72010-06-29 10:07:14 +020088 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
89 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
90
Tejun Heo3233cdb2011-02-16 18:10:19 +010091 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
92 /* call for help after 10ms
93 (min two ticks) */
Tejun Heoe22bee72010-06-29 10:07:14 +020094 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
95 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heoe22bee72010-06-29 10:07:14 +020096
97 /*
98 * Rescue workers are used only on emergencies and shared by
99 * all cpus. Give -20.
100 */
101 RESCUER_NICE_LEVEL = -20,
Tejun Heo32704762012-07-13 22:16:45 -0700102 HIGHPRI_NICE_LEVEL = -20,
Tejun Heoc8e55f32010-06-29 10:07:12 +0200103};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105/*
Tejun Heo4690c4a2010-06-29 10:07:10 +0200106 * Structure fields follow one of the following exclusion rules.
107 *
Tejun Heoe41e7042010-08-24 14:22:47 +0200108 * I: Modifiable by initialization/destruction paths and read-only for
109 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200110 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200111 * P: Preemption protected. Disabling preemption is enough and should
112 * only be modified and accessed from the local cpu.
113 *
Tejun Heo8b03ae32010-06-29 10:07:12 +0200114 * L: gcwq->lock protected. Access with gcwq->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200115 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200116 * X: During normal operation, modification requires gcwq->lock and
117 * should be done only from local cpu. Either disabling preemption
118 * on local cpu or grabbing gcwq->lock is enough for read access.
Tejun Heof3421792010-07-02 10:03:51 +0200119 * If GCWQ_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200120 *
Tejun Heo73f53c42010-06-29 10:07:11 +0200121 * F: wq->flush_mutex protected.
122 *
Tejun Heo4690c4a2010-06-29 10:07:10 +0200123 * W: workqueue_lock protected.
124 */
125
Tejun Heo8b03ae32010-06-29 10:07:12 +0200126struct global_cwq;
Tejun Heobd7bdd42012-07-12 14:46:37 -0700127struct worker_pool;
Tejun Heo25511a42012-07-17 12:39:27 -0700128struct idle_rebind;
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 */
152 struct idle_rebind *idle_rebind; /* L: for idle worker */
153 struct work_struct rebind_work; /* L: for busy worker */
Tejun Heoc34056a2010-06-29 10:07:11 +0200154};
155
Tejun Heobd7bdd42012-07-12 14:46:37 -0700156struct worker_pool {
157 struct global_cwq *gcwq; /* I: the owning gcwq */
Tejun Heo11ebea52012-07-12 14:46:37 -0700158 unsigned int flags; /* X: flags */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700159
160 struct list_head worklist; /* L: list of pending works */
161 int nr_workers; /* L: total number of workers */
162 int nr_idle; /* L: currently idle ones */
163
164 struct list_head idle_list; /* X: list of idle workers */
165 struct timer_list idle_timer; /* L: worker idle timeout */
166 struct timer_list mayday_timer; /* L: SOS timer for workers */
167
Tejun Heo60373152012-07-17 12:39:27 -0700168 struct mutex manager_mutex; /* mutex manager should hold */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700169 struct ida worker_ida; /* L: for worker IDs */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700170};
171
Tejun Heo4690c4a2010-06-29 10:07:10 +0200172/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200173 * Global per-cpu workqueue. There's one and only one for each cpu
174 * and all works are queued and processed here regardless of their
175 * target workqueues.
Tejun Heo8b03ae32010-06-29 10:07:12 +0200176 */
177struct global_cwq {
178 spinlock_t lock; /* the gcwq lock */
179 unsigned int cpu; /* I: the associated cpu */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200180 unsigned int flags; /* L: GCWQ_* flags */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200181
Tejun Heobd7bdd42012-07-12 14:46:37 -0700182 /* workers are chained either in busy_hash or pool idle_list */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200183 struct hlist_head busy_hash[BUSY_WORKER_HASH_SIZE];
184 /* L: hash of busy workers */
185
Joonsoo Kim330dad52012-08-15 23:25:36 +0900186 struct worker_pool pools[NR_WORKER_POOLS];
187 /* normal and highpri pools */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200188
Tejun Heo25511a42012-07-17 12:39:27 -0700189 wait_queue_head_t rebind_hold; /* rebind hold wait */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200190} ____cacheline_aligned_in_smp;
191
192/*
Tejun Heo502ca9d2010-06-29 10:07:13 +0200193 * The per-CPU workqueue. The lower WORK_STRUCT_FLAG_BITS of
Tejun Heo0f900042010-06-29 10:07:11 +0200194 * work_struct->data are used for flags and thus cwqs need to be
195 * aligned at two's power of the number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 */
197struct cpu_workqueue_struct {
Tejun Heobd7bdd42012-07-12 14:46:37 -0700198 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200199 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200200 int work_color; /* L: current color */
201 int flush_color; /* L: flushing color */
202 int nr_in_flight[WORK_NR_COLORS];
203 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200204 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200205 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200206 struct list_head delayed_works; /* L: delayed works */
Tejun Heo0f900042010-06-29 10:07:11 +0200207};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200210 * Structure used to wait for workqueue flush.
211 */
212struct wq_flusher {
213 struct list_head list; /* F: list of flushers */
214 int flush_color; /* F: flush color waiting for */
215 struct completion done; /* flush completion */
216};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Tejun Heo73f53c42010-06-29 10:07:11 +0200218/*
Tejun Heof2e005a2010-07-20 15:59:09 +0200219 * All cpumasks are assumed to be always set on UP and thus can't be
220 * used to determine whether there's something to be done.
221 */
222#ifdef CONFIG_SMP
223typedef cpumask_var_t mayday_mask_t;
224#define mayday_test_and_set_cpu(cpu, mask) \
225 cpumask_test_and_set_cpu((cpu), (mask))
226#define mayday_clear_cpu(cpu, mask) cpumask_clear_cpu((cpu), (mask))
227#define for_each_mayday_cpu(cpu, mask) for_each_cpu((cpu), (mask))
Tejun Heo9c375472010-08-31 11:18:34 +0200228#define alloc_mayday_mask(maskp, gfp) zalloc_cpumask_var((maskp), (gfp))
Tejun Heof2e005a2010-07-20 15:59:09 +0200229#define free_mayday_mask(mask) free_cpumask_var((mask))
230#else
231typedef unsigned long mayday_mask_t;
232#define mayday_test_and_set_cpu(cpu, mask) test_and_set_bit(0, &(mask))
233#define mayday_clear_cpu(cpu, mask) clear_bit(0, &(mask))
234#define for_each_mayday_cpu(cpu, mask) if ((cpu) = 0, (mask))
235#define alloc_mayday_mask(maskp, gfp) true
236#define free_mayday_mask(mask) do { } while (0)
237#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239/*
240 * The externally visible workqueue abstraction is an array of
241 * per-CPU workqueues:
242 */
243struct workqueue_struct {
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200244 unsigned int flags; /* W: WQ_* flags */
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200245 union {
246 struct cpu_workqueue_struct __percpu *pcpu;
247 struct cpu_workqueue_struct *single;
248 unsigned long v;
249 } cpu_wq; /* I: cwq's */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200250 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200251
252 struct mutex flush_mutex; /* protects wq flushing */
253 int work_color; /* F: current work color */
254 int flush_color; /* F: current flush color */
255 atomic_t nr_cwqs_to_flush; /* flush in progress */
256 struct wq_flusher *first_flusher; /* F: first flusher */
257 struct list_head flusher_queue; /* F: flush waiters */
258 struct list_head flusher_overflow; /* F: flush overflow list */
259
Tejun Heof2e005a2010-07-20 15:59:09 +0200260 mayday_mask_t mayday_mask; /* cpus requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200261 struct worker *rescuer; /* I: rescue worker */
262
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200263 int nr_drainers; /* W: drain in progress */
Tejun Heodcd989c2010-06-29 10:07:14 +0200264 int saved_max_active; /* W: saved cwq max_active */
Johannes Berg4e6045f2007-10-18 23:39:55 -0700265#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200266 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700267#endif
Tejun Heob196be82012-01-10 15:11:35 -0800268 char name[]; /* I: workqueue name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269};
270
Tejun Heod320c032010-06-29 10:07:14 +0200271struct workqueue_struct *system_wq __read_mostly;
Joonsoo Kim1aabe902012-08-15 23:25:39 +0900272struct workqueue_struct *system_highpri_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200273struct workqueue_struct *system_long_wq __read_mostly;
274struct workqueue_struct *system_nrt_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200275struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100276struct workqueue_struct *system_freezable_wq __read_mostly;
Alan Stern62d3c542012-03-02 10:51:00 +0100277struct workqueue_struct *system_nrt_freezable_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200278EXPORT_SYMBOL_GPL(system_wq);
Joonsoo Kim1aabe902012-08-15 23:25:39 +0900279EXPORT_SYMBOL_GPL(system_highpri_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200280EXPORT_SYMBOL_GPL(system_long_wq);
281EXPORT_SYMBOL_GPL(system_nrt_wq);
Tejun Heof3421792010-07-02 10:03:51 +0200282EXPORT_SYMBOL_GPL(system_unbound_wq);
Tejun Heo24d51ad2011-02-21 09:52:50 +0100283EXPORT_SYMBOL_GPL(system_freezable_wq);
Alan Stern62d3c542012-03-02 10:51:00 +0100284EXPORT_SYMBOL_GPL(system_nrt_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200285
Tejun Heo97bd2342010-10-05 10:41:14 +0200286#define CREATE_TRACE_POINTS
287#include <trace/events/workqueue.h>
288
Tejun Heo4ce62e92012-07-13 22:16:44 -0700289#define for_each_worker_pool(pool, gcwq) \
Tejun Heo32704762012-07-13 22:16:45 -0700290 for ((pool) = &(gcwq)->pools[0]; \
291 (pool) < &(gcwq)->pools[NR_WORKER_POOLS]; (pool)++)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700292
Tejun Heodb7bccf2010-06-29 10:07:12 +0200293#define for_each_busy_worker(worker, i, pos, gcwq) \
294 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \
295 hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry)
296
Tejun Heof3421792010-07-02 10:03:51 +0200297static inline int __next_gcwq_cpu(int cpu, const struct cpumask *mask,
298 unsigned int sw)
299{
300 if (cpu < nr_cpu_ids) {
301 if (sw & 1) {
302 cpu = cpumask_next(cpu, mask);
303 if (cpu < nr_cpu_ids)
304 return cpu;
305 }
306 if (sw & 2)
307 return WORK_CPU_UNBOUND;
308 }
309 return WORK_CPU_NONE;
310}
311
312static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
313 struct workqueue_struct *wq)
314{
315 return __next_gcwq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2);
316}
317
Tejun Heo09884952010-08-01 11:50:12 +0200318/*
319 * CPU iterators
320 *
321 * An extra gcwq is defined for an invalid cpu number
322 * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any
323 * specific CPU. The following iterators are similar to
324 * for_each_*_cpu() iterators but also considers the unbound gcwq.
325 *
326 * for_each_gcwq_cpu() : possible CPUs + WORK_CPU_UNBOUND
327 * for_each_online_gcwq_cpu() : online CPUs + WORK_CPU_UNBOUND
328 * for_each_cwq_cpu() : possible CPUs for bound workqueues,
329 * WORK_CPU_UNBOUND for unbound workqueues
330 */
Tejun Heof3421792010-07-02 10:03:51 +0200331#define for_each_gcwq_cpu(cpu) \
332 for ((cpu) = __next_gcwq_cpu(-1, cpu_possible_mask, 3); \
333 (cpu) < WORK_CPU_NONE; \
334 (cpu) = __next_gcwq_cpu((cpu), cpu_possible_mask, 3))
335
336#define for_each_online_gcwq_cpu(cpu) \
337 for ((cpu) = __next_gcwq_cpu(-1, cpu_online_mask, 3); \
338 (cpu) < WORK_CPU_NONE; \
339 (cpu) = __next_gcwq_cpu((cpu), cpu_online_mask, 3))
340
341#define for_each_cwq_cpu(cpu, wq) \
342 for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, (wq)); \
343 (cpu) < WORK_CPU_NONE; \
344 (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq)))
345
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900346#ifdef CONFIG_DEBUG_OBJECTS_WORK
347
348static struct debug_obj_descr work_debug_descr;
349
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100350static void *work_debug_hint(void *addr)
351{
352 return ((struct work_struct *) addr)->func;
353}
354
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900355/*
356 * fixup_init is called when:
357 * - an active object is initialized
358 */
359static int work_fixup_init(void *addr, enum debug_obj_state state)
360{
361 struct work_struct *work = addr;
362
363 switch (state) {
364 case ODEBUG_STATE_ACTIVE:
365 cancel_work_sync(work);
366 debug_object_init(work, &work_debug_descr);
367 return 1;
368 default:
369 return 0;
370 }
371}
372
373/*
374 * fixup_activate is called when:
375 * - an active object is activated
376 * - an unknown object is activated (might be a statically initialized object)
377 */
378static int work_fixup_activate(void *addr, enum debug_obj_state state)
379{
380 struct work_struct *work = addr;
381
382 switch (state) {
383
384 case ODEBUG_STATE_NOTAVAILABLE:
385 /*
386 * This is not really a fixup. The work struct was
387 * statically initialized. We just make sure that it
388 * is tracked in the object tracker.
389 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200390 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900391 debug_object_init(work, &work_debug_descr);
392 debug_object_activate(work, &work_debug_descr);
393 return 0;
394 }
395 WARN_ON_ONCE(1);
396 return 0;
397
398 case ODEBUG_STATE_ACTIVE:
399 WARN_ON(1);
400
401 default:
402 return 0;
403 }
404}
405
406/*
407 * fixup_free is called when:
408 * - an active object is freed
409 */
410static int work_fixup_free(void *addr, enum debug_obj_state state)
411{
412 struct work_struct *work = addr;
413
414 switch (state) {
415 case ODEBUG_STATE_ACTIVE:
416 cancel_work_sync(work);
417 debug_object_free(work, &work_debug_descr);
418 return 1;
419 default:
420 return 0;
421 }
422}
423
424static struct debug_obj_descr work_debug_descr = {
425 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100426 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900427 .fixup_init = work_fixup_init,
428 .fixup_activate = work_fixup_activate,
429 .fixup_free = work_fixup_free,
430};
431
432static inline void debug_work_activate(struct work_struct *work)
433{
434 debug_object_activate(work, &work_debug_descr);
435}
436
437static inline void debug_work_deactivate(struct work_struct *work)
438{
439 debug_object_deactivate(work, &work_debug_descr);
440}
441
442void __init_work(struct work_struct *work, int onstack)
443{
444 if (onstack)
445 debug_object_init_on_stack(work, &work_debug_descr);
446 else
447 debug_object_init(work, &work_debug_descr);
448}
449EXPORT_SYMBOL_GPL(__init_work);
450
451void destroy_work_on_stack(struct work_struct *work)
452{
453 debug_object_free(work, &work_debug_descr);
454}
455EXPORT_SYMBOL_GPL(destroy_work_on_stack);
456
457#else
458static inline void debug_work_activate(struct work_struct *work) { }
459static inline void debug_work_deactivate(struct work_struct *work) { }
460#endif
461
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100462/* Serializes the accesses to the list of workqueues. */
463static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464static LIST_HEAD(workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200465static bool workqueue_freezing; /* W: have wqs started freezing? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Oleg Nesterov14441962007-05-23 13:57:57 -0700467/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200468 * The almighty global cpu workqueues. nr_running is the only field
469 * which is expected to be used frequently by other cpus via
470 * try_to_wake_up(). Put it in a separate cacheline.
Oleg Nesterov14441962007-05-23 13:57:57 -0700471 */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200472static DEFINE_PER_CPU(struct global_cwq, global_cwq);
Tejun Heo4ce62e92012-07-13 22:16:44 -0700473static DEFINE_PER_CPU_SHARED_ALIGNED(atomic_t, pool_nr_running[NR_WORKER_POOLS]);
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800474
Tejun Heof3421792010-07-02 10:03:51 +0200475/*
476 * Global cpu workqueue and nr_running counter for unbound gcwq. The
477 * gcwq is always online, has GCWQ_DISASSOCIATED set, and all its
478 * workers have WORKER_UNBOUND set.
479 */
480static struct global_cwq unbound_global_cwq;
Tejun Heo4ce62e92012-07-13 22:16:44 -0700481static atomic_t unbound_pool_nr_running[NR_WORKER_POOLS] = {
482 [0 ... NR_WORKER_POOLS - 1] = ATOMIC_INIT(0), /* always 0 */
483};
Tejun Heof3421792010-07-02 10:03:51 +0200484
Tejun Heoc34056a2010-06-29 10:07:11 +0200485static int worker_thread(void *__worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Tejun Heo32704762012-07-13 22:16:45 -0700487static int worker_pool_pri(struct worker_pool *pool)
488{
489 return pool - pool->gcwq->pools;
490}
491
Tejun Heo8b03ae32010-06-29 10:07:12 +0200492static struct global_cwq *get_gcwq(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Tejun Heof3421792010-07-02 10:03:51 +0200494 if (cpu != WORK_CPU_UNBOUND)
495 return &per_cpu(global_cwq, cpu);
496 else
497 return &unbound_global_cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
Tejun Heo63d95a92012-07-12 14:46:37 -0700500static atomic_t *get_pool_nr_running(struct worker_pool *pool)
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700501{
Tejun Heo63d95a92012-07-12 14:46:37 -0700502 int cpu = pool->gcwq->cpu;
Tejun Heo32704762012-07-13 22:16:45 -0700503 int idx = worker_pool_pri(pool);
Tejun Heo63d95a92012-07-12 14:46:37 -0700504
Tejun Heof3421792010-07-02 10:03:51 +0200505 if (cpu != WORK_CPU_UNBOUND)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700506 return &per_cpu(pool_nr_running, cpu)[idx];
Tejun Heof3421792010-07-02 10:03:51 +0200507 else
Tejun Heo4ce62e92012-07-13 22:16:44 -0700508 return &unbound_pool_nr_running[idx];
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700509}
510
Tejun Heo4690c4a2010-06-29 10:07:10 +0200511static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
512 struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700513{
Tejun Heof3421792010-07-02 10:03:51 +0200514 if (!(wq->flags & WQ_UNBOUND)) {
Lai Jiangshane06ffa12012-03-09 18:03:20 +0800515 if (likely(cpu < nr_cpu_ids))
Tejun Heof3421792010-07-02 10:03:51 +0200516 return per_cpu_ptr(wq->cpu_wq.pcpu, cpu);
Tejun Heof3421792010-07-02 10:03:51 +0200517 } else if (likely(cpu == WORK_CPU_UNBOUND))
518 return wq->cpu_wq.single;
519 return NULL;
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700520}
521
Tejun Heo73f53c42010-06-29 10:07:11 +0200522static unsigned int work_color_to_flags(int color)
523{
524 return color << WORK_STRUCT_COLOR_SHIFT;
525}
526
527static int get_work_color(struct work_struct *work)
528{
529 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
530 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
531}
532
533static int work_next_color(int color)
534{
535 return (color + 1) % WORK_NR_COLORS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
David Howells4594bf12006-12-07 11:33:26 +0000538/*
Tejun Heob5490072012-08-03 10:30:46 -0700539 * While queued, %WORK_STRUCT_CWQ is set and non flag bits of a work's data
540 * contain the pointer to the queued cwq. Once execution starts, the flag
541 * is cleared and the high bits contain OFFQ flags and CPU number.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200542 *
Tejun Heobbb68df2012-08-03 10:30:46 -0700543 * set_work_cwq(), set_work_cpu_and_clear_pending(), mark_work_canceling()
544 * and clear_work_data() can be used to set the cwq, cpu or clear
545 * work->data. These functions should only be called while the work is
546 * owned - ie. while the PENDING bit is set.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200547 *
Tejun Heobbb68df2012-08-03 10:30:46 -0700548 * get_work_[g]cwq() can be used to obtain the gcwq or cwq corresponding to
549 * a work. gcwq is available once the work has been queued anywhere after
550 * initialization until it is sync canceled. cwq is available only while
551 * the work item is queued.
552 *
553 * %WORK_OFFQ_CANCELING is used to mark a work item which is being
554 * canceled. While being canceled, a work item may have its PENDING set
555 * but stay off timer and worklist for arbitrarily long and nobody should
556 * try to steal the PENDING bit.
David Howells4594bf12006-12-07 11:33:26 +0000557 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200558static inline void set_work_data(struct work_struct *work, unsigned long data,
559 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000560{
David Howells4594bf12006-12-07 11:33:26 +0000561 BUG_ON(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200562 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000563}
David Howells365970a2006-11-22 14:54:49 +0000564
Tejun Heo7a22ad72010-06-29 10:07:13 +0200565static void set_work_cwq(struct work_struct *work,
566 struct cpu_workqueue_struct *cwq,
567 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200568{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200569 set_work_data(work, (unsigned long)cwq,
Tejun Heoe1201532010-07-22 14:14:25 +0200570 WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200571}
572
Tejun Heo8930cab2012-08-03 10:30:45 -0700573static void set_work_cpu_and_clear_pending(struct work_struct *work,
574 unsigned int cpu)
David Howells365970a2006-11-22 14:54:49 +0000575{
Tejun Heo23657bb2012-08-13 17:08:19 -0700576 /*
577 * The following wmb is paired with the implied mb in
578 * test_and_set_bit(PENDING) and ensures all updates to @work made
579 * here are visible to and precede any updates by the next PENDING
580 * owner.
581 */
582 smp_wmb();
Tejun Heob5490072012-08-03 10:30:46 -0700583 set_work_data(work, (unsigned long)cpu << WORK_OFFQ_CPU_SHIFT, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200584}
585
586static void clear_work_data(struct work_struct *work)
587{
Tejun Heo23657bb2012-08-13 17:08:19 -0700588 smp_wmb(); /* see set_work_cpu_and_clear_pending() */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200589 set_work_data(work, WORK_STRUCT_NO_CPU, 0);
590}
591
Tejun Heo7a22ad72010-06-29 10:07:13 +0200592static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work)
593{
Tejun Heoe1201532010-07-22 14:14:25 +0200594 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200595
Tejun Heoe1201532010-07-22 14:14:25 +0200596 if (data & WORK_STRUCT_CWQ)
597 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
598 else
599 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200600}
601
602static struct global_cwq *get_work_gcwq(struct work_struct *work)
603{
Tejun Heoe1201532010-07-22 14:14:25 +0200604 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200605 unsigned int cpu;
606
Tejun Heoe1201532010-07-22 14:14:25 +0200607 if (data & WORK_STRUCT_CWQ)
608 return ((struct cpu_workqueue_struct *)
Tejun Heobd7bdd42012-07-12 14:46:37 -0700609 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->gcwq;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200610
Tejun Heob5490072012-08-03 10:30:46 -0700611 cpu = data >> WORK_OFFQ_CPU_SHIFT;
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200612 if (cpu == WORK_CPU_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200613 return NULL;
614
Tejun Heof3421792010-07-02 10:03:51 +0200615 BUG_ON(cpu >= nr_cpu_ids && cpu != WORK_CPU_UNBOUND);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200616 return get_gcwq(cpu);
David Howells365970a2006-11-22 14:54:49 +0000617}
618
Tejun Heobbb68df2012-08-03 10:30:46 -0700619static void mark_work_canceling(struct work_struct *work)
620{
621 struct global_cwq *gcwq = get_work_gcwq(work);
622 unsigned long cpu = gcwq ? gcwq->cpu : WORK_CPU_NONE;
623
624 set_work_data(work, (cpu << WORK_OFFQ_CPU_SHIFT) | WORK_OFFQ_CANCELING,
625 WORK_STRUCT_PENDING);
626}
627
628static bool work_is_canceling(struct work_struct *work)
629{
630 unsigned long data = atomic_long_read(&work->data);
631
632 return !(data & WORK_STRUCT_CWQ) && (data & WORK_OFFQ_CANCELING);
633}
634
David Howells365970a2006-11-22 14:54:49 +0000635/*
Tejun Heo32704762012-07-13 22:16:45 -0700636 * Policy functions. These define the policies on how the global worker
637 * pools are managed. Unless noted otherwise, these functions assume that
638 * they're being called with gcwq->lock held.
David Howells365970a2006-11-22 14:54:49 +0000639 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200640
Tejun Heo63d95a92012-07-12 14:46:37 -0700641static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000642{
Tejun Heo32704762012-07-13 22:16:45 -0700643 return !atomic_read(get_pool_nr_running(pool));
David Howells365970a2006-11-22 14:54:49 +0000644}
645
Tejun Heoe22bee72010-06-29 10:07:14 +0200646/*
647 * Need to wake up a worker? Called from anything but currently
648 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700649 *
650 * Note that, because unbound workers never contribute to nr_running, this
651 * function will always return %true for unbound gcwq as long as the
652 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200653 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700654static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000655{
Tejun Heo63d95a92012-07-12 14:46:37 -0700656 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000657}
658
Tejun Heoe22bee72010-06-29 10:07:14 +0200659/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700660static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200661{
Tejun Heo63d95a92012-07-12 14:46:37 -0700662 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200663}
664
665/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700666static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200667{
Tejun Heo63d95a92012-07-12 14:46:37 -0700668 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200669
Tejun Heo32704762012-07-13 22:16:45 -0700670 return !list_empty(&pool->worklist) && atomic_read(nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200671}
672
673/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700674static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200675{
Tejun Heo63d95a92012-07-12 14:46:37 -0700676 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200677}
678
679/* Do I need to be the manager? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700680static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200681{
Tejun Heo63d95a92012-07-12 14:46:37 -0700682 return need_to_create_worker(pool) ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700683 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200684}
685
686/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700687static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200688{
Tejun Heo60373152012-07-17 12:39:27 -0700689 bool managing = mutex_is_locked(&pool->manager_mutex);
Tejun Heo63d95a92012-07-12 14:46:37 -0700690 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
691 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200692
693 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
694}
695
696/*
697 * Wake up functions.
698 */
699
Tejun Heo7e116292010-06-29 10:07:13 +0200700/* Return the first worker. Safe with preemption disabled */
Tejun Heo63d95a92012-07-12 14:46:37 -0700701static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200702{
Tejun Heo63d95a92012-07-12 14:46:37 -0700703 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200704 return NULL;
705
Tejun Heo63d95a92012-07-12 14:46:37 -0700706 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200707}
708
709/**
710 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700711 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200712 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700713 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200714 *
715 * CONTEXT:
716 * spin_lock_irq(gcwq->lock).
717 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700718static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200719{
Tejun Heo63d95a92012-07-12 14:46:37 -0700720 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200721
722 if (likely(worker))
723 wake_up_process(worker->task);
724}
725
Tejun Heo4690c4a2010-06-29 10:07:10 +0200726/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200727 * wq_worker_waking_up - a worker is waking up
728 * @task: task waking up
729 * @cpu: CPU @task is waking up to
730 *
731 * This function is called during try_to_wake_up() when a worker is
732 * being awoken.
733 *
734 * CONTEXT:
735 * spin_lock_irq(rq->lock)
736 */
737void wq_worker_waking_up(struct task_struct *task, unsigned int cpu)
738{
739 struct worker *worker = kthread_data(task);
740
Steven Rostedt2d646722010-12-03 23:12:33 -0500741 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heo63d95a92012-07-12 14:46:37 -0700742 atomic_inc(get_pool_nr_running(worker->pool));
Tejun Heoe22bee72010-06-29 10:07:14 +0200743}
744
745/**
746 * wq_worker_sleeping - a worker is going to sleep
747 * @task: task going to sleep
748 * @cpu: CPU in question, must be the current CPU number
749 *
750 * This function is called during schedule() when a busy worker is
751 * going to sleep. Worker on the same cpu can be woken up by
752 * returning pointer to its task.
753 *
754 * CONTEXT:
755 * spin_lock_irq(rq->lock)
756 *
757 * RETURNS:
758 * Worker task on @cpu to wake up, %NULL if none.
759 */
760struct task_struct *wq_worker_sleeping(struct task_struct *task,
761 unsigned int cpu)
762{
763 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heobd7bdd42012-07-12 14:46:37 -0700764 struct worker_pool *pool = worker->pool;
Tejun Heo63d95a92012-07-12 14:46:37 -0700765 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200766
Steven Rostedt2d646722010-12-03 23:12:33 -0500767 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200768 return NULL;
769
770 /* this can only happen on the local cpu */
771 BUG_ON(cpu != raw_smp_processor_id());
772
773 /*
774 * The counterpart of the following dec_and_test, implied mb,
775 * worklist not empty test sequence is in insert_work().
776 * Please read comment there.
777 *
Tejun Heo628c78e2012-07-17 12:39:27 -0700778 * NOT_RUNNING is clear. This means that we're bound to and
779 * running on the local cpu w/ rq lock held and preemption
780 * disabled, which in turn means that none else could be
781 * manipulating idle_list, so dereferencing idle_list without gcwq
782 * lock is safe.
Tejun Heoe22bee72010-06-29 10:07:14 +0200783 */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700784 if (atomic_dec_and_test(nr_running) && !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700785 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200786 return to_wakeup ? to_wakeup->task : NULL;
787}
788
789/**
790 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200791 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200792 * @flags: flags to set
793 * @wakeup: wakeup an idle worker if necessary
794 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200795 * Set @flags in @worker->flags and adjust nr_running accordingly. If
796 * nr_running becomes zero and @wakeup is %true, an idle worker is
797 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200798 *
Tejun Heocb444762010-07-02 10:03:50 +0200799 * CONTEXT:
800 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200801 */
802static inline void worker_set_flags(struct worker *worker, unsigned int flags,
803 bool wakeup)
804{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700805 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200806
Tejun Heocb444762010-07-02 10:03:50 +0200807 WARN_ON_ONCE(worker->task != current);
808
Tejun Heoe22bee72010-06-29 10:07:14 +0200809 /*
810 * If transitioning into NOT_RUNNING, adjust nr_running and
811 * wake up an idle worker as necessary if requested by
812 * @wakeup.
813 */
814 if ((flags & WORKER_NOT_RUNNING) &&
815 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heo63d95a92012-07-12 14:46:37 -0700816 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200817
818 if (wakeup) {
819 if (atomic_dec_and_test(nr_running) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -0700820 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700821 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200822 } else
823 atomic_dec(nr_running);
824 }
825
Tejun Heod302f012010-06-29 10:07:13 +0200826 worker->flags |= flags;
827}
828
829/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200830 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200831 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200832 * @flags: flags to clear
833 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200834 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200835 *
Tejun Heocb444762010-07-02 10:03:50 +0200836 * CONTEXT:
837 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200838 */
839static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
840{
Tejun Heo63d95a92012-07-12 14:46:37 -0700841 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200842 unsigned int oflags = worker->flags;
843
Tejun Heocb444762010-07-02 10:03:50 +0200844 WARN_ON_ONCE(worker->task != current);
845
Tejun Heod302f012010-06-29 10:07:13 +0200846 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200847
Tejun Heo42c025f2011-01-11 15:58:49 +0100848 /*
849 * If transitioning out of NOT_RUNNING, increment nr_running. Note
850 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
851 * of multiple flags, not a single flag.
852 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200853 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
854 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heo63d95a92012-07-12 14:46:37 -0700855 atomic_inc(get_pool_nr_running(pool));
Tejun Heod302f012010-06-29 10:07:13 +0200856}
857
858/**
Tejun Heoc8e55f32010-06-29 10:07:12 +0200859 * busy_worker_head - return the busy hash head for a work
860 * @gcwq: gcwq of interest
861 * @work: work to be hashed
862 *
863 * Return hash head of @gcwq for @work.
864 *
865 * CONTEXT:
866 * spin_lock_irq(gcwq->lock).
867 *
868 * RETURNS:
869 * Pointer to the hash head.
870 */
871static struct hlist_head *busy_worker_head(struct global_cwq *gcwq,
872 struct work_struct *work)
873{
874 const int base_shift = ilog2(sizeof(struct work_struct));
875 unsigned long v = (unsigned long)work;
876
877 /* simple shift and fold hash, do we need something better? */
878 v >>= base_shift;
879 v += v >> BUSY_WORKER_HASH_ORDER;
880 v &= BUSY_WORKER_HASH_MASK;
881
882 return &gcwq->busy_hash[v];
883}
884
885/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200886 * __find_worker_executing_work - find worker which is executing a work
887 * @gcwq: gcwq of interest
888 * @bwh: hash head as returned by busy_worker_head()
889 * @work: work to find worker for
890 *
891 * Find a worker which is executing @work on @gcwq. @bwh should be
892 * the hash head obtained by calling busy_worker_head() with the same
893 * work.
894 *
895 * CONTEXT:
896 * spin_lock_irq(gcwq->lock).
897 *
898 * RETURNS:
899 * Pointer to worker which is executing @work if found, NULL
900 * otherwise.
901 */
902static struct worker *__find_worker_executing_work(struct global_cwq *gcwq,
903 struct hlist_head *bwh,
904 struct work_struct *work)
905{
906 struct worker *worker;
907 struct hlist_node *tmp;
908
909 hlist_for_each_entry(worker, tmp, bwh, hentry)
910 if (worker->current_work == work)
911 return worker;
912 return NULL;
913}
914
915/**
916 * find_worker_executing_work - find worker which is executing a work
917 * @gcwq: gcwq of interest
918 * @work: work to find worker for
919 *
920 * Find a worker which is executing @work on @gcwq. This function is
921 * identical to __find_worker_executing_work() except that this
922 * function calculates @bwh itself.
923 *
924 * CONTEXT:
925 * spin_lock_irq(gcwq->lock).
926 *
927 * RETURNS:
928 * Pointer to worker which is executing @work if found, NULL
929 * otherwise.
930 */
931static struct worker *find_worker_executing_work(struct global_cwq *gcwq,
932 struct work_struct *work)
933{
934 return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work),
935 work);
936}
937
938/**
Tejun Heobf4ede02012-08-03 10:30:46 -0700939 * move_linked_works - move linked works to a list
940 * @work: start of series of works to be scheduled
941 * @head: target list to append @work to
942 * @nextp: out paramter for nested worklist walking
943 *
944 * Schedule linked works starting from @work to @head. Work series to
945 * be scheduled starts at @work and includes any consecutive work with
946 * WORK_STRUCT_LINKED set in its predecessor.
947 *
948 * If @nextp is not NULL, it's updated to point to the next work of
949 * the last scheduled work. This allows move_linked_works() to be
950 * nested inside outer list_for_each_entry_safe().
951 *
952 * CONTEXT:
953 * spin_lock_irq(gcwq->lock).
954 */
955static void move_linked_works(struct work_struct *work, struct list_head *head,
956 struct work_struct **nextp)
957{
958 struct work_struct *n;
959
960 /*
961 * Linked worklist will always end before the end of the list,
962 * use NULL for list head.
963 */
964 list_for_each_entry_safe_from(work, n, NULL, entry) {
965 list_move_tail(&work->entry, head);
966 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
967 break;
968 }
969
970 /*
971 * If we're already inside safe list traversal and have moved
972 * multiple works to the scheduled queue, the next position
973 * needs to be updated.
974 */
975 if (nextp)
976 *nextp = n;
977}
978
979static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
980{
981 struct work_struct *work = list_first_entry(&cwq->delayed_works,
982 struct work_struct, entry);
983
984 trace_workqueue_activate_work(work);
985 move_linked_works(work, &cwq->pool->worklist, NULL);
986 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
987 cwq->nr_active++;
988}
989
990/**
991 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
992 * @cwq: cwq of interest
993 * @color: color of work which left the queue
994 * @delayed: for a delayed work
995 *
996 * A work either has completed or is removed from pending queue,
997 * decrement nr_in_flight of its cwq and handle workqueue flushing.
998 *
999 * CONTEXT:
1000 * spin_lock_irq(gcwq->lock).
1001 */
1002static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color,
1003 bool delayed)
1004{
1005 /* ignore uncolored works */
1006 if (color == WORK_NO_COLOR)
1007 return;
1008
1009 cwq->nr_in_flight[color]--;
1010
1011 if (!delayed) {
1012 cwq->nr_active--;
1013 if (!list_empty(&cwq->delayed_works)) {
1014 /* one down, submit a delayed one */
1015 if (cwq->nr_active < cwq->max_active)
1016 cwq_activate_first_delayed(cwq);
1017 }
1018 }
1019
1020 /* is flush in progress and are we at the flushing tip? */
1021 if (likely(cwq->flush_color != color))
1022 return;
1023
1024 /* are there still in-flight works? */
1025 if (cwq->nr_in_flight[color])
1026 return;
1027
1028 /* this cwq is done, clear flush_color */
1029 cwq->flush_color = -1;
1030
1031 /*
1032 * If this was the last cwq, wake up the first flusher. It
1033 * will handle the rest.
1034 */
1035 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
1036 complete(&cwq->wq->first_flusher->done);
1037}
1038
Tejun Heo36e227d2012-08-03 10:30:46 -07001039/**
Tejun Heobbb68df2012-08-03 10:30:46 -07001040 * try_to_grab_pending - steal work item from worklist and disable irq
Tejun Heo36e227d2012-08-03 10:30:46 -07001041 * @work: work item to steal
1042 * @is_dwork: @work is a delayed_work
Tejun Heobbb68df2012-08-03 10:30:46 -07001043 * @flags: place to store irq state
Tejun Heo36e227d2012-08-03 10:30:46 -07001044 *
1045 * Try to grab PENDING bit of @work. This function can handle @work in any
1046 * stable state - idle, on timer or on worklist. Return values are
1047 *
1048 * 1 if @work was pending and we successfully stole PENDING
1049 * 0 if @work was idle and we claimed PENDING
1050 * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry
Tejun Heobbb68df2012-08-03 10:30:46 -07001051 * -ENOENT if someone else is canceling @work, this state may persist
1052 * for arbitrarily long
Tejun Heo36e227d2012-08-03 10:30:46 -07001053 *
Tejun Heobbb68df2012-08-03 10:30:46 -07001054 * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting
1055 * preempted while holding PENDING and @work off queue, preemption must be
1056 * disabled on entry. This ensures that we don't return -EAGAIN while
1057 * another task is preempted in this function.
1058 *
1059 * On successful return, >= 0, irq is disabled and the caller is
1060 * responsible for releasing it using local_irq_restore(*@flags).
1061 *
1062 * This function is safe to call from any context other than IRQ handler.
1063 * An IRQ handler may run on top of delayed_work_timer_fn() which can make
1064 * this function return -EAGAIN perpetually.
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
1079 if (likely(del_timer(&dwork->timer)))
1080 return 1;
1081 }
1082
1083 /* try to claim PENDING the normal way */
Tejun Heobf4ede02012-08-03 10:30:46 -07001084 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1085 return 0;
1086
1087 /*
1088 * The queueing is in progress, or it is already queued. Try to
1089 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1090 */
1091 gcwq = get_work_gcwq(work);
1092 if (!gcwq)
Tejun Heobbb68df2012-08-03 10:30:46 -07001093 goto fail;
Tejun Heobf4ede02012-08-03 10:30:46 -07001094
Tejun Heobbb68df2012-08-03 10:30:46 -07001095 spin_lock(&gcwq->lock);
Tejun Heobf4ede02012-08-03 10:30:46 -07001096 if (!list_empty(&work->entry)) {
1097 /*
1098 * This work is queued, but perhaps we locked the wrong gcwq.
1099 * In that case we must see the new value after rmb(), see
1100 * insert_work()->wmb().
1101 */
1102 smp_rmb();
1103 if (gcwq == get_work_gcwq(work)) {
1104 debug_work_deactivate(work);
1105 list_del_init(&work->entry);
1106 cwq_dec_nr_in_flight(get_work_cwq(work),
1107 get_work_color(work),
1108 *work_data_bits(work) & WORK_STRUCT_DELAYED);
Tejun Heo36e227d2012-08-03 10:30:46 -07001109
Tejun Heobbb68df2012-08-03 10:30:46 -07001110 spin_unlock(&gcwq->lock);
Tejun Heo36e227d2012-08-03 10:30:46 -07001111 return 1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001112 }
1113 }
Tejun Heobbb68df2012-08-03 10:30:46 -07001114 spin_unlock(&gcwq->lock);
1115fail:
1116 local_irq_restore(*flags);
1117 if (work_is_canceling(work))
1118 return -ENOENT;
1119 cpu_relax();
Tejun Heo36e227d2012-08-03 10:30:46 -07001120 return -EAGAIN;
Tejun Heobf4ede02012-08-03 10:30:46 -07001121}
1122
1123/**
Tejun Heo7e116292010-06-29 10:07:13 +02001124 * insert_work - insert a work into gcwq
Tejun Heo4690c4a2010-06-29 10:07:10 +02001125 * @cwq: cwq @work belongs to
1126 * @work: work to insert
1127 * @head: insertion point
1128 * @extra_flags: extra WORK_STRUCT_* flags to set
1129 *
Tejun Heo7e116292010-06-29 10:07:13 +02001130 * Insert @work which belongs to @cwq into @gcwq after @head.
1131 * @extra_flags is or'd to work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +02001132 *
1133 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001134 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02001135 */
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001136static void insert_work(struct cpu_workqueue_struct *cwq,
Tejun Heo4690c4a2010-06-29 10:07:10 +02001137 struct work_struct *work, struct list_head *head,
1138 unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001139{
Tejun Heo63d95a92012-07-12 14:46:37 -07001140 struct worker_pool *pool = cwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01001141
Tejun Heo4690c4a2010-06-29 10:07:10 +02001142 /* we own @work, set data and link */
Tejun Heo7a22ad72010-06-29 10:07:13 +02001143 set_work_cwq(work, cwq, extra_flags);
Tejun Heo4690c4a2010-06-29 10:07:10 +02001144
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001145 /*
1146 * Ensure that we get the right work->data if we see the
1147 * result of list_add() below, see try_to_grab_pending().
1148 */
1149 smp_wmb();
Tejun Heo4690c4a2010-06-29 10:07:10 +02001150
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -07001151 list_add_tail(&work->entry, head);
Tejun Heoe22bee72010-06-29 10:07:14 +02001152
1153 /*
1154 * Ensure either worker_sched_deactivated() sees the above
1155 * list_add_tail() or we see zero nr_running to avoid workers
1156 * lying around lazily while there are works to be processed.
1157 */
1158 smp_mb();
1159
Tejun Heo63d95a92012-07-12 14:46:37 -07001160 if (__need_more_worker(pool))
1161 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001162}
1163
Tejun Heoc8efcc22010-12-20 19:32:04 +01001164/*
1165 * Test whether @work is being queued from another work executing on the
1166 * same workqueue. This is rather expensive and should only be used from
1167 * cold paths.
1168 */
1169static bool is_chained_work(struct workqueue_struct *wq)
1170{
1171 unsigned long flags;
1172 unsigned int cpu;
1173
1174 for_each_gcwq_cpu(cpu) {
1175 struct global_cwq *gcwq = get_gcwq(cpu);
1176 struct worker *worker;
1177 struct hlist_node *pos;
1178 int i;
1179
1180 spin_lock_irqsave(&gcwq->lock, flags);
1181 for_each_busy_worker(worker, i, pos, gcwq) {
1182 if (worker->task != current)
1183 continue;
1184 spin_unlock_irqrestore(&gcwq->lock, flags);
1185 /*
1186 * I'm @worker, no locking necessary. See if @work
1187 * is headed to the same workqueue.
1188 */
1189 return worker->current_cwq->wq == wq;
1190 }
1191 spin_unlock_irqrestore(&gcwq->lock, flags);
1192 }
1193 return false;
1194}
1195
Tejun Heo4690c4a2010-06-29 10:07:10 +02001196static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 struct work_struct *work)
1198{
Tejun Heo502ca9d2010-06-29 10:07:13 +02001199 struct global_cwq *gcwq;
1200 struct cpu_workqueue_struct *cwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001201 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001202 unsigned int work_flags;
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001203 unsigned int req_cpu = cpu;
Tejun Heo8930cab2012-08-03 10:30:45 -07001204
1205 /*
1206 * While a work item is PENDING && off queue, a task trying to
1207 * steal the PENDING will busy-loop waiting for it to either get
1208 * queued or lose PENDING. Grabbing PENDING and queueing should
1209 * happen with IRQ disabled.
1210 */
1211 WARN_ON_ONCE(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001213 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001214
Tejun Heoc8efcc22010-12-20 19:32:04 +01001215 /* if dying, only works from the same workqueue are allowed */
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02001216 if (unlikely(wq->flags & WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001217 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001218 return;
1219
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001220 /* determine gcwq to use */
1221 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001222 struct global_cwq *last_gcwq;
1223
Tejun Heo57469822012-08-03 10:30:45 -07001224 if (cpu == WORK_CPU_UNBOUND)
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001225 cpu = raw_smp_processor_id();
1226
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001227 /*
1228 * It's multi cpu. If @wq is non-reentrant and @work
1229 * was previously on a different cpu, it might still
1230 * be running there, in which case the work needs to
1231 * be queued on that cpu to guarantee non-reentrance.
1232 */
Tejun Heo502ca9d2010-06-29 10:07:13 +02001233 gcwq = get_gcwq(cpu);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001234 if (wq->flags & WQ_NON_REENTRANT &&
1235 (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) {
1236 struct worker *worker;
1237
Tejun Heo8930cab2012-08-03 10:30:45 -07001238 spin_lock(&last_gcwq->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001239
1240 worker = find_worker_executing_work(last_gcwq, work);
1241
1242 if (worker && worker->current_cwq->wq == wq)
1243 gcwq = last_gcwq;
1244 else {
1245 /* meh... not running there, queue here */
Tejun Heo8930cab2012-08-03 10:30:45 -07001246 spin_unlock(&last_gcwq->lock);
1247 spin_lock(&gcwq->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001248 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001249 } else {
1250 spin_lock(&gcwq->lock);
1251 }
Tejun Heof3421792010-07-02 10:03:51 +02001252 } else {
1253 gcwq = get_gcwq(WORK_CPU_UNBOUND);
Tejun Heo8930cab2012-08-03 10:30:45 -07001254 spin_lock(&gcwq->lock);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001255 }
1256
1257 /* gcwq determined, get cwq and queue */
1258 cwq = get_cwq(gcwq->cpu, wq);
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001259 trace_workqueue_queue_work(req_cpu, cwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001260
Dan Carpenterf5b25522012-04-13 22:06:58 +03001261 if (WARN_ON(!list_empty(&work->entry))) {
Tejun Heo8930cab2012-08-03 10:30:45 -07001262 spin_unlock(&gcwq->lock);
Dan Carpenterf5b25522012-04-13 22:06:58 +03001263 return;
1264 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001265
Tejun Heo73f53c42010-06-29 10:07:11 +02001266 cwq->nr_in_flight[cwq->work_color]++;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001267 work_flags = work_color_to_flags(cwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001268
1269 if (likely(cwq->nr_active < cwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001270 trace_workqueue_activate_work(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001271 cwq->nr_active++;
Tejun Heo32704762012-07-13 22:16:45 -07001272 worklist = &cwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001273 } else {
1274 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001275 worklist = &cwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001276 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001277
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001278 insert_work(cwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001279
Tejun Heo8930cab2012-08-03 10:30:45 -07001280 spin_unlock(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
1282
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001283/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07001284 * queue_work_on - queue work on specific cpu
1285 * @cpu: CPU number to execute work on
1286 * @wq: workqueue to use
1287 * @work: work to queue
1288 *
Tejun Heod4283e92012-08-03 10:30:44 -07001289 * Returns %false if @work was already on a queue, %true otherwise.
Zhang Ruic1a220e2008-07-23 21:28:39 -07001290 *
1291 * We queue the work to a specific CPU, the caller must ensure it
1292 * can't go away.
1293 */
Tejun Heod4283e92012-08-03 10:30:44 -07001294bool queue_work_on(int cpu, struct workqueue_struct *wq,
1295 struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07001296{
Tejun Heod4283e92012-08-03 10:30:44 -07001297 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001298 unsigned long flags;
1299
1300 local_irq_save(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001301
Tejun Heo22df02b2010-06-29 10:07:10 +02001302 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001303 __queue_work(cpu, wq, work);
Tejun Heod4283e92012-08-03 10:30:44 -07001304 ret = true;
Zhang Ruic1a220e2008-07-23 21:28:39 -07001305 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001306
1307 local_irq_restore(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001308 return ret;
1309}
1310EXPORT_SYMBOL_GPL(queue_work_on);
1311
Tejun Heo0a13c002012-08-03 10:30:44 -07001312/**
1313 * queue_work - queue work on a workqueue
1314 * @wq: workqueue to use
1315 * @work: work to queue
1316 *
Tejun Heod4283e92012-08-03 10:30:44 -07001317 * Returns %false if @work was already on a queue, %true otherwise.
Tejun Heo0a13c002012-08-03 10:30:44 -07001318 *
1319 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1320 * it can be processed by another CPU.
1321 */
Tejun Heod4283e92012-08-03 10:30:44 -07001322bool queue_work(struct workqueue_struct *wq, struct work_struct *work)
Tejun Heo0a13c002012-08-03 10:30:44 -07001323{
Tejun Heo57469822012-08-03 10:30:45 -07001324 return queue_work_on(WORK_CPU_UNBOUND, wq, work);
Tejun Heo0a13c002012-08-03 10:30:44 -07001325}
1326EXPORT_SYMBOL_GPL(queue_work);
1327
Tejun Heod8e794d2012-08-03 10:30:45 -07001328void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
David Howells52bad642006-11-22 14:54:01 +00001330 struct delayed_work *dwork = (struct delayed_work *)__data;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001331 struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Tejun Heo8930cab2012-08-03 10:30:45 -07001333 local_irq_disable();
Tejun Heo12650572012-08-08 09:38:42 -07001334 __queue_work(dwork->cpu, cwq->wq, &dwork->work);
Tejun Heo8930cab2012-08-03 10:30:45 -07001335 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336}
Tejun Heod8e794d2012-08-03 10:30:45 -07001337EXPORT_SYMBOL_GPL(delayed_work_timer_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001339static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
1340 struct delayed_work *dwork, unsigned long delay)
1341{
1342 struct timer_list *timer = &dwork->timer;
1343 struct work_struct *work = &dwork->work;
1344 unsigned int lcpu;
1345
1346 WARN_ON_ONCE(timer->function != delayed_work_timer_fn ||
1347 timer->data != (unsigned long)dwork);
1348 BUG_ON(timer_pending(timer));
1349 BUG_ON(!list_empty(&work->entry));
1350
1351 timer_stats_timer_set_start_info(&dwork->timer);
1352
1353 /*
1354 * This stores cwq for the moment, for the timer_fn. Note that the
1355 * work's gcwq is preserved to allow reentrance detection for
1356 * delayed works.
1357 */
1358 if (!(wq->flags & WQ_UNBOUND)) {
1359 struct global_cwq *gcwq = get_work_gcwq(work);
1360
Joonsoo Kime42986d2012-08-15 23:25:38 +09001361 /*
1362 * If we cannot get the last gcwq from @work directly,
1363 * select the last CPU such that it avoids unnecessarily
1364 * triggering non-reentrancy check in __queue_work().
1365 */
1366 lcpu = cpu;
1367 if (gcwq)
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001368 lcpu = gcwq->cpu;
Joonsoo Kime42986d2012-08-15 23:25:38 +09001369 if (lcpu == WORK_CPU_UNBOUND)
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001370 lcpu = raw_smp_processor_id();
1371 } else {
1372 lcpu = WORK_CPU_UNBOUND;
1373 }
1374
1375 set_work_cwq(work, get_cwq(lcpu, wq), 0);
1376
Tejun Heo12650572012-08-08 09:38:42 -07001377 dwork->cpu = cpu;
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001378 timer->expires = jiffies + delay;
1379
1380 if (unlikely(cpu != WORK_CPU_UNBOUND))
1381 add_timer_on(timer, cpu);
1382 else
1383 add_timer(timer);
1384}
1385
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001386/**
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001387 * queue_delayed_work_on - queue work on specific CPU after delay
1388 * @cpu: CPU number to execute work on
1389 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001390 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001391 * @delay: number of jiffies to wait before queueing
1392 *
Tejun Heo715f1302012-08-03 10:30:46 -07001393 * Returns %false if @work was already on a queue, %true otherwise. If
1394 * @delay is zero and @dwork is idle, it will be scheduled for immediate
1395 * execution.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001396 */
Tejun Heod4283e92012-08-03 10:30:44 -07001397bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
1398 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001399{
David Howells52bad642006-11-22 14:54:01 +00001400 struct work_struct *work = &dwork->work;
Tejun Heod4283e92012-08-03 10:30:44 -07001401 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001402 unsigned long flags;
1403
Tejun Heo715f1302012-08-03 10:30:46 -07001404 if (!delay)
1405 return queue_work_on(cpu, wq, &dwork->work);
1406
Tejun Heo8930cab2012-08-03 10:30:45 -07001407 /* read the comment in __queue_work() */
1408 local_irq_save(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001409
Tejun Heo22df02b2010-06-29 10:07:10 +02001410 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001411 __queue_delayed_work(cpu, wq, dwork, delay);
Tejun Heod4283e92012-08-03 10:30:44 -07001412 ret = true;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001413 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001414
1415 local_irq_restore(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001416 return ret;
1417}
Dave Jonesae90dd52006-06-30 01:40:45 -04001418EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Tejun Heoc8e55f32010-06-29 10:07:12 +02001420/**
Tejun Heo0a13c002012-08-03 10:30:44 -07001421 * queue_delayed_work - queue work on a workqueue after delay
1422 * @wq: workqueue to use
1423 * @dwork: delayable work to queue
1424 * @delay: number of jiffies to wait before queueing
1425 *
Tejun Heo715f1302012-08-03 10:30:46 -07001426 * Equivalent to queue_delayed_work_on() but tries to use the local CPU.
Tejun Heo0a13c002012-08-03 10:30:44 -07001427 */
Tejun Heod4283e92012-08-03 10:30:44 -07001428bool queue_delayed_work(struct workqueue_struct *wq,
Tejun Heo0a13c002012-08-03 10:30:44 -07001429 struct delayed_work *dwork, unsigned long delay)
1430{
Tejun Heo57469822012-08-03 10:30:45 -07001431 return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
Tejun Heo0a13c002012-08-03 10:30:44 -07001432}
1433EXPORT_SYMBOL_GPL(queue_delayed_work);
1434
1435/**
Tejun Heo8376fe22012-08-03 10:30:47 -07001436 * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
1437 * @cpu: CPU number to execute work on
1438 * @wq: workqueue to use
1439 * @dwork: work to queue
1440 * @delay: number of jiffies to wait before queueing
1441 *
1442 * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
1443 * modify @dwork's timer so that it expires after @delay. If @delay is
1444 * zero, @work is guaranteed to be scheduled immediately regardless of its
1445 * current state.
1446 *
1447 * Returns %false if @dwork was idle and queued, %true if @dwork was
1448 * pending and its timer was modified.
1449 *
1450 * This function is safe to call from any context other than IRQ handler.
1451 * See try_to_grab_pending() for details.
1452 */
1453bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
1454 struct delayed_work *dwork, unsigned long delay)
1455{
1456 unsigned long flags;
1457 int ret;
1458
1459 do {
1460 ret = try_to_grab_pending(&dwork->work, true, &flags);
1461 } while (unlikely(ret == -EAGAIN));
1462
1463 if (likely(ret >= 0)) {
1464 __queue_delayed_work(cpu, wq, dwork, delay);
1465 local_irq_restore(flags);
1466 }
1467
1468 /* -ENOENT from try_to_grab_pending() becomes %true */
1469 return ret;
1470}
1471EXPORT_SYMBOL_GPL(mod_delayed_work_on);
1472
1473/**
1474 * mod_delayed_work - modify delay of or queue a delayed work
1475 * @wq: workqueue to use
1476 * @dwork: work to queue
1477 * @delay: number of jiffies to wait before queueing
1478 *
1479 * mod_delayed_work_on() on local CPU.
1480 */
1481bool mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dwork,
1482 unsigned long delay)
1483{
1484 return mod_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
1485}
1486EXPORT_SYMBOL_GPL(mod_delayed_work);
1487
1488/**
Tejun Heoc8e55f32010-06-29 10:07:12 +02001489 * worker_enter_idle - enter idle state
1490 * @worker: worker which is entering idle state
1491 *
1492 * @worker is entering idle state. Update stats and idle timer if
1493 * necessary.
1494 *
1495 * LOCKING:
1496 * spin_lock_irq(gcwq->lock).
1497 */
1498static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001500 struct worker_pool *pool = worker->pool;
1501 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Tejun Heoc8e55f32010-06-29 10:07:12 +02001503 BUG_ON(worker->flags & WORKER_IDLE);
1504 BUG_ON(!list_empty(&worker->entry) &&
1505 (worker->hentry.next || worker->hentry.pprev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Tejun Heocb444762010-07-02 10:03:50 +02001507 /* can't use worker_set_flags(), also called from start_worker() */
1508 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001509 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001510 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001511
Tejun Heoc8e55f32010-06-29 10:07:12 +02001512 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001513 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001514
Tejun Heo628c78e2012-07-17 12:39:27 -07001515 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1516 mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
Tejun Heocb444762010-07-02 10:03:50 +02001517
Tejun Heo544ecf32012-05-14 15:04:50 -07001518 /*
Tejun Heo628c78e2012-07-17 12:39:27 -07001519 * Sanity check nr_running. Because gcwq_unbind_fn() releases
1520 * gcwq->lock between setting %WORKER_UNBOUND and zapping
1521 * nr_running, the warning may trigger spuriously. Check iff
1522 * unbind is not in progress.
Tejun Heo544ecf32012-05-14 15:04:50 -07001523 */
Tejun Heo628c78e2012-07-17 12:39:27 -07001524 WARN_ON_ONCE(!(gcwq->flags & GCWQ_DISASSOCIATED) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001525 pool->nr_workers == pool->nr_idle &&
Tejun Heo63d95a92012-07-12 14:46:37 -07001526 atomic_read(get_pool_nr_running(pool)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527}
1528
Tejun Heoc8e55f32010-06-29 10:07:12 +02001529/**
1530 * worker_leave_idle - leave idle state
1531 * @worker: worker which is leaving idle state
1532 *
1533 * @worker is leaving idle state. Update stats.
1534 *
1535 * LOCKING:
1536 * spin_lock_irq(gcwq->lock).
1537 */
1538static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001540 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Tejun Heoc8e55f32010-06-29 10:07:12 +02001542 BUG_ON(!(worker->flags & WORKER_IDLE));
Tejun Heod302f012010-06-29 10:07:13 +02001543 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001544 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001545 list_del_init(&worker->entry);
1546}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Tejun Heoe22bee72010-06-29 10:07:14 +02001548/**
1549 * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq
1550 * @worker: self
1551 *
1552 * Works which are scheduled while the cpu is online must at least be
1553 * scheduled to a worker which is bound to the cpu so that if they are
1554 * flushed from cpu callbacks while cpu is going down, they are
1555 * guaranteed to execute on the cpu.
1556 *
1557 * This function is to be used by rogue workers and rescuers to bind
1558 * themselves to the target cpu and may race with cpu going down or
1559 * coming online. kthread_bind() can't be used because it may put the
1560 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1561 * verbatim as it's best effort and blocking and gcwq may be
1562 * [dis]associated in the meantime.
1563 *
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001564 * This function tries set_cpus_allowed() and locks gcwq and verifies the
1565 * binding against %GCWQ_DISASSOCIATED which is set during
1566 * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1567 * enters idle state or fetches works without dropping lock, it can
1568 * guarantee the scheduling requirement described in the first paragraph.
Tejun Heoe22bee72010-06-29 10:07:14 +02001569 *
1570 * CONTEXT:
1571 * Might sleep. Called without any lock but returns with gcwq->lock
1572 * held.
1573 *
1574 * RETURNS:
1575 * %true if the associated gcwq is online (@worker is successfully
1576 * bound), %false if offline.
1577 */
1578static bool worker_maybe_bind_and_lock(struct worker *worker)
Namhyung Kim972fa1c2010-08-22 23:19:43 +09001579__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001580{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001581 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001582 struct task_struct *task = worker->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Tejun Heoe22bee72010-06-29 10:07:14 +02001584 while (true) {
1585 /*
1586 * The following call may fail, succeed or succeed
1587 * without actually migrating the task to the cpu if
1588 * it races with cpu hotunplug operation. Verify
1589 * against GCWQ_DISASSOCIATED.
1590 */
Tejun Heof3421792010-07-02 10:03:51 +02001591 if (!(gcwq->flags & GCWQ_DISASSOCIATED))
1592 set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu));
Oleg Nesterov85f41862007-05-09 02:34:20 -07001593
Tejun Heoe22bee72010-06-29 10:07:14 +02001594 spin_lock_irq(&gcwq->lock);
1595 if (gcwq->flags & GCWQ_DISASSOCIATED)
1596 return false;
1597 if (task_cpu(task) == gcwq->cpu &&
1598 cpumask_equal(&current->cpus_allowed,
1599 get_cpu_mask(gcwq->cpu)))
1600 return true;
1601 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001602
Tejun Heo5035b202011-04-29 18:08:37 +02001603 /*
1604 * We've raced with CPU hot[un]plug. Give it a breather
1605 * and retry migration. cond_resched() is required here;
1606 * otherwise, we might deadlock against cpu_stop trying to
1607 * bring down the CPU on non-preemptive kernel.
1608 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001609 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001610 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001611 }
1612}
1613
Tejun Heo25511a42012-07-17 12:39:27 -07001614struct idle_rebind {
1615 int cnt; /* # workers to be rebound */
1616 struct completion done; /* all workers rebound */
1617};
1618
Tejun Heoe22bee72010-06-29 10:07:14 +02001619/*
Tejun Heo25511a42012-07-17 12:39:27 -07001620 * Rebind an idle @worker to its CPU. During CPU onlining, this has to
1621 * happen synchronously for idle workers. worker_thread() will test
1622 * %WORKER_REBIND before leaving idle and call this function.
1623 */
1624static void idle_worker_rebind(struct worker *worker)
1625{
1626 struct global_cwq *gcwq = worker->pool->gcwq;
1627
1628 /* CPU must be online at this point */
1629 WARN_ON(!worker_maybe_bind_and_lock(worker));
1630 if (!--worker->idle_rebind->cnt)
1631 complete(&worker->idle_rebind->done);
1632 spin_unlock_irq(&worker->pool->gcwq->lock);
1633
1634 /* we did our part, wait for rebind_workers() to finish up */
1635 wait_event(gcwq->rebind_hold, !(worker->flags & WORKER_REBIND));
1636}
1637
1638/*
1639 * Function for @worker->rebind.work used to rebind unbound busy workers to
Tejun Heo403c8212012-07-17 12:39:27 -07001640 * the associated cpu which is coming back online. This is scheduled by
1641 * cpu up but can race with other cpu hotplug operations and may be
1642 * executed twice without intervening cpu down.
Tejun Heoe22bee72010-06-29 10:07:14 +02001643 */
Tejun Heo25511a42012-07-17 12:39:27 -07001644static void busy_worker_rebind_fn(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001645{
1646 struct worker *worker = container_of(work, struct worker, rebind_work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001647 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001648
1649 if (worker_maybe_bind_and_lock(worker))
1650 worker_clr_flags(worker, WORKER_REBIND);
1651
1652 spin_unlock_irq(&gcwq->lock);
1653}
1654
Tejun Heo25511a42012-07-17 12:39:27 -07001655/**
1656 * rebind_workers - rebind all workers of a gcwq to the associated CPU
1657 * @gcwq: gcwq of interest
1658 *
1659 * @gcwq->cpu is coming online. Rebind all workers to the CPU. Rebinding
1660 * is different for idle and busy ones.
1661 *
1662 * The idle ones should be rebound synchronously and idle rebinding should
1663 * be complete before any worker starts executing work items with
1664 * concurrency management enabled; otherwise, scheduler may oops trying to
1665 * wake up non-local idle worker from wq_worker_sleeping().
1666 *
1667 * This is achieved by repeatedly requesting rebinding until all idle
1668 * workers are known to have been rebound under @gcwq->lock and holding all
1669 * idle workers from becoming busy until idle rebinding is complete.
1670 *
1671 * Once idle workers are rebound, busy workers can be rebound as they
1672 * finish executing their current work items. Queueing the rebind work at
1673 * the head of their scheduled lists is enough. Note that nr_running will
1674 * be properbly bumped as busy workers rebind.
1675 *
1676 * On return, all workers are guaranteed to either be bound or have rebind
1677 * work item scheduled.
1678 */
1679static void rebind_workers(struct global_cwq *gcwq)
1680 __releases(&gcwq->lock) __acquires(&gcwq->lock)
1681{
1682 struct idle_rebind idle_rebind;
1683 struct worker_pool *pool;
1684 struct worker *worker;
1685 struct hlist_node *pos;
1686 int i;
1687
1688 lockdep_assert_held(&gcwq->lock);
1689
1690 for_each_worker_pool(pool, gcwq)
1691 lockdep_assert_held(&pool->manager_mutex);
1692
1693 /*
1694 * Rebind idle workers. Interlocked both ways. We wait for
1695 * workers to rebind via @idle_rebind.done. Workers will wait for
1696 * us to finish up by watching %WORKER_REBIND.
1697 */
1698 init_completion(&idle_rebind.done);
1699retry:
1700 idle_rebind.cnt = 1;
1701 INIT_COMPLETION(idle_rebind.done);
1702
1703 /* set REBIND and kick idle ones, we'll wait for these later */
1704 for_each_worker_pool(pool, gcwq) {
1705 list_for_each_entry(worker, &pool->idle_list, entry) {
1706 if (worker->flags & WORKER_REBIND)
1707 continue;
1708
1709 /* morph UNBOUND to REBIND */
1710 worker->flags &= ~WORKER_UNBOUND;
1711 worker->flags |= WORKER_REBIND;
1712
1713 idle_rebind.cnt++;
1714 worker->idle_rebind = &idle_rebind;
1715
1716 /* worker_thread() will call idle_worker_rebind() */
1717 wake_up_process(worker->task);
1718 }
1719 }
1720
1721 if (--idle_rebind.cnt) {
1722 spin_unlock_irq(&gcwq->lock);
1723 wait_for_completion(&idle_rebind.done);
1724 spin_lock_irq(&gcwq->lock);
1725 /* busy ones might have become idle while waiting, retry */
1726 goto retry;
1727 }
1728
1729 /*
1730 * All idle workers are rebound and waiting for %WORKER_REBIND to
1731 * be cleared inside idle_worker_rebind(). Clear and release.
1732 * Clearing %WORKER_REBIND from this foreign context is safe
1733 * because these workers are still guaranteed to be idle.
1734 */
1735 for_each_worker_pool(pool, gcwq)
1736 list_for_each_entry(worker, &pool->idle_list, entry)
1737 worker->flags &= ~WORKER_REBIND;
1738
1739 wake_up_all(&gcwq->rebind_hold);
1740
1741 /* rebind busy workers */
1742 for_each_busy_worker(worker, i, pos, gcwq) {
1743 struct work_struct *rebind_work = &worker->rebind_work;
1744
1745 /* morph UNBOUND to REBIND */
1746 worker->flags &= ~WORKER_UNBOUND;
1747 worker->flags |= WORKER_REBIND;
1748
1749 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
1750 work_data_bits(rebind_work)))
1751 continue;
1752
1753 /* wq doesn't matter, use the default one */
1754 debug_work_activate(rebind_work);
1755 insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work,
1756 worker->scheduled.next,
1757 work_color_to_flags(WORK_NO_COLOR));
1758 }
1759}
1760
Tejun Heoc34056a2010-06-29 10:07:11 +02001761static struct worker *alloc_worker(void)
1762{
1763 struct worker *worker;
1764
1765 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001766 if (worker) {
1767 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001768 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heo25511a42012-07-17 12:39:27 -07001769 INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn);
Tejun Heoe22bee72010-06-29 10:07:14 +02001770 /* on creation a worker is in !idle && prep state */
1771 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001772 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001773 return worker;
1774}
1775
1776/**
1777 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001778 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001779 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001780 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001781 * can be started by calling start_worker() or destroyed using
1782 * destroy_worker().
1783 *
1784 * CONTEXT:
1785 * Might sleep. Does GFP_KERNEL allocations.
1786 *
1787 * RETURNS:
1788 * Pointer to the newly created worker.
1789 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001790static struct worker *create_worker(struct worker_pool *pool)
Tejun Heoc34056a2010-06-29 10:07:11 +02001791{
Tejun Heo63d95a92012-07-12 14:46:37 -07001792 struct global_cwq *gcwq = pool->gcwq;
Tejun Heo32704762012-07-13 22:16:45 -07001793 const char *pri = worker_pool_pri(pool) ? "H" : "";
Tejun Heoc34056a2010-06-29 10:07:11 +02001794 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001795 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001796
Tejun Heo8b03ae32010-06-29 10:07:12 +02001797 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001798 while (ida_get_new(&pool->worker_ida, &id)) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001799 spin_unlock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001800 if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001801 goto fail;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001802 spin_lock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001803 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02001804 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001805
1806 worker = alloc_worker();
1807 if (!worker)
1808 goto fail;
1809
Tejun Heobd7bdd42012-07-12 14:46:37 -07001810 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001811 worker->id = id;
1812
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001813 if (gcwq->cpu != WORK_CPU_UNBOUND)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001814 worker->task = kthread_create_on_node(worker_thread,
Tejun Heo32704762012-07-13 22:16:45 -07001815 worker, cpu_to_node(gcwq->cpu),
1816 "kworker/%u:%d%s", gcwq->cpu, id, pri);
Tejun Heof3421792010-07-02 10:03:51 +02001817 else
1818 worker->task = kthread_create(worker_thread, worker,
Tejun Heo32704762012-07-13 22:16:45 -07001819 "kworker/u:%d%s", id, pri);
Tejun Heoc34056a2010-06-29 10:07:11 +02001820 if (IS_ERR(worker->task))
1821 goto fail;
1822
Tejun Heo32704762012-07-13 22:16:45 -07001823 if (worker_pool_pri(pool))
1824 set_user_nice(worker->task, HIGHPRI_NICE_LEVEL);
1825
Tejun Heodb7bccf2010-06-29 10:07:12 +02001826 /*
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001827 * Determine CPU binding of the new worker depending on
1828 * %GCWQ_DISASSOCIATED. The caller is responsible for ensuring the
1829 * flag remains stable across this function. See the comments
1830 * above the flag definition for details.
1831 *
1832 * As an unbound worker may later become a regular one if CPU comes
1833 * online, make sure every worker has %PF_THREAD_BOUND set.
Tejun Heodb7bccf2010-06-29 10:07:12 +02001834 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001835 if (!(gcwq->flags & GCWQ_DISASSOCIATED)) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001836 kthread_bind(worker->task, gcwq->cpu);
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001837 } else {
Tejun Heodb7bccf2010-06-29 10:07:12 +02001838 worker->task->flags |= PF_THREAD_BOUND;
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001839 worker->flags |= WORKER_UNBOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001841
Tejun Heoc34056a2010-06-29 10:07:11 +02001842 return worker;
1843fail:
1844 if (id >= 0) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001845 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001846 ida_remove(&pool->worker_ida, id);
Tejun Heo8b03ae32010-06-29 10:07:12 +02001847 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001848 }
1849 kfree(worker);
1850 return NULL;
1851}
1852
1853/**
1854 * start_worker - start a newly created worker
1855 * @worker: worker to start
1856 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001857 * Make the gcwq aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001858 *
1859 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001860 * spin_lock_irq(gcwq->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001861 */
1862static void start_worker(struct worker *worker)
1863{
Tejun Heocb444762010-07-02 10:03:50 +02001864 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001865 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001866 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001867 wake_up_process(worker->task);
1868}
1869
1870/**
1871 * destroy_worker - destroy a workqueue worker
1872 * @worker: worker to be destroyed
1873 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001874 * Destroy @worker and adjust @gcwq stats accordingly.
1875 *
1876 * CONTEXT:
1877 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001878 */
1879static void destroy_worker(struct worker *worker)
1880{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001881 struct worker_pool *pool = worker->pool;
1882 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001883 int id = worker->id;
1884
1885 /* sanity check frenzy */
1886 BUG_ON(worker->current_work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001887 BUG_ON(!list_empty(&worker->scheduled));
Tejun Heoc34056a2010-06-29 10:07:11 +02001888
Tejun Heoc8e55f32010-06-29 10:07:12 +02001889 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001890 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001891 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001892 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001893
1894 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001895 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001896
1897 spin_unlock_irq(&gcwq->lock);
1898
Tejun Heoc34056a2010-06-29 10:07:11 +02001899 kthread_stop(worker->task);
1900 kfree(worker);
1901
Tejun Heo8b03ae32010-06-29 10:07:12 +02001902 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001903 ida_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001904}
1905
Tejun Heo63d95a92012-07-12 14:46:37 -07001906static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001907{
Tejun Heo63d95a92012-07-12 14:46:37 -07001908 struct worker_pool *pool = (void *)__pool;
1909 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001910
1911 spin_lock_irq(&gcwq->lock);
1912
Tejun Heo63d95a92012-07-12 14:46:37 -07001913 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001914 struct worker *worker;
1915 unsigned long expires;
1916
1917 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001918 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001919 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1920
1921 if (time_before(jiffies, expires))
Tejun Heo63d95a92012-07-12 14:46:37 -07001922 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001923 else {
1924 /* it's been idle for too long, wake up manager */
Tejun Heo11ebea52012-07-12 14:46:37 -07001925 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -07001926 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001927 }
1928 }
1929
1930 spin_unlock_irq(&gcwq->lock);
1931}
1932
1933static bool send_mayday(struct work_struct *work)
1934{
1935 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1936 struct workqueue_struct *wq = cwq->wq;
Tejun Heof3421792010-07-02 10:03:51 +02001937 unsigned int cpu;
Tejun Heoe22bee72010-06-29 10:07:14 +02001938
1939 if (!(wq->flags & WQ_RESCUER))
1940 return false;
1941
1942 /* mayday mayday mayday */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001943 cpu = cwq->pool->gcwq->cpu;
Tejun Heof3421792010-07-02 10:03:51 +02001944 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1945 if (cpu == WORK_CPU_UNBOUND)
1946 cpu = 0;
Tejun Heof2e005a2010-07-20 15:59:09 +02001947 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001948 wake_up_process(wq->rescuer->task);
1949 return true;
1950}
1951
Tejun Heo63d95a92012-07-12 14:46:37 -07001952static void gcwq_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001953{
Tejun Heo63d95a92012-07-12 14:46:37 -07001954 struct worker_pool *pool = (void *)__pool;
1955 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001956 struct work_struct *work;
1957
1958 spin_lock_irq(&gcwq->lock);
1959
Tejun Heo63d95a92012-07-12 14:46:37 -07001960 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001961 /*
1962 * We've been trying to create a new worker but
1963 * haven't been successful. We might be hitting an
1964 * allocation deadlock. Send distress signals to
1965 * rescuers.
1966 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001967 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001968 send_mayday(work);
1969 }
1970
1971 spin_unlock_irq(&gcwq->lock);
1972
Tejun Heo63d95a92012-07-12 14:46:37 -07001973 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001974}
1975
1976/**
1977 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001978 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001979 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001980 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001981 * have at least one idle worker on return from this function. If
1982 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001983 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001984 * possible allocation deadlock.
1985 *
1986 * On return, need_to_create_worker() is guaranteed to be false and
1987 * may_start_working() true.
1988 *
1989 * LOCKING:
1990 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1991 * multiple times. Does GFP_KERNEL allocations. Called only from
1992 * manager.
1993 *
1994 * RETURNS:
1995 * false if no action was taken and gcwq->lock stayed locked, true
1996 * otherwise.
1997 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001998static bool maybe_create_worker(struct worker_pool *pool)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001999__releases(&gcwq->lock)
2000__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02002001{
Tejun Heo63d95a92012-07-12 14:46:37 -07002002 struct global_cwq *gcwq = pool->gcwq;
2003
2004 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002005 return false;
2006restart:
Tejun Heo9f9c2362010-07-14 11:31:20 +02002007 spin_unlock_irq(&gcwq->lock);
2008
Tejun Heoe22bee72010-06-29 10:07:14 +02002009 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07002010 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02002011
2012 while (true) {
2013 struct worker *worker;
2014
Tejun Heobc2ae0f2012-07-17 12:39:27 -07002015 worker = create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002016 if (worker) {
Tejun Heo63d95a92012-07-12 14:46:37 -07002017 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02002018 spin_lock_irq(&gcwq->lock);
2019 start_worker(worker);
Tejun Heo63d95a92012-07-12 14:46:37 -07002020 BUG_ON(need_to_create_worker(pool));
Tejun Heoe22bee72010-06-29 10:07:14 +02002021 return true;
2022 }
2023
Tejun Heo63d95a92012-07-12 14:46:37 -07002024 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002025 break;
2026
Tejun Heoe22bee72010-06-29 10:07:14 +02002027 __set_current_state(TASK_INTERRUPTIBLE);
2028 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02002029
Tejun Heo63d95a92012-07-12 14:46:37 -07002030 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002031 break;
2032 }
2033
Tejun Heo63d95a92012-07-12 14:46:37 -07002034 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02002035 spin_lock_irq(&gcwq->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07002036 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002037 goto restart;
2038 return true;
2039}
2040
2041/**
2042 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo63d95a92012-07-12 14:46:37 -07002043 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02002044 *
Tejun Heo63d95a92012-07-12 14:46:37 -07002045 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02002046 * IDLE_WORKER_TIMEOUT.
2047 *
2048 * LOCKING:
2049 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
2050 * multiple times. Called only from manager.
2051 *
2052 * RETURNS:
2053 * false if no action was taken and gcwq->lock stayed locked, true
2054 * otherwise.
2055 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002056static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02002057{
2058 bool ret = false;
2059
Tejun Heo63d95a92012-07-12 14:46:37 -07002060 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02002061 struct worker *worker;
2062 unsigned long expires;
2063
Tejun Heo63d95a92012-07-12 14:46:37 -07002064 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02002065 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
2066
2067 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07002068 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02002069 break;
2070 }
2071
2072 destroy_worker(worker);
2073 ret = true;
2074 }
2075
2076 return ret;
2077}
2078
2079/**
2080 * manage_workers - manage worker pool
2081 * @worker: self
2082 *
2083 * Assume the manager role and manage gcwq worker pool @worker belongs
2084 * to. At any given time, there can be only zero or one manager per
2085 * gcwq. The exclusion is handled automatically by this function.
2086 *
2087 * The caller can safely start processing works on false return. On
2088 * true return, it's guaranteed that need_to_create_worker() is false
2089 * and may_start_working() is true.
2090 *
2091 * CONTEXT:
2092 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
2093 * multiple times. Does GFP_KERNEL allocations.
2094 *
2095 * RETURNS:
2096 * false if no action was taken and gcwq->lock stayed locked, true if
2097 * some action was taken.
2098 */
2099static bool manage_workers(struct worker *worker)
2100{
Tejun Heo63d95a92012-07-12 14:46:37 -07002101 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002102 bool ret = false;
2103
Tejun Heo60373152012-07-17 12:39:27 -07002104 if (!mutex_trylock(&pool->manager_mutex))
Tejun Heoe22bee72010-06-29 10:07:14 +02002105 return ret;
2106
Tejun Heo11ebea52012-07-12 14:46:37 -07002107 pool->flags &= ~POOL_MANAGE_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02002108
2109 /*
2110 * Destroy and then create so that may_start_working() is true
2111 * on return.
2112 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002113 ret |= maybe_destroy_workers(pool);
2114 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002115
Tejun Heo60373152012-07-17 12:39:27 -07002116 mutex_unlock(&pool->manager_mutex);
Tejun Heoe22bee72010-06-29 10:07:14 +02002117 return ret;
2118}
2119
Tejun Heoa62428c2010-06-29 10:07:10 +02002120/**
2121 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02002122 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02002123 * @work: work to process
2124 *
2125 * Process @work. This function contains all the logics necessary to
2126 * process a single work including synchronization against and
2127 * interaction with other workers on the same cpu, queueing and
2128 * flushing. As long as context requirement is met, any worker can
2129 * call this function to process a work.
2130 *
2131 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002132 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02002133 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002134static void process_one_work(struct worker *worker, struct work_struct *work)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09002135__releases(&gcwq->lock)
2136__acquires(&gcwq->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02002137{
Tejun Heo7e116292010-06-29 10:07:13 +02002138 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002139 struct worker_pool *pool = worker->pool;
2140 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002141 struct hlist_head *bwh = busy_worker_head(gcwq, work);
Tejun Heofb0e7be2010-06-29 10:07:15 +02002142 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heoa62428c2010-06-29 10:07:10 +02002143 work_func_t f = work->func;
Tejun Heo73f53c42010-06-29 10:07:11 +02002144 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02002145 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02002146#ifdef CONFIG_LOCKDEP
2147 /*
2148 * It is permissible to free the struct work_struct from
2149 * inside the function that is called from it, this we need to
2150 * take into account for lockdep too. To avoid bogus "held
2151 * lock freed" warnings as well as problems when looking into
2152 * work->lockdep_map, make a copy and use that here.
2153 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07002154 struct lockdep_map lockdep_map;
2155
2156 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002157#endif
Tejun Heo6fec10a2012-07-22 10:16:34 -07002158 /*
2159 * Ensure we're on the correct CPU. DISASSOCIATED test is
2160 * necessary to avoid spurious warnings from rescuers servicing the
2161 * unbound or a disassociated gcwq.
2162 */
Tejun Heo25511a42012-07-17 12:39:27 -07002163 WARN_ON_ONCE(!(worker->flags & (WORKER_UNBOUND | WORKER_REBIND)) &&
Tejun Heo6fec10a2012-07-22 10:16:34 -07002164 !(gcwq->flags & GCWQ_DISASSOCIATED) &&
Tejun Heo25511a42012-07-17 12:39:27 -07002165 raw_smp_processor_id() != gcwq->cpu);
2166
Tejun Heo7e116292010-06-29 10:07:13 +02002167 /*
2168 * A single work shouldn't be executed concurrently by
2169 * multiple workers on a single cpu. Check whether anyone is
2170 * already processing the work. If so, defer the work to the
2171 * currently executing one.
2172 */
2173 collision = __find_worker_executing_work(gcwq, bwh, work);
2174 if (unlikely(collision)) {
2175 move_linked_works(work, &collision->scheduled, NULL);
2176 return;
2177 }
2178
Tejun Heo8930cab2012-08-03 10:30:45 -07002179 /* claim and dequeue */
Tejun Heoa62428c2010-06-29 10:07:10 +02002180 debug_work_deactivate(work);
Tejun Heoc8e55f32010-06-29 10:07:12 +02002181 hlist_add_head(&worker->hentry, bwh);
Tejun Heoc34056a2010-06-29 10:07:11 +02002182 worker->current_work = work;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02002183 worker->current_cwq = cwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002184 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002185
Tejun Heoa62428c2010-06-29 10:07:10 +02002186 list_del_init(&work->entry);
2187
Tejun Heo649027d2010-06-29 10:07:14 +02002188 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02002189 * CPU intensive works don't participate in concurrency
2190 * management. They're the scheduler's responsibility.
2191 */
2192 if (unlikely(cpu_intensive))
2193 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
2194
Tejun Heo974271c2012-07-12 14:46:37 -07002195 /*
2196 * Unbound gcwq isn't concurrency managed and work items should be
2197 * executed ASAP. Wake up another worker if necessary.
2198 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002199 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
2200 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07002201
Tejun Heo8930cab2012-08-03 10:30:45 -07002202 /*
Tejun Heo23657bb2012-08-13 17:08:19 -07002203 * Record the last CPU and clear PENDING which should be the last
2204 * update to @work. Also, do this inside @gcwq->lock so that
2205 * PENDING and queued state changes happen together while IRQ is
2206 * disabled.
Tejun Heo8930cab2012-08-03 10:30:45 -07002207 */
Tejun Heo8930cab2012-08-03 10:30:45 -07002208 set_work_cpu_and_clear_pending(work, gcwq->cpu);
Tejun Heoa62428c2010-06-29 10:07:10 +02002209
Tejun Heo8930cab2012-08-03 10:30:45 -07002210 spin_unlock_irq(&gcwq->lock);
Tejun Heo959d1af2012-08-03 10:30:45 -07002211
Tejun Heoe1594892011-01-09 23:32:15 +01002212 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002213 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002214 trace_workqueue_execute_start(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002215 f(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002216 /*
2217 * While we must be careful to not use "work" after this, the trace
2218 * point will only record its address.
2219 */
2220 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002221 lock_map_release(&lockdep_map);
2222 lock_map_release(&cwq->wq->lockdep_map);
2223
2224 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
2225 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
2226 "%s/0x%08x/%d\n",
2227 current->comm, preempt_count(), task_pid_nr(current));
2228 printk(KERN_ERR " last function: ");
2229 print_symbol("%s\n", (unsigned long)f);
2230 debug_show_held_locks(current);
2231 dump_stack();
2232 }
2233
Tejun Heo8b03ae32010-06-29 10:07:12 +02002234 spin_lock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002235
Tejun Heofb0e7be2010-06-29 10:07:15 +02002236 /* clear cpu intensive status */
2237 if (unlikely(cpu_intensive))
2238 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2239
Tejun Heoa62428c2010-06-29 10:07:10 +02002240 /* we're done with it, release */
Tejun Heoc8e55f32010-06-29 10:07:12 +02002241 hlist_del_init(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002242 worker->current_work = NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02002243 worker->current_cwq = NULL;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02002244 cwq_dec_nr_in_flight(cwq, work_color, false);
Tejun Heoa62428c2010-06-29 10:07:10 +02002245}
2246
Tejun Heoaffee4b2010-06-29 10:07:12 +02002247/**
2248 * process_scheduled_works - process scheduled works
2249 * @worker: self
2250 *
2251 * Process all scheduled works. Please note that the scheduled list
2252 * may change while processing a work, so this function repeatedly
2253 * fetches a work from the top and executes it.
2254 *
2255 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002256 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02002257 * multiple times.
2258 */
2259static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002261 while (!list_empty(&worker->scheduled)) {
2262 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002264 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266}
2267
Tejun Heo4690c4a2010-06-29 10:07:10 +02002268/**
2269 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02002270 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02002271 *
Tejun Heoe22bee72010-06-29 10:07:14 +02002272 * The gcwq worker thread function. There's a single dynamic pool of
2273 * these per each cpu. These workers process all works regardless of
2274 * their specific target workqueue. The only exception is works which
2275 * belong to workqueues with a rescuer which will be explained in
2276 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02002277 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002278static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279{
Tejun Heoc34056a2010-06-29 10:07:11 +02002280 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07002281 struct worker_pool *pool = worker->pool;
2282 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
Tejun Heoe22bee72010-06-29 10:07:14 +02002284 /* tell the scheduler that this is a workqueue worker */
2285 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002286woke_up:
Tejun Heoc8e55f32010-06-29 10:07:12 +02002287 spin_lock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
Tejun Heo25511a42012-07-17 12:39:27 -07002289 /*
2290 * DIE can be set only while idle and REBIND set while busy has
2291 * @worker->rebind_work scheduled. Checking here is enough.
2292 */
2293 if (unlikely(worker->flags & (WORKER_REBIND | WORKER_DIE))) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02002294 spin_unlock_irq(&gcwq->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07002295
2296 if (worker->flags & WORKER_DIE) {
2297 worker->task->flags &= ~PF_WQ_WORKER;
2298 return 0;
2299 }
2300
2301 idle_worker_rebind(worker);
2302 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 }
2304
Tejun Heoc8e55f32010-06-29 10:07:12 +02002305 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002306recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002307 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002308 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002309 goto sleep;
2310
2311 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002312 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002313 goto recheck;
2314
Tejun Heoc8e55f32010-06-29 10:07:12 +02002315 /*
2316 * ->scheduled list can only be filled while a worker is
2317 * preparing to process a work or actually processing it.
2318 * Make sure nobody diddled with it while I was sleeping.
2319 */
2320 BUG_ON(!list_empty(&worker->scheduled));
2321
Tejun Heoe22bee72010-06-29 10:07:14 +02002322 /*
2323 * When control reaches this point, we're guaranteed to have
2324 * at least one idle worker or that someone else has already
2325 * assumed the manager role.
2326 */
2327 worker_clr_flags(worker, WORKER_PREP);
2328
2329 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002330 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002331 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002332 struct work_struct, entry);
2333
2334 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2335 /* optimization path, not strictly necessary */
2336 process_one_work(worker, work);
2337 if (unlikely(!list_empty(&worker->scheduled)))
2338 process_scheduled_works(worker);
2339 } else {
2340 move_linked_works(work, &worker->scheduled, NULL);
2341 process_scheduled_works(worker);
2342 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002343 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002344
Tejun Heoe22bee72010-06-29 10:07:14 +02002345 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002346sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07002347 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002348 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002349
Tejun Heoc8e55f32010-06-29 10:07:12 +02002350 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02002351 * gcwq->lock is held and there's no work to process and no
2352 * need to manage, sleep. Workers are woken up only while
2353 * holding gcwq->lock or from local cpu, so setting the
2354 * current state before releasing gcwq->lock is enough to
2355 * prevent losing any event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002356 */
2357 worker_enter_idle(worker);
2358 __set_current_state(TASK_INTERRUPTIBLE);
2359 spin_unlock_irq(&gcwq->lock);
2360 schedule();
2361 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362}
2363
Tejun Heoe22bee72010-06-29 10:07:14 +02002364/**
2365 * rescuer_thread - the rescuer thread function
2366 * @__wq: the associated workqueue
2367 *
2368 * Workqueue rescuer thread function. There's one rescuer for each
2369 * workqueue which has WQ_RESCUER set.
2370 *
2371 * Regular work processing on a gcwq may block trying to create a new
2372 * worker which uses GFP_KERNEL allocation which has slight chance of
2373 * developing into deadlock if some works currently on the same queue
2374 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2375 * the problem rescuer solves.
2376 *
2377 * When such condition is possible, the gcwq summons rescuers of all
2378 * workqueues which have works queued on the gcwq and let them process
2379 * those works so that forward progress can be guaranteed.
2380 *
2381 * This should happen rarely.
2382 */
2383static int rescuer_thread(void *__wq)
2384{
2385 struct workqueue_struct *wq = __wq;
2386 struct worker *rescuer = wq->rescuer;
2387 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heof3421792010-07-02 10:03:51 +02002388 bool is_unbound = wq->flags & WQ_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02002389 unsigned int cpu;
2390
2391 set_user_nice(current, RESCUER_NICE_LEVEL);
2392repeat:
2393 set_current_state(TASK_INTERRUPTIBLE);
2394
2395 if (kthread_should_stop())
2396 return 0;
2397
Tejun Heof3421792010-07-02 10:03:51 +02002398 /*
2399 * See whether any cpu is asking for help. Unbounded
2400 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
2401 */
Tejun Heof2e005a2010-07-20 15:59:09 +02002402 for_each_mayday_cpu(cpu, wq->mayday_mask) {
Tejun Heof3421792010-07-02 10:03:51 +02002403 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2404 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002405 struct worker_pool *pool = cwq->pool;
2406 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002407 struct work_struct *work, *n;
2408
2409 __set_current_state(TASK_RUNNING);
Tejun Heof2e005a2010-07-20 15:59:09 +02002410 mayday_clear_cpu(cpu, wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002411
2412 /* migrate to the target cpu if possible */
Tejun Heobd7bdd42012-07-12 14:46:37 -07002413 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002414 worker_maybe_bind_and_lock(rescuer);
2415
2416 /*
2417 * Slurp in all works issued via this workqueue and
2418 * process'em.
2419 */
2420 BUG_ON(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002421 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02002422 if (get_work_cwq(work) == cwq)
2423 move_linked_works(work, scheduled, &n);
2424
2425 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002426
2427 /*
2428 * Leave this gcwq. If keep_working() is %true, notify a
2429 * regular worker; otherwise, we end up with 0 concurrency
2430 * and stalling the execution.
2431 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002432 if (keep_working(pool))
2433 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002434
Tejun Heoe22bee72010-06-29 10:07:14 +02002435 spin_unlock_irq(&gcwq->lock);
2436 }
2437
2438 schedule();
2439 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440}
2441
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002442struct wq_barrier {
2443 struct work_struct work;
2444 struct completion done;
2445};
2446
2447static void wq_barrier_func(struct work_struct *work)
2448{
2449 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2450 complete(&barr->done);
2451}
2452
Tejun Heo4690c4a2010-06-29 10:07:10 +02002453/**
2454 * insert_wq_barrier - insert a barrier work
2455 * @cwq: cwq to insert barrier into
2456 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002457 * @target: target work to attach @barr to
2458 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002459 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002460 * @barr is linked to @target such that @barr is completed only after
2461 * @target finishes execution. Please note that the ordering
2462 * guarantee is observed only with respect to @target and on the local
2463 * cpu.
2464 *
2465 * Currently, a queued barrier can't be canceled. This is because
2466 * try_to_grab_pending() can't determine whether the work to be
2467 * grabbed is at the head of the queue and thus can't clear LINKED
2468 * flag of the previous work while there must be a valid next work
2469 * after a work with LINKED flag set.
2470 *
2471 * Note that when @worker is non-NULL, @target may be modified
2472 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002473 *
2474 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002475 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002476 */
Oleg Nesterov83c22522007-05-09 02:33:54 -07002477static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002478 struct wq_barrier *barr,
2479 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002480{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002481 struct list_head *head;
2482 unsigned int linked = 0;
2483
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002484 /*
Tejun Heo8b03ae32010-06-29 10:07:12 +02002485 * debugobject calls are safe here even with gcwq->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002486 * as we know for sure that this will not trigger any of the
2487 * checks and call back into the fixup functions where we
2488 * might deadlock.
2489 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002490 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002491 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002492 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002493
Tejun Heoaffee4b2010-06-29 10:07:12 +02002494 /*
2495 * If @target is currently being executed, schedule the
2496 * barrier to the worker; otherwise, put it after @target.
2497 */
2498 if (worker)
2499 head = worker->scheduled.next;
2500 else {
2501 unsigned long *bits = work_data_bits(target);
2502
2503 head = target->entry.next;
2504 /* there can already be other linked works, inherit and set */
2505 linked = *bits & WORK_STRUCT_LINKED;
2506 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2507 }
2508
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002509 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002510 insert_work(cwq, &barr->work, head,
2511 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002512}
2513
Tejun Heo73f53c42010-06-29 10:07:11 +02002514/**
2515 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2516 * @wq: workqueue being flushed
2517 * @flush_color: new flush color, < 0 for no-op
2518 * @work_color: new work color, < 0 for no-op
2519 *
2520 * Prepare cwqs for workqueue flushing.
2521 *
2522 * If @flush_color is non-negative, flush_color on all cwqs should be
2523 * -1. If no cwq has in-flight commands at the specified color, all
2524 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2525 * has in flight commands, its cwq->flush_color is set to
2526 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2527 * wakeup logic is armed and %true is returned.
2528 *
2529 * The caller should have initialized @wq->first_flusher prior to
2530 * calling this function with non-negative @flush_color. If
2531 * @flush_color is negative, no flush color update is done and %false
2532 * is returned.
2533 *
2534 * If @work_color is non-negative, all cwqs should have the same
2535 * work_color which is previous to @work_color and all will be
2536 * advanced to @work_color.
2537 *
2538 * CONTEXT:
2539 * mutex_lock(wq->flush_mutex).
2540 *
2541 * RETURNS:
2542 * %true if @flush_color >= 0 and there's something to flush. %false
2543 * otherwise.
2544 */
2545static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2546 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547{
Tejun Heo73f53c42010-06-29 10:07:11 +02002548 bool wait = false;
2549 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -07002550
Tejun Heo73f53c42010-06-29 10:07:11 +02002551 if (flush_color >= 0) {
2552 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2553 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002554 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002555
Tejun Heof3421792010-07-02 10:03:51 +02002556 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002557 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002558 struct global_cwq *gcwq = cwq->pool->gcwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002559
Tejun Heo8b03ae32010-06-29 10:07:12 +02002560 spin_lock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002561
2562 if (flush_color >= 0) {
2563 BUG_ON(cwq->flush_color != -1);
2564
2565 if (cwq->nr_in_flight[flush_color]) {
2566 cwq->flush_color = flush_color;
2567 atomic_inc(&wq->nr_cwqs_to_flush);
2568 wait = true;
2569 }
2570 }
2571
2572 if (work_color >= 0) {
2573 BUG_ON(work_color != work_next_color(cwq->work_color));
2574 cwq->work_color = work_color;
2575 }
2576
Tejun Heo8b03ae32010-06-29 10:07:12 +02002577 spin_unlock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002578 }
2579
2580 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2581 complete(&wq->first_flusher->done);
2582
2583 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584}
2585
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002586/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002588 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 *
2590 * Forces execution of the workqueue and blocks until its completion.
2591 * This is typically used in driver shutdown handlers.
2592 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002593 * We sleep until all works which were queued on entry have been handled,
2594 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002596void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597{
Tejun Heo73f53c42010-06-29 10:07:11 +02002598 struct wq_flusher this_flusher = {
2599 .list = LIST_HEAD_INIT(this_flusher.list),
2600 .flush_color = -1,
2601 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2602 };
2603 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002604
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002605 lock_map_acquire(&wq->lockdep_map);
2606 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002607
2608 mutex_lock(&wq->flush_mutex);
2609
2610 /*
2611 * Start-to-wait phase
2612 */
2613 next_color = work_next_color(wq->work_color);
2614
2615 if (next_color != wq->flush_color) {
2616 /*
2617 * Color space is not full. The current work_color
2618 * becomes our flush_color and work_color is advanced
2619 * by one.
2620 */
2621 BUG_ON(!list_empty(&wq->flusher_overflow));
2622 this_flusher.flush_color = wq->work_color;
2623 wq->work_color = next_color;
2624
2625 if (!wq->first_flusher) {
2626 /* no flush in progress, become the first flusher */
2627 BUG_ON(wq->flush_color != this_flusher.flush_color);
2628
2629 wq->first_flusher = &this_flusher;
2630
2631 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2632 wq->work_color)) {
2633 /* nothing to flush, done */
2634 wq->flush_color = next_color;
2635 wq->first_flusher = NULL;
2636 goto out_unlock;
2637 }
2638 } else {
2639 /* wait in queue */
2640 BUG_ON(wq->flush_color == this_flusher.flush_color);
2641 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2642 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2643 }
2644 } else {
2645 /*
2646 * Oops, color space is full, wait on overflow queue.
2647 * The next flush completion will assign us
2648 * flush_color and transfer to flusher_queue.
2649 */
2650 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2651 }
2652
2653 mutex_unlock(&wq->flush_mutex);
2654
2655 wait_for_completion(&this_flusher.done);
2656
2657 /*
2658 * Wake-up-and-cascade phase
2659 *
2660 * First flushers are responsible for cascading flushes and
2661 * handling overflow. Non-first flushers can simply return.
2662 */
2663 if (wq->first_flusher != &this_flusher)
2664 return;
2665
2666 mutex_lock(&wq->flush_mutex);
2667
Tejun Heo4ce48b32010-07-02 10:03:51 +02002668 /* we might have raced, check again with mutex held */
2669 if (wq->first_flusher != &this_flusher)
2670 goto out_unlock;
2671
Tejun Heo73f53c42010-06-29 10:07:11 +02002672 wq->first_flusher = NULL;
2673
2674 BUG_ON(!list_empty(&this_flusher.list));
2675 BUG_ON(wq->flush_color != this_flusher.flush_color);
2676
2677 while (true) {
2678 struct wq_flusher *next, *tmp;
2679
2680 /* complete all the flushers sharing the current flush color */
2681 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2682 if (next->flush_color != wq->flush_color)
2683 break;
2684 list_del_init(&next->list);
2685 complete(&next->done);
2686 }
2687
2688 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2689 wq->flush_color != work_next_color(wq->work_color));
2690
2691 /* this flush_color is finished, advance by one */
2692 wq->flush_color = work_next_color(wq->flush_color);
2693
2694 /* one color has been freed, handle overflow queue */
2695 if (!list_empty(&wq->flusher_overflow)) {
2696 /*
2697 * Assign the same color to all overflowed
2698 * flushers, advance work_color and append to
2699 * flusher_queue. This is the start-to-wait
2700 * phase for these overflowed flushers.
2701 */
2702 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2703 tmp->flush_color = wq->work_color;
2704
2705 wq->work_color = work_next_color(wq->work_color);
2706
2707 list_splice_tail_init(&wq->flusher_overflow,
2708 &wq->flusher_queue);
2709 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2710 }
2711
2712 if (list_empty(&wq->flusher_queue)) {
2713 BUG_ON(wq->flush_color != wq->work_color);
2714 break;
2715 }
2716
2717 /*
2718 * Need to flush more colors. Make the next flusher
2719 * the new first flusher and arm cwqs.
2720 */
2721 BUG_ON(wq->flush_color == wq->work_color);
2722 BUG_ON(wq->flush_color != next->flush_color);
2723
2724 list_del_init(&next->list);
2725 wq->first_flusher = next;
2726
2727 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2728 break;
2729
2730 /*
2731 * Meh... this color is already done, clear first
2732 * flusher and repeat cascading.
2733 */
2734 wq->first_flusher = NULL;
2735 }
2736
2737out_unlock:
2738 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739}
Dave Jonesae90dd52006-06-30 01:40:45 -04002740EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002742/**
2743 * drain_workqueue - drain a workqueue
2744 * @wq: workqueue to drain
2745 *
2746 * Wait until the workqueue becomes empty. While draining is in progress,
2747 * only chain queueing is allowed. IOW, only currently pending or running
2748 * work items on @wq can queue further work items on it. @wq is flushed
2749 * repeatedly until it becomes empty. The number of flushing is detemined
2750 * by the depth of chaining and should be relatively short. Whine if it
2751 * takes too long.
2752 */
2753void drain_workqueue(struct workqueue_struct *wq)
2754{
2755 unsigned int flush_cnt = 0;
2756 unsigned int cpu;
2757
2758 /*
2759 * __queue_work() needs to test whether there are drainers, is much
2760 * hotter than drain_workqueue() and already looks at @wq->flags.
2761 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2762 */
2763 spin_lock(&workqueue_lock);
2764 if (!wq->nr_drainers++)
2765 wq->flags |= WQ_DRAINING;
2766 spin_unlock(&workqueue_lock);
2767reflush:
2768 flush_workqueue(wq);
2769
2770 for_each_cwq_cpu(cpu, wq) {
2771 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002772 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002773
Tejun Heobd7bdd42012-07-12 14:46:37 -07002774 spin_lock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002775 drained = !cwq->nr_active && list_empty(&cwq->delayed_works);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002776 spin_unlock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002777
2778 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002779 continue;
2780
2781 if (++flush_cnt == 10 ||
2782 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
2783 pr_warning("workqueue %s: flush on destruction isn't complete after %u tries\n",
2784 wq->name, flush_cnt);
2785 goto reflush;
2786 }
2787
2788 spin_lock(&workqueue_lock);
2789 if (!--wq->nr_drainers)
2790 wq->flags &= ~WQ_DRAINING;
2791 spin_unlock(&workqueue_lock);
2792}
2793EXPORT_SYMBOL_GPL(drain_workqueue);
2794
Tejun Heobaf59022010-09-16 10:42:16 +02002795static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
2796 bool wait_executing)
2797{
2798 struct worker *worker = NULL;
2799 struct global_cwq *gcwq;
2800 struct cpu_workqueue_struct *cwq;
2801
2802 might_sleep();
2803 gcwq = get_work_gcwq(work);
2804 if (!gcwq)
2805 return false;
2806
2807 spin_lock_irq(&gcwq->lock);
2808 if (!list_empty(&work->entry)) {
2809 /*
2810 * See the comment near try_to_grab_pending()->smp_rmb().
2811 * If it was re-queued to a different gcwq under us, we
2812 * are not going to wait.
2813 */
2814 smp_rmb();
2815 cwq = get_work_cwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002816 if (unlikely(!cwq || gcwq != cwq->pool->gcwq))
Tejun Heobaf59022010-09-16 10:42:16 +02002817 goto already_gone;
2818 } else if (wait_executing) {
2819 worker = find_worker_executing_work(gcwq, work);
2820 if (!worker)
2821 goto already_gone;
2822 cwq = worker->current_cwq;
2823 } else
2824 goto already_gone;
2825
2826 insert_wq_barrier(cwq, barr, work, worker);
2827 spin_unlock_irq(&gcwq->lock);
2828
Tejun Heoe1594892011-01-09 23:32:15 +01002829 /*
2830 * If @max_active is 1 or rescuer is in use, flushing another work
2831 * item on the same workqueue may lead to deadlock. Make sure the
2832 * flusher is not running on the same workqueue by verifying write
2833 * access.
2834 */
2835 if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER)
2836 lock_map_acquire(&cwq->wq->lockdep_map);
2837 else
2838 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heobaf59022010-09-16 10:42:16 +02002839 lock_map_release(&cwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002840
Tejun Heobaf59022010-09-16 10:42:16 +02002841 return true;
2842already_gone:
2843 spin_unlock_irq(&gcwq->lock);
2844 return false;
2845}
2846
Oleg Nesterovdb700892008-07-25 01:47:49 -07002847/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002848 * flush_work - wait for a work to finish executing the last queueing instance
2849 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002850 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002851 * Wait until @work has finished execution. This function considers
2852 * only the last queueing instance of @work. If @work has been
2853 * enqueued across different CPUs on a non-reentrant workqueue or on
2854 * multiple workqueues, @work might still be executing on return on
2855 * some of the CPUs from earlier queueing.
Oleg Nesterova67da702008-07-25 01:47:52 -07002856 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002857 * If @work was queued only on a non-reentrant, ordered or unbound
2858 * workqueue, @work is guaranteed to be idle on return if it hasn't
2859 * been requeued since flush started.
2860 *
2861 * RETURNS:
2862 * %true if flush_work() waited for the work to finish execution,
2863 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002864 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002865bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002866{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002867 struct wq_barrier barr;
2868
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002869 lock_map_acquire(&work->lockdep_map);
2870 lock_map_release(&work->lockdep_map);
2871
Tejun Heobaf59022010-09-16 10:42:16 +02002872 if (start_flush_work(work, &barr, true)) {
2873 wait_for_completion(&barr.done);
2874 destroy_work_on_stack(&barr.work);
2875 return true;
2876 } else
2877 return false;
Oleg Nesterovdb700892008-07-25 01:47:49 -07002878}
2879EXPORT_SYMBOL_GPL(flush_work);
2880
Tejun Heo401a8d02010-09-16 10:36:00 +02002881static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work)
2882{
2883 struct wq_barrier barr;
2884 struct worker *worker;
2885
2886 spin_lock_irq(&gcwq->lock);
2887
2888 worker = find_worker_executing_work(gcwq, work);
2889 if (unlikely(worker))
2890 insert_wq_barrier(worker->current_cwq, &barr, work, worker);
2891
2892 spin_unlock_irq(&gcwq->lock);
2893
2894 if (unlikely(worker)) {
2895 wait_for_completion(&barr.done);
2896 destroy_work_on_stack(&barr.work);
2897 return true;
2898 } else
2899 return false;
2900}
2901
2902static bool wait_on_work(struct work_struct *work)
2903{
2904 bool ret = false;
2905 int cpu;
2906
2907 might_sleep();
2908
2909 lock_map_acquire(&work->lockdep_map);
2910 lock_map_release(&work->lockdep_map);
2911
2912 for_each_gcwq_cpu(cpu)
2913 ret |= wait_on_cpu_work(get_gcwq(cpu), work);
2914 return ret;
2915}
2916
Tejun Heo09383492010-09-16 10:48:29 +02002917/**
2918 * flush_work_sync - wait until a work has finished execution
2919 * @work: the work to flush
2920 *
2921 * Wait until @work has finished execution. On return, it's
2922 * guaranteed that all queueing instances of @work which happened
2923 * before this function is called are finished. In other words, if
2924 * @work hasn't been requeued since this function was called, @work is
2925 * guaranteed to be idle on return.
2926 *
2927 * RETURNS:
2928 * %true if flush_work_sync() waited for the work to finish execution,
2929 * %false if it was already idle.
2930 */
2931bool flush_work_sync(struct work_struct *work)
2932{
2933 struct wq_barrier barr;
2934 bool pending, waited;
2935
2936 /* we'll wait for executions separately, queue barr only if pending */
2937 pending = start_flush_work(work, &barr, false);
2938
2939 /* wait for executions to finish */
2940 waited = wait_on_work(work);
2941
2942 /* wait for the pending one */
2943 if (pending) {
2944 wait_for_completion(&barr.done);
2945 destroy_work_on_stack(&barr.work);
2946 }
2947
2948 return pending || waited;
2949}
2950EXPORT_SYMBOL_GPL(flush_work_sync);
2951
Tejun Heo36e227d2012-08-03 10:30:46 -07002952static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002953{
Tejun Heobbb68df2012-08-03 10:30:46 -07002954 unsigned long flags;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002955 int ret;
2956
2957 do {
Tejun Heobbb68df2012-08-03 10:30:46 -07002958 ret = try_to_grab_pending(work, is_dwork, &flags);
2959 /*
2960 * If someone else is canceling, wait for the same event it
2961 * would be waiting for before retrying.
2962 */
2963 if (unlikely(ret == -ENOENT))
2964 wait_on_work(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002965 } while (unlikely(ret < 0));
2966
Tejun Heobbb68df2012-08-03 10:30:46 -07002967 /* tell other tasks trying to grab @work to back off */
2968 mark_work_canceling(work);
2969 local_irq_restore(flags);
2970
2971 wait_on_work(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002972 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002973 return ret;
2974}
2975
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002976/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002977 * cancel_work_sync - cancel a work and wait for it to finish
2978 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002979 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002980 * Cancel @work and wait for its execution to finish. This function
2981 * can be used even if the work re-queues itself or migrates to
2982 * another workqueue. On return from this function, @work is
2983 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002984 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002985 * cancel_work_sync(&delayed_work->work) must not be used for
2986 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002987 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002988 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002989 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002990 *
2991 * RETURNS:
2992 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002993 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002994bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002995{
Tejun Heo36e227d2012-08-03 10:30:46 -07002996 return __cancel_work_timer(work, false);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002997}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002998EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002999
Oleg Nesterov6e84d642007-05-09 02:34:46 -07003000/**
Tejun Heo401a8d02010-09-16 10:36:00 +02003001 * flush_delayed_work - wait for a dwork to finish executing the last queueing
3002 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07003003 *
Tejun Heo401a8d02010-09-16 10:36:00 +02003004 * Delayed timer is cancelled and the pending work is queued for
3005 * immediate execution. Like flush_work(), this function only
3006 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07003007 *
Tejun Heo401a8d02010-09-16 10:36:00 +02003008 * RETURNS:
3009 * %true if flush_work() waited for the work to finish execution,
3010 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07003011 */
Tejun Heo401a8d02010-09-16 10:36:00 +02003012bool flush_delayed_work(struct delayed_work *dwork)
3013{
Tejun Heo8930cab2012-08-03 10:30:45 -07003014 local_irq_disable();
Tejun Heo401a8d02010-09-16 10:36:00 +02003015 if (del_timer_sync(&dwork->timer))
Tejun Heo12650572012-08-08 09:38:42 -07003016 __queue_work(dwork->cpu,
Tejun Heo401a8d02010-09-16 10:36:00 +02003017 get_work_cwq(&dwork->work)->wq, &dwork->work);
Tejun Heo8930cab2012-08-03 10:30:45 -07003018 local_irq_enable();
Tejun Heo401a8d02010-09-16 10:36:00 +02003019 return flush_work(&dwork->work);
3020}
3021EXPORT_SYMBOL(flush_delayed_work);
3022
3023/**
Tejun Heo09383492010-09-16 10:48:29 +02003024 * flush_delayed_work_sync - wait for a dwork to finish
3025 * @dwork: the delayed work to flush
3026 *
3027 * Delayed timer is cancelled and the pending work is queued for
3028 * execution immediately. Other than timer handling, its behavior
3029 * is identical to flush_work_sync().
3030 *
3031 * RETURNS:
3032 * %true if flush_work_sync() waited for the work to finish execution,
3033 * %false if it was already idle.
3034 */
3035bool flush_delayed_work_sync(struct delayed_work *dwork)
3036{
Tejun Heo8930cab2012-08-03 10:30:45 -07003037 local_irq_disable();
Tejun Heo09383492010-09-16 10:48:29 +02003038 if (del_timer_sync(&dwork->timer))
Tejun Heo12650572012-08-08 09:38:42 -07003039 __queue_work(dwork->cpu,
Tejun Heo09383492010-09-16 10:48:29 +02003040 get_work_cwq(&dwork->work)->wq, &dwork->work);
Tejun Heo8930cab2012-08-03 10:30:45 -07003041 local_irq_enable();
Tejun Heo09383492010-09-16 10:48:29 +02003042 return flush_work_sync(&dwork->work);
3043}
3044EXPORT_SYMBOL(flush_delayed_work_sync);
3045
3046/**
Tejun Heo401a8d02010-09-16 10:36:00 +02003047 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
3048 * @dwork: the delayed work cancel
3049 *
3050 * This is cancel_work_sync() for delayed works.
3051 *
3052 * RETURNS:
3053 * %true if @dwork was pending, %false otherwise.
3054 */
3055bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07003056{
Tejun Heo36e227d2012-08-03 10:30:46 -07003057 return __cancel_work_timer(&dwork->work, true);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07003058}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07003059EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060
Tejun Heod4283e92012-08-03 10:30:44 -07003061/**
Tejun Heo0a13c002012-08-03 10:30:44 -07003062 * schedule_work_on - put work task on a specific cpu
3063 * @cpu: cpu to put the work task on
3064 * @work: job to be done
3065 *
3066 * This puts a job on a specific cpu
3067 */
Tejun Heod4283e92012-08-03 10:30:44 -07003068bool schedule_work_on(int cpu, struct work_struct *work)
Tejun Heo0a13c002012-08-03 10:30:44 -07003069{
3070 return queue_work_on(cpu, system_wq, work);
3071}
3072EXPORT_SYMBOL(schedule_work_on);
3073
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003074/**
3075 * schedule_work - put work task in global workqueue
3076 * @work: job to be done
3077 *
Tejun Heod4283e92012-08-03 10:30:44 -07003078 * Returns %false if @work was already on the kernel-global workqueue and
3079 * %true otherwise.
Bart Van Assche5b0f437d2009-07-30 19:00:53 +02003080 *
3081 * This puts a job in the kernel-global workqueue if it was not already
3082 * queued and leaves it in the same position on the kernel-global
3083 * workqueue otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003084 */
Tejun Heod4283e92012-08-03 10:30:44 -07003085bool schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086{
Tejun Heod320c032010-06-29 10:07:14 +02003087 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088}
Dave Jonesae90dd52006-06-30 01:40:45 -04003089EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003091/**
3092 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
3093 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +00003094 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003095 * @delay: number of jiffies to wait
3096 *
3097 * After waiting for a given time this puts a job in the kernel-global
3098 * workqueue on the specified CPU.
3099 */
Tejun Heod4283e92012-08-03 10:30:44 -07003100bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork,
3101 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102{
Tejun Heod320c032010-06-29 10:07:14 +02003103 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104}
Dave Jonesae90dd52006-06-30 01:40:45 -04003105EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
Andrew Mortonb6136772006-06-25 05:47:49 -07003107/**
Tejun Heo0a13c002012-08-03 10:30:44 -07003108 * schedule_delayed_work - put work task in global workqueue after delay
3109 * @dwork: job to be done
3110 * @delay: number of jiffies to wait or 0 for immediate execution
3111 *
3112 * After waiting for a given time this puts a job in the kernel-global
3113 * workqueue.
3114 */
Tejun Heod4283e92012-08-03 10:30:44 -07003115bool schedule_delayed_work(struct delayed_work *dwork, unsigned long delay)
Tejun Heo0a13c002012-08-03 10:30:44 -07003116{
3117 return queue_delayed_work(system_wq, dwork, delay);
3118}
3119EXPORT_SYMBOL(schedule_delayed_work);
3120
3121/**
Tejun Heo31ddd872010-10-19 11:14:49 +02003122 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07003123 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07003124 *
Tejun Heo31ddd872010-10-19 11:14:49 +02003125 * schedule_on_each_cpu() executes @func on each online CPU using the
3126 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07003127 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02003128 *
3129 * RETURNS:
3130 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07003131 */
David Howells65f27f32006-11-22 14:55:48 +00003132int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08003133{
3134 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02003135 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08003136
Andrew Mortonb6136772006-06-25 05:47:49 -07003137 works = alloc_percpu(struct work_struct);
3138 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08003139 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07003140
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003141 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08003142
Christoph Lameter15316ba2006-01-08 01:00:43 -08003143 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01003144 struct work_struct *work = per_cpu_ptr(works, cpu);
3145
3146 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003147 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02003148 }
Tejun Heo93981802009-11-17 14:06:20 -08003149
3150 for_each_online_cpu(cpu)
3151 flush_work(per_cpu_ptr(works, cpu));
3152
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003153 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07003154 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08003155 return 0;
3156}
3157
Alan Sterneef6a7d2010-02-12 17:39:21 +09003158/**
3159 * flush_scheduled_work - ensure that any scheduled work has run to completion.
3160 *
3161 * Forces execution of the kernel-global workqueue and blocks until its
3162 * completion.
3163 *
3164 * Think twice before calling this function! It's very easy to get into
3165 * trouble if you don't take great care. Either of the following situations
3166 * will lead to deadlock:
3167 *
3168 * One of the work items currently on the workqueue needs to acquire
3169 * a lock held by your code or its caller.
3170 *
3171 * Your code is running in the context of a work routine.
3172 *
3173 * They will be detected by lockdep when they occur, but the first might not
3174 * occur very often. It depends on what work items are on the workqueue and
3175 * what locks they need, which you have no control over.
3176 *
3177 * In most situations flushing the entire workqueue is overkill; you merely
3178 * need to know that a particular work item isn't queued and isn't running.
3179 * In such cases you should use cancel_delayed_work_sync() or
3180 * cancel_work_sync() instead.
3181 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182void flush_scheduled_work(void)
3183{
Tejun Heod320c032010-06-29 10:07:14 +02003184 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185}
Dave Jonesae90dd52006-06-30 01:40:45 -04003186EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187
3188/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06003189 * execute_in_process_context - reliably execute the routine with user context
3190 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06003191 * @ew: guaranteed storage for the execute work structure (must
3192 * be available when the work executes)
3193 *
3194 * Executes the function immediately if process context is available,
3195 * otherwise schedules the function for delayed execution.
3196 *
3197 * Returns: 0 - function was executed
3198 * 1 - function was scheduled for execution
3199 */
David Howells65f27f32006-11-22 14:55:48 +00003200int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06003201{
3202 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00003203 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003204 return 0;
3205 }
3206
David Howells65f27f32006-11-22 14:55:48 +00003207 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003208 schedule_work(&ew->work);
3209
3210 return 1;
3211}
3212EXPORT_SYMBOL_GPL(execute_in_process_context);
3213
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214int keventd_up(void)
3215{
Tejun Heod320c032010-06-29 10:07:14 +02003216 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217}
3218
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003219static int alloc_cwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220{
Oleg Nesterov3af244332007-05-09 02:34:09 -07003221 /*
Tejun Heo0f900042010-06-29 10:07:11 +02003222 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
3223 * Make sure that the alignment isn't lower than that of
3224 * unsigned long long.
Oleg Nesterov3af244332007-05-09 02:34:09 -07003225 */
Tejun Heo0f900042010-06-29 10:07:11 +02003226 const size_t size = sizeof(struct cpu_workqueue_struct);
3227 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
3228 __alignof__(unsigned long long));
Oleg Nesterov3af244332007-05-09 02:34:09 -07003229
Lai Jiangshane06ffa12012-03-09 18:03:20 +08003230 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02003231 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
Tejun Heo931ac772010-07-20 11:07:48 +02003232 else {
Tejun Heof3421792010-07-02 10:03:51 +02003233 void *ptr;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01003234
Tejun Heof3421792010-07-02 10:03:51 +02003235 /*
3236 * Allocate enough room to align cwq and put an extra
3237 * pointer at the end pointing back to the originally
3238 * allocated pointer which will be used for free.
3239 */
3240 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
3241 if (ptr) {
3242 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
3243 *(void **)(wq->cpu_wq.single + 1) = ptr;
3244 }
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003245 }
Tejun Heof3421792010-07-02 10:03:51 +02003246
Tejun Heo0415b00d12011-03-24 18:50:09 +01003247 /* just in case, make sure it's actually aligned */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003248 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
3249 return wq->cpu_wq.v ? 0 : -ENOMEM;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003250}
3251
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003252static void free_cwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07003253{
Lai Jiangshane06ffa12012-03-09 18:03:20 +08003254 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02003255 free_percpu(wq->cpu_wq.pcpu);
3256 else if (wq->cpu_wq.single) {
3257 /* the pointer to free is stored right after the cwq */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003258 kfree(*(void **)(wq->cpu_wq.single + 1));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07003259 }
3260}
3261
Tejun Heof3421792010-07-02 10:03:51 +02003262static int wq_clamp_max_active(int max_active, unsigned int flags,
3263 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003264{
Tejun Heof3421792010-07-02 10:03:51 +02003265 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3266
3267 if (max_active < 1 || max_active > lim)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003268 printk(KERN_WARNING "workqueue: max_active %d requested for %s "
3269 "is out of range, clamping between %d and %d\n",
Tejun Heof3421792010-07-02 10:03:51 +02003270 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003271
Tejun Heof3421792010-07-02 10:03:51 +02003272 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003273}
3274
Tejun Heob196be82012-01-10 15:11:35 -08003275struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02003276 unsigned int flags,
3277 int max_active,
3278 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08003279 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003280{
Tejun Heob196be82012-01-10 15:11:35 -08003281 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003282 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02003283 unsigned int cpu;
Tejun Heob196be82012-01-10 15:11:35 -08003284 size_t namelen;
3285
3286 /* determine namelen, allocate wq and format name */
3287 va_start(args, lock_name);
3288 va_copy(args1, args);
3289 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3290
3291 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3292 if (!wq)
3293 goto err;
3294
3295 vsnprintf(wq->name, namelen, fmt, args1);
3296 va_end(args);
3297 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003298
Tejun Heof3421792010-07-02 10:03:51 +02003299 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02003300 * Workqueues which may be used during memory reclaim should
3301 * have a rescuer to guarantee forward progress.
3302 */
3303 if (flags & WQ_MEM_RECLAIM)
3304 flags |= WQ_RESCUER;
3305
Tejun Heod320c032010-06-29 10:07:14 +02003306 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003307 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003308
Tejun Heob196be82012-01-10 15:11:35 -08003309 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003310 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003311 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003312 mutex_init(&wq->flush_mutex);
3313 atomic_set(&wq->nr_cwqs_to_flush, 0);
3314 INIT_LIST_HEAD(&wq->flusher_queue);
3315 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003316
Johannes Bergeb13ba82008-01-16 09:51:58 +01003317 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003318 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003319
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003320 if (alloc_cwqs(wq) < 0)
3321 goto err;
3322
Tejun Heof3421792010-07-02 10:03:51 +02003323 for_each_cwq_cpu(cpu, wq) {
Tejun Heo15376632010-06-29 10:07:11 +02003324 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003325 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo32704762012-07-13 22:16:45 -07003326 int pool_idx = (bool)(flags & WQ_HIGHPRI);
Tejun Heo15376632010-06-29 10:07:11 +02003327
Tejun Heo0f900042010-06-29 10:07:11 +02003328 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heo32704762012-07-13 22:16:45 -07003329 cwq->pool = &gcwq->pools[pool_idx];
Tejun Heoc34056a2010-06-29 10:07:11 +02003330 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02003331 cwq->flush_color = -1;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003332 cwq->max_active = max_active;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003333 INIT_LIST_HEAD(&cwq->delayed_works);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003334 }
3335
Tejun Heoe22bee72010-06-29 10:07:14 +02003336 if (flags & WQ_RESCUER) {
3337 struct worker *rescuer;
3338
Tejun Heof2e005a2010-07-20 15:59:09 +02003339 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
Tejun Heoe22bee72010-06-29 10:07:14 +02003340 goto err;
3341
3342 wq->rescuer = rescuer = alloc_worker();
3343 if (!rescuer)
3344 goto err;
3345
Tejun Heob196be82012-01-10 15:11:35 -08003346 rescuer->task = kthread_create(rescuer_thread, wq, "%s",
3347 wq->name);
Tejun Heoe22bee72010-06-29 10:07:14 +02003348 if (IS_ERR(rescuer->task))
3349 goto err;
3350
Tejun Heoe22bee72010-06-29 10:07:14 +02003351 rescuer->task->flags |= PF_THREAD_BOUND;
3352 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003353 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003354
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003355 /*
3356 * workqueue_lock protects global freeze state and workqueues
3357 * list. Grab it, set max_active accordingly and add the new
3358 * workqueue to workqueues list.
3359 */
Tejun Heo15376632010-06-29 10:07:11 +02003360 spin_lock(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003361
Tejun Heo58a69cb2011-02-16 09:25:31 +01003362 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heof3421792010-07-02 10:03:51 +02003363 for_each_cwq_cpu(cpu, wq)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003364 get_cwq(cpu, wq)->max_active = 0;
3365
Tejun Heo15376632010-06-29 10:07:11 +02003366 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003367
Tejun Heo15376632010-06-29 10:07:11 +02003368 spin_unlock(&workqueue_lock);
3369
Oleg Nesterov3af244332007-05-09 02:34:09 -07003370 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02003371err:
3372 if (wq) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003373 free_cwqs(wq);
Tejun Heof2e005a2010-07-20 15:59:09 +02003374 free_mayday_mask(wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02003375 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003376 kfree(wq);
3377 }
3378 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003379}
Tejun Heod320c032010-06-29 10:07:14 +02003380EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003381
3382/**
3383 * destroy_workqueue - safely terminate a workqueue
3384 * @wq: target workqueue
3385 *
3386 * Safely destroy a workqueue. All work currently pending will be done first.
3387 */
3388void destroy_workqueue(struct workqueue_struct *wq)
3389{
Tejun Heoc8e55f32010-06-29 10:07:12 +02003390 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003391
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003392 /* drain it before proceeding with destruction */
3393 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003394
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003395 /*
3396 * wq list is used to freeze wq, remove from list after
3397 * flushing is complete in case freeze races us.
3398 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003399 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07003400 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003401 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003402
Tejun Heoe22bee72010-06-29 10:07:14 +02003403 /* sanity check */
Tejun Heof3421792010-07-02 10:03:51 +02003404 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02003405 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3406 int i;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003407
Tejun Heo73f53c42010-06-29 10:07:11 +02003408 for (i = 0; i < WORK_NR_COLORS; i++)
3409 BUG_ON(cwq->nr_in_flight[i]);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003410 BUG_ON(cwq->nr_active);
3411 BUG_ON(!list_empty(&cwq->delayed_works));
Tejun Heo73f53c42010-06-29 10:07:11 +02003412 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003413
Tejun Heoe22bee72010-06-29 10:07:14 +02003414 if (wq->flags & WQ_RESCUER) {
3415 kthread_stop(wq->rescuer->task);
Tejun Heof2e005a2010-07-20 15:59:09 +02003416 free_mayday_mask(wq->mayday_mask);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003417 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003418 }
3419
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003420 free_cwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003421 kfree(wq);
3422}
3423EXPORT_SYMBOL_GPL(destroy_workqueue);
3424
Tejun Heodcd989c2010-06-29 10:07:14 +02003425/**
3426 * workqueue_set_max_active - adjust max_active of a workqueue
3427 * @wq: target workqueue
3428 * @max_active: new max_active value.
3429 *
3430 * Set max_active of @wq to @max_active.
3431 *
3432 * CONTEXT:
3433 * Don't call from IRQ context.
3434 */
3435void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3436{
3437 unsigned int cpu;
3438
Tejun Heof3421792010-07-02 10:03:51 +02003439 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003440
3441 spin_lock(&workqueue_lock);
3442
3443 wq->saved_max_active = max_active;
3444
Tejun Heof3421792010-07-02 10:03:51 +02003445 for_each_cwq_cpu(cpu, wq) {
Tejun Heodcd989c2010-06-29 10:07:14 +02003446 struct global_cwq *gcwq = get_gcwq(cpu);
3447
3448 spin_lock_irq(&gcwq->lock);
3449
Tejun Heo58a69cb2011-02-16 09:25:31 +01003450 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heodcd989c2010-06-29 10:07:14 +02003451 !(gcwq->flags & GCWQ_FREEZING))
3452 get_cwq(gcwq->cpu, wq)->max_active = max_active;
3453
3454 spin_unlock_irq(&gcwq->lock);
3455 }
3456
3457 spin_unlock(&workqueue_lock);
3458}
3459EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3460
3461/**
3462 * workqueue_congested - test whether a workqueue is congested
3463 * @cpu: CPU in question
3464 * @wq: target workqueue
3465 *
3466 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3467 * no synchronization around this function and the test result is
3468 * unreliable and only useful as advisory hints or for debugging.
3469 *
3470 * RETURNS:
3471 * %true if congested, %false otherwise.
3472 */
3473bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3474{
3475 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3476
3477 return !list_empty(&cwq->delayed_works);
3478}
3479EXPORT_SYMBOL_GPL(workqueue_congested);
3480
3481/**
3482 * work_cpu - return the last known associated cpu for @work
3483 * @work: the work of interest
3484 *
3485 * RETURNS:
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003486 * CPU number if @work was ever queued. WORK_CPU_NONE otherwise.
Tejun Heodcd989c2010-06-29 10:07:14 +02003487 */
3488unsigned int work_cpu(struct work_struct *work)
3489{
3490 struct global_cwq *gcwq = get_work_gcwq(work);
3491
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003492 return gcwq ? gcwq->cpu : WORK_CPU_NONE;
Tejun Heodcd989c2010-06-29 10:07:14 +02003493}
3494EXPORT_SYMBOL_GPL(work_cpu);
3495
3496/**
3497 * work_busy - test whether a work is currently pending or running
3498 * @work: the work to be tested
3499 *
3500 * Test whether @work is currently pending or running. There is no
3501 * synchronization around this function and the test result is
3502 * unreliable and only useful as advisory hints or for debugging.
3503 * Especially for reentrant wqs, the pending state might hide the
3504 * running state.
3505 *
3506 * RETURNS:
3507 * OR'd bitmask of WORK_BUSY_* bits.
3508 */
3509unsigned int work_busy(struct work_struct *work)
3510{
3511 struct global_cwq *gcwq = get_work_gcwq(work);
3512 unsigned long flags;
3513 unsigned int ret = 0;
3514
3515 if (!gcwq)
3516 return false;
3517
3518 spin_lock_irqsave(&gcwq->lock, flags);
3519
3520 if (work_pending(work))
3521 ret |= WORK_BUSY_PENDING;
3522 if (find_worker_executing_work(gcwq, work))
3523 ret |= WORK_BUSY_RUNNING;
3524
3525 spin_unlock_irqrestore(&gcwq->lock, flags);
3526
3527 return ret;
3528}
3529EXPORT_SYMBOL_GPL(work_busy);
3530
Tejun Heodb7bccf2010-06-29 10:07:12 +02003531/*
3532 * CPU hotplug.
3533 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003534 * There are two challenges in supporting CPU hotplug. Firstly, there
3535 * are a lot of assumptions on strong associations among work, cwq and
3536 * gcwq which make migrating pending and scheduled works very
3537 * difficult to implement without impacting hot paths. Secondly,
3538 * gcwqs serve mix of short, long and very long running works making
3539 * blocked draining impractical.
3540 *
Tejun Heo628c78e2012-07-17 12:39:27 -07003541 * This is solved by allowing a gcwq to be disassociated from the CPU
3542 * running as an unbound one and allowing it to be reattached later if the
3543 * cpu comes back online.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003544 */
3545
Tejun Heo60373152012-07-17 12:39:27 -07003546/* claim manager positions of all pools */
Tejun Heo8db25e72012-07-17 12:39:28 -07003547static void gcwq_claim_management_and_lock(struct global_cwq *gcwq)
Tejun Heo60373152012-07-17 12:39:27 -07003548{
3549 struct worker_pool *pool;
3550
3551 for_each_worker_pool(pool, gcwq)
3552 mutex_lock_nested(&pool->manager_mutex, pool - gcwq->pools);
Tejun Heo8db25e72012-07-17 12:39:28 -07003553 spin_lock_irq(&gcwq->lock);
Tejun Heo60373152012-07-17 12:39:27 -07003554}
3555
3556/* release manager positions */
Tejun Heo8db25e72012-07-17 12:39:28 -07003557static void gcwq_release_management_and_unlock(struct global_cwq *gcwq)
Tejun Heo60373152012-07-17 12:39:27 -07003558{
3559 struct worker_pool *pool;
3560
Tejun Heo8db25e72012-07-17 12:39:28 -07003561 spin_unlock_irq(&gcwq->lock);
Tejun Heo60373152012-07-17 12:39:27 -07003562 for_each_worker_pool(pool, gcwq)
3563 mutex_unlock(&pool->manager_mutex);
3564}
3565
Tejun Heo628c78e2012-07-17 12:39:27 -07003566static void gcwq_unbind_fn(struct work_struct *work)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003567{
Tejun Heo628c78e2012-07-17 12:39:27 -07003568 struct global_cwq *gcwq = get_gcwq(smp_processor_id());
Tejun Heo4ce62e92012-07-13 22:16:44 -07003569 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003570 struct worker *worker;
3571 struct hlist_node *pos;
3572 int i;
3573
3574 BUG_ON(gcwq->cpu != smp_processor_id());
3575
Tejun Heo8db25e72012-07-17 12:39:28 -07003576 gcwq_claim_management_and_lock(gcwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02003577
Tejun Heof2d5a0e2012-07-17 12:39:26 -07003578 /*
3579 * We've claimed all manager positions. Make all workers unbound
3580 * and set DISASSOCIATED. Before this, all workers except for the
3581 * ones which are still executing works from before the last CPU
3582 * down must be on the cpu. After this, they may become diasporas.
3583 */
Tejun Heo60373152012-07-17 12:39:27 -07003584 for_each_worker_pool(pool, gcwq)
Tejun Heo4ce62e92012-07-13 22:16:44 -07003585 list_for_each_entry(worker, &pool->idle_list, entry)
Tejun Heo403c8212012-07-17 12:39:27 -07003586 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003587
3588 for_each_busy_worker(worker, i, pos, gcwq)
Tejun Heo403c8212012-07-17 12:39:27 -07003589 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003590
Tejun Heof2d5a0e2012-07-17 12:39:26 -07003591 gcwq->flags |= GCWQ_DISASSOCIATED;
3592
Tejun Heo8db25e72012-07-17 12:39:28 -07003593 gcwq_release_management_and_unlock(gcwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02003594
3595 /*
Tejun Heo628c78e2012-07-17 12:39:27 -07003596 * Call schedule() so that we cross rq->lock and thus can guarantee
3597 * sched callbacks see the %WORKER_UNBOUND flag. This is necessary
3598 * as scheduler callbacks may be invoked from other cpus.
3599 */
3600 schedule();
3601
3602 /*
3603 * Sched callbacks are disabled now. Zap nr_running. After this,
3604 * nr_running stays zero and need_more_worker() and keep_working()
3605 * are always true as long as the worklist is not empty. @gcwq now
3606 * behaves as unbound (in terms of concurrency management) gcwq
3607 * which is served by workers tied to the CPU.
3608 *
3609 * On return from this function, the current worker would trigger
3610 * unbound chain execution of pending work items if other workers
3611 * didn't already.
Tejun Heoe22bee72010-06-29 10:07:14 +02003612 */
Tejun Heo4ce62e92012-07-13 22:16:44 -07003613 for_each_worker_pool(pool, gcwq)
3614 atomic_set(get_pool_nr_running(pool), 0);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003615}
3616
Tejun Heo8db25e72012-07-17 12:39:28 -07003617/*
3618 * Workqueues should be brought up before normal priority CPU notifiers.
3619 * This will be registered high priority CPU notifier.
3620 */
3621static int __devinit workqueue_cpu_up_callback(struct notifier_block *nfb,
3622 unsigned long action,
3623 void *hcpu)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003624{
3625 unsigned int cpu = (unsigned long)hcpu;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003626 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003627 struct worker_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628
Tejun Heo8db25e72012-07-17 12:39:28 -07003629 switch (action & ~CPU_TASKS_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 case CPU_UP_PREPARE:
Tejun Heo4ce62e92012-07-13 22:16:44 -07003631 for_each_worker_pool(pool, gcwq) {
Tejun Heo3ce63372012-07-17 12:39:27 -07003632 struct worker *worker;
3633
3634 if (pool->nr_workers)
3635 continue;
3636
3637 worker = create_worker(pool);
3638 if (!worker)
3639 return NOTIFY_BAD;
3640
3641 spin_lock_irq(&gcwq->lock);
3642 start_worker(worker);
3643 spin_unlock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003645 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003646
Tejun Heo65758202012-07-17 12:39:26 -07003647 case CPU_DOWN_FAILED:
3648 case CPU_ONLINE:
Tejun Heo8db25e72012-07-17 12:39:28 -07003649 gcwq_claim_management_and_lock(gcwq);
3650 gcwq->flags &= ~GCWQ_DISASSOCIATED;
3651 rebind_workers(gcwq);
3652 gcwq_release_management_and_unlock(gcwq);
3653 break;
Tejun Heo65758202012-07-17 12:39:26 -07003654 }
3655 return NOTIFY_OK;
3656}
3657
3658/*
3659 * Workqueues should be brought down after normal priority CPU notifiers.
3660 * This will be registered as low priority CPU notifier.
3661 */
3662static int __devinit workqueue_cpu_down_callback(struct notifier_block *nfb,
3663 unsigned long action,
3664 void *hcpu)
3665{
Tejun Heo8db25e72012-07-17 12:39:28 -07003666 unsigned int cpu = (unsigned long)hcpu;
3667 struct work_struct unbind_work;
3668
Tejun Heo65758202012-07-17 12:39:26 -07003669 switch (action & ~CPU_TASKS_FROZEN) {
3670 case CPU_DOWN_PREPARE:
Tejun Heo8db25e72012-07-17 12:39:28 -07003671 /* unbinding should happen on the local CPU */
3672 INIT_WORK_ONSTACK(&unbind_work, gcwq_unbind_fn);
3673 schedule_work_on(cpu, &unbind_work);
3674 flush_work(&unbind_work);
3675 break;
Tejun Heo65758202012-07-17 12:39:26 -07003676 }
3677 return NOTIFY_OK;
3678}
3679
Rusty Russell2d3854a2008-11-05 13:39:10 +11003680#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003681
Rusty Russell2d3854a2008-11-05 13:39:10 +11003682struct work_for_cpu {
Andrew Morton6b440032009-04-09 09:50:37 -06003683 struct completion completion;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003684 long (*fn)(void *);
3685 void *arg;
3686 long ret;
3687};
3688
Andrew Morton6b440032009-04-09 09:50:37 -06003689static int do_work_for_cpu(void *_wfc)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003690{
Andrew Morton6b440032009-04-09 09:50:37 -06003691 struct work_for_cpu *wfc = _wfc;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003692 wfc->ret = wfc->fn(wfc->arg);
Andrew Morton6b440032009-04-09 09:50:37 -06003693 complete(&wfc->completion);
3694 return 0;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003695}
3696
3697/**
3698 * work_on_cpu - run a function in user context on a particular cpu
3699 * @cpu: the cpu to run on
3700 * @fn: the function to run
3701 * @arg: the function arg
3702 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003703 * This will return the value @fn returns.
3704 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06003705 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003706 */
3707long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3708{
Andrew Morton6b440032009-04-09 09:50:37 -06003709 struct task_struct *sub_thread;
3710 struct work_for_cpu wfc = {
3711 .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
3712 .fn = fn,
3713 .arg = arg,
3714 };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003715
Andrew Morton6b440032009-04-09 09:50:37 -06003716 sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
3717 if (IS_ERR(sub_thread))
3718 return PTR_ERR(sub_thread);
3719 kthread_bind(sub_thread, cpu);
3720 wake_up_process(sub_thread);
3721 wait_for_completion(&wfc.completion);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003722 return wfc.ret;
3723}
3724EXPORT_SYMBOL_GPL(work_on_cpu);
3725#endif /* CONFIG_SMP */
3726
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003727#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303728
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003729/**
3730 * freeze_workqueues_begin - begin freezing workqueues
3731 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003732 * Start freezing workqueues. After this function returns, all freezable
3733 * workqueues will queue new works to their frozen_works list instead of
3734 * gcwq->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003735 *
3736 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003737 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003738 */
3739void freeze_workqueues_begin(void)
3740{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003741 unsigned int cpu;
3742
3743 spin_lock(&workqueue_lock);
3744
3745 BUG_ON(workqueue_freezing);
3746 workqueue_freezing = true;
3747
Tejun Heof3421792010-07-02 10:03:51 +02003748 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003749 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003750 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003751
3752 spin_lock_irq(&gcwq->lock);
3753
Tejun Heodb7bccf2010-06-29 10:07:12 +02003754 BUG_ON(gcwq->flags & GCWQ_FREEZING);
3755 gcwq->flags |= GCWQ_FREEZING;
3756
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003757 list_for_each_entry(wq, &workqueues, list) {
3758 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3759
Tejun Heo58a69cb2011-02-16 09:25:31 +01003760 if (cwq && wq->flags & WQ_FREEZABLE)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003761 cwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003762 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003763
3764 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003765 }
3766
3767 spin_unlock(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003769
3770/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003771 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003772 *
3773 * Check whether freezing is complete. This function must be called
3774 * between freeze_workqueues_begin() and thaw_workqueues().
3775 *
3776 * CONTEXT:
3777 * Grabs and releases workqueue_lock.
3778 *
3779 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01003780 * %true if some freezable workqueues are still busy. %false if freezing
3781 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003782 */
3783bool freeze_workqueues_busy(void)
3784{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003785 unsigned int cpu;
3786 bool busy = false;
3787
3788 spin_lock(&workqueue_lock);
3789
3790 BUG_ON(!workqueue_freezing);
3791
Tejun Heof3421792010-07-02 10:03:51 +02003792 for_each_gcwq_cpu(cpu) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003793 struct workqueue_struct *wq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003794 /*
3795 * nr_active is monotonically decreasing. It's safe
3796 * to peek without lock.
3797 */
3798 list_for_each_entry(wq, &workqueues, list) {
3799 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3800
Tejun Heo58a69cb2011-02-16 09:25:31 +01003801 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003802 continue;
3803
3804 BUG_ON(cwq->nr_active < 0);
3805 if (cwq->nr_active) {
3806 busy = true;
3807 goto out_unlock;
3808 }
3809 }
3810 }
3811out_unlock:
3812 spin_unlock(&workqueue_lock);
3813 return busy;
3814}
3815
3816/**
3817 * thaw_workqueues - thaw workqueues
3818 *
3819 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo7e116292010-06-29 10:07:13 +02003820 * frozen works are transferred to their respective gcwq worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003821 *
3822 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003823 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003824 */
3825void thaw_workqueues(void)
3826{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003827 unsigned int cpu;
3828
3829 spin_lock(&workqueue_lock);
3830
3831 if (!workqueue_freezing)
3832 goto out_unlock;
3833
Tejun Heof3421792010-07-02 10:03:51 +02003834 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003835 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003836 struct worker_pool *pool;
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003837 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003838
3839 spin_lock_irq(&gcwq->lock);
3840
Tejun Heodb7bccf2010-06-29 10:07:12 +02003841 BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
3842 gcwq->flags &= ~GCWQ_FREEZING;
3843
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003844 list_for_each_entry(wq, &workqueues, list) {
3845 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3846
Tejun Heo58a69cb2011-02-16 09:25:31 +01003847 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003848 continue;
3849
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003850 /* restore max_active and repopulate worklist */
3851 cwq->max_active = wq->saved_max_active;
3852
3853 while (!list_empty(&cwq->delayed_works) &&
3854 cwq->nr_active < cwq->max_active)
3855 cwq_activate_first_delayed(cwq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003856 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003857
Tejun Heo4ce62e92012-07-13 22:16:44 -07003858 for_each_worker_pool(pool, gcwq)
3859 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02003860
Tejun Heo8b03ae32010-06-29 10:07:12 +02003861 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003862 }
3863
3864 workqueue_freezing = false;
3865out_unlock:
3866 spin_unlock(&workqueue_lock);
3867}
3868#endif /* CONFIG_FREEZER */
3869
Suresh Siddha6ee05782010-07-30 14:57:37 -07003870static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871{
Tejun Heoc34056a2010-06-29 10:07:11 +02003872 unsigned int cpu;
Tejun Heoc8e55f32010-06-29 10:07:12 +02003873 int i;
Tejun Heoc34056a2010-06-29 10:07:11 +02003874
Tejun Heob5490072012-08-03 10:30:46 -07003875 /* make sure we have enough bits for OFFQ CPU number */
3876 BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_CPU_SHIFT)) <
3877 WORK_CPU_LAST);
3878
Tejun Heo65758202012-07-17 12:39:26 -07003879 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
3880 cpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003881
3882 /* initialize gcwqs */
Tejun Heof3421792010-07-02 10:03:51 +02003883 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003884 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003885 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003886
3887 spin_lock_init(&gcwq->lock);
3888 gcwq->cpu = cpu;
Tejun Heo477a3c32010-08-31 10:54:35 +02003889 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003890
Tejun Heoc8e55f32010-06-29 10:07:12 +02003891 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
3892 INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
3893
Tejun Heo4ce62e92012-07-13 22:16:44 -07003894 for_each_worker_pool(pool, gcwq) {
3895 pool->gcwq = gcwq;
3896 INIT_LIST_HEAD(&pool->worklist);
3897 INIT_LIST_HEAD(&pool->idle_list);
Tejun Heoe22bee72010-06-29 10:07:14 +02003898
Tejun Heo4ce62e92012-07-13 22:16:44 -07003899 init_timer_deferrable(&pool->idle_timer);
3900 pool->idle_timer.function = idle_worker_timeout;
3901 pool->idle_timer.data = (unsigned long)pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003902
Tejun Heo4ce62e92012-07-13 22:16:44 -07003903 setup_timer(&pool->mayday_timer, gcwq_mayday_timeout,
3904 (unsigned long)pool);
3905
Tejun Heo60373152012-07-17 12:39:27 -07003906 mutex_init(&pool->manager_mutex);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003907 ida_init(&pool->worker_ida);
3908 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003909
Tejun Heo25511a42012-07-17 12:39:27 -07003910 init_waitqueue_head(&gcwq->rebind_hold);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003911 }
3912
Tejun Heoe22bee72010-06-29 10:07:14 +02003913 /* create the initial worker */
Tejun Heof3421792010-07-02 10:03:51 +02003914 for_each_online_gcwq_cpu(cpu) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003915 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003916 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003917
Tejun Heo477a3c32010-08-31 10:54:35 +02003918 if (cpu != WORK_CPU_UNBOUND)
3919 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003920
3921 for_each_worker_pool(pool, gcwq) {
3922 struct worker *worker;
3923
Tejun Heobc2ae0f2012-07-17 12:39:27 -07003924 worker = create_worker(pool);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003925 BUG_ON(!worker);
3926 spin_lock_irq(&gcwq->lock);
3927 start_worker(worker);
3928 spin_unlock_irq(&gcwq->lock);
3929 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003930 }
3931
Tejun Heod320c032010-06-29 10:07:14 +02003932 system_wq = alloc_workqueue("events", 0, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09003933 system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
Tejun Heod320c032010-06-29 10:07:14 +02003934 system_long_wq = alloc_workqueue("events_long", 0, 0);
3935 system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0);
Tejun Heof3421792010-07-02 10:03:51 +02003936 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3937 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01003938 system_freezable_wq = alloc_workqueue("events_freezable",
3939 WQ_FREEZABLE, 0);
Alan Stern62d3c542012-03-02 10:51:00 +01003940 system_nrt_freezable_wq = alloc_workqueue("events_nrt_freezable",
3941 WQ_NON_REENTRANT | WQ_FREEZABLE, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09003942 BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
3943 !system_nrt_wq || !system_unbound_wq || !system_freezable_wq ||
3944 !system_nrt_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07003945 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946}
Suresh Siddha6ee05782010-07-30 14:57:37 -07003947early_initcall(init_workqueues);