blob: a4d7e3f0a87415abee8a3727a102a0ef463b8922 [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Tejun Heoe34cdddb2013-01-24 11:01:33 -0800455static int std_worker_pool_pri(struct worker_pool *pool)
Tejun Heo32704762012-07-13 22:16:45 -0700456{
457 return pool - pool->gcwq->pools;
458}
459
Tejun Heo8b03ae32010-06-29 10:07:12 +0200460static struct global_cwq *get_gcwq(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Tejun Heof3421792010-07-02 10:03:51 +0200462 if (cpu != WORK_CPU_UNBOUND)
463 return &per_cpu(global_cwq, cpu);
464 else
465 return &unbound_global_cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
Tejun Heo9daf9e62013-01-24 11:01:33 -0800468/* allocate ID and assign it to @pool */
469static int worker_pool_assign_id(struct worker_pool *pool)
470{
471 int ret;
472
473 mutex_lock(&worker_pool_idr_mutex);
474 idr_pre_get(&worker_pool_idr, GFP_KERNEL);
475 ret = idr_get_new(&worker_pool_idr, pool, &pool->id);
476 mutex_unlock(&worker_pool_idr_mutex);
477
478 return ret;
479}
480
Tejun Heo7c3eed52013-01-24 11:01:33 -0800481/*
482 * Lookup worker_pool by id. The idr currently is built during boot and
483 * never modified. Don't worry about locking for now.
484 */
485static struct worker_pool *worker_pool_by_id(int pool_id)
486{
487 return idr_find(&worker_pool_idr, pool_id);
488}
489
Tejun Heo63d95a92012-07-12 14:46:37 -0700490static atomic_t *get_pool_nr_running(struct worker_pool *pool)
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700491{
Tejun Heo63d95a92012-07-12 14:46:37 -0700492 int cpu = pool->gcwq->cpu;
Tejun Heoe34cdddb2013-01-24 11:01:33 -0800493 int idx = std_worker_pool_pri(pool);
Tejun Heo63d95a92012-07-12 14:46:37 -0700494
Tejun Heof3421792010-07-02 10:03:51 +0200495 if (cpu != WORK_CPU_UNBOUND)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700496 return &per_cpu(pool_nr_running, cpu)[idx];
Tejun Heof3421792010-07-02 10:03:51 +0200497 else
Tejun Heo4ce62e92012-07-13 22:16:44 -0700498 return &unbound_pool_nr_running[idx];
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700499}
500
Tejun Heo4690c4a2010-06-29 10:07:10 +0200501static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
502 struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700503{
Tejun Heof3421792010-07-02 10:03:51 +0200504 if (!(wq->flags & WQ_UNBOUND)) {
Lai Jiangshane06ffa12012-03-09 18:03:20 +0800505 if (likely(cpu < nr_cpu_ids))
Tejun Heof3421792010-07-02 10:03:51 +0200506 return per_cpu_ptr(wq->cpu_wq.pcpu, cpu);
Tejun Heof3421792010-07-02 10:03:51 +0200507 } else if (likely(cpu == WORK_CPU_UNBOUND))
508 return wq->cpu_wq.single;
509 return NULL;
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700510}
511
Tejun Heo73f53c42010-06-29 10:07:11 +0200512static unsigned int work_color_to_flags(int color)
513{
514 return color << WORK_STRUCT_COLOR_SHIFT;
515}
516
517static int get_work_color(struct work_struct *work)
518{
519 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
520 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
521}
522
523static int work_next_color(int color)
524{
525 return (color + 1) % WORK_NR_COLORS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527
David Howells4594bf12006-12-07 11:33:26 +0000528/*
Tejun Heob5490072012-08-03 10:30:46 -0700529 * While queued, %WORK_STRUCT_CWQ is set and non flag bits of a work's data
530 * contain the pointer to the queued cwq. Once execution starts, the flag
Tejun Heo7c3eed52013-01-24 11:01:33 -0800531 * is cleared and the high bits contain OFFQ flags and pool ID.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200532 *
Tejun Heo7c3eed52013-01-24 11:01:33 -0800533 * set_work_cwq(), set_work_pool_and_clear_pending(), mark_work_canceling()
534 * and clear_work_data() can be used to set the cwq, pool or clear
Tejun Heobbb68df2012-08-03 10:30:46 -0700535 * work->data. These functions should only be called while the work is
536 * owned - ie. while the PENDING bit is set.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200537 *
Tejun Heo7c3eed52013-01-24 11:01:33 -0800538 * get_work_pool() and get_work_cwq() can be used to obtain the pool or cwq
539 * corresponding to a work. Pool is available once the work has been
540 * queued anywhere after initialization until it is sync canceled. cwq is
541 * available only while the work item is queued.
Tejun Heobbb68df2012-08-03 10:30:46 -0700542 *
543 * %WORK_OFFQ_CANCELING is used to mark a work item which is being
544 * canceled. While being canceled, a work item may have its PENDING set
545 * but stay off timer and worklist for arbitrarily long and nobody should
546 * try to steal the PENDING bit.
David Howells4594bf12006-12-07 11:33:26 +0000547 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200548static inline void set_work_data(struct work_struct *work, unsigned long data,
549 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000550{
David Howells4594bf12006-12-07 11:33:26 +0000551 BUG_ON(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200552 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000553}
David Howells365970a2006-11-22 14:54:49 +0000554
Tejun Heo7a22ad72010-06-29 10:07:13 +0200555static void set_work_cwq(struct work_struct *work,
556 struct cpu_workqueue_struct *cwq,
557 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200558{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200559 set_work_data(work, (unsigned long)cwq,
Tejun Heoe1201532010-07-22 14:14:25 +0200560 WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200561}
562
Tejun Heo7c3eed52013-01-24 11:01:33 -0800563static void set_work_pool_and_clear_pending(struct work_struct *work,
564 int pool_id)
David Howells365970a2006-11-22 14:54:49 +0000565{
Tejun Heo23657bb2012-08-13 17:08:19 -0700566 /*
567 * The following wmb is paired with the implied mb in
568 * test_and_set_bit(PENDING) and ensures all updates to @work made
569 * here are visible to and precede any updates by the next PENDING
570 * owner.
571 */
572 smp_wmb();
Tejun Heo7c3eed52013-01-24 11:01:33 -0800573 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200574}
575
576static void clear_work_data(struct work_struct *work)
577{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800578 smp_wmb(); /* see set_work_pool_and_clear_pending() */
579 set_work_data(work, WORK_STRUCT_NO_POOL, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200580}
581
Tejun Heo7a22ad72010-06-29 10:07:13 +0200582static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work)
583{
Tejun Heoe1201532010-07-22 14:14:25 +0200584 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200585
Tejun Heoe1201532010-07-22 14:14:25 +0200586 if (data & WORK_STRUCT_CWQ)
587 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
588 else
589 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200590}
591
Tejun Heo7c3eed52013-01-24 11:01:33 -0800592/**
593 * get_work_pool - return the worker_pool a given work was associated with
594 * @work: the work item of interest
595 *
596 * Return the worker_pool @work was last associated with. %NULL if none.
597 */
598static struct worker_pool *get_work_pool(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200599{
Tejun Heoe1201532010-07-22 14:14:25 +0200600 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800601 struct worker_pool *pool;
602 int pool_id;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200603
Tejun Heoe1201532010-07-22 14:14:25 +0200604 if (data & WORK_STRUCT_CWQ)
605 return ((struct cpu_workqueue_struct *)
Tejun Heo7c3eed52013-01-24 11:01:33 -0800606 (data & WORK_STRUCT_WQ_DATA_MASK))->pool;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200607
Tejun Heo7c3eed52013-01-24 11:01:33 -0800608 pool_id = data >> WORK_OFFQ_POOL_SHIFT;
609 if (pool_id == WORK_OFFQ_POOL_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200610 return NULL;
611
Tejun Heo7c3eed52013-01-24 11:01:33 -0800612 pool = worker_pool_by_id(pool_id);
613 WARN_ON_ONCE(!pool);
614 return pool;
615}
616
617/**
618 * get_work_pool_id - return the worker pool ID a given work is associated with
619 * @work: the work item of interest
620 *
621 * Return the worker_pool ID @work was last associated with.
622 * %WORK_OFFQ_POOL_NONE if none.
623 */
624static int get_work_pool_id(struct work_struct *work)
625{
626 struct worker_pool *pool = get_work_pool(work);
627
628 return pool ? pool->id : WORK_OFFQ_POOL_NONE;
629}
630
631static struct global_cwq *get_work_gcwq(struct work_struct *work)
632{
633 struct worker_pool *pool = get_work_pool(work);
634
635 return pool ? pool->gcwq : NULL;
David Howells365970a2006-11-22 14:54:49 +0000636}
637
Tejun Heobbb68df2012-08-03 10:30:46 -0700638static void mark_work_canceling(struct work_struct *work)
639{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800640 unsigned long pool_id = get_work_pool_id(work);
Tejun Heobbb68df2012-08-03 10:30:46 -0700641
Tejun Heo7c3eed52013-01-24 11:01:33 -0800642 pool_id <<= WORK_OFFQ_POOL_SHIFT;
643 set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700644}
645
646static bool work_is_canceling(struct work_struct *work)
647{
648 unsigned long data = atomic_long_read(&work->data);
649
650 return !(data & WORK_STRUCT_CWQ) && (data & WORK_OFFQ_CANCELING);
651}
652
David Howells365970a2006-11-22 14:54:49 +0000653/*
Tejun Heo32704762012-07-13 22:16:45 -0700654 * Policy functions. These define the policies on how the global worker
655 * pools are managed. Unless noted otherwise, these functions assume that
656 * they're being called with gcwq->lock held.
David Howells365970a2006-11-22 14:54:49 +0000657 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200658
Tejun Heo63d95a92012-07-12 14:46:37 -0700659static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000660{
Tejun Heo32704762012-07-13 22:16:45 -0700661 return !atomic_read(get_pool_nr_running(pool));
David Howells365970a2006-11-22 14:54:49 +0000662}
663
Tejun Heoe22bee72010-06-29 10:07:14 +0200664/*
665 * Need to wake up a worker? Called from anything but currently
666 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700667 *
668 * Note that, because unbound workers never contribute to nr_running, this
669 * function will always return %true for unbound gcwq as long as the
670 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200671 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700672static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000673{
Tejun Heo63d95a92012-07-12 14:46:37 -0700674 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000675}
676
Tejun Heoe22bee72010-06-29 10:07:14 +0200677/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700678static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200679{
Tejun Heo63d95a92012-07-12 14:46:37 -0700680 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200681}
682
683/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700684static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200685{
Tejun Heo63d95a92012-07-12 14:46:37 -0700686 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200687
Tejun Heo32704762012-07-13 22:16:45 -0700688 return !list_empty(&pool->worklist) && atomic_read(nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200689}
690
691/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700692static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200693{
Tejun Heo63d95a92012-07-12 14:46:37 -0700694 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200695}
696
697/* Do I need to be the manager? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700698static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200699{
Tejun Heo63d95a92012-07-12 14:46:37 -0700700 return need_to_create_worker(pool) ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700701 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200702}
703
704/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700705static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200706{
Lai Jiangshan552a37e2012-09-10 10:03:33 -0700707 bool managing = pool->flags & POOL_MANAGING_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -0700708 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
709 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200710
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700711 /*
712 * nr_idle and idle_list may disagree if idle rebinding is in
713 * progress. Never return %true if idle_list is empty.
714 */
715 if (list_empty(&pool->idle_list))
716 return false;
717
Tejun Heoe22bee72010-06-29 10:07:14 +0200718 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
719}
720
721/*
722 * Wake up functions.
723 */
724
Tejun Heo7e116292010-06-29 10:07:13 +0200725/* Return the first worker. Safe with preemption disabled */
Tejun Heo63d95a92012-07-12 14:46:37 -0700726static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200727{
Tejun Heo63d95a92012-07-12 14:46:37 -0700728 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200729 return NULL;
730
Tejun Heo63d95a92012-07-12 14:46:37 -0700731 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200732}
733
734/**
735 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700736 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200737 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700738 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200739 *
740 * CONTEXT:
741 * spin_lock_irq(gcwq->lock).
742 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700743static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200744{
Tejun Heo63d95a92012-07-12 14:46:37 -0700745 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200746
747 if (likely(worker))
748 wake_up_process(worker->task);
749}
750
Tejun Heo4690c4a2010-06-29 10:07:10 +0200751/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200752 * wq_worker_waking_up - a worker is waking up
753 * @task: task waking up
754 * @cpu: CPU @task is waking up to
755 *
756 * This function is called during try_to_wake_up() when a worker is
757 * being awoken.
758 *
759 * CONTEXT:
760 * spin_lock_irq(rq->lock)
761 */
762void wq_worker_waking_up(struct task_struct *task, unsigned int cpu)
763{
764 struct worker *worker = kthread_data(task);
765
Joonsoo Kim36576002012-10-26 23:03:49 +0900766 if (!(worker->flags & WORKER_NOT_RUNNING)) {
767 WARN_ON_ONCE(worker->pool->gcwq->cpu != cpu);
Tejun Heo63d95a92012-07-12 14:46:37 -0700768 atomic_inc(get_pool_nr_running(worker->pool));
Joonsoo Kim36576002012-10-26 23:03:49 +0900769 }
Tejun Heoe22bee72010-06-29 10:07:14 +0200770}
771
772/**
773 * wq_worker_sleeping - a worker is going to sleep
774 * @task: task going to sleep
775 * @cpu: CPU in question, must be the current CPU number
776 *
777 * This function is called during schedule() when a busy worker is
778 * going to sleep. Worker on the same cpu can be woken up by
779 * returning pointer to its task.
780 *
781 * CONTEXT:
782 * spin_lock_irq(rq->lock)
783 *
784 * RETURNS:
785 * Worker task on @cpu to wake up, %NULL if none.
786 */
787struct task_struct *wq_worker_sleeping(struct task_struct *task,
788 unsigned int cpu)
789{
790 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heo111c2252013-01-17 17:16:24 -0800791 struct worker_pool *pool;
792 atomic_t *nr_running;
Tejun Heoe22bee72010-06-29 10:07:14 +0200793
Tejun Heo111c2252013-01-17 17:16:24 -0800794 /*
795 * Rescuers, which may not have all the fields set up like normal
796 * workers, also reach here, let's not access anything before
797 * checking NOT_RUNNING.
798 */
Steven Rostedt2d646722010-12-03 23:12:33 -0500799 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200800 return NULL;
801
Tejun Heo111c2252013-01-17 17:16:24 -0800802 pool = worker->pool;
803 nr_running = get_pool_nr_running(pool);
804
Tejun Heoe22bee72010-06-29 10:07:14 +0200805 /* this can only happen on the local cpu */
806 BUG_ON(cpu != raw_smp_processor_id());
807
808 /*
809 * The counterpart of the following dec_and_test, implied mb,
810 * worklist not empty test sequence is in insert_work().
811 * Please read comment there.
812 *
Tejun Heo628c78e2012-07-17 12:39:27 -0700813 * NOT_RUNNING is clear. This means that we're bound to and
814 * running on the local cpu w/ rq lock held and preemption
815 * disabled, which in turn means that none else could be
816 * manipulating idle_list, so dereferencing idle_list without gcwq
817 * lock is safe.
Tejun Heoe22bee72010-06-29 10:07:14 +0200818 */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700819 if (atomic_dec_and_test(nr_running) && !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700820 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200821 return to_wakeup ? to_wakeup->task : NULL;
822}
823
824/**
825 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200826 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200827 * @flags: flags to set
828 * @wakeup: wakeup an idle worker if necessary
829 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200830 * Set @flags in @worker->flags and adjust nr_running accordingly. If
831 * nr_running becomes zero and @wakeup is %true, an idle worker is
832 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200833 *
Tejun Heocb444762010-07-02 10:03:50 +0200834 * CONTEXT:
835 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200836 */
837static inline void worker_set_flags(struct worker *worker, unsigned int flags,
838 bool wakeup)
839{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700840 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200841
Tejun Heocb444762010-07-02 10:03:50 +0200842 WARN_ON_ONCE(worker->task != current);
843
Tejun Heoe22bee72010-06-29 10:07:14 +0200844 /*
845 * If transitioning into NOT_RUNNING, adjust nr_running and
846 * wake up an idle worker as necessary if requested by
847 * @wakeup.
848 */
849 if ((flags & WORKER_NOT_RUNNING) &&
850 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heo63d95a92012-07-12 14:46:37 -0700851 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200852
853 if (wakeup) {
854 if (atomic_dec_and_test(nr_running) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -0700855 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700856 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200857 } else
858 atomic_dec(nr_running);
859 }
860
Tejun Heod302f012010-06-29 10:07:13 +0200861 worker->flags |= flags;
862}
863
864/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200865 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200866 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200867 * @flags: flags to clear
868 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200869 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200870 *
Tejun Heocb444762010-07-02 10:03:50 +0200871 * CONTEXT:
872 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200873 */
874static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
875{
Tejun Heo63d95a92012-07-12 14:46:37 -0700876 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200877 unsigned int oflags = worker->flags;
878
Tejun Heocb444762010-07-02 10:03:50 +0200879 WARN_ON_ONCE(worker->task != current);
880
Tejun Heod302f012010-06-29 10:07:13 +0200881 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200882
Tejun Heo42c025f2011-01-11 15:58:49 +0100883 /*
884 * If transitioning out of NOT_RUNNING, increment nr_running. Note
885 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
886 * of multiple flags, not a single flag.
887 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200888 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
889 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heo63d95a92012-07-12 14:46:37 -0700890 atomic_inc(get_pool_nr_running(pool));
Tejun Heod302f012010-06-29 10:07:13 +0200891}
892
893/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200894 * find_worker_executing_work - find worker which is executing a work
895 * @gcwq: gcwq of interest
896 * @work: work to find worker for
897 *
Tejun Heoa2c1c572012-12-18 10:35:02 -0800898 * Find a worker which is executing @work on @gcwq by searching
899 * @gcwq->busy_hash which is keyed by the address of @work. For a worker
900 * to match, its current execution should match the address of @work and
901 * its work function. This is to avoid unwanted dependency between
902 * unrelated work executions through a work item being recycled while still
903 * being executed.
904 *
905 * This is a bit tricky. A work item may be freed once its execution
906 * starts and nothing prevents the freed area from being recycled for
907 * another work item. If the same work item address ends up being reused
908 * before the original execution finishes, workqueue will identify the
909 * recycled work item as currently executing and make it wait until the
910 * current execution finishes, introducing an unwanted dependency.
911 *
912 * This function checks the work item address, work function and workqueue
913 * to avoid false positives. Note that this isn't complete as one may
914 * construct a work function which can introduce dependency onto itself
915 * through a recycled work item. Well, if somebody wants to shoot oneself
916 * in the foot that badly, there's only so much we can do, and if such
917 * deadlock actually occurs, it should be easy to locate the culprit work
918 * function.
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200919 *
920 * CONTEXT:
921 * spin_lock_irq(gcwq->lock).
922 *
923 * RETURNS:
924 * Pointer to worker which is executing @work if found, NULL
925 * otherwise.
926 */
927static struct worker *find_worker_executing_work(struct global_cwq *gcwq,
928 struct work_struct *work)
929{
Sasha Levin42f85702012-12-17 10:01:23 -0500930 struct worker *worker;
931 struct hlist_node *tmp;
932
Tejun Heoa2c1c572012-12-18 10:35:02 -0800933 hash_for_each_possible(gcwq->busy_hash, worker, tmp, hentry,
934 (unsigned long)work)
935 if (worker->current_work == work &&
936 worker->current_func == work->func)
Sasha Levin42f85702012-12-17 10:01:23 -0500937 return worker;
938
939 return NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200940}
941
942/**
Tejun Heobf4ede02012-08-03 10:30:46 -0700943 * move_linked_works - move linked works to a list
944 * @work: start of series of works to be scheduled
945 * @head: target list to append @work to
946 * @nextp: out paramter for nested worklist walking
947 *
948 * Schedule linked works starting from @work to @head. Work series to
949 * be scheduled starts at @work and includes any consecutive work with
950 * WORK_STRUCT_LINKED set in its predecessor.
951 *
952 * If @nextp is not NULL, it's updated to point to the next work of
953 * the last scheduled work. This allows move_linked_works() to be
954 * nested inside outer list_for_each_entry_safe().
955 *
956 * CONTEXT:
957 * spin_lock_irq(gcwq->lock).
958 */
959static void move_linked_works(struct work_struct *work, struct list_head *head,
960 struct work_struct **nextp)
961{
962 struct work_struct *n;
963
964 /*
965 * Linked worklist will always end before the end of the list,
966 * use NULL for list head.
967 */
968 list_for_each_entry_safe_from(work, n, NULL, entry) {
969 list_move_tail(&work->entry, head);
970 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
971 break;
972 }
973
974 /*
975 * If we're already inside safe list traversal and have moved
976 * multiple works to the scheduled queue, the next position
977 * needs to be updated.
978 */
979 if (nextp)
980 *nextp = n;
981}
982
Lai Jiangshan3aa62492012-09-18 10:40:00 -0700983static void cwq_activate_delayed_work(struct work_struct *work)
Tejun Heobf4ede02012-08-03 10:30:46 -0700984{
Lai Jiangshan3aa62492012-09-18 10:40:00 -0700985 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heobf4ede02012-08-03 10:30:46 -0700986
987 trace_workqueue_activate_work(work);
988 move_linked_works(work, &cwq->pool->worklist, NULL);
989 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
990 cwq->nr_active++;
991}
992
Lai Jiangshan3aa62492012-09-18 10:40:00 -0700993static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
994{
995 struct work_struct *work = list_first_entry(&cwq->delayed_works,
996 struct work_struct, entry);
997
998 cwq_activate_delayed_work(work);
999}
1000
Tejun Heobf4ede02012-08-03 10:30:46 -07001001/**
1002 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
1003 * @cwq: cwq of interest
1004 * @color: color of work which left the queue
Tejun Heobf4ede02012-08-03 10:30:46 -07001005 *
1006 * A work either has completed or is removed from pending queue,
1007 * decrement nr_in_flight of its cwq and handle workqueue flushing.
1008 *
1009 * CONTEXT:
1010 * spin_lock_irq(gcwq->lock).
1011 */
Lai Jiangshanb3f9f402012-09-18 10:40:00 -07001012static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color)
Tejun Heobf4ede02012-08-03 10:30:46 -07001013{
1014 /* ignore uncolored works */
1015 if (color == WORK_NO_COLOR)
1016 return;
1017
1018 cwq->nr_in_flight[color]--;
1019
Lai Jiangshanb3f9f402012-09-18 10:40:00 -07001020 cwq->nr_active--;
1021 if (!list_empty(&cwq->delayed_works)) {
1022 /* one down, submit a delayed one */
1023 if (cwq->nr_active < cwq->max_active)
1024 cwq_activate_first_delayed(cwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001025 }
1026
1027 /* is flush in progress and are we at the flushing tip? */
1028 if (likely(cwq->flush_color != color))
1029 return;
1030
1031 /* are there still in-flight works? */
1032 if (cwq->nr_in_flight[color])
1033 return;
1034
1035 /* this cwq is done, clear flush_color */
1036 cwq->flush_color = -1;
1037
1038 /*
1039 * If this was the last cwq, wake up the first flusher. It
1040 * will handle the rest.
1041 */
1042 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
1043 complete(&cwq->wq->first_flusher->done);
1044}
1045
Tejun Heo36e227d2012-08-03 10:30:46 -07001046/**
Tejun Heobbb68df2012-08-03 10:30:46 -07001047 * try_to_grab_pending - steal work item from worklist and disable irq
Tejun Heo36e227d2012-08-03 10:30:46 -07001048 * @work: work item to steal
1049 * @is_dwork: @work is a delayed_work
Tejun Heobbb68df2012-08-03 10:30:46 -07001050 * @flags: place to store irq state
Tejun Heo36e227d2012-08-03 10:30:46 -07001051 *
1052 * Try to grab PENDING bit of @work. This function can handle @work in any
1053 * stable state - idle, on timer or on worklist. Return values are
1054 *
1055 * 1 if @work was pending and we successfully stole PENDING
1056 * 0 if @work was idle and we claimed PENDING
1057 * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry
Tejun Heobbb68df2012-08-03 10:30:46 -07001058 * -ENOENT if someone else is canceling @work, this state may persist
1059 * for arbitrarily long
Tejun Heo36e227d2012-08-03 10:30:46 -07001060 *
Tejun Heobbb68df2012-08-03 10:30:46 -07001061 * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001062 * interrupted while holding PENDING and @work off queue, irq must be
1063 * disabled on entry. This, combined with delayed_work->timer being
1064 * irqsafe, ensures that we return -EAGAIN for finite short period of time.
Tejun Heobbb68df2012-08-03 10:30:46 -07001065 *
1066 * On successful return, >= 0, irq is disabled and the caller is
1067 * responsible for releasing it using local_irq_restore(*@flags).
1068 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001069 * This function is safe to call from any context including IRQ handler.
Tejun Heobf4ede02012-08-03 10:30:46 -07001070 */
Tejun Heobbb68df2012-08-03 10:30:46 -07001071static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
1072 unsigned long *flags)
Tejun Heobf4ede02012-08-03 10:30:46 -07001073{
1074 struct global_cwq *gcwq;
Tejun Heobf4ede02012-08-03 10:30:46 -07001075
Tejun Heobbb68df2012-08-03 10:30:46 -07001076 local_irq_save(*flags);
1077
Tejun Heo36e227d2012-08-03 10:30:46 -07001078 /* try to steal the timer if it exists */
1079 if (is_dwork) {
1080 struct delayed_work *dwork = to_delayed_work(work);
1081
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001082 /*
1083 * dwork->timer is irqsafe. If del_timer() fails, it's
1084 * guaranteed that the timer is not queued anywhere and not
1085 * running on the local CPU.
1086 */
Tejun Heo36e227d2012-08-03 10:30:46 -07001087 if (likely(del_timer(&dwork->timer)))
1088 return 1;
1089 }
1090
1091 /* try to claim PENDING the normal way */
Tejun Heobf4ede02012-08-03 10:30:46 -07001092 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1093 return 0;
1094
1095 /*
1096 * The queueing is in progress, or it is already queued. Try to
1097 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1098 */
1099 gcwq = get_work_gcwq(work);
1100 if (!gcwq)
Tejun Heobbb68df2012-08-03 10:30:46 -07001101 goto fail;
Tejun Heobf4ede02012-08-03 10:30:46 -07001102
Tejun Heobbb68df2012-08-03 10:30:46 -07001103 spin_lock(&gcwq->lock);
Tejun Heobf4ede02012-08-03 10:30:46 -07001104 if (!list_empty(&work->entry)) {
1105 /*
1106 * This work is queued, but perhaps we locked the wrong gcwq.
1107 * In that case we must see the new value after rmb(), see
1108 * insert_work()->wmb().
1109 */
1110 smp_rmb();
1111 if (gcwq == get_work_gcwq(work)) {
1112 debug_work_deactivate(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001113
1114 /*
1115 * A delayed work item cannot be grabbed directly
1116 * because it might have linked NO_COLOR work items
1117 * which, if left on the delayed_list, will confuse
1118 * cwq->nr_active management later on and cause
1119 * stall. Make sure the work item is activated
1120 * before grabbing.
1121 */
1122 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
1123 cwq_activate_delayed_work(work);
1124
Tejun Heobf4ede02012-08-03 10:30:46 -07001125 list_del_init(&work->entry);
1126 cwq_dec_nr_in_flight(get_work_cwq(work),
Lai Jiangshanb3f9f402012-09-18 10:40:00 -07001127 get_work_color(work));
Tejun Heo36e227d2012-08-03 10:30:46 -07001128
Tejun Heobbb68df2012-08-03 10:30:46 -07001129 spin_unlock(&gcwq->lock);
Tejun Heo36e227d2012-08-03 10:30:46 -07001130 return 1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001131 }
1132 }
Tejun Heobbb68df2012-08-03 10:30:46 -07001133 spin_unlock(&gcwq->lock);
1134fail:
1135 local_irq_restore(*flags);
1136 if (work_is_canceling(work))
1137 return -ENOENT;
1138 cpu_relax();
Tejun Heo36e227d2012-08-03 10:30:46 -07001139 return -EAGAIN;
Tejun Heobf4ede02012-08-03 10:30:46 -07001140}
1141
1142/**
Tejun Heo7e116292010-06-29 10:07:13 +02001143 * insert_work - insert a work into gcwq
Tejun Heo4690c4a2010-06-29 10:07:10 +02001144 * @cwq: cwq @work belongs to
1145 * @work: work to insert
1146 * @head: insertion point
1147 * @extra_flags: extra WORK_STRUCT_* flags to set
1148 *
Tejun Heo7e116292010-06-29 10:07:13 +02001149 * Insert @work which belongs to @cwq into @gcwq after @head.
1150 * @extra_flags is or'd to work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +02001151 *
1152 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001153 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02001154 */
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001155static void insert_work(struct cpu_workqueue_struct *cwq,
Tejun Heo4690c4a2010-06-29 10:07:10 +02001156 struct work_struct *work, struct list_head *head,
1157 unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001158{
Tejun Heo63d95a92012-07-12 14:46:37 -07001159 struct worker_pool *pool = cwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01001160
Tejun Heo4690c4a2010-06-29 10:07:10 +02001161 /* we own @work, set data and link */
Tejun Heo7a22ad72010-06-29 10:07:13 +02001162 set_work_cwq(work, cwq, extra_flags);
Tejun Heo4690c4a2010-06-29 10:07:10 +02001163
Oleg Nesterov6e84d642007-05-09 02:34:46 -07001164 /*
1165 * Ensure that we get the right work->data if we see the
1166 * result of list_add() below, see try_to_grab_pending().
1167 */
1168 smp_wmb();
Tejun Heo4690c4a2010-06-29 10:07:10 +02001169
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -07001170 list_add_tail(&work->entry, head);
Tejun Heoe22bee72010-06-29 10:07:14 +02001171
1172 /*
1173 * Ensure either worker_sched_deactivated() sees the above
1174 * list_add_tail() or we see zero nr_running to avoid workers
1175 * lying around lazily while there are works to be processed.
1176 */
1177 smp_mb();
1178
Tejun Heo63d95a92012-07-12 14:46:37 -07001179 if (__need_more_worker(pool))
1180 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001181}
1182
Tejun Heoc8efcc22010-12-20 19:32:04 +01001183/*
1184 * Test whether @work is being queued from another work executing on the
1185 * same workqueue. This is rather expensive and should only be used from
1186 * cold paths.
1187 */
1188static bool is_chained_work(struct workqueue_struct *wq)
1189{
1190 unsigned long flags;
1191 unsigned int cpu;
1192
1193 for_each_gcwq_cpu(cpu) {
1194 struct global_cwq *gcwq = get_gcwq(cpu);
1195 struct worker *worker;
1196 struct hlist_node *pos;
1197 int i;
1198
1199 spin_lock_irqsave(&gcwq->lock, flags);
1200 for_each_busy_worker(worker, i, pos, gcwq) {
1201 if (worker->task != current)
1202 continue;
1203 spin_unlock_irqrestore(&gcwq->lock, flags);
1204 /*
1205 * I'm @worker, no locking necessary. See if @work
1206 * is headed to the same workqueue.
1207 */
1208 return worker->current_cwq->wq == wq;
1209 }
1210 spin_unlock_irqrestore(&gcwq->lock, flags);
1211 }
1212 return false;
1213}
1214
Tejun Heo4690c4a2010-06-29 10:07:10 +02001215static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 struct work_struct *work)
1217{
Tejun Heo502ca9d2010-06-29 10:07:13 +02001218 struct global_cwq *gcwq;
1219 struct cpu_workqueue_struct *cwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001220 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001221 unsigned int work_flags;
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001222 unsigned int req_cpu = cpu;
Tejun Heo8930cab2012-08-03 10:30:45 -07001223
1224 /*
1225 * While a work item is PENDING && off queue, a task trying to
1226 * steal the PENDING will busy-loop waiting for it to either get
1227 * queued or lose PENDING. Grabbing PENDING and queueing should
1228 * happen with IRQ disabled.
1229 */
1230 WARN_ON_ONCE(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001232 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001233
Tejun Heoc8efcc22010-12-20 19:32:04 +01001234 /* if dying, only works from the same workqueue are allowed */
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02001235 if (unlikely(wq->flags & WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001236 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001237 return;
1238
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001239 /* determine gcwq to use */
1240 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001241 struct global_cwq *last_gcwq;
1242
Tejun Heo57469822012-08-03 10:30:45 -07001243 if (cpu == WORK_CPU_UNBOUND)
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001244 cpu = raw_smp_processor_id();
1245
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001246 /*
Tejun Heodbf25762012-08-20 14:51:23 -07001247 * It's multi cpu. If @work was previously on a different
1248 * cpu, it might still be running there, in which case the
1249 * work needs to be queued on that cpu to guarantee
1250 * non-reentrancy.
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001251 */
Tejun Heo502ca9d2010-06-29 10:07:13 +02001252 gcwq = get_gcwq(cpu);
Tejun Heodbf25762012-08-20 14:51:23 -07001253 last_gcwq = get_work_gcwq(work);
1254
1255 if (last_gcwq && last_gcwq != gcwq) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001256 struct worker *worker;
1257
Tejun Heo8930cab2012-08-03 10:30:45 -07001258 spin_lock(&last_gcwq->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001259
1260 worker = find_worker_executing_work(last_gcwq, work);
1261
1262 if (worker && worker->current_cwq->wq == wq)
1263 gcwq = last_gcwq;
1264 else {
1265 /* meh... not running there, queue here */
Tejun Heo8930cab2012-08-03 10:30:45 -07001266 spin_unlock(&last_gcwq->lock);
1267 spin_lock(&gcwq->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001268 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001269 } else {
1270 spin_lock(&gcwq->lock);
1271 }
Tejun Heof3421792010-07-02 10:03:51 +02001272 } else {
1273 gcwq = get_gcwq(WORK_CPU_UNBOUND);
Tejun Heo8930cab2012-08-03 10:30:45 -07001274 spin_lock(&gcwq->lock);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001275 }
1276
1277 /* gcwq determined, get cwq and queue */
1278 cwq = get_cwq(gcwq->cpu, wq);
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001279 trace_workqueue_queue_work(req_cpu, cwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001280
Dan Carpenterf5b25522012-04-13 22:06:58 +03001281 if (WARN_ON(!list_empty(&work->entry))) {
Tejun Heo8930cab2012-08-03 10:30:45 -07001282 spin_unlock(&gcwq->lock);
Dan Carpenterf5b25522012-04-13 22:06:58 +03001283 return;
1284 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001285
Tejun Heo73f53c42010-06-29 10:07:11 +02001286 cwq->nr_in_flight[cwq->work_color]++;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001287 work_flags = work_color_to_flags(cwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001288
1289 if (likely(cwq->nr_active < cwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001290 trace_workqueue_activate_work(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001291 cwq->nr_active++;
Tejun Heo32704762012-07-13 22:16:45 -07001292 worklist = &cwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001293 } else {
1294 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001295 worklist = &cwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001296 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001297
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001298 insert_work(cwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001299
Tejun Heo8930cab2012-08-03 10:30:45 -07001300 spin_unlock(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301}
1302
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001303/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07001304 * queue_work_on - queue work on specific cpu
1305 * @cpu: CPU number to execute work on
1306 * @wq: workqueue to use
1307 * @work: work to queue
1308 *
Tejun Heod4283e92012-08-03 10:30:44 -07001309 * Returns %false if @work was already on a queue, %true otherwise.
Zhang Ruic1a220e2008-07-23 21:28:39 -07001310 *
1311 * We queue the work to a specific CPU, the caller must ensure it
1312 * can't go away.
1313 */
Tejun Heod4283e92012-08-03 10:30:44 -07001314bool queue_work_on(int cpu, struct workqueue_struct *wq,
1315 struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07001316{
Tejun Heod4283e92012-08-03 10:30:44 -07001317 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001318 unsigned long flags;
1319
1320 local_irq_save(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001321
Tejun Heo22df02b2010-06-29 10:07:10 +02001322 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001323 __queue_work(cpu, wq, work);
Tejun Heod4283e92012-08-03 10:30:44 -07001324 ret = true;
Zhang Ruic1a220e2008-07-23 21:28:39 -07001325 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001326
1327 local_irq_restore(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001328 return ret;
1329}
1330EXPORT_SYMBOL_GPL(queue_work_on);
1331
Tejun Heo0a13c002012-08-03 10:30:44 -07001332/**
1333 * queue_work - queue work on a workqueue
1334 * @wq: workqueue to use
1335 * @work: work to queue
1336 *
Tejun Heod4283e92012-08-03 10:30:44 -07001337 * Returns %false if @work was already on a queue, %true otherwise.
Tejun Heo0a13c002012-08-03 10:30:44 -07001338 *
1339 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1340 * it can be processed by another CPU.
1341 */
Tejun Heod4283e92012-08-03 10:30:44 -07001342bool queue_work(struct workqueue_struct *wq, struct work_struct *work)
Tejun Heo0a13c002012-08-03 10:30:44 -07001343{
Tejun Heo57469822012-08-03 10:30:45 -07001344 return queue_work_on(WORK_CPU_UNBOUND, wq, work);
Tejun Heo0a13c002012-08-03 10:30:44 -07001345}
1346EXPORT_SYMBOL_GPL(queue_work);
1347
Tejun Heod8e794d2012-08-03 10:30:45 -07001348void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
David Howells52bad642006-11-22 14:54:01 +00001350 struct delayed_work *dwork = (struct delayed_work *)__data;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001351 struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001353 /* should have been called from irqsafe timer with irq already off */
Tejun Heo12650572012-08-08 09:38:42 -07001354 __queue_work(dwork->cpu, cwq->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355}
Tejun Heod8e794d2012-08-03 10:30:45 -07001356EXPORT_SYMBOL_GPL(delayed_work_timer_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001358static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
1359 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001361 struct timer_list *timer = &dwork->timer;
1362 struct work_struct *work = &dwork->work;
1363 unsigned int lcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001365 WARN_ON_ONCE(timer->function != delayed_work_timer_fn ||
1366 timer->data != (unsigned long)dwork);
Tejun Heofc4b5142012-12-04 07:40:39 -08001367 WARN_ON_ONCE(timer_pending(timer));
1368 WARN_ON_ONCE(!list_empty(&work->entry));
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001369
Tejun Heo8852aac2012-12-01 16:23:42 -08001370 /*
1371 * If @delay is 0, queue @dwork->work immediately. This is for
1372 * both optimization and correctness. The earliest @timer can
1373 * expire is on the closest next tick and delayed_work users depend
1374 * on that there's no such delay when @delay is 0.
1375 */
1376 if (!delay) {
1377 __queue_work(cpu, wq, &dwork->work);
1378 return;
1379 }
1380
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001381 timer_stats_timer_set_start_info(&dwork->timer);
1382
1383 /*
1384 * This stores cwq for the moment, for the timer_fn. Note that the
1385 * work's gcwq is preserved to allow reentrance detection for
1386 * delayed works.
1387 */
1388 if (!(wq->flags & WQ_UNBOUND)) {
1389 struct global_cwq *gcwq = get_work_gcwq(work);
1390
Joonsoo Kime42986d2012-08-15 23:25:38 +09001391 /*
1392 * If we cannot get the last gcwq from @work directly,
1393 * select the last CPU such that it avoids unnecessarily
1394 * triggering non-reentrancy check in __queue_work().
1395 */
1396 lcpu = cpu;
1397 if (gcwq)
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001398 lcpu = gcwq->cpu;
Joonsoo Kime42986d2012-08-15 23:25:38 +09001399 if (lcpu == WORK_CPU_UNBOUND)
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001400 lcpu = raw_smp_processor_id();
1401 } else {
1402 lcpu = WORK_CPU_UNBOUND;
1403 }
1404
1405 set_work_cwq(work, get_cwq(lcpu, wq), 0);
1406
Tejun Heo12650572012-08-08 09:38:42 -07001407 dwork->cpu = cpu;
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001408 timer->expires = jiffies + delay;
1409
1410 if (unlikely(cpu != WORK_CPU_UNBOUND))
1411 add_timer_on(timer, cpu);
1412 else
1413 add_timer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414}
1415
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001416/**
1417 * queue_delayed_work_on - queue work on specific CPU after delay
1418 * @cpu: CPU number to execute work on
1419 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001420 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001421 * @delay: number of jiffies to wait before queueing
1422 *
Tejun Heo715f1302012-08-03 10:30:46 -07001423 * Returns %false if @work was already on a queue, %true otherwise. If
1424 * @delay is zero and @dwork is idle, it will be scheduled for immediate
1425 * execution.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001426 */
Tejun Heod4283e92012-08-03 10:30:44 -07001427bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
1428 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001429{
David Howells52bad642006-11-22 14:54:01 +00001430 struct work_struct *work = &dwork->work;
Tejun Heod4283e92012-08-03 10:30:44 -07001431 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001432 unsigned long flags;
1433
1434 /* read the comment in __queue_work() */
1435 local_irq_save(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001436
Tejun Heo22df02b2010-06-29 10:07:10 +02001437 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001438 __queue_delayed_work(cpu, wq, dwork, delay);
Tejun Heod4283e92012-08-03 10:30:44 -07001439 ret = true;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001440 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001441
1442 local_irq_restore(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001443 return ret;
1444}
Dave Jonesae90dd52006-06-30 01:40:45 -04001445EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Tejun Heoc8e55f32010-06-29 10:07:12 +02001447/**
Tejun Heo0a13c002012-08-03 10:30:44 -07001448 * queue_delayed_work - queue work on a workqueue after delay
1449 * @wq: workqueue to use
1450 * @dwork: delayable work to queue
1451 * @delay: number of jiffies to wait before queueing
1452 *
Tejun Heo715f1302012-08-03 10:30:46 -07001453 * Equivalent to queue_delayed_work_on() but tries to use the local CPU.
Tejun Heo0a13c002012-08-03 10:30:44 -07001454 */
Tejun Heod4283e92012-08-03 10:30:44 -07001455bool queue_delayed_work(struct workqueue_struct *wq,
Tejun Heo0a13c002012-08-03 10:30:44 -07001456 struct delayed_work *dwork, unsigned long delay)
1457{
Tejun Heo57469822012-08-03 10:30:45 -07001458 return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
Tejun Heo0a13c002012-08-03 10:30:44 -07001459}
1460EXPORT_SYMBOL_GPL(queue_delayed_work);
1461
1462/**
Tejun Heo8376fe22012-08-03 10:30:47 -07001463 * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
1464 * @cpu: CPU number to execute work on
1465 * @wq: workqueue to use
1466 * @dwork: work to queue
1467 * @delay: number of jiffies to wait before queueing
1468 *
1469 * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
1470 * modify @dwork's timer so that it expires after @delay. If @delay is
1471 * zero, @work is guaranteed to be scheduled immediately regardless of its
1472 * current state.
1473 *
1474 * Returns %false if @dwork was idle and queued, %true if @dwork was
1475 * pending and its timer was modified.
1476 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001477 * This function is safe to call from any context including IRQ handler.
Tejun Heo8376fe22012-08-03 10:30:47 -07001478 * See try_to_grab_pending() for details.
1479 */
1480bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
1481 struct delayed_work *dwork, unsigned long delay)
1482{
1483 unsigned long flags;
1484 int ret;
1485
1486 do {
1487 ret = try_to_grab_pending(&dwork->work, true, &flags);
1488 } while (unlikely(ret == -EAGAIN));
1489
1490 if (likely(ret >= 0)) {
1491 __queue_delayed_work(cpu, wq, dwork, delay);
1492 local_irq_restore(flags);
1493 }
1494
1495 /* -ENOENT from try_to_grab_pending() becomes %true */
1496 return ret;
1497}
1498EXPORT_SYMBOL_GPL(mod_delayed_work_on);
1499
1500/**
1501 * mod_delayed_work - modify delay of or queue a delayed work
1502 * @wq: workqueue to use
1503 * @dwork: work to queue
1504 * @delay: number of jiffies to wait before queueing
1505 *
1506 * mod_delayed_work_on() on local CPU.
1507 */
1508bool mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dwork,
1509 unsigned long delay)
1510{
1511 return mod_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
1512}
1513EXPORT_SYMBOL_GPL(mod_delayed_work);
1514
1515/**
Tejun Heoc8e55f32010-06-29 10:07:12 +02001516 * worker_enter_idle - enter idle state
1517 * @worker: worker which is entering idle state
1518 *
1519 * @worker is entering idle state. Update stats and idle timer if
1520 * necessary.
1521 *
1522 * LOCKING:
1523 * spin_lock_irq(gcwq->lock).
1524 */
1525static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001527 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Tejun Heoc8e55f32010-06-29 10:07:12 +02001529 BUG_ON(worker->flags & WORKER_IDLE);
1530 BUG_ON(!list_empty(&worker->entry) &&
1531 (worker->hentry.next || worker->hentry.pprev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Tejun Heocb444762010-07-02 10:03:50 +02001533 /* can't use worker_set_flags(), also called from start_worker() */
1534 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001535 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001536 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001537
Tejun Heoc8e55f32010-06-29 10:07:12 +02001538 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001539 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001540
Tejun Heo628c78e2012-07-17 12:39:27 -07001541 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1542 mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
Tejun Heocb444762010-07-02 10:03:50 +02001543
Tejun Heo544ecf32012-05-14 15:04:50 -07001544 /*
Tejun Heo628c78e2012-07-17 12:39:27 -07001545 * Sanity check nr_running. Because gcwq_unbind_fn() releases
1546 * gcwq->lock between setting %WORKER_UNBOUND and zapping
1547 * nr_running, the warning may trigger spuriously. Check iff
1548 * unbind is not in progress.
Tejun Heo544ecf32012-05-14 15:04:50 -07001549 */
Tejun Heo24647572013-01-24 11:01:33 -08001550 WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001551 pool->nr_workers == pool->nr_idle &&
Tejun Heo63d95a92012-07-12 14:46:37 -07001552 atomic_read(get_pool_nr_running(pool)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553}
1554
Tejun Heoc8e55f32010-06-29 10:07:12 +02001555/**
1556 * worker_leave_idle - leave idle state
1557 * @worker: worker which is leaving idle state
1558 *
1559 * @worker is leaving idle state. Update stats.
1560 *
1561 * LOCKING:
1562 * spin_lock_irq(gcwq->lock).
1563 */
1564static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001566 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Tejun Heoc8e55f32010-06-29 10:07:12 +02001568 BUG_ON(!(worker->flags & WORKER_IDLE));
Tejun Heod302f012010-06-29 10:07:13 +02001569 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001570 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001571 list_del_init(&worker->entry);
1572}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Tejun Heoe22bee72010-06-29 10:07:14 +02001574/**
1575 * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq
1576 * @worker: self
1577 *
1578 * Works which are scheduled while the cpu is online must at least be
1579 * scheduled to a worker which is bound to the cpu so that if they are
1580 * flushed from cpu callbacks while cpu is going down, they are
1581 * guaranteed to execute on the cpu.
1582 *
1583 * This function is to be used by rogue workers and rescuers to bind
1584 * themselves to the target cpu and may race with cpu going down or
1585 * coming online. kthread_bind() can't be used because it may put the
1586 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1587 * verbatim as it's best effort and blocking and gcwq may be
1588 * [dis]associated in the meantime.
1589 *
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001590 * This function tries set_cpus_allowed() and locks gcwq and verifies the
Tejun Heo24647572013-01-24 11:01:33 -08001591 * binding against %POOL_DISASSOCIATED which is set during
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001592 * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1593 * enters idle state or fetches works without dropping lock, it can
1594 * guarantee the scheduling requirement described in the first paragraph.
Tejun Heoe22bee72010-06-29 10:07:14 +02001595 *
1596 * CONTEXT:
1597 * Might sleep. Called without any lock but returns with gcwq->lock
1598 * held.
1599 *
1600 * RETURNS:
1601 * %true if the associated gcwq is online (@worker is successfully
1602 * bound), %false if offline.
1603 */
1604static bool worker_maybe_bind_and_lock(struct worker *worker)
Namhyung Kim972fa1c2010-08-22 23:19:43 +09001605__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001606{
Tejun Heo24647572013-01-24 11:01:33 -08001607 struct worker_pool *pool = worker->pool;
1608 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001609 struct task_struct *task = worker->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Tejun Heoe22bee72010-06-29 10:07:14 +02001611 while (true) {
1612 /*
1613 * The following call may fail, succeed or succeed
1614 * without actually migrating the task to the cpu if
1615 * it races with cpu hotunplug operation. Verify
Tejun Heo24647572013-01-24 11:01:33 -08001616 * against POOL_DISASSOCIATED.
Tejun Heoe22bee72010-06-29 10:07:14 +02001617 */
Tejun Heo24647572013-01-24 11:01:33 -08001618 if (!(pool->flags & POOL_DISASSOCIATED))
Tejun Heof3421792010-07-02 10:03:51 +02001619 set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu));
Oleg Nesterov85f41862007-05-09 02:34:20 -07001620
Tejun Heoe22bee72010-06-29 10:07:14 +02001621 spin_lock_irq(&gcwq->lock);
Tejun Heo24647572013-01-24 11:01:33 -08001622 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heoe22bee72010-06-29 10:07:14 +02001623 return false;
1624 if (task_cpu(task) == gcwq->cpu &&
1625 cpumask_equal(&current->cpus_allowed,
1626 get_cpu_mask(gcwq->cpu)))
1627 return true;
1628 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001629
Tejun Heo5035b202011-04-29 18:08:37 +02001630 /*
1631 * We've raced with CPU hot[un]plug. Give it a breather
1632 * and retry migration. cond_resched() is required here;
1633 * otherwise, we might deadlock against cpu_stop trying to
1634 * bring down the CPU on non-preemptive kernel.
1635 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001636 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001637 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001638 }
1639}
1640
1641/*
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001642 * Rebind an idle @worker to its CPU. worker_thread() will test
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001643 * list_empty(@worker->entry) before leaving idle and call this function.
Tejun Heo25511a42012-07-17 12:39:27 -07001644 */
1645static void idle_worker_rebind(struct worker *worker)
1646{
1647 struct global_cwq *gcwq = worker->pool->gcwq;
1648
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001649 /* CPU may go down again inbetween, clear UNBOUND only on success */
1650 if (worker_maybe_bind_and_lock(worker))
1651 worker_clr_flags(worker, WORKER_UNBOUND);
Tejun Heo25511a42012-07-17 12:39:27 -07001652
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001653 /* rebind complete, become available again */
1654 list_add(&worker->entry, &worker->pool->idle_list);
1655 spin_unlock_irq(&gcwq->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07001656}
1657
1658/*
1659 * Function for @worker->rebind.work used to rebind unbound busy workers to
Tejun Heo403c8212012-07-17 12:39:27 -07001660 * the associated cpu which is coming back online. This is scheduled by
1661 * cpu up but can race with other cpu hotplug operations and may be
1662 * executed twice without intervening cpu down.
Tejun Heoe22bee72010-06-29 10:07:14 +02001663 */
Tejun Heo25511a42012-07-17 12:39:27 -07001664static void busy_worker_rebind_fn(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001665{
1666 struct worker *worker = container_of(work, struct worker, rebind_work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001667 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001668
Lai Jiangshaneab6d822012-09-18 09:59:22 -07001669 if (worker_maybe_bind_and_lock(worker))
1670 worker_clr_flags(worker, WORKER_UNBOUND);
Tejun Heoe22bee72010-06-29 10:07:14 +02001671
1672 spin_unlock_irq(&gcwq->lock);
1673}
1674
Tejun Heo25511a42012-07-17 12:39:27 -07001675/**
1676 * rebind_workers - rebind all workers of a gcwq to the associated CPU
1677 * @gcwq: gcwq of interest
1678 *
1679 * @gcwq->cpu is coming online. Rebind all workers to the CPU. Rebinding
1680 * is different for idle and busy ones.
1681 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001682 * Idle ones will be removed from the idle_list and woken up. They will
1683 * add themselves back after completing rebind. This ensures that the
1684 * idle_list doesn't contain any unbound workers when re-bound busy workers
1685 * try to perform local wake-ups for concurrency management.
Tejun Heo25511a42012-07-17 12:39:27 -07001686 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001687 * Busy workers can rebind after they finish their current work items.
1688 * Queueing the rebind work item at the head of the scheduled list is
1689 * enough. Note that nr_running will be properly bumped as busy workers
1690 * rebind.
Tejun Heo25511a42012-07-17 12:39:27 -07001691 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001692 * On return, all non-manager workers are scheduled for rebind - see
1693 * manage_workers() for the manager special case. Any idle worker
1694 * including the manager will not appear on @idle_list until rebind is
1695 * complete, making local wake-ups safe.
Tejun Heo25511a42012-07-17 12:39:27 -07001696 */
1697static void rebind_workers(struct global_cwq *gcwq)
Tejun Heo25511a42012-07-17 12:39:27 -07001698{
Tejun Heo25511a42012-07-17 12:39:27 -07001699 struct worker_pool *pool;
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001700 struct worker *worker, *n;
Tejun Heo25511a42012-07-17 12:39:27 -07001701 struct hlist_node *pos;
1702 int i;
1703
1704 lockdep_assert_held(&gcwq->lock);
1705
1706 for_each_worker_pool(pool, gcwq)
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07001707 lockdep_assert_held(&pool->assoc_mutex);
Tejun Heo25511a42012-07-17 12:39:27 -07001708
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001709 /* dequeue and kick idle ones */
Tejun Heo25511a42012-07-17 12:39:27 -07001710 for_each_worker_pool(pool, gcwq) {
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001711 list_for_each_entry_safe(worker, n, &pool->idle_list, entry) {
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001712 /*
1713 * idle workers should be off @pool->idle_list
1714 * until rebind is complete to avoid receiving
1715 * premature local wake-ups.
1716 */
1717 list_del_init(&worker->entry);
Lai Jiangshan96e65302012-09-02 00:28:19 +08001718
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001719 /*
1720 * worker_thread() will see the above dequeuing
1721 * and call idle_worker_rebind().
1722 */
Tejun Heo25511a42012-07-17 12:39:27 -07001723 wake_up_process(worker->task);
1724 }
1725 }
1726
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001727 /* rebind busy workers */
Tejun Heo25511a42012-07-17 12:39:27 -07001728 for_each_busy_worker(worker, i, pos, gcwq) {
1729 struct work_struct *rebind_work = &worker->rebind_work;
Joonsoo Kime2b6a6d2012-08-15 23:25:40 +09001730 struct workqueue_struct *wq;
Tejun Heo25511a42012-07-17 12:39:27 -07001731
1732 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
1733 work_data_bits(rebind_work)))
1734 continue;
1735
Tejun Heo25511a42012-07-17 12:39:27 -07001736 debug_work_activate(rebind_work);
Tejun Heo90beca52012-09-04 23:12:33 -07001737
Joonsoo Kime2b6a6d2012-08-15 23:25:40 +09001738 /*
1739 * wq doesn't really matter but let's keep @worker->pool
1740 * and @cwq->pool consistent for sanity.
1741 */
Tejun Heoe34cdddb2013-01-24 11:01:33 -08001742 if (std_worker_pool_pri(worker->pool))
Joonsoo Kime2b6a6d2012-08-15 23:25:40 +09001743 wq = system_highpri_wq;
1744 else
1745 wq = system_wq;
Tejun Heoec588152012-09-04 23:16:32 -07001746
Joonsoo Kime2b6a6d2012-08-15 23:25:40 +09001747 insert_work(get_cwq(gcwq->cpu, wq), rebind_work,
1748 worker->scheduled.next,
1749 work_color_to_flags(WORK_NO_COLOR));
Tejun Heoec588152012-09-04 23:16:32 -07001750 }
Tejun Heo25511a42012-07-17 12:39:27 -07001751}
1752
Tejun Heoc34056a2010-06-29 10:07:11 +02001753static struct worker *alloc_worker(void)
1754{
1755 struct worker *worker;
1756
1757 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001758 if (worker) {
1759 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001760 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heo25511a42012-07-17 12:39:27 -07001761 INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn);
Tejun Heoe22bee72010-06-29 10:07:14 +02001762 /* on creation a worker is in !idle && prep state */
1763 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001764 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001765 return worker;
1766}
1767
1768/**
1769 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001770 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001771 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001772 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001773 * can be started by calling start_worker() or destroyed using
1774 * destroy_worker().
1775 *
1776 * CONTEXT:
1777 * Might sleep. Does GFP_KERNEL allocations.
1778 *
1779 * RETURNS:
1780 * Pointer to the newly created worker.
1781 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001782static struct worker *create_worker(struct worker_pool *pool)
Tejun Heoc34056a2010-06-29 10:07:11 +02001783{
Tejun Heo63d95a92012-07-12 14:46:37 -07001784 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe34cdddb2013-01-24 11:01:33 -08001785 const char *pri = std_worker_pool_pri(pool) ? "H" : "";
Tejun Heoc34056a2010-06-29 10:07:11 +02001786 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001787 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001788
Tejun Heo8b03ae32010-06-29 10:07:12 +02001789 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001790 while (ida_get_new(&pool->worker_ida, &id)) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001791 spin_unlock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001792 if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001793 goto fail;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001794 spin_lock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001795 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02001796 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001797
1798 worker = alloc_worker();
1799 if (!worker)
1800 goto fail;
1801
Tejun Heobd7bdd42012-07-12 14:46:37 -07001802 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001803 worker->id = id;
1804
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001805 if (gcwq->cpu != WORK_CPU_UNBOUND)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001806 worker->task = kthread_create_on_node(worker_thread,
Tejun Heo32704762012-07-13 22:16:45 -07001807 worker, cpu_to_node(gcwq->cpu),
1808 "kworker/%u:%d%s", gcwq->cpu, id, pri);
Tejun Heof3421792010-07-02 10:03:51 +02001809 else
1810 worker->task = kthread_create(worker_thread, worker,
Tejun Heo32704762012-07-13 22:16:45 -07001811 "kworker/u:%d%s", id, pri);
Tejun Heoc34056a2010-06-29 10:07:11 +02001812 if (IS_ERR(worker->task))
1813 goto fail;
1814
Tejun Heoe34cdddb2013-01-24 11:01:33 -08001815 if (std_worker_pool_pri(pool))
Tejun Heo32704762012-07-13 22:16:45 -07001816 set_user_nice(worker->task, HIGHPRI_NICE_LEVEL);
1817
Tejun Heodb7bccf2010-06-29 10:07:12 +02001818 /*
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001819 * Determine CPU binding of the new worker depending on
Tejun Heo24647572013-01-24 11:01:33 -08001820 * %POOL_DISASSOCIATED. The caller is responsible for ensuring the
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001821 * flag remains stable across this function. See the comments
1822 * above the flag definition for details.
1823 *
1824 * As an unbound worker may later become a regular one if CPU comes
1825 * online, make sure every worker has %PF_THREAD_BOUND set.
Tejun Heodb7bccf2010-06-29 10:07:12 +02001826 */
Tejun Heo24647572013-01-24 11:01:33 -08001827 if (!(pool->flags & POOL_DISASSOCIATED)) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001828 kthread_bind(worker->task, gcwq->cpu);
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001829 } else {
Tejun Heodb7bccf2010-06-29 10:07:12 +02001830 worker->task->flags |= PF_THREAD_BOUND;
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001831 worker->flags |= WORKER_UNBOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001833
Tejun Heoc34056a2010-06-29 10:07:11 +02001834 return worker;
1835fail:
1836 if (id >= 0) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001837 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001838 ida_remove(&pool->worker_ida, id);
Tejun Heo8b03ae32010-06-29 10:07:12 +02001839 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001840 }
1841 kfree(worker);
1842 return NULL;
1843}
1844
1845/**
1846 * start_worker - start a newly created worker
1847 * @worker: worker to start
1848 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001849 * Make the gcwq aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001850 *
1851 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001852 * spin_lock_irq(gcwq->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001853 */
1854static void start_worker(struct worker *worker)
1855{
Tejun Heocb444762010-07-02 10:03:50 +02001856 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001857 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001858 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001859 wake_up_process(worker->task);
1860}
1861
1862/**
1863 * destroy_worker - destroy a workqueue worker
1864 * @worker: worker to be destroyed
1865 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001866 * Destroy @worker and adjust @gcwq stats accordingly.
1867 *
1868 * CONTEXT:
1869 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001870 */
1871static void destroy_worker(struct worker *worker)
1872{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001873 struct worker_pool *pool = worker->pool;
1874 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001875 int id = worker->id;
1876
1877 /* sanity check frenzy */
1878 BUG_ON(worker->current_work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001879 BUG_ON(!list_empty(&worker->scheduled));
Tejun Heoc34056a2010-06-29 10:07:11 +02001880
Tejun Heoc8e55f32010-06-29 10:07:12 +02001881 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001882 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001883 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001884 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001885
1886 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001887 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001888
1889 spin_unlock_irq(&gcwq->lock);
1890
Tejun Heoc34056a2010-06-29 10:07:11 +02001891 kthread_stop(worker->task);
1892 kfree(worker);
1893
Tejun Heo8b03ae32010-06-29 10:07:12 +02001894 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001895 ida_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001896}
1897
Tejun Heo63d95a92012-07-12 14:46:37 -07001898static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001899{
Tejun Heo63d95a92012-07-12 14:46:37 -07001900 struct worker_pool *pool = (void *)__pool;
1901 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001902
1903 spin_lock_irq(&gcwq->lock);
1904
Tejun Heo63d95a92012-07-12 14:46:37 -07001905 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001906 struct worker *worker;
1907 unsigned long expires;
1908
1909 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001910 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001911 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1912
1913 if (time_before(jiffies, expires))
Tejun Heo63d95a92012-07-12 14:46:37 -07001914 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001915 else {
1916 /* it's been idle for too long, wake up manager */
Tejun Heo11ebea52012-07-12 14:46:37 -07001917 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -07001918 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001919 }
1920 }
1921
1922 spin_unlock_irq(&gcwq->lock);
1923}
1924
1925static bool send_mayday(struct work_struct *work)
1926{
1927 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1928 struct workqueue_struct *wq = cwq->wq;
Tejun Heof3421792010-07-02 10:03:51 +02001929 unsigned int cpu;
Tejun Heoe22bee72010-06-29 10:07:14 +02001930
1931 if (!(wq->flags & WQ_RESCUER))
1932 return false;
1933
1934 /* mayday mayday mayday */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001935 cpu = cwq->pool->gcwq->cpu;
Tejun Heof3421792010-07-02 10:03:51 +02001936 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1937 if (cpu == WORK_CPU_UNBOUND)
1938 cpu = 0;
Tejun Heof2e005a2010-07-20 15:59:09 +02001939 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001940 wake_up_process(wq->rescuer->task);
1941 return true;
1942}
1943
Tejun Heo63d95a92012-07-12 14:46:37 -07001944static void gcwq_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001945{
Tejun Heo63d95a92012-07-12 14:46:37 -07001946 struct worker_pool *pool = (void *)__pool;
1947 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001948 struct work_struct *work;
1949
1950 spin_lock_irq(&gcwq->lock);
1951
Tejun Heo63d95a92012-07-12 14:46:37 -07001952 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001953 /*
1954 * We've been trying to create a new worker but
1955 * haven't been successful. We might be hitting an
1956 * allocation deadlock. Send distress signals to
1957 * rescuers.
1958 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001959 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001960 send_mayday(work);
1961 }
1962
1963 spin_unlock_irq(&gcwq->lock);
1964
Tejun Heo63d95a92012-07-12 14:46:37 -07001965 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001966}
1967
1968/**
1969 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001970 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001971 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001972 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001973 * have at least one idle worker on return from this function. If
1974 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001975 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001976 * possible allocation deadlock.
1977 *
1978 * On return, need_to_create_worker() is guaranteed to be false and
1979 * may_start_working() true.
1980 *
1981 * LOCKING:
1982 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1983 * multiple times. Does GFP_KERNEL allocations. Called only from
1984 * manager.
1985 *
1986 * RETURNS:
1987 * false if no action was taken and gcwq->lock stayed locked, true
1988 * otherwise.
1989 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001990static bool maybe_create_worker(struct worker_pool *pool)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001991__releases(&gcwq->lock)
1992__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001993{
Tejun Heo63d95a92012-07-12 14:46:37 -07001994 struct global_cwq *gcwq = pool->gcwq;
1995
1996 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001997 return false;
1998restart:
Tejun Heo9f9c2362010-07-14 11:31:20 +02001999 spin_unlock_irq(&gcwq->lock);
2000
Tejun Heoe22bee72010-06-29 10:07:14 +02002001 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07002002 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02002003
2004 while (true) {
2005 struct worker *worker;
2006
Tejun Heobc2ae0f2012-07-17 12:39:27 -07002007 worker = create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002008 if (worker) {
Tejun Heo63d95a92012-07-12 14:46:37 -07002009 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02002010 spin_lock_irq(&gcwq->lock);
2011 start_worker(worker);
Tejun Heo63d95a92012-07-12 14:46:37 -07002012 BUG_ON(need_to_create_worker(pool));
Tejun Heoe22bee72010-06-29 10:07:14 +02002013 return true;
2014 }
2015
Tejun Heo63d95a92012-07-12 14:46:37 -07002016 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002017 break;
2018
Tejun Heoe22bee72010-06-29 10:07:14 +02002019 __set_current_state(TASK_INTERRUPTIBLE);
2020 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02002021
Tejun Heo63d95a92012-07-12 14:46:37 -07002022 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002023 break;
2024 }
2025
Tejun Heo63d95a92012-07-12 14:46:37 -07002026 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02002027 spin_lock_irq(&gcwq->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07002028 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002029 goto restart;
2030 return true;
2031}
2032
2033/**
2034 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo63d95a92012-07-12 14:46:37 -07002035 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02002036 *
Tejun Heo63d95a92012-07-12 14:46:37 -07002037 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02002038 * IDLE_WORKER_TIMEOUT.
2039 *
2040 * LOCKING:
2041 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
2042 * multiple times. Called only from manager.
2043 *
2044 * RETURNS:
2045 * false if no action was taken and gcwq->lock stayed locked, true
2046 * otherwise.
2047 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002048static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02002049{
2050 bool ret = false;
2051
Tejun Heo63d95a92012-07-12 14:46:37 -07002052 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02002053 struct worker *worker;
2054 unsigned long expires;
2055
Tejun Heo63d95a92012-07-12 14:46:37 -07002056 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02002057 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
2058
2059 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07002060 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02002061 break;
2062 }
2063
2064 destroy_worker(worker);
2065 ret = true;
2066 }
2067
2068 return ret;
2069}
2070
2071/**
2072 * manage_workers - manage worker pool
2073 * @worker: self
2074 *
2075 * Assume the manager role and manage gcwq worker pool @worker belongs
2076 * to. At any given time, there can be only zero or one manager per
2077 * gcwq. The exclusion is handled automatically by this function.
2078 *
2079 * The caller can safely start processing works on false return. On
2080 * true return, it's guaranteed that need_to_create_worker() is false
2081 * and may_start_working() is true.
2082 *
2083 * CONTEXT:
2084 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
2085 * multiple times. Does GFP_KERNEL allocations.
2086 *
2087 * RETURNS:
2088 * false if no action was taken and gcwq->lock stayed locked, true if
2089 * some action was taken.
2090 */
2091static bool manage_workers(struct worker *worker)
2092{
Tejun Heo63d95a92012-07-12 14:46:37 -07002093 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002094 bool ret = false;
2095
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002096 if (pool->flags & POOL_MANAGING_WORKERS)
Tejun Heoe22bee72010-06-29 10:07:14 +02002097 return ret;
2098
Lai Jiangshan552a37e2012-09-10 10:03:33 -07002099 pool->flags |= POOL_MANAGING_WORKERS;
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002100
2101 /*
2102 * To simplify both worker management and CPU hotplug, hold off
2103 * management while hotplug is in progress. CPU hotplug path can't
2104 * grab %POOL_MANAGING_WORKERS to achieve this because that can
2105 * lead to idle worker depletion (all become busy thinking someone
2106 * else is managing) which in turn can result in deadlock under
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002107 * extreme circumstances. Use @pool->assoc_mutex to synchronize
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002108 * manager against CPU hotplug.
2109 *
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002110 * assoc_mutex would always be free unless CPU hotplug is in
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002111 * progress. trylock first without dropping @gcwq->lock.
2112 */
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002113 if (unlikely(!mutex_trylock(&pool->assoc_mutex))) {
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002114 spin_unlock_irq(&pool->gcwq->lock);
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002115 mutex_lock(&pool->assoc_mutex);
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002116 /*
2117 * CPU hotplug could have happened while we were waiting
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002118 * for assoc_mutex. Hotplug itself can't handle us
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002119 * because manager isn't either on idle or busy list, and
2120 * @gcwq's state and ours could have deviated.
2121 *
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002122 * As hotplug is now excluded via assoc_mutex, we can
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002123 * simply try to bind. It will succeed or fail depending
2124 * on @gcwq's current state. Try it and adjust
2125 * %WORKER_UNBOUND accordingly.
2126 */
2127 if (worker_maybe_bind_and_lock(worker))
2128 worker->flags &= ~WORKER_UNBOUND;
2129 else
2130 worker->flags |= WORKER_UNBOUND;
2131
2132 ret = true;
2133 }
2134
Tejun Heo11ebea52012-07-12 14:46:37 -07002135 pool->flags &= ~POOL_MANAGE_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02002136
2137 /*
2138 * Destroy and then create so that may_start_working() is true
2139 * on return.
2140 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002141 ret |= maybe_destroy_workers(pool);
2142 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002143
Lai Jiangshan552a37e2012-09-10 10:03:33 -07002144 pool->flags &= ~POOL_MANAGING_WORKERS;
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002145 mutex_unlock(&pool->assoc_mutex);
Tejun Heoe22bee72010-06-29 10:07:14 +02002146 return ret;
2147}
2148
Tejun Heoa62428c2010-06-29 10:07:10 +02002149/**
2150 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02002151 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02002152 * @work: work to process
2153 *
2154 * Process @work. This function contains all the logics necessary to
2155 * process a single work including synchronization against and
2156 * interaction with other workers on the same cpu, queueing and
2157 * flushing. As long as context requirement is met, any worker can
2158 * call this function to process a work.
2159 *
2160 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002161 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02002162 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002163static void process_one_work(struct worker *worker, struct work_struct *work)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09002164__releases(&gcwq->lock)
2165__acquires(&gcwq->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02002166{
Tejun Heo7e116292010-06-29 10:07:13 +02002167 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002168 struct worker_pool *pool = worker->pool;
2169 struct global_cwq *gcwq = pool->gcwq;
Tejun Heofb0e7be2010-06-29 10:07:15 +02002170 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02002171 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02002172 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02002173#ifdef CONFIG_LOCKDEP
2174 /*
2175 * It is permissible to free the struct work_struct from
2176 * inside the function that is called from it, this we need to
2177 * take into account for lockdep too. To avoid bogus "held
2178 * lock freed" warnings as well as problems when looking into
2179 * work->lockdep_map, make a copy and use that here.
2180 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07002181 struct lockdep_map lockdep_map;
2182
2183 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002184#endif
Tejun Heo6fec10a2012-07-22 10:16:34 -07002185 /*
2186 * Ensure we're on the correct CPU. DISASSOCIATED test is
2187 * necessary to avoid spurious warnings from rescuers servicing the
Tejun Heo24647572013-01-24 11:01:33 -08002188 * unbound or a disassociated pool.
Tejun Heo6fec10a2012-07-22 10:16:34 -07002189 */
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002190 WARN_ON_ONCE(!(worker->flags & WORKER_UNBOUND) &&
Tejun Heo24647572013-01-24 11:01:33 -08002191 !(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heo25511a42012-07-17 12:39:27 -07002192 raw_smp_processor_id() != gcwq->cpu);
2193
Tejun Heo7e116292010-06-29 10:07:13 +02002194 /*
2195 * A single work shouldn't be executed concurrently by
2196 * multiple workers on a single cpu. Check whether anyone is
2197 * already processing the work. If so, defer the work to the
2198 * currently executing one.
2199 */
Sasha Levin42f85702012-12-17 10:01:23 -05002200 collision = find_worker_executing_work(gcwq, work);
Tejun Heo7e116292010-06-29 10:07:13 +02002201 if (unlikely(collision)) {
2202 move_linked_works(work, &collision->scheduled, NULL);
2203 return;
2204 }
2205
Tejun Heo8930cab2012-08-03 10:30:45 -07002206 /* claim and dequeue */
Tejun Heoa62428c2010-06-29 10:07:10 +02002207 debug_work_deactivate(work);
Tejun Heo023f27d2012-12-19 11:24:06 -08002208 hash_add(gcwq->busy_hash, &worker->hentry, (unsigned long)work);
Tejun Heoc34056a2010-06-29 10:07:11 +02002209 worker->current_work = work;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002210 worker->current_func = work->func;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02002211 worker->current_cwq = cwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002212 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002213
Tejun Heoa62428c2010-06-29 10:07:10 +02002214 list_del_init(&work->entry);
2215
Tejun Heo649027d2010-06-29 10:07:14 +02002216 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02002217 * CPU intensive works don't participate in concurrency
2218 * management. They're the scheduler's responsibility.
2219 */
2220 if (unlikely(cpu_intensive))
2221 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
2222
Tejun Heo974271c2012-07-12 14:46:37 -07002223 /*
2224 * Unbound gcwq isn't concurrency managed and work items should be
2225 * executed ASAP. Wake up another worker if necessary.
2226 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002227 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
2228 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07002229
Tejun Heo8930cab2012-08-03 10:30:45 -07002230 /*
Tejun Heo7c3eed52013-01-24 11:01:33 -08002231 * Record the last pool and clear PENDING which should be the last
Tejun Heo23657bb2012-08-13 17:08:19 -07002232 * update to @work. Also, do this inside @gcwq->lock so that
2233 * PENDING and queued state changes happen together while IRQ is
2234 * disabled.
Tejun Heo8930cab2012-08-03 10:30:45 -07002235 */
Tejun Heo7c3eed52013-01-24 11:01:33 -08002236 set_work_pool_and_clear_pending(work, pool->id);
Tejun Heoa62428c2010-06-29 10:07:10 +02002237
Tejun Heoa62428c2010-06-29 10:07:10 +02002238 spin_unlock_irq(&gcwq->lock);
2239
Tejun Heoe1594892011-01-09 23:32:15 +01002240 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002241 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002242 trace_workqueue_execute_start(work);
Tejun Heoa2c1c572012-12-18 10:35:02 -08002243 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002244 /*
2245 * While we must be careful to not use "work" after this, the trace
2246 * point will only record its address.
2247 */
2248 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002249 lock_map_release(&lockdep_map);
2250 lock_map_release(&cwq->wq->lockdep_map);
2251
2252 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Valentin Ilie044c7822012-08-19 00:52:42 +03002253 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
2254 " last function: %pf\n",
Tejun Heoa2c1c572012-12-18 10:35:02 -08002255 current->comm, preempt_count(), task_pid_nr(current),
2256 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02002257 debug_show_held_locks(current);
2258 dump_stack();
2259 }
2260
Tejun Heo8b03ae32010-06-29 10:07:12 +02002261 spin_lock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002262
Tejun Heofb0e7be2010-06-29 10:07:15 +02002263 /* clear cpu intensive status */
2264 if (unlikely(cpu_intensive))
2265 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2266
Tejun Heoa62428c2010-06-29 10:07:10 +02002267 /* we're done with it, release */
Sasha Levin42f85702012-12-17 10:01:23 -05002268 hash_del(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002269 worker->current_work = NULL;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002270 worker->current_func = NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02002271 worker->current_cwq = NULL;
Lai Jiangshanb3f9f402012-09-18 10:40:00 -07002272 cwq_dec_nr_in_flight(cwq, work_color);
Tejun Heoa62428c2010-06-29 10:07:10 +02002273}
2274
Tejun Heoaffee4b2010-06-29 10:07:12 +02002275/**
2276 * process_scheduled_works - process scheduled works
2277 * @worker: self
2278 *
2279 * Process all scheduled works. Please note that the scheduled list
2280 * may change while processing a work, so this function repeatedly
2281 * fetches a work from the top and executes it.
2282 *
2283 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002284 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02002285 * multiple times.
2286 */
2287static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002289 while (!list_empty(&worker->scheduled)) {
2290 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002292 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294}
2295
Tejun Heo4690c4a2010-06-29 10:07:10 +02002296/**
2297 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02002298 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02002299 *
Tejun Heoe22bee72010-06-29 10:07:14 +02002300 * The gcwq worker thread function. There's a single dynamic pool of
2301 * these per each cpu. These workers process all works regardless of
2302 * their specific target workqueue. The only exception is works which
2303 * belong to workqueues with a rescuer which will be explained in
2304 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02002305 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002306static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307{
Tejun Heoc34056a2010-06-29 10:07:11 +02002308 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07002309 struct worker_pool *pool = worker->pool;
2310 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Tejun Heoe22bee72010-06-29 10:07:14 +02002312 /* tell the scheduler that this is a workqueue worker */
2313 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002314woke_up:
Tejun Heoc8e55f32010-06-29 10:07:12 +02002315 spin_lock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002317 /* we are off idle list if destruction or rebind is requested */
2318 if (unlikely(list_empty(&worker->entry))) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02002319 spin_unlock_irq(&gcwq->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07002320
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002321 /* if DIE is set, destruction is requested */
Tejun Heo25511a42012-07-17 12:39:27 -07002322 if (worker->flags & WORKER_DIE) {
2323 worker->task->flags &= ~PF_WQ_WORKER;
2324 return 0;
2325 }
2326
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002327 /* otherwise, rebind */
Tejun Heo25511a42012-07-17 12:39:27 -07002328 idle_worker_rebind(worker);
2329 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 }
2331
Tejun Heoc8e55f32010-06-29 10:07:12 +02002332 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002333recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002334 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002335 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002336 goto sleep;
2337
2338 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002339 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002340 goto recheck;
2341
Tejun Heoc8e55f32010-06-29 10:07:12 +02002342 /*
2343 * ->scheduled list can only be filled while a worker is
2344 * preparing to process a work or actually processing it.
2345 * Make sure nobody diddled with it while I was sleeping.
2346 */
2347 BUG_ON(!list_empty(&worker->scheduled));
2348
Tejun Heoe22bee72010-06-29 10:07:14 +02002349 /*
2350 * When control reaches this point, we're guaranteed to have
2351 * at least one idle worker or that someone else has already
2352 * assumed the manager role.
2353 */
2354 worker_clr_flags(worker, WORKER_PREP);
2355
2356 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002357 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002358 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002359 struct work_struct, entry);
2360
2361 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2362 /* optimization path, not strictly necessary */
2363 process_one_work(worker, work);
2364 if (unlikely(!list_empty(&worker->scheduled)))
2365 process_scheduled_works(worker);
2366 } else {
2367 move_linked_works(work, &worker->scheduled, NULL);
2368 process_scheduled_works(worker);
2369 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002370 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002371
Tejun Heoe22bee72010-06-29 10:07:14 +02002372 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002373sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07002374 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002375 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002376
Tejun Heoc8e55f32010-06-29 10:07:12 +02002377 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02002378 * gcwq->lock is held and there's no work to process and no
2379 * need to manage, sleep. Workers are woken up only while
2380 * holding gcwq->lock or from local cpu, so setting the
2381 * current state before releasing gcwq->lock is enough to
2382 * prevent losing any event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002383 */
2384 worker_enter_idle(worker);
2385 __set_current_state(TASK_INTERRUPTIBLE);
2386 spin_unlock_irq(&gcwq->lock);
2387 schedule();
2388 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389}
2390
Tejun Heoe22bee72010-06-29 10:07:14 +02002391/**
2392 * rescuer_thread - the rescuer thread function
Tejun Heo111c2252013-01-17 17:16:24 -08002393 * @__rescuer: self
Tejun Heoe22bee72010-06-29 10:07:14 +02002394 *
2395 * Workqueue rescuer thread function. There's one rescuer for each
2396 * workqueue which has WQ_RESCUER set.
2397 *
2398 * Regular work processing on a gcwq may block trying to create a new
2399 * worker which uses GFP_KERNEL allocation which has slight chance of
2400 * developing into deadlock if some works currently on the same queue
2401 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2402 * the problem rescuer solves.
2403 *
2404 * When such condition is possible, the gcwq summons rescuers of all
2405 * workqueues which have works queued on the gcwq and let them process
2406 * those works so that forward progress can be guaranteed.
2407 *
2408 * This should happen rarely.
2409 */
Tejun Heo111c2252013-01-17 17:16:24 -08002410static int rescuer_thread(void *__rescuer)
Tejun Heoe22bee72010-06-29 10:07:14 +02002411{
Tejun Heo111c2252013-01-17 17:16:24 -08002412 struct worker *rescuer = __rescuer;
2413 struct workqueue_struct *wq = rescuer->rescue_wq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002414 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heof3421792010-07-02 10:03:51 +02002415 bool is_unbound = wq->flags & WQ_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02002416 unsigned int cpu;
2417
2418 set_user_nice(current, RESCUER_NICE_LEVEL);
Tejun Heo111c2252013-01-17 17:16:24 -08002419
2420 /*
2421 * Mark rescuer as worker too. As WORKER_PREP is never cleared, it
2422 * doesn't participate in concurrency management.
2423 */
2424 rescuer->task->flags |= PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002425repeat:
2426 set_current_state(TASK_INTERRUPTIBLE);
2427
Mike Galbraith412d32e2012-11-28 07:17:18 +01002428 if (kthread_should_stop()) {
2429 __set_current_state(TASK_RUNNING);
Tejun Heo111c2252013-01-17 17:16:24 -08002430 rescuer->task->flags &= ~PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002431 return 0;
Mike Galbraith412d32e2012-11-28 07:17:18 +01002432 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002433
Tejun Heof3421792010-07-02 10:03:51 +02002434 /*
2435 * See whether any cpu is asking for help. Unbounded
2436 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
2437 */
Tejun Heof2e005a2010-07-20 15:59:09 +02002438 for_each_mayday_cpu(cpu, wq->mayday_mask) {
Tejun Heof3421792010-07-02 10:03:51 +02002439 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2440 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002441 struct worker_pool *pool = cwq->pool;
2442 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002443 struct work_struct *work, *n;
2444
2445 __set_current_state(TASK_RUNNING);
Tejun Heof2e005a2010-07-20 15:59:09 +02002446 mayday_clear_cpu(cpu, wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002447
2448 /* migrate to the target cpu if possible */
Tejun Heobd7bdd42012-07-12 14:46:37 -07002449 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002450 worker_maybe_bind_and_lock(rescuer);
2451
2452 /*
2453 * Slurp in all works issued via this workqueue and
2454 * process'em.
2455 */
2456 BUG_ON(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002457 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02002458 if (get_work_cwq(work) == cwq)
2459 move_linked_works(work, scheduled, &n);
2460
2461 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002462
2463 /*
2464 * Leave this gcwq. If keep_working() is %true, notify a
2465 * regular worker; otherwise, we end up with 0 concurrency
2466 * and stalling the execution.
2467 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002468 if (keep_working(pool))
2469 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002470
Tejun Heoe22bee72010-06-29 10:07:14 +02002471 spin_unlock_irq(&gcwq->lock);
2472 }
2473
Tejun Heo111c2252013-01-17 17:16:24 -08002474 /* rescuers should never participate in concurrency management */
2475 WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
Tejun Heoe22bee72010-06-29 10:07:14 +02002476 schedule();
2477 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478}
2479
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002480struct wq_barrier {
2481 struct work_struct work;
2482 struct completion done;
2483};
2484
2485static void wq_barrier_func(struct work_struct *work)
2486{
2487 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2488 complete(&barr->done);
2489}
2490
Tejun Heo4690c4a2010-06-29 10:07:10 +02002491/**
2492 * insert_wq_barrier - insert a barrier work
2493 * @cwq: cwq to insert barrier into
2494 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002495 * @target: target work to attach @barr to
2496 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002497 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002498 * @barr is linked to @target such that @barr is completed only after
2499 * @target finishes execution. Please note that the ordering
2500 * guarantee is observed only with respect to @target and on the local
2501 * cpu.
2502 *
2503 * Currently, a queued barrier can't be canceled. This is because
2504 * try_to_grab_pending() can't determine whether the work to be
2505 * grabbed is at the head of the queue and thus can't clear LINKED
2506 * flag of the previous work while there must be a valid next work
2507 * after a work with LINKED flag set.
2508 *
2509 * Note that when @worker is non-NULL, @target may be modified
2510 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002511 *
2512 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002513 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002514 */
Oleg Nesterov83c22522007-05-09 02:33:54 -07002515static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002516 struct wq_barrier *barr,
2517 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002518{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002519 struct list_head *head;
2520 unsigned int linked = 0;
2521
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002522 /*
Tejun Heo8b03ae32010-06-29 10:07:12 +02002523 * debugobject calls are safe here even with gcwq->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002524 * as we know for sure that this will not trigger any of the
2525 * checks and call back into the fixup functions where we
2526 * might deadlock.
2527 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002528 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002529 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002530 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002531
Tejun Heoaffee4b2010-06-29 10:07:12 +02002532 /*
2533 * If @target is currently being executed, schedule the
2534 * barrier to the worker; otherwise, put it after @target.
2535 */
2536 if (worker)
2537 head = worker->scheduled.next;
2538 else {
2539 unsigned long *bits = work_data_bits(target);
2540
2541 head = target->entry.next;
2542 /* there can already be other linked works, inherit and set */
2543 linked = *bits & WORK_STRUCT_LINKED;
2544 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2545 }
2546
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002547 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002548 insert_work(cwq, &barr->work, head,
2549 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002550}
2551
Tejun Heo73f53c42010-06-29 10:07:11 +02002552/**
2553 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2554 * @wq: workqueue being flushed
2555 * @flush_color: new flush color, < 0 for no-op
2556 * @work_color: new work color, < 0 for no-op
2557 *
2558 * Prepare cwqs for workqueue flushing.
2559 *
2560 * If @flush_color is non-negative, flush_color on all cwqs should be
2561 * -1. If no cwq has in-flight commands at the specified color, all
2562 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2563 * has in flight commands, its cwq->flush_color is set to
2564 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2565 * wakeup logic is armed and %true is returned.
2566 *
2567 * The caller should have initialized @wq->first_flusher prior to
2568 * calling this function with non-negative @flush_color. If
2569 * @flush_color is negative, no flush color update is done and %false
2570 * is returned.
2571 *
2572 * If @work_color is non-negative, all cwqs should have the same
2573 * work_color which is previous to @work_color and all will be
2574 * advanced to @work_color.
2575 *
2576 * CONTEXT:
2577 * mutex_lock(wq->flush_mutex).
2578 *
2579 * RETURNS:
2580 * %true if @flush_color >= 0 and there's something to flush. %false
2581 * otherwise.
2582 */
2583static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2584 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585{
Tejun Heo73f53c42010-06-29 10:07:11 +02002586 bool wait = false;
2587 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -07002588
Tejun Heo73f53c42010-06-29 10:07:11 +02002589 if (flush_color >= 0) {
2590 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2591 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002592 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002593
Tejun Heof3421792010-07-02 10:03:51 +02002594 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002595 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002596 struct global_cwq *gcwq = cwq->pool->gcwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002597
Tejun Heo8b03ae32010-06-29 10:07:12 +02002598 spin_lock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002599
2600 if (flush_color >= 0) {
2601 BUG_ON(cwq->flush_color != -1);
2602
2603 if (cwq->nr_in_flight[flush_color]) {
2604 cwq->flush_color = flush_color;
2605 atomic_inc(&wq->nr_cwqs_to_flush);
2606 wait = true;
2607 }
2608 }
2609
2610 if (work_color >= 0) {
2611 BUG_ON(work_color != work_next_color(cwq->work_color));
2612 cwq->work_color = work_color;
2613 }
2614
Tejun Heo8b03ae32010-06-29 10:07:12 +02002615 spin_unlock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002616 }
2617
2618 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2619 complete(&wq->first_flusher->done);
2620
2621 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622}
2623
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002624/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002626 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 *
2628 * Forces execution of the workqueue and blocks until its completion.
2629 * This is typically used in driver shutdown handlers.
2630 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002631 * We sleep until all works which were queued on entry have been handled,
2632 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002634void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635{
Tejun Heo73f53c42010-06-29 10:07:11 +02002636 struct wq_flusher this_flusher = {
2637 .list = LIST_HEAD_INIT(this_flusher.list),
2638 .flush_color = -1,
2639 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2640 };
2641 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002642
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002643 lock_map_acquire(&wq->lockdep_map);
2644 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002645
2646 mutex_lock(&wq->flush_mutex);
2647
2648 /*
2649 * Start-to-wait phase
2650 */
2651 next_color = work_next_color(wq->work_color);
2652
2653 if (next_color != wq->flush_color) {
2654 /*
2655 * Color space is not full. The current work_color
2656 * becomes our flush_color and work_color is advanced
2657 * by one.
2658 */
2659 BUG_ON(!list_empty(&wq->flusher_overflow));
2660 this_flusher.flush_color = wq->work_color;
2661 wq->work_color = next_color;
2662
2663 if (!wq->first_flusher) {
2664 /* no flush in progress, become the first flusher */
2665 BUG_ON(wq->flush_color != this_flusher.flush_color);
2666
2667 wq->first_flusher = &this_flusher;
2668
2669 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2670 wq->work_color)) {
2671 /* nothing to flush, done */
2672 wq->flush_color = next_color;
2673 wq->first_flusher = NULL;
2674 goto out_unlock;
2675 }
2676 } else {
2677 /* wait in queue */
2678 BUG_ON(wq->flush_color == this_flusher.flush_color);
2679 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2680 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2681 }
2682 } else {
2683 /*
2684 * Oops, color space is full, wait on overflow queue.
2685 * The next flush completion will assign us
2686 * flush_color and transfer to flusher_queue.
2687 */
2688 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2689 }
2690
2691 mutex_unlock(&wq->flush_mutex);
2692
2693 wait_for_completion(&this_flusher.done);
2694
2695 /*
2696 * Wake-up-and-cascade phase
2697 *
2698 * First flushers are responsible for cascading flushes and
2699 * handling overflow. Non-first flushers can simply return.
2700 */
2701 if (wq->first_flusher != &this_flusher)
2702 return;
2703
2704 mutex_lock(&wq->flush_mutex);
2705
Tejun Heo4ce48b32010-07-02 10:03:51 +02002706 /* we might have raced, check again with mutex held */
2707 if (wq->first_flusher != &this_flusher)
2708 goto out_unlock;
2709
Tejun Heo73f53c42010-06-29 10:07:11 +02002710 wq->first_flusher = NULL;
2711
2712 BUG_ON(!list_empty(&this_flusher.list));
2713 BUG_ON(wq->flush_color != this_flusher.flush_color);
2714
2715 while (true) {
2716 struct wq_flusher *next, *tmp;
2717
2718 /* complete all the flushers sharing the current flush color */
2719 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2720 if (next->flush_color != wq->flush_color)
2721 break;
2722 list_del_init(&next->list);
2723 complete(&next->done);
2724 }
2725
2726 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2727 wq->flush_color != work_next_color(wq->work_color));
2728
2729 /* this flush_color is finished, advance by one */
2730 wq->flush_color = work_next_color(wq->flush_color);
2731
2732 /* one color has been freed, handle overflow queue */
2733 if (!list_empty(&wq->flusher_overflow)) {
2734 /*
2735 * Assign the same color to all overflowed
2736 * flushers, advance work_color and append to
2737 * flusher_queue. This is the start-to-wait
2738 * phase for these overflowed flushers.
2739 */
2740 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2741 tmp->flush_color = wq->work_color;
2742
2743 wq->work_color = work_next_color(wq->work_color);
2744
2745 list_splice_tail_init(&wq->flusher_overflow,
2746 &wq->flusher_queue);
2747 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2748 }
2749
2750 if (list_empty(&wq->flusher_queue)) {
2751 BUG_ON(wq->flush_color != wq->work_color);
2752 break;
2753 }
2754
2755 /*
2756 * Need to flush more colors. Make the next flusher
2757 * the new first flusher and arm cwqs.
2758 */
2759 BUG_ON(wq->flush_color == wq->work_color);
2760 BUG_ON(wq->flush_color != next->flush_color);
2761
2762 list_del_init(&next->list);
2763 wq->first_flusher = next;
2764
2765 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2766 break;
2767
2768 /*
2769 * Meh... this color is already done, clear first
2770 * flusher and repeat cascading.
2771 */
2772 wq->first_flusher = NULL;
2773 }
2774
2775out_unlock:
2776 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777}
Dave Jonesae90dd52006-06-30 01:40:45 -04002778EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002780/**
2781 * drain_workqueue - drain a workqueue
2782 * @wq: workqueue to drain
2783 *
2784 * Wait until the workqueue becomes empty. While draining is in progress,
2785 * only chain queueing is allowed. IOW, only currently pending or running
2786 * work items on @wq can queue further work items on it. @wq is flushed
2787 * repeatedly until it becomes empty. The number of flushing is detemined
2788 * by the depth of chaining and should be relatively short. Whine if it
2789 * takes too long.
2790 */
2791void drain_workqueue(struct workqueue_struct *wq)
2792{
2793 unsigned int flush_cnt = 0;
2794 unsigned int cpu;
2795
2796 /*
2797 * __queue_work() needs to test whether there are drainers, is much
2798 * hotter than drain_workqueue() and already looks at @wq->flags.
2799 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2800 */
2801 spin_lock(&workqueue_lock);
2802 if (!wq->nr_drainers++)
2803 wq->flags |= WQ_DRAINING;
2804 spin_unlock(&workqueue_lock);
2805reflush:
2806 flush_workqueue(wq);
2807
2808 for_each_cwq_cpu(cpu, wq) {
2809 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002810 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002811
Tejun Heobd7bdd42012-07-12 14:46:37 -07002812 spin_lock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002813 drained = !cwq->nr_active && list_empty(&cwq->delayed_works);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002814 spin_unlock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002815
2816 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002817 continue;
2818
2819 if (++flush_cnt == 10 ||
2820 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
Valentin Ilie044c7822012-08-19 00:52:42 +03002821 pr_warn("workqueue %s: flush on destruction isn't complete after %u tries\n",
2822 wq->name, flush_cnt);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002823 goto reflush;
2824 }
2825
2826 spin_lock(&workqueue_lock);
2827 if (!--wq->nr_drainers)
2828 wq->flags &= ~WQ_DRAINING;
2829 spin_unlock(&workqueue_lock);
2830}
2831EXPORT_SYMBOL_GPL(drain_workqueue);
2832
Tejun Heo606a5022012-08-20 14:51:23 -07002833static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
Tejun Heobaf59022010-09-16 10:42:16 +02002834{
2835 struct worker *worker = NULL;
2836 struct global_cwq *gcwq;
2837 struct cpu_workqueue_struct *cwq;
2838
2839 might_sleep();
2840 gcwq = get_work_gcwq(work);
2841 if (!gcwq)
2842 return false;
2843
2844 spin_lock_irq(&gcwq->lock);
2845 if (!list_empty(&work->entry)) {
2846 /*
2847 * See the comment near try_to_grab_pending()->smp_rmb().
2848 * If it was re-queued to a different gcwq under us, we
2849 * are not going to wait.
2850 */
2851 smp_rmb();
2852 cwq = get_work_cwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002853 if (unlikely(!cwq || gcwq != cwq->pool->gcwq))
Tejun Heobaf59022010-09-16 10:42:16 +02002854 goto already_gone;
Tejun Heo606a5022012-08-20 14:51:23 -07002855 } else {
Tejun Heobaf59022010-09-16 10:42:16 +02002856 worker = find_worker_executing_work(gcwq, work);
2857 if (!worker)
2858 goto already_gone;
2859 cwq = worker->current_cwq;
Tejun Heo606a5022012-08-20 14:51:23 -07002860 }
Tejun Heobaf59022010-09-16 10:42:16 +02002861
2862 insert_wq_barrier(cwq, barr, work, worker);
2863 spin_unlock_irq(&gcwq->lock);
2864
Tejun Heoe1594892011-01-09 23:32:15 +01002865 /*
2866 * If @max_active is 1 or rescuer is in use, flushing another work
2867 * item on the same workqueue may lead to deadlock. Make sure the
2868 * flusher is not running on the same workqueue by verifying write
2869 * access.
2870 */
2871 if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER)
2872 lock_map_acquire(&cwq->wq->lockdep_map);
2873 else
2874 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heobaf59022010-09-16 10:42:16 +02002875 lock_map_release(&cwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002876
Tejun Heobaf59022010-09-16 10:42:16 +02002877 return true;
2878already_gone:
2879 spin_unlock_irq(&gcwq->lock);
2880 return false;
2881}
2882
Oleg Nesterovdb700892008-07-25 01:47:49 -07002883/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002884 * flush_work - wait for a work to finish executing the last queueing instance
2885 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002886 *
Tejun Heo606a5022012-08-20 14:51:23 -07002887 * Wait until @work has finished execution. @work is guaranteed to be idle
2888 * on return if it hasn't been requeued since flush started.
Tejun Heo401a8d02010-09-16 10:36:00 +02002889 *
2890 * RETURNS:
2891 * %true if flush_work() waited for the work to finish execution,
2892 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002893 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002894bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002895{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002896 struct wq_barrier barr;
2897
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002898 lock_map_acquire(&work->lockdep_map);
2899 lock_map_release(&work->lockdep_map);
2900
Tejun Heo606a5022012-08-20 14:51:23 -07002901 if (start_flush_work(work, &barr)) {
Tejun Heobaf59022010-09-16 10:42:16 +02002902 wait_for_completion(&barr.done);
2903 destroy_work_on_stack(&barr.work);
2904 return true;
Tejun Heo606a5022012-08-20 14:51:23 -07002905 } else {
Tejun Heobaf59022010-09-16 10:42:16 +02002906 return false;
Tejun Heo606a5022012-08-20 14:51:23 -07002907 }
Oleg Nesterovdb700892008-07-25 01:47:49 -07002908}
2909EXPORT_SYMBOL_GPL(flush_work);
2910
Tejun Heo36e227d2012-08-03 10:30:46 -07002911static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
Tejun Heo401a8d02010-09-16 10:36:00 +02002912{
Tejun Heobbb68df2012-08-03 10:30:46 -07002913 unsigned long flags;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002914 int ret;
2915
2916 do {
Tejun Heobbb68df2012-08-03 10:30:46 -07002917 ret = try_to_grab_pending(work, is_dwork, &flags);
2918 /*
2919 * If someone else is canceling, wait for the same event it
2920 * would be waiting for before retrying.
2921 */
2922 if (unlikely(ret == -ENOENT))
Tejun Heo606a5022012-08-20 14:51:23 -07002923 flush_work(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002924 } while (unlikely(ret < 0));
2925
Tejun Heobbb68df2012-08-03 10:30:46 -07002926 /* tell other tasks trying to grab @work to back off */
2927 mark_work_canceling(work);
2928 local_irq_restore(flags);
2929
Tejun Heo606a5022012-08-20 14:51:23 -07002930 flush_work(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002931 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002932 return ret;
2933}
2934
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002935/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002936 * cancel_work_sync - cancel a work and wait for it to finish
2937 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002938 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002939 * Cancel @work and wait for its execution to finish. This function
2940 * can be used even if the work re-queues itself or migrates to
2941 * another workqueue. On return from this function, @work is
2942 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002943 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002944 * cancel_work_sync(&delayed_work->work) must not be used for
2945 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002946 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002947 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002948 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002949 *
2950 * RETURNS:
2951 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002952 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002953bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002954{
Tejun Heo36e227d2012-08-03 10:30:46 -07002955 return __cancel_work_timer(work, false);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002956}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002957EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002958
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002959/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002960 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2961 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002962 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002963 * Delayed timer is cancelled and the pending work is queued for
2964 * immediate execution. Like flush_work(), this function only
2965 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002966 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002967 * RETURNS:
2968 * %true if flush_work() waited for the work to finish execution,
2969 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002970 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002971bool flush_delayed_work(struct delayed_work *dwork)
2972{
Tejun Heo8930cab2012-08-03 10:30:45 -07002973 local_irq_disable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002974 if (del_timer_sync(&dwork->timer))
Tejun Heo12650572012-08-08 09:38:42 -07002975 __queue_work(dwork->cpu,
Tejun Heo401a8d02010-09-16 10:36:00 +02002976 get_work_cwq(&dwork->work)->wq, &dwork->work);
Tejun Heo8930cab2012-08-03 10:30:45 -07002977 local_irq_enable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002978 return flush_work(&dwork->work);
2979}
2980EXPORT_SYMBOL(flush_delayed_work);
2981
2982/**
Tejun Heo57b30ae2012-08-21 13:18:24 -07002983 * cancel_delayed_work - cancel a delayed work
2984 * @dwork: delayed_work to cancel
Tejun Heo09383492010-09-16 10:48:29 +02002985 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002986 * Kill off a pending delayed_work. Returns %true if @dwork was pending
2987 * and canceled; %false if wasn't pending. Note that the work callback
2988 * function may still be running on return, unless it returns %true and the
2989 * work doesn't re-arm itself. Explicitly flush or use
2990 * cancel_delayed_work_sync() to wait on it.
Tejun Heo09383492010-09-16 10:48:29 +02002991 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002992 * This function is safe to call from any context including IRQ handler.
Tejun Heo09383492010-09-16 10:48:29 +02002993 */
Tejun Heo57b30ae2012-08-21 13:18:24 -07002994bool cancel_delayed_work(struct delayed_work *dwork)
Tejun Heo09383492010-09-16 10:48:29 +02002995{
Tejun Heo57b30ae2012-08-21 13:18:24 -07002996 unsigned long flags;
2997 int ret;
2998
2999 do {
3000 ret = try_to_grab_pending(&dwork->work, true, &flags);
3001 } while (unlikely(ret == -EAGAIN));
3002
3003 if (unlikely(ret < 0))
3004 return false;
3005
Tejun Heo7c3eed52013-01-24 11:01:33 -08003006 set_work_pool_and_clear_pending(&dwork->work,
3007 get_work_pool_id(&dwork->work));
Tejun Heo57b30ae2012-08-21 13:18:24 -07003008 local_irq_restore(flags);
Dan Magenheimerc0158ca2012-10-18 16:31:37 -07003009 return ret;
Tejun Heo09383492010-09-16 10:48:29 +02003010}
Tejun Heo57b30ae2012-08-21 13:18:24 -07003011EXPORT_SYMBOL(cancel_delayed_work);
Tejun Heo09383492010-09-16 10:48:29 +02003012
3013/**
Tejun Heo401a8d02010-09-16 10:36:00 +02003014 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
3015 * @dwork: the delayed work cancel
3016 *
3017 * This is cancel_work_sync() for delayed works.
3018 *
3019 * RETURNS:
3020 * %true if @dwork was pending, %false otherwise.
3021 */
3022bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07003023{
Tejun Heo36e227d2012-08-03 10:30:46 -07003024 return __cancel_work_timer(&dwork->work, true);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07003025}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07003026EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003028/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07003029 * schedule_work_on - put work task on a specific cpu
3030 * @cpu: cpu to put the work task on
3031 * @work: job to be done
3032 *
3033 * This puts a job on a specific cpu
3034 */
Tejun Heod4283e92012-08-03 10:30:44 -07003035bool schedule_work_on(int cpu, struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07003036{
Tejun Heod320c032010-06-29 10:07:14 +02003037 return queue_work_on(cpu, system_wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07003038}
3039EXPORT_SYMBOL(schedule_work_on);
3040
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003041/**
Dave Jonesae90dd52006-06-30 01:40:45 -04003042 * schedule_work - put work task in global workqueue
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 * @work: job to be done
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 *
Tejun Heod4283e92012-08-03 10:30:44 -07003045 * Returns %false if @work was already on the kernel-global workqueue and
3046 * %true otherwise.
David Howells52bad642006-11-22 14:54:01 +00003047 *
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003048 * This puts a job in the kernel-global workqueue if it was not already
3049 * queued and leaves it in the same position on the kernel-global
3050 * workqueue otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 */
Tejun Heod4283e92012-08-03 10:30:44 -07003052bool schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053{
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003054 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055}
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003056EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003058/**
3059 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
3060 * @cpu: cpu to use
3061 * @dwork: job to be done
3062 * @delay: number of jiffies to wait
3063 *
3064 * After waiting for a given time this puts a job in the kernel-global
3065 * workqueue on the specified CPU.
3066 */
Tejun Heod4283e92012-08-03 10:30:44 -07003067bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork,
3068 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069{
Tejun Heod320c032010-06-29 10:07:14 +02003070 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071}
Dave Jonesae90dd52006-06-30 01:40:45 -04003072EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
Andrew Mortonb6136772006-06-25 05:47:49 -07003074/**
Tejun Heo0a13c002012-08-03 10:30:44 -07003075 * schedule_delayed_work - put work task in global workqueue after delay
3076 * @dwork: job to be done
3077 * @delay: number of jiffies to wait or 0 for immediate execution
3078 *
3079 * After waiting for a given time this puts a job in the kernel-global
3080 * workqueue.
3081 */
Tejun Heod4283e92012-08-03 10:30:44 -07003082bool schedule_delayed_work(struct delayed_work *dwork, unsigned long delay)
Tejun Heo0a13c002012-08-03 10:30:44 -07003083{
3084 return queue_delayed_work(system_wq, dwork, delay);
3085}
3086EXPORT_SYMBOL(schedule_delayed_work);
3087
3088/**
Tejun Heo31ddd872010-10-19 11:14:49 +02003089 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07003090 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07003091 *
Tejun Heo31ddd872010-10-19 11:14:49 +02003092 * schedule_on_each_cpu() executes @func on each online CPU using the
3093 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07003094 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02003095 *
3096 * RETURNS:
3097 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07003098 */
David Howells65f27f32006-11-22 14:55:48 +00003099int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08003100{
3101 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02003102 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08003103
Andrew Mortonb6136772006-06-25 05:47:49 -07003104 works = alloc_percpu(struct work_struct);
3105 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08003106 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07003107
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003108 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08003109
Christoph Lameter15316ba2006-01-08 01:00:43 -08003110 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01003111 struct work_struct *work = per_cpu_ptr(works, cpu);
3112
3113 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003114 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02003115 }
Tejun Heo93981802009-11-17 14:06:20 -08003116
3117 for_each_online_cpu(cpu)
3118 flush_work(per_cpu_ptr(works, cpu));
3119
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003120 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07003121 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08003122 return 0;
3123}
3124
Alan Sterneef6a7d2010-02-12 17:39:21 +09003125/**
3126 * flush_scheduled_work - ensure that any scheduled work has run to completion.
3127 *
3128 * Forces execution of the kernel-global workqueue and blocks until its
3129 * completion.
3130 *
3131 * Think twice before calling this function! It's very easy to get into
3132 * trouble if you don't take great care. Either of the following situations
3133 * will lead to deadlock:
3134 *
3135 * One of the work items currently on the workqueue needs to acquire
3136 * a lock held by your code or its caller.
3137 *
3138 * Your code is running in the context of a work routine.
3139 *
3140 * They will be detected by lockdep when they occur, but the first might not
3141 * occur very often. It depends on what work items are on the workqueue and
3142 * what locks they need, which you have no control over.
3143 *
3144 * In most situations flushing the entire workqueue is overkill; you merely
3145 * need to know that a particular work item isn't queued and isn't running.
3146 * In such cases you should use cancel_delayed_work_sync() or
3147 * cancel_work_sync() instead.
3148 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149void flush_scheduled_work(void)
3150{
Tejun Heod320c032010-06-29 10:07:14 +02003151 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152}
Dave Jonesae90dd52006-06-30 01:40:45 -04003153EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
3155/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06003156 * execute_in_process_context - reliably execute the routine with user context
3157 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06003158 * @ew: guaranteed storage for the execute work structure (must
3159 * be available when the work executes)
3160 *
3161 * Executes the function immediately if process context is available,
3162 * otherwise schedules the function for delayed execution.
3163 *
3164 * Returns: 0 - function was executed
3165 * 1 - function was scheduled for execution
3166 */
David Howells65f27f32006-11-22 14:55:48 +00003167int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06003168{
3169 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00003170 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003171 return 0;
3172 }
3173
David Howells65f27f32006-11-22 14:55:48 +00003174 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003175 schedule_work(&ew->work);
3176
3177 return 1;
3178}
3179EXPORT_SYMBOL_GPL(execute_in_process_context);
3180
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181int keventd_up(void)
3182{
Tejun Heod320c032010-06-29 10:07:14 +02003183 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184}
3185
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003186static int alloc_cwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187{
Oleg Nesterov3af244332007-05-09 02:34:09 -07003188 /*
Tejun Heo0f900042010-06-29 10:07:11 +02003189 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
3190 * Make sure that the alignment isn't lower than that of
3191 * unsigned long long.
Oleg Nesterov3af244332007-05-09 02:34:09 -07003192 */
Tejun Heo0f900042010-06-29 10:07:11 +02003193 const size_t size = sizeof(struct cpu_workqueue_struct);
3194 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
3195 __alignof__(unsigned long long));
Oleg Nesterov3af244332007-05-09 02:34:09 -07003196
Lai Jiangshane06ffa12012-03-09 18:03:20 +08003197 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02003198 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
Tejun Heo931ac772010-07-20 11:07:48 +02003199 else {
Tejun Heof3421792010-07-02 10:03:51 +02003200 void *ptr;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01003201
Tejun Heof3421792010-07-02 10:03:51 +02003202 /*
3203 * Allocate enough room to align cwq and put an extra
3204 * pointer at the end pointing back to the originally
3205 * allocated pointer which will be used for free.
3206 */
3207 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
3208 if (ptr) {
3209 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
3210 *(void **)(wq->cpu_wq.single + 1) = ptr;
3211 }
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003212 }
Tejun Heof3421792010-07-02 10:03:51 +02003213
Tejun Heo0415b00d12011-03-24 18:50:09 +01003214 /* just in case, make sure it's actually aligned */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003215 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
3216 return wq->cpu_wq.v ? 0 : -ENOMEM;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003217}
3218
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003219static void free_cwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07003220{
Lai Jiangshane06ffa12012-03-09 18:03:20 +08003221 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02003222 free_percpu(wq->cpu_wq.pcpu);
3223 else if (wq->cpu_wq.single) {
3224 /* the pointer to free is stored right after the cwq */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003225 kfree(*(void **)(wq->cpu_wq.single + 1));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07003226 }
3227}
3228
Tejun Heof3421792010-07-02 10:03:51 +02003229static int wq_clamp_max_active(int max_active, unsigned int flags,
3230 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003231{
Tejun Heof3421792010-07-02 10:03:51 +02003232 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3233
3234 if (max_active < 1 || max_active > lim)
Valentin Ilie044c7822012-08-19 00:52:42 +03003235 pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
3236 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003237
Tejun Heof3421792010-07-02 10:03:51 +02003238 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003239}
3240
Tejun Heob196be82012-01-10 15:11:35 -08003241struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02003242 unsigned int flags,
3243 int max_active,
3244 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08003245 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003246{
Tejun Heob196be82012-01-10 15:11:35 -08003247 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003248 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02003249 unsigned int cpu;
Tejun Heob196be82012-01-10 15:11:35 -08003250 size_t namelen;
3251
3252 /* determine namelen, allocate wq and format name */
3253 va_start(args, lock_name);
3254 va_copy(args1, args);
3255 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3256
3257 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3258 if (!wq)
3259 goto err;
3260
3261 vsnprintf(wq->name, namelen, fmt, args1);
3262 va_end(args);
3263 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003264
Tejun Heof3421792010-07-02 10:03:51 +02003265 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02003266 * Workqueues which may be used during memory reclaim should
3267 * have a rescuer to guarantee forward progress.
3268 */
3269 if (flags & WQ_MEM_RECLAIM)
3270 flags |= WQ_RESCUER;
3271
Tejun Heod320c032010-06-29 10:07:14 +02003272 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003273 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003274
Tejun Heob196be82012-01-10 15:11:35 -08003275 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003276 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003277 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003278 mutex_init(&wq->flush_mutex);
3279 atomic_set(&wq->nr_cwqs_to_flush, 0);
3280 INIT_LIST_HEAD(&wq->flusher_queue);
3281 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003282
Johannes Bergeb13ba82008-01-16 09:51:58 +01003283 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003284 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003285
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003286 if (alloc_cwqs(wq) < 0)
3287 goto err;
3288
Tejun Heof3421792010-07-02 10:03:51 +02003289 for_each_cwq_cpu(cpu, wq) {
Tejun Heo15376632010-06-29 10:07:11 +02003290 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003291 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo32704762012-07-13 22:16:45 -07003292 int pool_idx = (bool)(flags & WQ_HIGHPRI);
Tejun Heo15376632010-06-29 10:07:11 +02003293
Tejun Heo0f900042010-06-29 10:07:11 +02003294 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heo32704762012-07-13 22:16:45 -07003295 cwq->pool = &gcwq->pools[pool_idx];
Tejun Heoc34056a2010-06-29 10:07:11 +02003296 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02003297 cwq->flush_color = -1;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003298 cwq->max_active = max_active;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003299 INIT_LIST_HEAD(&cwq->delayed_works);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003300 }
3301
Tejun Heoe22bee72010-06-29 10:07:14 +02003302 if (flags & WQ_RESCUER) {
3303 struct worker *rescuer;
3304
Tejun Heof2e005a2010-07-20 15:59:09 +02003305 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
Tejun Heoe22bee72010-06-29 10:07:14 +02003306 goto err;
3307
3308 wq->rescuer = rescuer = alloc_worker();
3309 if (!rescuer)
3310 goto err;
3311
Tejun Heo111c2252013-01-17 17:16:24 -08003312 rescuer->rescue_wq = wq;
3313 rescuer->task = kthread_create(rescuer_thread, rescuer, "%s",
Tejun Heob196be82012-01-10 15:11:35 -08003314 wq->name);
Tejun Heoe22bee72010-06-29 10:07:14 +02003315 if (IS_ERR(rescuer->task))
3316 goto err;
3317
Tejun Heoe22bee72010-06-29 10:07:14 +02003318 rescuer->task->flags |= PF_THREAD_BOUND;
3319 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003320 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003321
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003322 /*
3323 * workqueue_lock protects global freeze state and workqueues
3324 * list. Grab it, set max_active accordingly and add the new
3325 * workqueue to workqueues list.
3326 */
Tejun Heo15376632010-06-29 10:07:11 +02003327 spin_lock(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003328
Tejun Heo58a69cb2011-02-16 09:25:31 +01003329 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heof3421792010-07-02 10:03:51 +02003330 for_each_cwq_cpu(cpu, wq)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003331 get_cwq(cpu, wq)->max_active = 0;
3332
Tejun Heo15376632010-06-29 10:07:11 +02003333 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003334
Tejun Heo15376632010-06-29 10:07:11 +02003335 spin_unlock(&workqueue_lock);
3336
Oleg Nesterov3af244332007-05-09 02:34:09 -07003337 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02003338err:
3339 if (wq) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003340 free_cwqs(wq);
Tejun Heof2e005a2010-07-20 15:59:09 +02003341 free_mayday_mask(wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02003342 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003343 kfree(wq);
3344 }
3345 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003346}
Tejun Heod320c032010-06-29 10:07:14 +02003347EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003348
3349/**
3350 * destroy_workqueue - safely terminate a workqueue
3351 * @wq: target workqueue
3352 *
3353 * Safely destroy a workqueue. All work currently pending will be done first.
3354 */
3355void destroy_workqueue(struct workqueue_struct *wq)
3356{
Tejun Heoc8e55f32010-06-29 10:07:12 +02003357 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003358
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003359 /* drain it before proceeding with destruction */
3360 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003361
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003362 /*
3363 * wq list is used to freeze wq, remove from list after
3364 * flushing is complete in case freeze races us.
3365 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003366 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07003367 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003368 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003369
Tejun Heoe22bee72010-06-29 10:07:14 +02003370 /* sanity check */
Tejun Heof3421792010-07-02 10:03:51 +02003371 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02003372 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3373 int i;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003374
Tejun Heo73f53c42010-06-29 10:07:11 +02003375 for (i = 0; i < WORK_NR_COLORS; i++)
3376 BUG_ON(cwq->nr_in_flight[i]);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003377 BUG_ON(cwq->nr_active);
3378 BUG_ON(!list_empty(&cwq->delayed_works));
Tejun Heo73f53c42010-06-29 10:07:11 +02003379 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003380
Tejun Heoe22bee72010-06-29 10:07:14 +02003381 if (wq->flags & WQ_RESCUER) {
3382 kthread_stop(wq->rescuer->task);
Tejun Heof2e005a2010-07-20 15:59:09 +02003383 free_mayday_mask(wq->mayday_mask);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003384 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003385 }
3386
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003387 free_cwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003388 kfree(wq);
3389}
3390EXPORT_SYMBOL_GPL(destroy_workqueue);
3391
Tejun Heodcd989c2010-06-29 10:07:14 +02003392/**
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003393 * cwq_set_max_active - adjust max_active of a cwq
3394 * @cwq: target cpu_workqueue_struct
3395 * @max_active: new max_active value.
3396 *
3397 * Set @cwq->max_active to @max_active and activate delayed works if
3398 * increased.
3399 *
3400 * CONTEXT:
3401 * spin_lock_irq(gcwq->lock).
3402 */
3403static void cwq_set_max_active(struct cpu_workqueue_struct *cwq, int max_active)
3404{
3405 cwq->max_active = max_active;
3406
3407 while (!list_empty(&cwq->delayed_works) &&
3408 cwq->nr_active < cwq->max_active)
3409 cwq_activate_first_delayed(cwq);
3410}
3411
3412/**
Tejun Heodcd989c2010-06-29 10:07:14 +02003413 * workqueue_set_max_active - adjust max_active of a workqueue
3414 * @wq: target workqueue
3415 * @max_active: new max_active value.
3416 *
3417 * Set max_active of @wq to @max_active.
3418 *
3419 * CONTEXT:
3420 * Don't call from IRQ context.
3421 */
3422void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3423{
3424 unsigned int cpu;
3425
Tejun Heof3421792010-07-02 10:03:51 +02003426 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003427
3428 spin_lock(&workqueue_lock);
3429
3430 wq->saved_max_active = max_active;
3431
Tejun Heof3421792010-07-02 10:03:51 +02003432 for_each_cwq_cpu(cpu, wq) {
Tejun Heo35b6bb62013-01-24 11:01:33 -08003433 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3434 struct worker_pool *pool = cwq->pool;
3435 struct global_cwq *gcwq = pool->gcwq;
Tejun Heodcd989c2010-06-29 10:07:14 +02003436
3437 spin_lock_irq(&gcwq->lock);
3438
Tejun Heo58a69cb2011-02-16 09:25:31 +01003439 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heo35b6bb62013-01-24 11:01:33 -08003440 !(pool->flags & POOL_FREEZING))
3441 cwq_set_max_active(cwq, max_active);
Tejun Heodcd989c2010-06-29 10:07:14 +02003442
3443 spin_unlock_irq(&gcwq->lock);
3444 }
3445
3446 spin_unlock(&workqueue_lock);
3447}
3448EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3449
3450/**
3451 * workqueue_congested - test whether a workqueue is congested
3452 * @cpu: CPU in question
3453 * @wq: target workqueue
3454 *
3455 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3456 * no synchronization around this function and the test result is
3457 * unreliable and only useful as advisory hints or for debugging.
3458 *
3459 * RETURNS:
3460 * %true if congested, %false otherwise.
3461 */
3462bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3463{
3464 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3465
3466 return !list_empty(&cwq->delayed_works);
3467}
3468EXPORT_SYMBOL_GPL(workqueue_congested);
3469
3470/**
Tejun Heodcd989c2010-06-29 10:07:14 +02003471 * work_busy - test whether a work is currently pending or running
3472 * @work: the work to be tested
3473 *
3474 * Test whether @work is currently pending or running. There is no
3475 * synchronization around this function and the test result is
3476 * unreliable and only useful as advisory hints or for debugging.
3477 * Especially for reentrant wqs, the pending state might hide the
3478 * running state.
3479 *
3480 * RETURNS:
3481 * OR'd bitmask of WORK_BUSY_* bits.
3482 */
3483unsigned int work_busy(struct work_struct *work)
3484{
3485 struct global_cwq *gcwq = get_work_gcwq(work);
3486 unsigned long flags;
3487 unsigned int ret = 0;
3488
3489 if (!gcwq)
Joonsoo Kim999767b2012-10-21 01:30:06 +09003490 return 0;
Tejun Heodcd989c2010-06-29 10:07:14 +02003491
3492 spin_lock_irqsave(&gcwq->lock, flags);
3493
3494 if (work_pending(work))
3495 ret |= WORK_BUSY_PENDING;
3496 if (find_worker_executing_work(gcwq, work))
3497 ret |= WORK_BUSY_RUNNING;
3498
3499 spin_unlock_irqrestore(&gcwq->lock, flags);
3500
3501 return ret;
3502}
3503EXPORT_SYMBOL_GPL(work_busy);
3504
Tejun Heodb7bccf2010-06-29 10:07:12 +02003505/*
3506 * CPU hotplug.
3507 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003508 * There are two challenges in supporting CPU hotplug. Firstly, there
3509 * are a lot of assumptions on strong associations among work, cwq and
3510 * gcwq which make migrating pending and scheduled works very
3511 * difficult to implement without impacting hot paths. Secondly,
3512 * gcwqs serve mix of short, long and very long running works making
3513 * blocked draining impractical.
3514 *
Tejun Heo24647572013-01-24 11:01:33 -08003515 * This is solved by allowing the pools to be disassociated from the CPU
Tejun Heo628c78e2012-07-17 12:39:27 -07003516 * running as an unbound one and allowing it to be reattached later if the
3517 * cpu comes back online.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003518 */
3519
Tejun Heo60373152012-07-17 12:39:27 -07003520/* claim manager positions of all pools */
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07003521static void gcwq_claim_assoc_and_lock(struct global_cwq *gcwq)
Tejun Heo60373152012-07-17 12:39:27 -07003522{
3523 struct worker_pool *pool;
3524
3525 for_each_worker_pool(pool, gcwq)
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07003526 mutex_lock_nested(&pool->assoc_mutex, pool - gcwq->pools);
Tejun Heo8db25e72012-07-17 12:39:28 -07003527 spin_lock_irq(&gcwq->lock);
Tejun Heo60373152012-07-17 12:39:27 -07003528}
3529
3530/* release manager positions */
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07003531static void gcwq_release_assoc_and_unlock(struct global_cwq *gcwq)
Tejun Heo60373152012-07-17 12:39:27 -07003532{
3533 struct worker_pool *pool;
3534
Tejun Heo8db25e72012-07-17 12:39:28 -07003535 spin_unlock_irq(&gcwq->lock);
Tejun Heo60373152012-07-17 12:39:27 -07003536 for_each_worker_pool(pool, gcwq)
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07003537 mutex_unlock(&pool->assoc_mutex);
Tejun Heo60373152012-07-17 12:39:27 -07003538}
3539
Tejun Heo628c78e2012-07-17 12:39:27 -07003540static void gcwq_unbind_fn(struct work_struct *work)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003541{
Tejun Heo628c78e2012-07-17 12:39:27 -07003542 struct global_cwq *gcwq = get_gcwq(smp_processor_id());
Tejun Heo4ce62e92012-07-13 22:16:44 -07003543 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003544 struct worker *worker;
3545 struct hlist_node *pos;
3546 int i;
3547
3548 BUG_ON(gcwq->cpu != smp_processor_id());
3549
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07003550 gcwq_claim_assoc_and_lock(gcwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02003551
Tejun Heof2d5a0e2012-07-17 12:39:26 -07003552 /*
3553 * We've claimed all manager positions. Make all workers unbound
3554 * and set DISASSOCIATED. Before this, all workers except for the
3555 * ones which are still executing works from before the last CPU
3556 * down must be on the cpu. After this, they may become diasporas.
3557 */
Tejun Heo60373152012-07-17 12:39:27 -07003558 for_each_worker_pool(pool, gcwq)
Tejun Heo4ce62e92012-07-13 22:16:44 -07003559 list_for_each_entry(worker, &pool->idle_list, entry)
Tejun Heo403c8212012-07-17 12:39:27 -07003560 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003561
3562 for_each_busy_worker(worker, i, pos, gcwq)
Tejun Heo403c8212012-07-17 12:39:27 -07003563 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003564
Tejun Heo24647572013-01-24 11:01:33 -08003565 for_each_worker_pool(pool, gcwq)
3566 pool->flags |= POOL_DISASSOCIATED;
Tejun Heof2d5a0e2012-07-17 12:39:26 -07003567
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07003568 gcwq_release_assoc_and_unlock(gcwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02003569
3570 /*
Tejun Heo628c78e2012-07-17 12:39:27 -07003571 * Call schedule() so that we cross rq->lock and thus can guarantee
3572 * sched callbacks see the %WORKER_UNBOUND flag. This is necessary
3573 * as scheduler callbacks may be invoked from other cpus.
3574 */
3575 schedule();
3576
3577 /*
3578 * Sched callbacks are disabled now. Zap nr_running. After this,
3579 * nr_running stays zero and need_more_worker() and keep_working()
3580 * are always true as long as the worklist is not empty. @gcwq now
3581 * behaves as unbound (in terms of concurrency management) gcwq
3582 * which is served by workers tied to the CPU.
3583 *
3584 * On return from this function, the current worker would trigger
3585 * unbound chain execution of pending work items if other workers
3586 * didn't already.
Tejun Heoe22bee72010-06-29 10:07:14 +02003587 */
Tejun Heo4ce62e92012-07-13 22:16:44 -07003588 for_each_worker_pool(pool, gcwq)
3589 atomic_set(get_pool_nr_running(pool), 0);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003590}
3591
Tejun Heo8db25e72012-07-17 12:39:28 -07003592/*
3593 * Workqueues should be brought up before normal priority CPU notifiers.
3594 * This will be registered high priority CPU notifier.
3595 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07003596static int __cpuinit workqueue_cpu_up_callback(struct notifier_block *nfb,
Tejun Heo8db25e72012-07-17 12:39:28 -07003597 unsigned long action,
3598 void *hcpu)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003599{
3600 unsigned int cpu = (unsigned long)hcpu;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003601 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003602 struct worker_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603
Tejun Heo8db25e72012-07-17 12:39:28 -07003604 switch (action & ~CPU_TASKS_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 case CPU_UP_PREPARE:
Tejun Heo4ce62e92012-07-13 22:16:44 -07003606 for_each_worker_pool(pool, gcwq) {
Tejun Heo3ce63372012-07-17 12:39:27 -07003607 struct worker *worker;
3608
3609 if (pool->nr_workers)
3610 continue;
3611
3612 worker = create_worker(pool);
3613 if (!worker)
3614 return NOTIFY_BAD;
3615
3616 spin_lock_irq(&gcwq->lock);
3617 start_worker(worker);
3618 spin_unlock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003620 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003621
Tejun Heo65758202012-07-17 12:39:26 -07003622 case CPU_DOWN_FAILED:
3623 case CPU_ONLINE:
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07003624 gcwq_claim_assoc_and_lock(gcwq);
Tejun Heo24647572013-01-24 11:01:33 -08003625 for_each_worker_pool(pool, gcwq)
3626 pool->flags &= ~POOL_DISASSOCIATED;
Tejun Heo8db25e72012-07-17 12:39:28 -07003627 rebind_workers(gcwq);
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07003628 gcwq_release_assoc_and_unlock(gcwq);
Tejun Heo8db25e72012-07-17 12:39:28 -07003629 break;
Tejun Heo65758202012-07-17 12:39:26 -07003630 }
3631 return NOTIFY_OK;
3632}
3633
3634/*
3635 * Workqueues should be brought down after normal priority CPU notifiers.
3636 * This will be registered as low priority CPU notifier.
3637 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07003638static int __cpuinit workqueue_cpu_down_callback(struct notifier_block *nfb,
Tejun Heo65758202012-07-17 12:39:26 -07003639 unsigned long action,
3640 void *hcpu)
3641{
Tejun Heo8db25e72012-07-17 12:39:28 -07003642 unsigned int cpu = (unsigned long)hcpu;
3643 struct work_struct unbind_work;
3644
Tejun Heo65758202012-07-17 12:39:26 -07003645 switch (action & ~CPU_TASKS_FROZEN) {
3646 case CPU_DOWN_PREPARE:
Tejun Heo8db25e72012-07-17 12:39:28 -07003647 /* unbinding should happen on the local CPU */
3648 INIT_WORK_ONSTACK(&unbind_work, gcwq_unbind_fn);
Joonsoo Kim7635d2f2012-08-15 23:25:41 +09003649 queue_work_on(cpu, system_highpri_wq, &unbind_work);
Tejun Heo8db25e72012-07-17 12:39:28 -07003650 flush_work(&unbind_work);
3651 break;
Tejun Heo65758202012-07-17 12:39:26 -07003652 }
3653 return NOTIFY_OK;
3654}
3655
Rusty Russell2d3854a2008-11-05 13:39:10 +11003656#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003657
Rusty Russell2d3854a2008-11-05 13:39:10 +11003658struct work_for_cpu {
Tejun Heoed48ece2012-09-18 12:48:43 -07003659 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003660 long (*fn)(void *);
3661 void *arg;
3662 long ret;
3663};
3664
Tejun Heoed48ece2012-09-18 12:48:43 -07003665static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003666{
Tejun Heoed48ece2012-09-18 12:48:43 -07003667 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
3668
Rusty Russell2d3854a2008-11-05 13:39:10 +11003669 wfc->ret = wfc->fn(wfc->arg);
3670}
3671
3672/**
3673 * work_on_cpu - run a function in user context on a particular cpu
3674 * @cpu: the cpu to run on
3675 * @fn: the function to run
3676 * @arg: the function arg
3677 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003678 * This will return the value @fn returns.
3679 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06003680 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003681 */
3682long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3683{
Tejun Heoed48ece2012-09-18 12:48:43 -07003684 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003685
Tejun Heoed48ece2012-09-18 12:48:43 -07003686 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
3687 schedule_work_on(cpu, &wfc.work);
3688 flush_work(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003689 return wfc.ret;
3690}
3691EXPORT_SYMBOL_GPL(work_on_cpu);
3692#endif /* CONFIG_SMP */
3693
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003694#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303695
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003696/**
3697 * freeze_workqueues_begin - begin freezing workqueues
3698 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003699 * Start freezing workqueues. After this function returns, all freezable
3700 * workqueues will queue new works to their frozen_works list instead of
3701 * gcwq->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003702 *
3703 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003704 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003705 */
3706void freeze_workqueues_begin(void)
3707{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003708 unsigned int cpu;
3709
3710 spin_lock(&workqueue_lock);
3711
3712 BUG_ON(workqueue_freezing);
3713 workqueue_freezing = true;
3714
Tejun Heof3421792010-07-02 10:03:51 +02003715 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003716 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo35b6bb62013-01-24 11:01:33 -08003717 struct worker_pool *pool;
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003718 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003719
3720 spin_lock_irq(&gcwq->lock);
3721
Tejun Heo35b6bb62013-01-24 11:01:33 -08003722 for_each_worker_pool(pool, gcwq) {
3723 WARN_ON_ONCE(pool->flags & POOL_FREEZING);
3724 pool->flags |= POOL_FREEZING;
3725 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003726
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003727 list_for_each_entry(wq, &workqueues, list) {
3728 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3729
Tejun Heo58a69cb2011-02-16 09:25:31 +01003730 if (cwq && wq->flags & WQ_FREEZABLE)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003731 cwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003732 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003733
3734 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003735 }
3736
3737 spin_unlock(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003739
3740/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003741 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003742 *
3743 * Check whether freezing is complete. This function must be called
3744 * between freeze_workqueues_begin() and thaw_workqueues().
3745 *
3746 * CONTEXT:
3747 * Grabs and releases workqueue_lock.
3748 *
3749 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01003750 * %true if some freezable workqueues are still busy. %false if freezing
3751 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003752 */
3753bool freeze_workqueues_busy(void)
3754{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003755 unsigned int cpu;
3756 bool busy = false;
3757
3758 spin_lock(&workqueue_lock);
3759
3760 BUG_ON(!workqueue_freezing);
3761
Tejun Heof3421792010-07-02 10:03:51 +02003762 for_each_gcwq_cpu(cpu) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003763 struct workqueue_struct *wq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003764 /*
3765 * nr_active is monotonically decreasing. It's safe
3766 * to peek without lock.
3767 */
3768 list_for_each_entry(wq, &workqueues, list) {
3769 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3770
Tejun Heo58a69cb2011-02-16 09:25:31 +01003771 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003772 continue;
3773
3774 BUG_ON(cwq->nr_active < 0);
3775 if (cwq->nr_active) {
3776 busy = true;
3777 goto out_unlock;
3778 }
3779 }
3780 }
3781out_unlock:
3782 spin_unlock(&workqueue_lock);
3783 return busy;
3784}
3785
3786/**
3787 * thaw_workqueues - thaw workqueues
3788 *
3789 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo7e116292010-06-29 10:07:13 +02003790 * frozen works are transferred to their respective gcwq worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003791 *
3792 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003793 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003794 */
3795void thaw_workqueues(void)
3796{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003797 unsigned int cpu;
3798
3799 spin_lock(&workqueue_lock);
3800
3801 if (!workqueue_freezing)
3802 goto out_unlock;
3803
Tejun Heof3421792010-07-02 10:03:51 +02003804 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003805 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003806 struct worker_pool *pool;
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003807 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003808
3809 spin_lock_irq(&gcwq->lock);
3810
Tejun Heo35b6bb62013-01-24 11:01:33 -08003811 for_each_worker_pool(pool, gcwq) {
3812 WARN_ON_ONCE(!(pool->flags & POOL_FREEZING));
3813 pool->flags &= ~POOL_FREEZING;
3814 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003815
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003816 list_for_each_entry(wq, &workqueues, list) {
3817 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3818
Tejun Heo58a69cb2011-02-16 09:25:31 +01003819 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003820 continue;
3821
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003822 /* restore max_active and repopulate worklist */
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003823 cwq_set_max_active(cwq, wq->saved_max_active);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003824 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003825
Tejun Heo4ce62e92012-07-13 22:16:44 -07003826 for_each_worker_pool(pool, gcwq)
3827 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02003828
Tejun Heo8b03ae32010-06-29 10:07:12 +02003829 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003830 }
3831
3832 workqueue_freezing = false;
3833out_unlock:
3834 spin_unlock(&workqueue_lock);
3835}
3836#endif /* CONFIG_FREEZER */
3837
Suresh Siddha6ee05782010-07-30 14:57:37 -07003838static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839{
Tejun Heoc34056a2010-06-29 10:07:11 +02003840 unsigned int cpu;
3841
Tejun Heo7c3eed52013-01-24 11:01:33 -08003842 /* make sure we have enough bits for OFFQ pool ID */
3843 BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) <
3844 WORK_CPU_LAST * NR_STD_WORKER_POOLS);
Tejun Heob5490072012-08-03 10:30:46 -07003845
Tejun Heo65758202012-07-17 12:39:26 -07003846 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
Lai Jiangshana5b4e572012-09-18 09:59:23 -07003847 hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003848
3849 /* initialize gcwqs */
Tejun Heof3421792010-07-02 10:03:51 +02003850 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003851 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003852 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003853
3854 spin_lock_init(&gcwq->lock);
3855 gcwq->cpu = cpu;
3856
Sasha Levin42f85702012-12-17 10:01:23 -05003857 hash_init(gcwq->busy_hash);
Tejun Heoc8e55f32010-06-29 10:07:12 +02003858
Tejun Heo4ce62e92012-07-13 22:16:44 -07003859 for_each_worker_pool(pool, gcwq) {
3860 pool->gcwq = gcwq;
Tejun Heo24647572013-01-24 11:01:33 -08003861 pool->flags |= POOL_DISASSOCIATED;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003862 INIT_LIST_HEAD(&pool->worklist);
3863 INIT_LIST_HEAD(&pool->idle_list);
Tejun Heoe22bee72010-06-29 10:07:14 +02003864
Tejun Heo4ce62e92012-07-13 22:16:44 -07003865 init_timer_deferrable(&pool->idle_timer);
3866 pool->idle_timer.function = idle_worker_timeout;
3867 pool->idle_timer.data = (unsigned long)pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003868
Tejun Heo4ce62e92012-07-13 22:16:44 -07003869 setup_timer(&pool->mayday_timer, gcwq_mayday_timeout,
3870 (unsigned long)pool);
3871
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07003872 mutex_init(&pool->assoc_mutex);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003873 ida_init(&pool->worker_ida);
Tejun Heo9daf9e62013-01-24 11:01:33 -08003874
3875 /* alloc pool ID */
3876 BUG_ON(worker_pool_assign_id(pool));
Tejun Heo4ce62e92012-07-13 22:16:44 -07003877 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003878 }
3879
Tejun Heoe22bee72010-06-29 10:07:14 +02003880 /* create the initial worker */
Tejun Heof3421792010-07-02 10:03:51 +02003881 for_each_online_gcwq_cpu(cpu) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003882 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003883 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003884
Tejun Heo4ce62e92012-07-13 22:16:44 -07003885 for_each_worker_pool(pool, gcwq) {
3886 struct worker *worker;
3887
Tejun Heo24647572013-01-24 11:01:33 -08003888 if (cpu != WORK_CPU_UNBOUND)
3889 pool->flags &= ~POOL_DISASSOCIATED;
3890
Tejun Heobc2ae0f2012-07-17 12:39:27 -07003891 worker = create_worker(pool);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003892 BUG_ON(!worker);
3893 spin_lock_irq(&gcwq->lock);
3894 start_worker(worker);
3895 spin_unlock_irq(&gcwq->lock);
3896 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003897 }
3898
Tejun Heod320c032010-06-29 10:07:14 +02003899 system_wq = alloc_workqueue("events", 0, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09003900 system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
Tejun Heod320c032010-06-29 10:07:14 +02003901 system_long_wq = alloc_workqueue("events_long", 0, 0);
Tejun Heof3421792010-07-02 10:03:51 +02003902 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3903 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01003904 system_freezable_wq = alloc_workqueue("events_freezable",
3905 WQ_FREEZABLE, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09003906 BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
Tejun Heoae930e02012-08-20 14:51:23 -07003907 !system_unbound_wq || !system_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07003908 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909}
Suresh Siddha6ee05782010-07-30 14:57:37 -07003910early_initcall(init_workqueues);