blob: 9c6ad974bb9ed8237da1d378b02fa9fe73fc9582 [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>
Sasha Levin42f85702012-12-17 10:01:23 -050044#include <linux/hashtable.h>
Tejun Heoe22bee72010-06-29 10:07:14 +020045
Tejun Heoea138442013-01-18 14:05:55 -080046#include "workqueue_internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Tejun Heoc8e55f32010-06-29 10:07:12 +020048enum {
Tejun Heobc2ae0f2012-07-17 12:39:27 -070049 /*
Tejun Heo24647572013-01-24 11:01:33 -080050 * worker_pool flags
Tejun Heobc2ae0f2012-07-17 12:39:27 -070051 *
Tejun Heo24647572013-01-24 11:01:33 -080052 * A bound pool is either associated or disassociated with its CPU.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070053 * While associated (!DISASSOCIATED), all workers are bound to the
54 * CPU and none has %WORKER_UNBOUND set and concurrency management
55 * is in effect.
56 *
57 * While DISASSOCIATED, the cpu may be offline and all workers have
58 * %WORKER_UNBOUND set and concurrency management disabled, and may
Tejun Heo24647572013-01-24 11:01:33 -080059 * be executing on any CPU. The pool behaves as an unbound one.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070060 *
61 * Note that DISASSOCIATED can be flipped only while holding
Tejun Heo24647572013-01-24 11:01:33 -080062 * assoc_mutex to avoid changing binding state while
63 * create_worker() is in progress.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070064 */
Tejun Heo11ebea52012-07-12 14:46:37 -070065 POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
Lai Jiangshan552a37e2012-09-10 10:03:33 -070066 POOL_MANAGING_WORKERS = 1 << 1, /* managing workers */
Tejun Heo24647572013-01-24 11:01:33 -080067 POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */
Tejun Heo35b6bb62013-01-24 11:01:33 -080068 POOL_FREEZING = 1 << 3, /* freeze in progress */
Tejun Heodb7bccf2010-06-29 10:07:12 +020069
Tejun Heoc8e55f32010-06-29 10:07:12 +020070 /* worker flags */
71 WORKER_STARTED = 1 << 0, /* started */
72 WORKER_DIE = 1 << 1, /* die die die */
73 WORKER_IDLE = 1 << 2, /* is idle */
Tejun Heoe22bee72010-06-29 10:07:14 +020074 WORKER_PREP = 1 << 3, /* preparing to run works */
Tejun Heofb0e7be2010-06-29 10:07:15 +020075 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
Tejun Heof3421792010-07-02 10:03:51 +020076 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
Tejun Heoe22bee72010-06-29 10:07:14 +020077
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -070078 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_UNBOUND |
Tejun Heo403c8212012-07-17 12:39:27 -070079 WORKER_CPU_INTENSIVE,
Tejun Heodb7bccf2010-06-29 10:07:12 +020080
Tejun Heoe34cdddb2013-01-24 11:01:33 -080081 NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */
Tejun Heo4ce62e92012-07-13 22:16:44 -070082
Tejun Heoc8e55f32010-06-29 10:07:12 +020083 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
Tejun Heodb7bccf2010-06-29 10:07:12 +020084
Tejun Heoe22bee72010-06-29 10:07:14 +020085 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
86 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
87
Tejun Heo3233cdb2011-02-16 18:10:19 +010088 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
89 /* call for help after 10ms
90 (min two ticks) */
Tejun Heoe22bee72010-06-29 10:07:14 +020091 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
92 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heoe22bee72010-06-29 10:07:14 +020093
94 /*
95 * Rescue workers are used only on emergencies and shared by
96 * all cpus. Give -20.
97 */
98 RESCUER_NICE_LEVEL = -20,
Tejun Heo32704762012-07-13 22:16:45 -070099 HIGHPRI_NICE_LEVEL = -20,
Tejun Heoc8e55f32010-06-29 10:07:12 +0200100};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102/*
Tejun Heo4690c4a2010-06-29 10:07:10 +0200103 * Structure fields follow one of the following exclusion rules.
104 *
Tejun Heoe41e7042010-08-24 14:22:47 +0200105 * I: Modifiable by initialization/destruction paths and read-only for
106 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200107 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200108 * P: Preemption protected. Disabling preemption is enough and should
109 * only be modified and accessed from the local cpu.
110 *
Tejun Heo8b03ae32010-06-29 10:07:12 +0200111 * L: gcwq->lock protected. Access with gcwq->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200112 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200113 * X: During normal operation, modification requires gcwq->lock and
114 * should be done only from local cpu. Either disabling preemption
115 * on local cpu or grabbing gcwq->lock is enough for read access.
Tejun Heo24647572013-01-24 11:01:33 -0800116 * If POOL_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200117 *
Tejun Heo73f53c42010-06-29 10:07:11 +0200118 * F: wq->flush_mutex protected.
119 *
Tejun Heo4690c4a2010-06-29 10:07:10 +0200120 * W: workqueue_lock protected.
121 */
122
Tejun Heo2eaebdb2013-01-18 14:05:55 -0800123/* struct worker is defined in workqueue_internal.h */
Tejun Heoc34056a2010-06-29 10:07:11 +0200124
Tejun Heobd7bdd42012-07-12 14:46:37 -0700125struct worker_pool {
126 struct global_cwq *gcwq; /* I: the owning gcwq */
Tejun Heo9daf9e62013-01-24 11:01:33 -0800127 int id; /* I: pool ID */
Tejun Heo11ebea52012-07-12 14:46:37 -0700128 unsigned int flags; /* X: flags */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700129
130 struct list_head worklist; /* L: list of pending works */
131 int nr_workers; /* L: total number of workers */
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700132
133 /* nr_idle includes the ones off idle_list for rebinding */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700134 int nr_idle; /* L: currently idle ones */
135
136 struct list_head idle_list; /* X: list of idle workers */
137 struct timer_list idle_timer; /* L: worker idle timeout */
138 struct timer_list mayday_timer; /* L: SOS timer for workers */
139
Tejun Heo24647572013-01-24 11:01:33 -0800140 struct mutex assoc_mutex; /* protect POOL_DISASSOCIATED */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700141 struct ida worker_ida; /* L: for worker IDs */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700142};
143
Tejun Heo4690c4a2010-06-29 10:07:10 +0200144/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200145 * Global per-cpu workqueue. There's one and only one for each cpu
146 * and all works are queued and processed here regardless of their
147 * target workqueues.
Tejun Heo8b03ae32010-06-29 10:07:12 +0200148 */
149struct global_cwq {
150 spinlock_t lock; /* the gcwq lock */
151 unsigned int cpu; /* I: the associated cpu */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200152
Tejun Heobd7bdd42012-07-12 14:46:37 -0700153 /* workers are chained either in busy_hash or pool idle_list */
Sasha Levin42f85702012-12-17 10:01:23 -0500154 DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER);
Tejun Heoc8e55f32010-06-29 10:07:12 +0200155 /* L: hash of busy workers */
156
Tejun Heoe34cdddb2013-01-24 11:01:33 -0800157 struct worker_pool pools[NR_STD_WORKER_POOLS];
Joonsoo Kim330dad52012-08-15 23:25:36 +0900158 /* normal and highpri pools */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200159} ____cacheline_aligned_in_smp;
160
161/*
Tejun Heo502ca9d2010-06-29 10:07:13 +0200162 * The per-CPU workqueue. The lower WORK_STRUCT_FLAG_BITS of
Tejun Heo0f900042010-06-29 10:07:11 +0200163 * work_struct->data are used for flags and thus cwqs need to be
164 * aligned at two's power of the number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 */
166struct cpu_workqueue_struct {
Tejun Heobd7bdd42012-07-12 14:46:37 -0700167 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200168 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200169 int work_color; /* L: current color */
170 int flush_color; /* L: flushing color */
171 int nr_in_flight[WORK_NR_COLORS];
172 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200173 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200174 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200175 struct list_head delayed_works; /* L: delayed works */
Tejun Heo0f900042010-06-29 10:07:11 +0200176};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200179 * Structure used to wait for workqueue flush.
180 */
181struct wq_flusher {
182 struct list_head list; /* F: list of flushers */
183 int flush_color; /* F: flush color waiting for */
184 struct completion done; /* flush completion */
185};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Tejun Heo73f53c42010-06-29 10:07:11 +0200187/*
Tejun Heof2e005a2010-07-20 15:59:09 +0200188 * All cpumasks are assumed to be always set on UP and thus can't be
189 * used to determine whether there's something to be done.
190 */
191#ifdef CONFIG_SMP
192typedef cpumask_var_t mayday_mask_t;
193#define mayday_test_and_set_cpu(cpu, mask) \
194 cpumask_test_and_set_cpu((cpu), (mask))
195#define mayday_clear_cpu(cpu, mask) cpumask_clear_cpu((cpu), (mask))
196#define for_each_mayday_cpu(cpu, mask) for_each_cpu((cpu), (mask))
Tejun Heo9c375472010-08-31 11:18:34 +0200197#define alloc_mayday_mask(maskp, gfp) zalloc_cpumask_var((maskp), (gfp))
Tejun Heof2e005a2010-07-20 15:59:09 +0200198#define free_mayday_mask(mask) free_cpumask_var((mask))
199#else
200typedef unsigned long mayday_mask_t;
201#define mayday_test_and_set_cpu(cpu, mask) test_and_set_bit(0, &(mask))
202#define mayday_clear_cpu(cpu, mask) clear_bit(0, &(mask))
203#define for_each_mayday_cpu(cpu, mask) if ((cpu) = 0, (mask))
204#define alloc_mayday_mask(maskp, gfp) true
205#define free_mayday_mask(mask) do { } while (0)
206#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208/*
209 * The externally visible workqueue abstraction is an array of
210 * per-CPU workqueues:
211 */
212struct workqueue_struct {
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200213 unsigned int flags; /* W: WQ_* flags */
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200214 union {
215 struct cpu_workqueue_struct __percpu *pcpu;
216 struct cpu_workqueue_struct *single;
217 unsigned long v;
218 } cpu_wq; /* I: cwq's */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200219 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200220
221 struct mutex flush_mutex; /* protects wq flushing */
222 int work_color; /* F: current work color */
223 int flush_color; /* F: current flush color */
224 atomic_t nr_cwqs_to_flush; /* flush in progress */
225 struct wq_flusher *first_flusher; /* F: first flusher */
226 struct list_head flusher_queue; /* F: flush waiters */
227 struct list_head flusher_overflow; /* F: flush overflow list */
228
Tejun Heof2e005a2010-07-20 15:59:09 +0200229 mayday_mask_t mayday_mask; /* cpus requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200230 struct worker *rescuer; /* I: rescue worker */
231
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200232 int nr_drainers; /* W: drain in progress */
Tejun Heodcd989c2010-06-29 10:07:14 +0200233 int saved_max_active; /* W: saved cwq max_active */
Johannes Berg4e6045f2007-10-18 23:39:55 -0700234#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200235 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700236#endif
Tejun Heob196be82012-01-10 15:11:35 -0800237 char name[]; /* I: workqueue name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238};
239
Tejun Heod320c032010-06-29 10:07:14 +0200240struct workqueue_struct *system_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200241EXPORT_SYMBOL_GPL(system_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300242struct workqueue_struct *system_highpri_wq __read_mostly;
Joonsoo Kim1aabe902012-08-15 23:25:39 +0900243EXPORT_SYMBOL_GPL(system_highpri_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300244struct workqueue_struct *system_long_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200245EXPORT_SYMBOL_GPL(system_long_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300246struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200247EXPORT_SYMBOL_GPL(system_unbound_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300248struct workqueue_struct *system_freezable_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100249EXPORT_SYMBOL_GPL(system_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200250
Tejun Heo97bd2342010-10-05 10:41:14 +0200251#define CREATE_TRACE_POINTS
252#include <trace/events/workqueue.h>
253
Tejun Heo4ce62e92012-07-13 22:16:44 -0700254#define for_each_worker_pool(pool, gcwq) \
Tejun Heo32704762012-07-13 22:16:45 -0700255 for ((pool) = &(gcwq)->pools[0]; \
Tejun Heoe34cdddb2013-01-24 11:01:33 -0800256 (pool) < &(gcwq)->pools[NR_STD_WORKER_POOLS]; (pool)++)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700257
Tejun Heodb7bccf2010-06-29 10:07:12 +0200258#define for_each_busy_worker(worker, i, pos, gcwq) \
Sasha Levin42f85702012-12-17 10:01:23 -0500259 hash_for_each(gcwq->busy_hash, i, pos, worker, hentry)
Tejun Heodb7bccf2010-06-29 10:07:12 +0200260
Tejun Heof3421792010-07-02 10:03:51 +0200261static inline int __next_gcwq_cpu(int cpu, const struct cpumask *mask,
262 unsigned int sw)
263{
264 if (cpu < nr_cpu_ids) {
265 if (sw & 1) {
266 cpu = cpumask_next(cpu, mask);
267 if (cpu < nr_cpu_ids)
268 return cpu;
269 }
270 if (sw & 2)
271 return WORK_CPU_UNBOUND;
272 }
273 return WORK_CPU_NONE;
274}
275
276static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
277 struct workqueue_struct *wq)
278{
279 return __next_gcwq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2);
280}
281
Tejun Heo09884952010-08-01 11:50:12 +0200282/*
283 * CPU iterators
284 *
285 * An extra gcwq is defined for an invalid cpu number
286 * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any
287 * specific CPU. The following iterators are similar to
288 * for_each_*_cpu() iterators but also considers the unbound gcwq.
289 *
290 * for_each_gcwq_cpu() : possible CPUs + WORK_CPU_UNBOUND
291 * for_each_online_gcwq_cpu() : online CPUs + WORK_CPU_UNBOUND
292 * for_each_cwq_cpu() : possible CPUs for bound workqueues,
293 * WORK_CPU_UNBOUND for unbound workqueues
294 */
Tejun Heof3421792010-07-02 10:03:51 +0200295#define for_each_gcwq_cpu(cpu) \
296 for ((cpu) = __next_gcwq_cpu(-1, cpu_possible_mask, 3); \
297 (cpu) < WORK_CPU_NONE; \
298 (cpu) = __next_gcwq_cpu((cpu), cpu_possible_mask, 3))
299
300#define for_each_online_gcwq_cpu(cpu) \
301 for ((cpu) = __next_gcwq_cpu(-1, cpu_online_mask, 3); \
302 (cpu) < WORK_CPU_NONE; \
303 (cpu) = __next_gcwq_cpu((cpu), cpu_online_mask, 3))
304
305#define for_each_cwq_cpu(cpu, wq) \
306 for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, (wq)); \
307 (cpu) < WORK_CPU_NONE; \
308 (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq)))
309
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900310#ifdef CONFIG_DEBUG_OBJECTS_WORK
311
312static struct debug_obj_descr work_debug_descr;
313
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100314static void *work_debug_hint(void *addr)
315{
316 return ((struct work_struct *) addr)->func;
317}
318
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900319/*
320 * fixup_init is called when:
321 * - an active object is initialized
322 */
323static int work_fixup_init(void *addr, enum debug_obj_state state)
324{
325 struct work_struct *work = addr;
326
327 switch (state) {
328 case ODEBUG_STATE_ACTIVE:
329 cancel_work_sync(work);
330 debug_object_init(work, &work_debug_descr);
331 return 1;
332 default:
333 return 0;
334 }
335}
336
337/*
338 * fixup_activate is called when:
339 * - an active object is activated
340 * - an unknown object is activated (might be a statically initialized object)
341 */
342static int work_fixup_activate(void *addr, enum debug_obj_state state)
343{
344 struct work_struct *work = addr;
345
346 switch (state) {
347
348 case ODEBUG_STATE_NOTAVAILABLE:
349 /*
350 * This is not really a fixup. The work struct was
351 * statically initialized. We just make sure that it
352 * is tracked in the object tracker.
353 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200354 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900355 debug_object_init(work, &work_debug_descr);
356 debug_object_activate(work, &work_debug_descr);
357 return 0;
358 }
359 WARN_ON_ONCE(1);
360 return 0;
361
362 case ODEBUG_STATE_ACTIVE:
363 WARN_ON(1);
364
365 default:
366 return 0;
367 }
368}
369
370/*
371 * fixup_free is called when:
372 * - an active object is freed
373 */
374static int work_fixup_free(void *addr, enum debug_obj_state state)
375{
376 struct work_struct *work = addr;
377
378 switch (state) {
379 case ODEBUG_STATE_ACTIVE:
380 cancel_work_sync(work);
381 debug_object_free(work, &work_debug_descr);
382 return 1;
383 default:
384 return 0;
385 }
386}
387
388static struct debug_obj_descr work_debug_descr = {
389 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100390 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900391 .fixup_init = work_fixup_init,
392 .fixup_activate = work_fixup_activate,
393 .fixup_free = work_fixup_free,
394};
395
396static inline void debug_work_activate(struct work_struct *work)
397{
398 debug_object_activate(work, &work_debug_descr);
399}
400
401static inline void debug_work_deactivate(struct work_struct *work)
402{
403 debug_object_deactivate(work, &work_debug_descr);
404}
405
406void __init_work(struct work_struct *work, int onstack)
407{
408 if (onstack)
409 debug_object_init_on_stack(work, &work_debug_descr);
410 else
411 debug_object_init(work, &work_debug_descr);
412}
413EXPORT_SYMBOL_GPL(__init_work);
414
415void destroy_work_on_stack(struct work_struct *work)
416{
417 debug_object_free(work, &work_debug_descr);
418}
419EXPORT_SYMBOL_GPL(destroy_work_on_stack);
420
421#else
422static inline void debug_work_activate(struct work_struct *work) { }
423static inline void debug_work_deactivate(struct work_struct *work) { }
424#endif
425
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100426/* Serializes the accesses to the list of workqueues. */
427static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428static LIST_HEAD(workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200429static bool workqueue_freezing; /* W: have wqs started freezing? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Oleg Nesterov14441962007-05-23 13:57:57 -0700431/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200432 * The almighty global cpu workqueues. nr_running is the only field
433 * which is expected to be used frequently by other cpus via
434 * try_to_wake_up(). Put it in a separate cacheline.
Oleg Nesterov14441962007-05-23 13:57:57 -0700435 */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200436static DEFINE_PER_CPU(struct global_cwq, global_cwq);
Tejun Heoe34cdddb2013-01-24 11:01:33 -0800437static DEFINE_PER_CPU_SHARED_ALIGNED(atomic_t, pool_nr_running[NR_STD_WORKER_POOLS]);
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800438
Tejun Heof3421792010-07-02 10:03:51 +0200439/*
Tejun Heo24647572013-01-24 11:01:33 -0800440 * Global cpu workqueue and nr_running counter for unbound gcwq. The pools
441 * for online CPUs have POOL_DISASSOCIATED set, and all their workers have
442 * WORKER_UNBOUND set.
Tejun Heof3421792010-07-02 10:03:51 +0200443 */
444static struct global_cwq unbound_global_cwq;
Tejun Heoe34cdddb2013-01-24 11:01:33 -0800445static atomic_t unbound_pool_nr_running[NR_STD_WORKER_POOLS] = {
446 [0 ... NR_STD_WORKER_POOLS - 1] = ATOMIC_INIT(0), /* always 0 */
Tejun Heo4ce62e92012-07-13 22:16:44 -0700447};
Tejun Heof3421792010-07-02 10:03:51 +0200448
Tejun Heo9daf9e62013-01-24 11:01:33 -0800449/* idr of all pools */
450static DEFINE_MUTEX(worker_pool_idr_mutex);
451static DEFINE_IDR(worker_pool_idr);
452
Tejun Heoc34056a2010-06-29 10:07:11 +0200453static int worker_thread(void *__worker);
Tejun Heoe2905b22013-01-24 11:01:32 -0800454static unsigned int work_cpu(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Tejun Heoe34cdddb2013-01-24 11:01:33 -0800456static int std_worker_pool_pri(struct worker_pool *pool)
Tejun Heo32704762012-07-13 22:16:45 -0700457{
458 return pool - pool->gcwq->pools;
459}
460
Tejun Heo8b03ae32010-06-29 10:07:12 +0200461static struct global_cwq *get_gcwq(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Tejun Heof3421792010-07-02 10:03:51 +0200463 if (cpu != WORK_CPU_UNBOUND)
464 return &per_cpu(global_cwq, cpu);
465 else
466 return &unbound_global_cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
Tejun Heo9daf9e62013-01-24 11:01:33 -0800469/* allocate ID and assign it to @pool */
470static int worker_pool_assign_id(struct worker_pool *pool)
471{
472 int ret;
473
474 mutex_lock(&worker_pool_idr_mutex);
475 idr_pre_get(&worker_pool_idr, GFP_KERNEL);
476 ret = idr_get_new(&worker_pool_idr, pool, &pool->id);
477 mutex_unlock(&worker_pool_idr_mutex);
478
479 return ret;
480}
481
Tejun Heo63d95a92012-07-12 14:46:37 -0700482static atomic_t *get_pool_nr_running(struct worker_pool *pool)
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700483{
Tejun Heo63d95a92012-07-12 14:46:37 -0700484 int cpu = pool->gcwq->cpu;
Tejun Heoe34cdddb2013-01-24 11:01:33 -0800485 int idx = std_worker_pool_pri(pool);
Tejun Heo63d95a92012-07-12 14:46:37 -0700486
Tejun Heof3421792010-07-02 10:03:51 +0200487 if (cpu != WORK_CPU_UNBOUND)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700488 return &per_cpu(pool_nr_running, cpu)[idx];
Tejun Heof3421792010-07-02 10:03:51 +0200489 else
Tejun Heo4ce62e92012-07-13 22:16:44 -0700490 return &unbound_pool_nr_running[idx];
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700491}
492
Tejun Heo4690c4a2010-06-29 10:07:10 +0200493static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
494 struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700495{
Tejun Heof3421792010-07-02 10:03:51 +0200496 if (!(wq->flags & WQ_UNBOUND)) {
Lai Jiangshane06ffa12012-03-09 18:03:20 +0800497 if (likely(cpu < nr_cpu_ids))
Tejun Heof3421792010-07-02 10:03:51 +0200498 return per_cpu_ptr(wq->cpu_wq.pcpu, cpu);
Tejun Heof3421792010-07-02 10:03:51 +0200499 } else if (likely(cpu == WORK_CPU_UNBOUND))
500 return wq->cpu_wq.single;
501 return NULL;
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700502}
503
Tejun Heo73f53c42010-06-29 10:07:11 +0200504static unsigned int work_color_to_flags(int color)
505{
506 return color << WORK_STRUCT_COLOR_SHIFT;
507}
508
509static int get_work_color(struct work_struct *work)
510{
511 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
512 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
513}
514
515static int work_next_color(int color)
516{
517 return (color + 1) % WORK_NR_COLORS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
David Howells4594bf12006-12-07 11:33:26 +0000520/*
Tejun Heob5490072012-08-03 10:30:46 -0700521 * While queued, %WORK_STRUCT_CWQ is set and non flag bits of a work's data
522 * contain the pointer to the queued cwq. Once execution starts, the flag
523 * is cleared and the high bits contain OFFQ flags and CPU number.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200524 *
Tejun Heobbb68df2012-08-03 10:30:46 -0700525 * set_work_cwq(), set_work_cpu_and_clear_pending(), mark_work_canceling()
526 * and clear_work_data() can be used to set the cwq, cpu or clear
527 * work->data. These functions should only be called while the work is
528 * owned - ie. while the PENDING bit is set.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200529 *
Tejun Heobbb68df2012-08-03 10:30:46 -0700530 * get_work_[g]cwq() can be used to obtain the gcwq or cwq corresponding to
531 * a work. gcwq is available once the work has been queued anywhere after
532 * initialization until it is sync canceled. cwq is available only while
533 * the work item is queued.
534 *
535 * %WORK_OFFQ_CANCELING is used to mark a work item which is being
536 * canceled. While being canceled, a work item may have its PENDING set
537 * but stay off timer and worklist for arbitrarily long and nobody should
538 * try to steal the PENDING bit.
David Howells4594bf12006-12-07 11:33:26 +0000539 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200540static inline void set_work_data(struct work_struct *work, unsigned long data,
541 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000542{
David Howells4594bf12006-12-07 11:33:26 +0000543 BUG_ON(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200544 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000545}
David Howells365970a2006-11-22 14:54:49 +0000546
Tejun Heo7a22ad72010-06-29 10:07:13 +0200547static void set_work_cwq(struct work_struct *work,
548 struct cpu_workqueue_struct *cwq,
549 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200550{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200551 set_work_data(work, (unsigned long)cwq,
Tejun Heoe1201532010-07-22 14:14:25 +0200552 WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200553}
554
Tejun Heo8930cab2012-08-03 10:30:45 -0700555static void set_work_cpu_and_clear_pending(struct work_struct *work,
556 unsigned int cpu)
David Howells365970a2006-11-22 14:54:49 +0000557{
Tejun Heo23657bb2012-08-13 17:08:19 -0700558 /*
559 * The following wmb is paired with the implied mb in
560 * test_and_set_bit(PENDING) and ensures all updates to @work made
561 * here are visible to and precede any updates by the next PENDING
562 * owner.
563 */
564 smp_wmb();
Tejun Heob5490072012-08-03 10:30:46 -0700565 set_work_data(work, (unsigned long)cpu << WORK_OFFQ_CPU_SHIFT, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200566}
567
568static void clear_work_data(struct work_struct *work)
569{
Tejun Heo23657bb2012-08-13 17:08:19 -0700570 smp_wmb(); /* see set_work_cpu_and_clear_pending() */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200571 set_work_data(work, WORK_STRUCT_NO_CPU, 0);
572}
573
Tejun Heo7a22ad72010-06-29 10:07:13 +0200574static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work)
575{
Tejun Heoe1201532010-07-22 14:14:25 +0200576 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200577
Tejun Heoe1201532010-07-22 14:14:25 +0200578 if (data & WORK_STRUCT_CWQ)
579 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
580 else
581 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200582}
583
584static struct global_cwq *get_work_gcwq(struct work_struct *work)
585{
Tejun Heoe1201532010-07-22 14:14:25 +0200586 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200587 unsigned int cpu;
588
Tejun Heoe1201532010-07-22 14:14:25 +0200589 if (data & WORK_STRUCT_CWQ)
590 return ((struct cpu_workqueue_struct *)
Tejun Heobd7bdd42012-07-12 14:46:37 -0700591 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->gcwq;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200592
Tejun Heob5490072012-08-03 10:30:46 -0700593 cpu = data >> WORK_OFFQ_CPU_SHIFT;
Tejun Heo715b06b2013-01-24 11:01:33 -0800594 if (cpu == WORK_OFFQ_CPU_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200595 return NULL;
596
Tejun Heof3421792010-07-02 10:03:51 +0200597 BUG_ON(cpu >= nr_cpu_ids && cpu != WORK_CPU_UNBOUND);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200598 return get_gcwq(cpu);
David Howells365970a2006-11-22 14:54:49 +0000599}
600
Tejun Heobbb68df2012-08-03 10:30:46 -0700601static void mark_work_canceling(struct work_struct *work)
602{
603 struct global_cwq *gcwq = get_work_gcwq(work);
Tejun Heo715b06b2013-01-24 11:01:33 -0800604 unsigned long cpu = gcwq ? gcwq->cpu : WORK_OFFQ_CPU_NONE;
Tejun Heobbb68df2012-08-03 10:30:46 -0700605
606 set_work_data(work, (cpu << WORK_OFFQ_CPU_SHIFT) | WORK_OFFQ_CANCELING,
607 WORK_STRUCT_PENDING);
608}
609
610static bool work_is_canceling(struct work_struct *work)
611{
612 unsigned long data = atomic_long_read(&work->data);
613
614 return !(data & WORK_STRUCT_CWQ) && (data & WORK_OFFQ_CANCELING);
615}
616
David Howells365970a2006-11-22 14:54:49 +0000617/*
Tejun Heo32704762012-07-13 22:16:45 -0700618 * Policy functions. These define the policies on how the global worker
619 * pools are managed. Unless noted otherwise, these functions assume that
620 * they're being called with gcwq->lock held.
David Howells365970a2006-11-22 14:54:49 +0000621 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200622
Tejun Heo63d95a92012-07-12 14:46:37 -0700623static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000624{
Tejun Heo32704762012-07-13 22:16:45 -0700625 return !atomic_read(get_pool_nr_running(pool));
David Howells365970a2006-11-22 14:54:49 +0000626}
627
Tejun Heoe22bee72010-06-29 10:07:14 +0200628/*
629 * Need to wake up a worker? Called from anything but currently
630 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700631 *
632 * Note that, because unbound workers never contribute to nr_running, this
633 * function will always return %true for unbound gcwq as long as the
634 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200635 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700636static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000637{
Tejun Heo63d95a92012-07-12 14:46:37 -0700638 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000639}
640
Tejun Heoe22bee72010-06-29 10:07:14 +0200641/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700642static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200643{
Tejun Heo63d95a92012-07-12 14:46:37 -0700644 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200645}
646
647/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700648static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200649{
Tejun Heo63d95a92012-07-12 14:46:37 -0700650 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200651
Tejun Heo32704762012-07-13 22:16:45 -0700652 return !list_empty(&pool->worklist) && atomic_read(nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200653}
654
655/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700656static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200657{
Tejun Heo63d95a92012-07-12 14:46:37 -0700658 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200659}
660
661/* Do I need to be the manager? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700662static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200663{
Tejun Heo63d95a92012-07-12 14:46:37 -0700664 return need_to_create_worker(pool) ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700665 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200666}
667
668/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700669static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200670{
Lai Jiangshan552a37e2012-09-10 10:03:33 -0700671 bool managing = pool->flags & POOL_MANAGING_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -0700672 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
673 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200674
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700675 /*
676 * nr_idle and idle_list may disagree if idle rebinding is in
677 * progress. Never return %true if idle_list is empty.
678 */
679 if (list_empty(&pool->idle_list))
680 return false;
681
Tejun Heoe22bee72010-06-29 10:07:14 +0200682 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
683}
684
685/*
686 * Wake up functions.
687 */
688
Tejun Heo7e116292010-06-29 10:07:13 +0200689/* Return the first worker. Safe with preemption disabled */
Tejun Heo63d95a92012-07-12 14:46:37 -0700690static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200691{
Tejun Heo63d95a92012-07-12 14:46:37 -0700692 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200693 return NULL;
694
Tejun Heo63d95a92012-07-12 14:46:37 -0700695 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200696}
697
698/**
699 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700700 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200701 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700702 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200703 *
704 * CONTEXT:
705 * spin_lock_irq(gcwq->lock).
706 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700707static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200708{
Tejun Heo63d95a92012-07-12 14:46:37 -0700709 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200710
711 if (likely(worker))
712 wake_up_process(worker->task);
713}
714
Tejun Heo4690c4a2010-06-29 10:07:10 +0200715/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200716 * wq_worker_waking_up - a worker is waking up
717 * @task: task waking up
718 * @cpu: CPU @task is waking up to
719 *
720 * This function is called during try_to_wake_up() when a worker is
721 * being awoken.
722 *
723 * CONTEXT:
724 * spin_lock_irq(rq->lock)
725 */
726void wq_worker_waking_up(struct task_struct *task, unsigned int cpu)
727{
728 struct worker *worker = kthread_data(task);
729
Joonsoo Kim36576002012-10-26 23:03:49 +0900730 if (!(worker->flags & WORKER_NOT_RUNNING)) {
731 WARN_ON_ONCE(worker->pool->gcwq->cpu != cpu);
Tejun Heo63d95a92012-07-12 14:46:37 -0700732 atomic_inc(get_pool_nr_running(worker->pool));
Joonsoo Kim36576002012-10-26 23:03:49 +0900733 }
Tejun Heoe22bee72010-06-29 10:07:14 +0200734}
735
736/**
737 * wq_worker_sleeping - a worker is going to sleep
738 * @task: task going to sleep
739 * @cpu: CPU in question, must be the current CPU number
740 *
741 * This function is called during schedule() when a busy worker is
742 * going to sleep. Worker on the same cpu can be woken up by
743 * returning pointer to its task.
744 *
745 * CONTEXT:
746 * spin_lock_irq(rq->lock)
747 *
748 * RETURNS:
749 * Worker task on @cpu to wake up, %NULL if none.
750 */
751struct task_struct *wq_worker_sleeping(struct task_struct *task,
752 unsigned int cpu)
753{
754 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heo111c2252013-01-17 17:16:24 -0800755 struct worker_pool *pool;
756 atomic_t *nr_running;
Tejun Heoe22bee72010-06-29 10:07:14 +0200757
Tejun Heo111c2252013-01-17 17:16:24 -0800758 /*
759 * Rescuers, which may not have all the fields set up like normal
760 * workers, also reach here, let's not access anything before
761 * checking NOT_RUNNING.
762 */
Steven Rostedt2d646722010-12-03 23:12:33 -0500763 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200764 return NULL;
765
Tejun Heo111c2252013-01-17 17:16:24 -0800766 pool = worker->pool;
767 nr_running = get_pool_nr_running(pool);
768
Tejun Heoe22bee72010-06-29 10:07:14 +0200769 /* this can only happen on the local cpu */
770 BUG_ON(cpu != raw_smp_processor_id());
771
772 /*
773 * The counterpart of the following dec_and_test, implied mb,
774 * worklist not empty test sequence is in insert_work().
775 * Please read comment there.
776 *
Tejun Heo628c78e2012-07-17 12:39:27 -0700777 * NOT_RUNNING is clear. This means that we're bound to and
778 * running on the local cpu w/ rq lock held and preemption
779 * disabled, which in turn means that none else could be
780 * manipulating idle_list, so dereferencing idle_list without gcwq
781 * lock is safe.
Tejun Heoe22bee72010-06-29 10:07:14 +0200782 */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700783 if (atomic_dec_and_test(nr_running) && !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700784 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200785 return to_wakeup ? to_wakeup->task : NULL;
786}
787
788/**
789 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200790 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200791 * @flags: flags to set
792 * @wakeup: wakeup an idle worker if necessary
793 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200794 * Set @flags in @worker->flags and adjust nr_running accordingly. If
795 * nr_running becomes zero and @wakeup is %true, an idle worker is
796 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200797 *
Tejun Heocb444762010-07-02 10:03:50 +0200798 * CONTEXT:
799 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200800 */
801static inline void worker_set_flags(struct worker *worker, unsigned int flags,
802 bool wakeup)
803{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700804 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200805
Tejun Heocb444762010-07-02 10:03:50 +0200806 WARN_ON_ONCE(worker->task != current);
807
Tejun Heoe22bee72010-06-29 10:07:14 +0200808 /*
809 * If transitioning into NOT_RUNNING, adjust nr_running and
810 * wake up an idle worker as necessary if requested by
811 * @wakeup.
812 */
813 if ((flags & WORKER_NOT_RUNNING) &&
814 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heo63d95a92012-07-12 14:46:37 -0700815 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200816
817 if (wakeup) {
818 if (atomic_dec_and_test(nr_running) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -0700819 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700820 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200821 } else
822 atomic_dec(nr_running);
823 }
824
Tejun Heod302f012010-06-29 10:07:13 +0200825 worker->flags |= flags;
826}
827
828/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200829 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200830 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200831 * @flags: flags to clear
832 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200833 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200834 *
Tejun Heocb444762010-07-02 10:03:50 +0200835 * CONTEXT:
836 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200837 */
838static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
839{
Tejun Heo63d95a92012-07-12 14:46:37 -0700840 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200841 unsigned int oflags = worker->flags;
842
Tejun Heocb444762010-07-02 10:03:50 +0200843 WARN_ON_ONCE(worker->task != current);
844
Tejun Heod302f012010-06-29 10:07:13 +0200845 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200846
Tejun Heo42c025f2011-01-11 15:58:49 +0100847 /*
848 * If transitioning out of NOT_RUNNING, increment nr_running. Note
849 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
850 * of multiple flags, not a single flag.
851 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200852 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
853 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heo63d95a92012-07-12 14:46:37 -0700854 atomic_inc(get_pool_nr_running(pool));
Tejun Heod302f012010-06-29 10:07:13 +0200855}
856
857/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200858 * find_worker_executing_work - find worker which is executing a work
859 * @gcwq: gcwq of interest
860 * @work: work to find worker for
861 *
Tejun Heoa2c1c572012-12-18 10:35:02 -0800862 * Find a worker which is executing @work on @gcwq by searching
863 * @gcwq->busy_hash which is keyed by the address of @work. For a worker
864 * to match, its current execution should match the address of @work and
865 * its work function. This is to avoid unwanted dependency between
866 * unrelated work executions through a work item being recycled while still
867 * being executed.
868 *
869 * This is a bit tricky. A work item may be freed once its execution
870 * starts and nothing prevents the freed area from being recycled for
871 * another work item. If the same work item address ends up being reused
872 * before the original execution finishes, workqueue will identify the
873 * recycled work item as currently executing and make it wait until the
874 * current execution finishes, introducing an unwanted dependency.
875 *
876 * This function checks the work item address, work function and workqueue
877 * to avoid false positives. Note that this isn't complete as one may
878 * construct a work function which can introduce dependency onto itself
879 * through a recycled work item. Well, if somebody wants to shoot oneself
880 * in the foot that badly, there's only so much we can do, and if such
881 * deadlock actually occurs, it should be easy to locate the culprit work
882 * function.
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200883 *
884 * CONTEXT:
885 * spin_lock_irq(gcwq->lock).
886 *
887 * RETURNS:
888 * Pointer to worker which is executing @work if found, NULL
889 * otherwise.
890 */
891static struct worker *find_worker_executing_work(struct global_cwq *gcwq,
892 struct work_struct *work)
893{
Sasha Levin42f85702012-12-17 10:01:23 -0500894 struct worker *worker;
895 struct hlist_node *tmp;
896
Tejun Heoa2c1c572012-12-18 10:35:02 -0800897 hash_for_each_possible(gcwq->busy_hash, worker, tmp, hentry,
898 (unsigned long)work)
899 if (worker->current_work == work &&
900 worker->current_func == work->func)
Sasha Levin42f85702012-12-17 10:01:23 -0500901 return worker;
902
903 return NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200904}
905
906/**
Tejun Heobf4ede02012-08-03 10:30:46 -0700907 * move_linked_works - move linked works to a list
908 * @work: start of series of works to be scheduled
909 * @head: target list to append @work to
910 * @nextp: out paramter for nested worklist walking
911 *
912 * Schedule linked works starting from @work to @head. Work series to
913 * be scheduled starts at @work and includes any consecutive work with
914 * WORK_STRUCT_LINKED set in its predecessor.
915 *
916 * If @nextp is not NULL, it's updated to point to the next work of
917 * the last scheduled work. This allows move_linked_works() to be
918 * nested inside outer list_for_each_entry_safe().
919 *
920 * CONTEXT:
921 * spin_lock_irq(gcwq->lock).
922 */
923static void move_linked_works(struct work_struct *work, struct list_head *head,
924 struct work_struct **nextp)
925{
926 struct work_struct *n;
927
928 /*
929 * Linked worklist will always end before the end of the list,
930 * use NULL for list head.
931 */
932 list_for_each_entry_safe_from(work, n, NULL, entry) {
933 list_move_tail(&work->entry, head);
934 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
935 break;
936 }
937
938 /*
939 * If we're already inside safe list traversal and have moved
940 * multiple works to the scheduled queue, the next position
941 * needs to be updated.
942 */
943 if (nextp)
944 *nextp = n;
945}
946
Lai Jiangshan3aa62492012-09-18 10:40:00 -0700947static void cwq_activate_delayed_work(struct work_struct *work)
Tejun Heobf4ede02012-08-03 10:30:46 -0700948{
Lai Jiangshan3aa62492012-09-18 10:40:00 -0700949 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heobf4ede02012-08-03 10:30:46 -0700950
951 trace_workqueue_activate_work(work);
952 move_linked_works(work, &cwq->pool->worklist, NULL);
953 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
954 cwq->nr_active++;
955}
956
Lai Jiangshan3aa62492012-09-18 10:40:00 -0700957static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
958{
959 struct work_struct *work = list_first_entry(&cwq->delayed_works,
960 struct work_struct, entry);
961
962 cwq_activate_delayed_work(work);
963}
964
Tejun Heobf4ede02012-08-03 10:30:46 -0700965/**
966 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
967 * @cwq: cwq of interest
968 * @color: color of work which left the queue
Tejun Heobf4ede02012-08-03 10:30:46 -0700969 *
970 * A work either has completed or is removed from pending queue,
971 * decrement nr_in_flight of its cwq and handle workqueue flushing.
972 *
973 * CONTEXT:
974 * spin_lock_irq(gcwq->lock).
975 */
Lai Jiangshanb3f9f402012-09-18 10:40:00 -0700976static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color)
Tejun Heobf4ede02012-08-03 10:30:46 -0700977{
978 /* ignore uncolored works */
979 if (color == WORK_NO_COLOR)
980 return;
981
982 cwq->nr_in_flight[color]--;
983
Lai Jiangshanb3f9f402012-09-18 10:40:00 -0700984 cwq->nr_active--;
985 if (!list_empty(&cwq->delayed_works)) {
986 /* one down, submit a delayed one */
987 if (cwq->nr_active < cwq->max_active)
988 cwq_activate_first_delayed(cwq);
Tejun Heobf4ede02012-08-03 10:30:46 -0700989 }
990
991 /* is flush in progress and are we at the flushing tip? */
992 if (likely(cwq->flush_color != color))
993 return;
994
995 /* are there still in-flight works? */
996 if (cwq->nr_in_flight[color])
997 return;
998
999 /* this cwq is done, clear flush_color */
1000 cwq->flush_color = -1;
1001
1002 /*
1003 * If this was the last cwq, wake up the first flusher. It
1004 * will handle the rest.
1005 */
1006 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
1007 complete(&cwq->wq->first_flusher->done);
1008}
1009
Tejun Heo36e227d2012-08-03 10:30:46 -07001010/**
Tejun Heobbb68df2012-08-03 10:30:46 -07001011 * try_to_grab_pending - steal work item from worklist and disable irq
Tejun Heo36e227d2012-08-03 10:30:46 -07001012 * @work: work item to steal
1013 * @is_dwork: @work is a delayed_work
Tejun Heobbb68df2012-08-03 10:30:46 -07001014 * @flags: place to store irq state
Tejun Heo36e227d2012-08-03 10:30:46 -07001015 *
1016 * Try to grab PENDING bit of @work. This function can handle @work in any
1017 * stable state - idle, on timer or on worklist. Return values are
1018 *
1019 * 1 if @work was pending and we successfully stole PENDING
1020 * 0 if @work was idle and we claimed PENDING
1021 * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry
Tejun Heobbb68df2012-08-03 10:30:46 -07001022 * -ENOENT if someone else is canceling @work, this state may persist
1023 * for arbitrarily long
Tejun Heo36e227d2012-08-03 10:30:46 -07001024 *
Tejun Heobbb68df2012-08-03 10:30:46 -07001025 * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001026 * interrupted while holding PENDING and @work off queue, irq must be
1027 * disabled on entry. This, combined with delayed_work->timer being
1028 * irqsafe, ensures that we return -EAGAIN for finite short period of time.
Tejun Heobbb68df2012-08-03 10:30:46 -07001029 *
1030 * On successful return, >= 0, irq is disabled and the caller is
1031 * responsible for releasing it using local_irq_restore(*@flags).
1032 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001033 * This function is safe to call from any context including IRQ handler.
Tejun Heobf4ede02012-08-03 10:30:46 -07001034 */
Tejun Heobbb68df2012-08-03 10:30:46 -07001035static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
1036 unsigned long *flags)
Tejun Heobf4ede02012-08-03 10:30:46 -07001037{
1038 struct global_cwq *gcwq;
Tejun Heobf4ede02012-08-03 10:30:46 -07001039
Tejun Heobbb68df2012-08-03 10:30:46 -07001040 local_irq_save(*flags);
1041
Tejun Heo36e227d2012-08-03 10:30:46 -07001042 /* try to steal the timer if it exists */
1043 if (is_dwork) {
1044 struct delayed_work *dwork = to_delayed_work(work);
1045
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001046 /*
1047 * dwork->timer is irqsafe. If del_timer() fails, it's
1048 * guaranteed that the timer is not queued anywhere and not
1049 * running on the local CPU.
1050 */
Tejun Heo36e227d2012-08-03 10:30:46 -07001051 if (likely(del_timer(&dwork->timer)))
1052 return 1;
1053 }
1054
1055 /* try to claim PENDING the normal way */
Tejun Heobf4ede02012-08-03 10:30:46 -07001056 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1057 return 0;
1058
1059 /*
1060 * The queueing is in progress, or it is already queued. Try to
1061 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1062 */
1063 gcwq = get_work_gcwq(work);
1064 if (!gcwq)
Tejun Heobbb68df2012-08-03 10:30:46 -07001065 goto fail;
Tejun Heobf4ede02012-08-03 10:30:46 -07001066
Tejun Heobbb68df2012-08-03 10:30:46 -07001067 spin_lock(&gcwq->lock);
Tejun Heobf4ede02012-08-03 10:30:46 -07001068 if (!list_empty(&work->entry)) {
1069 /*
1070 * This work is queued, but perhaps we locked the wrong gcwq.
1071 * In that case we must see the new value after rmb(), see
1072 * insert_work()->wmb().
1073 */
1074 smp_rmb();
1075 if (gcwq == get_work_gcwq(work)) {
1076 debug_work_deactivate(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001077
1078 /*
1079 * A delayed work item cannot be grabbed directly
1080 * because it might have linked NO_COLOR work items
1081 * which, if left on the delayed_list, will confuse
1082 * cwq->nr_active management later on and cause
1083 * stall. Make sure the work item is activated
1084 * before grabbing.
1085 */
1086 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
1087 cwq_activate_delayed_work(work);
1088
Tejun Heobf4ede02012-08-03 10:30:46 -07001089 list_del_init(&work->entry);
1090 cwq_dec_nr_in_flight(get_work_cwq(work),
Lai Jiangshanb3f9f402012-09-18 10:40:00 -07001091 get_work_color(work));
Tejun Heo36e227d2012-08-03 10:30:46 -07001092
Tejun Heobbb68df2012-08-03 10:30:46 -07001093 spin_unlock(&gcwq->lock);
Tejun Heo36e227d2012-08-03 10:30:46 -07001094 return 1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001095 }
1096 }
Tejun Heobbb68df2012-08-03 10:30:46 -07001097 spin_unlock(&gcwq->lock);
1098fail:
1099 local_irq_restore(*flags);
1100 if (work_is_canceling(work))
1101 return -ENOENT;
1102 cpu_relax();
Tejun Heo36e227d2012-08-03 10:30:46 -07001103 return -EAGAIN;
Tejun Heobf4ede02012-08-03 10:30:46 -07001104}
1105
1106/**
Tejun Heo7e116292010-06-29 10:07:13 +02001107 * insert_work - insert a work into gcwq
Tejun Heo4690c4a2010-06-29 10:07:10 +02001108 * @cwq: cwq @work belongs to
1109 * @work: work to insert
1110 * @head: insertion point
1111 * @extra_flags: extra WORK_STRUCT_* flags to set
1112 *
Tejun Heo7e116292010-06-29 10:07:13 +02001113 * Insert @work which belongs to @cwq into @gcwq after @head.
1114 * @extra_flags is or'd to work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +02001115 *
1116 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001117 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02001118 */
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001119static void insert_work(struct cpu_workqueue_struct *cwq,
Tejun Heo4690c4a2010-06-29 10:07:10 +02001120 struct work_struct *work, struct list_head *head,
1121 unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001122{
Tejun Heo63d95a92012-07-12 14:46:37 -07001123 struct worker_pool *pool = cwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01001124
Tejun Heo4690c4a2010-06-29 10:07:10 +02001125 /* we own @work, set data and link */
Tejun Heo7a22ad72010-06-29 10:07:13 +02001126 set_work_cwq(work, cwq, extra_flags);
Tejun Heo4690c4a2010-06-29 10:07:10 +02001127
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001128 /*
1129 * Ensure that we get the right work->data if we see the
1130 * result of list_add() below, see try_to_grab_pending().
1131 */
1132 smp_wmb();
Tejun Heo4690c4a2010-06-29 10:07:10 +02001133
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -07001134 list_add_tail(&work->entry, head);
Tejun Heoe22bee72010-06-29 10:07:14 +02001135
1136 /*
1137 * Ensure either worker_sched_deactivated() sees the above
1138 * list_add_tail() or we see zero nr_running to avoid workers
1139 * lying around lazily while there are works to be processed.
1140 */
1141 smp_mb();
1142
Tejun Heo63d95a92012-07-12 14:46:37 -07001143 if (__need_more_worker(pool))
1144 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001145}
1146
Tejun Heoc8efcc22010-12-20 19:32:04 +01001147/*
1148 * Test whether @work is being queued from another work executing on the
1149 * same workqueue. This is rather expensive and should only be used from
1150 * cold paths.
1151 */
1152static bool is_chained_work(struct workqueue_struct *wq)
1153{
1154 unsigned long flags;
1155 unsigned int cpu;
1156
1157 for_each_gcwq_cpu(cpu) {
1158 struct global_cwq *gcwq = get_gcwq(cpu);
1159 struct worker *worker;
1160 struct hlist_node *pos;
1161 int i;
1162
1163 spin_lock_irqsave(&gcwq->lock, flags);
1164 for_each_busy_worker(worker, i, pos, gcwq) {
1165 if (worker->task != current)
1166 continue;
1167 spin_unlock_irqrestore(&gcwq->lock, flags);
1168 /*
1169 * I'm @worker, no locking necessary. See if @work
1170 * is headed to the same workqueue.
1171 */
1172 return worker->current_cwq->wq == wq;
1173 }
1174 spin_unlock_irqrestore(&gcwq->lock, flags);
1175 }
1176 return false;
1177}
1178
Tejun Heo4690c4a2010-06-29 10:07:10 +02001179static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 struct work_struct *work)
1181{
Tejun Heo502ca9d2010-06-29 10:07:13 +02001182 struct global_cwq *gcwq;
1183 struct cpu_workqueue_struct *cwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001184 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001185 unsigned int work_flags;
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001186 unsigned int req_cpu = cpu;
Tejun Heo8930cab2012-08-03 10:30:45 -07001187
1188 /*
1189 * While a work item is PENDING && off queue, a task trying to
1190 * steal the PENDING will busy-loop waiting for it to either get
1191 * queued or lose PENDING. Grabbing PENDING and queueing should
1192 * happen with IRQ disabled.
1193 */
1194 WARN_ON_ONCE(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001196 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001197
Tejun Heoc8efcc22010-12-20 19:32:04 +01001198 /* if dying, only works from the same workqueue are allowed */
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02001199 if (unlikely(wq->flags & WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001200 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001201 return;
1202
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001203 /* determine gcwq to use */
1204 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001205 struct global_cwq *last_gcwq;
1206
Tejun Heo57469822012-08-03 10:30:45 -07001207 if (cpu == WORK_CPU_UNBOUND)
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001208 cpu = raw_smp_processor_id();
1209
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001210 /*
Tejun Heodbf25762012-08-20 14:51:23 -07001211 * It's multi cpu. If @work was previously on a different
1212 * cpu, it might still be running there, in which case the
1213 * work needs to be queued on that cpu to guarantee
1214 * non-reentrancy.
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001215 */
Tejun Heo502ca9d2010-06-29 10:07:13 +02001216 gcwq = get_gcwq(cpu);
Tejun Heodbf25762012-08-20 14:51:23 -07001217 last_gcwq = get_work_gcwq(work);
1218
1219 if (last_gcwq && last_gcwq != gcwq) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001220 struct worker *worker;
1221
Tejun Heo8930cab2012-08-03 10:30:45 -07001222 spin_lock(&last_gcwq->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001223
1224 worker = find_worker_executing_work(last_gcwq, work);
1225
1226 if (worker && worker->current_cwq->wq == wq)
1227 gcwq = last_gcwq;
1228 else {
1229 /* meh... not running there, queue here */
Tejun Heo8930cab2012-08-03 10:30:45 -07001230 spin_unlock(&last_gcwq->lock);
1231 spin_lock(&gcwq->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001232 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001233 } else {
1234 spin_lock(&gcwq->lock);
1235 }
Tejun Heof3421792010-07-02 10:03:51 +02001236 } else {
1237 gcwq = get_gcwq(WORK_CPU_UNBOUND);
Tejun Heo8930cab2012-08-03 10:30:45 -07001238 spin_lock(&gcwq->lock);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001239 }
1240
1241 /* gcwq determined, get cwq and queue */
1242 cwq = get_cwq(gcwq->cpu, wq);
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001243 trace_workqueue_queue_work(req_cpu, cwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001244
Dan Carpenterf5b25522012-04-13 22:06:58 +03001245 if (WARN_ON(!list_empty(&work->entry))) {
Tejun Heo8930cab2012-08-03 10:30:45 -07001246 spin_unlock(&gcwq->lock);
Dan Carpenterf5b25522012-04-13 22:06:58 +03001247 return;
1248 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001249
Tejun Heo73f53c42010-06-29 10:07:11 +02001250 cwq->nr_in_flight[cwq->work_color]++;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001251 work_flags = work_color_to_flags(cwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001252
1253 if (likely(cwq->nr_active < cwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001254 trace_workqueue_activate_work(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001255 cwq->nr_active++;
Tejun Heo32704762012-07-13 22:16:45 -07001256 worklist = &cwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001257 } else {
1258 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001259 worklist = &cwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001260 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001261
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001262 insert_work(cwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001263
Tejun Heo8930cab2012-08-03 10:30:45 -07001264 spin_unlock(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265}
1266
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001267/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07001268 * queue_work_on - queue work on specific cpu
1269 * @cpu: CPU number to execute work on
1270 * @wq: workqueue to use
1271 * @work: work to queue
1272 *
Tejun Heod4283e92012-08-03 10:30:44 -07001273 * Returns %false if @work was already on a queue, %true otherwise.
Zhang Ruic1a220e2008-07-23 21:28:39 -07001274 *
1275 * We queue the work to a specific CPU, the caller must ensure it
1276 * can't go away.
1277 */
Tejun Heod4283e92012-08-03 10:30:44 -07001278bool queue_work_on(int cpu, struct workqueue_struct *wq,
1279 struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07001280{
Tejun Heod4283e92012-08-03 10:30:44 -07001281 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001282 unsigned long flags;
1283
1284 local_irq_save(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001285
Tejun Heo22df02b2010-06-29 10:07:10 +02001286 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001287 __queue_work(cpu, wq, work);
Tejun Heod4283e92012-08-03 10:30:44 -07001288 ret = true;
Zhang Ruic1a220e2008-07-23 21:28:39 -07001289 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001290
1291 local_irq_restore(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001292 return ret;
1293}
1294EXPORT_SYMBOL_GPL(queue_work_on);
1295
Tejun Heo0a13c002012-08-03 10:30:44 -07001296/**
1297 * queue_work - queue work on a workqueue
1298 * @wq: workqueue to use
1299 * @work: work to queue
1300 *
Tejun Heod4283e92012-08-03 10:30:44 -07001301 * Returns %false if @work was already on a queue, %true otherwise.
Tejun Heo0a13c002012-08-03 10:30:44 -07001302 *
1303 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1304 * it can be processed by another CPU.
1305 */
Tejun Heod4283e92012-08-03 10:30:44 -07001306bool queue_work(struct workqueue_struct *wq, struct work_struct *work)
Tejun Heo0a13c002012-08-03 10:30:44 -07001307{
Tejun Heo57469822012-08-03 10:30:45 -07001308 return queue_work_on(WORK_CPU_UNBOUND, wq, work);
Tejun Heo0a13c002012-08-03 10:30:44 -07001309}
1310EXPORT_SYMBOL_GPL(queue_work);
1311
Tejun Heod8e794d2012-08-03 10:30:45 -07001312void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
David Howells52bad642006-11-22 14:54:01 +00001314 struct delayed_work *dwork = (struct delayed_work *)__data;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001315 struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001317 /* should have been called from irqsafe timer with irq already off */
Tejun Heo12650572012-08-08 09:38:42 -07001318 __queue_work(dwork->cpu, cwq->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319}
Tejun Heod8e794d2012-08-03 10:30:45 -07001320EXPORT_SYMBOL_GPL(delayed_work_timer_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001322static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
1323 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001325 struct timer_list *timer = &dwork->timer;
1326 struct work_struct *work = &dwork->work;
1327 unsigned int lcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001329 WARN_ON_ONCE(timer->function != delayed_work_timer_fn ||
1330 timer->data != (unsigned long)dwork);
Tejun Heofc4b5142012-12-04 07:40:39 -08001331 WARN_ON_ONCE(timer_pending(timer));
1332 WARN_ON_ONCE(!list_empty(&work->entry));
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001333
Tejun Heo8852aac2012-12-01 16:23:42 -08001334 /*
1335 * If @delay is 0, queue @dwork->work immediately. This is for
1336 * both optimization and correctness. The earliest @timer can
1337 * expire is on the closest next tick and delayed_work users depend
1338 * on that there's no such delay when @delay is 0.
1339 */
1340 if (!delay) {
1341 __queue_work(cpu, wq, &dwork->work);
1342 return;
1343 }
1344
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001345 timer_stats_timer_set_start_info(&dwork->timer);
1346
1347 /*
1348 * This stores cwq for the moment, for the timer_fn. Note that the
1349 * work's gcwq is preserved to allow reentrance detection for
1350 * delayed works.
1351 */
1352 if (!(wq->flags & WQ_UNBOUND)) {
1353 struct global_cwq *gcwq = get_work_gcwq(work);
1354
Joonsoo Kime42986d2012-08-15 23:25:38 +09001355 /*
1356 * If we cannot get the last gcwq from @work directly,
1357 * select the last CPU such that it avoids unnecessarily
1358 * triggering non-reentrancy check in __queue_work().
1359 */
1360 lcpu = cpu;
1361 if (gcwq)
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001362 lcpu = gcwq->cpu;
Joonsoo Kime42986d2012-08-15 23:25:38 +09001363 if (lcpu == WORK_CPU_UNBOUND)
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001364 lcpu = raw_smp_processor_id();
1365 } else {
1366 lcpu = WORK_CPU_UNBOUND;
1367 }
1368
1369 set_work_cwq(work, get_cwq(lcpu, wq), 0);
1370
Tejun Heo12650572012-08-08 09:38:42 -07001371 dwork->cpu = cpu;
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001372 timer->expires = jiffies + delay;
1373
1374 if (unlikely(cpu != WORK_CPU_UNBOUND))
1375 add_timer_on(timer, cpu);
1376 else
1377 add_timer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378}
1379
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001380/**
1381 * queue_delayed_work_on - queue work on specific CPU after delay
1382 * @cpu: CPU number to execute work on
1383 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001384 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001385 * @delay: number of jiffies to wait before queueing
1386 *
Tejun Heo715f1302012-08-03 10:30:46 -07001387 * Returns %false if @work was already on a queue, %true otherwise. If
1388 * @delay is zero and @dwork is idle, it will be scheduled for immediate
1389 * execution.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001390 */
Tejun Heod4283e92012-08-03 10:30:44 -07001391bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
1392 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001393{
David Howells52bad642006-11-22 14:54:01 +00001394 struct work_struct *work = &dwork->work;
Tejun Heod4283e92012-08-03 10:30:44 -07001395 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001396 unsigned long flags;
1397
1398 /* read the comment in __queue_work() */
1399 local_irq_save(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001400
Tejun Heo22df02b2010-06-29 10:07:10 +02001401 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001402 __queue_delayed_work(cpu, wq, dwork, delay);
Tejun Heod4283e92012-08-03 10:30:44 -07001403 ret = true;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001404 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001405
1406 local_irq_restore(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001407 return ret;
1408}
Dave Jonesae90dd52006-06-30 01:40:45 -04001409EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Tejun Heoc8e55f32010-06-29 10:07:12 +02001411/**
Tejun Heo0a13c002012-08-03 10:30:44 -07001412 * queue_delayed_work - queue work on a workqueue after delay
1413 * @wq: workqueue to use
1414 * @dwork: delayable work to queue
1415 * @delay: number of jiffies to wait before queueing
1416 *
Tejun Heo715f1302012-08-03 10:30:46 -07001417 * Equivalent to queue_delayed_work_on() but tries to use the local CPU.
Tejun Heo0a13c002012-08-03 10:30:44 -07001418 */
Tejun Heod4283e92012-08-03 10:30:44 -07001419bool queue_delayed_work(struct workqueue_struct *wq,
Tejun Heo0a13c002012-08-03 10:30:44 -07001420 struct delayed_work *dwork, unsigned long delay)
1421{
Tejun Heo57469822012-08-03 10:30:45 -07001422 return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
Tejun Heo0a13c002012-08-03 10:30:44 -07001423}
1424EXPORT_SYMBOL_GPL(queue_delayed_work);
1425
1426/**
Tejun Heo8376fe22012-08-03 10:30:47 -07001427 * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
1428 * @cpu: CPU number to execute work on
1429 * @wq: workqueue to use
1430 * @dwork: work to queue
1431 * @delay: number of jiffies to wait before queueing
1432 *
1433 * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
1434 * modify @dwork's timer so that it expires after @delay. If @delay is
1435 * zero, @work is guaranteed to be scheduled immediately regardless of its
1436 * current state.
1437 *
1438 * Returns %false if @dwork was idle and queued, %true if @dwork was
1439 * pending and its timer was modified.
1440 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001441 * This function is safe to call from any context including IRQ handler.
Tejun Heo8376fe22012-08-03 10:30:47 -07001442 * See try_to_grab_pending() for details.
1443 */
1444bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
1445 struct delayed_work *dwork, unsigned long delay)
1446{
1447 unsigned long flags;
1448 int ret;
1449
1450 do {
1451 ret = try_to_grab_pending(&dwork->work, true, &flags);
1452 } while (unlikely(ret == -EAGAIN));
1453
1454 if (likely(ret >= 0)) {
1455 __queue_delayed_work(cpu, wq, dwork, delay);
1456 local_irq_restore(flags);
1457 }
1458
1459 /* -ENOENT from try_to_grab_pending() becomes %true */
1460 return ret;
1461}
1462EXPORT_SYMBOL_GPL(mod_delayed_work_on);
1463
1464/**
1465 * mod_delayed_work - modify delay of or queue a delayed work
1466 * @wq: workqueue to use
1467 * @dwork: work to queue
1468 * @delay: number of jiffies to wait before queueing
1469 *
1470 * mod_delayed_work_on() on local CPU.
1471 */
1472bool mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dwork,
1473 unsigned long delay)
1474{
1475 return mod_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
1476}
1477EXPORT_SYMBOL_GPL(mod_delayed_work);
1478
1479/**
Tejun Heoc8e55f32010-06-29 10:07:12 +02001480 * worker_enter_idle - enter idle state
1481 * @worker: worker which is entering idle state
1482 *
1483 * @worker is entering idle state. Update stats and idle timer if
1484 * necessary.
1485 *
1486 * LOCKING:
1487 * spin_lock_irq(gcwq->lock).
1488 */
1489static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001491 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Tejun Heoc8e55f32010-06-29 10:07:12 +02001493 BUG_ON(worker->flags & WORKER_IDLE);
1494 BUG_ON(!list_empty(&worker->entry) &&
1495 (worker->hentry.next || worker->hentry.pprev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Tejun Heocb444762010-07-02 10:03:50 +02001497 /* can't use worker_set_flags(), also called from start_worker() */
1498 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001499 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001500 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001501
Tejun Heoc8e55f32010-06-29 10:07:12 +02001502 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001503 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001504
Tejun Heo628c78e2012-07-17 12:39:27 -07001505 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1506 mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
Tejun Heocb444762010-07-02 10:03:50 +02001507
Tejun Heo544ecf32012-05-14 15:04:50 -07001508 /*
Tejun Heo628c78e2012-07-17 12:39:27 -07001509 * Sanity check nr_running. Because gcwq_unbind_fn() releases
1510 * gcwq->lock between setting %WORKER_UNBOUND and zapping
1511 * nr_running, the warning may trigger spuriously. Check iff
1512 * unbind is not in progress.
Tejun Heo544ecf32012-05-14 15:04:50 -07001513 */
Tejun Heo24647572013-01-24 11:01:33 -08001514 WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001515 pool->nr_workers == pool->nr_idle &&
Tejun Heo63d95a92012-07-12 14:46:37 -07001516 atomic_read(get_pool_nr_running(pool)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517}
1518
Tejun Heoc8e55f32010-06-29 10:07:12 +02001519/**
1520 * worker_leave_idle - leave idle state
1521 * @worker: worker which is leaving idle state
1522 *
1523 * @worker is leaving idle state. Update stats.
1524 *
1525 * LOCKING:
1526 * spin_lock_irq(gcwq->lock).
1527 */
1528static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001530 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Tejun Heoc8e55f32010-06-29 10:07:12 +02001532 BUG_ON(!(worker->flags & WORKER_IDLE));
Tejun Heod302f012010-06-29 10:07:13 +02001533 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001534 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001535 list_del_init(&worker->entry);
1536}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Tejun Heoe22bee72010-06-29 10:07:14 +02001538/**
1539 * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq
1540 * @worker: self
1541 *
1542 * Works which are scheduled while the cpu is online must at least be
1543 * scheduled to a worker which is bound to the cpu so that if they are
1544 * flushed from cpu callbacks while cpu is going down, they are
1545 * guaranteed to execute on the cpu.
1546 *
1547 * This function is to be used by rogue workers and rescuers to bind
1548 * themselves to the target cpu and may race with cpu going down or
1549 * coming online. kthread_bind() can't be used because it may put the
1550 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1551 * verbatim as it's best effort and blocking and gcwq may be
1552 * [dis]associated in the meantime.
1553 *
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001554 * This function tries set_cpus_allowed() and locks gcwq and verifies the
Tejun Heo24647572013-01-24 11:01:33 -08001555 * binding against %POOL_DISASSOCIATED which is set during
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001556 * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1557 * enters idle state or fetches works without dropping lock, it can
1558 * guarantee the scheduling requirement described in the first paragraph.
Tejun Heoe22bee72010-06-29 10:07:14 +02001559 *
1560 * CONTEXT:
1561 * Might sleep. Called without any lock but returns with gcwq->lock
1562 * held.
1563 *
1564 * RETURNS:
1565 * %true if the associated gcwq is online (@worker is successfully
1566 * bound), %false if offline.
1567 */
1568static bool worker_maybe_bind_and_lock(struct worker *worker)
Namhyung Kim972fa1c2010-08-22 23:19:43 +09001569__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001570{
Tejun Heo24647572013-01-24 11:01:33 -08001571 struct worker_pool *pool = worker->pool;
1572 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001573 struct task_struct *task = worker->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Tejun Heoe22bee72010-06-29 10:07:14 +02001575 while (true) {
1576 /*
1577 * The following call may fail, succeed or succeed
1578 * without actually migrating the task to the cpu if
1579 * it races with cpu hotunplug operation. Verify
Tejun Heo24647572013-01-24 11:01:33 -08001580 * against POOL_DISASSOCIATED.
Tejun Heoe22bee72010-06-29 10:07:14 +02001581 */
Tejun Heo24647572013-01-24 11:01:33 -08001582 if (!(pool->flags & POOL_DISASSOCIATED))
Tejun Heof3421792010-07-02 10:03:51 +02001583 set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu));
Oleg Nesterov85f41862007-05-09 02:34:20 -07001584
Tejun Heoe22bee72010-06-29 10:07:14 +02001585 spin_lock_irq(&gcwq->lock);
Tejun Heo24647572013-01-24 11:01:33 -08001586 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heoe22bee72010-06-29 10:07:14 +02001587 return false;
1588 if (task_cpu(task) == gcwq->cpu &&
1589 cpumask_equal(&current->cpus_allowed,
1590 get_cpu_mask(gcwq->cpu)))
1591 return true;
1592 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001593
Tejun Heo5035b202011-04-29 18:08:37 +02001594 /*
1595 * We've raced with CPU hot[un]plug. Give it a breather
1596 * and retry migration. cond_resched() is required here;
1597 * otherwise, we might deadlock against cpu_stop trying to
1598 * bring down the CPU on non-preemptive kernel.
1599 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001600 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001601 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001602 }
1603}
1604
1605/*
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001606 * Rebind an idle @worker to its CPU. worker_thread() will test
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001607 * list_empty(@worker->entry) before leaving idle and call this function.
Tejun Heo25511a42012-07-17 12:39:27 -07001608 */
1609static void idle_worker_rebind(struct worker *worker)
1610{
1611 struct global_cwq *gcwq = worker->pool->gcwq;
1612
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001613 /* CPU may go down again inbetween, clear UNBOUND only on success */
1614 if (worker_maybe_bind_and_lock(worker))
1615 worker_clr_flags(worker, WORKER_UNBOUND);
Tejun Heo25511a42012-07-17 12:39:27 -07001616
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001617 /* rebind complete, become available again */
1618 list_add(&worker->entry, &worker->pool->idle_list);
1619 spin_unlock_irq(&gcwq->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07001620}
1621
1622/*
1623 * Function for @worker->rebind.work used to rebind unbound busy workers to
Tejun Heo403c8212012-07-17 12:39:27 -07001624 * the associated cpu which is coming back online. This is scheduled by
1625 * cpu up but can race with other cpu hotplug operations and may be
1626 * executed twice without intervening cpu down.
Tejun Heoe22bee72010-06-29 10:07:14 +02001627 */
Tejun Heo25511a42012-07-17 12:39:27 -07001628static void busy_worker_rebind_fn(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001629{
1630 struct worker *worker = container_of(work, struct worker, rebind_work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001631 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001632
Lai Jiangshaneab6d822012-09-18 09:59:22 -07001633 if (worker_maybe_bind_and_lock(worker))
1634 worker_clr_flags(worker, WORKER_UNBOUND);
Tejun Heoe22bee72010-06-29 10:07:14 +02001635
1636 spin_unlock_irq(&gcwq->lock);
1637}
1638
Tejun Heo25511a42012-07-17 12:39:27 -07001639/**
1640 * rebind_workers - rebind all workers of a gcwq to the associated CPU
1641 * @gcwq: gcwq of interest
1642 *
1643 * @gcwq->cpu is coming online. Rebind all workers to the CPU. Rebinding
1644 * is different for idle and busy ones.
1645 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001646 * Idle ones will be removed from the idle_list and woken up. They will
1647 * add themselves back after completing rebind. This ensures that the
1648 * idle_list doesn't contain any unbound workers when re-bound busy workers
1649 * try to perform local wake-ups for concurrency management.
Tejun Heo25511a42012-07-17 12:39:27 -07001650 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001651 * Busy workers can rebind after they finish their current work items.
1652 * Queueing the rebind work item at the head of the scheduled list is
1653 * enough. Note that nr_running will be properly bumped as busy workers
1654 * rebind.
Tejun Heo25511a42012-07-17 12:39:27 -07001655 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001656 * On return, all non-manager workers are scheduled for rebind - see
1657 * manage_workers() for the manager special case. Any idle worker
1658 * including the manager will not appear on @idle_list until rebind is
1659 * complete, making local wake-ups safe.
Tejun Heo25511a42012-07-17 12:39:27 -07001660 */
1661static void rebind_workers(struct global_cwq *gcwq)
Tejun Heo25511a42012-07-17 12:39:27 -07001662{
Tejun Heo25511a42012-07-17 12:39:27 -07001663 struct worker_pool *pool;
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001664 struct worker *worker, *n;
Tejun Heo25511a42012-07-17 12:39:27 -07001665 struct hlist_node *pos;
1666 int i;
1667
1668 lockdep_assert_held(&gcwq->lock);
1669
1670 for_each_worker_pool(pool, gcwq)
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07001671 lockdep_assert_held(&pool->assoc_mutex);
Tejun Heo25511a42012-07-17 12:39:27 -07001672
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001673 /* dequeue and kick idle ones */
Tejun Heo25511a42012-07-17 12:39:27 -07001674 for_each_worker_pool(pool, gcwq) {
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001675 list_for_each_entry_safe(worker, n, &pool->idle_list, entry) {
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001676 /*
1677 * idle workers should be off @pool->idle_list
1678 * until rebind is complete to avoid receiving
1679 * premature local wake-ups.
1680 */
1681 list_del_init(&worker->entry);
Lai Jiangshan96e65302012-09-02 00:28:19 +08001682
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001683 /*
1684 * worker_thread() will see the above dequeuing
1685 * and call idle_worker_rebind().
1686 */
Tejun Heo25511a42012-07-17 12:39:27 -07001687 wake_up_process(worker->task);
1688 }
1689 }
1690
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001691 /* rebind busy workers */
Tejun Heo25511a42012-07-17 12:39:27 -07001692 for_each_busy_worker(worker, i, pos, gcwq) {
1693 struct work_struct *rebind_work = &worker->rebind_work;
Joonsoo Kime2b6a6d2012-08-15 23:25:40 +09001694 struct workqueue_struct *wq;
Tejun Heo25511a42012-07-17 12:39:27 -07001695
1696 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
1697 work_data_bits(rebind_work)))
1698 continue;
1699
Tejun Heo25511a42012-07-17 12:39:27 -07001700 debug_work_activate(rebind_work);
Tejun Heo90beca52012-09-04 23:12:33 -07001701
Joonsoo Kime2b6a6d2012-08-15 23:25:40 +09001702 /*
1703 * wq doesn't really matter but let's keep @worker->pool
1704 * and @cwq->pool consistent for sanity.
1705 */
Tejun Heoe34cdddb2013-01-24 11:01:33 -08001706 if (std_worker_pool_pri(worker->pool))
Joonsoo Kime2b6a6d2012-08-15 23:25:40 +09001707 wq = system_highpri_wq;
1708 else
1709 wq = system_wq;
Tejun Heoec588152012-09-04 23:16:32 -07001710
Joonsoo Kime2b6a6d2012-08-15 23:25:40 +09001711 insert_work(get_cwq(gcwq->cpu, wq), rebind_work,
1712 worker->scheduled.next,
1713 work_color_to_flags(WORK_NO_COLOR));
Tejun Heoec588152012-09-04 23:16:32 -07001714 }
Tejun Heo25511a42012-07-17 12:39:27 -07001715}
1716
Tejun Heoc34056a2010-06-29 10:07:11 +02001717static struct worker *alloc_worker(void)
1718{
1719 struct worker *worker;
1720
1721 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001722 if (worker) {
1723 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001724 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heo25511a42012-07-17 12:39:27 -07001725 INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn);
Tejun Heoe22bee72010-06-29 10:07:14 +02001726 /* on creation a worker is in !idle && prep state */
1727 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001728 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001729 return worker;
1730}
1731
1732/**
1733 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001734 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001735 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001736 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001737 * can be started by calling start_worker() or destroyed using
1738 * destroy_worker().
1739 *
1740 * CONTEXT:
1741 * Might sleep. Does GFP_KERNEL allocations.
1742 *
1743 * RETURNS:
1744 * Pointer to the newly created worker.
1745 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001746static struct worker *create_worker(struct worker_pool *pool)
Tejun Heoc34056a2010-06-29 10:07:11 +02001747{
Tejun Heo63d95a92012-07-12 14:46:37 -07001748 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe34cdddb2013-01-24 11:01:33 -08001749 const char *pri = std_worker_pool_pri(pool) ? "H" : "";
Tejun Heoc34056a2010-06-29 10:07:11 +02001750 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001751 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001752
Tejun Heo8b03ae32010-06-29 10:07:12 +02001753 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001754 while (ida_get_new(&pool->worker_ida, &id)) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001755 spin_unlock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001756 if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001757 goto fail;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001758 spin_lock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001759 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02001760 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001761
1762 worker = alloc_worker();
1763 if (!worker)
1764 goto fail;
1765
Tejun Heobd7bdd42012-07-12 14:46:37 -07001766 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001767 worker->id = id;
1768
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001769 if (gcwq->cpu != WORK_CPU_UNBOUND)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001770 worker->task = kthread_create_on_node(worker_thread,
Tejun Heo32704762012-07-13 22:16:45 -07001771 worker, cpu_to_node(gcwq->cpu),
1772 "kworker/%u:%d%s", gcwq->cpu, id, pri);
Tejun Heof3421792010-07-02 10:03:51 +02001773 else
1774 worker->task = kthread_create(worker_thread, worker,
Tejun Heo32704762012-07-13 22:16:45 -07001775 "kworker/u:%d%s", id, pri);
Tejun Heoc34056a2010-06-29 10:07:11 +02001776 if (IS_ERR(worker->task))
1777 goto fail;
1778
Tejun Heoe34cdddb2013-01-24 11:01:33 -08001779 if (std_worker_pool_pri(pool))
Tejun Heo32704762012-07-13 22:16:45 -07001780 set_user_nice(worker->task, HIGHPRI_NICE_LEVEL);
1781
Tejun Heodb7bccf2010-06-29 10:07:12 +02001782 /*
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001783 * Determine CPU binding of the new worker depending on
Tejun Heo24647572013-01-24 11:01:33 -08001784 * %POOL_DISASSOCIATED. The caller is responsible for ensuring the
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001785 * flag remains stable across this function. See the comments
1786 * above the flag definition for details.
1787 *
1788 * As an unbound worker may later become a regular one if CPU comes
1789 * online, make sure every worker has %PF_THREAD_BOUND set.
Tejun Heodb7bccf2010-06-29 10:07:12 +02001790 */
Tejun Heo24647572013-01-24 11:01:33 -08001791 if (!(pool->flags & POOL_DISASSOCIATED)) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001792 kthread_bind(worker->task, gcwq->cpu);
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001793 } else {
Tejun Heodb7bccf2010-06-29 10:07:12 +02001794 worker->task->flags |= PF_THREAD_BOUND;
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001795 worker->flags |= WORKER_UNBOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001797
Tejun Heoc34056a2010-06-29 10:07:11 +02001798 return worker;
1799fail:
1800 if (id >= 0) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001801 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001802 ida_remove(&pool->worker_ida, id);
Tejun Heo8b03ae32010-06-29 10:07:12 +02001803 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001804 }
1805 kfree(worker);
1806 return NULL;
1807}
1808
1809/**
1810 * start_worker - start a newly created worker
1811 * @worker: worker to start
1812 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001813 * Make the gcwq aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001814 *
1815 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001816 * spin_lock_irq(gcwq->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001817 */
1818static void start_worker(struct worker *worker)
1819{
Tejun Heocb444762010-07-02 10:03:50 +02001820 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001821 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001822 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001823 wake_up_process(worker->task);
1824}
1825
1826/**
1827 * destroy_worker - destroy a workqueue worker
1828 * @worker: worker to be destroyed
1829 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001830 * Destroy @worker and adjust @gcwq stats accordingly.
1831 *
1832 * CONTEXT:
1833 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001834 */
1835static void destroy_worker(struct worker *worker)
1836{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001837 struct worker_pool *pool = worker->pool;
1838 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001839 int id = worker->id;
1840
1841 /* sanity check frenzy */
1842 BUG_ON(worker->current_work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001843 BUG_ON(!list_empty(&worker->scheduled));
Tejun Heoc34056a2010-06-29 10:07:11 +02001844
Tejun Heoc8e55f32010-06-29 10:07:12 +02001845 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001846 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001847 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001848 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001849
1850 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001851 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001852
1853 spin_unlock_irq(&gcwq->lock);
1854
Tejun Heoc34056a2010-06-29 10:07:11 +02001855 kthread_stop(worker->task);
1856 kfree(worker);
1857
Tejun Heo8b03ae32010-06-29 10:07:12 +02001858 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001859 ida_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001860}
1861
Tejun Heo63d95a92012-07-12 14:46:37 -07001862static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001863{
Tejun Heo63d95a92012-07-12 14:46:37 -07001864 struct worker_pool *pool = (void *)__pool;
1865 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001866
1867 spin_lock_irq(&gcwq->lock);
1868
Tejun Heo63d95a92012-07-12 14:46:37 -07001869 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001870 struct worker *worker;
1871 unsigned long expires;
1872
1873 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001874 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001875 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1876
1877 if (time_before(jiffies, expires))
Tejun Heo63d95a92012-07-12 14:46:37 -07001878 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001879 else {
1880 /* it's been idle for too long, wake up manager */
Tejun Heo11ebea52012-07-12 14:46:37 -07001881 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -07001882 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001883 }
1884 }
1885
1886 spin_unlock_irq(&gcwq->lock);
1887}
1888
1889static bool send_mayday(struct work_struct *work)
1890{
1891 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1892 struct workqueue_struct *wq = cwq->wq;
Tejun Heof3421792010-07-02 10:03:51 +02001893 unsigned int cpu;
Tejun Heoe22bee72010-06-29 10:07:14 +02001894
1895 if (!(wq->flags & WQ_RESCUER))
1896 return false;
1897
1898 /* mayday mayday mayday */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001899 cpu = cwq->pool->gcwq->cpu;
Tejun Heof3421792010-07-02 10:03:51 +02001900 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1901 if (cpu == WORK_CPU_UNBOUND)
1902 cpu = 0;
Tejun Heof2e005a2010-07-20 15:59:09 +02001903 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001904 wake_up_process(wq->rescuer->task);
1905 return true;
1906}
1907
Tejun Heo63d95a92012-07-12 14:46:37 -07001908static void gcwq_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001909{
Tejun Heo63d95a92012-07-12 14:46:37 -07001910 struct worker_pool *pool = (void *)__pool;
1911 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001912 struct work_struct *work;
1913
1914 spin_lock_irq(&gcwq->lock);
1915
Tejun Heo63d95a92012-07-12 14:46:37 -07001916 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001917 /*
1918 * We've been trying to create a new worker but
1919 * haven't been successful. We might be hitting an
1920 * allocation deadlock. Send distress signals to
1921 * rescuers.
1922 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001923 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001924 send_mayday(work);
1925 }
1926
1927 spin_unlock_irq(&gcwq->lock);
1928
Tejun Heo63d95a92012-07-12 14:46:37 -07001929 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001930}
1931
1932/**
1933 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001934 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001935 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001936 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001937 * have at least one idle worker on return from this function. If
1938 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001939 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001940 * possible allocation deadlock.
1941 *
1942 * On return, need_to_create_worker() is guaranteed to be false and
1943 * may_start_working() true.
1944 *
1945 * LOCKING:
1946 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1947 * multiple times. Does GFP_KERNEL allocations. Called only from
1948 * manager.
1949 *
1950 * RETURNS:
1951 * false if no action was taken and gcwq->lock stayed locked, true
1952 * otherwise.
1953 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001954static bool maybe_create_worker(struct worker_pool *pool)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001955__releases(&gcwq->lock)
1956__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001957{
Tejun Heo63d95a92012-07-12 14:46:37 -07001958 struct global_cwq *gcwq = pool->gcwq;
1959
1960 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001961 return false;
1962restart:
Tejun Heo9f9c2362010-07-14 11:31:20 +02001963 spin_unlock_irq(&gcwq->lock);
1964
Tejun Heoe22bee72010-06-29 10:07:14 +02001965 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07001966 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001967
1968 while (true) {
1969 struct worker *worker;
1970
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001971 worker = create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001972 if (worker) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001973 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001974 spin_lock_irq(&gcwq->lock);
1975 start_worker(worker);
Tejun Heo63d95a92012-07-12 14:46:37 -07001976 BUG_ON(need_to_create_worker(pool));
Tejun Heoe22bee72010-06-29 10:07:14 +02001977 return true;
1978 }
1979
Tejun Heo63d95a92012-07-12 14:46:37 -07001980 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001981 break;
1982
Tejun Heoe22bee72010-06-29 10:07:14 +02001983 __set_current_state(TASK_INTERRUPTIBLE);
1984 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001985
Tejun Heo63d95a92012-07-12 14:46:37 -07001986 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001987 break;
1988 }
1989
Tejun Heo63d95a92012-07-12 14:46:37 -07001990 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001991 spin_lock_irq(&gcwq->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07001992 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001993 goto restart;
1994 return true;
1995}
1996
1997/**
1998 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo63d95a92012-07-12 14:46:37 -07001999 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02002000 *
Tejun Heo63d95a92012-07-12 14:46:37 -07002001 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02002002 * IDLE_WORKER_TIMEOUT.
2003 *
2004 * LOCKING:
2005 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
2006 * multiple times. Called only from manager.
2007 *
2008 * RETURNS:
2009 * false if no action was taken and gcwq->lock stayed locked, true
2010 * otherwise.
2011 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002012static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02002013{
2014 bool ret = false;
2015
Tejun Heo63d95a92012-07-12 14:46:37 -07002016 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02002017 struct worker *worker;
2018 unsigned long expires;
2019
Tejun Heo63d95a92012-07-12 14:46:37 -07002020 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02002021 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
2022
2023 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07002024 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02002025 break;
2026 }
2027
2028 destroy_worker(worker);
2029 ret = true;
2030 }
2031
2032 return ret;
2033}
2034
2035/**
2036 * manage_workers - manage worker pool
2037 * @worker: self
2038 *
2039 * Assume the manager role and manage gcwq worker pool @worker belongs
2040 * to. At any given time, there can be only zero or one manager per
2041 * gcwq. The exclusion is handled automatically by this function.
2042 *
2043 * The caller can safely start processing works on false return. On
2044 * true return, it's guaranteed that need_to_create_worker() is false
2045 * and may_start_working() is true.
2046 *
2047 * CONTEXT:
2048 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
2049 * multiple times. Does GFP_KERNEL allocations.
2050 *
2051 * RETURNS:
2052 * false if no action was taken and gcwq->lock stayed locked, true if
2053 * some action was taken.
2054 */
2055static bool manage_workers(struct worker *worker)
2056{
Tejun Heo63d95a92012-07-12 14:46:37 -07002057 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002058 bool ret = false;
2059
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002060 if (pool->flags & POOL_MANAGING_WORKERS)
Tejun Heoe22bee72010-06-29 10:07:14 +02002061 return ret;
2062
Lai Jiangshan552a37e2012-09-10 10:03:33 -07002063 pool->flags |= POOL_MANAGING_WORKERS;
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002064
2065 /*
2066 * To simplify both worker management and CPU hotplug, hold off
2067 * management while hotplug is in progress. CPU hotplug path can't
2068 * grab %POOL_MANAGING_WORKERS to achieve this because that can
2069 * lead to idle worker depletion (all become busy thinking someone
2070 * else is managing) which in turn can result in deadlock under
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002071 * extreme circumstances. Use @pool->assoc_mutex to synchronize
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002072 * manager against CPU hotplug.
2073 *
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002074 * assoc_mutex would always be free unless CPU hotplug is in
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002075 * progress. trylock first without dropping @gcwq->lock.
2076 */
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002077 if (unlikely(!mutex_trylock(&pool->assoc_mutex))) {
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002078 spin_unlock_irq(&pool->gcwq->lock);
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002079 mutex_lock(&pool->assoc_mutex);
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002080 /*
2081 * CPU hotplug could have happened while we were waiting
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002082 * for assoc_mutex. Hotplug itself can't handle us
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002083 * because manager isn't either on idle or busy list, and
2084 * @gcwq's state and ours could have deviated.
2085 *
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002086 * As hotplug is now excluded via assoc_mutex, we can
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002087 * simply try to bind. It will succeed or fail depending
2088 * on @gcwq's current state. Try it and adjust
2089 * %WORKER_UNBOUND accordingly.
2090 */
2091 if (worker_maybe_bind_and_lock(worker))
2092 worker->flags &= ~WORKER_UNBOUND;
2093 else
2094 worker->flags |= WORKER_UNBOUND;
2095
2096 ret = true;
2097 }
2098
Tejun Heo11ebea52012-07-12 14:46:37 -07002099 pool->flags &= ~POOL_MANAGE_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02002100
2101 /*
2102 * Destroy and then create so that may_start_working() is true
2103 * on return.
2104 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002105 ret |= maybe_destroy_workers(pool);
2106 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002107
Lai Jiangshan552a37e2012-09-10 10:03:33 -07002108 pool->flags &= ~POOL_MANAGING_WORKERS;
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002109 mutex_unlock(&pool->assoc_mutex);
Tejun Heoe22bee72010-06-29 10:07:14 +02002110 return ret;
2111}
2112
Tejun Heoa62428c2010-06-29 10:07:10 +02002113/**
2114 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02002115 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02002116 * @work: work to process
2117 *
2118 * Process @work. This function contains all the logics necessary to
2119 * process a single work including synchronization against and
2120 * interaction with other workers on the same cpu, queueing and
2121 * flushing. As long as context requirement is met, any worker can
2122 * call this function to process a work.
2123 *
2124 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002125 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02002126 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002127static void process_one_work(struct worker *worker, struct work_struct *work)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09002128__releases(&gcwq->lock)
2129__acquires(&gcwq->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02002130{
Tejun Heo7e116292010-06-29 10:07:13 +02002131 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002132 struct worker_pool *pool = worker->pool;
2133 struct global_cwq *gcwq = pool->gcwq;
Tejun Heofb0e7be2010-06-29 10:07:15 +02002134 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02002135 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02002136 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02002137#ifdef CONFIG_LOCKDEP
2138 /*
2139 * It is permissible to free the struct work_struct from
2140 * inside the function that is called from it, this we need to
2141 * take into account for lockdep too. To avoid bogus "held
2142 * lock freed" warnings as well as problems when looking into
2143 * work->lockdep_map, make a copy and use that here.
2144 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07002145 struct lockdep_map lockdep_map;
2146
2147 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002148#endif
Tejun Heo6fec10a2012-07-22 10:16:34 -07002149 /*
2150 * Ensure we're on the correct CPU. DISASSOCIATED test is
2151 * necessary to avoid spurious warnings from rescuers servicing the
Tejun Heo24647572013-01-24 11:01:33 -08002152 * unbound or a disassociated pool.
Tejun Heo6fec10a2012-07-22 10:16:34 -07002153 */
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002154 WARN_ON_ONCE(!(worker->flags & WORKER_UNBOUND) &&
Tejun Heo24647572013-01-24 11:01:33 -08002155 !(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heo25511a42012-07-17 12:39:27 -07002156 raw_smp_processor_id() != gcwq->cpu);
2157
Tejun Heo7e116292010-06-29 10:07:13 +02002158 /*
2159 * A single work shouldn't be executed concurrently by
2160 * multiple workers on a single cpu. Check whether anyone is
2161 * already processing the work. If so, defer the work to the
2162 * currently executing one.
2163 */
Sasha Levin42f85702012-12-17 10:01:23 -05002164 collision = find_worker_executing_work(gcwq, work);
Tejun Heo7e116292010-06-29 10:07:13 +02002165 if (unlikely(collision)) {
2166 move_linked_works(work, &collision->scheduled, NULL);
2167 return;
2168 }
2169
Tejun Heo8930cab2012-08-03 10:30:45 -07002170 /* claim and dequeue */
Tejun Heoa62428c2010-06-29 10:07:10 +02002171 debug_work_deactivate(work);
Tejun Heo023f27d2012-12-19 11:24:06 -08002172 hash_add(gcwq->busy_hash, &worker->hentry, (unsigned long)work);
Tejun Heoc34056a2010-06-29 10:07:11 +02002173 worker->current_work = work;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002174 worker->current_func = work->func;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02002175 worker->current_cwq = cwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002176 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002177
Tejun Heoa62428c2010-06-29 10:07:10 +02002178 list_del_init(&work->entry);
2179
Tejun Heo649027d2010-06-29 10:07:14 +02002180 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02002181 * CPU intensive works don't participate in concurrency
2182 * management. They're the scheduler's responsibility.
2183 */
2184 if (unlikely(cpu_intensive))
2185 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
2186
Tejun Heo974271c2012-07-12 14:46:37 -07002187 /*
2188 * Unbound gcwq isn't concurrency managed and work items should be
2189 * executed ASAP. Wake up another worker if necessary.
2190 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002191 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
2192 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07002193
Tejun Heo8930cab2012-08-03 10:30:45 -07002194 /*
Tejun Heo23657bb2012-08-13 17:08:19 -07002195 * Record the last CPU and clear PENDING which should be the last
2196 * update to @work. Also, do this inside @gcwq->lock so that
2197 * PENDING and queued state changes happen together while IRQ is
2198 * disabled.
Tejun Heo8930cab2012-08-03 10:30:45 -07002199 */
Tejun Heo8930cab2012-08-03 10:30:45 -07002200 set_work_cpu_and_clear_pending(work, gcwq->cpu);
Tejun Heoa62428c2010-06-29 10:07:10 +02002201
Tejun Heoa62428c2010-06-29 10:07:10 +02002202 spin_unlock_irq(&gcwq->lock);
2203
Tejun Heoe1594892011-01-09 23:32:15 +01002204 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002205 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002206 trace_workqueue_execute_start(work);
Tejun Heoa2c1c572012-12-18 10:35:02 -08002207 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002208 /*
2209 * While we must be careful to not use "work" after this, the trace
2210 * point will only record its address.
2211 */
2212 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002213 lock_map_release(&lockdep_map);
2214 lock_map_release(&cwq->wq->lockdep_map);
2215
2216 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Valentin Ilie044c7822012-08-19 00:52:42 +03002217 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
2218 " last function: %pf\n",
Tejun Heoa2c1c572012-12-18 10:35:02 -08002219 current->comm, preempt_count(), task_pid_nr(current),
2220 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02002221 debug_show_held_locks(current);
2222 dump_stack();
2223 }
2224
Tejun Heo8b03ae32010-06-29 10:07:12 +02002225 spin_lock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002226
Tejun Heofb0e7be2010-06-29 10:07:15 +02002227 /* clear cpu intensive status */
2228 if (unlikely(cpu_intensive))
2229 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2230
Tejun Heoa62428c2010-06-29 10:07:10 +02002231 /* we're done with it, release */
Sasha Levin42f85702012-12-17 10:01:23 -05002232 hash_del(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002233 worker->current_work = NULL;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002234 worker->current_func = NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02002235 worker->current_cwq = NULL;
Lai Jiangshanb3f9f402012-09-18 10:40:00 -07002236 cwq_dec_nr_in_flight(cwq, work_color);
Tejun Heoa62428c2010-06-29 10:07:10 +02002237}
2238
Tejun Heoaffee4b2010-06-29 10:07:12 +02002239/**
2240 * process_scheduled_works - process scheduled works
2241 * @worker: self
2242 *
2243 * Process all scheduled works. Please note that the scheduled list
2244 * may change while processing a work, so this function repeatedly
2245 * fetches a work from the top and executes it.
2246 *
2247 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002248 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02002249 * multiple times.
2250 */
2251static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002253 while (!list_empty(&worker->scheduled)) {
2254 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002256 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258}
2259
Tejun Heo4690c4a2010-06-29 10:07:10 +02002260/**
2261 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02002262 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02002263 *
Tejun Heoe22bee72010-06-29 10:07:14 +02002264 * The gcwq worker thread function. There's a single dynamic pool of
2265 * these per each cpu. These workers process all works regardless of
2266 * their specific target workqueue. The only exception is works which
2267 * belong to workqueues with a rescuer which will be explained in
2268 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02002269 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002270static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271{
Tejun Heoc34056a2010-06-29 10:07:11 +02002272 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07002273 struct worker_pool *pool = worker->pool;
2274 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
Tejun Heoe22bee72010-06-29 10:07:14 +02002276 /* tell the scheduler that this is a workqueue worker */
2277 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002278woke_up:
Tejun Heoc8e55f32010-06-29 10:07:12 +02002279 spin_lock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002281 /* we are off idle list if destruction or rebind is requested */
2282 if (unlikely(list_empty(&worker->entry))) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02002283 spin_unlock_irq(&gcwq->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07002284
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002285 /* if DIE is set, destruction is requested */
Tejun Heo25511a42012-07-17 12:39:27 -07002286 if (worker->flags & WORKER_DIE) {
2287 worker->task->flags &= ~PF_WQ_WORKER;
2288 return 0;
2289 }
2290
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002291 /* otherwise, rebind */
Tejun Heo25511a42012-07-17 12:39:27 -07002292 idle_worker_rebind(worker);
2293 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 }
2295
Tejun Heoc8e55f32010-06-29 10:07:12 +02002296 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002297recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002298 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002299 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002300 goto sleep;
2301
2302 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002303 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002304 goto recheck;
2305
Tejun Heoc8e55f32010-06-29 10:07:12 +02002306 /*
2307 * ->scheduled list can only be filled while a worker is
2308 * preparing to process a work or actually processing it.
2309 * Make sure nobody diddled with it while I was sleeping.
2310 */
2311 BUG_ON(!list_empty(&worker->scheduled));
2312
Tejun Heoe22bee72010-06-29 10:07:14 +02002313 /*
2314 * When control reaches this point, we're guaranteed to have
2315 * at least one idle worker or that someone else has already
2316 * assumed the manager role.
2317 */
2318 worker_clr_flags(worker, WORKER_PREP);
2319
2320 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002321 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002322 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002323 struct work_struct, entry);
2324
2325 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2326 /* optimization path, not strictly necessary */
2327 process_one_work(worker, work);
2328 if (unlikely(!list_empty(&worker->scheduled)))
2329 process_scheduled_works(worker);
2330 } else {
2331 move_linked_works(work, &worker->scheduled, NULL);
2332 process_scheduled_works(worker);
2333 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002334 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002335
Tejun Heoe22bee72010-06-29 10:07:14 +02002336 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002337sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07002338 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002339 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002340
Tejun Heoc8e55f32010-06-29 10:07:12 +02002341 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02002342 * gcwq->lock is held and there's no work to process and no
2343 * need to manage, sleep. Workers are woken up only while
2344 * holding gcwq->lock or from local cpu, so setting the
2345 * current state before releasing gcwq->lock is enough to
2346 * prevent losing any event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002347 */
2348 worker_enter_idle(worker);
2349 __set_current_state(TASK_INTERRUPTIBLE);
2350 spin_unlock_irq(&gcwq->lock);
2351 schedule();
2352 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353}
2354
Tejun Heoe22bee72010-06-29 10:07:14 +02002355/**
2356 * rescuer_thread - the rescuer thread function
Tejun Heo111c2252013-01-17 17:16:24 -08002357 * @__rescuer: self
Tejun Heoe22bee72010-06-29 10:07:14 +02002358 *
2359 * Workqueue rescuer thread function. There's one rescuer for each
2360 * workqueue which has WQ_RESCUER set.
2361 *
2362 * Regular work processing on a gcwq may block trying to create a new
2363 * worker which uses GFP_KERNEL allocation which has slight chance of
2364 * developing into deadlock if some works currently on the same queue
2365 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2366 * the problem rescuer solves.
2367 *
2368 * When such condition is possible, the gcwq summons rescuers of all
2369 * workqueues which have works queued on the gcwq and let them process
2370 * those works so that forward progress can be guaranteed.
2371 *
2372 * This should happen rarely.
2373 */
Tejun Heo111c2252013-01-17 17:16:24 -08002374static int rescuer_thread(void *__rescuer)
Tejun Heoe22bee72010-06-29 10:07:14 +02002375{
Tejun Heo111c2252013-01-17 17:16:24 -08002376 struct worker *rescuer = __rescuer;
2377 struct workqueue_struct *wq = rescuer->rescue_wq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002378 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heof3421792010-07-02 10:03:51 +02002379 bool is_unbound = wq->flags & WQ_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02002380 unsigned int cpu;
2381
2382 set_user_nice(current, RESCUER_NICE_LEVEL);
Tejun Heo111c2252013-01-17 17:16:24 -08002383
2384 /*
2385 * Mark rescuer as worker too. As WORKER_PREP is never cleared, it
2386 * doesn't participate in concurrency management.
2387 */
2388 rescuer->task->flags |= PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002389repeat:
2390 set_current_state(TASK_INTERRUPTIBLE);
2391
Mike Galbraith412d32e2012-11-28 07:17:18 +01002392 if (kthread_should_stop()) {
2393 __set_current_state(TASK_RUNNING);
Tejun Heo111c2252013-01-17 17:16:24 -08002394 rescuer->task->flags &= ~PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002395 return 0;
Mike Galbraith412d32e2012-11-28 07:17:18 +01002396 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002397
Tejun Heof3421792010-07-02 10:03:51 +02002398 /*
2399 * See whether any cpu is asking for help. Unbounded
2400 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
2401 */
Tejun Heof2e005a2010-07-20 15:59:09 +02002402 for_each_mayday_cpu(cpu, wq->mayday_mask) {
Tejun Heof3421792010-07-02 10:03:51 +02002403 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2404 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002405 struct worker_pool *pool = cwq->pool;
2406 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002407 struct work_struct *work, *n;
2408
2409 __set_current_state(TASK_RUNNING);
Tejun Heof2e005a2010-07-20 15:59:09 +02002410 mayday_clear_cpu(cpu, wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002411
2412 /* migrate to the target cpu if possible */
Tejun Heobd7bdd42012-07-12 14:46:37 -07002413 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002414 worker_maybe_bind_and_lock(rescuer);
2415
2416 /*
2417 * Slurp in all works issued via this workqueue and
2418 * process'em.
2419 */
2420 BUG_ON(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002421 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02002422 if (get_work_cwq(work) == cwq)
2423 move_linked_works(work, scheduled, &n);
2424
2425 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002426
2427 /*
2428 * Leave this gcwq. If keep_working() is %true, notify a
2429 * regular worker; otherwise, we end up with 0 concurrency
2430 * and stalling the execution.
2431 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002432 if (keep_working(pool))
2433 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002434
Tejun Heoe22bee72010-06-29 10:07:14 +02002435 spin_unlock_irq(&gcwq->lock);
2436 }
2437
Tejun Heo111c2252013-01-17 17:16:24 -08002438 /* rescuers should never participate in concurrency management */
2439 WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
Tejun Heoe22bee72010-06-29 10:07:14 +02002440 schedule();
2441 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442}
2443
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002444struct wq_barrier {
2445 struct work_struct work;
2446 struct completion done;
2447};
2448
2449static void wq_barrier_func(struct work_struct *work)
2450{
2451 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2452 complete(&barr->done);
2453}
2454
Tejun Heo4690c4a2010-06-29 10:07:10 +02002455/**
2456 * insert_wq_barrier - insert a barrier work
2457 * @cwq: cwq to insert barrier into
2458 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002459 * @target: target work to attach @barr to
2460 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002461 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002462 * @barr is linked to @target such that @barr is completed only after
2463 * @target finishes execution. Please note that the ordering
2464 * guarantee is observed only with respect to @target and on the local
2465 * cpu.
2466 *
2467 * Currently, a queued barrier can't be canceled. This is because
2468 * try_to_grab_pending() can't determine whether the work to be
2469 * grabbed is at the head of the queue and thus can't clear LINKED
2470 * flag of the previous work while there must be a valid next work
2471 * after a work with LINKED flag set.
2472 *
2473 * Note that when @worker is non-NULL, @target may be modified
2474 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002475 *
2476 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002477 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002478 */
Oleg Nesterov83c22522007-05-09 02:33:54 -07002479static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002480 struct wq_barrier *barr,
2481 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002482{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002483 struct list_head *head;
2484 unsigned int linked = 0;
2485
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002486 /*
Tejun Heo8b03ae32010-06-29 10:07:12 +02002487 * debugobject calls are safe here even with gcwq->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002488 * as we know for sure that this will not trigger any of the
2489 * checks and call back into the fixup functions where we
2490 * might deadlock.
2491 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002492 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002493 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002494 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002495
Tejun Heoaffee4b2010-06-29 10:07:12 +02002496 /*
2497 * If @target is currently being executed, schedule the
2498 * barrier to the worker; otherwise, put it after @target.
2499 */
2500 if (worker)
2501 head = worker->scheduled.next;
2502 else {
2503 unsigned long *bits = work_data_bits(target);
2504
2505 head = target->entry.next;
2506 /* there can already be other linked works, inherit and set */
2507 linked = *bits & WORK_STRUCT_LINKED;
2508 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2509 }
2510
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002511 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002512 insert_work(cwq, &barr->work, head,
2513 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002514}
2515
Tejun Heo73f53c42010-06-29 10:07:11 +02002516/**
2517 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2518 * @wq: workqueue being flushed
2519 * @flush_color: new flush color, < 0 for no-op
2520 * @work_color: new work color, < 0 for no-op
2521 *
2522 * Prepare cwqs for workqueue flushing.
2523 *
2524 * If @flush_color is non-negative, flush_color on all cwqs should be
2525 * -1. If no cwq has in-flight commands at the specified color, all
2526 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2527 * has in flight commands, its cwq->flush_color is set to
2528 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2529 * wakeup logic is armed and %true is returned.
2530 *
2531 * The caller should have initialized @wq->first_flusher prior to
2532 * calling this function with non-negative @flush_color. If
2533 * @flush_color is negative, no flush color update is done and %false
2534 * is returned.
2535 *
2536 * If @work_color is non-negative, all cwqs should have the same
2537 * work_color which is previous to @work_color and all will be
2538 * advanced to @work_color.
2539 *
2540 * CONTEXT:
2541 * mutex_lock(wq->flush_mutex).
2542 *
2543 * RETURNS:
2544 * %true if @flush_color >= 0 and there's something to flush. %false
2545 * otherwise.
2546 */
2547static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2548 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549{
Tejun Heo73f53c42010-06-29 10:07:11 +02002550 bool wait = false;
2551 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -07002552
Tejun Heo73f53c42010-06-29 10:07:11 +02002553 if (flush_color >= 0) {
2554 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2555 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002556 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002557
Tejun Heof3421792010-07-02 10:03:51 +02002558 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002559 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002560 struct global_cwq *gcwq = cwq->pool->gcwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002561
Tejun Heo8b03ae32010-06-29 10:07:12 +02002562 spin_lock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002563
2564 if (flush_color >= 0) {
2565 BUG_ON(cwq->flush_color != -1);
2566
2567 if (cwq->nr_in_flight[flush_color]) {
2568 cwq->flush_color = flush_color;
2569 atomic_inc(&wq->nr_cwqs_to_flush);
2570 wait = true;
2571 }
2572 }
2573
2574 if (work_color >= 0) {
2575 BUG_ON(work_color != work_next_color(cwq->work_color));
2576 cwq->work_color = work_color;
2577 }
2578
Tejun Heo8b03ae32010-06-29 10:07:12 +02002579 spin_unlock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002580 }
2581
2582 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2583 complete(&wq->first_flusher->done);
2584
2585 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586}
2587
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002588/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002590 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 *
2592 * Forces execution of the workqueue and blocks until its completion.
2593 * This is typically used in driver shutdown handlers.
2594 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002595 * We sleep until all works which were queued on entry have been handled,
2596 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002598void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599{
Tejun Heo73f53c42010-06-29 10:07:11 +02002600 struct wq_flusher this_flusher = {
2601 .list = LIST_HEAD_INIT(this_flusher.list),
2602 .flush_color = -1,
2603 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2604 };
2605 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002606
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002607 lock_map_acquire(&wq->lockdep_map);
2608 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002609
2610 mutex_lock(&wq->flush_mutex);
2611
2612 /*
2613 * Start-to-wait phase
2614 */
2615 next_color = work_next_color(wq->work_color);
2616
2617 if (next_color != wq->flush_color) {
2618 /*
2619 * Color space is not full. The current work_color
2620 * becomes our flush_color and work_color is advanced
2621 * by one.
2622 */
2623 BUG_ON(!list_empty(&wq->flusher_overflow));
2624 this_flusher.flush_color = wq->work_color;
2625 wq->work_color = next_color;
2626
2627 if (!wq->first_flusher) {
2628 /* no flush in progress, become the first flusher */
2629 BUG_ON(wq->flush_color != this_flusher.flush_color);
2630
2631 wq->first_flusher = &this_flusher;
2632
2633 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2634 wq->work_color)) {
2635 /* nothing to flush, done */
2636 wq->flush_color = next_color;
2637 wq->first_flusher = NULL;
2638 goto out_unlock;
2639 }
2640 } else {
2641 /* wait in queue */
2642 BUG_ON(wq->flush_color == this_flusher.flush_color);
2643 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2644 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2645 }
2646 } else {
2647 /*
2648 * Oops, color space is full, wait on overflow queue.
2649 * The next flush completion will assign us
2650 * flush_color and transfer to flusher_queue.
2651 */
2652 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2653 }
2654
2655 mutex_unlock(&wq->flush_mutex);
2656
2657 wait_for_completion(&this_flusher.done);
2658
2659 /*
2660 * Wake-up-and-cascade phase
2661 *
2662 * First flushers are responsible for cascading flushes and
2663 * handling overflow. Non-first flushers can simply return.
2664 */
2665 if (wq->first_flusher != &this_flusher)
2666 return;
2667
2668 mutex_lock(&wq->flush_mutex);
2669
Tejun Heo4ce48b32010-07-02 10:03:51 +02002670 /* we might have raced, check again with mutex held */
2671 if (wq->first_flusher != &this_flusher)
2672 goto out_unlock;
2673
Tejun Heo73f53c42010-06-29 10:07:11 +02002674 wq->first_flusher = NULL;
2675
2676 BUG_ON(!list_empty(&this_flusher.list));
2677 BUG_ON(wq->flush_color != this_flusher.flush_color);
2678
2679 while (true) {
2680 struct wq_flusher *next, *tmp;
2681
2682 /* complete all the flushers sharing the current flush color */
2683 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2684 if (next->flush_color != wq->flush_color)
2685 break;
2686 list_del_init(&next->list);
2687 complete(&next->done);
2688 }
2689
2690 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2691 wq->flush_color != work_next_color(wq->work_color));
2692
2693 /* this flush_color is finished, advance by one */
2694 wq->flush_color = work_next_color(wq->flush_color);
2695
2696 /* one color has been freed, handle overflow queue */
2697 if (!list_empty(&wq->flusher_overflow)) {
2698 /*
2699 * Assign the same color to all overflowed
2700 * flushers, advance work_color and append to
2701 * flusher_queue. This is the start-to-wait
2702 * phase for these overflowed flushers.
2703 */
2704 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2705 tmp->flush_color = wq->work_color;
2706
2707 wq->work_color = work_next_color(wq->work_color);
2708
2709 list_splice_tail_init(&wq->flusher_overflow,
2710 &wq->flusher_queue);
2711 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2712 }
2713
2714 if (list_empty(&wq->flusher_queue)) {
2715 BUG_ON(wq->flush_color != wq->work_color);
2716 break;
2717 }
2718
2719 /*
2720 * Need to flush more colors. Make the next flusher
2721 * the new first flusher and arm cwqs.
2722 */
2723 BUG_ON(wq->flush_color == wq->work_color);
2724 BUG_ON(wq->flush_color != next->flush_color);
2725
2726 list_del_init(&next->list);
2727 wq->first_flusher = next;
2728
2729 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2730 break;
2731
2732 /*
2733 * Meh... this color is already done, clear first
2734 * flusher and repeat cascading.
2735 */
2736 wq->first_flusher = NULL;
2737 }
2738
2739out_unlock:
2740 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741}
Dave Jonesae90dd52006-06-30 01:40:45 -04002742EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002744/**
2745 * drain_workqueue - drain a workqueue
2746 * @wq: workqueue to drain
2747 *
2748 * Wait until the workqueue becomes empty. While draining is in progress,
2749 * only chain queueing is allowed. IOW, only currently pending or running
2750 * work items on @wq can queue further work items on it. @wq is flushed
2751 * repeatedly until it becomes empty. The number of flushing is detemined
2752 * by the depth of chaining and should be relatively short. Whine if it
2753 * takes too long.
2754 */
2755void drain_workqueue(struct workqueue_struct *wq)
2756{
2757 unsigned int flush_cnt = 0;
2758 unsigned int cpu;
2759
2760 /*
2761 * __queue_work() needs to test whether there are drainers, is much
2762 * hotter than drain_workqueue() and already looks at @wq->flags.
2763 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2764 */
2765 spin_lock(&workqueue_lock);
2766 if (!wq->nr_drainers++)
2767 wq->flags |= WQ_DRAINING;
2768 spin_unlock(&workqueue_lock);
2769reflush:
2770 flush_workqueue(wq);
2771
2772 for_each_cwq_cpu(cpu, wq) {
2773 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002774 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002775
Tejun Heobd7bdd42012-07-12 14:46:37 -07002776 spin_lock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002777 drained = !cwq->nr_active && list_empty(&cwq->delayed_works);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002778 spin_unlock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002779
2780 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002781 continue;
2782
2783 if (++flush_cnt == 10 ||
2784 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
Valentin Ilie044c7822012-08-19 00:52:42 +03002785 pr_warn("workqueue %s: flush on destruction isn't complete after %u tries\n",
2786 wq->name, flush_cnt);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002787 goto reflush;
2788 }
2789
2790 spin_lock(&workqueue_lock);
2791 if (!--wq->nr_drainers)
2792 wq->flags &= ~WQ_DRAINING;
2793 spin_unlock(&workqueue_lock);
2794}
2795EXPORT_SYMBOL_GPL(drain_workqueue);
2796
Tejun Heo606a5022012-08-20 14:51:23 -07002797static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
Tejun Heobaf59022010-09-16 10:42:16 +02002798{
2799 struct worker *worker = NULL;
2800 struct global_cwq *gcwq;
2801 struct cpu_workqueue_struct *cwq;
2802
2803 might_sleep();
2804 gcwq = get_work_gcwq(work);
2805 if (!gcwq)
2806 return false;
2807
2808 spin_lock_irq(&gcwq->lock);
2809 if (!list_empty(&work->entry)) {
2810 /*
2811 * See the comment near try_to_grab_pending()->smp_rmb().
2812 * If it was re-queued to a different gcwq under us, we
2813 * are not going to wait.
2814 */
2815 smp_rmb();
2816 cwq = get_work_cwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002817 if (unlikely(!cwq || gcwq != cwq->pool->gcwq))
Tejun Heobaf59022010-09-16 10:42:16 +02002818 goto already_gone;
Tejun Heo606a5022012-08-20 14:51:23 -07002819 } else {
Tejun Heobaf59022010-09-16 10:42:16 +02002820 worker = find_worker_executing_work(gcwq, work);
2821 if (!worker)
2822 goto already_gone;
2823 cwq = worker->current_cwq;
Tejun Heo606a5022012-08-20 14:51:23 -07002824 }
Tejun Heobaf59022010-09-16 10:42:16 +02002825
2826 insert_wq_barrier(cwq, barr, work, worker);
2827 spin_unlock_irq(&gcwq->lock);
2828
Tejun Heoe1594892011-01-09 23:32:15 +01002829 /*
2830 * If @max_active is 1 or rescuer is in use, flushing another work
2831 * item on the same workqueue may lead to deadlock. Make sure the
2832 * flusher is not running on the same workqueue by verifying write
2833 * access.
2834 */
2835 if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER)
2836 lock_map_acquire(&cwq->wq->lockdep_map);
2837 else
2838 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heobaf59022010-09-16 10:42:16 +02002839 lock_map_release(&cwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002840
Tejun Heobaf59022010-09-16 10:42:16 +02002841 return true;
2842already_gone:
2843 spin_unlock_irq(&gcwq->lock);
2844 return false;
2845}
2846
Oleg Nesterovdb700892008-07-25 01:47:49 -07002847/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002848 * flush_work - wait for a work to finish executing the last queueing instance
2849 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002850 *
Tejun Heo606a5022012-08-20 14:51:23 -07002851 * Wait until @work has finished execution. @work is guaranteed to be idle
2852 * on return if it hasn't been requeued since flush started.
Tejun Heo401a8d02010-09-16 10:36:00 +02002853 *
2854 * RETURNS:
2855 * %true if flush_work() waited for the work to finish execution,
2856 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002857 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002858bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002859{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002860 struct wq_barrier barr;
2861
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002862 lock_map_acquire(&work->lockdep_map);
2863 lock_map_release(&work->lockdep_map);
2864
Tejun Heo606a5022012-08-20 14:51:23 -07002865 if (start_flush_work(work, &barr)) {
Tejun Heobaf59022010-09-16 10:42:16 +02002866 wait_for_completion(&barr.done);
2867 destroy_work_on_stack(&barr.work);
2868 return true;
Tejun Heo606a5022012-08-20 14:51:23 -07002869 } else {
Tejun Heobaf59022010-09-16 10:42:16 +02002870 return false;
Tejun Heo606a5022012-08-20 14:51:23 -07002871 }
Oleg Nesterovdb700892008-07-25 01:47:49 -07002872}
2873EXPORT_SYMBOL_GPL(flush_work);
2874
Tejun Heo36e227d2012-08-03 10:30:46 -07002875static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
Tejun Heo401a8d02010-09-16 10:36:00 +02002876{
Tejun Heobbb68df2012-08-03 10:30:46 -07002877 unsigned long flags;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002878 int ret;
2879
2880 do {
Tejun Heobbb68df2012-08-03 10:30:46 -07002881 ret = try_to_grab_pending(work, is_dwork, &flags);
2882 /*
2883 * If someone else is canceling, wait for the same event it
2884 * would be waiting for before retrying.
2885 */
2886 if (unlikely(ret == -ENOENT))
Tejun Heo606a5022012-08-20 14:51:23 -07002887 flush_work(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002888 } while (unlikely(ret < 0));
2889
Tejun Heobbb68df2012-08-03 10:30:46 -07002890 /* tell other tasks trying to grab @work to back off */
2891 mark_work_canceling(work);
2892 local_irq_restore(flags);
2893
Tejun Heo606a5022012-08-20 14:51:23 -07002894 flush_work(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002895 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002896 return ret;
2897}
2898
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002899/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002900 * cancel_work_sync - cancel a work and wait for it to finish
2901 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002902 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002903 * Cancel @work and wait for its execution to finish. This function
2904 * can be used even if the work re-queues itself or migrates to
2905 * another workqueue. On return from this function, @work is
2906 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002907 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002908 * cancel_work_sync(&delayed_work->work) must not be used for
2909 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002910 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002911 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002912 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002913 *
2914 * RETURNS:
2915 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002916 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002917bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002918{
Tejun Heo36e227d2012-08-03 10:30:46 -07002919 return __cancel_work_timer(work, false);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002920}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002921EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002922
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002923/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002924 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2925 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002926 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002927 * Delayed timer is cancelled and the pending work is queued for
2928 * immediate execution. Like flush_work(), this function only
2929 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002930 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002931 * RETURNS:
2932 * %true if flush_work() waited for the work to finish execution,
2933 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002934 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002935bool flush_delayed_work(struct delayed_work *dwork)
2936{
Tejun Heo8930cab2012-08-03 10:30:45 -07002937 local_irq_disable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002938 if (del_timer_sync(&dwork->timer))
Tejun Heo12650572012-08-08 09:38:42 -07002939 __queue_work(dwork->cpu,
Tejun Heo401a8d02010-09-16 10:36:00 +02002940 get_work_cwq(&dwork->work)->wq, &dwork->work);
Tejun Heo8930cab2012-08-03 10:30:45 -07002941 local_irq_enable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002942 return flush_work(&dwork->work);
2943}
2944EXPORT_SYMBOL(flush_delayed_work);
2945
2946/**
Tejun Heo57b30ae2012-08-21 13:18:24 -07002947 * cancel_delayed_work - cancel a delayed work
2948 * @dwork: delayed_work to cancel
Tejun Heo09383492010-09-16 10:48:29 +02002949 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002950 * Kill off a pending delayed_work. Returns %true if @dwork was pending
2951 * and canceled; %false if wasn't pending. Note that the work callback
2952 * function may still be running on return, unless it returns %true and the
2953 * work doesn't re-arm itself. Explicitly flush or use
2954 * cancel_delayed_work_sync() to wait on it.
Tejun Heo09383492010-09-16 10:48:29 +02002955 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002956 * This function is safe to call from any context including IRQ handler.
Tejun Heo09383492010-09-16 10:48:29 +02002957 */
Tejun Heo57b30ae2012-08-21 13:18:24 -07002958bool cancel_delayed_work(struct delayed_work *dwork)
Tejun Heo09383492010-09-16 10:48:29 +02002959{
Tejun Heo57b30ae2012-08-21 13:18:24 -07002960 unsigned long flags;
2961 int ret;
2962
2963 do {
2964 ret = try_to_grab_pending(&dwork->work, true, &flags);
2965 } while (unlikely(ret == -EAGAIN));
2966
2967 if (unlikely(ret < 0))
2968 return false;
2969
2970 set_work_cpu_and_clear_pending(&dwork->work, work_cpu(&dwork->work));
2971 local_irq_restore(flags);
Dan Magenheimerc0158ca2012-10-18 16:31:37 -07002972 return ret;
Tejun Heo09383492010-09-16 10:48:29 +02002973}
Tejun Heo57b30ae2012-08-21 13:18:24 -07002974EXPORT_SYMBOL(cancel_delayed_work);
Tejun Heo09383492010-09-16 10:48:29 +02002975
2976/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002977 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2978 * @dwork: the delayed work cancel
2979 *
2980 * This is cancel_work_sync() for delayed works.
2981 *
2982 * RETURNS:
2983 * %true if @dwork was pending, %false otherwise.
2984 */
2985bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002986{
Tejun Heo36e227d2012-08-03 10:30:46 -07002987 return __cancel_work_timer(&dwork->work, true);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002988}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002989EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002991/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07002992 * schedule_work_on - put work task on a specific cpu
2993 * @cpu: cpu to put the work task on
2994 * @work: job to be done
2995 *
2996 * This puts a job on a specific cpu
2997 */
Tejun Heod4283e92012-08-03 10:30:44 -07002998bool schedule_work_on(int cpu, struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07002999{
Tejun Heod320c032010-06-29 10:07:14 +02003000 return queue_work_on(cpu, system_wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07003001}
3002EXPORT_SYMBOL(schedule_work_on);
3003
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003004/**
Dave Jonesae90dd52006-06-30 01:40:45 -04003005 * schedule_work - put work task in global workqueue
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 * @work: job to be done
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 *
Tejun Heod4283e92012-08-03 10:30:44 -07003008 * Returns %false if @work was already on the kernel-global workqueue and
3009 * %true otherwise.
David Howells52bad642006-11-22 14:54:01 +00003010 *
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003011 * This puts a job in the kernel-global workqueue if it was not already
3012 * queued and leaves it in the same position on the kernel-global
3013 * workqueue otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 */
Tejun Heod4283e92012-08-03 10:30:44 -07003015bool schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016{
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003017 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018}
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003019EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003021/**
3022 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
3023 * @cpu: cpu to use
3024 * @dwork: job to be done
3025 * @delay: number of jiffies to wait
3026 *
3027 * After waiting for a given time this puts a job in the kernel-global
3028 * workqueue on the specified CPU.
3029 */
Tejun Heod4283e92012-08-03 10:30:44 -07003030bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork,
3031 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032{
Tejun Heod320c032010-06-29 10:07:14 +02003033 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034}
Dave Jonesae90dd52006-06-30 01:40:45 -04003035EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036
Andrew Mortonb6136772006-06-25 05:47:49 -07003037/**
Tejun Heo0a13c002012-08-03 10:30:44 -07003038 * schedule_delayed_work - put work task in global workqueue after delay
3039 * @dwork: job to be done
3040 * @delay: number of jiffies to wait or 0 for immediate execution
3041 *
3042 * After waiting for a given time this puts a job in the kernel-global
3043 * workqueue.
3044 */
Tejun Heod4283e92012-08-03 10:30:44 -07003045bool schedule_delayed_work(struct delayed_work *dwork, unsigned long delay)
Tejun Heo0a13c002012-08-03 10:30:44 -07003046{
3047 return queue_delayed_work(system_wq, dwork, delay);
3048}
3049EXPORT_SYMBOL(schedule_delayed_work);
3050
3051/**
Tejun Heo31ddd872010-10-19 11:14:49 +02003052 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07003053 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07003054 *
Tejun Heo31ddd872010-10-19 11:14:49 +02003055 * schedule_on_each_cpu() executes @func on each online CPU using the
3056 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07003057 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02003058 *
3059 * RETURNS:
3060 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07003061 */
David Howells65f27f32006-11-22 14:55:48 +00003062int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08003063{
3064 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02003065 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08003066
Andrew Mortonb6136772006-06-25 05:47:49 -07003067 works = alloc_percpu(struct work_struct);
3068 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08003069 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07003070
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003071 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08003072
Christoph Lameter15316ba2006-01-08 01:00:43 -08003073 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01003074 struct work_struct *work = per_cpu_ptr(works, cpu);
3075
3076 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003077 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02003078 }
Tejun Heo93981802009-11-17 14:06:20 -08003079
3080 for_each_online_cpu(cpu)
3081 flush_work(per_cpu_ptr(works, cpu));
3082
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003083 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07003084 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08003085 return 0;
3086}
3087
Alan Sterneef6a7d2010-02-12 17:39:21 +09003088/**
3089 * flush_scheduled_work - ensure that any scheduled work has run to completion.
3090 *
3091 * Forces execution of the kernel-global workqueue and blocks until its
3092 * completion.
3093 *
3094 * Think twice before calling this function! It's very easy to get into
3095 * trouble if you don't take great care. Either of the following situations
3096 * will lead to deadlock:
3097 *
3098 * One of the work items currently on the workqueue needs to acquire
3099 * a lock held by your code or its caller.
3100 *
3101 * Your code is running in the context of a work routine.
3102 *
3103 * They will be detected by lockdep when they occur, but the first might not
3104 * occur very often. It depends on what work items are on the workqueue and
3105 * what locks they need, which you have no control over.
3106 *
3107 * In most situations flushing the entire workqueue is overkill; you merely
3108 * need to know that a particular work item isn't queued and isn't running.
3109 * In such cases you should use cancel_delayed_work_sync() or
3110 * cancel_work_sync() instead.
3111 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112void flush_scheduled_work(void)
3113{
Tejun Heod320c032010-06-29 10:07:14 +02003114 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115}
Dave Jonesae90dd52006-06-30 01:40:45 -04003116EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
3118/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06003119 * execute_in_process_context - reliably execute the routine with user context
3120 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06003121 * @ew: guaranteed storage for the execute work structure (must
3122 * be available when the work executes)
3123 *
3124 * Executes the function immediately if process context is available,
3125 * otherwise schedules the function for delayed execution.
3126 *
3127 * Returns: 0 - function was executed
3128 * 1 - function was scheduled for execution
3129 */
David Howells65f27f32006-11-22 14:55:48 +00003130int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06003131{
3132 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00003133 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003134 return 0;
3135 }
3136
David Howells65f27f32006-11-22 14:55:48 +00003137 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003138 schedule_work(&ew->work);
3139
3140 return 1;
3141}
3142EXPORT_SYMBOL_GPL(execute_in_process_context);
3143
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144int keventd_up(void)
3145{
Tejun Heod320c032010-06-29 10:07:14 +02003146 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147}
3148
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003149static int alloc_cwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150{
Oleg Nesterov3af244332007-05-09 02:34:09 -07003151 /*
Tejun Heo0f900042010-06-29 10:07:11 +02003152 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
3153 * Make sure that the alignment isn't lower than that of
3154 * unsigned long long.
Oleg Nesterov3af244332007-05-09 02:34:09 -07003155 */
Tejun Heo0f900042010-06-29 10:07:11 +02003156 const size_t size = sizeof(struct cpu_workqueue_struct);
3157 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
3158 __alignof__(unsigned long long));
Oleg Nesterov3af244332007-05-09 02:34:09 -07003159
Lai Jiangshane06ffa12012-03-09 18:03:20 +08003160 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02003161 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
Tejun Heo931ac772010-07-20 11:07:48 +02003162 else {
Tejun Heof3421792010-07-02 10:03:51 +02003163 void *ptr;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01003164
Tejun Heof3421792010-07-02 10:03:51 +02003165 /*
3166 * Allocate enough room to align cwq and put an extra
3167 * pointer at the end pointing back to the originally
3168 * allocated pointer which will be used for free.
3169 */
3170 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
3171 if (ptr) {
3172 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
3173 *(void **)(wq->cpu_wq.single + 1) = ptr;
3174 }
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003175 }
Tejun Heof3421792010-07-02 10:03:51 +02003176
Tejun Heo0415b00d12011-03-24 18:50:09 +01003177 /* just in case, make sure it's actually aligned */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003178 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
3179 return wq->cpu_wq.v ? 0 : -ENOMEM;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003180}
3181
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003182static void free_cwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07003183{
Lai Jiangshane06ffa12012-03-09 18:03:20 +08003184 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02003185 free_percpu(wq->cpu_wq.pcpu);
3186 else if (wq->cpu_wq.single) {
3187 /* the pointer to free is stored right after the cwq */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003188 kfree(*(void **)(wq->cpu_wq.single + 1));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07003189 }
3190}
3191
Tejun Heof3421792010-07-02 10:03:51 +02003192static int wq_clamp_max_active(int max_active, unsigned int flags,
3193 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003194{
Tejun Heof3421792010-07-02 10:03:51 +02003195 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3196
3197 if (max_active < 1 || max_active > lim)
Valentin Ilie044c7822012-08-19 00:52:42 +03003198 pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
3199 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003200
Tejun Heof3421792010-07-02 10:03:51 +02003201 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003202}
3203
Tejun Heob196be82012-01-10 15:11:35 -08003204struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02003205 unsigned int flags,
3206 int max_active,
3207 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08003208 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003209{
Tejun Heob196be82012-01-10 15:11:35 -08003210 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003211 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02003212 unsigned int cpu;
Tejun Heob196be82012-01-10 15:11:35 -08003213 size_t namelen;
3214
3215 /* determine namelen, allocate wq and format name */
3216 va_start(args, lock_name);
3217 va_copy(args1, args);
3218 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3219
3220 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3221 if (!wq)
3222 goto err;
3223
3224 vsnprintf(wq->name, namelen, fmt, args1);
3225 va_end(args);
3226 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003227
Tejun Heof3421792010-07-02 10:03:51 +02003228 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02003229 * Workqueues which may be used during memory reclaim should
3230 * have a rescuer to guarantee forward progress.
3231 */
3232 if (flags & WQ_MEM_RECLAIM)
3233 flags |= WQ_RESCUER;
3234
Tejun Heod320c032010-06-29 10:07:14 +02003235 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003236 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003237
Tejun Heob196be82012-01-10 15:11:35 -08003238 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003239 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003240 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003241 mutex_init(&wq->flush_mutex);
3242 atomic_set(&wq->nr_cwqs_to_flush, 0);
3243 INIT_LIST_HEAD(&wq->flusher_queue);
3244 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003245
Johannes Bergeb13ba82008-01-16 09:51:58 +01003246 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003247 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003248
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003249 if (alloc_cwqs(wq) < 0)
3250 goto err;
3251
Tejun Heof3421792010-07-02 10:03:51 +02003252 for_each_cwq_cpu(cpu, wq) {
Tejun Heo15376632010-06-29 10:07:11 +02003253 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003254 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo32704762012-07-13 22:16:45 -07003255 int pool_idx = (bool)(flags & WQ_HIGHPRI);
Tejun Heo15376632010-06-29 10:07:11 +02003256
Tejun Heo0f900042010-06-29 10:07:11 +02003257 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heo32704762012-07-13 22:16:45 -07003258 cwq->pool = &gcwq->pools[pool_idx];
Tejun Heoc34056a2010-06-29 10:07:11 +02003259 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02003260 cwq->flush_color = -1;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003261 cwq->max_active = max_active;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003262 INIT_LIST_HEAD(&cwq->delayed_works);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003263 }
3264
Tejun Heoe22bee72010-06-29 10:07:14 +02003265 if (flags & WQ_RESCUER) {
3266 struct worker *rescuer;
3267
Tejun Heof2e005a2010-07-20 15:59:09 +02003268 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
Tejun Heoe22bee72010-06-29 10:07:14 +02003269 goto err;
3270
3271 wq->rescuer = rescuer = alloc_worker();
3272 if (!rescuer)
3273 goto err;
3274
Tejun Heo111c2252013-01-17 17:16:24 -08003275 rescuer->rescue_wq = wq;
3276 rescuer->task = kthread_create(rescuer_thread, rescuer, "%s",
Tejun Heob196be82012-01-10 15:11:35 -08003277 wq->name);
Tejun Heoe22bee72010-06-29 10:07:14 +02003278 if (IS_ERR(rescuer->task))
3279 goto err;
3280
Tejun Heoe22bee72010-06-29 10:07:14 +02003281 rescuer->task->flags |= PF_THREAD_BOUND;
3282 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003283 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003284
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003285 /*
3286 * workqueue_lock protects global freeze state and workqueues
3287 * list. Grab it, set max_active accordingly and add the new
3288 * workqueue to workqueues list.
3289 */
Tejun Heo15376632010-06-29 10:07:11 +02003290 spin_lock(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003291
Tejun Heo58a69cb2011-02-16 09:25:31 +01003292 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heof3421792010-07-02 10:03:51 +02003293 for_each_cwq_cpu(cpu, wq)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003294 get_cwq(cpu, wq)->max_active = 0;
3295
Tejun Heo15376632010-06-29 10:07:11 +02003296 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003297
Tejun Heo15376632010-06-29 10:07:11 +02003298 spin_unlock(&workqueue_lock);
3299
Oleg Nesterov3af244332007-05-09 02:34:09 -07003300 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02003301err:
3302 if (wq) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003303 free_cwqs(wq);
Tejun Heof2e005a2010-07-20 15:59:09 +02003304 free_mayday_mask(wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02003305 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003306 kfree(wq);
3307 }
3308 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003309}
Tejun Heod320c032010-06-29 10:07:14 +02003310EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003311
3312/**
3313 * destroy_workqueue - safely terminate a workqueue
3314 * @wq: target workqueue
3315 *
3316 * Safely destroy a workqueue. All work currently pending will be done first.
3317 */
3318void destroy_workqueue(struct workqueue_struct *wq)
3319{
Tejun Heoc8e55f32010-06-29 10:07:12 +02003320 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003321
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003322 /* drain it before proceeding with destruction */
3323 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003324
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003325 /*
3326 * wq list is used to freeze wq, remove from list after
3327 * flushing is complete in case freeze races us.
3328 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003329 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07003330 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003331 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003332
Tejun Heoe22bee72010-06-29 10:07:14 +02003333 /* sanity check */
Tejun Heof3421792010-07-02 10:03:51 +02003334 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02003335 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3336 int i;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003337
Tejun Heo73f53c42010-06-29 10:07:11 +02003338 for (i = 0; i < WORK_NR_COLORS; i++)
3339 BUG_ON(cwq->nr_in_flight[i]);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003340 BUG_ON(cwq->nr_active);
3341 BUG_ON(!list_empty(&cwq->delayed_works));
Tejun Heo73f53c42010-06-29 10:07:11 +02003342 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003343
Tejun Heoe22bee72010-06-29 10:07:14 +02003344 if (wq->flags & WQ_RESCUER) {
3345 kthread_stop(wq->rescuer->task);
Tejun Heof2e005a2010-07-20 15:59:09 +02003346 free_mayday_mask(wq->mayday_mask);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003347 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003348 }
3349
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003350 free_cwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003351 kfree(wq);
3352}
3353EXPORT_SYMBOL_GPL(destroy_workqueue);
3354
Tejun Heodcd989c2010-06-29 10:07:14 +02003355/**
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003356 * cwq_set_max_active - adjust max_active of a cwq
3357 * @cwq: target cpu_workqueue_struct
3358 * @max_active: new max_active value.
3359 *
3360 * Set @cwq->max_active to @max_active and activate delayed works if
3361 * increased.
3362 *
3363 * CONTEXT:
3364 * spin_lock_irq(gcwq->lock).
3365 */
3366static void cwq_set_max_active(struct cpu_workqueue_struct *cwq, int max_active)
3367{
3368 cwq->max_active = max_active;
3369
3370 while (!list_empty(&cwq->delayed_works) &&
3371 cwq->nr_active < cwq->max_active)
3372 cwq_activate_first_delayed(cwq);
3373}
3374
3375/**
Tejun Heodcd989c2010-06-29 10:07:14 +02003376 * workqueue_set_max_active - adjust max_active of a workqueue
3377 * @wq: target workqueue
3378 * @max_active: new max_active value.
3379 *
3380 * Set max_active of @wq to @max_active.
3381 *
3382 * CONTEXT:
3383 * Don't call from IRQ context.
3384 */
3385void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3386{
3387 unsigned int cpu;
3388
Tejun Heof3421792010-07-02 10:03:51 +02003389 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003390
3391 spin_lock(&workqueue_lock);
3392
3393 wq->saved_max_active = max_active;
3394
Tejun Heof3421792010-07-02 10:03:51 +02003395 for_each_cwq_cpu(cpu, wq) {
Tejun Heo35b6bb62013-01-24 11:01:33 -08003396 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3397 struct worker_pool *pool = cwq->pool;
3398 struct global_cwq *gcwq = pool->gcwq;
Tejun Heodcd989c2010-06-29 10:07:14 +02003399
3400 spin_lock_irq(&gcwq->lock);
3401
Tejun Heo58a69cb2011-02-16 09:25:31 +01003402 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heo35b6bb62013-01-24 11:01:33 -08003403 !(pool->flags & POOL_FREEZING))
3404 cwq_set_max_active(cwq, max_active);
Tejun Heodcd989c2010-06-29 10:07:14 +02003405
3406 spin_unlock_irq(&gcwq->lock);
3407 }
3408
3409 spin_unlock(&workqueue_lock);
3410}
3411EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3412
3413/**
3414 * workqueue_congested - test whether a workqueue is congested
3415 * @cpu: CPU in question
3416 * @wq: target workqueue
3417 *
3418 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3419 * no synchronization around this function and the test result is
3420 * unreliable and only useful as advisory hints or for debugging.
3421 *
3422 * RETURNS:
3423 * %true if congested, %false otherwise.
3424 */
3425bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3426{
3427 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3428
3429 return !list_empty(&cwq->delayed_works);
3430}
3431EXPORT_SYMBOL_GPL(workqueue_congested);
3432
3433/**
3434 * work_cpu - return the last known associated cpu for @work
3435 * @work: the work of interest
3436 *
3437 * RETURNS:
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003438 * CPU number if @work was ever queued. WORK_CPU_NONE otherwise.
Tejun Heodcd989c2010-06-29 10:07:14 +02003439 */
Tejun Heoe2905b22013-01-24 11:01:32 -08003440static unsigned int work_cpu(struct work_struct *work)
Tejun Heodcd989c2010-06-29 10:07:14 +02003441{
3442 struct global_cwq *gcwq = get_work_gcwq(work);
3443
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003444 return gcwq ? gcwq->cpu : WORK_CPU_NONE;
Tejun Heodcd989c2010-06-29 10:07:14 +02003445}
Tejun Heodcd989c2010-06-29 10:07:14 +02003446
3447/**
3448 * work_busy - test whether a work is currently pending or running
3449 * @work: the work to be tested
3450 *
3451 * Test whether @work is currently pending or running. There is no
3452 * synchronization around this function and the test result is
3453 * unreliable and only useful as advisory hints or for debugging.
3454 * Especially for reentrant wqs, the pending state might hide the
3455 * running state.
3456 *
3457 * RETURNS:
3458 * OR'd bitmask of WORK_BUSY_* bits.
3459 */
3460unsigned int work_busy(struct work_struct *work)
3461{
3462 struct global_cwq *gcwq = get_work_gcwq(work);
3463 unsigned long flags;
3464 unsigned int ret = 0;
3465
3466 if (!gcwq)
Joonsoo Kim999767b2012-10-21 01:30:06 +09003467 return 0;
Tejun Heodcd989c2010-06-29 10:07:14 +02003468
3469 spin_lock_irqsave(&gcwq->lock, flags);
3470
3471 if (work_pending(work))
3472 ret |= WORK_BUSY_PENDING;
3473 if (find_worker_executing_work(gcwq, work))
3474 ret |= WORK_BUSY_RUNNING;
3475
3476 spin_unlock_irqrestore(&gcwq->lock, flags);
3477
3478 return ret;
3479}
3480EXPORT_SYMBOL_GPL(work_busy);
3481
Tejun Heodb7bccf2010-06-29 10:07:12 +02003482/*
3483 * CPU hotplug.
3484 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003485 * There are two challenges in supporting CPU hotplug. Firstly, there
3486 * are a lot of assumptions on strong associations among work, cwq and
3487 * gcwq which make migrating pending and scheduled works very
3488 * difficult to implement without impacting hot paths. Secondly,
3489 * gcwqs serve mix of short, long and very long running works making
3490 * blocked draining impractical.
3491 *
Tejun Heo24647572013-01-24 11:01:33 -08003492 * This is solved by allowing the pools to be disassociated from the CPU
Tejun Heo628c78e2012-07-17 12:39:27 -07003493 * running as an unbound one and allowing it to be reattached later if the
3494 * cpu comes back online.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003495 */
3496
Tejun Heo60373152012-07-17 12:39:27 -07003497/* claim manager positions of all pools */
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07003498static void gcwq_claim_assoc_and_lock(struct global_cwq *gcwq)
Tejun Heo60373152012-07-17 12:39:27 -07003499{
3500 struct worker_pool *pool;
3501
3502 for_each_worker_pool(pool, gcwq)
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07003503 mutex_lock_nested(&pool->assoc_mutex, pool - gcwq->pools);
Tejun Heo8db25e72012-07-17 12:39:28 -07003504 spin_lock_irq(&gcwq->lock);
Tejun Heo60373152012-07-17 12:39:27 -07003505}
3506
3507/* release manager positions */
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07003508static void gcwq_release_assoc_and_unlock(struct global_cwq *gcwq)
Tejun Heo60373152012-07-17 12:39:27 -07003509{
3510 struct worker_pool *pool;
3511
Tejun Heo8db25e72012-07-17 12:39:28 -07003512 spin_unlock_irq(&gcwq->lock);
Tejun Heo60373152012-07-17 12:39:27 -07003513 for_each_worker_pool(pool, gcwq)
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07003514 mutex_unlock(&pool->assoc_mutex);
Tejun Heo60373152012-07-17 12:39:27 -07003515}
3516
Tejun Heo628c78e2012-07-17 12:39:27 -07003517static void gcwq_unbind_fn(struct work_struct *work)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003518{
Tejun Heo628c78e2012-07-17 12:39:27 -07003519 struct global_cwq *gcwq = get_gcwq(smp_processor_id());
Tejun Heo4ce62e92012-07-13 22:16:44 -07003520 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003521 struct worker *worker;
3522 struct hlist_node *pos;
3523 int i;
3524
3525 BUG_ON(gcwq->cpu != smp_processor_id());
3526
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07003527 gcwq_claim_assoc_and_lock(gcwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02003528
Tejun Heof2d5a0e2012-07-17 12:39:26 -07003529 /*
3530 * We've claimed all manager positions. Make all workers unbound
3531 * and set DISASSOCIATED. Before this, all workers except for the
3532 * ones which are still executing works from before the last CPU
3533 * down must be on the cpu. After this, they may become diasporas.
3534 */
Tejun Heo60373152012-07-17 12:39:27 -07003535 for_each_worker_pool(pool, gcwq)
Tejun Heo4ce62e92012-07-13 22:16:44 -07003536 list_for_each_entry(worker, &pool->idle_list, entry)
Tejun Heo403c8212012-07-17 12:39:27 -07003537 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003538
3539 for_each_busy_worker(worker, i, pos, gcwq)
Tejun Heo403c8212012-07-17 12:39:27 -07003540 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003541
Tejun Heo24647572013-01-24 11:01:33 -08003542 for_each_worker_pool(pool, gcwq)
3543 pool->flags |= POOL_DISASSOCIATED;
Tejun Heof2d5a0e2012-07-17 12:39:26 -07003544
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07003545 gcwq_release_assoc_and_unlock(gcwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02003546
3547 /*
Tejun Heo628c78e2012-07-17 12:39:27 -07003548 * Call schedule() so that we cross rq->lock and thus can guarantee
3549 * sched callbacks see the %WORKER_UNBOUND flag. This is necessary
3550 * as scheduler callbacks may be invoked from other cpus.
3551 */
3552 schedule();
3553
3554 /*
3555 * Sched callbacks are disabled now. Zap nr_running. After this,
3556 * nr_running stays zero and need_more_worker() and keep_working()
3557 * are always true as long as the worklist is not empty. @gcwq now
3558 * behaves as unbound (in terms of concurrency management) gcwq
3559 * which is served by workers tied to the CPU.
3560 *
3561 * On return from this function, the current worker would trigger
3562 * unbound chain execution of pending work items if other workers
3563 * didn't already.
Tejun Heoe22bee72010-06-29 10:07:14 +02003564 */
Tejun Heo4ce62e92012-07-13 22:16:44 -07003565 for_each_worker_pool(pool, gcwq)
3566 atomic_set(get_pool_nr_running(pool), 0);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003567}
3568
Tejun Heo8db25e72012-07-17 12:39:28 -07003569/*
3570 * Workqueues should be brought up before normal priority CPU notifiers.
3571 * This will be registered high priority CPU notifier.
3572 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07003573static int __cpuinit workqueue_cpu_up_callback(struct notifier_block *nfb,
Tejun Heo8db25e72012-07-17 12:39:28 -07003574 unsigned long action,
3575 void *hcpu)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003576{
3577 unsigned int cpu = (unsigned long)hcpu;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003578 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003579 struct worker_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580
Tejun Heo8db25e72012-07-17 12:39:28 -07003581 switch (action & ~CPU_TASKS_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 case CPU_UP_PREPARE:
Tejun Heo4ce62e92012-07-13 22:16:44 -07003583 for_each_worker_pool(pool, gcwq) {
Tejun Heo3ce63372012-07-17 12:39:27 -07003584 struct worker *worker;
3585
3586 if (pool->nr_workers)
3587 continue;
3588
3589 worker = create_worker(pool);
3590 if (!worker)
3591 return NOTIFY_BAD;
3592
3593 spin_lock_irq(&gcwq->lock);
3594 start_worker(worker);
3595 spin_unlock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003597 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003598
Tejun Heo65758202012-07-17 12:39:26 -07003599 case CPU_DOWN_FAILED:
3600 case CPU_ONLINE:
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07003601 gcwq_claim_assoc_and_lock(gcwq);
Tejun Heo24647572013-01-24 11:01:33 -08003602 for_each_worker_pool(pool, gcwq)
3603 pool->flags &= ~POOL_DISASSOCIATED;
Tejun Heo8db25e72012-07-17 12:39:28 -07003604 rebind_workers(gcwq);
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07003605 gcwq_release_assoc_and_unlock(gcwq);
Tejun Heo8db25e72012-07-17 12:39:28 -07003606 break;
Tejun Heo65758202012-07-17 12:39:26 -07003607 }
3608 return NOTIFY_OK;
3609}
3610
3611/*
3612 * Workqueues should be brought down after normal priority CPU notifiers.
3613 * This will be registered as low priority CPU notifier.
3614 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07003615static int __cpuinit workqueue_cpu_down_callback(struct notifier_block *nfb,
Tejun Heo65758202012-07-17 12:39:26 -07003616 unsigned long action,
3617 void *hcpu)
3618{
Tejun Heo8db25e72012-07-17 12:39:28 -07003619 unsigned int cpu = (unsigned long)hcpu;
3620 struct work_struct unbind_work;
3621
Tejun Heo65758202012-07-17 12:39:26 -07003622 switch (action & ~CPU_TASKS_FROZEN) {
3623 case CPU_DOWN_PREPARE:
Tejun Heo8db25e72012-07-17 12:39:28 -07003624 /* unbinding should happen on the local CPU */
3625 INIT_WORK_ONSTACK(&unbind_work, gcwq_unbind_fn);
Joonsoo Kim7635d2f2012-08-15 23:25:41 +09003626 queue_work_on(cpu, system_highpri_wq, &unbind_work);
Tejun Heo8db25e72012-07-17 12:39:28 -07003627 flush_work(&unbind_work);
3628 break;
Tejun Heo65758202012-07-17 12:39:26 -07003629 }
3630 return NOTIFY_OK;
3631}
3632
Rusty Russell2d3854a2008-11-05 13:39:10 +11003633#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003634
Rusty Russell2d3854a2008-11-05 13:39:10 +11003635struct work_for_cpu {
Tejun Heoed48ece2012-09-18 12:48:43 -07003636 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003637 long (*fn)(void *);
3638 void *arg;
3639 long ret;
3640};
3641
Tejun Heoed48ece2012-09-18 12:48:43 -07003642static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003643{
Tejun Heoed48ece2012-09-18 12:48:43 -07003644 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
3645
Rusty Russell2d3854a2008-11-05 13:39:10 +11003646 wfc->ret = wfc->fn(wfc->arg);
3647}
3648
3649/**
3650 * work_on_cpu - run a function in user context on a particular cpu
3651 * @cpu: the cpu to run on
3652 * @fn: the function to run
3653 * @arg: the function arg
3654 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003655 * This will return the value @fn returns.
3656 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06003657 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003658 */
3659long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3660{
Tejun Heoed48ece2012-09-18 12:48:43 -07003661 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003662
Tejun Heoed48ece2012-09-18 12:48:43 -07003663 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
3664 schedule_work_on(cpu, &wfc.work);
3665 flush_work(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003666 return wfc.ret;
3667}
3668EXPORT_SYMBOL_GPL(work_on_cpu);
3669#endif /* CONFIG_SMP */
3670
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003671#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303672
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003673/**
3674 * freeze_workqueues_begin - begin freezing workqueues
3675 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003676 * Start freezing workqueues. After this function returns, all freezable
3677 * workqueues will queue new works to their frozen_works list instead of
3678 * gcwq->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003679 *
3680 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003681 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003682 */
3683void freeze_workqueues_begin(void)
3684{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003685 unsigned int cpu;
3686
3687 spin_lock(&workqueue_lock);
3688
3689 BUG_ON(workqueue_freezing);
3690 workqueue_freezing = true;
3691
Tejun Heof3421792010-07-02 10:03:51 +02003692 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003693 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo35b6bb62013-01-24 11:01:33 -08003694 struct worker_pool *pool;
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003695 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003696
3697 spin_lock_irq(&gcwq->lock);
3698
Tejun Heo35b6bb62013-01-24 11:01:33 -08003699 for_each_worker_pool(pool, gcwq) {
3700 WARN_ON_ONCE(pool->flags & POOL_FREEZING);
3701 pool->flags |= POOL_FREEZING;
3702 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003703
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003704 list_for_each_entry(wq, &workqueues, list) {
3705 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3706
Tejun Heo58a69cb2011-02-16 09:25:31 +01003707 if (cwq && wq->flags & WQ_FREEZABLE)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003708 cwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003709 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003710
3711 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003712 }
3713
3714 spin_unlock(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003716
3717/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003718 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003719 *
3720 * Check whether freezing is complete. This function must be called
3721 * between freeze_workqueues_begin() and thaw_workqueues().
3722 *
3723 * CONTEXT:
3724 * Grabs and releases workqueue_lock.
3725 *
3726 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01003727 * %true if some freezable workqueues are still busy. %false if freezing
3728 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003729 */
3730bool freeze_workqueues_busy(void)
3731{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003732 unsigned int cpu;
3733 bool busy = false;
3734
3735 spin_lock(&workqueue_lock);
3736
3737 BUG_ON(!workqueue_freezing);
3738
Tejun Heof3421792010-07-02 10:03:51 +02003739 for_each_gcwq_cpu(cpu) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003740 struct workqueue_struct *wq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003741 /*
3742 * nr_active is monotonically decreasing. It's safe
3743 * to peek without lock.
3744 */
3745 list_for_each_entry(wq, &workqueues, list) {
3746 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3747
Tejun Heo58a69cb2011-02-16 09:25:31 +01003748 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003749 continue;
3750
3751 BUG_ON(cwq->nr_active < 0);
3752 if (cwq->nr_active) {
3753 busy = true;
3754 goto out_unlock;
3755 }
3756 }
3757 }
3758out_unlock:
3759 spin_unlock(&workqueue_lock);
3760 return busy;
3761}
3762
3763/**
3764 * thaw_workqueues - thaw workqueues
3765 *
3766 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo7e116292010-06-29 10:07:13 +02003767 * frozen works are transferred to their respective gcwq worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003768 *
3769 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003770 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003771 */
3772void thaw_workqueues(void)
3773{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003774 unsigned int cpu;
3775
3776 spin_lock(&workqueue_lock);
3777
3778 if (!workqueue_freezing)
3779 goto out_unlock;
3780
Tejun Heof3421792010-07-02 10:03:51 +02003781 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003782 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003783 struct worker_pool *pool;
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003784 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003785
3786 spin_lock_irq(&gcwq->lock);
3787
Tejun Heo35b6bb62013-01-24 11:01:33 -08003788 for_each_worker_pool(pool, gcwq) {
3789 WARN_ON_ONCE(!(pool->flags & POOL_FREEZING));
3790 pool->flags &= ~POOL_FREEZING;
3791 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003792
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003793 list_for_each_entry(wq, &workqueues, list) {
3794 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3795
Tejun Heo58a69cb2011-02-16 09:25:31 +01003796 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003797 continue;
3798
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003799 /* restore max_active and repopulate worklist */
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003800 cwq_set_max_active(cwq, wq->saved_max_active);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003801 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003802
Tejun Heo4ce62e92012-07-13 22:16:44 -07003803 for_each_worker_pool(pool, gcwq)
3804 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02003805
Tejun Heo8b03ae32010-06-29 10:07:12 +02003806 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003807 }
3808
3809 workqueue_freezing = false;
3810out_unlock:
3811 spin_unlock(&workqueue_lock);
3812}
3813#endif /* CONFIG_FREEZER */
3814
Suresh Siddha6ee05782010-07-30 14:57:37 -07003815static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816{
Tejun Heoc34056a2010-06-29 10:07:11 +02003817 unsigned int cpu;
3818
Tejun Heob5490072012-08-03 10:30:46 -07003819 /* make sure we have enough bits for OFFQ CPU number */
3820 BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_CPU_SHIFT)) <
3821 WORK_CPU_LAST);
3822
Tejun Heo65758202012-07-17 12:39:26 -07003823 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
Lai Jiangshana5b4e572012-09-18 09:59:23 -07003824 hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003825
3826 /* initialize gcwqs */
Tejun Heof3421792010-07-02 10:03:51 +02003827 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003828 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003829 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003830
3831 spin_lock_init(&gcwq->lock);
3832 gcwq->cpu = cpu;
3833
Sasha Levin42f85702012-12-17 10:01:23 -05003834 hash_init(gcwq->busy_hash);
Tejun Heoc8e55f32010-06-29 10:07:12 +02003835
Tejun Heo4ce62e92012-07-13 22:16:44 -07003836 for_each_worker_pool(pool, gcwq) {
3837 pool->gcwq = gcwq;
Tejun Heo24647572013-01-24 11:01:33 -08003838 pool->flags |= POOL_DISASSOCIATED;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003839 INIT_LIST_HEAD(&pool->worklist);
3840 INIT_LIST_HEAD(&pool->idle_list);
Tejun Heoe22bee72010-06-29 10:07:14 +02003841
Tejun Heo4ce62e92012-07-13 22:16:44 -07003842 init_timer_deferrable(&pool->idle_timer);
3843 pool->idle_timer.function = idle_worker_timeout;
3844 pool->idle_timer.data = (unsigned long)pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003845
Tejun Heo4ce62e92012-07-13 22:16:44 -07003846 setup_timer(&pool->mayday_timer, gcwq_mayday_timeout,
3847 (unsigned long)pool);
3848
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07003849 mutex_init(&pool->assoc_mutex);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003850 ida_init(&pool->worker_ida);
Tejun Heo9daf9e62013-01-24 11:01:33 -08003851
3852 /* alloc pool ID */
3853 BUG_ON(worker_pool_assign_id(pool));
Tejun Heo4ce62e92012-07-13 22:16:44 -07003854 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003855 }
3856
Tejun Heoe22bee72010-06-29 10:07:14 +02003857 /* create the initial worker */
Tejun Heof3421792010-07-02 10:03:51 +02003858 for_each_online_gcwq_cpu(cpu) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003859 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003860 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003861
Tejun Heo4ce62e92012-07-13 22:16:44 -07003862 for_each_worker_pool(pool, gcwq) {
3863 struct worker *worker;
3864
Tejun Heo24647572013-01-24 11:01:33 -08003865 if (cpu != WORK_CPU_UNBOUND)
3866 pool->flags &= ~POOL_DISASSOCIATED;
3867
Tejun Heobc2ae0f2012-07-17 12:39:27 -07003868 worker = create_worker(pool);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003869 BUG_ON(!worker);
3870 spin_lock_irq(&gcwq->lock);
3871 start_worker(worker);
3872 spin_unlock_irq(&gcwq->lock);
3873 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003874 }
3875
Tejun Heod320c032010-06-29 10:07:14 +02003876 system_wq = alloc_workqueue("events", 0, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09003877 system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
Tejun Heod320c032010-06-29 10:07:14 +02003878 system_long_wq = alloc_workqueue("events_long", 0, 0);
Tejun Heof3421792010-07-02 10:03:51 +02003879 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3880 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01003881 system_freezable_wq = alloc_workqueue("events_freezable",
3882 WQ_FREEZABLE, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09003883 BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
Tejun Heoae930e02012-08-20 14:51:23 -07003884 !system_unbound_wq || !system_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07003885 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886}
Suresh Siddha6ee05782010-07-30 14:57:37 -07003887early_initcall(init_workqueues);