blob: acfabb22e2c42fb84f454fa6b4e6dac703e26eac [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
82 /* gcwq->trustee_state */
83 TRUSTEE_START = 0, /* start */
84 TRUSTEE_IN_CHARGE = 1, /* trustee in charge of gcwq */
85 TRUSTEE_BUTCHER = 2, /* butcher workers */
86 TRUSTEE_RELEASE = 3, /* release workers */
87 TRUSTEE_DONE = 4, /* trustee is done */
Tejun Heoc8e55f32010-06-29 10:07:12 +020088
Tejun Heo32704762012-07-13 22:16:45 -070089 NR_WORKER_POOLS = 2, /* # worker pools per gcwq */
Tejun Heo4ce62e92012-07-13 22:16:44 -070090
Tejun Heoc8e55f32010-06-29 10:07:12 +020091 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
92 BUSY_WORKER_HASH_SIZE = 1 << BUSY_WORKER_HASH_ORDER,
93 BUSY_WORKER_HASH_MASK = BUSY_WORKER_HASH_SIZE - 1,
Tejun Heodb7bccf2010-06-29 10:07:12 +020094
Tejun Heoe22bee72010-06-29 10:07:14 +020095 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
96 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
97
Tejun Heo3233cdb2011-02-16 18:10:19 +010098 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
99 /* call for help after 10ms
100 (min two ticks) */
Tejun Heoe22bee72010-06-29 10:07:14 +0200101 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
102 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200103 TRUSTEE_COOLDOWN = HZ / 10, /* for trustee draining */
Tejun Heoe22bee72010-06-29 10:07:14 +0200104
105 /*
106 * Rescue workers are used only on emergencies and shared by
107 * all cpus. Give -20.
108 */
109 RESCUER_NICE_LEVEL = -20,
Tejun Heo32704762012-07-13 22:16:45 -0700110 HIGHPRI_NICE_LEVEL = -20,
Tejun Heoc8e55f32010-06-29 10:07:12 +0200111};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113/*
Tejun Heo4690c4a2010-06-29 10:07:10 +0200114 * Structure fields follow one of the following exclusion rules.
115 *
Tejun Heoe41e7042010-08-24 14:22:47 +0200116 * I: Modifiable by initialization/destruction paths and read-only for
117 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200118 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200119 * P: Preemption protected. Disabling preemption is enough and should
120 * only be modified and accessed from the local cpu.
121 *
Tejun Heo8b03ae32010-06-29 10:07:12 +0200122 * L: gcwq->lock protected. Access with gcwq->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200123 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200124 * X: During normal operation, modification requires gcwq->lock and
125 * should be done only from local cpu. Either disabling preemption
126 * on local cpu or grabbing gcwq->lock is enough for read access.
Tejun Heof3421792010-07-02 10:03:51 +0200127 * If GCWQ_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200128 *
Tejun Heo73f53c42010-06-29 10:07:11 +0200129 * F: wq->flush_mutex protected.
130 *
Tejun Heo4690c4a2010-06-29 10:07:10 +0200131 * W: workqueue_lock protected.
132 */
133
Tejun Heo8b03ae32010-06-29 10:07:12 +0200134struct global_cwq;
Tejun Heobd7bdd42012-07-12 14:46:37 -0700135struct worker_pool;
Tejun Heo25511a42012-07-17 12:39:27 -0700136struct idle_rebind;
Tejun Heoc34056a2010-06-29 10:07:11 +0200137
Tejun Heoe22bee72010-06-29 10:07:14 +0200138/*
139 * The poor guys doing the actual heavy lifting. All on-duty workers
140 * are either serving the manager role, on idle list or on busy hash.
141 */
Tejun Heoc34056a2010-06-29 10:07:11 +0200142struct worker {
Tejun Heoc8e55f32010-06-29 10:07:12 +0200143 /* on idle list while idle, on busy hash table while busy */
144 union {
145 struct list_head entry; /* L: while idle */
146 struct hlist_node hentry; /* L: while busy */
147 };
148
Tejun Heoc34056a2010-06-29 10:07:11 +0200149 struct work_struct *current_work; /* L: work being processed */
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200150 struct cpu_workqueue_struct *current_cwq; /* L: current_work's cwq */
Tejun Heoaffee4b2010-06-29 10:07:12 +0200151 struct list_head scheduled; /* L: scheduled works */
Tejun Heoc34056a2010-06-29 10:07:11 +0200152 struct task_struct *task; /* I: worker task */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700153 struct worker_pool *pool; /* I: the associated pool */
Tejun Heoe22bee72010-06-29 10:07:14 +0200154 /* 64 bytes boundary on 64bit, 32 on 32bit */
155 unsigned long last_active; /* L: last active timestamp */
156 unsigned int flags; /* X: flags */
Tejun Heoc34056a2010-06-29 10:07:11 +0200157 int id; /* I: worker id */
Tejun Heo25511a42012-07-17 12:39:27 -0700158
159 /* for rebinding worker to CPU */
160 struct idle_rebind *idle_rebind; /* L: for idle worker */
161 struct work_struct rebind_work; /* L: for busy worker */
Tejun Heoc34056a2010-06-29 10:07:11 +0200162};
163
Tejun Heobd7bdd42012-07-12 14:46:37 -0700164struct worker_pool {
165 struct global_cwq *gcwq; /* I: the owning gcwq */
Tejun Heo11ebea52012-07-12 14:46:37 -0700166 unsigned int flags; /* X: flags */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700167
168 struct list_head worklist; /* L: list of pending works */
169 int nr_workers; /* L: total number of workers */
170 int nr_idle; /* L: currently idle ones */
171
172 struct list_head idle_list; /* X: list of idle workers */
173 struct timer_list idle_timer; /* L: worker idle timeout */
174 struct timer_list mayday_timer; /* L: SOS timer for workers */
175
Tejun Heo60373152012-07-17 12:39:27 -0700176 struct mutex manager_mutex; /* mutex manager should hold */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700177 struct ida worker_ida; /* L: for worker IDs */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700178};
179
Tejun Heo4690c4a2010-06-29 10:07:10 +0200180/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200181 * Global per-cpu workqueue. There's one and only one for each cpu
182 * and all works are queued and processed here regardless of their
183 * target workqueues.
Tejun Heo8b03ae32010-06-29 10:07:12 +0200184 */
185struct global_cwq {
186 spinlock_t lock; /* the gcwq lock */
187 unsigned int cpu; /* I: the associated cpu */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200188 unsigned int flags; /* L: GCWQ_* flags */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200189
Tejun Heobd7bdd42012-07-12 14:46:37 -0700190 /* workers are chained either in busy_hash or pool idle_list */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200191 struct hlist_head busy_hash[BUSY_WORKER_HASH_SIZE];
192 /* L: hash of busy workers */
193
Tejun Heo32704762012-07-13 22:16:45 -0700194 struct worker_pool pools[2]; /* normal and highpri pools */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200195
Tejun Heo25511a42012-07-17 12:39:27 -0700196 wait_queue_head_t rebind_hold; /* rebind hold wait */
197
Tejun Heodb7bccf2010-06-29 10:07:12 +0200198 struct task_struct *trustee; /* L: for gcwq shutdown */
199 unsigned int trustee_state; /* L: trustee state */
200 wait_queue_head_t trustee_wait; /* trustee wait */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200201} ____cacheline_aligned_in_smp;
202
203/*
Tejun Heo502ca9d2010-06-29 10:07:13 +0200204 * The per-CPU workqueue. The lower WORK_STRUCT_FLAG_BITS of
Tejun Heo0f900042010-06-29 10:07:11 +0200205 * work_struct->data are used for flags and thus cwqs need to be
206 * aligned at two's power of the number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 */
208struct cpu_workqueue_struct {
Tejun Heobd7bdd42012-07-12 14:46:37 -0700209 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200210 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200211 int work_color; /* L: current color */
212 int flush_color; /* L: flushing color */
213 int nr_in_flight[WORK_NR_COLORS];
214 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200215 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200216 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200217 struct list_head delayed_works; /* L: delayed works */
Tejun Heo0f900042010-06-29 10:07:11 +0200218};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200221 * Structure used to wait for workqueue flush.
222 */
223struct wq_flusher {
224 struct list_head list; /* F: list of flushers */
225 int flush_color; /* F: flush color waiting for */
226 struct completion done; /* flush completion */
227};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Tejun Heo73f53c42010-06-29 10:07:11 +0200229/*
Tejun Heof2e005a2010-07-20 15:59:09 +0200230 * All cpumasks are assumed to be always set on UP and thus can't be
231 * used to determine whether there's something to be done.
232 */
233#ifdef CONFIG_SMP
234typedef cpumask_var_t mayday_mask_t;
235#define mayday_test_and_set_cpu(cpu, mask) \
236 cpumask_test_and_set_cpu((cpu), (mask))
237#define mayday_clear_cpu(cpu, mask) cpumask_clear_cpu((cpu), (mask))
238#define for_each_mayday_cpu(cpu, mask) for_each_cpu((cpu), (mask))
Tejun Heo9c375472010-08-31 11:18:34 +0200239#define alloc_mayday_mask(maskp, gfp) zalloc_cpumask_var((maskp), (gfp))
Tejun Heof2e005a2010-07-20 15:59:09 +0200240#define free_mayday_mask(mask) free_cpumask_var((mask))
241#else
242typedef unsigned long mayday_mask_t;
243#define mayday_test_and_set_cpu(cpu, mask) test_and_set_bit(0, &(mask))
244#define mayday_clear_cpu(cpu, mask) clear_bit(0, &(mask))
245#define for_each_mayday_cpu(cpu, mask) if ((cpu) = 0, (mask))
246#define alloc_mayday_mask(maskp, gfp) true
247#define free_mayday_mask(mask) do { } while (0)
248#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250/*
251 * The externally visible workqueue abstraction is an array of
252 * per-CPU workqueues:
253 */
254struct workqueue_struct {
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200255 unsigned int flags; /* W: WQ_* flags */
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200256 union {
257 struct cpu_workqueue_struct __percpu *pcpu;
258 struct cpu_workqueue_struct *single;
259 unsigned long v;
260 } cpu_wq; /* I: cwq's */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200261 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200262
263 struct mutex flush_mutex; /* protects wq flushing */
264 int work_color; /* F: current work color */
265 int flush_color; /* F: current flush color */
266 atomic_t nr_cwqs_to_flush; /* flush in progress */
267 struct wq_flusher *first_flusher; /* F: first flusher */
268 struct list_head flusher_queue; /* F: flush waiters */
269 struct list_head flusher_overflow; /* F: flush overflow list */
270
Tejun Heof2e005a2010-07-20 15:59:09 +0200271 mayday_mask_t mayday_mask; /* cpus requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200272 struct worker *rescuer; /* I: rescue worker */
273
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200274 int nr_drainers; /* W: drain in progress */
Tejun Heodcd989c2010-06-29 10:07:14 +0200275 int saved_max_active; /* W: saved cwq max_active */
Johannes Berg4e6045f2007-10-18 23:39:55 -0700276#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200277 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700278#endif
Tejun Heob196be82012-01-10 15:11:35 -0800279 char name[]; /* I: workqueue name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280};
281
Tejun Heod320c032010-06-29 10:07:14 +0200282struct workqueue_struct *system_wq __read_mostly;
283struct workqueue_struct *system_long_wq __read_mostly;
284struct workqueue_struct *system_nrt_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200285struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100286struct workqueue_struct *system_freezable_wq __read_mostly;
Alan Stern62d3c542012-03-02 10:51:00 +0100287struct workqueue_struct *system_nrt_freezable_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200288EXPORT_SYMBOL_GPL(system_wq);
289EXPORT_SYMBOL_GPL(system_long_wq);
290EXPORT_SYMBOL_GPL(system_nrt_wq);
Tejun Heof3421792010-07-02 10:03:51 +0200291EXPORT_SYMBOL_GPL(system_unbound_wq);
Tejun Heo24d51ad2011-02-21 09:52:50 +0100292EXPORT_SYMBOL_GPL(system_freezable_wq);
Alan Stern62d3c542012-03-02 10:51:00 +0100293EXPORT_SYMBOL_GPL(system_nrt_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200294
Tejun Heo97bd2342010-10-05 10:41:14 +0200295#define CREATE_TRACE_POINTS
296#include <trace/events/workqueue.h>
297
Tejun Heo4ce62e92012-07-13 22:16:44 -0700298#define for_each_worker_pool(pool, gcwq) \
Tejun Heo32704762012-07-13 22:16:45 -0700299 for ((pool) = &(gcwq)->pools[0]; \
300 (pool) < &(gcwq)->pools[NR_WORKER_POOLS]; (pool)++)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700301
Tejun Heodb7bccf2010-06-29 10:07:12 +0200302#define for_each_busy_worker(worker, i, pos, gcwq) \
303 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \
304 hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry)
305
Tejun Heof3421792010-07-02 10:03:51 +0200306static inline int __next_gcwq_cpu(int cpu, const struct cpumask *mask,
307 unsigned int sw)
308{
309 if (cpu < nr_cpu_ids) {
310 if (sw & 1) {
311 cpu = cpumask_next(cpu, mask);
312 if (cpu < nr_cpu_ids)
313 return cpu;
314 }
315 if (sw & 2)
316 return WORK_CPU_UNBOUND;
317 }
318 return WORK_CPU_NONE;
319}
320
321static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
322 struct workqueue_struct *wq)
323{
324 return __next_gcwq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2);
325}
326
Tejun Heo09884952010-08-01 11:50:12 +0200327/*
328 * CPU iterators
329 *
330 * An extra gcwq is defined for an invalid cpu number
331 * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any
332 * specific CPU. The following iterators are similar to
333 * for_each_*_cpu() iterators but also considers the unbound gcwq.
334 *
335 * for_each_gcwq_cpu() : possible CPUs + WORK_CPU_UNBOUND
336 * for_each_online_gcwq_cpu() : online CPUs + WORK_CPU_UNBOUND
337 * for_each_cwq_cpu() : possible CPUs for bound workqueues,
338 * WORK_CPU_UNBOUND for unbound workqueues
339 */
Tejun Heof3421792010-07-02 10:03:51 +0200340#define for_each_gcwq_cpu(cpu) \
341 for ((cpu) = __next_gcwq_cpu(-1, cpu_possible_mask, 3); \
342 (cpu) < WORK_CPU_NONE; \
343 (cpu) = __next_gcwq_cpu((cpu), cpu_possible_mask, 3))
344
345#define for_each_online_gcwq_cpu(cpu) \
346 for ((cpu) = __next_gcwq_cpu(-1, cpu_online_mask, 3); \
347 (cpu) < WORK_CPU_NONE; \
348 (cpu) = __next_gcwq_cpu((cpu), cpu_online_mask, 3))
349
350#define for_each_cwq_cpu(cpu, wq) \
351 for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, (wq)); \
352 (cpu) < WORK_CPU_NONE; \
353 (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq)))
354
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900355#ifdef CONFIG_DEBUG_OBJECTS_WORK
356
357static struct debug_obj_descr work_debug_descr;
358
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100359static void *work_debug_hint(void *addr)
360{
361 return ((struct work_struct *) addr)->func;
362}
363
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900364/*
365 * fixup_init is called when:
366 * - an active object is initialized
367 */
368static int work_fixup_init(void *addr, enum debug_obj_state state)
369{
370 struct work_struct *work = addr;
371
372 switch (state) {
373 case ODEBUG_STATE_ACTIVE:
374 cancel_work_sync(work);
375 debug_object_init(work, &work_debug_descr);
376 return 1;
377 default:
378 return 0;
379 }
380}
381
382/*
383 * fixup_activate is called when:
384 * - an active object is activated
385 * - an unknown object is activated (might be a statically initialized object)
386 */
387static int work_fixup_activate(void *addr, enum debug_obj_state state)
388{
389 struct work_struct *work = addr;
390
391 switch (state) {
392
393 case ODEBUG_STATE_NOTAVAILABLE:
394 /*
395 * This is not really a fixup. The work struct was
396 * statically initialized. We just make sure that it
397 * is tracked in the object tracker.
398 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200399 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900400 debug_object_init(work, &work_debug_descr);
401 debug_object_activate(work, &work_debug_descr);
402 return 0;
403 }
404 WARN_ON_ONCE(1);
405 return 0;
406
407 case ODEBUG_STATE_ACTIVE:
408 WARN_ON(1);
409
410 default:
411 return 0;
412 }
413}
414
415/*
416 * fixup_free is called when:
417 * - an active object is freed
418 */
419static int work_fixup_free(void *addr, enum debug_obj_state state)
420{
421 struct work_struct *work = addr;
422
423 switch (state) {
424 case ODEBUG_STATE_ACTIVE:
425 cancel_work_sync(work);
426 debug_object_free(work, &work_debug_descr);
427 return 1;
428 default:
429 return 0;
430 }
431}
432
433static struct debug_obj_descr work_debug_descr = {
434 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100435 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900436 .fixup_init = work_fixup_init,
437 .fixup_activate = work_fixup_activate,
438 .fixup_free = work_fixup_free,
439};
440
441static inline void debug_work_activate(struct work_struct *work)
442{
443 debug_object_activate(work, &work_debug_descr);
444}
445
446static inline void debug_work_deactivate(struct work_struct *work)
447{
448 debug_object_deactivate(work, &work_debug_descr);
449}
450
451void __init_work(struct work_struct *work, int onstack)
452{
453 if (onstack)
454 debug_object_init_on_stack(work, &work_debug_descr);
455 else
456 debug_object_init(work, &work_debug_descr);
457}
458EXPORT_SYMBOL_GPL(__init_work);
459
460void destroy_work_on_stack(struct work_struct *work)
461{
462 debug_object_free(work, &work_debug_descr);
463}
464EXPORT_SYMBOL_GPL(destroy_work_on_stack);
465
466#else
467static inline void debug_work_activate(struct work_struct *work) { }
468static inline void debug_work_deactivate(struct work_struct *work) { }
469#endif
470
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100471/* Serializes the accesses to the list of workqueues. */
472static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473static LIST_HEAD(workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200474static bool workqueue_freezing; /* W: have wqs started freezing? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Oleg Nesterov14441962007-05-23 13:57:57 -0700476/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200477 * The almighty global cpu workqueues. nr_running is the only field
478 * which is expected to be used frequently by other cpus via
479 * try_to_wake_up(). Put it in a separate cacheline.
Oleg Nesterov14441962007-05-23 13:57:57 -0700480 */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200481static DEFINE_PER_CPU(struct global_cwq, global_cwq);
Tejun Heo4ce62e92012-07-13 22:16:44 -0700482static DEFINE_PER_CPU_SHARED_ALIGNED(atomic_t, pool_nr_running[NR_WORKER_POOLS]);
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800483
Tejun Heof3421792010-07-02 10:03:51 +0200484/*
485 * Global cpu workqueue and nr_running counter for unbound gcwq. The
486 * gcwq is always online, has GCWQ_DISASSOCIATED set, and all its
487 * workers have WORKER_UNBOUND set.
488 */
489static struct global_cwq unbound_global_cwq;
Tejun Heo4ce62e92012-07-13 22:16:44 -0700490static atomic_t unbound_pool_nr_running[NR_WORKER_POOLS] = {
491 [0 ... NR_WORKER_POOLS - 1] = ATOMIC_INIT(0), /* always 0 */
492};
Tejun Heof3421792010-07-02 10:03:51 +0200493
Tejun Heoc34056a2010-06-29 10:07:11 +0200494static int worker_thread(void *__worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Tejun Heo32704762012-07-13 22:16:45 -0700496static int worker_pool_pri(struct worker_pool *pool)
497{
498 return pool - pool->gcwq->pools;
499}
500
Tejun Heo8b03ae32010-06-29 10:07:12 +0200501static struct global_cwq *get_gcwq(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Tejun Heof3421792010-07-02 10:03:51 +0200503 if (cpu != WORK_CPU_UNBOUND)
504 return &per_cpu(global_cwq, cpu);
505 else
506 return &unbound_global_cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
Tejun Heo63d95a92012-07-12 14:46:37 -0700509static atomic_t *get_pool_nr_running(struct worker_pool *pool)
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700510{
Tejun Heo63d95a92012-07-12 14:46:37 -0700511 int cpu = pool->gcwq->cpu;
Tejun Heo32704762012-07-13 22:16:45 -0700512 int idx = worker_pool_pri(pool);
Tejun Heo63d95a92012-07-12 14:46:37 -0700513
Tejun Heof3421792010-07-02 10:03:51 +0200514 if (cpu != WORK_CPU_UNBOUND)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700515 return &per_cpu(pool_nr_running, cpu)[idx];
Tejun Heof3421792010-07-02 10:03:51 +0200516 else
Tejun Heo4ce62e92012-07-13 22:16:44 -0700517 return &unbound_pool_nr_running[idx];
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700518}
519
Tejun Heo4690c4a2010-06-29 10:07:10 +0200520static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
521 struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700522{
Tejun Heof3421792010-07-02 10:03:51 +0200523 if (!(wq->flags & WQ_UNBOUND)) {
Lai Jiangshane06ffa12012-03-09 18:03:20 +0800524 if (likely(cpu < nr_cpu_ids))
Tejun Heof3421792010-07-02 10:03:51 +0200525 return per_cpu_ptr(wq->cpu_wq.pcpu, cpu);
Tejun Heof3421792010-07-02 10:03:51 +0200526 } else if (likely(cpu == WORK_CPU_UNBOUND))
527 return wq->cpu_wq.single;
528 return NULL;
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700529}
530
Tejun Heo73f53c42010-06-29 10:07:11 +0200531static unsigned int work_color_to_flags(int color)
532{
533 return color << WORK_STRUCT_COLOR_SHIFT;
534}
535
536static int get_work_color(struct work_struct *work)
537{
538 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
539 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
540}
541
542static int work_next_color(int color)
543{
544 return (color + 1) % WORK_NR_COLORS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
David Howells4594bf12006-12-07 11:33:26 +0000547/*
Tejun Heoe1201532010-07-22 14:14:25 +0200548 * A work's data points to the cwq with WORK_STRUCT_CWQ set while the
549 * work is on queue. Once execution starts, WORK_STRUCT_CWQ is
550 * cleared and the work data contains the cpu number it was last on.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200551 *
552 * set_work_{cwq|cpu}() and clear_work_data() can be used to set the
553 * cwq, cpu or clear work->data. These functions should only be
554 * called while the work is owned - ie. while the PENDING bit is set.
555 *
556 * get_work_[g]cwq() can be used to obtain the gcwq or cwq
557 * corresponding to a work. gcwq is available once the work has been
558 * queued anywhere after initialization. cwq is available only from
559 * queueing until execution starts.
David Howells4594bf12006-12-07 11:33:26 +0000560 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200561static inline void set_work_data(struct work_struct *work, unsigned long data,
562 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000563{
David Howells4594bf12006-12-07 11:33:26 +0000564 BUG_ON(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200565 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000566}
David Howells365970a2006-11-22 14:54:49 +0000567
Tejun Heo7a22ad72010-06-29 10:07:13 +0200568static void set_work_cwq(struct work_struct *work,
569 struct cpu_workqueue_struct *cwq,
570 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200571{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200572 set_work_data(work, (unsigned long)cwq,
Tejun Heoe1201532010-07-22 14:14:25 +0200573 WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200574}
575
Tejun Heo7a22ad72010-06-29 10:07:13 +0200576static void set_work_cpu(struct work_struct *work, unsigned int cpu)
David Howells365970a2006-11-22 14:54:49 +0000577{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200578 set_work_data(work, cpu << WORK_STRUCT_FLAG_BITS, WORK_STRUCT_PENDING);
579}
580
581static void clear_work_data(struct work_struct *work)
582{
583 set_work_data(work, WORK_STRUCT_NO_CPU, 0);
584}
585
Tejun Heo7a22ad72010-06-29 10:07:13 +0200586static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work)
587{
Tejun Heoe1201532010-07-22 14:14:25 +0200588 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200589
Tejun Heoe1201532010-07-22 14:14:25 +0200590 if (data & WORK_STRUCT_CWQ)
591 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
592 else
593 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200594}
595
596static struct global_cwq *get_work_gcwq(struct work_struct *work)
597{
Tejun Heoe1201532010-07-22 14:14:25 +0200598 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200599 unsigned int cpu;
600
Tejun Heoe1201532010-07-22 14:14:25 +0200601 if (data & WORK_STRUCT_CWQ)
602 return ((struct cpu_workqueue_struct *)
Tejun Heobd7bdd42012-07-12 14:46:37 -0700603 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->gcwq;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200604
605 cpu = data >> WORK_STRUCT_FLAG_BITS;
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200606 if (cpu == WORK_CPU_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200607 return NULL;
608
Tejun Heof3421792010-07-02 10:03:51 +0200609 BUG_ON(cpu >= nr_cpu_ids && cpu != WORK_CPU_UNBOUND);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200610 return get_gcwq(cpu);
David Howells365970a2006-11-22 14:54:49 +0000611}
612
613/*
Tejun Heo32704762012-07-13 22:16:45 -0700614 * Policy functions. These define the policies on how the global worker
615 * pools are managed. Unless noted otherwise, these functions assume that
616 * they're being called with gcwq->lock held.
David Howells365970a2006-11-22 14:54:49 +0000617 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200618
Tejun Heo63d95a92012-07-12 14:46:37 -0700619static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000620{
Tejun Heo32704762012-07-13 22:16:45 -0700621 return !atomic_read(get_pool_nr_running(pool));
David Howells365970a2006-11-22 14:54:49 +0000622}
623
Tejun Heoe22bee72010-06-29 10:07:14 +0200624/*
625 * Need to wake up a worker? Called from anything but currently
626 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700627 *
628 * Note that, because unbound workers never contribute to nr_running, this
629 * function will always return %true for unbound gcwq as long as the
630 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200631 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700632static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000633{
Tejun Heo63d95a92012-07-12 14:46:37 -0700634 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000635}
636
Tejun Heoe22bee72010-06-29 10:07:14 +0200637/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700638static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200639{
Tejun Heo63d95a92012-07-12 14:46:37 -0700640 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200641}
642
643/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700644static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200645{
Tejun Heo63d95a92012-07-12 14:46:37 -0700646 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200647
Tejun Heo32704762012-07-13 22:16:45 -0700648 return !list_empty(&pool->worklist) && atomic_read(nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200649}
650
651/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700652static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200653{
Tejun Heo63d95a92012-07-12 14:46:37 -0700654 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200655}
656
657/* Do I need to be the manager? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700658static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200659{
Tejun Heo63d95a92012-07-12 14:46:37 -0700660 return need_to_create_worker(pool) ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700661 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200662}
663
664/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700665static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200666{
Tejun Heo60373152012-07-17 12:39:27 -0700667 bool managing = mutex_is_locked(&pool->manager_mutex);
Tejun Heo63d95a92012-07-12 14:46:37 -0700668 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
669 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200670
671 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
672}
673
674/*
675 * Wake up functions.
676 */
677
Tejun Heo7e116292010-06-29 10:07:13 +0200678/* Return the first worker. Safe with preemption disabled */
Tejun Heo63d95a92012-07-12 14:46:37 -0700679static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200680{
Tejun Heo63d95a92012-07-12 14:46:37 -0700681 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200682 return NULL;
683
Tejun Heo63d95a92012-07-12 14:46:37 -0700684 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200685}
686
687/**
688 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700689 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200690 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700691 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200692 *
693 * CONTEXT:
694 * spin_lock_irq(gcwq->lock).
695 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700696static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200697{
Tejun Heo63d95a92012-07-12 14:46:37 -0700698 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200699
700 if (likely(worker))
701 wake_up_process(worker->task);
702}
703
Tejun Heo4690c4a2010-06-29 10:07:10 +0200704/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200705 * wq_worker_waking_up - a worker is waking up
706 * @task: task waking up
707 * @cpu: CPU @task is waking up to
708 *
709 * This function is called during try_to_wake_up() when a worker is
710 * being awoken.
711 *
712 * CONTEXT:
713 * spin_lock_irq(rq->lock)
714 */
715void wq_worker_waking_up(struct task_struct *task, unsigned int cpu)
716{
717 struct worker *worker = kthread_data(task);
718
Steven Rostedt2d646722010-12-03 23:12:33 -0500719 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heo63d95a92012-07-12 14:46:37 -0700720 atomic_inc(get_pool_nr_running(worker->pool));
Tejun Heoe22bee72010-06-29 10:07:14 +0200721}
722
723/**
724 * wq_worker_sleeping - a worker is going to sleep
725 * @task: task going to sleep
726 * @cpu: CPU in question, must be the current CPU number
727 *
728 * This function is called during schedule() when a busy worker is
729 * going to sleep. Worker on the same cpu can be woken up by
730 * returning pointer to its task.
731 *
732 * CONTEXT:
733 * spin_lock_irq(rq->lock)
734 *
735 * RETURNS:
736 * Worker task on @cpu to wake up, %NULL if none.
737 */
738struct task_struct *wq_worker_sleeping(struct task_struct *task,
739 unsigned int cpu)
740{
741 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heobd7bdd42012-07-12 14:46:37 -0700742 struct worker_pool *pool = worker->pool;
Tejun Heo63d95a92012-07-12 14:46:37 -0700743 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200744
Steven Rostedt2d646722010-12-03 23:12:33 -0500745 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200746 return NULL;
747
748 /* this can only happen on the local cpu */
749 BUG_ON(cpu != raw_smp_processor_id());
750
751 /*
752 * The counterpart of the following dec_and_test, implied mb,
753 * worklist not empty test sequence is in insert_work().
754 * Please read comment there.
755 *
756 * NOT_RUNNING is clear. This means that trustee is not in
757 * charge and we're running on the local cpu w/ rq lock held
758 * and preemption disabled, which in turn means that none else
759 * could be manipulating idle_list, so dereferencing idle_list
760 * without gcwq lock is safe.
761 */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700762 if (atomic_dec_and_test(nr_running) && !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700763 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200764 return to_wakeup ? to_wakeup->task : NULL;
765}
766
767/**
768 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200769 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200770 * @flags: flags to set
771 * @wakeup: wakeup an idle worker if necessary
772 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200773 * Set @flags in @worker->flags and adjust nr_running accordingly. If
774 * nr_running becomes zero and @wakeup is %true, an idle worker is
775 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200776 *
Tejun Heocb444762010-07-02 10:03:50 +0200777 * CONTEXT:
778 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200779 */
780static inline void worker_set_flags(struct worker *worker, unsigned int flags,
781 bool wakeup)
782{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700783 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200784
Tejun Heocb444762010-07-02 10:03:50 +0200785 WARN_ON_ONCE(worker->task != current);
786
Tejun Heoe22bee72010-06-29 10:07:14 +0200787 /*
788 * If transitioning into NOT_RUNNING, adjust nr_running and
789 * wake up an idle worker as necessary if requested by
790 * @wakeup.
791 */
792 if ((flags & WORKER_NOT_RUNNING) &&
793 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heo63d95a92012-07-12 14:46:37 -0700794 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200795
796 if (wakeup) {
797 if (atomic_dec_and_test(nr_running) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -0700798 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700799 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200800 } else
801 atomic_dec(nr_running);
802 }
803
Tejun Heod302f012010-06-29 10:07:13 +0200804 worker->flags |= flags;
805}
806
807/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200808 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200809 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200810 * @flags: flags to clear
811 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200812 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200813 *
Tejun Heocb444762010-07-02 10:03:50 +0200814 * CONTEXT:
815 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200816 */
817static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
818{
Tejun Heo63d95a92012-07-12 14:46:37 -0700819 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200820 unsigned int oflags = worker->flags;
821
Tejun Heocb444762010-07-02 10:03:50 +0200822 WARN_ON_ONCE(worker->task != current);
823
Tejun Heod302f012010-06-29 10:07:13 +0200824 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200825
Tejun Heo42c025f2011-01-11 15:58:49 +0100826 /*
827 * If transitioning out of NOT_RUNNING, increment nr_running. Note
828 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
829 * of multiple flags, not a single flag.
830 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200831 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
832 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heo63d95a92012-07-12 14:46:37 -0700833 atomic_inc(get_pool_nr_running(pool));
Tejun Heod302f012010-06-29 10:07:13 +0200834}
835
836/**
Tejun Heoc8e55f32010-06-29 10:07:12 +0200837 * busy_worker_head - return the busy hash head for a work
838 * @gcwq: gcwq of interest
839 * @work: work to be hashed
840 *
841 * Return hash head of @gcwq for @work.
842 *
843 * CONTEXT:
844 * spin_lock_irq(gcwq->lock).
845 *
846 * RETURNS:
847 * Pointer to the hash head.
848 */
849static struct hlist_head *busy_worker_head(struct global_cwq *gcwq,
850 struct work_struct *work)
851{
852 const int base_shift = ilog2(sizeof(struct work_struct));
853 unsigned long v = (unsigned long)work;
854
855 /* simple shift and fold hash, do we need something better? */
856 v >>= base_shift;
857 v += v >> BUSY_WORKER_HASH_ORDER;
858 v &= BUSY_WORKER_HASH_MASK;
859
860 return &gcwq->busy_hash[v];
861}
862
863/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200864 * __find_worker_executing_work - find worker which is executing a work
865 * @gcwq: gcwq of interest
866 * @bwh: hash head as returned by busy_worker_head()
867 * @work: work to find worker for
868 *
869 * Find a worker which is executing @work on @gcwq. @bwh should be
870 * the hash head obtained by calling busy_worker_head() with the same
871 * work.
872 *
873 * CONTEXT:
874 * spin_lock_irq(gcwq->lock).
875 *
876 * RETURNS:
877 * Pointer to worker which is executing @work if found, NULL
878 * otherwise.
879 */
880static struct worker *__find_worker_executing_work(struct global_cwq *gcwq,
881 struct hlist_head *bwh,
882 struct work_struct *work)
883{
884 struct worker *worker;
885 struct hlist_node *tmp;
886
887 hlist_for_each_entry(worker, tmp, bwh, hentry)
888 if (worker->current_work == work)
889 return worker;
890 return NULL;
891}
892
893/**
894 * find_worker_executing_work - find worker which is executing a work
895 * @gcwq: gcwq of interest
896 * @work: work to find worker for
897 *
898 * Find a worker which is executing @work on @gcwq. This function is
899 * identical to __find_worker_executing_work() except that this
900 * function calculates @bwh itself.
901 *
902 * CONTEXT:
903 * spin_lock_irq(gcwq->lock).
904 *
905 * RETURNS:
906 * Pointer to worker which is executing @work if found, NULL
907 * otherwise.
908 */
909static struct worker *find_worker_executing_work(struct global_cwq *gcwq,
910 struct work_struct *work)
911{
912 return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work),
913 work);
914}
915
916/**
Tejun Heo7e116292010-06-29 10:07:13 +0200917 * insert_work - insert a work into gcwq
Tejun Heo4690c4a2010-06-29 10:07:10 +0200918 * @cwq: cwq @work belongs to
919 * @work: work to insert
920 * @head: insertion point
921 * @extra_flags: extra WORK_STRUCT_* flags to set
922 *
Tejun Heo7e116292010-06-29 10:07:13 +0200923 * Insert @work which belongs to @cwq into @gcwq after @head.
924 * @extra_flags is or'd to work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200925 *
926 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +0200927 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +0200928 */
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700929static void insert_work(struct cpu_workqueue_struct *cwq,
Tejun Heo4690c4a2010-06-29 10:07:10 +0200930 struct work_struct *work, struct list_head *head,
931 unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700932{
Tejun Heo63d95a92012-07-12 14:46:37 -0700933 struct worker_pool *pool = cwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100934
Tejun Heo4690c4a2010-06-29 10:07:10 +0200935 /* we own @work, set data and link */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200936 set_work_cwq(work, cwq, extra_flags);
Tejun Heo4690c4a2010-06-29 10:07:10 +0200937
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700938 /*
939 * Ensure that we get the right work->data if we see the
940 * result of list_add() below, see try_to_grab_pending().
941 */
942 smp_wmb();
Tejun Heo4690c4a2010-06-29 10:07:10 +0200943
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700944 list_add_tail(&work->entry, head);
Tejun Heoe22bee72010-06-29 10:07:14 +0200945
946 /*
947 * Ensure either worker_sched_deactivated() sees the above
948 * list_add_tail() or we see zero nr_running to avoid workers
949 * lying around lazily while there are works to be processed.
950 */
951 smp_mb();
952
Tejun Heo63d95a92012-07-12 14:46:37 -0700953 if (__need_more_worker(pool))
954 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700955}
956
Tejun Heoc8efcc22010-12-20 19:32:04 +0100957/*
958 * Test whether @work is being queued from another work executing on the
959 * same workqueue. This is rather expensive and should only be used from
960 * cold paths.
961 */
962static bool is_chained_work(struct workqueue_struct *wq)
963{
964 unsigned long flags;
965 unsigned int cpu;
966
967 for_each_gcwq_cpu(cpu) {
968 struct global_cwq *gcwq = get_gcwq(cpu);
969 struct worker *worker;
970 struct hlist_node *pos;
971 int i;
972
973 spin_lock_irqsave(&gcwq->lock, flags);
974 for_each_busy_worker(worker, i, pos, gcwq) {
975 if (worker->task != current)
976 continue;
977 spin_unlock_irqrestore(&gcwq->lock, flags);
978 /*
979 * I'm @worker, no locking necessary. See if @work
980 * is headed to the same workqueue.
981 */
982 return worker->current_cwq->wq == wq;
983 }
984 spin_unlock_irqrestore(&gcwq->lock, flags);
985 }
986 return false;
987}
988
Tejun Heo4690c4a2010-06-29 10:07:10 +0200989static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 struct work_struct *work)
991{
Tejun Heo502ca9d2010-06-29 10:07:13 +0200992 struct global_cwq *gcwq;
993 struct cpu_workqueue_struct *cwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200994 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +0200995 unsigned int work_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 unsigned long flags;
997
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900998 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200999
Tejun Heoc8efcc22010-12-20 19:32:04 +01001000 /* if dying, only works from the same workqueue are allowed */
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02001001 if (unlikely(wq->flags & WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001002 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001003 return;
1004
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001005 /* determine gcwq to use */
1006 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001007 struct global_cwq *last_gcwq;
1008
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001009 if (unlikely(cpu == WORK_CPU_UNBOUND))
1010 cpu = raw_smp_processor_id();
1011
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001012 /*
1013 * It's multi cpu. If @wq is non-reentrant and @work
1014 * was previously on a different cpu, it might still
1015 * be running there, in which case the work needs to
1016 * be queued on that cpu to guarantee non-reentrance.
1017 */
Tejun Heo502ca9d2010-06-29 10:07:13 +02001018 gcwq = get_gcwq(cpu);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001019 if (wq->flags & WQ_NON_REENTRANT &&
1020 (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) {
1021 struct worker *worker;
1022
1023 spin_lock_irqsave(&last_gcwq->lock, flags);
1024
1025 worker = find_worker_executing_work(last_gcwq, work);
1026
1027 if (worker && worker->current_cwq->wq == wq)
1028 gcwq = last_gcwq;
1029 else {
1030 /* meh... not running there, queue here */
1031 spin_unlock_irqrestore(&last_gcwq->lock, flags);
1032 spin_lock_irqsave(&gcwq->lock, flags);
1033 }
1034 } else
1035 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heof3421792010-07-02 10:03:51 +02001036 } else {
1037 gcwq = get_gcwq(WORK_CPU_UNBOUND);
1038 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001039 }
1040
1041 /* gcwq determined, get cwq and queue */
1042 cwq = get_cwq(gcwq->cpu, wq);
Tejun Heocdadf002010-10-05 10:49:55 +02001043 trace_workqueue_queue_work(cpu, cwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001044
Dan Carpenterf5b25522012-04-13 22:06:58 +03001045 if (WARN_ON(!list_empty(&work->entry))) {
1046 spin_unlock_irqrestore(&gcwq->lock, flags);
1047 return;
1048 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001049
Tejun Heo73f53c42010-06-29 10:07:11 +02001050 cwq->nr_in_flight[cwq->work_color]++;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001051 work_flags = work_color_to_flags(cwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001052
1053 if (likely(cwq->nr_active < cwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001054 trace_workqueue_activate_work(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001055 cwq->nr_active++;
Tejun Heo32704762012-07-13 22:16:45 -07001056 worklist = &cwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001057 } else {
1058 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001059 worklist = &cwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001060 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001061
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001062 insert_work(cwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001063
Tejun Heo8b03ae32010-06-29 10:07:12 +02001064 spin_unlock_irqrestore(&gcwq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
1066
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001067/**
1068 * queue_work - queue work on a workqueue
1069 * @wq: workqueue to use
1070 * @work: work to queue
1071 *
Alan Stern057647f2006-10-28 10:38:58 -07001072 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 *
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07001074 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1075 * it can be processed by another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001077int queue_work(struct workqueue_struct *wq, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001079 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001081 ret = queue_work_on(get_cpu(), wq, work);
1082 put_cpu();
1083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return ret;
1085}
Dave Jonesae90dd52006-06-30 01:40:45 -04001086EXPORT_SYMBOL_GPL(queue_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Zhang Ruic1a220e2008-07-23 21:28:39 -07001088/**
1089 * queue_work_on - queue work on specific cpu
1090 * @cpu: CPU number to execute work on
1091 * @wq: workqueue to use
1092 * @work: work to queue
1093 *
1094 * Returns 0 if @work was already on a queue, non-zero otherwise.
1095 *
1096 * We queue the work to a specific CPU, the caller must ensure it
1097 * can't go away.
1098 */
1099int
1100queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
1101{
1102 int ret = 0;
1103
Tejun Heo22df02b2010-06-29 10:07:10 +02001104 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001105 __queue_work(cpu, wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001106 ret = 1;
1107 }
1108 return ret;
1109}
1110EXPORT_SYMBOL_GPL(queue_work_on);
1111
Li Zefan6d141c32008-02-08 04:21:09 -08001112static void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
David Howells52bad642006-11-22 14:54:01 +00001114 struct delayed_work *dwork = (struct delayed_work *)__data;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001115 struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Tejun Heo4690c4a2010-06-29 10:07:10 +02001117 __queue_work(smp_processor_id(), cwq->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
1119
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001120/**
1121 * queue_delayed_work - queue work on a workqueue after delay
1122 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001123 * @dwork: delayable work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001124 * @delay: number of jiffies to wait before queueing
1125 *
Alan Stern057647f2006-10-28 10:38:58 -07001126 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001127 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001128int queue_delayed_work(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001129 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
David Howells52bad642006-11-22 14:54:01 +00001131 if (delay == 0)
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001132 return queue_work(wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001134 return queue_delayed_work_on(-1, wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135}
Dave Jonesae90dd52006-06-30 01:40:45 -04001136EXPORT_SYMBOL_GPL(queue_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001138/**
1139 * queue_delayed_work_on - queue work on specific CPU after delay
1140 * @cpu: CPU number to execute work on
1141 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001142 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001143 * @delay: number of jiffies to wait before queueing
1144 *
Alan Stern057647f2006-10-28 10:38:58 -07001145 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001146 */
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001147int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001148 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001149{
1150 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +00001151 struct timer_list *timer = &dwork->timer;
1152 struct work_struct *work = &dwork->work;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001153
Tejun Heo22df02b2010-06-29 10:07:10 +02001154 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001155 unsigned int lcpu;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001156
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001157 BUG_ON(timer_pending(timer));
1158 BUG_ON(!list_empty(&work->entry));
1159
Andrew Liu8a3e77c2008-05-01 04:35:14 -07001160 timer_stats_timer_set_start_info(&dwork->timer);
1161
Tejun Heo7a22ad72010-06-29 10:07:13 +02001162 /*
1163 * This stores cwq for the moment, for the timer_fn.
1164 * Note that the work's gcwq is preserved to allow
1165 * reentrance detection for delayed works.
1166 */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001167 if (!(wq->flags & WQ_UNBOUND)) {
1168 struct global_cwq *gcwq = get_work_gcwq(work);
1169
1170 if (gcwq && gcwq->cpu != WORK_CPU_UNBOUND)
1171 lcpu = gcwq->cpu;
1172 else
1173 lcpu = raw_smp_processor_id();
1174 } else
1175 lcpu = WORK_CPU_UNBOUND;
1176
Tejun Heo7a22ad72010-06-29 10:07:13 +02001177 set_work_cwq(work, get_cwq(lcpu, wq), 0);
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001178
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001179 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +00001180 timer->data = (unsigned long)dwork;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001181 timer->function = delayed_work_timer_fn;
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001182
1183 if (unlikely(cpu >= 0))
1184 add_timer_on(timer, cpu);
1185 else
1186 add_timer(timer);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001187 ret = 1;
1188 }
1189 return ret;
1190}
Dave Jonesae90dd52006-06-30 01:40:45 -04001191EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Tejun Heoc8e55f32010-06-29 10:07:12 +02001193/**
1194 * worker_enter_idle - enter idle state
1195 * @worker: worker which is entering idle state
1196 *
1197 * @worker is entering idle state. Update stats and idle timer if
1198 * necessary.
1199 *
1200 * LOCKING:
1201 * spin_lock_irq(gcwq->lock).
1202 */
1203static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001205 struct worker_pool *pool = worker->pool;
1206 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Tejun Heoc8e55f32010-06-29 10:07:12 +02001208 BUG_ON(worker->flags & WORKER_IDLE);
1209 BUG_ON(!list_empty(&worker->entry) &&
1210 (worker->hentry.next || worker->hentry.pprev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Tejun Heocb444762010-07-02 10:03:50 +02001212 /* can't use worker_set_flags(), also called from start_worker() */
1213 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001214 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001215 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001216
Tejun Heoc8e55f32010-06-29 10:07:12 +02001217 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001218 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001219
Tejun Heo403c8212012-07-17 12:39:27 -07001220 if (likely(gcwq->trustee_state != TRUSTEE_DONE)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001221 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
Tejun Heobd7bdd42012-07-12 14:46:37 -07001222 mod_timer(&pool->idle_timer,
Tejun Heoe22bee72010-06-29 10:07:14 +02001223 jiffies + IDLE_WORKER_TIMEOUT);
1224 } else
Tejun Heodb7bccf2010-06-29 10:07:12 +02001225 wake_up_all(&gcwq->trustee_wait);
Tejun Heocb444762010-07-02 10:03:50 +02001226
Tejun Heo544ecf32012-05-14 15:04:50 -07001227 /*
1228 * Sanity check nr_running. Because trustee releases gcwq->lock
Tejun Heo403c8212012-07-17 12:39:27 -07001229 * between setting %WORKER_UNBOUND and zapping nr_running, the
Tejun Heo544ecf32012-05-14 15:04:50 -07001230 * warning may trigger spuriously. Check iff trustee is idle.
1231 */
1232 WARN_ON_ONCE(gcwq->trustee_state == TRUSTEE_DONE &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001233 pool->nr_workers == pool->nr_idle &&
Tejun Heo63d95a92012-07-12 14:46:37 -07001234 atomic_read(get_pool_nr_running(pool)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
Tejun Heoc8e55f32010-06-29 10:07:12 +02001237/**
1238 * worker_leave_idle - leave idle state
1239 * @worker: worker which is leaving idle state
1240 *
1241 * @worker is leaving idle state. Update stats.
1242 *
1243 * LOCKING:
1244 * spin_lock_irq(gcwq->lock).
1245 */
1246static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001248 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Tejun Heoc8e55f32010-06-29 10:07:12 +02001250 BUG_ON(!(worker->flags & WORKER_IDLE));
Tejun Heod302f012010-06-29 10:07:13 +02001251 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001252 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001253 list_del_init(&worker->entry);
1254}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Tejun Heoe22bee72010-06-29 10:07:14 +02001256/**
1257 * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq
1258 * @worker: self
1259 *
1260 * Works which are scheduled while the cpu is online must at least be
1261 * scheduled to a worker which is bound to the cpu so that if they are
1262 * flushed from cpu callbacks while cpu is going down, they are
1263 * guaranteed to execute on the cpu.
1264 *
1265 * This function is to be used by rogue workers and rescuers to bind
1266 * themselves to the target cpu and may race with cpu going down or
1267 * coming online. kthread_bind() can't be used because it may put the
1268 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1269 * verbatim as it's best effort and blocking and gcwq may be
1270 * [dis]associated in the meantime.
1271 *
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001272 * This function tries set_cpus_allowed() and locks gcwq and verifies the
1273 * binding against %GCWQ_DISASSOCIATED which is set during
1274 * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1275 * enters idle state or fetches works without dropping lock, it can
1276 * guarantee the scheduling requirement described in the first paragraph.
Tejun Heoe22bee72010-06-29 10:07:14 +02001277 *
1278 * CONTEXT:
1279 * Might sleep. Called without any lock but returns with gcwq->lock
1280 * held.
1281 *
1282 * RETURNS:
1283 * %true if the associated gcwq is online (@worker is successfully
1284 * bound), %false if offline.
1285 */
1286static bool worker_maybe_bind_and_lock(struct worker *worker)
Namhyung Kim972fa1c2010-08-22 23:19:43 +09001287__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001288{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001289 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001290 struct task_struct *task = worker->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Tejun Heoe22bee72010-06-29 10:07:14 +02001292 while (true) {
1293 /*
1294 * The following call may fail, succeed or succeed
1295 * without actually migrating the task to the cpu if
1296 * it races with cpu hotunplug operation. Verify
1297 * against GCWQ_DISASSOCIATED.
1298 */
Tejun Heof3421792010-07-02 10:03:51 +02001299 if (!(gcwq->flags & GCWQ_DISASSOCIATED))
1300 set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu));
Oleg Nesterov85f41862007-05-09 02:34:20 -07001301
Tejun Heoe22bee72010-06-29 10:07:14 +02001302 spin_lock_irq(&gcwq->lock);
1303 if (gcwq->flags & GCWQ_DISASSOCIATED)
1304 return false;
1305 if (task_cpu(task) == gcwq->cpu &&
1306 cpumask_equal(&current->cpus_allowed,
1307 get_cpu_mask(gcwq->cpu)))
1308 return true;
1309 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001310
Tejun Heo5035b202011-04-29 18:08:37 +02001311 /*
1312 * We've raced with CPU hot[un]plug. Give it a breather
1313 * and retry migration. cond_resched() is required here;
1314 * otherwise, we might deadlock against cpu_stop trying to
1315 * bring down the CPU on non-preemptive kernel.
1316 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001317 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001318 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001319 }
1320}
1321
Tejun Heo25511a42012-07-17 12:39:27 -07001322struct idle_rebind {
1323 int cnt; /* # workers to be rebound */
1324 struct completion done; /* all workers rebound */
1325};
1326
Tejun Heoe22bee72010-06-29 10:07:14 +02001327/*
Tejun Heo25511a42012-07-17 12:39:27 -07001328 * Rebind an idle @worker to its CPU. During CPU onlining, this has to
1329 * happen synchronously for idle workers. worker_thread() will test
1330 * %WORKER_REBIND before leaving idle and call this function.
1331 */
1332static void idle_worker_rebind(struct worker *worker)
1333{
1334 struct global_cwq *gcwq = worker->pool->gcwq;
1335
1336 /* CPU must be online at this point */
1337 WARN_ON(!worker_maybe_bind_and_lock(worker));
1338 if (!--worker->idle_rebind->cnt)
1339 complete(&worker->idle_rebind->done);
1340 spin_unlock_irq(&worker->pool->gcwq->lock);
1341
1342 /* we did our part, wait for rebind_workers() to finish up */
1343 wait_event(gcwq->rebind_hold, !(worker->flags & WORKER_REBIND));
1344}
1345
1346/*
1347 * Function for @worker->rebind.work used to rebind unbound busy workers to
Tejun Heo403c8212012-07-17 12:39:27 -07001348 * the associated cpu which is coming back online. This is scheduled by
1349 * cpu up but can race with other cpu hotplug operations and may be
1350 * executed twice without intervening cpu down.
Tejun Heoe22bee72010-06-29 10:07:14 +02001351 */
Tejun Heo25511a42012-07-17 12:39:27 -07001352static void busy_worker_rebind_fn(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001353{
1354 struct worker *worker = container_of(work, struct worker, rebind_work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001355 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001356
1357 if (worker_maybe_bind_and_lock(worker))
1358 worker_clr_flags(worker, WORKER_REBIND);
1359
1360 spin_unlock_irq(&gcwq->lock);
1361}
1362
Tejun Heo25511a42012-07-17 12:39:27 -07001363/**
1364 * rebind_workers - rebind all workers of a gcwq to the associated CPU
1365 * @gcwq: gcwq of interest
1366 *
1367 * @gcwq->cpu is coming online. Rebind all workers to the CPU. Rebinding
1368 * is different for idle and busy ones.
1369 *
1370 * The idle ones should be rebound synchronously and idle rebinding should
1371 * be complete before any worker starts executing work items with
1372 * concurrency management enabled; otherwise, scheduler may oops trying to
1373 * wake up non-local idle worker from wq_worker_sleeping().
1374 *
1375 * This is achieved by repeatedly requesting rebinding until all idle
1376 * workers are known to have been rebound under @gcwq->lock and holding all
1377 * idle workers from becoming busy until idle rebinding is complete.
1378 *
1379 * Once idle workers are rebound, busy workers can be rebound as they
1380 * finish executing their current work items. Queueing the rebind work at
1381 * the head of their scheduled lists is enough. Note that nr_running will
1382 * be properbly bumped as busy workers rebind.
1383 *
1384 * On return, all workers are guaranteed to either be bound or have rebind
1385 * work item scheduled.
1386 */
1387static void rebind_workers(struct global_cwq *gcwq)
1388 __releases(&gcwq->lock) __acquires(&gcwq->lock)
1389{
1390 struct idle_rebind idle_rebind;
1391 struct worker_pool *pool;
1392 struct worker *worker;
1393 struct hlist_node *pos;
1394 int i;
1395
1396 lockdep_assert_held(&gcwq->lock);
1397
1398 for_each_worker_pool(pool, gcwq)
1399 lockdep_assert_held(&pool->manager_mutex);
1400
1401 /*
1402 * Rebind idle workers. Interlocked both ways. We wait for
1403 * workers to rebind via @idle_rebind.done. Workers will wait for
1404 * us to finish up by watching %WORKER_REBIND.
1405 */
1406 init_completion(&idle_rebind.done);
1407retry:
1408 idle_rebind.cnt = 1;
1409 INIT_COMPLETION(idle_rebind.done);
1410
1411 /* set REBIND and kick idle ones, we'll wait for these later */
1412 for_each_worker_pool(pool, gcwq) {
1413 list_for_each_entry(worker, &pool->idle_list, entry) {
1414 if (worker->flags & WORKER_REBIND)
1415 continue;
1416
1417 /* morph UNBOUND to REBIND */
1418 worker->flags &= ~WORKER_UNBOUND;
1419 worker->flags |= WORKER_REBIND;
1420
1421 idle_rebind.cnt++;
1422 worker->idle_rebind = &idle_rebind;
1423
1424 /* worker_thread() will call idle_worker_rebind() */
1425 wake_up_process(worker->task);
1426 }
1427 }
1428
1429 if (--idle_rebind.cnt) {
1430 spin_unlock_irq(&gcwq->lock);
1431 wait_for_completion(&idle_rebind.done);
1432 spin_lock_irq(&gcwq->lock);
1433 /* busy ones might have become idle while waiting, retry */
1434 goto retry;
1435 }
1436
1437 /*
1438 * All idle workers are rebound and waiting for %WORKER_REBIND to
1439 * be cleared inside idle_worker_rebind(). Clear and release.
1440 * Clearing %WORKER_REBIND from this foreign context is safe
1441 * because these workers are still guaranteed to be idle.
1442 */
1443 for_each_worker_pool(pool, gcwq)
1444 list_for_each_entry(worker, &pool->idle_list, entry)
1445 worker->flags &= ~WORKER_REBIND;
1446
1447 wake_up_all(&gcwq->rebind_hold);
1448
1449 /* rebind busy workers */
1450 for_each_busy_worker(worker, i, pos, gcwq) {
1451 struct work_struct *rebind_work = &worker->rebind_work;
1452
1453 /* morph UNBOUND to REBIND */
1454 worker->flags &= ~WORKER_UNBOUND;
1455 worker->flags |= WORKER_REBIND;
1456
1457 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
1458 work_data_bits(rebind_work)))
1459 continue;
1460
1461 /* wq doesn't matter, use the default one */
1462 debug_work_activate(rebind_work);
1463 insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work,
1464 worker->scheduled.next,
1465 work_color_to_flags(WORK_NO_COLOR));
1466 }
1467}
1468
Tejun Heoc34056a2010-06-29 10:07:11 +02001469static struct worker *alloc_worker(void)
1470{
1471 struct worker *worker;
1472
1473 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001474 if (worker) {
1475 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001476 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heo25511a42012-07-17 12:39:27 -07001477 INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn);
Tejun Heoe22bee72010-06-29 10:07:14 +02001478 /* on creation a worker is in !idle && prep state */
1479 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001480 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001481 return worker;
1482}
1483
1484/**
1485 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001486 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001487 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001488 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001489 * can be started by calling start_worker() or destroyed using
1490 * destroy_worker().
1491 *
1492 * CONTEXT:
1493 * Might sleep. Does GFP_KERNEL allocations.
1494 *
1495 * RETURNS:
1496 * Pointer to the newly created worker.
1497 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001498static struct worker *create_worker(struct worker_pool *pool)
Tejun Heoc34056a2010-06-29 10:07:11 +02001499{
Tejun Heo63d95a92012-07-12 14:46:37 -07001500 struct global_cwq *gcwq = pool->gcwq;
Tejun Heo32704762012-07-13 22:16:45 -07001501 const char *pri = worker_pool_pri(pool) ? "H" : "";
Tejun Heoc34056a2010-06-29 10:07:11 +02001502 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001503 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001504
Tejun Heo8b03ae32010-06-29 10:07:12 +02001505 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001506 while (ida_get_new(&pool->worker_ida, &id)) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001507 spin_unlock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001508 if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001509 goto fail;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001510 spin_lock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001511 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02001512 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001513
1514 worker = alloc_worker();
1515 if (!worker)
1516 goto fail;
1517
Tejun Heobd7bdd42012-07-12 14:46:37 -07001518 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001519 worker->id = id;
1520
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001521 if (gcwq->cpu != WORK_CPU_UNBOUND)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001522 worker->task = kthread_create_on_node(worker_thread,
Tejun Heo32704762012-07-13 22:16:45 -07001523 worker, cpu_to_node(gcwq->cpu),
1524 "kworker/%u:%d%s", gcwq->cpu, id, pri);
Tejun Heof3421792010-07-02 10:03:51 +02001525 else
1526 worker->task = kthread_create(worker_thread, worker,
Tejun Heo32704762012-07-13 22:16:45 -07001527 "kworker/u:%d%s", id, pri);
Tejun Heoc34056a2010-06-29 10:07:11 +02001528 if (IS_ERR(worker->task))
1529 goto fail;
1530
Tejun Heo32704762012-07-13 22:16:45 -07001531 if (worker_pool_pri(pool))
1532 set_user_nice(worker->task, HIGHPRI_NICE_LEVEL);
1533
Tejun Heodb7bccf2010-06-29 10:07:12 +02001534 /*
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001535 * Determine CPU binding of the new worker depending on
1536 * %GCWQ_DISASSOCIATED. The caller is responsible for ensuring the
1537 * flag remains stable across this function. See the comments
1538 * above the flag definition for details.
1539 *
1540 * As an unbound worker may later become a regular one if CPU comes
1541 * online, make sure every worker has %PF_THREAD_BOUND set.
Tejun Heodb7bccf2010-06-29 10:07:12 +02001542 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001543 if (!(gcwq->flags & GCWQ_DISASSOCIATED)) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001544 kthread_bind(worker->task, gcwq->cpu);
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001545 } else {
Tejun Heodb7bccf2010-06-29 10:07:12 +02001546 worker->task->flags |= PF_THREAD_BOUND;
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001547 worker->flags |= WORKER_UNBOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001549
Tejun Heoc34056a2010-06-29 10:07:11 +02001550 return worker;
1551fail:
1552 if (id >= 0) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001553 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001554 ida_remove(&pool->worker_ida, id);
Tejun Heo8b03ae32010-06-29 10:07:12 +02001555 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001556 }
1557 kfree(worker);
1558 return NULL;
1559}
1560
1561/**
1562 * start_worker - start a newly created worker
1563 * @worker: worker to start
1564 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001565 * Make the gcwq aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001566 *
1567 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001568 * spin_lock_irq(gcwq->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001569 */
1570static void start_worker(struct worker *worker)
1571{
Tejun Heocb444762010-07-02 10:03:50 +02001572 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001573 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001574 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001575 wake_up_process(worker->task);
1576}
1577
1578/**
1579 * destroy_worker - destroy a workqueue worker
1580 * @worker: worker to be destroyed
1581 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001582 * Destroy @worker and adjust @gcwq stats accordingly.
1583 *
1584 * CONTEXT:
1585 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001586 */
1587static void destroy_worker(struct worker *worker)
1588{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001589 struct worker_pool *pool = worker->pool;
1590 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001591 int id = worker->id;
1592
1593 /* sanity check frenzy */
1594 BUG_ON(worker->current_work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001595 BUG_ON(!list_empty(&worker->scheduled));
Tejun Heoc34056a2010-06-29 10:07:11 +02001596
Tejun Heoc8e55f32010-06-29 10:07:12 +02001597 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001598 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001599 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001600 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001601
1602 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001603 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001604
1605 spin_unlock_irq(&gcwq->lock);
1606
Tejun Heoc34056a2010-06-29 10:07:11 +02001607 kthread_stop(worker->task);
1608 kfree(worker);
1609
Tejun Heo8b03ae32010-06-29 10:07:12 +02001610 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001611 ida_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001612}
1613
Tejun Heo63d95a92012-07-12 14:46:37 -07001614static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001615{
Tejun Heo63d95a92012-07-12 14:46:37 -07001616 struct worker_pool *pool = (void *)__pool;
1617 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001618
1619 spin_lock_irq(&gcwq->lock);
1620
Tejun Heo63d95a92012-07-12 14:46:37 -07001621 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001622 struct worker *worker;
1623 unsigned long expires;
1624
1625 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001626 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001627 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1628
1629 if (time_before(jiffies, expires))
Tejun Heo63d95a92012-07-12 14:46:37 -07001630 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001631 else {
1632 /* it's been idle for too long, wake up manager */
Tejun Heo11ebea52012-07-12 14:46:37 -07001633 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -07001634 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001635 }
1636 }
1637
1638 spin_unlock_irq(&gcwq->lock);
1639}
1640
1641static bool send_mayday(struct work_struct *work)
1642{
1643 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1644 struct workqueue_struct *wq = cwq->wq;
Tejun Heof3421792010-07-02 10:03:51 +02001645 unsigned int cpu;
Tejun Heoe22bee72010-06-29 10:07:14 +02001646
1647 if (!(wq->flags & WQ_RESCUER))
1648 return false;
1649
1650 /* mayday mayday mayday */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001651 cpu = cwq->pool->gcwq->cpu;
Tejun Heof3421792010-07-02 10:03:51 +02001652 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1653 if (cpu == WORK_CPU_UNBOUND)
1654 cpu = 0;
Tejun Heof2e005a2010-07-20 15:59:09 +02001655 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001656 wake_up_process(wq->rescuer->task);
1657 return true;
1658}
1659
Tejun Heo63d95a92012-07-12 14:46:37 -07001660static void gcwq_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001661{
Tejun Heo63d95a92012-07-12 14:46:37 -07001662 struct worker_pool *pool = (void *)__pool;
1663 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001664 struct work_struct *work;
1665
1666 spin_lock_irq(&gcwq->lock);
1667
Tejun Heo63d95a92012-07-12 14:46:37 -07001668 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001669 /*
1670 * We've been trying to create a new worker but
1671 * haven't been successful. We might be hitting an
1672 * allocation deadlock. Send distress signals to
1673 * rescuers.
1674 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001675 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001676 send_mayday(work);
1677 }
1678
1679 spin_unlock_irq(&gcwq->lock);
1680
Tejun Heo63d95a92012-07-12 14:46:37 -07001681 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001682}
1683
1684/**
1685 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001686 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001687 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001688 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001689 * have at least one idle worker on return from this function. If
1690 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001691 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001692 * possible allocation deadlock.
1693 *
1694 * On return, need_to_create_worker() is guaranteed to be false and
1695 * may_start_working() true.
1696 *
1697 * LOCKING:
1698 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1699 * multiple times. Does GFP_KERNEL allocations. Called only from
1700 * manager.
1701 *
1702 * RETURNS:
1703 * false if no action was taken and gcwq->lock stayed locked, true
1704 * otherwise.
1705 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001706static bool maybe_create_worker(struct worker_pool *pool)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001707__releases(&gcwq->lock)
1708__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001709{
Tejun Heo63d95a92012-07-12 14:46:37 -07001710 struct global_cwq *gcwq = pool->gcwq;
1711
1712 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001713 return false;
1714restart:
Tejun Heo9f9c2362010-07-14 11:31:20 +02001715 spin_unlock_irq(&gcwq->lock);
1716
Tejun Heoe22bee72010-06-29 10:07:14 +02001717 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07001718 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001719
1720 while (true) {
1721 struct worker *worker;
1722
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001723 worker = create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001724 if (worker) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001725 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001726 spin_lock_irq(&gcwq->lock);
1727 start_worker(worker);
Tejun Heo63d95a92012-07-12 14:46:37 -07001728 BUG_ON(need_to_create_worker(pool));
Tejun Heoe22bee72010-06-29 10:07:14 +02001729 return true;
1730 }
1731
Tejun Heo63d95a92012-07-12 14:46:37 -07001732 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001733 break;
1734
Tejun Heoe22bee72010-06-29 10:07:14 +02001735 __set_current_state(TASK_INTERRUPTIBLE);
1736 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001737
Tejun Heo63d95a92012-07-12 14:46:37 -07001738 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001739 break;
1740 }
1741
Tejun Heo63d95a92012-07-12 14:46:37 -07001742 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001743 spin_lock_irq(&gcwq->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07001744 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001745 goto restart;
1746 return true;
1747}
1748
1749/**
1750 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo63d95a92012-07-12 14:46:37 -07001751 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001752 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001753 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02001754 * IDLE_WORKER_TIMEOUT.
1755 *
1756 * LOCKING:
1757 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1758 * multiple times. Called only from manager.
1759 *
1760 * RETURNS:
1761 * false if no action was taken and gcwq->lock stayed locked, true
1762 * otherwise.
1763 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001764static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001765{
1766 bool ret = false;
1767
Tejun Heo63d95a92012-07-12 14:46:37 -07001768 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001769 struct worker *worker;
1770 unsigned long expires;
1771
Tejun Heo63d95a92012-07-12 14:46:37 -07001772 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001773 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1774
1775 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001776 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001777 break;
1778 }
1779
1780 destroy_worker(worker);
1781 ret = true;
1782 }
1783
1784 return ret;
1785}
1786
1787/**
1788 * manage_workers - manage worker pool
1789 * @worker: self
1790 *
1791 * Assume the manager role and manage gcwq worker pool @worker belongs
1792 * to. At any given time, there can be only zero or one manager per
1793 * gcwq. The exclusion is handled automatically by this function.
1794 *
1795 * The caller can safely start processing works on false return. On
1796 * true return, it's guaranteed that need_to_create_worker() is false
1797 * and may_start_working() is true.
1798 *
1799 * CONTEXT:
1800 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1801 * multiple times. Does GFP_KERNEL allocations.
1802 *
1803 * RETURNS:
1804 * false if no action was taken and gcwq->lock stayed locked, true if
1805 * some action was taken.
1806 */
1807static bool manage_workers(struct worker *worker)
1808{
Tejun Heo63d95a92012-07-12 14:46:37 -07001809 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001810 bool ret = false;
1811
Tejun Heo60373152012-07-17 12:39:27 -07001812 if (!mutex_trylock(&pool->manager_mutex))
Tejun Heoe22bee72010-06-29 10:07:14 +02001813 return ret;
1814
Tejun Heo11ebea52012-07-12 14:46:37 -07001815 pool->flags &= ~POOL_MANAGE_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02001816
1817 /*
1818 * Destroy and then create so that may_start_working() is true
1819 * on return.
1820 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001821 ret |= maybe_destroy_workers(pool);
1822 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001823
Tejun Heo60373152012-07-17 12:39:27 -07001824 mutex_unlock(&pool->manager_mutex);
Tejun Heoe22bee72010-06-29 10:07:14 +02001825 return ret;
1826}
1827
Tejun Heoa62428c2010-06-29 10:07:10 +02001828/**
Tejun Heoaffee4b2010-06-29 10:07:12 +02001829 * move_linked_works - move linked works to a list
1830 * @work: start of series of works to be scheduled
1831 * @head: target list to append @work to
1832 * @nextp: out paramter for nested worklist walking
1833 *
1834 * Schedule linked works starting from @work to @head. Work series to
1835 * be scheduled starts at @work and includes any consecutive work with
1836 * WORK_STRUCT_LINKED set in its predecessor.
1837 *
1838 * If @nextp is not NULL, it's updated to point to the next work of
1839 * the last scheduled work. This allows move_linked_works() to be
1840 * nested inside outer list_for_each_entry_safe().
1841 *
1842 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001843 * spin_lock_irq(gcwq->lock).
Tejun Heoaffee4b2010-06-29 10:07:12 +02001844 */
1845static void move_linked_works(struct work_struct *work, struct list_head *head,
1846 struct work_struct **nextp)
1847{
1848 struct work_struct *n;
1849
1850 /*
1851 * Linked worklist will always end before the end of the list,
1852 * use NULL for list head.
1853 */
1854 list_for_each_entry_safe_from(work, n, NULL, entry) {
1855 list_move_tail(&work->entry, head);
1856 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1857 break;
1858 }
1859
1860 /*
1861 * If we're already inside safe list traversal and have moved
1862 * multiple works to the scheduled queue, the next position
1863 * needs to be updated.
1864 */
1865 if (nextp)
1866 *nextp = n;
1867}
1868
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001869static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
1870{
1871 struct work_struct *work = list_first_entry(&cwq->delayed_works,
1872 struct work_struct, entry);
1873
Tejun Heocdadf002010-10-05 10:49:55 +02001874 trace_workqueue_activate_work(work);
Tejun Heo32704762012-07-13 22:16:45 -07001875 move_linked_works(work, &cwq->pool->worklist, NULL);
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001876 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001877 cwq->nr_active++;
1878}
1879
Tejun Heoaffee4b2010-06-29 10:07:12 +02001880/**
Tejun Heo73f53c42010-06-29 10:07:11 +02001881 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
1882 * @cwq: cwq of interest
1883 * @color: color of work which left the queue
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001884 * @delayed: for a delayed work
Tejun Heo73f53c42010-06-29 10:07:11 +02001885 *
1886 * A work either has completed or is removed from pending queue,
1887 * decrement nr_in_flight of its cwq and handle workqueue flushing.
1888 *
1889 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001890 * spin_lock_irq(gcwq->lock).
Tejun Heo73f53c42010-06-29 10:07:11 +02001891 */
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001892static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color,
1893 bool delayed)
Tejun Heo73f53c42010-06-29 10:07:11 +02001894{
1895 /* ignore uncolored works */
1896 if (color == WORK_NO_COLOR)
1897 return;
1898
1899 cwq->nr_in_flight[color]--;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001900
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001901 if (!delayed) {
1902 cwq->nr_active--;
1903 if (!list_empty(&cwq->delayed_works)) {
1904 /* one down, submit a delayed one */
1905 if (cwq->nr_active < cwq->max_active)
1906 cwq_activate_first_delayed(cwq);
1907 }
Tejun Heo502ca9d2010-06-29 10:07:13 +02001908 }
Tejun Heo73f53c42010-06-29 10:07:11 +02001909
1910 /* is flush in progress and are we at the flushing tip? */
1911 if (likely(cwq->flush_color != color))
1912 return;
1913
1914 /* are there still in-flight works? */
1915 if (cwq->nr_in_flight[color])
1916 return;
1917
1918 /* this cwq is done, clear flush_color */
1919 cwq->flush_color = -1;
1920
1921 /*
1922 * If this was the last cwq, wake up the first flusher. It
1923 * will handle the rest.
1924 */
1925 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
1926 complete(&cwq->wq->first_flusher->done);
1927}
1928
1929/**
Tejun Heoa62428c2010-06-29 10:07:10 +02001930 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02001931 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02001932 * @work: work to process
1933 *
1934 * Process @work. This function contains all the logics necessary to
1935 * process a single work including synchronization against and
1936 * interaction with other workers on the same cpu, queueing and
1937 * flushing. As long as context requirement is met, any worker can
1938 * call this function to process a work.
1939 *
1940 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001941 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02001942 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001943static void process_one_work(struct worker *worker, struct work_struct *work)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001944__releases(&gcwq->lock)
1945__acquires(&gcwq->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02001946{
Tejun Heo7e116292010-06-29 10:07:13 +02001947 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001948 struct worker_pool *pool = worker->pool;
1949 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001950 struct hlist_head *bwh = busy_worker_head(gcwq, work);
Tejun Heofb0e7be2010-06-29 10:07:15 +02001951 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heoa62428c2010-06-29 10:07:10 +02001952 work_func_t f = work->func;
Tejun Heo73f53c42010-06-29 10:07:11 +02001953 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02001954 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02001955#ifdef CONFIG_LOCKDEP
1956 /*
1957 * It is permissible to free the struct work_struct from
1958 * inside the function that is called from it, this we need to
1959 * take into account for lockdep too. To avoid bogus "held
1960 * lock freed" warnings as well as problems when looking into
1961 * work->lockdep_map, make a copy and use that here.
1962 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07001963 struct lockdep_map lockdep_map;
1964
1965 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02001966#endif
Tejun Heo25511a42012-07-17 12:39:27 -07001967 WARN_ON_ONCE(!(worker->flags & (WORKER_UNBOUND | WORKER_REBIND)) &&
1968 raw_smp_processor_id() != gcwq->cpu);
1969
Tejun Heo7e116292010-06-29 10:07:13 +02001970 /*
1971 * A single work shouldn't be executed concurrently by
1972 * multiple workers on a single cpu. Check whether anyone is
1973 * already processing the work. If so, defer the work to the
1974 * currently executing one.
1975 */
1976 collision = __find_worker_executing_work(gcwq, bwh, work);
1977 if (unlikely(collision)) {
1978 move_linked_works(work, &collision->scheduled, NULL);
1979 return;
1980 }
1981
Tejun Heoa62428c2010-06-29 10:07:10 +02001982 /* claim and process */
Tejun Heoa62428c2010-06-29 10:07:10 +02001983 debug_work_deactivate(work);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001984 hlist_add_head(&worker->hentry, bwh);
Tejun Heoc34056a2010-06-29 10:07:11 +02001985 worker->current_work = work;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001986 worker->current_cwq = cwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02001987 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02001988
Tejun Heo7a22ad72010-06-29 10:07:13 +02001989 /* record the current cpu number in the work data and dequeue */
1990 set_work_cpu(work, gcwq->cpu);
Tejun Heoa62428c2010-06-29 10:07:10 +02001991 list_del_init(&work->entry);
1992
Tejun Heo649027d2010-06-29 10:07:14 +02001993 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02001994 * CPU intensive works don't participate in concurrency
1995 * management. They're the scheduler's responsibility.
1996 */
1997 if (unlikely(cpu_intensive))
1998 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
1999
Tejun Heo974271c2012-07-12 14:46:37 -07002000 /*
2001 * Unbound gcwq isn't concurrency managed and work items should be
2002 * executed ASAP. Wake up another worker if necessary.
2003 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002004 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
2005 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07002006
Tejun Heo8b03ae32010-06-29 10:07:12 +02002007 spin_unlock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002008
Tejun Heoa62428c2010-06-29 10:07:10 +02002009 work_clear_pending(work);
Tejun Heoe1594892011-01-09 23:32:15 +01002010 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002011 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002012 trace_workqueue_execute_start(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002013 f(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002014 /*
2015 * While we must be careful to not use "work" after this, the trace
2016 * point will only record its address.
2017 */
2018 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002019 lock_map_release(&lockdep_map);
2020 lock_map_release(&cwq->wq->lockdep_map);
2021
2022 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
2023 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
2024 "%s/0x%08x/%d\n",
2025 current->comm, preempt_count(), task_pid_nr(current));
2026 printk(KERN_ERR " last function: ");
2027 print_symbol("%s\n", (unsigned long)f);
2028 debug_show_held_locks(current);
2029 dump_stack();
2030 }
2031
Tejun Heo8b03ae32010-06-29 10:07:12 +02002032 spin_lock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002033
Tejun Heofb0e7be2010-06-29 10:07:15 +02002034 /* clear cpu intensive status */
2035 if (unlikely(cpu_intensive))
2036 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2037
Tejun Heoa62428c2010-06-29 10:07:10 +02002038 /* we're done with it, release */
Tejun Heoc8e55f32010-06-29 10:07:12 +02002039 hlist_del_init(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002040 worker->current_work = NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02002041 worker->current_cwq = NULL;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02002042 cwq_dec_nr_in_flight(cwq, work_color, false);
Tejun Heoa62428c2010-06-29 10:07:10 +02002043}
2044
Tejun Heoaffee4b2010-06-29 10:07:12 +02002045/**
2046 * process_scheduled_works - process scheduled works
2047 * @worker: self
2048 *
2049 * Process all scheduled works. Please note that the scheduled list
2050 * may change while processing a work, so this function repeatedly
2051 * fetches a work from the top and executes it.
2052 *
2053 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002054 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02002055 * multiple times.
2056 */
2057static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002059 while (!list_empty(&worker->scheduled)) {
2060 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002062 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064}
2065
Tejun Heo4690c4a2010-06-29 10:07:10 +02002066/**
2067 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02002068 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02002069 *
Tejun Heoe22bee72010-06-29 10:07:14 +02002070 * The gcwq worker thread function. There's a single dynamic pool of
2071 * these per each cpu. These workers process all works regardless of
2072 * their specific target workqueue. The only exception is works which
2073 * belong to workqueues with a rescuer which will be explained in
2074 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02002075 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002076static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077{
Tejun Heoc34056a2010-06-29 10:07:11 +02002078 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07002079 struct worker_pool *pool = worker->pool;
2080 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Tejun Heoe22bee72010-06-29 10:07:14 +02002082 /* tell the scheduler that this is a workqueue worker */
2083 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002084woke_up:
Tejun Heoc8e55f32010-06-29 10:07:12 +02002085 spin_lock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Tejun Heo25511a42012-07-17 12:39:27 -07002087 /*
2088 * DIE can be set only while idle and REBIND set while busy has
2089 * @worker->rebind_work scheduled. Checking here is enough.
2090 */
2091 if (unlikely(worker->flags & (WORKER_REBIND | WORKER_DIE))) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02002092 spin_unlock_irq(&gcwq->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07002093
2094 if (worker->flags & WORKER_DIE) {
2095 worker->task->flags &= ~PF_WQ_WORKER;
2096 return 0;
2097 }
2098
2099 idle_worker_rebind(worker);
2100 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 }
2102
Tejun Heoc8e55f32010-06-29 10:07:12 +02002103 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002104recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002105 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002106 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002107 goto sleep;
2108
2109 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002110 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002111 goto recheck;
2112
Tejun Heoc8e55f32010-06-29 10:07:12 +02002113 /*
2114 * ->scheduled list can only be filled while a worker is
2115 * preparing to process a work or actually processing it.
2116 * Make sure nobody diddled with it while I was sleeping.
2117 */
2118 BUG_ON(!list_empty(&worker->scheduled));
2119
Tejun Heoe22bee72010-06-29 10:07:14 +02002120 /*
2121 * When control reaches this point, we're guaranteed to have
2122 * at least one idle worker or that someone else has already
2123 * assumed the manager role.
2124 */
2125 worker_clr_flags(worker, WORKER_PREP);
2126
2127 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002128 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002129 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002130 struct work_struct, entry);
2131
2132 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2133 /* optimization path, not strictly necessary */
2134 process_one_work(worker, work);
2135 if (unlikely(!list_empty(&worker->scheduled)))
2136 process_scheduled_works(worker);
2137 } else {
2138 move_linked_works(work, &worker->scheduled, NULL);
2139 process_scheduled_works(worker);
2140 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002141 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002142
Tejun Heoe22bee72010-06-29 10:07:14 +02002143 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002144sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07002145 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002146 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002147
Tejun Heoc8e55f32010-06-29 10:07:12 +02002148 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02002149 * gcwq->lock is held and there's no work to process and no
2150 * need to manage, sleep. Workers are woken up only while
2151 * holding gcwq->lock or from local cpu, so setting the
2152 * current state before releasing gcwq->lock is enough to
2153 * prevent losing any event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002154 */
2155 worker_enter_idle(worker);
2156 __set_current_state(TASK_INTERRUPTIBLE);
2157 spin_unlock_irq(&gcwq->lock);
2158 schedule();
2159 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160}
2161
Tejun Heoe22bee72010-06-29 10:07:14 +02002162/**
2163 * rescuer_thread - the rescuer thread function
2164 * @__wq: the associated workqueue
2165 *
2166 * Workqueue rescuer thread function. There's one rescuer for each
2167 * workqueue which has WQ_RESCUER set.
2168 *
2169 * Regular work processing on a gcwq may block trying to create a new
2170 * worker which uses GFP_KERNEL allocation which has slight chance of
2171 * developing into deadlock if some works currently on the same queue
2172 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2173 * the problem rescuer solves.
2174 *
2175 * When such condition is possible, the gcwq summons rescuers of all
2176 * workqueues which have works queued on the gcwq and let them process
2177 * those works so that forward progress can be guaranteed.
2178 *
2179 * This should happen rarely.
2180 */
2181static int rescuer_thread(void *__wq)
2182{
2183 struct workqueue_struct *wq = __wq;
2184 struct worker *rescuer = wq->rescuer;
2185 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heof3421792010-07-02 10:03:51 +02002186 bool is_unbound = wq->flags & WQ_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02002187 unsigned int cpu;
2188
2189 set_user_nice(current, RESCUER_NICE_LEVEL);
2190repeat:
2191 set_current_state(TASK_INTERRUPTIBLE);
2192
2193 if (kthread_should_stop())
2194 return 0;
2195
Tejun Heof3421792010-07-02 10:03:51 +02002196 /*
2197 * See whether any cpu is asking for help. Unbounded
2198 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
2199 */
Tejun Heof2e005a2010-07-20 15:59:09 +02002200 for_each_mayday_cpu(cpu, wq->mayday_mask) {
Tejun Heof3421792010-07-02 10:03:51 +02002201 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2202 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002203 struct worker_pool *pool = cwq->pool;
2204 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002205 struct work_struct *work, *n;
2206
2207 __set_current_state(TASK_RUNNING);
Tejun Heof2e005a2010-07-20 15:59:09 +02002208 mayday_clear_cpu(cpu, wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002209
2210 /* migrate to the target cpu if possible */
Tejun Heobd7bdd42012-07-12 14:46:37 -07002211 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002212 worker_maybe_bind_and_lock(rescuer);
2213
2214 /*
2215 * Slurp in all works issued via this workqueue and
2216 * process'em.
2217 */
2218 BUG_ON(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002219 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02002220 if (get_work_cwq(work) == cwq)
2221 move_linked_works(work, scheduled, &n);
2222
2223 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002224
2225 /*
2226 * Leave this gcwq. If keep_working() is %true, notify a
2227 * regular worker; otherwise, we end up with 0 concurrency
2228 * and stalling the execution.
2229 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002230 if (keep_working(pool))
2231 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002232
Tejun Heoe22bee72010-06-29 10:07:14 +02002233 spin_unlock_irq(&gcwq->lock);
2234 }
2235
2236 schedule();
2237 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238}
2239
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002240struct wq_barrier {
2241 struct work_struct work;
2242 struct completion done;
2243};
2244
2245static void wq_barrier_func(struct work_struct *work)
2246{
2247 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2248 complete(&barr->done);
2249}
2250
Tejun Heo4690c4a2010-06-29 10:07:10 +02002251/**
2252 * insert_wq_barrier - insert a barrier work
2253 * @cwq: cwq to insert barrier into
2254 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002255 * @target: target work to attach @barr to
2256 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002257 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002258 * @barr is linked to @target such that @barr is completed only after
2259 * @target finishes execution. Please note that the ordering
2260 * guarantee is observed only with respect to @target and on the local
2261 * cpu.
2262 *
2263 * Currently, a queued barrier can't be canceled. This is because
2264 * try_to_grab_pending() can't determine whether the work to be
2265 * grabbed is at the head of the queue and thus can't clear LINKED
2266 * flag of the previous work while there must be a valid next work
2267 * after a work with LINKED flag set.
2268 *
2269 * Note that when @worker is non-NULL, @target may be modified
2270 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002271 *
2272 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002273 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002274 */
Oleg Nesterov83c22522007-05-09 02:33:54 -07002275static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002276 struct wq_barrier *barr,
2277 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002278{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002279 struct list_head *head;
2280 unsigned int linked = 0;
2281
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002282 /*
Tejun Heo8b03ae32010-06-29 10:07:12 +02002283 * debugobject calls are safe here even with gcwq->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002284 * as we know for sure that this will not trigger any of the
2285 * checks and call back into the fixup functions where we
2286 * might deadlock.
2287 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002288 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002289 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002290 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002291
Tejun Heoaffee4b2010-06-29 10:07:12 +02002292 /*
2293 * If @target is currently being executed, schedule the
2294 * barrier to the worker; otherwise, put it after @target.
2295 */
2296 if (worker)
2297 head = worker->scheduled.next;
2298 else {
2299 unsigned long *bits = work_data_bits(target);
2300
2301 head = target->entry.next;
2302 /* there can already be other linked works, inherit and set */
2303 linked = *bits & WORK_STRUCT_LINKED;
2304 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2305 }
2306
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002307 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002308 insert_work(cwq, &barr->work, head,
2309 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002310}
2311
Tejun Heo73f53c42010-06-29 10:07:11 +02002312/**
2313 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2314 * @wq: workqueue being flushed
2315 * @flush_color: new flush color, < 0 for no-op
2316 * @work_color: new work color, < 0 for no-op
2317 *
2318 * Prepare cwqs for workqueue flushing.
2319 *
2320 * If @flush_color is non-negative, flush_color on all cwqs should be
2321 * -1. If no cwq has in-flight commands at the specified color, all
2322 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2323 * has in flight commands, its cwq->flush_color is set to
2324 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2325 * wakeup logic is armed and %true is returned.
2326 *
2327 * The caller should have initialized @wq->first_flusher prior to
2328 * calling this function with non-negative @flush_color. If
2329 * @flush_color is negative, no flush color update is done and %false
2330 * is returned.
2331 *
2332 * If @work_color is non-negative, all cwqs should have the same
2333 * work_color which is previous to @work_color and all will be
2334 * advanced to @work_color.
2335 *
2336 * CONTEXT:
2337 * mutex_lock(wq->flush_mutex).
2338 *
2339 * RETURNS:
2340 * %true if @flush_color >= 0 and there's something to flush. %false
2341 * otherwise.
2342 */
2343static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2344 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345{
Tejun Heo73f53c42010-06-29 10:07:11 +02002346 bool wait = false;
2347 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -07002348
Tejun Heo73f53c42010-06-29 10:07:11 +02002349 if (flush_color >= 0) {
2350 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2351 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002352 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002353
Tejun Heof3421792010-07-02 10:03:51 +02002354 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002355 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002356 struct global_cwq *gcwq = cwq->pool->gcwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002357
Tejun Heo8b03ae32010-06-29 10:07:12 +02002358 spin_lock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002359
2360 if (flush_color >= 0) {
2361 BUG_ON(cwq->flush_color != -1);
2362
2363 if (cwq->nr_in_flight[flush_color]) {
2364 cwq->flush_color = flush_color;
2365 atomic_inc(&wq->nr_cwqs_to_flush);
2366 wait = true;
2367 }
2368 }
2369
2370 if (work_color >= 0) {
2371 BUG_ON(work_color != work_next_color(cwq->work_color));
2372 cwq->work_color = work_color;
2373 }
2374
Tejun Heo8b03ae32010-06-29 10:07:12 +02002375 spin_unlock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002376 }
2377
2378 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2379 complete(&wq->first_flusher->done);
2380
2381 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382}
2383
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002384/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002386 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 *
2388 * Forces execution of the workqueue and blocks until its completion.
2389 * This is typically used in driver shutdown handlers.
2390 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002391 * We sleep until all works which were queued on entry have been handled,
2392 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002394void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395{
Tejun Heo73f53c42010-06-29 10:07:11 +02002396 struct wq_flusher this_flusher = {
2397 .list = LIST_HEAD_INIT(this_flusher.list),
2398 .flush_color = -1,
2399 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2400 };
2401 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002402
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002403 lock_map_acquire(&wq->lockdep_map);
2404 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002405
2406 mutex_lock(&wq->flush_mutex);
2407
2408 /*
2409 * Start-to-wait phase
2410 */
2411 next_color = work_next_color(wq->work_color);
2412
2413 if (next_color != wq->flush_color) {
2414 /*
2415 * Color space is not full. The current work_color
2416 * becomes our flush_color and work_color is advanced
2417 * by one.
2418 */
2419 BUG_ON(!list_empty(&wq->flusher_overflow));
2420 this_flusher.flush_color = wq->work_color;
2421 wq->work_color = next_color;
2422
2423 if (!wq->first_flusher) {
2424 /* no flush in progress, become the first flusher */
2425 BUG_ON(wq->flush_color != this_flusher.flush_color);
2426
2427 wq->first_flusher = &this_flusher;
2428
2429 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2430 wq->work_color)) {
2431 /* nothing to flush, done */
2432 wq->flush_color = next_color;
2433 wq->first_flusher = NULL;
2434 goto out_unlock;
2435 }
2436 } else {
2437 /* wait in queue */
2438 BUG_ON(wq->flush_color == this_flusher.flush_color);
2439 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2440 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2441 }
2442 } else {
2443 /*
2444 * Oops, color space is full, wait on overflow queue.
2445 * The next flush completion will assign us
2446 * flush_color and transfer to flusher_queue.
2447 */
2448 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2449 }
2450
2451 mutex_unlock(&wq->flush_mutex);
2452
2453 wait_for_completion(&this_flusher.done);
2454
2455 /*
2456 * Wake-up-and-cascade phase
2457 *
2458 * First flushers are responsible for cascading flushes and
2459 * handling overflow. Non-first flushers can simply return.
2460 */
2461 if (wq->first_flusher != &this_flusher)
2462 return;
2463
2464 mutex_lock(&wq->flush_mutex);
2465
Tejun Heo4ce48b32010-07-02 10:03:51 +02002466 /* we might have raced, check again with mutex held */
2467 if (wq->first_flusher != &this_flusher)
2468 goto out_unlock;
2469
Tejun Heo73f53c42010-06-29 10:07:11 +02002470 wq->first_flusher = NULL;
2471
2472 BUG_ON(!list_empty(&this_flusher.list));
2473 BUG_ON(wq->flush_color != this_flusher.flush_color);
2474
2475 while (true) {
2476 struct wq_flusher *next, *tmp;
2477
2478 /* complete all the flushers sharing the current flush color */
2479 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2480 if (next->flush_color != wq->flush_color)
2481 break;
2482 list_del_init(&next->list);
2483 complete(&next->done);
2484 }
2485
2486 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2487 wq->flush_color != work_next_color(wq->work_color));
2488
2489 /* this flush_color is finished, advance by one */
2490 wq->flush_color = work_next_color(wq->flush_color);
2491
2492 /* one color has been freed, handle overflow queue */
2493 if (!list_empty(&wq->flusher_overflow)) {
2494 /*
2495 * Assign the same color to all overflowed
2496 * flushers, advance work_color and append to
2497 * flusher_queue. This is the start-to-wait
2498 * phase for these overflowed flushers.
2499 */
2500 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2501 tmp->flush_color = wq->work_color;
2502
2503 wq->work_color = work_next_color(wq->work_color);
2504
2505 list_splice_tail_init(&wq->flusher_overflow,
2506 &wq->flusher_queue);
2507 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2508 }
2509
2510 if (list_empty(&wq->flusher_queue)) {
2511 BUG_ON(wq->flush_color != wq->work_color);
2512 break;
2513 }
2514
2515 /*
2516 * Need to flush more colors. Make the next flusher
2517 * the new first flusher and arm cwqs.
2518 */
2519 BUG_ON(wq->flush_color == wq->work_color);
2520 BUG_ON(wq->flush_color != next->flush_color);
2521
2522 list_del_init(&next->list);
2523 wq->first_flusher = next;
2524
2525 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2526 break;
2527
2528 /*
2529 * Meh... this color is already done, clear first
2530 * flusher and repeat cascading.
2531 */
2532 wq->first_flusher = NULL;
2533 }
2534
2535out_unlock:
2536 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537}
Dave Jonesae90dd52006-06-30 01:40:45 -04002538EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002540/**
2541 * drain_workqueue - drain a workqueue
2542 * @wq: workqueue to drain
2543 *
2544 * Wait until the workqueue becomes empty. While draining is in progress,
2545 * only chain queueing is allowed. IOW, only currently pending or running
2546 * work items on @wq can queue further work items on it. @wq is flushed
2547 * repeatedly until it becomes empty. The number of flushing is detemined
2548 * by the depth of chaining and should be relatively short. Whine if it
2549 * takes too long.
2550 */
2551void drain_workqueue(struct workqueue_struct *wq)
2552{
2553 unsigned int flush_cnt = 0;
2554 unsigned int cpu;
2555
2556 /*
2557 * __queue_work() needs to test whether there are drainers, is much
2558 * hotter than drain_workqueue() and already looks at @wq->flags.
2559 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2560 */
2561 spin_lock(&workqueue_lock);
2562 if (!wq->nr_drainers++)
2563 wq->flags |= WQ_DRAINING;
2564 spin_unlock(&workqueue_lock);
2565reflush:
2566 flush_workqueue(wq);
2567
2568 for_each_cwq_cpu(cpu, wq) {
2569 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002570 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002571
Tejun Heobd7bdd42012-07-12 14:46:37 -07002572 spin_lock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002573 drained = !cwq->nr_active && list_empty(&cwq->delayed_works);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002574 spin_unlock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002575
2576 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002577 continue;
2578
2579 if (++flush_cnt == 10 ||
2580 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
2581 pr_warning("workqueue %s: flush on destruction isn't complete after %u tries\n",
2582 wq->name, flush_cnt);
2583 goto reflush;
2584 }
2585
2586 spin_lock(&workqueue_lock);
2587 if (!--wq->nr_drainers)
2588 wq->flags &= ~WQ_DRAINING;
2589 spin_unlock(&workqueue_lock);
2590}
2591EXPORT_SYMBOL_GPL(drain_workqueue);
2592
Tejun Heobaf59022010-09-16 10:42:16 +02002593static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
2594 bool wait_executing)
2595{
2596 struct worker *worker = NULL;
2597 struct global_cwq *gcwq;
2598 struct cpu_workqueue_struct *cwq;
2599
2600 might_sleep();
2601 gcwq = get_work_gcwq(work);
2602 if (!gcwq)
2603 return false;
2604
2605 spin_lock_irq(&gcwq->lock);
2606 if (!list_empty(&work->entry)) {
2607 /*
2608 * See the comment near try_to_grab_pending()->smp_rmb().
2609 * If it was re-queued to a different gcwq under us, we
2610 * are not going to wait.
2611 */
2612 smp_rmb();
2613 cwq = get_work_cwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002614 if (unlikely(!cwq || gcwq != cwq->pool->gcwq))
Tejun Heobaf59022010-09-16 10:42:16 +02002615 goto already_gone;
2616 } else if (wait_executing) {
2617 worker = find_worker_executing_work(gcwq, work);
2618 if (!worker)
2619 goto already_gone;
2620 cwq = worker->current_cwq;
2621 } else
2622 goto already_gone;
2623
2624 insert_wq_barrier(cwq, barr, work, worker);
2625 spin_unlock_irq(&gcwq->lock);
2626
Tejun Heoe1594892011-01-09 23:32:15 +01002627 /*
2628 * If @max_active is 1 or rescuer is in use, flushing another work
2629 * item on the same workqueue may lead to deadlock. Make sure the
2630 * flusher is not running on the same workqueue by verifying write
2631 * access.
2632 */
2633 if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER)
2634 lock_map_acquire(&cwq->wq->lockdep_map);
2635 else
2636 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heobaf59022010-09-16 10:42:16 +02002637 lock_map_release(&cwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002638
Tejun Heobaf59022010-09-16 10:42:16 +02002639 return true;
2640already_gone:
2641 spin_unlock_irq(&gcwq->lock);
2642 return false;
2643}
2644
Oleg Nesterovdb700892008-07-25 01:47:49 -07002645/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002646 * flush_work - wait for a work to finish executing the last queueing instance
2647 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002648 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002649 * Wait until @work has finished execution. This function considers
2650 * only the last queueing instance of @work. If @work has been
2651 * enqueued across different CPUs on a non-reentrant workqueue or on
2652 * multiple workqueues, @work might still be executing on return on
2653 * some of the CPUs from earlier queueing.
Oleg Nesterova67da702008-07-25 01:47:52 -07002654 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002655 * If @work was queued only on a non-reentrant, ordered or unbound
2656 * workqueue, @work is guaranteed to be idle on return if it hasn't
2657 * been requeued since flush started.
2658 *
2659 * RETURNS:
2660 * %true if flush_work() waited for the work to finish execution,
2661 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002662 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002663bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002664{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002665 struct wq_barrier barr;
2666
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002667 lock_map_acquire(&work->lockdep_map);
2668 lock_map_release(&work->lockdep_map);
2669
Tejun Heobaf59022010-09-16 10:42:16 +02002670 if (start_flush_work(work, &barr, true)) {
2671 wait_for_completion(&barr.done);
2672 destroy_work_on_stack(&barr.work);
2673 return true;
2674 } else
2675 return false;
Oleg Nesterovdb700892008-07-25 01:47:49 -07002676}
2677EXPORT_SYMBOL_GPL(flush_work);
2678
Tejun Heo401a8d02010-09-16 10:36:00 +02002679static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work)
2680{
2681 struct wq_barrier barr;
2682 struct worker *worker;
2683
2684 spin_lock_irq(&gcwq->lock);
2685
2686 worker = find_worker_executing_work(gcwq, work);
2687 if (unlikely(worker))
2688 insert_wq_barrier(worker->current_cwq, &barr, work, worker);
2689
2690 spin_unlock_irq(&gcwq->lock);
2691
2692 if (unlikely(worker)) {
2693 wait_for_completion(&barr.done);
2694 destroy_work_on_stack(&barr.work);
2695 return true;
2696 } else
2697 return false;
2698}
2699
2700static bool wait_on_work(struct work_struct *work)
2701{
2702 bool ret = false;
2703 int cpu;
2704
2705 might_sleep();
2706
2707 lock_map_acquire(&work->lockdep_map);
2708 lock_map_release(&work->lockdep_map);
2709
2710 for_each_gcwq_cpu(cpu)
2711 ret |= wait_on_cpu_work(get_gcwq(cpu), work);
2712 return ret;
2713}
2714
Tejun Heo09383492010-09-16 10:48:29 +02002715/**
2716 * flush_work_sync - wait until a work has finished execution
2717 * @work: the work to flush
2718 *
2719 * Wait until @work has finished execution. On return, it's
2720 * guaranteed that all queueing instances of @work which happened
2721 * before this function is called are finished. In other words, if
2722 * @work hasn't been requeued since this function was called, @work is
2723 * guaranteed to be idle on return.
2724 *
2725 * RETURNS:
2726 * %true if flush_work_sync() waited for the work to finish execution,
2727 * %false if it was already idle.
2728 */
2729bool flush_work_sync(struct work_struct *work)
2730{
2731 struct wq_barrier barr;
2732 bool pending, waited;
2733
2734 /* we'll wait for executions separately, queue barr only if pending */
2735 pending = start_flush_work(work, &barr, false);
2736
2737 /* wait for executions to finish */
2738 waited = wait_on_work(work);
2739
2740 /* wait for the pending one */
2741 if (pending) {
2742 wait_for_completion(&barr.done);
2743 destroy_work_on_stack(&barr.work);
2744 }
2745
2746 return pending || waited;
2747}
2748EXPORT_SYMBOL_GPL(flush_work_sync);
2749
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002750/*
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002751 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002752 * so this work can't be re-armed in any way.
2753 */
2754static int try_to_grab_pending(struct work_struct *work)
2755{
Tejun Heo8b03ae32010-06-29 10:07:12 +02002756 struct global_cwq *gcwq;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002757 int ret = -1;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002758
Tejun Heo22df02b2010-06-29 10:07:10 +02002759 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002760 return 0;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002761
2762 /*
2763 * The queueing is in progress, or it is already queued. Try to
2764 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
2765 */
Tejun Heo7a22ad72010-06-29 10:07:13 +02002766 gcwq = get_work_gcwq(work);
2767 if (!gcwq)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002768 return ret;
2769
Tejun Heo8b03ae32010-06-29 10:07:12 +02002770 spin_lock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002771 if (!list_empty(&work->entry)) {
2772 /*
Tejun Heo7a22ad72010-06-29 10:07:13 +02002773 * This work is queued, but perhaps we locked the wrong gcwq.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002774 * In that case we must see the new value after rmb(), see
2775 * insert_work()->wmb().
2776 */
2777 smp_rmb();
Tejun Heo7a22ad72010-06-29 10:07:13 +02002778 if (gcwq == get_work_gcwq(work)) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002779 debug_work_deactivate(work);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002780 list_del_init(&work->entry);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002781 cwq_dec_nr_in_flight(get_work_cwq(work),
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02002782 get_work_color(work),
2783 *work_data_bits(work) & WORK_STRUCT_DELAYED);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002784 ret = 1;
2785 }
2786 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02002787 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002788
2789 return ret;
2790}
2791
Tejun Heo401a8d02010-09-16 10:36:00 +02002792static bool __cancel_work_timer(struct work_struct *work,
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002793 struct timer_list* timer)
2794{
2795 int ret;
2796
2797 do {
2798 ret = (timer && likely(del_timer(timer)));
2799 if (!ret)
2800 ret = try_to_grab_pending(work);
2801 wait_on_work(work);
2802 } while (unlikely(ret < 0));
2803
Tejun Heo7a22ad72010-06-29 10:07:13 +02002804 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002805 return ret;
2806}
2807
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002808/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002809 * cancel_work_sync - cancel a work and wait for it to finish
2810 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002811 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002812 * Cancel @work and wait for its execution to finish. This function
2813 * can be used even if the work re-queues itself or migrates to
2814 * another workqueue. On return from this function, @work is
2815 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002816 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002817 * cancel_work_sync(&delayed_work->work) must not be used for
2818 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002819 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002820 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002821 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002822 *
2823 * RETURNS:
2824 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002825 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002826bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002827{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002828 return __cancel_work_timer(work, NULL);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002829}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002830EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002831
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002832/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002833 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2834 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002835 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002836 * Delayed timer is cancelled and the pending work is queued for
2837 * immediate execution. Like flush_work(), this function only
2838 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002839 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002840 * RETURNS:
2841 * %true if flush_work() waited for the work to finish execution,
2842 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002843 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002844bool flush_delayed_work(struct delayed_work *dwork)
2845{
2846 if (del_timer_sync(&dwork->timer))
2847 __queue_work(raw_smp_processor_id(),
2848 get_work_cwq(&dwork->work)->wq, &dwork->work);
2849 return flush_work(&dwork->work);
2850}
2851EXPORT_SYMBOL(flush_delayed_work);
2852
2853/**
Tejun Heo09383492010-09-16 10:48:29 +02002854 * flush_delayed_work_sync - wait for a dwork to finish
2855 * @dwork: the delayed work to flush
2856 *
2857 * Delayed timer is cancelled and the pending work is queued for
2858 * execution immediately. Other than timer handling, its behavior
2859 * is identical to flush_work_sync().
2860 *
2861 * RETURNS:
2862 * %true if flush_work_sync() waited for the work to finish execution,
2863 * %false if it was already idle.
2864 */
2865bool flush_delayed_work_sync(struct delayed_work *dwork)
2866{
2867 if (del_timer_sync(&dwork->timer))
2868 __queue_work(raw_smp_processor_id(),
2869 get_work_cwq(&dwork->work)->wq, &dwork->work);
2870 return flush_work_sync(&dwork->work);
2871}
2872EXPORT_SYMBOL(flush_delayed_work_sync);
2873
2874/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002875 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2876 * @dwork: the delayed work cancel
2877 *
2878 * This is cancel_work_sync() for delayed works.
2879 *
2880 * RETURNS:
2881 * %true if @dwork was pending, %false otherwise.
2882 */
2883bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002884{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002885 return __cancel_work_timer(&dwork->work, &dwork->timer);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002886}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002887EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002889/**
2890 * schedule_work - put work task in global workqueue
2891 * @work: job to be done
2892 *
Bart Van Assche5b0f437d2009-07-30 19:00:53 +02002893 * Returns zero if @work was already on the kernel-global workqueue and
2894 * non-zero otherwise.
2895 *
2896 * This puts a job in the kernel-global workqueue if it was not already
2897 * queued and leaves it in the same position on the kernel-global
2898 * workqueue otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002899 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002900int schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901{
Tejun Heod320c032010-06-29 10:07:14 +02002902 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903}
Dave Jonesae90dd52006-06-30 01:40:45 -04002904EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
Zhang Ruic1a220e2008-07-23 21:28:39 -07002906/*
2907 * schedule_work_on - put work task on a specific cpu
2908 * @cpu: cpu to put the work task on
2909 * @work: job to be done
2910 *
2911 * This puts a job on a specific cpu
2912 */
2913int schedule_work_on(int cpu, struct work_struct *work)
2914{
Tejun Heod320c032010-06-29 10:07:14 +02002915 return queue_work_on(cpu, system_wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07002916}
2917EXPORT_SYMBOL(schedule_work_on);
2918
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002919/**
2920 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +00002921 * @dwork: job to be done
2922 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002923 *
2924 * After waiting for a given time this puts a job in the kernel-global
2925 * workqueue.
2926 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002927int schedule_delayed_work(struct delayed_work *dwork,
Ingo Molnar82f67cd2007-02-16 01:28:13 -08002928 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929{
Tejun Heod320c032010-06-29 10:07:14 +02002930 return queue_delayed_work(system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931}
Dave Jonesae90dd52006-06-30 01:40:45 -04002932EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002934/**
2935 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
2936 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +00002937 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002938 * @delay: number of jiffies to wait
2939 *
2940 * After waiting for a given time this puts a job in the kernel-global
2941 * workqueue on the specified CPU.
2942 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +00002944 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945{
Tejun Heod320c032010-06-29 10:07:14 +02002946 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947}
Dave Jonesae90dd52006-06-30 01:40:45 -04002948EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949
Andrew Mortonb6136772006-06-25 05:47:49 -07002950/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002951 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002952 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002953 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002954 * schedule_on_each_cpu() executes @func on each online CPU using the
2955 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002956 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002957 *
2958 * RETURNS:
2959 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002960 */
David Howells65f27f32006-11-22 14:55:48 +00002961int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002962{
2963 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002964 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002965
Andrew Mortonb6136772006-06-25 05:47:49 -07002966 works = alloc_percpu(struct work_struct);
2967 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002968 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002969
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002970 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002971
Christoph Lameter15316ba2006-01-08 01:00:43 -08002972 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002973 struct work_struct *work = per_cpu_ptr(works, cpu);
2974
2975 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002976 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002977 }
Tejun Heo93981802009-11-17 14:06:20 -08002978
2979 for_each_online_cpu(cpu)
2980 flush_work(per_cpu_ptr(works, cpu));
2981
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002982 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002983 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002984 return 0;
2985}
2986
Alan Sterneef6a7d2010-02-12 17:39:21 +09002987/**
2988 * flush_scheduled_work - ensure that any scheduled work has run to completion.
2989 *
2990 * Forces execution of the kernel-global workqueue and blocks until its
2991 * completion.
2992 *
2993 * Think twice before calling this function! It's very easy to get into
2994 * trouble if you don't take great care. Either of the following situations
2995 * will lead to deadlock:
2996 *
2997 * One of the work items currently on the workqueue needs to acquire
2998 * a lock held by your code or its caller.
2999 *
3000 * Your code is running in the context of a work routine.
3001 *
3002 * They will be detected by lockdep when they occur, but the first might not
3003 * occur very often. It depends on what work items are on the workqueue and
3004 * what locks they need, which you have no control over.
3005 *
3006 * In most situations flushing the entire workqueue is overkill; you merely
3007 * need to know that a particular work item isn't queued and isn't running.
3008 * In such cases you should use cancel_delayed_work_sync() or
3009 * cancel_work_sync() instead.
3010 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011void flush_scheduled_work(void)
3012{
Tejun Heod320c032010-06-29 10:07:14 +02003013 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014}
Dave Jonesae90dd52006-06-30 01:40:45 -04003015EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
3017/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06003018 * execute_in_process_context - reliably execute the routine with user context
3019 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06003020 * @ew: guaranteed storage for the execute work structure (must
3021 * be available when the work executes)
3022 *
3023 * Executes the function immediately if process context is available,
3024 * otherwise schedules the function for delayed execution.
3025 *
3026 * Returns: 0 - function was executed
3027 * 1 - function was scheduled for execution
3028 */
David Howells65f27f32006-11-22 14:55:48 +00003029int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06003030{
3031 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00003032 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003033 return 0;
3034 }
3035
David Howells65f27f32006-11-22 14:55:48 +00003036 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003037 schedule_work(&ew->work);
3038
3039 return 1;
3040}
3041EXPORT_SYMBOL_GPL(execute_in_process_context);
3042
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043int keventd_up(void)
3044{
Tejun Heod320c032010-06-29 10:07:14 +02003045 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046}
3047
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003048static int alloc_cwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049{
Oleg Nesterov3af244332007-05-09 02:34:09 -07003050 /*
Tejun Heo0f900042010-06-29 10:07:11 +02003051 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
3052 * Make sure that the alignment isn't lower than that of
3053 * unsigned long long.
Oleg Nesterov3af244332007-05-09 02:34:09 -07003054 */
Tejun Heo0f900042010-06-29 10:07:11 +02003055 const size_t size = sizeof(struct cpu_workqueue_struct);
3056 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
3057 __alignof__(unsigned long long));
Oleg Nesterov3af244332007-05-09 02:34:09 -07003058
Lai Jiangshane06ffa12012-03-09 18:03:20 +08003059 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02003060 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
Tejun Heo931ac772010-07-20 11:07:48 +02003061 else {
Tejun Heof3421792010-07-02 10:03:51 +02003062 void *ptr;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01003063
Tejun Heof3421792010-07-02 10:03:51 +02003064 /*
3065 * Allocate enough room to align cwq and put an extra
3066 * pointer at the end pointing back to the originally
3067 * allocated pointer which will be used for free.
3068 */
3069 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
3070 if (ptr) {
3071 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
3072 *(void **)(wq->cpu_wq.single + 1) = ptr;
3073 }
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003074 }
Tejun Heof3421792010-07-02 10:03:51 +02003075
Tejun Heo0415b00d12011-03-24 18:50:09 +01003076 /* just in case, make sure it's actually aligned */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003077 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
3078 return wq->cpu_wq.v ? 0 : -ENOMEM;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003079}
3080
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003081static void free_cwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07003082{
Lai Jiangshane06ffa12012-03-09 18:03:20 +08003083 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02003084 free_percpu(wq->cpu_wq.pcpu);
3085 else if (wq->cpu_wq.single) {
3086 /* the pointer to free is stored right after the cwq */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003087 kfree(*(void **)(wq->cpu_wq.single + 1));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07003088 }
3089}
3090
Tejun Heof3421792010-07-02 10:03:51 +02003091static int wq_clamp_max_active(int max_active, unsigned int flags,
3092 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003093{
Tejun Heof3421792010-07-02 10:03:51 +02003094 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3095
3096 if (max_active < 1 || max_active > lim)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003097 printk(KERN_WARNING "workqueue: max_active %d requested for %s "
3098 "is out of range, clamping between %d and %d\n",
Tejun Heof3421792010-07-02 10:03:51 +02003099 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003100
Tejun Heof3421792010-07-02 10:03:51 +02003101 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003102}
3103
Tejun Heob196be82012-01-10 15:11:35 -08003104struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02003105 unsigned int flags,
3106 int max_active,
3107 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08003108 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003109{
Tejun Heob196be82012-01-10 15:11:35 -08003110 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003111 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02003112 unsigned int cpu;
Tejun Heob196be82012-01-10 15:11:35 -08003113 size_t namelen;
3114
3115 /* determine namelen, allocate wq and format name */
3116 va_start(args, lock_name);
3117 va_copy(args1, args);
3118 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3119
3120 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3121 if (!wq)
3122 goto err;
3123
3124 vsnprintf(wq->name, namelen, fmt, args1);
3125 va_end(args);
3126 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003127
Tejun Heof3421792010-07-02 10:03:51 +02003128 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02003129 * Workqueues which may be used during memory reclaim should
3130 * have a rescuer to guarantee forward progress.
3131 */
3132 if (flags & WQ_MEM_RECLAIM)
3133 flags |= WQ_RESCUER;
3134
Tejun Heod320c032010-06-29 10:07:14 +02003135 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003136 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003137
Tejun Heob196be82012-01-10 15:11:35 -08003138 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003139 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003140 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003141 mutex_init(&wq->flush_mutex);
3142 atomic_set(&wq->nr_cwqs_to_flush, 0);
3143 INIT_LIST_HEAD(&wq->flusher_queue);
3144 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003145
Johannes Bergeb13ba82008-01-16 09:51:58 +01003146 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003147 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003148
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003149 if (alloc_cwqs(wq) < 0)
3150 goto err;
3151
Tejun Heof3421792010-07-02 10:03:51 +02003152 for_each_cwq_cpu(cpu, wq) {
Tejun Heo15376632010-06-29 10:07:11 +02003153 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003154 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo32704762012-07-13 22:16:45 -07003155 int pool_idx = (bool)(flags & WQ_HIGHPRI);
Tejun Heo15376632010-06-29 10:07:11 +02003156
Tejun Heo0f900042010-06-29 10:07:11 +02003157 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heo32704762012-07-13 22:16:45 -07003158 cwq->pool = &gcwq->pools[pool_idx];
Tejun Heoc34056a2010-06-29 10:07:11 +02003159 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02003160 cwq->flush_color = -1;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003161 cwq->max_active = max_active;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003162 INIT_LIST_HEAD(&cwq->delayed_works);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003163 }
3164
Tejun Heoe22bee72010-06-29 10:07:14 +02003165 if (flags & WQ_RESCUER) {
3166 struct worker *rescuer;
3167
Tejun Heof2e005a2010-07-20 15:59:09 +02003168 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
Tejun Heoe22bee72010-06-29 10:07:14 +02003169 goto err;
3170
3171 wq->rescuer = rescuer = alloc_worker();
3172 if (!rescuer)
3173 goto err;
3174
Tejun Heob196be82012-01-10 15:11:35 -08003175 rescuer->task = kthread_create(rescuer_thread, wq, "%s",
3176 wq->name);
Tejun Heoe22bee72010-06-29 10:07:14 +02003177 if (IS_ERR(rescuer->task))
3178 goto err;
3179
Tejun Heoe22bee72010-06-29 10:07:14 +02003180 rescuer->task->flags |= PF_THREAD_BOUND;
3181 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003182 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003183
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003184 /*
3185 * workqueue_lock protects global freeze state and workqueues
3186 * list. Grab it, set max_active accordingly and add the new
3187 * workqueue to workqueues list.
3188 */
Tejun Heo15376632010-06-29 10:07:11 +02003189 spin_lock(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003190
Tejun Heo58a69cb2011-02-16 09:25:31 +01003191 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heof3421792010-07-02 10:03:51 +02003192 for_each_cwq_cpu(cpu, wq)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003193 get_cwq(cpu, wq)->max_active = 0;
3194
Tejun Heo15376632010-06-29 10:07:11 +02003195 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003196
Tejun Heo15376632010-06-29 10:07:11 +02003197 spin_unlock(&workqueue_lock);
3198
Oleg Nesterov3af244332007-05-09 02:34:09 -07003199 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02003200err:
3201 if (wq) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003202 free_cwqs(wq);
Tejun Heof2e005a2010-07-20 15:59:09 +02003203 free_mayday_mask(wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02003204 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003205 kfree(wq);
3206 }
3207 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003208}
Tejun Heod320c032010-06-29 10:07:14 +02003209EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003210
3211/**
3212 * destroy_workqueue - safely terminate a workqueue
3213 * @wq: target workqueue
3214 *
3215 * Safely destroy a workqueue. All work currently pending will be done first.
3216 */
3217void destroy_workqueue(struct workqueue_struct *wq)
3218{
Tejun Heoc8e55f32010-06-29 10:07:12 +02003219 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003220
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003221 /* drain it before proceeding with destruction */
3222 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003223
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003224 /*
3225 * wq list is used to freeze wq, remove from list after
3226 * flushing is complete in case freeze races us.
3227 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003228 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07003229 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003230 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003231
Tejun Heoe22bee72010-06-29 10:07:14 +02003232 /* sanity check */
Tejun Heof3421792010-07-02 10:03:51 +02003233 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02003234 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3235 int i;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003236
Tejun Heo73f53c42010-06-29 10:07:11 +02003237 for (i = 0; i < WORK_NR_COLORS; i++)
3238 BUG_ON(cwq->nr_in_flight[i]);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003239 BUG_ON(cwq->nr_active);
3240 BUG_ON(!list_empty(&cwq->delayed_works));
Tejun Heo73f53c42010-06-29 10:07:11 +02003241 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003242
Tejun Heoe22bee72010-06-29 10:07:14 +02003243 if (wq->flags & WQ_RESCUER) {
3244 kthread_stop(wq->rescuer->task);
Tejun Heof2e005a2010-07-20 15:59:09 +02003245 free_mayday_mask(wq->mayday_mask);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003246 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003247 }
3248
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003249 free_cwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003250 kfree(wq);
3251}
3252EXPORT_SYMBOL_GPL(destroy_workqueue);
3253
Tejun Heodcd989c2010-06-29 10:07:14 +02003254/**
3255 * workqueue_set_max_active - adjust max_active of a workqueue
3256 * @wq: target workqueue
3257 * @max_active: new max_active value.
3258 *
3259 * Set max_active of @wq to @max_active.
3260 *
3261 * CONTEXT:
3262 * Don't call from IRQ context.
3263 */
3264void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3265{
3266 unsigned int cpu;
3267
Tejun Heof3421792010-07-02 10:03:51 +02003268 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003269
3270 spin_lock(&workqueue_lock);
3271
3272 wq->saved_max_active = max_active;
3273
Tejun Heof3421792010-07-02 10:03:51 +02003274 for_each_cwq_cpu(cpu, wq) {
Tejun Heodcd989c2010-06-29 10:07:14 +02003275 struct global_cwq *gcwq = get_gcwq(cpu);
3276
3277 spin_lock_irq(&gcwq->lock);
3278
Tejun Heo58a69cb2011-02-16 09:25:31 +01003279 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heodcd989c2010-06-29 10:07:14 +02003280 !(gcwq->flags & GCWQ_FREEZING))
3281 get_cwq(gcwq->cpu, wq)->max_active = max_active;
3282
3283 spin_unlock_irq(&gcwq->lock);
3284 }
3285
3286 spin_unlock(&workqueue_lock);
3287}
3288EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3289
3290/**
3291 * workqueue_congested - test whether a workqueue is congested
3292 * @cpu: CPU in question
3293 * @wq: target workqueue
3294 *
3295 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3296 * no synchronization around this function and the test result is
3297 * unreliable and only useful as advisory hints or for debugging.
3298 *
3299 * RETURNS:
3300 * %true if congested, %false otherwise.
3301 */
3302bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3303{
3304 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3305
3306 return !list_empty(&cwq->delayed_works);
3307}
3308EXPORT_SYMBOL_GPL(workqueue_congested);
3309
3310/**
3311 * work_cpu - return the last known associated cpu for @work
3312 * @work: the work of interest
3313 *
3314 * RETURNS:
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003315 * CPU number if @work was ever queued. WORK_CPU_NONE otherwise.
Tejun Heodcd989c2010-06-29 10:07:14 +02003316 */
3317unsigned int work_cpu(struct work_struct *work)
3318{
3319 struct global_cwq *gcwq = get_work_gcwq(work);
3320
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003321 return gcwq ? gcwq->cpu : WORK_CPU_NONE;
Tejun Heodcd989c2010-06-29 10:07:14 +02003322}
3323EXPORT_SYMBOL_GPL(work_cpu);
3324
3325/**
3326 * work_busy - test whether a work is currently pending or running
3327 * @work: the work to be tested
3328 *
3329 * Test whether @work is currently pending or running. There is no
3330 * synchronization around this function and the test result is
3331 * unreliable and only useful as advisory hints or for debugging.
3332 * Especially for reentrant wqs, the pending state might hide the
3333 * running state.
3334 *
3335 * RETURNS:
3336 * OR'd bitmask of WORK_BUSY_* bits.
3337 */
3338unsigned int work_busy(struct work_struct *work)
3339{
3340 struct global_cwq *gcwq = get_work_gcwq(work);
3341 unsigned long flags;
3342 unsigned int ret = 0;
3343
3344 if (!gcwq)
3345 return false;
3346
3347 spin_lock_irqsave(&gcwq->lock, flags);
3348
3349 if (work_pending(work))
3350 ret |= WORK_BUSY_PENDING;
3351 if (find_worker_executing_work(gcwq, work))
3352 ret |= WORK_BUSY_RUNNING;
3353
3354 spin_unlock_irqrestore(&gcwq->lock, flags);
3355
3356 return ret;
3357}
3358EXPORT_SYMBOL_GPL(work_busy);
3359
Tejun Heodb7bccf2010-06-29 10:07:12 +02003360/*
3361 * CPU hotplug.
3362 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003363 * There are two challenges in supporting CPU hotplug. Firstly, there
3364 * are a lot of assumptions on strong associations among work, cwq and
3365 * gcwq which make migrating pending and scheduled works very
3366 * difficult to implement without impacting hot paths. Secondly,
3367 * gcwqs serve mix of short, long and very long running works making
3368 * blocked draining impractical.
3369 *
Tejun Heo403c8212012-07-17 12:39:27 -07003370 * This is solved by allowing a gcwq to be detached from CPU, running it
3371 * with unbound workers and allowing it to be reattached later if the cpu
3372 * comes back online. A separate thread is created to govern a gcwq in
3373 * such state and is called the trustee of the gcwq.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003374 *
3375 * Trustee states and their descriptions.
3376 *
3377 * START Command state used on startup. On CPU_DOWN_PREPARE, a
3378 * new trustee is started with this state.
3379 *
3380 * IN_CHARGE Once started, trustee will enter this state after
Tejun Heoe22bee72010-06-29 10:07:14 +02003381 * assuming the manager role and making all existing
3382 * workers rogue. DOWN_PREPARE waits for trustee to
3383 * enter this state. After reaching IN_CHARGE, trustee
3384 * tries to execute the pending worklist until it's empty
3385 * and the state is set to BUTCHER, or the state is set
3386 * to RELEASE.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003387 *
3388 * BUTCHER Command state which is set by the cpu callback after
3389 * the cpu has went down. Once this state is set trustee
3390 * knows that there will be no new works on the worklist
3391 * and once the worklist is empty it can proceed to
3392 * killing idle workers.
3393 *
3394 * RELEASE Command state which is set by the cpu callback if the
3395 * cpu down has been canceled or it has come online
3396 * again. After recognizing this state, trustee stops
Tejun Heoe22bee72010-06-29 10:07:14 +02003397 * trying to drain or butcher and clears ROGUE, rebinds
3398 * all remaining workers back to the cpu and releases
3399 * manager role.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003400 *
3401 * DONE Trustee will enter this state after BUTCHER or RELEASE
3402 * is complete.
3403 *
3404 * trustee CPU draining
3405 * took over down complete
3406 * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE
3407 * | | ^
3408 * | CPU is back online v return workers |
3409 * ----------------> RELEASE --------------
3410 */
3411
Tejun Heo60373152012-07-17 12:39:27 -07003412/* claim manager positions of all pools */
3413static void gcwq_claim_management(struct global_cwq *gcwq)
3414{
3415 struct worker_pool *pool;
3416
3417 for_each_worker_pool(pool, gcwq)
3418 mutex_lock_nested(&pool->manager_mutex, pool - gcwq->pools);
3419}
3420
3421/* release manager positions */
3422static void gcwq_release_management(struct global_cwq *gcwq)
3423{
3424 struct worker_pool *pool;
3425
3426 for_each_worker_pool(pool, gcwq)
3427 mutex_unlock(&pool->manager_mutex);
3428}
3429
Tejun Heodb7bccf2010-06-29 10:07:12 +02003430/**
3431 * trustee_wait_event_timeout - timed event wait for trustee
3432 * @cond: condition to wait for
3433 * @timeout: timeout in jiffies
3434 *
3435 * wait_event_timeout() for trustee to use. Handles locking and
3436 * checks for RELEASE request.
3437 *
3438 * CONTEXT:
3439 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3440 * multiple times. To be used by trustee.
3441 *
3442 * RETURNS:
3443 * Positive indicating left time if @cond is satisfied, 0 if timed
3444 * out, -1 if canceled.
3445 */
3446#define trustee_wait_event_timeout(cond, timeout) ({ \
3447 long __ret = (timeout); \
3448 while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) && \
3449 __ret) { \
3450 spin_unlock_irq(&gcwq->lock); \
3451 __wait_event_timeout(gcwq->trustee_wait, (cond) || \
3452 (gcwq->trustee_state == TRUSTEE_RELEASE), \
3453 __ret); \
3454 spin_lock_irq(&gcwq->lock); \
3455 } \
3456 gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret); \
3457})
3458
3459/**
3460 * trustee_wait_event - event wait for trustee
3461 * @cond: condition to wait for
3462 *
3463 * wait_event() for trustee to use. Automatically handles locking and
3464 * checks for CANCEL request.
3465 *
3466 * CONTEXT:
3467 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3468 * multiple times. To be used by trustee.
3469 *
3470 * RETURNS:
3471 * 0 if @cond is satisfied, -1 if canceled.
3472 */
3473#define trustee_wait_event(cond) ({ \
3474 long __ret1; \
3475 __ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\
3476 __ret1 < 0 ? -1 : 0; \
3477})
3478
3479static int __cpuinit trustee_thread(void *__gcwq)
3480{
3481 struct global_cwq *gcwq = __gcwq;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003482 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003483 struct worker *worker;
Tejun Heoe22bee72010-06-29 10:07:14 +02003484 struct work_struct *work;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003485 struct hlist_node *pos;
3486 int i;
3487
3488 BUG_ON(gcwq->cpu != smp_processor_id());
3489
Tejun Heo60373152012-07-17 12:39:27 -07003490 gcwq_claim_management(gcwq);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003491 spin_lock_irq(&gcwq->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02003492
Tejun Heof2d5a0e2012-07-17 12:39:26 -07003493 /*
3494 * We've claimed all manager positions. Make all workers unbound
3495 * and set DISASSOCIATED. Before this, all workers except for the
3496 * ones which are still executing works from before the last CPU
3497 * down must be on the cpu. After this, they may become diasporas.
3498 */
Tejun Heo60373152012-07-17 12:39:27 -07003499 for_each_worker_pool(pool, gcwq)
Tejun Heo4ce62e92012-07-13 22:16:44 -07003500 list_for_each_entry(worker, &pool->idle_list, entry)
Tejun Heo403c8212012-07-17 12:39:27 -07003501 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003502
3503 for_each_busy_worker(worker, i, pos, gcwq)
Tejun Heo403c8212012-07-17 12:39:27 -07003504 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003505
Tejun Heof2d5a0e2012-07-17 12:39:26 -07003506 gcwq->flags |= GCWQ_DISASSOCIATED;
3507
Tejun Heodb7bccf2010-06-29 10:07:12 +02003508 /*
Tejun Heo403c8212012-07-17 12:39:27 -07003509 * Call schedule() so that we cross rq->lock and thus can guarantee
3510 * sched callbacks see the unbound flag. This is necessary as
3511 * scheduler callbacks may be invoked from other cpus.
Tejun Heoe22bee72010-06-29 10:07:14 +02003512 */
3513 spin_unlock_irq(&gcwq->lock);
3514 schedule();
3515 spin_lock_irq(&gcwq->lock);
3516
3517 /*
Tejun Heocb444762010-07-02 10:03:50 +02003518 * Sched callbacks are disabled now. Zap nr_running. After
3519 * this, nr_running stays zero and need_more_worker() and
3520 * keep_working() are always true as long as the worklist is
3521 * not empty.
Tejun Heoe22bee72010-06-29 10:07:14 +02003522 */
Tejun Heo4ce62e92012-07-13 22:16:44 -07003523 for_each_worker_pool(pool, gcwq)
3524 atomic_set(get_pool_nr_running(pool), 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003525
3526 spin_unlock_irq(&gcwq->lock);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003527 for_each_worker_pool(pool, gcwq)
3528 del_timer_sync(&pool->idle_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003529 spin_lock_irq(&gcwq->lock);
3530
3531 /*
Tejun Heodb7bccf2010-06-29 10:07:12 +02003532 * We're now in charge. Notify and proceed to drain. We need
3533 * to keep the gcwq running during the whole CPU down
3534 * procedure as other cpu hotunplug callbacks may need to
3535 * flush currently running tasks.
3536 */
3537 gcwq->trustee_state = TRUSTEE_IN_CHARGE;
3538 wake_up_all(&gcwq->trustee_wait);
3539
3540 /*
3541 * The original cpu is in the process of dying and may go away
3542 * anytime now. When that happens, we and all workers would
Tejun Heoe22bee72010-06-29 10:07:14 +02003543 * be migrated to other cpus. Try draining any left work. We
3544 * want to get it over with ASAP - spam rescuers, wake up as
3545 * many idlers as necessary and create new ones till the
3546 * worklist is empty. Note that if the gcwq is frozen, there
Tejun Heo58a69cb2011-02-16 09:25:31 +01003547 * may be frozen works in freezable cwqs. Don't declare
Tejun Heoe22bee72010-06-29 10:07:14 +02003548 * completion while frozen.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003549 */
Tejun Heo4ce62e92012-07-13 22:16:44 -07003550 while (true) {
3551 bool busy = false;
Tejun Heoe22bee72010-06-29 10:07:14 +02003552
Tejun Heo4ce62e92012-07-13 22:16:44 -07003553 for_each_worker_pool(pool, gcwq)
3554 busy |= pool->nr_workers != pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +02003555
Tejun Heo4ce62e92012-07-13 22:16:44 -07003556 if (!busy && !(gcwq->flags & GCWQ_FREEZING) &&
3557 gcwq->trustee_state != TRUSTEE_IN_CHARGE)
3558 break;
Tejun Heoe22bee72010-06-29 10:07:14 +02003559
Tejun Heo4ce62e92012-07-13 22:16:44 -07003560 for_each_worker_pool(pool, gcwq) {
3561 int nr_works = 0;
3562
3563 list_for_each_entry(work, &pool->worklist, entry) {
3564 send_mayday(work);
3565 nr_works++;
3566 }
3567
3568 list_for_each_entry(worker, &pool->idle_list, entry) {
3569 if (!nr_works--)
3570 break;
3571 wake_up_process(worker->task);
3572 }
3573
3574 if (need_to_create_worker(pool)) {
3575 spin_unlock_irq(&gcwq->lock);
Tejun Heobc2ae0f2012-07-17 12:39:27 -07003576 worker = create_worker(pool);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003577 spin_lock_irq(&gcwq->lock);
Tejun Heobc2ae0f2012-07-17 12:39:27 -07003578 if (worker)
Tejun Heo4ce62e92012-07-13 22:16:44 -07003579 start_worker(worker);
Tejun Heoe22bee72010-06-29 10:07:14 +02003580 }
3581 }
3582
Tejun Heodb7bccf2010-06-29 10:07:12 +02003583 /* give a breather */
3584 if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN) < 0)
3585 break;
3586 }
3587
Tejun Heo60373152012-07-17 12:39:27 -07003588 gcwq_release_management(gcwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02003589
Tejun Heodb7bccf2010-06-29 10:07:12 +02003590 /* notify completion */
3591 gcwq->trustee = NULL;
3592 gcwq->trustee_state = TRUSTEE_DONE;
3593 wake_up_all(&gcwq->trustee_wait);
3594 spin_unlock_irq(&gcwq->lock);
3595 return 0;
3596}
3597
3598/**
3599 * wait_trustee_state - wait for trustee to enter the specified state
3600 * @gcwq: gcwq the trustee of interest belongs to
3601 * @state: target state to wait for
3602 *
3603 * Wait for the trustee to reach @state. DONE is already matched.
3604 *
3605 * CONTEXT:
3606 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3607 * multiple times. To be used by cpu_callback.
3608 */
3609static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09003610__releases(&gcwq->lock)
3611__acquires(&gcwq->lock)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003612{
3613 if (!(gcwq->trustee_state == state ||
3614 gcwq->trustee_state == TRUSTEE_DONE)) {
3615 spin_unlock_irq(&gcwq->lock);
3616 __wait_event(gcwq->trustee_wait,
3617 gcwq->trustee_state == state ||
3618 gcwq->trustee_state == TRUSTEE_DONE);
3619 spin_lock_irq(&gcwq->lock);
3620 }
3621}
3622
Oleg Nesterov3af244332007-05-09 02:34:09 -07003623static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
3624 unsigned long action,
3625 void *hcpu)
3626{
3627 unsigned int cpu = (unsigned long)hcpu;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003628 struct global_cwq *gcwq = get_gcwq(cpu);
3629 struct task_struct *new_trustee = NULL;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003630 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003631 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003633 action &= ~CPU_TASKS_FROZEN;
3634
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003636 case CPU_DOWN_PREPARE:
3637 new_trustee = kthread_create(trustee_thread, gcwq,
3638 "workqueue_trustee/%d\n", cpu);
3639 if (IS_ERR(new_trustee))
3640 return notifier_from_errno(PTR_ERR(new_trustee));
3641 kthread_bind(new_trustee, cpu);
Tejun Heo3ce63372012-07-17 12:39:27 -07003642 break;
3643
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 case CPU_UP_PREPARE:
Tejun Heo4ce62e92012-07-13 22:16:44 -07003645 for_each_worker_pool(pool, gcwq) {
Tejun Heo3ce63372012-07-17 12:39:27 -07003646 struct worker *worker;
3647
3648 if (pool->nr_workers)
3649 continue;
3650
3651 worker = create_worker(pool);
3652 if (!worker)
3653 return NOTIFY_BAD;
3654
3655 spin_lock_irq(&gcwq->lock);
3656 start_worker(worker);
3657 spin_unlock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 }
3660
Tejun Heodb7bccf2010-06-29 10:07:12 +02003661 /* some are called w/ irq disabled, don't disturb irq status */
3662 spin_lock_irqsave(&gcwq->lock, flags);
3663
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003664 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003665 case CPU_DOWN_PREPARE:
3666 /* initialize trustee and tell it to acquire the gcwq */
3667 BUG_ON(gcwq->trustee || gcwq->trustee_state != TRUSTEE_DONE);
3668 gcwq->trustee = new_trustee;
3669 gcwq->trustee_state = TRUSTEE_START;
3670 wake_up_process(gcwq->trustee);
3671 wait_trustee_state(gcwq, TRUSTEE_IN_CHARGE);
Tejun Heoe22bee72010-06-29 10:07:14 +02003672 break;
3673
Oleg Nesterov3da1c842008-07-25 01:47:50 -07003674 case CPU_POST_DEAD:
Tejun Heodb7bccf2010-06-29 10:07:12 +02003675 gcwq->trustee_state = TRUSTEE_BUTCHER;
3676 break;
3677
3678 case CPU_DOWN_FAILED:
3679 case CPU_ONLINE:
3680 if (gcwq->trustee_state != TRUSTEE_DONE) {
3681 gcwq->trustee_state = TRUSTEE_RELEASE;
3682 wake_up_process(gcwq->trustee);
3683 wait_trustee_state(gcwq, TRUSTEE_DONE);
3684 }
3685
Tejun Heo25511a42012-07-17 12:39:27 -07003686 spin_unlock_irq(&gcwq->lock);
3687 gcwq_claim_management(gcwq);
3688 spin_lock_irq(&gcwq->lock);
3689
Tejun Heobc2ae0f2012-07-17 12:39:27 -07003690 gcwq->flags &= ~GCWQ_DISASSOCIATED;
3691
Tejun Heo25511a42012-07-17 12:39:27 -07003692 rebind_workers(gcwq);
3693
3694 gcwq_release_management(gcwq);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003695 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003696 }
3697
Tejun Heodb7bccf2010-06-29 10:07:12 +02003698 spin_unlock_irqrestore(&gcwq->lock, flags);
3699
Tejun Heo15376632010-06-29 10:07:11 +02003700 return notifier_from_errno(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702
Tejun Heo65758202012-07-17 12:39:26 -07003703/*
3704 * Workqueues should be brought up before normal priority CPU notifiers.
3705 * This will be registered high priority CPU notifier.
3706 */
3707static int __devinit workqueue_cpu_up_callback(struct notifier_block *nfb,
3708 unsigned long action,
3709 void *hcpu)
3710{
3711 switch (action & ~CPU_TASKS_FROZEN) {
3712 case CPU_UP_PREPARE:
Tejun Heo65758202012-07-17 12:39:26 -07003713 case CPU_DOWN_FAILED:
3714 case CPU_ONLINE:
3715 return workqueue_cpu_callback(nfb, action, hcpu);
3716 }
3717 return NOTIFY_OK;
3718}
3719
3720/*
3721 * Workqueues should be brought down after normal priority CPU notifiers.
3722 * This will be registered as low priority CPU notifier.
3723 */
3724static int __devinit workqueue_cpu_down_callback(struct notifier_block *nfb,
3725 unsigned long action,
3726 void *hcpu)
3727{
3728 switch (action & ~CPU_TASKS_FROZEN) {
3729 case CPU_DOWN_PREPARE:
Tejun Heo65758202012-07-17 12:39:26 -07003730 case CPU_POST_DEAD:
3731 return workqueue_cpu_callback(nfb, action, hcpu);
3732 }
3733 return NOTIFY_OK;
3734}
3735
Rusty Russell2d3854a2008-11-05 13:39:10 +11003736#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003737
Rusty Russell2d3854a2008-11-05 13:39:10 +11003738struct work_for_cpu {
Andrew Morton6b440032009-04-09 09:50:37 -06003739 struct completion completion;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003740 long (*fn)(void *);
3741 void *arg;
3742 long ret;
3743};
3744
Andrew Morton6b440032009-04-09 09:50:37 -06003745static int do_work_for_cpu(void *_wfc)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003746{
Andrew Morton6b440032009-04-09 09:50:37 -06003747 struct work_for_cpu *wfc = _wfc;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003748 wfc->ret = wfc->fn(wfc->arg);
Andrew Morton6b440032009-04-09 09:50:37 -06003749 complete(&wfc->completion);
3750 return 0;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003751}
3752
3753/**
3754 * work_on_cpu - run a function in user context on a particular cpu
3755 * @cpu: the cpu to run on
3756 * @fn: the function to run
3757 * @arg: the function arg
3758 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003759 * This will return the value @fn returns.
3760 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06003761 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003762 */
3763long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3764{
Andrew Morton6b440032009-04-09 09:50:37 -06003765 struct task_struct *sub_thread;
3766 struct work_for_cpu wfc = {
3767 .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
3768 .fn = fn,
3769 .arg = arg,
3770 };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003771
Andrew Morton6b440032009-04-09 09:50:37 -06003772 sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
3773 if (IS_ERR(sub_thread))
3774 return PTR_ERR(sub_thread);
3775 kthread_bind(sub_thread, cpu);
3776 wake_up_process(sub_thread);
3777 wait_for_completion(&wfc.completion);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003778 return wfc.ret;
3779}
3780EXPORT_SYMBOL_GPL(work_on_cpu);
3781#endif /* CONFIG_SMP */
3782
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003783#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303784
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003785/**
3786 * freeze_workqueues_begin - begin freezing workqueues
3787 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003788 * Start freezing workqueues. After this function returns, all freezable
3789 * workqueues will queue new works to their frozen_works list instead of
3790 * gcwq->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003791 *
3792 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003793 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003794 */
3795void freeze_workqueues_begin(void)
3796{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003797 unsigned int cpu;
3798
3799 spin_lock(&workqueue_lock);
3800
3801 BUG_ON(workqueue_freezing);
3802 workqueue_freezing = true;
3803
Tejun Heof3421792010-07-02 10:03:51 +02003804 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003805 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003806 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003807
3808 spin_lock_irq(&gcwq->lock);
3809
Tejun Heodb7bccf2010-06-29 10:07:12 +02003810 BUG_ON(gcwq->flags & GCWQ_FREEZING);
3811 gcwq->flags |= GCWQ_FREEZING;
3812
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003813 list_for_each_entry(wq, &workqueues, list) {
3814 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3815
Tejun Heo58a69cb2011-02-16 09:25:31 +01003816 if (cwq && wq->flags & WQ_FREEZABLE)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003817 cwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003818 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003819
3820 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003821 }
3822
3823 spin_unlock(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003825
3826/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003827 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003828 *
3829 * Check whether freezing is complete. This function must be called
3830 * between freeze_workqueues_begin() and thaw_workqueues().
3831 *
3832 * CONTEXT:
3833 * Grabs and releases workqueue_lock.
3834 *
3835 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01003836 * %true if some freezable workqueues are still busy. %false if freezing
3837 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003838 */
3839bool freeze_workqueues_busy(void)
3840{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003841 unsigned int cpu;
3842 bool busy = false;
3843
3844 spin_lock(&workqueue_lock);
3845
3846 BUG_ON(!workqueue_freezing);
3847
Tejun Heof3421792010-07-02 10:03:51 +02003848 for_each_gcwq_cpu(cpu) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003849 struct workqueue_struct *wq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003850 /*
3851 * nr_active is monotonically decreasing. It's safe
3852 * to peek without lock.
3853 */
3854 list_for_each_entry(wq, &workqueues, list) {
3855 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3856
Tejun Heo58a69cb2011-02-16 09:25:31 +01003857 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003858 continue;
3859
3860 BUG_ON(cwq->nr_active < 0);
3861 if (cwq->nr_active) {
3862 busy = true;
3863 goto out_unlock;
3864 }
3865 }
3866 }
3867out_unlock:
3868 spin_unlock(&workqueue_lock);
3869 return busy;
3870}
3871
3872/**
3873 * thaw_workqueues - thaw workqueues
3874 *
3875 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo7e116292010-06-29 10:07:13 +02003876 * frozen works are transferred to their respective gcwq worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003877 *
3878 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003879 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003880 */
3881void thaw_workqueues(void)
3882{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003883 unsigned int cpu;
3884
3885 spin_lock(&workqueue_lock);
3886
3887 if (!workqueue_freezing)
3888 goto out_unlock;
3889
Tejun Heof3421792010-07-02 10:03:51 +02003890 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003891 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003892 struct worker_pool *pool;
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003893 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003894
3895 spin_lock_irq(&gcwq->lock);
3896
Tejun Heodb7bccf2010-06-29 10:07:12 +02003897 BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
3898 gcwq->flags &= ~GCWQ_FREEZING;
3899
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003900 list_for_each_entry(wq, &workqueues, list) {
3901 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3902
Tejun Heo58a69cb2011-02-16 09:25:31 +01003903 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003904 continue;
3905
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003906 /* restore max_active and repopulate worklist */
3907 cwq->max_active = wq->saved_max_active;
3908
3909 while (!list_empty(&cwq->delayed_works) &&
3910 cwq->nr_active < cwq->max_active)
3911 cwq_activate_first_delayed(cwq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003912 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003913
Tejun Heo4ce62e92012-07-13 22:16:44 -07003914 for_each_worker_pool(pool, gcwq)
3915 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02003916
Tejun Heo8b03ae32010-06-29 10:07:12 +02003917 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003918 }
3919
3920 workqueue_freezing = false;
3921out_unlock:
3922 spin_unlock(&workqueue_lock);
3923}
3924#endif /* CONFIG_FREEZER */
3925
Suresh Siddha6ee05782010-07-30 14:57:37 -07003926static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927{
Tejun Heoc34056a2010-06-29 10:07:11 +02003928 unsigned int cpu;
Tejun Heoc8e55f32010-06-29 10:07:12 +02003929 int i;
Tejun Heoc34056a2010-06-29 10:07:11 +02003930
Tejun Heo65758202012-07-17 12:39:26 -07003931 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
3932 cpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003933
3934 /* initialize gcwqs */
Tejun Heof3421792010-07-02 10:03:51 +02003935 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003936 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003937 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003938
3939 spin_lock_init(&gcwq->lock);
3940 gcwq->cpu = cpu;
Tejun Heo477a3c32010-08-31 10:54:35 +02003941 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003942
Tejun Heoc8e55f32010-06-29 10:07:12 +02003943 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
3944 INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
3945
Tejun Heo4ce62e92012-07-13 22:16:44 -07003946 for_each_worker_pool(pool, gcwq) {
3947 pool->gcwq = gcwq;
3948 INIT_LIST_HEAD(&pool->worklist);
3949 INIT_LIST_HEAD(&pool->idle_list);
Tejun Heoe22bee72010-06-29 10:07:14 +02003950
Tejun Heo4ce62e92012-07-13 22:16:44 -07003951 init_timer_deferrable(&pool->idle_timer);
3952 pool->idle_timer.function = idle_worker_timeout;
3953 pool->idle_timer.data = (unsigned long)pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003954
Tejun Heo4ce62e92012-07-13 22:16:44 -07003955 setup_timer(&pool->mayday_timer, gcwq_mayday_timeout,
3956 (unsigned long)pool);
3957
Tejun Heo60373152012-07-17 12:39:27 -07003958 mutex_init(&pool->manager_mutex);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003959 ida_init(&pool->worker_ida);
3960 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003961
Tejun Heo25511a42012-07-17 12:39:27 -07003962 init_waitqueue_head(&gcwq->rebind_hold);
3963
Tejun Heodb7bccf2010-06-29 10:07:12 +02003964 gcwq->trustee_state = TRUSTEE_DONE;
3965 init_waitqueue_head(&gcwq->trustee_wait);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003966 }
3967
Tejun Heoe22bee72010-06-29 10:07:14 +02003968 /* create the initial worker */
Tejun Heof3421792010-07-02 10:03:51 +02003969 for_each_online_gcwq_cpu(cpu) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003970 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003971 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003972
Tejun Heo477a3c32010-08-31 10:54:35 +02003973 if (cpu != WORK_CPU_UNBOUND)
3974 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003975
3976 for_each_worker_pool(pool, gcwq) {
3977 struct worker *worker;
3978
Tejun Heobc2ae0f2012-07-17 12:39:27 -07003979 worker = create_worker(pool);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003980 BUG_ON(!worker);
3981 spin_lock_irq(&gcwq->lock);
3982 start_worker(worker);
3983 spin_unlock_irq(&gcwq->lock);
3984 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003985 }
3986
Tejun Heod320c032010-06-29 10:07:14 +02003987 system_wq = alloc_workqueue("events", 0, 0);
3988 system_long_wq = alloc_workqueue("events_long", 0, 0);
3989 system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0);
Tejun Heof3421792010-07-02 10:03:51 +02003990 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3991 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01003992 system_freezable_wq = alloc_workqueue("events_freezable",
3993 WQ_FREEZABLE, 0);
Alan Stern62d3c542012-03-02 10:51:00 +01003994 system_nrt_freezable_wq = alloc_workqueue("events_nrt_freezable",
3995 WQ_NON_REENTRANT | WQ_FREEZABLE, 0);
Hitoshi Mitakee5cba242010-11-26 12:06:44 +01003996 BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq ||
Alan Stern62d3c542012-03-02 10:51:00 +01003997 !system_unbound_wq || !system_freezable_wq ||
3998 !system_nrt_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07003999 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000}
Suresh Siddha6ee05782010-07-30 14:57:37 -07004001early_initcall(init_workqueues);