blob: 9210601b5de60d95ff8464cb6e3c8cc683064159 [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>
Syed Rameez Mustafa1bee7b92013-07-15 11:52:09 -070044#include <linux/bug.h>
Tejun Heoe22bee72010-06-29 10:07:14 +020045
46#include "workqueue_sched.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Tejun Heoc8e55f32010-06-29 10:07:12 +020048enum {
Tejun Heodb7bccf2010-06-29 10:07:12 +020049 /* global_cwq flags */
Tejun Heo22ad5642012-07-12 14:46:37 -070050 GCWQ_DISASSOCIATED = 1 << 0, /* cpu can't serve workers */
51 GCWQ_FREEZING = 1 << 1, /* freeze in progress */
52
53 /* pool flags */
54 POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
55 POOL_MANAGING_WORKERS = 1 << 1, /* managing workers */
Tejun Heodb7bccf2010-06-29 10:07:12 +020056
Tejun Heoc8e55f32010-06-29 10:07:12 +020057 /* worker flags */
58 WORKER_STARTED = 1 << 0, /* started */
59 WORKER_DIE = 1 << 1, /* die die die */
60 WORKER_IDLE = 1 << 2, /* is idle */
Tejun Heoe22bee72010-06-29 10:07:14 +020061 WORKER_PREP = 1 << 3, /* preparing to run works */
Tejun Heodb7bccf2010-06-29 10:07:12 +020062 WORKER_ROGUE = 1 << 4, /* not bound to any cpu */
Tejun Heoe22bee72010-06-29 10:07:14 +020063 WORKER_REBIND = 1 << 5, /* mom is home, come back */
Tejun Heofb0e7be2010-06-29 10:07:15 +020064 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
Tejun Heof3421792010-07-02 10:03:51 +020065 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
Tejun Heoe22bee72010-06-29 10:07:14 +020066
Tejun Heofb0e7be2010-06-29 10:07:15 +020067 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_ROGUE | WORKER_REBIND |
Tejun Heof3421792010-07-02 10:03:51 +020068 WORKER_CPU_INTENSIVE | WORKER_UNBOUND,
Tejun Heodb7bccf2010-06-29 10:07:12 +020069
70 /* gcwq->trustee_state */
71 TRUSTEE_START = 0, /* start */
72 TRUSTEE_IN_CHARGE = 1, /* trustee in charge of gcwq */
73 TRUSTEE_BUTCHER = 2, /* butcher workers */
74 TRUSTEE_RELEASE = 3, /* release workers */
75 TRUSTEE_DONE = 4, /* trustee is done */
Tejun Heoc8e55f32010-06-29 10:07:12 +020076
Tejun Heodcb32ee2012-07-13 22:16:45 -070077 NR_WORKER_POOLS = 2, /* # worker pools per gcwq */
Tejun Heo9c6bae02012-07-13 22:16:44 -070078
Tejun Heoc8e55f32010-06-29 10:07:12 +020079 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
80 BUSY_WORKER_HASH_SIZE = 1 << BUSY_WORKER_HASH_ORDER,
81 BUSY_WORKER_HASH_MASK = BUSY_WORKER_HASH_SIZE - 1,
Tejun Heodb7bccf2010-06-29 10:07:12 +020082
Tejun Heoe22bee72010-06-29 10:07:14 +020083 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
84 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
85
Tejun Heo3233cdb2011-02-16 18:10:19 +010086 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
87 /* call for help after 10ms
88 (min two ticks) */
Tejun Heoe22bee72010-06-29 10:07:14 +020089 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
90 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heodb7bccf2010-06-29 10:07:12 +020091 TRUSTEE_COOLDOWN = HZ / 10, /* for trustee draining */
Tejun Heoe22bee72010-06-29 10:07:14 +020092
93 /*
94 * Rescue workers are used only on emergencies and shared by
95 * all cpus. Give -20.
96 */
97 RESCUER_NICE_LEVEL = -20,
Tejun Heodcb32ee2012-07-13 22:16:45 -070098 HIGHPRI_NICE_LEVEL = -20,
Tejun Heoc8e55f32010-06-29 10:07:12 +020099};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101/*
Tejun Heo4690c4a2010-06-29 10:07:10 +0200102 * Structure fields follow one of the following exclusion rules.
103 *
Tejun Heoe41e7042010-08-24 14:22:47 +0200104 * I: Modifiable by initialization/destruction paths and read-only for
105 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200106 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200107 * P: Preemption protected. Disabling preemption is enough and should
108 * only be modified and accessed from the local cpu.
109 *
Tejun Heo8b03ae32010-06-29 10:07:12 +0200110 * L: gcwq->lock protected. Access with gcwq->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200111 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200112 * X: During normal operation, modification requires gcwq->lock and
113 * should be done only from local cpu. Either disabling preemption
114 * on local cpu or grabbing gcwq->lock is enough for read access.
Tejun Heof3421792010-07-02 10:03:51 +0200115 * If GCWQ_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200116 *
Tejun Heo73f53c42010-06-29 10:07:11 +0200117 * F: wq->flush_mutex protected.
118 *
Tejun Heo4690c4a2010-06-29 10:07:10 +0200119 * W: workqueue_lock protected.
120 */
121
Tejun Heo8b03ae32010-06-29 10:07:12 +0200122struct global_cwq;
Tejun Heo58658882012-07-12 14:46:37 -0700123struct worker_pool;
Tejun Heoc34056a2010-06-29 10:07:11 +0200124
Tejun Heoe22bee72010-06-29 10:07:14 +0200125/*
126 * The poor guys doing the actual heavy lifting. All on-duty workers
127 * are either serving the manager role, on idle list or on busy hash.
128 */
Tejun Heoc34056a2010-06-29 10:07:11 +0200129struct worker {
Tejun Heoc8e55f32010-06-29 10:07:12 +0200130 /* on idle list while idle, on busy hash table while busy */
131 union {
132 struct list_head entry; /* L: while idle */
133 struct hlist_node hentry; /* L: while busy */
134 };
135
Tejun Heoc34056a2010-06-29 10:07:11 +0200136 struct work_struct *current_work; /* L: work being processed */
Tejun Heo55e3e1f2012-12-18 10:35:02 -0800137 work_func_t current_func; /* L: current_work's fn */
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200138 struct cpu_workqueue_struct *current_cwq; /* L: current_work's cwq */
Tejun Heoaffee4b2010-06-29 10:07:12 +0200139 struct list_head scheduled; /* L: scheduled works */
Tejun Heoc34056a2010-06-29 10:07:11 +0200140 struct task_struct *task; /* I: worker task */
Tejun Heo58658882012-07-12 14:46:37 -0700141 struct worker_pool *pool; /* I: the associated pool */
Tejun Heoe22bee72010-06-29 10:07:14 +0200142 /* 64 bytes boundary on 64bit, 32 on 32bit */
143 unsigned long last_active; /* L: last active timestamp */
144 unsigned int flags; /* X: flags */
Tejun Heoc34056a2010-06-29 10:07:11 +0200145 int id; /* I: worker id */
Tejun Heoe22bee72010-06-29 10:07:14 +0200146 struct work_struct rebind_work; /* L: rebind worker to cpu */
Tejun Heoc34056a2010-06-29 10:07:11 +0200147};
148
Tejun Heo58658882012-07-12 14:46:37 -0700149struct worker_pool {
150 struct global_cwq *gcwq; /* I: the owning gcwq */
Tejun Heo22ad5642012-07-12 14:46:37 -0700151 unsigned int flags; /* X: flags */
Tejun Heo58658882012-07-12 14:46:37 -0700152
153 struct list_head worklist; /* L: list of pending works */
154 int nr_workers; /* L: total number of workers */
155 int nr_idle; /* L: currently idle ones */
156
157 struct list_head idle_list; /* X: list of idle workers */
158 struct timer_list idle_timer; /* L: worker idle timeout */
159 struct timer_list mayday_timer; /* L: SOS timer for workers */
160
161 struct ida worker_ida; /* L: for worker IDs */
162 struct worker *first_idle; /* L: first idle worker */
163};
164
Tejun Heo4690c4a2010-06-29 10:07:10 +0200165/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200166 * Global per-cpu workqueue. There's one and only one for each cpu
167 * and all works are queued and processed here regardless of their
168 * target workqueues.
Tejun Heo8b03ae32010-06-29 10:07:12 +0200169 */
170struct global_cwq {
171 spinlock_t lock; /* the gcwq lock */
172 unsigned int cpu; /* I: the associated cpu */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200173 unsigned int flags; /* L: GCWQ_* flags */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200174
Tejun Heo58658882012-07-12 14:46:37 -0700175 /* workers are chained either in busy_hash or pool idle_list */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200176 struct hlist_head busy_hash[BUSY_WORKER_HASH_SIZE];
177 /* L: hash of busy workers */
178
Tejun Heodcb32ee2012-07-13 22:16:45 -0700179 struct worker_pool pools[2]; /* normal and highpri pools */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200180
181 struct task_struct *trustee; /* L: for gcwq shutdown */
182 unsigned int trustee_state; /* L: trustee state */
183 wait_queue_head_t trustee_wait; /* trustee wait */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200184} ____cacheline_aligned_in_smp;
185
186/*
Tejun Heo502ca9d2010-06-29 10:07:13 +0200187 * The per-CPU workqueue. The lower WORK_STRUCT_FLAG_BITS of
Tejun Heo0f900042010-06-29 10:07:11 +0200188 * work_struct->data are used for flags and thus cwqs need to be
189 * aligned at two's power of the number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 */
191struct cpu_workqueue_struct {
Tejun Heo58658882012-07-12 14:46:37 -0700192 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200193 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200194 int work_color; /* L: current color */
195 int flush_color; /* L: flushing color */
196 int nr_in_flight[WORK_NR_COLORS];
197 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200198 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200199 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200200 struct list_head delayed_works; /* L: delayed works */
Tejun Heo0f900042010-06-29 10:07:11 +0200201};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200204 * Structure used to wait for workqueue flush.
205 */
206struct wq_flusher {
207 struct list_head list; /* F: list of flushers */
208 int flush_color; /* F: flush color waiting for */
209 struct completion done; /* flush completion */
210};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Tejun Heo73f53c42010-06-29 10:07:11 +0200212/*
Tejun Heof2e005a2010-07-20 15:59:09 +0200213 * All cpumasks are assumed to be always set on UP and thus can't be
214 * used to determine whether there's something to be done.
215 */
216#ifdef CONFIG_SMP
217typedef cpumask_var_t mayday_mask_t;
218#define mayday_test_and_set_cpu(cpu, mask) \
219 cpumask_test_and_set_cpu((cpu), (mask))
220#define mayday_clear_cpu(cpu, mask) cpumask_clear_cpu((cpu), (mask))
221#define for_each_mayday_cpu(cpu, mask) for_each_cpu((cpu), (mask))
Tejun Heo9c375472010-08-31 11:18:34 +0200222#define alloc_mayday_mask(maskp, gfp) zalloc_cpumask_var((maskp), (gfp))
Tejun Heof2e005a2010-07-20 15:59:09 +0200223#define free_mayday_mask(mask) free_cpumask_var((mask))
224#else
225typedef unsigned long mayday_mask_t;
226#define mayday_test_and_set_cpu(cpu, mask) test_and_set_bit(0, &(mask))
227#define mayday_clear_cpu(cpu, mask) clear_bit(0, &(mask))
228#define for_each_mayday_cpu(cpu, mask) if ((cpu) = 0, (mask))
229#define alloc_mayday_mask(maskp, gfp) true
230#define free_mayday_mask(mask) do { } while (0)
231#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233/*
234 * The externally visible workqueue abstraction is an array of
235 * per-CPU workqueues:
236 */
237struct workqueue_struct {
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200238 unsigned int flags; /* W: WQ_* flags */
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200239 union {
240 struct cpu_workqueue_struct __percpu *pcpu;
241 struct cpu_workqueue_struct *single;
242 unsigned long v;
243 } cpu_wq; /* I: cwq's */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200244 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200245
246 struct mutex flush_mutex; /* protects wq flushing */
247 int work_color; /* F: current work color */
248 int flush_color; /* F: current flush color */
249 atomic_t nr_cwqs_to_flush; /* flush in progress */
250 struct wq_flusher *first_flusher; /* F: first flusher */
251 struct list_head flusher_queue; /* F: flush waiters */
252 struct list_head flusher_overflow; /* F: flush overflow list */
253
Tejun Heof2e005a2010-07-20 15:59:09 +0200254 mayday_mask_t mayday_mask; /* cpus requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200255 struct worker *rescuer; /* I: rescue worker */
256
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200257 int nr_drainers; /* W: drain in progress */
Tejun Heodcd989c2010-06-29 10:07:14 +0200258 int saved_max_active; /* W: saved cwq max_active */
Johannes Berg4e6045f2007-10-18 23:39:55 -0700259#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200260 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700261#endif
Tejun Heob196be82012-01-10 15:11:35 -0800262 char name[]; /* I: workqueue name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263};
264
Viresh Kumar154e3112013-04-08 16:45:40 +0530265/* see the comment above the definition of WQ_POWER_EFFICIENT */
266#ifdef CONFIG_WQ_POWER_EFFICIENT_DEFAULT
267static bool wq_power_efficient = true;
268#else
269static bool wq_power_efficient;
270#endif
271
Tejun Heod320c032010-06-29 10:07:14 +0200272struct workqueue_struct *system_wq __read_mostly;
273struct workqueue_struct *system_long_wq __read_mostly;
274struct workqueue_struct *system_nrt_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200275struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100276struct workqueue_struct *system_freezable_wq __read_mostly;
Alan Stern62d3c542012-03-02 10:51:00 +0100277struct workqueue_struct *system_nrt_freezable_wq __read_mostly;
Viresh Kumar1adb4b62013-04-24 17:12:54 +0530278struct workqueue_struct *system_power_efficient_wq __read_mostly;
279struct workqueue_struct *system_freezable_power_efficient_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200280EXPORT_SYMBOL_GPL(system_wq);
281EXPORT_SYMBOL_GPL(system_long_wq);
282EXPORT_SYMBOL_GPL(system_nrt_wq);
Tejun Heof3421792010-07-02 10:03:51 +0200283EXPORT_SYMBOL_GPL(system_unbound_wq);
Tejun Heo24d51ad2011-02-21 09:52:50 +0100284EXPORT_SYMBOL_GPL(system_freezable_wq);
Alan Stern62d3c542012-03-02 10:51:00 +0100285EXPORT_SYMBOL_GPL(system_nrt_freezable_wq);
Viresh Kumar1adb4b62013-04-24 17:12:54 +0530286EXPORT_SYMBOL_GPL(system_power_efficient_wq);
287EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq);
Nick Reuterb8678b32018-08-30 02:53:55 -0500288
Tejun Heo97bd2342010-10-05 10:41:14 +0200289#define CREATE_TRACE_POINTS
290#include <trace/events/workqueue.h>
291
Tejun Heo9c6bae02012-07-13 22:16:44 -0700292#define for_each_worker_pool(pool, gcwq) \
Tejun Heodcb32ee2012-07-13 22:16:45 -0700293 for ((pool) = &(gcwq)->pools[0]; \
294 (pool) < &(gcwq)->pools[NR_WORKER_POOLS]; (pool)++)
Tejun Heo9c6bae02012-07-13 22:16:44 -0700295
Tejun Heodb7bccf2010-06-29 10:07:12 +0200296#define for_each_busy_worker(worker, i, pos, gcwq) \
297 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \
298 hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry)
299
Tejun Heof3421792010-07-02 10:03:51 +0200300static inline int __next_gcwq_cpu(int cpu, const struct cpumask *mask,
301 unsigned int sw)
302{
303 if (cpu < nr_cpu_ids) {
304 if (sw & 1) {
305 cpu = cpumask_next(cpu, mask);
306 if (cpu < nr_cpu_ids)
307 return cpu;
308 }
309 if (sw & 2)
310 return WORK_CPU_UNBOUND;
311 }
312 return WORK_CPU_NONE;
313}
314
315static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
316 struct workqueue_struct *wq)
317{
318 return __next_gcwq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2);
319}
320
Tejun Heo09884952010-08-01 11:50:12 +0200321/*
322 * CPU iterators
323 *
324 * An extra gcwq is defined for an invalid cpu number
325 * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any
326 * specific CPU. The following iterators are similar to
327 * for_each_*_cpu() iterators but also considers the unbound gcwq.
328 *
329 * for_each_gcwq_cpu() : possible CPUs + WORK_CPU_UNBOUND
330 * for_each_online_gcwq_cpu() : online CPUs + WORK_CPU_UNBOUND
331 * for_each_cwq_cpu() : possible CPUs for bound workqueues,
332 * WORK_CPU_UNBOUND for unbound workqueues
333 */
Tejun Heof3421792010-07-02 10:03:51 +0200334#define for_each_gcwq_cpu(cpu) \
335 for ((cpu) = __next_gcwq_cpu(-1, cpu_possible_mask, 3); \
336 (cpu) < WORK_CPU_NONE; \
337 (cpu) = __next_gcwq_cpu((cpu), cpu_possible_mask, 3))
338
339#define for_each_online_gcwq_cpu(cpu) \
340 for ((cpu) = __next_gcwq_cpu(-1, cpu_online_mask, 3); \
341 (cpu) < WORK_CPU_NONE; \
342 (cpu) = __next_gcwq_cpu((cpu), cpu_online_mask, 3))
343
344#define for_each_cwq_cpu(cpu, wq) \
345 for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, (wq)); \
346 (cpu) < WORK_CPU_NONE; \
347 (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq)))
348
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900349#ifdef CONFIG_DEBUG_OBJECTS_WORK
350
351static struct debug_obj_descr work_debug_descr;
352
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100353static void *work_debug_hint(void *addr)
354{
355 return ((struct work_struct *) addr)->func;
356}
357
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900358/*
359 * fixup_init is called when:
360 * - an active object is initialized
361 */
362static int work_fixup_init(void *addr, enum debug_obj_state state)
363{
364 struct work_struct *work = addr;
365
366 switch (state) {
367 case ODEBUG_STATE_ACTIVE:
368 cancel_work_sync(work);
369 debug_object_init(work, &work_debug_descr);
370 return 1;
371 default:
372 return 0;
373 }
374}
375
376/*
377 * fixup_activate is called when:
378 * - an active object is activated
379 * - an unknown object is activated (might be a statically initialized object)
380 */
381static int work_fixup_activate(void *addr, enum debug_obj_state state)
382{
383 struct work_struct *work = addr;
384
385 switch (state) {
386
387 case ODEBUG_STATE_NOTAVAILABLE:
388 /*
389 * This is not really a fixup. The work struct was
390 * statically initialized. We just make sure that it
391 * is tracked in the object tracker.
392 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200393 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900394 debug_object_init(work, &work_debug_descr);
395 debug_object_activate(work, &work_debug_descr);
396 return 0;
397 }
398 WARN_ON_ONCE(1);
399 return 0;
400
401 case ODEBUG_STATE_ACTIVE:
402 WARN_ON(1);
403
404 default:
405 return 0;
406 }
407}
408
409/*
410 * fixup_free is called when:
411 * - an active object is freed
412 */
413static int work_fixup_free(void *addr, enum debug_obj_state state)
414{
415 struct work_struct *work = addr;
416
417 switch (state) {
418 case ODEBUG_STATE_ACTIVE:
419 cancel_work_sync(work);
420 debug_object_free(work, &work_debug_descr);
421 return 1;
422 default:
423 return 0;
424 }
425}
426
427static struct debug_obj_descr work_debug_descr = {
428 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100429 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900430 .fixup_init = work_fixup_init,
431 .fixup_activate = work_fixup_activate,
432 .fixup_free = work_fixup_free,
433};
434
435static inline void debug_work_activate(struct work_struct *work)
436{
437 debug_object_activate(work, &work_debug_descr);
438}
439
440static inline void debug_work_deactivate(struct work_struct *work)
441{
442 debug_object_deactivate(work, &work_debug_descr);
443}
444
445void __init_work(struct work_struct *work, int onstack)
446{
447 if (onstack)
448 debug_object_init_on_stack(work, &work_debug_descr);
449 else
450 debug_object_init(work, &work_debug_descr);
451}
452EXPORT_SYMBOL_GPL(__init_work);
453
454void destroy_work_on_stack(struct work_struct *work)
455{
456 debug_object_free(work, &work_debug_descr);
457}
458EXPORT_SYMBOL_GPL(destroy_work_on_stack);
459
460#else
461static inline void debug_work_activate(struct work_struct *work) { }
462static inline void debug_work_deactivate(struct work_struct *work) { }
463#endif
464
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100465/* Serializes the accesses to the list of workqueues. */
466static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467static LIST_HEAD(workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200468static bool workqueue_freezing; /* W: have wqs started freezing? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Oleg Nesterov14441962007-05-23 13:57:57 -0700470/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200471 * The almighty global cpu workqueues. nr_running is the only field
472 * which is expected to be used frequently by other cpus via
473 * try_to_wake_up(). Put it in a separate cacheline.
Oleg Nesterov14441962007-05-23 13:57:57 -0700474 */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200475static DEFINE_PER_CPU(struct global_cwq, global_cwq);
Tejun Heo9c6bae02012-07-13 22:16:44 -0700476static DEFINE_PER_CPU_SHARED_ALIGNED(atomic_t, pool_nr_running[NR_WORKER_POOLS]);
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800477
Tejun Heof3421792010-07-02 10:03:51 +0200478/*
479 * Global cpu workqueue and nr_running counter for unbound gcwq. The
480 * gcwq is always online, has GCWQ_DISASSOCIATED set, and all its
481 * workers have WORKER_UNBOUND set.
482 */
483static struct global_cwq unbound_global_cwq;
Tejun Heo9c6bae02012-07-13 22:16:44 -0700484static atomic_t unbound_pool_nr_running[NR_WORKER_POOLS] = {
485 [0 ... NR_WORKER_POOLS - 1] = ATOMIC_INIT(0), /* always 0 */
486};
Tejun Heof3421792010-07-02 10:03:51 +0200487
Tejun Heoc34056a2010-06-29 10:07:11 +0200488static int worker_thread(void *__worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Tejun Heodcb32ee2012-07-13 22:16:45 -0700490static int worker_pool_pri(struct worker_pool *pool)
491{
492 return pool - pool->gcwq->pools;
493}
494
Tejun Heo8b03ae32010-06-29 10:07:12 +0200495static struct global_cwq *get_gcwq(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Tejun Heof3421792010-07-02 10:03:51 +0200497 if (cpu != WORK_CPU_UNBOUND)
498 return &per_cpu(global_cwq, cpu);
499 else
500 return &unbound_global_cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
Tejun Heo7ef6a932012-07-12 14:46:37 -0700503static atomic_t *get_pool_nr_running(struct worker_pool *pool)
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700504{
Tejun Heo7ef6a932012-07-12 14:46:37 -0700505 int cpu = pool->gcwq->cpu;
Tejun Heodcb32ee2012-07-13 22:16:45 -0700506 int idx = worker_pool_pri(pool);
Tejun Heo7ef6a932012-07-12 14:46:37 -0700507
Tejun Heof3421792010-07-02 10:03:51 +0200508 if (cpu != WORK_CPU_UNBOUND)
Tejun Heo9c6bae02012-07-13 22:16:44 -0700509 return &per_cpu(pool_nr_running, cpu)[idx];
Tejun Heof3421792010-07-02 10:03:51 +0200510 else
Tejun Heo9c6bae02012-07-13 22:16:44 -0700511 return &unbound_pool_nr_running[idx];
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700512}
513
Tejun Heo4690c4a2010-06-29 10:07:10 +0200514static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
515 struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700516{
Tejun Heof3421792010-07-02 10:03:51 +0200517 if (!(wq->flags & WQ_UNBOUND)) {
Lai Jiangshane06ffa12012-03-09 18:03:20 +0800518 if (likely(cpu < nr_cpu_ids))
Tejun Heof3421792010-07-02 10:03:51 +0200519 return per_cpu_ptr(wq->cpu_wq.pcpu, cpu);
Tejun Heof3421792010-07-02 10:03:51 +0200520 } else if (likely(cpu == WORK_CPU_UNBOUND))
521 return wq->cpu_wq.single;
522 return NULL;
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700523}
524
Tejun Heo73f53c42010-06-29 10:07:11 +0200525static unsigned int work_color_to_flags(int color)
526{
527 return color << WORK_STRUCT_COLOR_SHIFT;
528}
529
530static int get_work_color(struct work_struct *work)
531{
532 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
533 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
534}
535
536static int work_next_color(int color)
537{
538 return (color + 1) % WORK_NR_COLORS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
David Howells4594bf12006-12-07 11:33:26 +0000541/*
Tejun Heoe1201532010-07-22 14:14:25 +0200542 * A work's data points to the cwq with WORK_STRUCT_CWQ set while the
543 * work is on queue. Once execution starts, WORK_STRUCT_CWQ is
544 * cleared and the work data contains the cpu number it was last on.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200545 *
546 * set_work_{cwq|cpu}() and clear_work_data() can be used to set the
547 * cwq, cpu or clear work->data. These functions should only be
548 * called while the work is owned - ie. while the PENDING bit is set.
549 *
550 * get_work_[g]cwq() can be used to obtain the gcwq or cwq
551 * corresponding to a work. gcwq is available once the work has been
552 * queued anywhere after initialization. cwq is available only from
553 * queueing until execution starts.
David Howells4594bf12006-12-07 11:33:26 +0000554 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200555static inline void set_work_data(struct work_struct *work, unsigned long data,
556 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000557{
David Howells4594bf12006-12-07 11:33:26 +0000558 BUG_ON(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200559 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000560}
David Howells365970a2006-11-22 14:54:49 +0000561
Tejun Heo7a22ad72010-06-29 10:07:13 +0200562static void set_work_cwq(struct work_struct *work,
563 struct cpu_workqueue_struct *cwq,
564 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200565{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200566 set_work_data(work, (unsigned long)cwq,
Tejun Heoe1201532010-07-22 14:14:25 +0200567 WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200568}
569
Tejun Heo7a22ad72010-06-29 10:07:13 +0200570static void set_work_cpu(struct work_struct *work, unsigned int cpu)
David Howells365970a2006-11-22 14:54:49 +0000571{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200572 set_work_data(work, cpu << WORK_STRUCT_FLAG_BITS, WORK_STRUCT_PENDING);
573}
574
575static void clear_work_data(struct work_struct *work)
576{
577 set_work_data(work, WORK_STRUCT_NO_CPU, 0);
578}
579
Tejun Heo7a22ad72010-06-29 10:07:13 +0200580static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work)
581{
Tejun Heoe1201532010-07-22 14:14:25 +0200582 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200583
Tejun Heoe1201532010-07-22 14:14:25 +0200584 if (data & WORK_STRUCT_CWQ)
585 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
586 else
Srinivasarao Pb6e586c2013-09-18 14:33:45 +0530587 {
588 WARN_ON_ONCE(1);
Tejun Heoe1201532010-07-22 14:14:25 +0200589 return NULL;
Srinivasarao Pb6e586c2013-09-18 14:33:45 +0530590 }
Tejun Heo7a22ad72010-06-29 10:07:13 +0200591}
592
593static struct global_cwq *get_work_gcwq(struct work_struct *work)
594{
Tejun Heoe1201532010-07-22 14:14:25 +0200595 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200596 unsigned int cpu;
597
Tejun Heoe1201532010-07-22 14:14:25 +0200598 if (data & WORK_STRUCT_CWQ)
599 return ((struct cpu_workqueue_struct *)
Tejun Heo58658882012-07-12 14:46:37 -0700600 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->gcwq;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200601
602 cpu = data >> WORK_STRUCT_FLAG_BITS;
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200603 if (cpu == WORK_CPU_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200604 return NULL;
605
Tejun Heof3421792010-07-02 10:03:51 +0200606 BUG_ON(cpu >= nr_cpu_ids && cpu != WORK_CPU_UNBOUND);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200607 return get_gcwq(cpu);
David Howells365970a2006-11-22 14:54:49 +0000608}
609
610/*
Tejun Heodcb32ee2012-07-13 22:16:45 -0700611 * Policy functions. These define the policies on how the global worker
612 * pools are managed. Unless noted otherwise, these functions assume that
613 * they're being called with gcwq->lock held.
David Howells365970a2006-11-22 14:54:49 +0000614 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200615
Tejun Heo7ef6a932012-07-12 14:46:37 -0700616static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000617{
Tejun Heodcb32ee2012-07-13 22:16:45 -0700618 return !atomic_read(get_pool_nr_running(pool));
David Howells365970a2006-11-22 14:54:49 +0000619}
620
Tejun Heoe22bee72010-06-29 10:07:14 +0200621/*
622 * Need to wake up a worker? Called from anything but currently
623 * running workers.
Tejun Heob7b5c682012-07-12 14:46:37 -0700624 *
625 * Note that, because unbound workers never contribute to nr_running, this
626 * function will always return %true for unbound gcwq as long as the
627 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200628 */
Tejun Heo7ef6a932012-07-12 14:46:37 -0700629static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000630{
Tejun Heo7ef6a932012-07-12 14:46:37 -0700631 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000632}
633
Tejun Heoe22bee72010-06-29 10:07:14 +0200634/* Can I start working? Called from busy but !running workers. */
Tejun Heo7ef6a932012-07-12 14:46:37 -0700635static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200636{
Tejun Heo7ef6a932012-07-12 14:46:37 -0700637 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200638}
639
640/* Do I need to keep working? Called from currently running workers. */
Tejun Heo7ef6a932012-07-12 14:46:37 -0700641static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200642{
Tejun Heo7ef6a932012-07-12 14:46:37 -0700643 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200644
Tejun Heodcb32ee2012-07-13 22:16:45 -0700645 return !list_empty(&pool->worklist) && atomic_read(nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200646}
647
648/* Do we need a new worker? Called from manager. */
Tejun Heo7ef6a932012-07-12 14:46:37 -0700649static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200650{
Tejun Heo7ef6a932012-07-12 14:46:37 -0700651 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200652}
653
654/* Do I need to be the manager? */
Tejun Heo7ef6a932012-07-12 14:46:37 -0700655static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200656{
Tejun Heo7ef6a932012-07-12 14:46:37 -0700657 return need_to_create_worker(pool) ||
Tejun Heo22ad5642012-07-12 14:46:37 -0700658 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200659}
660
661/* Do we have too many workers and should some go away? */
Tejun Heo7ef6a932012-07-12 14:46:37 -0700662static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200663{
Tejun Heo22ad5642012-07-12 14:46:37 -0700664 bool managing = pool->flags & POOL_MANAGING_WORKERS;
Tejun Heo7ef6a932012-07-12 14:46:37 -0700665 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
666 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200667
668 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
669}
670
671/*
672 * Wake up functions.
673 */
674
Tejun Heo7e116292010-06-29 10:07:13 +0200675/* Return the first worker. Safe with preemption disabled */
Tejun Heo7ef6a932012-07-12 14:46:37 -0700676static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200677{
Tejun Heo7ef6a932012-07-12 14:46:37 -0700678 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200679 return NULL;
680
Tejun Heo7ef6a932012-07-12 14:46:37 -0700681 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200682}
683
684/**
685 * wake_up_worker - wake up an idle worker
Tejun Heo7ef6a932012-07-12 14:46:37 -0700686 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200687 *
Tejun Heo7ef6a932012-07-12 14:46:37 -0700688 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200689 *
690 * CONTEXT:
691 * spin_lock_irq(gcwq->lock).
692 */
Tejun Heo7ef6a932012-07-12 14:46:37 -0700693static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200694{
Tejun Heo7ef6a932012-07-12 14:46:37 -0700695 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200696
697 if (likely(worker))
698 wake_up_process(worker->task);
699}
700
Tejun Heo4690c4a2010-06-29 10:07:10 +0200701/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200702 * wq_worker_waking_up - a worker is waking up
703 * @task: task waking up
704 * @cpu: CPU @task is waking up to
705 *
706 * This function is called during try_to_wake_up() when a worker is
707 * being awoken.
708 *
709 * CONTEXT:
710 * spin_lock_irq(rq->lock)
711 */
712void wq_worker_waking_up(struct task_struct *task, unsigned int cpu)
713{
714 struct worker *worker = kthread_data(task);
715
Steven Rostedt2d646722010-12-03 23:12:33 -0500716 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heo7ef6a932012-07-12 14:46:37 -0700717 atomic_inc(get_pool_nr_running(worker->pool));
Tejun Heoe22bee72010-06-29 10:07:14 +0200718}
719
720/**
721 * wq_worker_sleeping - a worker is going to sleep
722 * @task: task going to sleep
723 * @cpu: CPU in question, must be the current CPU number
724 *
725 * This function is called during schedule() when a busy worker is
726 * going to sleep. Worker on the same cpu can be woken up by
727 * returning pointer to its task.
728 *
729 * CONTEXT:
730 * spin_lock_irq(rq->lock)
731 *
732 * RETURNS:
733 * Worker task on @cpu to wake up, %NULL if none.
734 */
735struct task_struct *wq_worker_sleeping(struct task_struct *task,
736 unsigned int cpu)
737{
738 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heo58658882012-07-12 14:46:37 -0700739 struct worker_pool *pool = worker->pool;
Tejun Heo7ef6a932012-07-12 14:46:37 -0700740 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200741
Steven Rostedt2d646722010-12-03 23:12:33 -0500742 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200743 return NULL;
744
745 /* this can only happen on the local cpu */
746 BUG_ON(cpu != raw_smp_processor_id());
747
748 /*
749 * The counterpart of the following dec_and_test, implied mb,
750 * worklist not empty test sequence is in insert_work().
751 * Please read comment there.
752 *
753 * NOT_RUNNING is clear. This means that trustee is not in
754 * charge and we're running on the local cpu w/ rq lock held
755 * and preemption disabled, which in turn means that none else
756 * could be manipulating idle_list, so dereferencing idle_list
757 * without gcwq lock is safe.
758 */
Tejun Heo58658882012-07-12 14:46:37 -0700759 if (atomic_dec_and_test(nr_running) && !list_empty(&pool->worklist))
Tejun Heo7ef6a932012-07-12 14:46:37 -0700760 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200761 return to_wakeup ? to_wakeup->task : NULL;
762}
763
764/**
765 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200766 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200767 * @flags: flags to set
768 * @wakeup: wakeup an idle worker if necessary
769 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200770 * Set @flags in @worker->flags and adjust nr_running accordingly. If
771 * nr_running becomes zero and @wakeup is %true, an idle worker is
772 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200773 *
Tejun Heocb444762010-07-02 10:03:50 +0200774 * CONTEXT:
775 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200776 */
777static inline void worker_set_flags(struct worker *worker, unsigned int flags,
778 bool wakeup)
779{
Tejun Heo58658882012-07-12 14:46:37 -0700780 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200781
Tejun Heocb444762010-07-02 10:03:50 +0200782 WARN_ON_ONCE(worker->task != current);
783
Tejun Heoe22bee72010-06-29 10:07:14 +0200784 /*
785 * If transitioning into NOT_RUNNING, adjust nr_running and
786 * wake up an idle worker as necessary if requested by
787 * @wakeup.
788 */
789 if ((flags & WORKER_NOT_RUNNING) &&
790 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heo7ef6a932012-07-12 14:46:37 -0700791 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200792
793 if (wakeup) {
794 if (atomic_dec_and_test(nr_running) &&
Tejun Heo58658882012-07-12 14:46:37 -0700795 !list_empty(&pool->worklist))
Tejun Heo7ef6a932012-07-12 14:46:37 -0700796 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200797 } else
798 atomic_dec(nr_running);
799 }
800
Tejun Heod302f012010-06-29 10:07:13 +0200801 worker->flags |= flags;
802}
803
804/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200805 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200806 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200807 * @flags: flags to clear
808 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200809 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200810 *
Tejun Heocb444762010-07-02 10:03:50 +0200811 * CONTEXT:
812 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200813 */
814static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
815{
Tejun Heo7ef6a932012-07-12 14:46:37 -0700816 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200817 unsigned int oflags = worker->flags;
818
Tejun Heocb444762010-07-02 10:03:50 +0200819 WARN_ON_ONCE(worker->task != current);
820
Tejun Heod302f012010-06-29 10:07:13 +0200821 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200822
Tejun Heo42c025f2011-01-11 15:58:49 +0100823 /*
824 * If transitioning out of NOT_RUNNING, increment nr_running. Note
825 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
826 * of multiple flags, not a single flag.
827 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200828 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
829 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heo7ef6a932012-07-12 14:46:37 -0700830 atomic_inc(get_pool_nr_running(pool));
Tejun Heod302f012010-06-29 10:07:13 +0200831}
832
833/**
Tejun Heoc8e55f32010-06-29 10:07:12 +0200834 * busy_worker_head - return the busy hash head for a work
835 * @gcwq: gcwq of interest
836 * @work: work to be hashed
837 *
838 * Return hash head of @gcwq for @work.
839 *
840 * CONTEXT:
841 * spin_lock_irq(gcwq->lock).
842 *
843 * RETURNS:
844 * Pointer to the hash head.
845 */
846static struct hlist_head *busy_worker_head(struct global_cwq *gcwq,
847 struct work_struct *work)
848{
849 const int base_shift = ilog2(sizeof(struct work_struct));
850 unsigned long v = (unsigned long)work;
851
852 /* simple shift and fold hash, do we need something better? */
853 v >>= base_shift;
854 v += v >> BUSY_WORKER_HASH_ORDER;
855 v &= BUSY_WORKER_HASH_MASK;
856
857 return &gcwq->busy_hash[v];
858}
859
860/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200861 * __find_worker_executing_work - find worker which is executing a work
862 * @gcwq: gcwq of interest
863 * @bwh: hash head as returned by busy_worker_head()
864 * @work: work to find worker for
865 *
866 * Find a worker which is executing @work on @gcwq. @bwh should be
867 * the hash head obtained by calling busy_worker_head() with the same
868 * work.
869 *
870 * CONTEXT:
871 * spin_lock_irq(gcwq->lock).
872 *
873 * RETURNS:
874 * Pointer to worker which is executing @work if found, NULL
875 * otherwise.
876 */
877static struct worker *__find_worker_executing_work(struct global_cwq *gcwq,
878 struct hlist_head *bwh,
879 struct work_struct *work)
880{
881 struct worker *worker;
882 struct hlist_node *tmp;
883
884 hlist_for_each_entry(worker, tmp, bwh, hentry)
Tejun Heo55e3e1f2012-12-18 10:35:02 -0800885 if (worker->current_work == work &&
886 worker->current_func == work->func)
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200887 return worker;
888 return NULL;
889}
890
891/**
892 * find_worker_executing_work - find worker which is executing a work
893 * @gcwq: gcwq of interest
894 * @work: work to find worker for
895 *
Tejun Heo55e3e1f2012-12-18 10:35:02 -0800896 * Find a worker which is executing @work on @gcwq by searching
897 * @gcwq->busy_hash which is keyed by the address of @work. For a worker
898 * to match, its current execution should match the address of @work and
899 * its work function. This is to avoid unwanted dependency between
900 * unrelated work executions through a work item being recycled while still
901 * being executed.
902 *
903 * This is a bit tricky. A work item may be freed once its execution
904 * starts and nothing prevents the freed area from being recycled for
905 * another work item. If the same work item address ends up being reused
906 * before the original execution finishes, workqueue will identify the
907 * recycled work item as currently executing and make it wait until the
908 * current execution finishes, introducing an unwanted dependency.
909 *
910 * This function checks the work item address, work function and workqueue
911 * to avoid false positives. Note that this isn't complete as one may
912 * construct a work function which can introduce dependency onto itself
913 * through a recycled work item. Well, if somebody wants to shoot oneself
914 * in the foot that badly, there's only so much we can do, and if such
915 * deadlock actually occurs, it should be easy to locate the culprit work
916 * function.
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200917 *
918 * CONTEXT:
919 * spin_lock_irq(gcwq->lock).
920 *
921 * RETURNS:
922 * Pointer to worker which is executing @work if found, NULL
923 * otherwise.
924 */
925static struct worker *find_worker_executing_work(struct global_cwq *gcwq,
926 struct work_struct *work)
927{
928 return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work),
929 work);
930}
931
932/**
Tejun Heo7e116292010-06-29 10:07:13 +0200933 * insert_work - insert a work into gcwq
Tejun Heo4690c4a2010-06-29 10:07:10 +0200934 * @cwq: cwq @work belongs to
935 * @work: work to insert
936 * @head: insertion point
937 * @extra_flags: extra WORK_STRUCT_* flags to set
938 *
Tejun Heo7e116292010-06-29 10:07:13 +0200939 * Insert @work which belongs to @cwq into @gcwq after @head.
940 * @extra_flags is or'd to work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200941 *
942 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +0200943 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +0200944 */
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700945static void insert_work(struct cpu_workqueue_struct *cwq,
Tejun Heo4690c4a2010-06-29 10:07:10 +0200946 struct work_struct *work, struct list_head *head,
947 unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700948{
Tejun Heo7ef6a932012-07-12 14:46:37 -0700949 struct worker_pool *pool = cwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100950
Tejun Heo4690c4a2010-06-29 10:07:10 +0200951 /* we own @work, set data and link */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200952 set_work_cwq(work, cwq, extra_flags);
Tejun Heo4690c4a2010-06-29 10:07:10 +0200953
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700954 /*
955 * Ensure that we get the right work->data if we see the
956 * result of list_add() below, see try_to_grab_pending().
957 */
958 smp_wmb();
Tejun Heo4690c4a2010-06-29 10:07:10 +0200959
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700960 list_add_tail(&work->entry, head);
Tejun Heoe22bee72010-06-29 10:07:14 +0200961
962 /*
963 * Ensure either worker_sched_deactivated() sees the above
964 * list_add_tail() or we see zero nr_running to avoid workers
965 * lying around lazily while there are works to be processed.
966 */
967 smp_mb();
968
Tejun Heo7ef6a932012-07-12 14:46:37 -0700969 if (__need_more_worker(pool))
970 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700971}
972
Tejun Heoc8efcc22010-12-20 19:32:04 +0100973/*
974 * Test whether @work is being queued from another work executing on the
975 * same workqueue. This is rather expensive and should only be used from
976 * cold paths.
977 */
978static bool is_chained_work(struct workqueue_struct *wq)
979{
980 unsigned long flags;
981 unsigned int cpu;
982
983 for_each_gcwq_cpu(cpu) {
984 struct global_cwq *gcwq = get_gcwq(cpu);
985 struct worker *worker;
986 struct hlist_node *pos;
987 int i;
988
989 spin_lock_irqsave(&gcwq->lock, flags);
990 for_each_busy_worker(worker, i, pos, gcwq) {
991 if (worker->task != current)
992 continue;
993 spin_unlock_irqrestore(&gcwq->lock, flags);
994 /*
995 * I'm @worker, no locking necessary. See if @work
996 * is headed to the same workqueue.
997 */
998 return worker->current_cwq->wq == wq;
999 }
1000 spin_unlock_irqrestore(&gcwq->lock, flags);
1001 }
1002 return false;
1003}
1004
Tejun Heo4690c4a2010-06-29 10:07:10 +02001005static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 struct work_struct *work)
1007{
Tejun Heo502ca9d2010-06-29 10:07:13 +02001008 struct global_cwq *gcwq;
1009 struct cpu_workqueue_struct *cwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001010 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001011 unsigned int work_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 unsigned long flags;
1013
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001014 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001015
Tejun Heoc8efcc22010-12-20 19:32:04 +01001016 /* if dying, only works from the same workqueue are allowed */
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02001017 if (unlikely(wq->flags & WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001018 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001019 return;
1020
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001021 /* determine gcwq to use */
1022 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001023 struct global_cwq *last_gcwq;
1024
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001025 if (unlikely(cpu == WORK_CPU_UNBOUND))
1026 cpu = raw_smp_processor_id();
1027
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001028 /*
1029 * It's multi cpu. If @wq is non-reentrant and @work
1030 * was previously on a different cpu, it might still
1031 * be running there, in which case the work needs to
1032 * be queued on that cpu to guarantee non-reentrance.
1033 */
Tejun Heo502ca9d2010-06-29 10:07:13 +02001034 gcwq = get_gcwq(cpu);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001035 if (wq->flags & WQ_NON_REENTRANT &&
1036 (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) {
1037 struct worker *worker;
1038
1039 spin_lock_irqsave(&last_gcwq->lock, flags);
1040
1041 worker = find_worker_executing_work(last_gcwq, work);
1042
1043 if (worker && worker->current_cwq->wq == wq)
1044 gcwq = last_gcwq;
1045 else {
1046 /* meh... not running there, queue here */
1047 spin_unlock_irqrestore(&last_gcwq->lock, flags);
1048 spin_lock_irqsave(&gcwq->lock, flags);
1049 }
1050 } else
1051 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heof3421792010-07-02 10:03:51 +02001052 } else {
1053 gcwq = get_gcwq(WORK_CPU_UNBOUND);
1054 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001055 }
1056
1057 /* gcwq determined, get cwq and queue */
1058 cwq = get_cwq(gcwq->cpu, wq);
Tejun Heocdadf002010-10-05 10:49:55 +02001059 trace_workqueue_queue_work(cpu, cwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001060
Tejun Heo4690c4a2010-06-29 10:07:10 +02001061 BUG_ON(!list_empty(&work->entry));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001062
Tejun Heo73f53c42010-06-29 10:07:11 +02001063 cwq->nr_in_flight[cwq->work_color]++;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001064 work_flags = work_color_to_flags(cwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001065
1066 if (likely(cwq->nr_active < cwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001067 trace_workqueue_activate_work(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001068 cwq->nr_active++;
Tejun Heodcb32ee2012-07-13 22:16:45 -07001069 worklist = &cwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001070 } else {
1071 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001072 worklist = &cwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001073 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001074
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001075 insert_work(cwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001076
Tejun Heo8b03ae32010-06-29 10:07:12 +02001077 spin_unlock_irqrestore(&gcwq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
1079
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001080/**
1081 * queue_work - queue work on a workqueue
1082 * @wq: workqueue to use
1083 * @work: work to queue
1084 *
Alan Stern057647f2006-10-28 10:38:58 -07001085 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 *
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07001087 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1088 * it can be processed by another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001090int queue_work(struct workqueue_struct *wq, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001092 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001094 ret = queue_work_on(get_cpu(), wq, work);
1095 put_cpu();
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return ret;
1098}
Dave Jonesae90dd52006-06-30 01:40:45 -04001099EXPORT_SYMBOL_GPL(queue_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Zhang Ruic1a220e2008-07-23 21:28:39 -07001101/**
1102 * queue_work_on - queue work on specific cpu
1103 * @cpu: CPU number to execute work on
1104 * @wq: workqueue to use
1105 * @work: work to queue
1106 *
1107 * Returns 0 if @work was already on a queue, non-zero otherwise.
1108 *
1109 * We queue the work to a specific CPU, the caller must ensure it
1110 * can't go away.
1111 */
1112int
1113queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
1114{
1115 int ret = 0;
1116
Tejun Heo22df02b2010-06-29 10:07:10 +02001117 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001118 __queue_work(cpu, wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001119 ret = 1;
1120 }
1121 return ret;
1122}
1123EXPORT_SYMBOL_GPL(queue_work_on);
1124
Li Zefan6d141c32008-02-08 04:21:09 -08001125static void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
David Howells52bad642006-11-22 14:54:01 +00001127 struct delayed_work *dwork = (struct delayed_work *)__data;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001128 struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Srinivasarao Pb6e586c2013-09-18 14:33:45 +05301130 if (cwq != NULL)
1131 __queue_work(smp_processor_id(), cwq->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132}
1133
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001134/**
1135 * queue_delayed_work - queue work on a workqueue after delay
1136 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001137 * @dwork: delayable work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001138 * @delay: number of jiffies to wait before queueing
1139 *
Alan Stern057647f2006-10-28 10:38:58 -07001140 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001141 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001142int queue_delayed_work(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001143 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
David Howells52bad642006-11-22 14:54:01 +00001145 if (delay == 0)
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001146 return queue_work(wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001148 return queue_delayed_work_on(-1, wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
Dave Jonesae90dd52006-06-30 01:40:45 -04001150EXPORT_SYMBOL_GPL(queue_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001152/**
1153 * queue_delayed_work_on - queue work on specific CPU after delay
1154 * @cpu: CPU number to execute work on
1155 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001156 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001157 * @delay: number of jiffies to wait before queueing
1158 *
Alan Stern057647f2006-10-28 10:38:58 -07001159 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001160 */
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001161int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001162 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001163{
1164 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +00001165 struct timer_list *timer = &dwork->timer;
1166 struct work_struct *work = &dwork->work;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001167
Tejun Heo22df02b2010-06-29 10:07:10 +02001168 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001169 unsigned int lcpu;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001170
Tejun Heo4afca922012-12-04 07:40:39 -08001171 WARN_ON_ONCE(timer_pending(timer));
1172 WARN_ON_ONCE(!list_empty(&work->entry));
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001173
Andrew Liu8a3e77c2008-05-01 04:35:14 -07001174 timer_stats_timer_set_start_info(&dwork->timer);
1175
Tejun Heo7a22ad72010-06-29 10:07:13 +02001176 /*
1177 * This stores cwq for the moment, for the timer_fn.
1178 * Note that the work's gcwq is preserved to allow
1179 * reentrance detection for delayed works.
1180 */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001181 if (!(wq->flags & WQ_UNBOUND)) {
1182 struct global_cwq *gcwq = get_work_gcwq(work);
1183
1184 if (gcwq && gcwq->cpu != WORK_CPU_UNBOUND)
1185 lcpu = gcwq->cpu;
1186 else
1187 lcpu = raw_smp_processor_id();
1188 } else
1189 lcpu = WORK_CPU_UNBOUND;
1190
Tejun Heo7a22ad72010-06-29 10:07:13 +02001191 set_work_cwq(work, get_cwq(lcpu, wq), 0);
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001192
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001193 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +00001194 timer->data = (unsigned long)dwork;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001195 timer->function = delayed_work_timer_fn;
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001196
1197 if (unlikely(cpu >= 0))
1198 add_timer_on(timer, cpu);
1199 else
1200 add_timer(timer);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001201 ret = 1;
1202 }
1203 return ret;
1204}
Dave Jonesae90dd52006-06-30 01:40:45 -04001205EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Tejun Heoc8e55f32010-06-29 10:07:12 +02001207/**
1208 * worker_enter_idle - enter idle state
1209 * @worker: worker which is entering idle state
1210 *
1211 * @worker is entering idle state. Update stats and idle timer if
1212 * necessary.
1213 *
1214 * LOCKING:
1215 * spin_lock_irq(gcwq->lock).
1216 */
1217static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
Tejun Heo58658882012-07-12 14:46:37 -07001219 struct worker_pool *pool = worker->pool;
1220 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Tejun Heoc8e55f32010-06-29 10:07:12 +02001222 BUG_ON(worker->flags & WORKER_IDLE);
1223 BUG_ON(!list_empty(&worker->entry) &&
1224 (worker->hentry.next || worker->hentry.pprev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Tejun Heocb444762010-07-02 10:03:50 +02001226 /* can't use worker_set_flags(), also called from start_worker() */
1227 worker->flags |= WORKER_IDLE;
Tejun Heo58658882012-07-12 14:46:37 -07001228 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001229 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001230
Tejun Heoc8e55f32010-06-29 10:07:12 +02001231 /* idle_list is LIFO */
Tejun Heo58658882012-07-12 14:46:37 -07001232 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001233
Tejun Heoe22bee72010-06-29 10:07:14 +02001234 if (likely(!(worker->flags & WORKER_ROGUE))) {
Tejun Heo7ef6a932012-07-12 14:46:37 -07001235 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
Tejun Heo58658882012-07-12 14:46:37 -07001236 mod_timer(&pool->idle_timer,
Tejun Heoe22bee72010-06-29 10:07:14 +02001237 jiffies + IDLE_WORKER_TIMEOUT);
1238 } else
Tejun Heodb7bccf2010-06-29 10:07:12 +02001239 wake_up_all(&gcwq->trustee_wait);
Tejun Heocb444762010-07-02 10:03:50 +02001240
Tejun Heo24312d32012-05-14 15:04:50 -07001241 /*
1242 * Sanity check nr_running. Because trustee releases gcwq->lock
1243 * between setting %WORKER_ROGUE and zapping nr_running, the
1244 * warning may trigger spuriously. Check iff trustee is idle.
1245 */
1246 WARN_ON_ONCE(gcwq->trustee_state == TRUSTEE_DONE &&
Tejun Heo58658882012-07-12 14:46:37 -07001247 pool->nr_workers == pool->nr_idle &&
Tejun Heo7ef6a932012-07-12 14:46:37 -07001248 atomic_read(get_pool_nr_running(pool)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
Tejun Heoc8e55f32010-06-29 10:07:12 +02001251/**
1252 * worker_leave_idle - leave idle state
1253 * @worker: worker which is leaving idle state
1254 *
1255 * @worker is leaving idle state. Update stats.
1256 *
1257 * LOCKING:
1258 * spin_lock_irq(gcwq->lock).
1259 */
1260static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
Tejun Heo58658882012-07-12 14:46:37 -07001262 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Tejun Heoc8e55f32010-06-29 10:07:12 +02001264 BUG_ON(!(worker->flags & WORKER_IDLE));
Tejun Heod302f012010-06-29 10:07:13 +02001265 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heo58658882012-07-12 14:46:37 -07001266 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001267 list_del_init(&worker->entry);
1268}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Tejun Heoe22bee72010-06-29 10:07:14 +02001270/**
1271 * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq
1272 * @worker: self
1273 *
1274 * Works which are scheduled while the cpu is online must at least be
1275 * scheduled to a worker which is bound to the cpu so that if they are
1276 * flushed from cpu callbacks while cpu is going down, they are
1277 * guaranteed to execute on the cpu.
1278 *
1279 * This function is to be used by rogue workers and rescuers to bind
1280 * themselves to the target cpu and may race with cpu going down or
1281 * coming online. kthread_bind() can't be used because it may put the
1282 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1283 * verbatim as it's best effort and blocking and gcwq may be
1284 * [dis]associated in the meantime.
1285 *
1286 * This function tries set_cpus_allowed() and locks gcwq and verifies
1287 * the binding against GCWQ_DISASSOCIATED which is set during
1288 * CPU_DYING and cleared during CPU_ONLINE, so if the worker enters
1289 * idle state or fetches works without dropping lock, it can guarantee
1290 * the scheduling requirement described in the first paragraph.
1291 *
1292 * CONTEXT:
1293 * Might sleep. Called without any lock but returns with gcwq->lock
1294 * held.
1295 *
1296 * RETURNS:
1297 * %true if the associated gcwq is online (@worker is successfully
1298 * bound), %false if offline.
1299 */
1300static bool worker_maybe_bind_and_lock(struct worker *worker)
Namhyung Kim972fa1c2010-08-22 23:19:43 +09001301__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001302{
Tejun Heo58658882012-07-12 14:46:37 -07001303 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001304 struct task_struct *task = worker->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Tejun Heoe22bee72010-06-29 10:07:14 +02001306 while (true) {
1307 /*
1308 * The following call may fail, succeed or succeed
1309 * without actually migrating the task to the cpu if
1310 * it races with cpu hotunplug operation. Verify
1311 * against GCWQ_DISASSOCIATED.
1312 */
Tejun Heof3421792010-07-02 10:03:51 +02001313 if (!(gcwq->flags & GCWQ_DISASSOCIATED))
1314 set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu));
Oleg Nesterov85f41862007-05-09 02:34:20 -07001315
Tejun Heoe22bee72010-06-29 10:07:14 +02001316 spin_lock_irq(&gcwq->lock);
1317 if (gcwq->flags & GCWQ_DISASSOCIATED)
1318 return false;
1319 if (task_cpu(task) == gcwq->cpu &&
1320 cpumask_equal(&current->cpus_allowed,
1321 get_cpu_mask(gcwq->cpu)))
1322 return true;
1323 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001324
Tejun Heo5035b202011-04-29 18:08:37 +02001325 /*
1326 * We've raced with CPU hot[un]plug. Give it a breather
1327 * and retry migration. cond_resched() is required here;
1328 * otherwise, we might deadlock against cpu_stop trying to
1329 * bring down the CPU on non-preemptive kernel.
1330 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001331 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001332 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001333 }
1334}
1335
1336/*
1337 * Function for worker->rebind_work used to rebind rogue busy workers
1338 * to the associated cpu which is coming back online. This is
1339 * scheduled by cpu up but can race with other cpu hotplug operations
1340 * and may be executed twice without intervening cpu down.
1341 */
1342static void worker_rebind_fn(struct work_struct *work)
1343{
1344 struct worker *worker = container_of(work, struct worker, rebind_work);
Tejun Heo58658882012-07-12 14:46:37 -07001345 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001346
1347 if (worker_maybe_bind_and_lock(worker))
1348 worker_clr_flags(worker, WORKER_REBIND);
1349
1350 spin_unlock_irq(&gcwq->lock);
1351}
1352
Tejun Heoc34056a2010-06-29 10:07:11 +02001353static struct worker *alloc_worker(void)
1354{
1355 struct worker *worker;
1356
1357 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001358 if (worker) {
1359 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001360 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heoe22bee72010-06-29 10:07:14 +02001361 INIT_WORK(&worker->rebind_work, worker_rebind_fn);
1362 /* on creation a worker is in !idle && prep state */
1363 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001364 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001365 return worker;
1366}
1367
1368/**
1369 * create_worker - create a new workqueue worker
Tejun Heo7ef6a932012-07-12 14:46:37 -07001370 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001371 * @bind: whether to set affinity to @cpu or not
1372 *
Tejun Heo7ef6a932012-07-12 14:46:37 -07001373 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001374 * can be started by calling start_worker() or destroyed using
1375 * destroy_worker().
1376 *
1377 * CONTEXT:
1378 * Might sleep. Does GFP_KERNEL allocations.
1379 *
1380 * RETURNS:
1381 * Pointer to the newly created worker.
1382 */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001383static struct worker *create_worker(struct worker_pool *pool, bool bind)
Tejun Heoc34056a2010-06-29 10:07:11 +02001384{
Tejun Heo7ef6a932012-07-12 14:46:37 -07001385 struct global_cwq *gcwq = pool->gcwq;
Tejun Heof3421792010-07-02 10:03:51 +02001386 bool on_unbound_cpu = gcwq->cpu == WORK_CPU_UNBOUND;
Tejun Heodcb32ee2012-07-13 22:16:45 -07001387 const char *pri = worker_pool_pri(pool) ? "H" : "";
Tejun Heoc34056a2010-06-29 10:07:11 +02001388 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001389 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001390
Tejun Heo8b03ae32010-06-29 10:07:12 +02001391 spin_lock_irq(&gcwq->lock);
Tejun Heo58658882012-07-12 14:46:37 -07001392 while (ida_get_new(&pool->worker_ida, &id)) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001393 spin_unlock_irq(&gcwq->lock);
Tejun Heo58658882012-07-12 14:46:37 -07001394 if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001395 goto fail;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001396 spin_lock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001397 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02001398 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001399
1400 worker = alloc_worker();
1401 if (!worker)
1402 goto fail;
1403
Tejun Heo58658882012-07-12 14:46:37 -07001404 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001405 worker->id = id;
1406
Tejun Heof3421792010-07-02 10:03:51 +02001407 if (!on_unbound_cpu)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001408 worker->task = kthread_create_on_node(worker_thread,
Tejun Heodcb32ee2012-07-13 22:16:45 -07001409 worker, cpu_to_node(gcwq->cpu),
1410 "kworker/%u:%d%s", gcwq->cpu, id, pri);
Tejun Heof3421792010-07-02 10:03:51 +02001411 else
1412 worker->task = kthread_create(worker_thread, worker,
Tejun Heodcb32ee2012-07-13 22:16:45 -07001413 "kworker/u:%d%s", id, pri);
Tejun Heoc34056a2010-06-29 10:07:11 +02001414 if (IS_ERR(worker->task))
1415 goto fail;
1416
Tejun Heodcb32ee2012-07-13 22:16:45 -07001417 if (worker_pool_pri(pool))
1418 set_user_nice(worker->task, HIGHPRI_NICE_LEVEL);
1419
Tejun Heodb7bccf2010-06-29 10:07:12 +02001420 /*
1421 * A rogue worker will become a regular one if CPU comes
1422 * online later on. Make sure every worker has
1423 * PF_THREAD_BOUND set.
1424 */
Tejun Heof3421792010-07-02 10:03:51 +02001425 if (bind && !on_unbound_cpu)
Tejun Heo8b03ae32010-06-29 10:07:12 +02001426 kthread_bind(worker->task, gcwq->cpu);
Tejun Heof3421792010-07-02 10:03:51 +02001427 else {
Tejun Heodb7bccf2010-06-29 10:07:12 +02001428 worker->task->flags |= PF_THREAD_BOUND;
Tejun Heof3421792010-07-02 10:03:51 +02001429 if (on_unbound_cpu)
1430 worker->flags |= WORKER_UNBOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001432
Tejun Heoc34056a2010-06-29 10:07:11 +02001433 return worker;
1434fail:
1435 if (id >= 0) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001436 spin_lock_irq(&gcwq->lock);
Tejun Heo58658882012-07-12 14:46:37 -07001437 ida_remove(&pool->worker_ida, id);
Tejun Heo8b03ae32010-06-29 10:07:12 +02001438 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001439 }
1440 kfree(worker);
1441 return NULL;
1442}
1443
1444/**
1445 * start_worker - start a newly created worker
1446 * @worker: worker to start
1447 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001448 * Make the gcwq aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001449 *
1450 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001451 * spin_lock_irq(gcwq->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001452 */
1453static void start_worker(struct worker *worker)
1454{
Tejun Heocb444762010-07-02 10:03:50 +02001455 worker->flags |= WORKER_STARTED;
Tejun Heo58658882012-07-12 14:46:37 -07001456 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001457 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001458 wake_up_process(worker->task);
1459}
1460
1461/**
1462 * destroy_worker - destroy a workqueue worker
1463 * @worker: worker to be destroyed
1464 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001465 * Destroy @worker and adjust @gcwq stats accordingly.
1466 *
1467 * CONTEXT:
1468 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001469 */
1470static void destroy_worker(struct worker *worker)
1471{
Tejun Heo58658882012-07-12 14:46:37 -07001472 struct worker_pool *pool = worker->pool;
1473 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001474 int id = worker->id;
1475
1476 /* sanity check frenzy */
1477 BUG_ON(worker->current_work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001478 BUG_ON(!list_empty(&worker->scheduled));
Tejun Heoc34056a2010-06-29 10:07:11 +02001479
Tejun Heoc8e55f32010-06-29 10:07:12 +02001480 if (worker->flags & WORKER_STARTED)
Tejun Heo58658882012-07-12 14:46:37 -07001481 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001482 if (worker->flags & WORKER_IDLE)
Tejun Heo58658882012-07-12 14:46:37 -07001483 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001484
Lai Jiangshan23f09132014-02-15 22:02:28 +08001485 /*
1486 * Once WORKER_DIE is set, the kworker may destroy itself at any
1487 * point. Pin to ensure the task stays until we're done with it.
1488 */
1489 get_task_struct(worker->task);
1490
Tejun Heoc8e55f32010-06-29 10:07:12 +02001491 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001492 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001493
1494 spin_unlock_irq(&gcwq->lock);
1495
Tejun Heoc34056a2010-06-29 10:07:11 +02001496 kthread_stop(worker->task);
Lai Jiangshan23f09132014-02-15 22:02:28 +08001497 put_task_struct(worker->task);
Tejun Heoc34056a2010-06-29 10:07:11 +02001498 kfree(worker);
1499
Tejun Heo8b03ae32010-06-29 10:07:12 +02001500 spin_lock_irq(&gcwq->lock);
Tejun Heo58658882012-07-12 14:46:37 -07001501 ida_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001502}
1503
Tejun Heo7ef6a932012-07-12 14:46:37 -07001504static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001505{
Tejun Heo7ef6a932012-07-12 14:46:37 -07001506 struct worker_pool *pool = (void *)__pool;
1507 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001508
1509 spin_lock_irq(&gcwq->lock);
1510
Tejun Heo7ef6a932012-07-12 14:46:37 -07001511 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001512 struct worker *worker;
1513 unsigned long expires;
1514
1515 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001516 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001517 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1518
1519 if (time_before(jiffies, expires))
Tejun Heo7ef6a932012-07-12 14:46:37 -07001520 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001521 else {
1522 /* it's been idle for too long, wake up manager */
Tejun Heo22ad5642012-07-12 14:46:37 -07001523 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo7ef6a932012-07-12 14:46:37 -07001524 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001525 }
1526 }
1527
1528 spin_unlock_irq(&gcwq->lock);
1529}
1530
1531static bool send_mayday(struct work_struct *work)
1532{
1533 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1534 struct workqueue_struct *wq = cwq->wq;
Tejun Heof3421792010-07-02 10:03:51 +02001535 unsigned int cpu;
Tejun Heoe22bee72010-06-29 10:07:14 +02001536
1537 if (!(wq->flags & WQ_RESCUER))
1538 return false;
1539
1540 /* mayday mayday mayday */
Tejun Heo58658882012-07-12 14:46:37 -07001541 cpu = cwq->pool->gcwq->cpu;
Tejun Heof3421792010-07-02 10:03:51 +02001542 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1543 if (cpu == WORK_CPU_UNBOUND)
1544 cpu = 0;
Tejun Heof2e005a2010-07-20 15:59:09 +02001545 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001546 wake_up_process(wq->rescuer->task);
1547 return true;
1548}
1549
Tejun Heo7ef6a932012-07-12 14:46:37 -07001550static void gcwq_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001551{
Tejun Heo7ef6a932012-07-12 14:46:37 -07001552 struct worker_pool *pool = (void *)__pool;
1553 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001554 struct work_struct *work;
1555
1556 spin_lock_irq(&gcwq->lock);
1557
Tejun Heo7ef6a932012-07-12 14:46:37 -07001558 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001559 /*
1560 * We've been trying to create a new worker but
1561 * haven't been successful. We might be hitting an
1562 * allocation deadlock. Send distress signals to
1563 * rescuers.
1564 */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001565 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001566 send_mayday(work);
1567 }
1568
1569 spin_unlock_irq(&gcwq->lock);
1570
Tejun Heo7ef6a932012-07-12 14:46:37 -07001571 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001572}
1573
1574/**
1575 * maybe_create_worker - create a new worker if necessary
Tejun Heo7ef6a932012-07-12 14:46:37 -07001576 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001577 *
Tejun Heo7ef6a932012-07-12 14:46:37 -07001578 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001579 * have at least one idle worker on return from this function. If
1580 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo7ef6a932012-07-12 14:46:37 -07001581 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001582 * possible allocation deadlock.
1583 *
1584 * On return, need_to_create_worker() is guaranteed to be false and
1585 * may_start_working() true.
1586 *
1587 * LOCKING:
1588 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1589 * multiple times. Does GFP_KERNEL allocations. Called only from
1590 * manager.
1591 *
1592 * RETURNS:
1593 * false if no action was taken and gcwq->lock stayed locked, true
1594 * otherwise.
1595 */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001596static bool maybe_create_worker(struct worker_pool *pool)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001597__releases(&gcwq->lock)
1598__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001599{
Tejun Heo7ef6a932012-07-12 14:46:37 -07001600 struct global_cwq *gcwq = pool->gcwq;
1601
1602 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001603 return false;
1604restart:
Tejun Heo9f9c2362010-07-14 11:31:20 +02001605 spin_unlock_irq(&gcwq->lock);
1606
Tejun Heoe22bee72010-06-29 10:07:14 +02001607 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001608 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001609
1610 while (true) {
1611 struct worker *worker;
1612
Tejun Heo7ef6a932012-07-12 14:46:37 -07001613 worker = create_worker(pool, true);
Tejun Heoe22bee72010-06-29 10:07:14 +02001614 if (worker) {
Tejun Heo7ef6a932012-07-12 14:46:37 -07001615 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001616 spin_lock_irq(&gcwq->lock);
1617 start_worker(worker);
Tejun Heo7ef6a932012-07-12 14:46:37 -07001618 BUG_ON(need_to_create_worker(pool));
Tejun Heoe22bee72010-06-29 10:07:14 +02001619 return true;
1620 }
1621
Tejun Heo7ef6a932012-07-12 14:46:37 -07001622 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001623 break;
1624
Tejun Heoe22bee72010-06-29 10:07:14 +02001625 __set_current_state(TASK_INTERRUPTIBLE);
1626 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001627
Tejun Heo7ef6a932012-07-12 14:46:37 -07001628 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001629 break;
1630 }
1631
Tejun Heo7ef6a932012-07-12 14:46:37 -07001632 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001633 spin_lock_irq(&gcwq->lock);
Tejun Heo7ef6a932012-07-12 14:46:37 -07001634 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001635 goto restart;
1636 return true;
1637}
1638
1639/**
1640 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo7ef6a932012-07-12 14:46:37 -07001641 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001642 *
Tejun Heo7ef6a932012-07-12 14:46:37 -07001643 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02001644 * IDLE_WORKER_TIMEOUT.
1645 *
1646 * LOCKING:
1647 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1648 * multiple times. Called only from manager.
1649 *
1650 * RETURNS:
1651 * false if no action was taken and gcwq->lock stayed locked, true
1652 * otherwise.
1653 */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001654static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001655{
1656 bool ret = false;
1657
Tejun Heo7ef6a932012-07-12 14:46:37 -07001658 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001659 struct worker *worker;
1660 unsigned long expires;
1661
Tejun Heo7ef6a932012-07-12 14:46:37 -07001662 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001663 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1664
1665 if (time_before(jiffies, expires)) {
Tejun Heo7ef6a932012-07-12 14:46:37 -07001666 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001667 break;
1668 }
1669
1670 destroy_worker(worker);
1671 ret = true;
1672 }
1673
1674 return ret;
1675}
1676
1677/**
1678 * manage_workers - manage worker pool
1679 * @worker: self
1680 *
1681 * Assume the manager role and manage gcwq worker pool @worker belongs
1682 * to. At any given time, there can be only zero or one manager per
1683 * gcwq. The exclusion is handled automatically by this function.
1684 *
1685 * The caller can safely start processing works on false return. On
1686 * true return, it's guaranteed that need_to_create_worker() is false
1687 * and may_start_working() is true.
1688 *
1689 * CONTEXT:
1690 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1691 * multiple times. Does GFP_KERNEL allocations.
1692 *
1693 * RETURNS:
1694 * false if no action was taken and gcwq->lock stayed locked, true if
1695 * some action was taken.
1696 */
1697static bool manage_workers(struct worker *worker)
1698{
Tejun Heo7ef6a932012-07-12 14:46:37 -07001699 struct worker_pool *pool = worker->pool;
1700 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001701 bool ret = false;
1702
Tejun Heo22ad5642012-07-12 14:46:37 -07001703 if (pool->flags & POOL_MANAGING_WORKERS)
Tejun Heoe22bee72010-06-29 10:07:14 +02001704 return ret;
1705
Tejun Heo22ad5642012-07-12 14:46:37 -07001706 pool->flags &= ~POOL_MANAGE_WORKERS;
1707 pool->flags |= POOL_MANAGING_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02001708
1709 /*
1710 * Destroy and then create so that may_start_working() is true
1711 * on return.
1712 */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001713 ret |= maybe_destroy_workers(pool);
1714 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001715
Tejun Heo22ad5642012-07-12 14:46:37 -07001716 pool->flags &= ~POOL_MANAGING_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02001717
1718 /*
1719 * The trustee might be waiting to take over the manager
1720 * position, tell it we're done.
1721 */
1722 if (unlikely(gcwq->trustee))
1723 wake_up_all(&gcwq->trustee_wait);
1724
1725 return ret;
1726}
1727
Tejun Heoa62428c2010-06-29 10:07:10 +02001728/**
Tejun Heoaffee4b2010-06-29 10:07:12 +02001729 * move_linked_works - move linked works to a list
1730 * @work: start of series of works to be scheduled
1731 * @head: target list to append @work to
1732 * @nextp: out paramter for nested worklist walking
1733 *
1734 * Schedule linked works starting from @work to @head. Work series to
1735 * be scheduled starts at @work and includes any consecutive work with
1736 * WORK_STRUCT_LINKED set in its predecessor.
1737 *
1738 * If @nextp is not NULL, it's updated to point to the next work of
1739 * the last scheduled work. This allows move_linked_works() to be
1740 * nested inside outer list_for_each_entry_safe().
1741 *
1742 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001743 * spin_lock_irq(gcwq->lock).
Tejun Heoaffee4b2010-06-29 10:07:12 +02001744 */
1745static void move_linked_works(struct work_struct *work, struct list_head *head,
1746 struct work_struct **nextp)
1747{
1748 struct work_struct *n;
1749
1750 /*
1751 * Linked worklist will always end before the end of the list,
1752 * use NULL for list head.
1753 */
1754 list_for_each_entry_safe_from(work, n, NULL, entry) {
1755 list_move_tail(&work->entry, head);
1756 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1757 break;
1758 }
1759
1760 /*
1761 * If we're already inside safe list traversal and have moved
1762 * multiple works to the scheduled queue, the next position
1763 * needs to be updated.
1764 */
1765 if (nextp)
1766 *nextp = n;
1767}
1768
Lai Jiangshan31eafff2012-09-18 10:40:00 -07001769static void cwq_activate_delayed_work(struct work_struct *work)
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001770{
Lai Jiangshan31eafff2012-09-18 10:40:00 -07001771 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001772
Tejun Heocdadf002010-10-05 10:49:55 +02001773 trace_workqueue_activate_work(work);
Tejun Heodcb32ee2012-07-13 22:16:45 -07001774 move_linked_works(work, &cwq->pool->worklist, NULL);
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001775 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001776 cwq->nr_active++;
1777}
1778
Lai Jiangshan31eafff2012-09-18 10:40:00 -07001779static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
1780{
1781 struct work_struct *work = list_first_entry(&cwq->delayed_works,
1782 struct work_struct, entry);
1783
1784 cwq_activate_delayed_work(work);
1785}
1786
Tejun Heoaffee4b2010-06-29 10:07:12 +02001787/**
Tejun Heo73f53c42010-06-29 10:07:11 +02001788 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
1789 * @cwq: cwq of interest
1790 * @color: color of work which left the queue
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001791 * @delayed: for a delayed work
Tejun Heo73f53c42010-06-29 10:07:11 +02001792 *
1793 * A work either has completed or is removed from pending queue,
1794 * decrement nr_in_flight of its cwq and handle workqueue flushing.
1795 *
1796 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001797 * spin_lock_irq(gcwq->lock).
Tejun Heo73f53c42010-06-29 10:07:11 +02001798 */
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001799static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color,
1800 bool delayed)
Tejun Heo73f53c42010-06-29 10:07:11 +02001801{
1802 /* ignore uncolored works */
1803 if (color == WORK_NO_COLOR)
1804 return;
1805
1806 cwq->nr_in_flight[color]--;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001807
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001808 if (!delayed) {
1809 cwq->nr_active--;
1810 if (!list_empty(&cwq->delayed_works)) {
1811 /* one down, submit a delayed one */
1812 if (cwq->nr_active < cwq->max_active)
1813 cwq_activate_first_delayed(cwq);
1814 }
Tejun Heo502ca9d2010-06-29 10:07:13 +02001815 }
Tejun Heo73f53c42010-06-29 10:07:11 +02001816
1817 /* is flush in progress and are we at the flushing tip? */
1818 if (likely(cwq->flush_color != color))
1819 return;
1820
1821 /* are there still in-flight works? */
1822 if (cwq->nr_in_flight[color])
1823 return;
1824
1825 /* this cwq is done, clear flush_color */
1826 cwq->flush_color = -1;
1827
1828 /*
1829 * If this was the last cwq, wake up the first flusher. It
1830 * will handle the rest.
1831 */
1832 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
1833 complete(&cwq->wq->first_flusher->done);
1834}
1835
1836/**
Tejun Heoa62428c2010-06-29 10:07:10 +02001837 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02001838 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02001839 * @work: work to process
1840 *
1841 * Process @work. This function contains all the logics necessary to
1842 * process a single work including synchronization against and
1843 * interaction with other workers on the same cpu, queueing and
1844 * flushing. As long as context requirement is met, any worker can
1845 * call this function to process a work.
1846 *
1847 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001848 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02001849 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001850static void process_one_work(struct worker *worker, struct work_struct *work)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001851__releases(&gcwq->lock)
1852__acquires(&gcwq->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02001853{
Tejun Heo7e116292010-06-29 10:07:13 +02001854 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heo58658882012-07-12 14:46:37 -07001855 struct worker_pool *pool = worker->pool;
1856 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001857 struct hlist_head *bwh = busy_worker_head(gcwq, work);
Tejun Heofb0e7be2010-06-29 10:07:15 +02001858 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02001859 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02001860 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02001861#ifdef CONFIG_LOCKDEP
1862 /*
1863 * It is permissible to free the struct work_struct from
1864 * inside the function that is called from it, this we need to
1865 * take into account for lockdep too. To avoid bogus "held
1866 * lock freed" warnings as well as problems when looking into
1867 * work->lockdep_map, make a copy and use that here.
1868 */
1869 struct lockdep_map lockdep_map = work->lockdep_map;
1870#endif
Tejun Heo7e116292010-06-29 10:07:13 +02001871 /*
1872 * A single work shouldn't be executed concurrently by
1873 * multiple workers on a single cpu. Check whether anyone is
1874 * already processing the work. If so, defer the work to the
1875 * currently executing one.
1876 */
1877 collision = __find_worker_executing_work(gcwq, bwh, work);
1878 if (unlikely(collision)) {
1879 move_linked_works(work, &collision->scheduled, NULL);
1880 return;
1881 }
1882
Tejun Heoa62428c2010-06-29 10:07:10 +02001883 /* claim and process */
Tejun Heoa62428c2010-06-29 10:07:10 +02001884 debug_work_deactivate(work);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001885 hlist_add_head(&worker->hentry, bwh);
Tejun Heoc34056a2010-06-29 10:07:11 +02001886 worker->current_work = work;
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001887 worker->current_func = work->func;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001888 worker->current_cwq = cwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02001889 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02001890
Tejun Heo7a22ad72010-06-29 10:07:13 +02001891 /* record the current cpu number in the work data and dequeue */
1892 set_work_cpu(work, gcwq->cpu);
Tejun Heoa62428c2010-06-29 10:07:10 +02001893 list_del_init(&work->entry);
1894
Tejun Heo649027d2010-06-29 10:07:14 +02001895 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02001896 * CPU intensive works don't participate in concurrency
1897 * management. They're the scheduler's responsibility.
1898 */
1899 if (unlikely(cpu_intensive))
1900 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
1901
Tejun Heob7b5c682012-07-12 14:46:37 -07001902 /*
1903 * Unbound gcwq isn't concurrency managed and work items should be
1904 * executed ASAP. Wake up another worker if necessary.
1905 */
Tejun Heo7ef6a932012-07-12 14:46:37 -07001906 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
1907 wake_up_worker(pool);
Tejun Heob7b5c682012-07-12 14:46:37 -07001908
Tejun Heo8b03ae32010-06-29 10:07:12 +02001909 spin_unlock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001910
Tejun Heo66307ae2012-08-03 10:30:45 -07001911 smp_wmb(); /* paired with test_and_set_bit(PENDING) */
Tejun Heoa62428c2010-06-29 10:07:10 +02001912 work_clear_pending(work);
Tejun Heo66307ae2012-08-03 10:30:45 -07001913
Tejun Heoe1594892011-01-09 23:32:15 +01001914 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02001915 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001916 trace_workqueue_execute_start(work);
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001917 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07001918 /*
1919 * While we must be careful to not use "work" after this, the trace
1920 * point will only record its address.
1921 */
1922 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02001923 lock_map_release(&lockdep_map);
1924 lock_map_release(&cwq->wq->lockdep_map);
1925
1926 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001927 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
1928 " last function: %pf\n",
1929 current->comm, preempt_count(), task_pid_nr(current),
1930 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02001931 debug_show_held_locks(current);
Syed Rameez Mustafa1bee7b92013-07-15 11:52:09 -07001932 BUG_ON(PANIC_CORRUPTION);
Tejun Heoa62428c2010-06-29 10:07:10 +02001933 dump_stack();
1934 }
1935
Tejun Heo00cef7a2013-08-28 17:33:37 -04001936 /*
1937 * The following prevents a kworker from hogging CPU on !PREEMPT
1938 * kernels, where a requeueing work item waiting for something to
1939 * happen could deadlock with stop_machine as such work item could
1940 * indefinitely requeue itself while all other CPUs are trapped in
1941 * stop_machine.
1942 */
1943 cond_resched();
1944
Tejun Heo8b03ae32010-06-29 10:07:12 +02001945 spin_lock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02001946
Tejun Heofb0e7be2010-06-29 10:07:15 +02001947 /* clear cpu intensive status */
1948 if (unlikely(cpu_intensive))
1949 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
1950
Tejun Heoa62428c2010-06-29 10:07:10 +02001951 /* we're done with it, release */
Tejun Heoc8e55f32010-06-29 10:07:12 +02001952 hlist_del_init(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001953 worker->current_work = NULL;
Tejun Heo55e3e1f2012-12-18 10:35:02 -08001954 worker->current_func = NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001955 worker->current_cwq = NULL;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001956 cwq_dec_nr_in_flight(cwq, work_color, false);
Tejun Heoa62428c2010-06-29 10:07:10 +02001957}
1958
Tejun Heoaffee4b2010-06-29 10:07:12 +02001959/**
1960 * process_scheduled_works - process scheduled works
1961 * @worker: self
1962 *
1963 * Process all scheduled works. Please note that the scheduled list
1964 * may change while processing a work, so this function repeatedly
1965 * fetches a work from the top and executes it.
1966 *
1967 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001968 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02001969 * multiple times.
1970 */
1971static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972{
Tejun Heoaffee4b2010-06-29 10:07:12 +02001973 while (!list_empty(&worker->scheduled)) {
1974 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02001976 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978}
1979
Tejun Heo4690c4a2010-06-29 10:07:10 +02001980/**
1981 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02001982 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02001983 *
Tejun Heoe22bee72010-06-29 10:07:14 +02001984 * The gcwq worker thread function. There's a single dynamic pool of
1985 * these per each cpu. These workers process all works regardless of
1986 * their specific target workqueue. The only exception is works which
1987 * belong to workqueues with a rescuer which will be explained in
1988 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02001989 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001990static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991{
Tejun Heoc34056a2010-06-29 10:07:11 +02001992 struct worker *worker = __worker;
Tejun Heo58658882012-07-12 14:46:37 -07001993 struct worker_pool *pool = worker->pool;
1994 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Tejun Heoe22bee72010-06-29 10:07:14 +02001996 /* tell the scheduler that this is a workqueue worker */
1997 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001998woke_up:
Tejun Heoc8e55f32010-06-29 10:07:12 +02001999 spin_lock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
Tejun Heoc8e55f32010-06-29 10:07:12 +02002001 /* DIE can be set only while we're idle, checking here is enough */
2002 if (worker->flags & WORKER_DIE) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02002003 spin_unlock_irq(&gcwq->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002004 worker->task->flags &= ~PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002005 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 }
2007
Tejun Heoc8e55f32010-06-29 10:07:12 +02002008 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002009recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002010 /* no more worker necessary? */
Tejun Heo7ef6a932012-07-12 14:46:37 -07002011 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002012 goto sleep;
2013
2014 /* do we need to manage? */
Tejun Heo7ef6a932012-07-12 14:46:37 -07002015 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002016 goto recheck;
2017
Tejun Heoc8e55f32010-06-29 10:07:12 +02002018 /*
2019 * ->scheduled list can only be filled while a worker is
2020 * preparing to process a work or actually processing it.
2021 * Make sure nobody diddled with it while I was sleeping.
2022 */
2023 BUG_ON(!list_empty(&worker->scheduled));
2024
Tejun Heoe22bee72010-06-29 10:07:14 +02002025 /*
2026 * When control reaches this point, we're guaranteed to have
2027 * at least one idle worker or that someone else has already
2028 * assumed the manager role.
2029 */
2030 worker_clr_flags(worker, WORKER_PREP);
2031
2032 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002033 struct work_struct *work =
Tejun Heo58658882012-07-12 14:46:37 -07002034 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002035 struct work_struct, entry);
2036
2037 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2038 /* optimization path, not strictly necessary */
2039 process_one_work(worker, work);
2040 if (unlikely(!list_empty(&worker->scheduled)))
2041 process_scheduled_works(worker);
2042 } else {
2043 move_linked_works(work, &worker->scheduled, NULL);
2044 process_scheduled_works(worker);
2045 }
Tejun Heo7ef6a932012-07-12 14:46:37 -07002046 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002047
Tejun Heoe22bee72010-06-29 10:07:14 +02002048 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002049sleep:
Tejun Heo7ef6a932012-07-12 14:46:37 -07002050 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002051 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002052
Tejun Heoc8e55f32010-06-29 10:07:12 +02002053 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02002054 * gcwq->lock is held and there's no work to process and no
2055 * need to manage, sleep. Workers are woken up only while
2056 * holding gcwq->lock or from local cpu, so setting the
2057 * current state before releasing gcwq->lock is enough to
2058 * prevent losing any event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002059 */
2060 worker_enter_idle(worker);
2061 __set_current_state(TASK_INTERRUPTIBLE);
2062 spin_unlock_irq(&gcwq->lock);
2063 schedule();
2064 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065}
2066
Tejun Heoe22bee72010-06-29 10:07:14 +02002067/**
2068 * rescuer_thread - the rescuer thread function
2069 * @__wq: the associated workqueue
2070 *
2071 * Workqueue rescuer thread function. There's one rescuer for each
2072 * workqueue which has WQ_RESCUER set.
2073 *
2074 * Regular work processing on a gcwq may block trying to create a new
2075 * worker which uses GFP_KERNEL allocation which has slight chance of
2076 * developing into deadlock if some works currently on the same queue
2077 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2078 * the problem rescuer solves.
2079 *
2080 * When such condition is possible, the gcwq summons rescuers of all
2081 * workqueues which have works queued on the gcwq and let them process
2082 * those works so that forward progress can be guaranteed.
2083 *
2084 * This should happen rarely.
2085 */
2086static int rescuer_thread(void *__wq)
2087{
2088 struct workqueue_struct *wq = __wq;
2089 struct worker *rescuer = wq->rescuer;
2090 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heof3421792010-07-02 10:03:51 +02002091 bool is_unbound = wq->flags & WQ_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02002092 unsigned int cpu;
2093
2094 set_user_nice(current, RESCUER_NICE_LEVEL);
2095repeat:
2096 set_current_state(TASK_INTERRUPTIBLE);
2097
Mike Galbraithdbdd7f02012-11-28 07:17:18 +01002098 if (kthread_should_stop()) {
2099 __set_current_state(TASK_RUNNING);
Tejun Heoe22bee72010-06-29 10:07:14 +02002100 return 0;
Mike Galbraithdbdd7f02012-11-28 07:17:18 +01002101 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002102
Tejun Heof3421792010-07-02 10:03:51 +02002103 /*
2104 * See whether any cpu is asking for help. Unbounded
2105 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
2106 */
Tejun Heof2e005a2010-07-20 15:59:09 +02002107 for_each_mayday_cpu(cpu, wq->mayday_mask) {
Tejun Heof3421792010-07-02 10:03:51 +02002108 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2109 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
Tejun Heo58658882012-07-12 14:46:37 -07002110 struct worker_pool *pool = cwq->pool;
2111 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002112 struct work_struct *work, *n;
2113
2114 __set_current_state(TASK_RUNNING);
Tejun Heof2e005a2010-07-20 15:59:09 +02002115 mayday_clear_cpu(cpu, wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002116
2117 /* migrate to the target cpu if possible */
Tejun Heo58658882012-07-12 14:46:37 -07002118 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002119 worker_maybe_bind_and_lock(rescuer);
2120
2121 /*
2122 * Slurp in all works issued via this workqueue and
2123 * process'em.
2124 */
2125 BUG_ON(!list_empty(&rescuer->scheduled));
Tejun Heo58658882012-07-12 14:46:37 -07002126 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02002127 if (get_work_cwq(work) == cwq)
2128 move_linked_works(work, scheduled, &n);
2129
2130 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002131
2132 /*
2133 * Leave this gcwq. If keep_working() is %true, notify a
2134 * regular worker; otherwise, we end up with 0 concurrency
2135 * and stalling the execution.
2136 */
Tejun Heo7ef6a932012-07-12 14:46:37 -07002137 if (keep_working(pool))
2138 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002139
Tejun Heoe22bee72010-06-29 10:07:14 +02002140 spin_unlock_irq(&gcwq->lock);
2141 }
2142
2143 schedule();
2144 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145}
2146
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002147struct wq_barrier {
2148 struct work_struct work;
2149 struct completion done;
2150};
2151
2152static void wq_barrier_func(struct work_struct *work)
2153{
2154 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2155 complete(&barr->done);
2156}
2157
Tejun Heo4690c4a2010-06-29 10:07:10 +02002158/**
2159 * insert_wq_barrier - insert a barrier work
2160 * @cwq: cwq to insert barrier into
2161 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002162 * @target: target work to attach @barr to
2163 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002164 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002165 * @barr is linked to @target such that @barr is completed only after
2166 * @target finishes execution. Please note that the ordering
2167 * guarantee is observed only with respect to @target and on the local
2168 * cpu.
2169 *
2170 * Currently, a queued barrier can't be canceled. This is because
2171 * try_to_grab_pending() can't determine whether the work to be
2172 * grabbed is at the head of the queue and thus can't clear LINKED
2173 * flag of the previous work while there must be a valid next work
2174 * after a work with LINKED flag set.
2175 *
2176 * Note that when @worker is non-NULL, @target may be modified
2177 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002178 *
2179 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002180 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002181 */
Oleg Nesterov83c22522007-05-09 02:33:54 -07002182static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002183 struct wq_barrier *barr,
2184 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002185{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002186 struct list_head *head;
2187 unsigned int linked = 0;
2188
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002189 /*
Tejun Heo8b03ae32010-06-29 10:07:12 +02002190 * debugobject calls are safe here even with gcwq->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002191 * as we know for sure that this will not trigger any of the
2192 * checks and call back into the fixup functions where we
2193 * might deadlock.
2194 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002195 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002196 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002197 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002198
Tejun Heoaffee4b2010-06-29 10:07:12 +02002199 /*
2200 * If @target is currently being executed, schedule the
2201 * barrier to the worker; otherwise, put it after @target.
2202 */
2203 if (worker)
2204 head = worker->scheduled.next;
2205 else {
2206 unsigned long *bits = work_data_bits(target);
2207
2208 head = target->entry.next;
2209 /* there can already be other linked works, inherit and set */
2210 linked = *bits & WORK_STRUCT_LINKED;
2211 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2212 }
2213
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002214 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002215 insert_work(cwq, &barr->work, head,
2216 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002217}
2218
Tejun Heo73f53c42010-06-29 10:07:11 +02002219/**
2220 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2221 * @wq: workqueue being flushed
2222 * @flush_color: new flush color, < 0 for no-op
2223 * @work_color: new work color, < 0 for no-op
2224 *
2225 * Prepare cwqs for workqueue flushing.
2226 *
2227 * If @flush_color is non-negative, flush_color on all cwqs should be
2228 * -1. If no cwq has in-flight commands at the specified color, all
2229 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2230 * has in flight commands, its cwq->flush_color is set to
2231 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2232 * wakeup logic is armed and %true is returned.
2233 *
2234 * The caller should have initialized @wq->first_flusher prior to
2235 * calling this function with non-negative @flush_color. If
2236 * @flush_color is negative, no flush color update is done and %false
2237 * is returned.
2238 *
2239 * If @work_color is non-negative, all cwqs should have the same
2240 * work_color which is previous to @work_color and all will be
2241 * advanced to @work_color.
2242 *
2243 * CONTEXT:
2244 * mutex_lock(wq->flush_mutex).
2245 *
2246 * RETURNS:
2247 * %true if @flush_color >= 0 and there's something to flush. %false
2248 * otherwise.
2249 */
2250static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2251 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252{
Tejun Heo73f53c42010-06-29 10:07:11 +02002253 bool wait = false;
2254 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -07002255
Tejun Heo73f53c42010-06-29 10:07:11 +02002256 if (flush_color >= 0) {
2257 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2258 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002259 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002260
Tejun Heof3421792010-07-02 10:03:51 +02002261 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002262 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo58658882012-07-12 14:46:37 -07002263 struct global_cwq *gcwq = cwq->pool->gcwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002264
Tejun Heo8b03ae32010-06-29 10:07:12 +02002265 spin_lock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002266
2267 if (flush_color >= 0) {
2268 BUG_ON(cwq->flush_color != -1);
2269
2270 if (cwq->nr_in_flight[flush_color]) {
2271 cwq->flush_color = flush_color;
2272 atomic_inc(&wq->nr_cwqs_to_flush);
2273 wait = true;
2274 }
2275 }
2276
2277 if (work_color >= 0) {
2278 BUG_ON(work_color != work_next_color(cwq->work_color));
2279 cwq->work_color = work_color;
2280 }
2281
Tejun Heo8b03ae32010-06-29 10:07:12 +02002282 spin_unlock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002283 }
2284
2285 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2286 complete(&wq->first_flusher->done);
2287
2288 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289}
2290
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002291/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002293 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 *
2295 * Forces execution of the workqueue and blocks until its completion.
2296 * This is typically used in driver shutdown handlers.
2297 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002298 * We sleep until all works which were queued on entry have been handled,
2299 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002301void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302{
Tejun Heo73f53c42010-06-29 10:07:11 +02002303 struct wq_flusher this_flusher = {
2304 .list = LIST_HEAD_INIT(this_flusher.list),
2305 .flush_color = -1,
2306 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2307 };
2308 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002309
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002310 lock_map_acquire(&wq->lockdep_map);
2311 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002312
2313 mutex_lock(&wq->flush_mutex);
2314
2315 /*
2316 * Start-to-wait phase
2317 */
2318 next_color = work_next_color(wq->work_color);
2319
2320 if (next_color != wq->flush_color) {
2321 /*
2322 * Color space is not full. The current work_color
2323 * becomes our flush_color and work_color is advanced
2324 * by one.
2325 */
2326 BUG_ON(!list_empty(&wq->flusher_overflow));
2327 this_flusher.flush_color = wq->work_color;
2328 wq->work_color = next_color;
2329
2330 if (!wq->first_flusher) {
2331 /* no flush in progress, become the first flusher */
2332 BUG_ON(wq->flush_color != this_flusher.flush_color);
2333
2334 wq->first_flusher = &this_flusher;
2335
2336 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2337 wq->work_color)) {
2338 /* nothing to flush, done */
2339 wq->flush_color = next_color;
2340 wq->first_flusher = NULL;
2341 goto out_unlock;
2342 }
2343 } else {
2344 /* wait in queue */
2345 BUG_ON(wq->flush_color == this_flusher.flush_color);
2346 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2347 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2348 }
2349 } else {
2350 /*
2351 * Oops, color space is full, wait on overflow queue.
2352 * The next flush completion will assign us
2353 * flush_color and transfer to flusher_queue.
2354 */
2355 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2356 }
2357
2358 mutex_unlock(&wq->flush_mutex);
2359
2360 wait_for_completion(&this_flusher.done);
2361
2362 /*
2363 * Wake-up-and-cascade phase
2364 *
2365 * First flushers are responsible for cascading flushes and
2366 * handling overflow. Non-first flushers can simply return.
2367 */
2368 if (wq->first_flusher != &this_flusher)
2369 return;
2370
2371 mutex_lock(&wq->flush_mutex);
2372
Tejun Heo4ce48b32010-07-02 10:03:51 +02002373 /* we might have raced, check again with mutex held */
2374 if (wq->first_flusher != &this_flusher)
2375 goto out_unlock;
2376
Tejun Heo73f53c42010-06-29 10:07:11 +02002377 wq->first_flusher = NULL;
2378
2379 BUG_ON(!list_empty(&this_flusher.list));
2380 BUG_ON(wq->flush_color != this_flusher.flush_color);
2381
2382 while (true) {
2383 struct wq_flusher *next, *tmp;
2384
2385 /* complete all the flushers sharing the current flush color */
2386 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2387 if (next->flush_color != wq->flush_color)
2388 break;
2389 list_del_init(&next->list);
2390 complete(&next->done);
2391 }
2392
2393 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2394 wq->flush_color != work_next_color(wq->work_color));
2395
2396 /* this flush_color is finished, advance by one */
2397 wq->flush_color = work_next_color(wq->flush_color);
2398
2399 /* one color has been freed, handle overflow queue */
2400 if (!list_empty(&wq->flusher_overflow)) {
2401 /*
2402 * Assign the same color to all overflowed
2403 * flushers, advance work_color and append to
2404 * flusher_queue. This is the start-to-wait
2405 * phase for these overflowed flushers.
2406 */
2407 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2408 tmp->flush_color = wq->work_color;
2409
2410 wq->work_color = work_next_color(wq->work_color);
2411
2412 list_splice_tail_init(&wq->flusher_overflow,
2413 &wq->flusher_queue);
2414 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2415 }
2416
2417 if (list_empty(&wq->flusher_queue)) {
2418 BUG_ON(wq->flush_color != wq->work_color);
2419 break;
2420 }
2421
2422 /*
2423 * Need to flush more colors. Make the next flusher
2424 * the new first flusher and arm cwqs.
2425 */
2426 BUG_ON(wq->flush_color == wq->work_color);
2427 BUG_ON(wq->flush_color != next->flush_color);
2428
2429 list_del_init(&next->list);
2430 wq->first_flusher = next;
2431
2432 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2433 break;
2434
2435 /*
2436 * Meh... this color is already done, clear first
2437 * flusher and repeat cascading.
2438 */
2439 wq->first_flusher = NULL;
2440 }
2441
2442out_unlock:
2443 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444}
Dave Jonesae90dd52006-06-30 01:40:45 -04002445EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002447/**
2448 * drain_workqueue - drain a workqueue
2449 * @wq: workqueue to drain
2450 *
2451 * Wait until the workqueue becomes empty. While draining is in progress,
2452 * only chain queueing is allowed. IOW, only currently pending or running
2453 * work items on @wq can queue further work items on it. @wq is flushed
2454 * repeatedly until it becomes empty. The number of flushing is detemined
2455 * by the depth of chaining and should be relatively short. Whine if it
2456 * takes too long.
2457 */
2458void drain_workqueue(struct workqueue_struct *wq)
2459{
2460 unsigned int flush_cnt = 0;
2461 unsigned int cpu;
2462
2463 /*
2464 * __queue_work() needs to test whether there are drainers, is much
2465 * hotter than drain_workqueue() and already looks at @wq->flags.
2466 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2467 */
2468 spin_lock(&workqueue_lock);
2469 if (!wq->nr_drainers++)
2470 wq->flags |= WQ_DRAINING;
2471 spin_unlock(&workqueue_lock);
2472reflush:
2473 flush_workqueue(wq);
2474
2475 for_each_cwq_cpu(cpu, wq) {
2476 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002477 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002478
Tejun Heo58658882012-07-12 14:46:37 -07002479 spin_lock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002480 drained = !cwq->nr_active && list_empty(&cwq->delayed_works);
Tejun Heo58658882012-07-12 14:46:37 -07002481 spin_unlock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002482
2483 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002484 continue;
2485
2486 if (++flush_cnt == 10 ||
2487 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
2488 pr_warning("workqueue %s: flush on destruction isn't complete after %u tries\n",
2489 wq->name, flush_cnt);
2490 goto reflush;
2491 }
2492
2493 spin_lock(&workqueue_lock);
2494 if (!--wq->nr_drainers)
2495 wq->flags &= ~WQ_DRAINING;
2496 spin_unlock(&workqueue_lock);
2497}
2498EXPORT_SYMBOL_GPL(drain_workqueue);
2499
Tejun Heobaf59022010-09-16 10:42:16 +02002500static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
2501 bool wait_executing)
2502{
2503 struct worker *worker = NULL;
2504 struct global_cwq *gcwq;
2505 struct cpu_workqueue_struct *cwq;
2506
2507 might_sleep();
2508 gcwq = get_work_gcwq(work);
2509 if (!gcwq)
2510 return false;
2511
2512 spin_lock_irq(&gcwq->lock);
2513 if (!list_empty(&work->entry)) {
2514 /*
2515 * See the comment near try_to_grab_pending()->smp_rmb().
2516 * If it was re-queued to a different gcwq under us, we
2517 * are not going to wait.
2518 */
2519 smp_rmb();
2520 cwq = get_work_cwq(work);
Tejun Heo58658882012-07-12 14:46:37 -07002521 if (unlikely(!cwq || gcwq != cwq->pool->gcwq))
Tejun Heobaf59022010-09-16 10:42:16 +02002522 goto already_gone;
2523 } else if (wait_executing) {
2524 worker = find_worker_executing_work(gcwq, work);
2525 if (!worker)
2526 goto already_gone;
2527 cwq = worker->current_cwq;
2528 } else
2529 goto already_gone;
2530
2531 insert_wq_barrier(cwq, barr, work, worker);
2532 spin_unlock_irq(&gcwq->lock);
2533
Tejun Heoe1594892011-01-09 23:32:15 +01002534 /*
2535 * If @max_active is 1 or rescuer is in use, flushing another work
2536 * item on the same workqueue may lead to deadlock. Make sure the
2537 * flusher is not running on the same workqueue by verifying write
2538 * access.
2539 */
2540 if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER)
2541 lock_map_acquire(&cwq->wq->lockdep_map);
2542 else
2543 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heobaf59022010-09-16 10:42:16 +02002544 lock_map_release(&cwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002545
Tejun Heobaf59022010-09-16 10:42:16 +02002546 return true;
2547already_gone:
2548 spin_unlock_irq(&gcwq->lock);
2549 return false;
2550}
2551
Oleg Nesterovdb700892008-07-25 01:47:49 -07002552/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002553 * flush_work - wait for a work to finish executing the last queueing instance
2554 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002555 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002556 * Wait until @work has finished execution. This function considers
2557 * only the last queueing instance of @work. If @work has been
2558 * enqueued across different CPUs on a non-reentrant workqueue or on
2559 * multiple workqueues, @work might still be executing on return on
2560 * some of the CPUs from earlier queueing.
Oleg Nesterova67da702008-07-25 01:47:52 -07002561 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002562 * If @work was queued only on a non-reentrant, ordered or unbound
2563 * workqueue, @work is guaranteed to be idle on return if it hasn't
2564 * been requeued since flush started.
2565 *
2566 * RETURNS:
2567 * %true if flush_work() waited for the work to finish execution,
2568 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002569 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002570bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002571{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002572 struct wq_barrier barr;
2573
Tejun Heobaf59022010-09-16 10:42:16 +02002574 if (start_flush_work(work, &barr, true)) {
2575 wait_for_completion(&barr.done);
2576 destroy_work_on_stack(&barr.work);
2577 return true;
2578 } else
2579 return false;
Oleg Nesterovdb700892008-07-25 01:47:49 -07002580}
2581EXPORT_SYMBOL_GPL(flush_work);
2582
Tejun Heo401a8d02010-09-16 10:36:00 +02002583static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work)
2584{
2585 struct wq_barrier barr;
2586 struct worker *worker;
2587
2588 spin_lock_irq(&gcwq->lock);
2589
2590 worker = find_worker_executing_work(gcwq, work);
2591 if (unlikely(worker))
2592 insert_wq_barrier(worker->current_cwq, &barr, work, worker);
2593
2594 spin_unlock_irq(&gcwq->lock);
2595
2596 if (unlikely(worker)) {
2597 wait_for_completion(&barr.done);
2598 destroy_work_on_stack(&barr.work);
2599 return true;
2600 } else
2601 return false;
2602}
2603
2604static bool wait_on_work(struct work_struct *work)
2605{
2606 bool ret = false;
2607 int cpu;
2608
2609 might_sleep();
2610
2611 lock_map_acquire(&work->lockdep_map);
2612 lock_map_release(&work->lockdep_map);
2613
2614 for_each_gcwq_cpu(cpu)
2615 ret |= wait_on_cpu_work(get_gcwq(cpu), work);
2616 return ret;
2617}
2618
Tejun Heo09383492010-09-16 10:48:29 +02002619/**
2620 * flush_work_sync - wait until a work has finished execution
2621 * @work: the work to flush
2622 *
2623 * Wait until @work has finished execution. On return, it's
2624 * guaranteed that all queueing instances of @work which happened
2625 * before this function is called are finished. In other words, if
2626 * @work hasn't been requeued since this function was called, @work is
2627 * guaranteed to be idle on return.
2628 *
2629 * RETURNS:
2630 * %true if flush_work_sync() waited for the work to finish execution,
2631 * %false if it was already idle.
2632 */
2633bool flush_work_sync(struct work_struct *work)
2634{
2635 struct wq_barrier barr;
2636 bool pending, waited;
2637
2638 /* we'll wait for executions separately, queue barr only if pending */
2639 pending = start_flush_work(work, &barr, false);
2640
2641 /* wait for executions to finish */
2642 waited = wait_on_work(work);
2643
2644 /* wait for the pending one */
2645 if (pending) {
2646 wait_for_completion(&barr.done);
2647 destroy_work_on_stack(&barr.work);
2648 }
2649
2650 return pending || waited;
2651}
2652EXPORT_SYMBOL_GPL(flush_work_sync);
2653
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002654/*
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002655 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002656 * so this work can't be re-armed in any way.
2657 */
2658static int try_to_grab_pending(struct work_struct *work)
2659{
Tejun Heo8b03ae32010-06-29 10:07:12 +02002660 struct global_cwq *gcwq;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002661 int ret = -1;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002662
Tejun Heo22df02b2010-06-29 10:07:10 +02002663 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002664 return 0;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002665
2666 /*
2667 * The queueing is in progress, or it is already queued. Try to
2668 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
2669 */
Tejun Heo7a22ad72010-06-29 10:07:13 +02002670 gcwq = get_work_gcwq(work);
2671 if (!gcwq)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002672 return ret;
2673
Tejun Heo8b03ae32010-06-29 10:07:12 +02002674 spin_lock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002675 if (!list_empty(&work->entry)) {
2676 /*
Tejun Heo7a22ad72010-06-29 10:07:13 +02002677 * This work is queued, but perhaps we locked the wrong gcwq.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002678 * In that case we must see the new value after rmb(), see
2679 * insert_work()->wmb().
2680 */
2681 smp_rmb();
Tejun Heo7a22ad72010-06-29 10:07:13 +02002682 if (gcwq == get_work_gcwq(work)) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002683 debug_work_deactivate(work);
Lai Jiangshan31eafff2012-09-18 10:40:00 -07002684
2685 /*
2686 * A delayed work item cannot be grabbed directly
2687 * because it might have linked NO_COLOR work items
2688 * which, if left on the delayed_list, will confuse
2689 * cwq->nr_active management later on and cause
2690 * stall. Make sure the work item is activated
2691 * before grabbing.
2692 */
2693 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
2694 cwq_activate_delayed_work(work);
2695
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002696 list_del_init(&work->entry);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002697 cwq_dec_nr_in_flight(get_work_cwq(work),
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02002698 get_work_color(work),
2699 *work_data_bits(work) & WORK_STRUCT_DELAYED);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002700 ret = 1;
2701 }
2702 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02002703 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002704
2705 return ret;
2706}
2707
Tejun Heo401a8d02010-09-16 10:36:00 +02002708static bool __cancel_work_timer(struct work_struct *work,
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002709 struct timer_list* timer)
2710{
2711 int ret;
2712
2713 do {
2714 ret = (timer && likely(del_timer(timer)));
2715 if (!ret)
2716 ret = try_to_grab_pending(work);
2717 wait_on_work(work);
2718 } while (unlikely(ret < 0));
2719
Tejun Heo7a22ad72010-06-29 10:07:13 +02002720 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002721 return ret;
2722}
2723
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002724/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002725 * cancel_work_sync - cancel a work and wait for it to finish
2726 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002727 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002728 * Cancel @work and wait for its execution to finish. This function
2729 * can be used even if the work re-queues itself or migrates to
2730 * another workqueue. On return from this function, @work is
2731 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002732 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002733 * cancel_work_sync(&delayed_work->work) must not be used for
2734 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002735 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002736 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002737 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002738 *
2739 * RETURNS:
2740 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002741 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002742bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002743{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002744 return __cancel_work_timer(work, NULL);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002745}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002746EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002747
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002748/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002749 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2750 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002751 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002752 * Delayed timer is cancelled and the pending work is queued for
2753 * immediate execution. Like flush_work(), this function only
2754 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002755 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002756 * RETURNS:
2757 * %true if flush_work() waited for the work to finish execution,
2758 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002759 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002760bool flush_delayed_work(struct delayed_work *dwork)
2761{
2762 if (del_timer_sync(&dwork->timer))
2763 __queue_work(raw_smp_processor_id(),
2764 get_work_cwq(&dwork->work)->wq, &dwork->work);
2765 return flush_work(&dwork->work);
2766}
2767EXPORT_SYMBOL(flush_delayed_work);
2768
2769/**
Tejun Heo09383492010-09-16 10:48:29 +02002770 * flush_delayed_work_sync - wait for a dwork to finish
2771 * @dwork: the delayed work to flush
2772 *
2773 * Delayed timer is cancelled and the pending work is queued for
2774 * execution immediately. Other than timer handling, its behavior
2775 * is identical to flush_work_sync().
2776 *
2777 * RETURNS:
2778 * %true if flush_work_sync() waited for the work to finish execution,
2779 * %false if it was already idle.
2780 */
2781bool flush_delayed_work_sync(struct delayed_work *dwork)
2782{
2783 if (del_timer_sync(&dwork->timer))
2784 __queue_work(raw_smp_processor_id(),
2785 get_work_cwq(&dwork->work)->wq, &dwork->work);
2786 return flush_work_sync(&dwork->work);
2787}
2788EXPORT_SYMBOL(flush_delayed_work_sync);
2789
2790/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002791 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2792 * @dwork: the delayed work cancel
2793 *
2794 * This is cancel_work_sync() for delayed works.
2795 *
2796 * RETURNS:
2797 * %true if @dwork was pending, %false otherwise.
2798 */
2799bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002800{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002801 return __cancel_work_timer(&dwork->work, &dwork->timer);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002802}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002803EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002805/**
2806 * schedule_work - put work task in global workqueue
2807 * @work: job to be done
2808 *
Bart Van Assche5b0f437d2009-07-30 19:00:53 +02002809 * Returns zero if @work was already on the kernel-global workqueue and
2810 * non-zero otherwise.
2811 *
2812 * This puts a job in the kernel-global workqueue if it was not already
2813 * queued and leaves it in the same position on the kernel-global
2814 * workqueue otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002815 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002816int schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817{
Tejun Heod320c032010-06-29 10:07:14 +02002818 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819}
Dave Jonesae90dd52006-06-30 01:40:45 -04002820EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
Zhang Ruic1a220e2008-07-23 21:28:39 -07002822/*
2823 * schedule_work_on - put work task on a specific cpu
2824 * @cpu: cpu to put the work task on
2825 * @work: job to be done
2826 *
2827 * This puts a job on a specific cpu
2828 */
2829int schedule_work_on(int cpu, struct work_struct *work)
2830{
Tejun Heod320c032010-06-29 10:07:14 +02002831 return queue_work_on(cpu, system_wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07002832}
2833EXPORT_SYMBOL(schedule_work_on);
2834
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002835/**
2836 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +00002837 * @dwork: job to be done
2838 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002839 *
2840 * After waiting for a given time this puts a job in the kernel-global
2841 * workqueue.
2842 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002843int schedule_delayed_work(struct delayed_work *dwork,
Ingo Molnar82f67cd2007-02-16 01:28:13 -08002844 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845{
Tejun Heod320c032010-06-29 10:07:14 +02002846 return queue_delayed_work(system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847}
Dave Jonesae90dd52006-06-30 01:40:45 -04002848EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002850/**
2851 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
2852 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +00002853 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002854 * @delay: number of jiffies to wait
2855 *
2856 * After waiting for a given time this puts a job in the kernel-global
2857 * workqueue on the specified CPU.
2858 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +00002860 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861{
Tejun Heod320c032010-06-29 10:07:14 +02002862 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863}
Dave Jonesae90dd52006-06-30 01:40:45 -04002864EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
Andrew Mortonb6136772006-06-25 05:47:49 -07002866/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002867 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002868 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002869 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002870 * schedule_on_each_cpu() executes @func on each online CPU using the
2871 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002872 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002873 *
2874 * RETURNS:
2875 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002876 */
David Howells65f27f32006-11-22 14:55:48 +00002877int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002878{
2879 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002880 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002881
Andrew Mortonb6136772006-06-25 05:47:49 -07002882 works = alloc_percpu(struct work_struct);
2883 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002884 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002885
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002886 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002887
Christoph Lameter15316ba2006-01-08 01:00:43 -08002888 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002889 struct work_struct *work = per_cpu_ptr(works, cpu);
2890
2891 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002892 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002893 }
Tejun Heo93981802009-11-17 14:06:20 -08002894
2895 for_each_online_cpu(cpu)
2896 flush_work(per_cpu_ptr(works, cpu));
2897
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002898 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002899 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002900 return 0;
2901}
2902
Alan Sterneef6a7d2010-02-12 17:39:21 +09002903/**
2904 * flush_scheduled_work - ensure that any scheduled work has run to completion.
2905 *
2906 * Forces execution of the kernel-global workqueue and blocks until its
2907 * completion.
2908 *
2909 * Think twice before calling this function! It's very easy to get into
2910 * trouble if you don't take great care. Either of the following situations
2911 * will lead to deadlock:
2912 *
2913 * One of the work items currently on the workqueue needs to acquire
2914 * a lock held by your code or its caller.
2915 *
2916 * Your code is running in the context of a work routine.
2917 *
2918 * They will be detected by lockdep when they occur, but the first might not
2919 * occur very often. It depends on what work items are on the workqueue and
2920 * what locks they need, which you have no control over.
2921 *
2922 * In most situations flushing the entire workqueue is overkill; you merely
2923 * need to know that a particular work item isn't queued and isn't running.
2924 * In such cases you should use cancel_delayed_work_sync() or
2925 * cancel_work_sync() instead.
2926 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927void flush_scheduled_work(void)
2928{
Tejun Heod320c032010-06-29 10:07:14 +02002929 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930}
Dave Jonesae90dd52006-06-30 01:40:45 -04002931EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
2933/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06002934 * execute_in_process_context - reliably execute the routine with user context
2935 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06002936 * @ew: guaranteed storage for the execute work structure (must
2937 * be available when the work executes)
2938 *
2939 * Executes the function immediately if process context is available,
2940 * otherwise schedules the function for delayed execution.
2941 *
2942 * Returns: 0 - function was executed
2943 * 1 - function was scheduled for execution
2944 */
David Howells65f27f32006-11-22 14:55:48 +00002945int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06002946{
2947 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00002948 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002949 return 0;
2950 }
2951
David Howells65f27f32006-11-22 14:55:48 +00002952 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002953 schedule_work(&ew->work);
2954
2955 return 1;
2956}
2957EXPORT_SYMBOL_GPL(execute_in_process_context);
2958
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959int keventd_up(void)
2960{
Tejun Heod320c032010-06-29 10:07:14 +02002961 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962}
2963
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002964static int alloc_cwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965{
Oleg Nesterov3af244332007-05-09 02:34:09 -07002966 /*
Tejun Heo0f900042010-06-29 10:07:11 +02002967 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
2968 * Make sure that the alignment isn't lower than that of
2969 * unsigned long long.
Oleg Nesterov3af244332007-05-09 02:34:09 -07002970 */
Tejun Heo0f900042010-06-29 10:07:11 +02002971 const size_t size = sizeof(struct cpu_workqueue_struct);
2972 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
2973 __alignof__(unsigned long long));
Oleg Nesterov3af244332007-05-09 02:34:09 -07002974
Lai Jiangshane06ffa12012-03-09 18:03:20 +08002975 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02002976 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
Tejun Heo931ac772010-07-20 11:07:48 +02002977 else {
Tejun Heof3421792010-07-02 10:03:51 +02002978 void *ptr;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01002979
Tejun Heof3421792010-07-02 10:03:51 +02002980 /*
2981 * Allocate enough room to align cwq and put an extra
2982 * pointer at the end pointing back to the originally
2983 * allocated pointer which will be used for free.
2984 */
2985 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
2986 if (ptr) {
2987 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
2988 *(void **)(wq->cpu_wq.single + 1) = ptr;
2989 }
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002990 }
Tejun Heof3421792010-07-02 10:03:51 +02002991
Tejun Heo0415b002011-03-24 18:50:09 +01002992 /* just in case, make sure it's actually aligned */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002993 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
2994 return wq->cpu_wq.v ? 0 : -ENOMEM;
Oleg Nesterov3af244332007-05-09 02:34:09 -07002995}
2996
Tejun Heobdbc5dd2010-07-02 10:03:51 +02002997static void free_cwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07002998{
Lai Jiangshane06ffa12012-03-09 18:03:20 +08002999 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02003000 free_percpu(wq->cpu_wq.pcpu);
3001 else if (wq->cpu_wq.single) {
3002 /* the pointer to free is stored right after the cwq */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003003 kfree(*(void **)(wq->cpu_wq.single + 1));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07003004 }
3005}
3006
Tejun Heof3421792010-07-02 10:03:51 +02003007static int wq_clamp_max_active(int max_active, unsigned int flags,
3008 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003009{
Tejun Heof3421792010-07-02 10:03:51 +02003010 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3011
3012 if (max_active < 1 || max_active > lim)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003013 printk(KERN_WARNING "workqueue: max_active %d requested for %s "
3014 "is out of range, clamping between %d and %d\n",
Tejun Heof3421792010-07-02 10:03:51 +02003015 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003016
Tejun Heof3421792010-07-02 10:03:51 +02003017 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003018}
3019
Tejun Heob196be82012-01-10 15:11:35 -08003020struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02003021 unsigned int flags,
3022 int max_active,
3023 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08003024 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003025{
Tejun Heob196be82012-01-10 15:11:35 -08003026 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003027 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02003028 unsigned int cpu;
shumash7f490b22015-10-06 09:49:52 -06003029 size_t namelen;
Tejun Heob196be82012-01-10 15:11:35 -08003030
Viresh Kumar154e3112013-04-08 16:45:40 +05303031 /* see the comment above the definition of WQ_POWER_EFFICIENT */
3032 if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient)
3033 flags |= WQ_UNBOUND;
3034
Tejun Heob196be82012-01-10 15:11:35 -08003035 /* determine namelen, allocate wq and format name */
3036 va_start(args, lock_name);
3037 va_copy(args1, args);
3038 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3039
3040 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3041 if (!wq)
3042 goto err;
3043
3044 vsnprintf(wq->name, namelen, fmt, args1);
3045 va_end(args);
3046 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003047
Tejun Heof3421792010-07-02 10:03:51 +02003048 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02003049 * Workqueues which may be used during memory reclaim should
3050 * have a rescuer to guarantee forward progress.
3051 */
3052 if (flags & WQ_MEM_RECLAIM)
3053 flags |= WQ_RESCUER;
3054
Tejun Heod320c032010-06-29 10:07:14 +02003055 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003056 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003057
Tejun Heob196be82012-01-10 15:11:35 -08003058 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003059 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003060 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003061 mutex_init(&wq->flush_mutex);
3062 atomic_set(&wq->nr_cwqs_to_flush, 0);
3063 INIT_LIST_HEAD(&wq->flusher_queue);
3064 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003065
Johannes Bergeb13ba82008-01-16 09:51:58 +01003066 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003067 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003068
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003069 if (alloc_cwqs(wq) < 0)
3070 goto err;
3071
Tejun Heof3421792010-07-02 10:03:51 +02003072 for_each_cwq_cpu(cpu, wq) {
Tejun Heo15376632010-06-29 10:07:11 +02003073 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003074 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heodcb32ee2012-07-13 22:16:45 -07003075 int pool_idx = (bool)(flags & WQ_HIGHPRI);
Tejun Heo15376632010-06-29 10:07:11 +02003076
Tejun Heo0f900042010-06-29 10:07:11 +02003077 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heodcb32ee2012-07-13 22:16:45 -07003078 cwq->pool = &gcwq->pools[pool_idx];
Tejun Heoc34056a2010-06-29 10:07:11 +02003079 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02003080 cwq->flush_color = -1;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003081 cwq->max_active = max_active;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003082 INIT_LIST_HEAD(&cwq->delayed_works);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003083 }
3084
Tejun Heoe22bee72010-06-29 10:07:14 +02003085 if (flags & WQ_RESCUER) {
3086 struct worker *rescuer;
3087
Tejun Heof2e005a2010-07-20 15:59:09 +02003088 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
Tejun Heoe22bee72010-06-29 10:07:14 +02003089 goto err;
3090
3091 wq->rescuer = rescuer = alloc_worker();
3092 if (!rescuer)
3093 goto err;
3094
Tejun Heob196be82012-01-10 15:11:35 -08003095 rescuer->task = kthread_create(rescuer_thread, wq, "%s",
3096 wq->name);
Tejun Heoe22bee72010-06-29 10:07:14 +02003097 if (IS_ERR(rescuer->task))
3098 goto err;
3099
Tejun Heoe22bee72010-06-29 10:07:14 +02003100 rescuer->task->flags |= PF_THREAD_BOUND;
3101 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003102 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003103
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003104 /*
3105 * workqueue_lock protects global freeze state and workqueues
3106 * list. Grab it, set max_active accordingly and add the new
3107 * workqueue to workqueues list.
3108 */
Tejun Heo15376632010-06-29 10:07:11 +02003109 spin_lock(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003110
Tejun Heo58a69cb2011-02-16 09:25:31 +01003111 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heof3421792010-07-02 10:03:51 +02003112 for_each_cwq_cpu(cpu, wq)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003113 get_cwq(cpu, wq)->max_active = 0;
3114
Tejun Heo15376632010-06-29 10:07:11 +02003115 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003116
Tejun Heo15376632010-06-29 10:07:11 +02003117 spin_unlock(&workqueue_lock);
3118
Oleg Nesterov3af244332007-05-09 02:34:09 -07003119 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02003120err:
3121 if (wq) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003122 free_cwqs(wq);
Tejun Heof2e005a2010-07-20 15:59:09 +02003123 free_mayday_mask(wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02003124 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003125 kfree(wq);
3126 }
3127 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003128}
Tejun Heod320c032010-06-29 10:07:14 +02003129EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003130
3131/**
3132 * destroy_workqueue - safely terminate a workqueue
3133 * @wq: target workqueue
3134 *
3135 * Safely destroy a workqueue. All work currently pending will be done first.
3136 */
3137void destroy_workqueue(struct workqueue_struct *wq)
3138{
Tejun Heoc8e55f32010-06-29 10:07:12 +02003139 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003140
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003141 /* drain it before proceeding with destruction */
3142 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003143
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003144 /*
3145 * wq list is used to freeze wq, remove from list after
3146 * flushing is complete in case freeze races us.
3147 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003148 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07003149 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003150 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003151
Tejun Heoe22bee72010-06-29 10:07:14 +02003152 /* sanity check */
Tejun Heof3421792010-07-02 10:03:51 +02003153 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02003154 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3155 int i;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003156
Tejun Heo73f53c42010-06-29 10:07:11 +02003157 for (i = 0; i < WORK_NR_COLORS; i++)
3158 BUG_ON(cwq->nr_in_flight[i]);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003159 BUG_ON(cwq->nr_active);
3160 BUG_ON(!list_empty(&cwq->delayed_works));
Tejun Heo73f53c42010-06-29 10:07:11 +02003161 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003162
Tejun Heoe22bee72010-06-29 10:07:14 +02003163 if (wq->flags & WQ_RESCUER) {
3164 kthread_stop(wq->rescuer->task);
Tejun Heof2e005a2010-07-20 15:59:09 +02003165 free_mayday_mask(wq->mayday_mask);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003166 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003167 }
3168
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003169 free_cwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003170 kfree(wq);
3171}
3172EXPORT_SYMBOL_GPL(destroy_workqueue);
3173
Tejun Heodcd989c2010-06-29 10:07:14 +02003174/**
3175 * workqueue_set_max_active - adjust max_active of a workqueue
3176 * @wq: target workqueue
3177 * @max_active: new max_active value.
3178 *
3179 * Set max_active of @wq to @max_active.
3180 *
3181 * CONTEXT:
3182 * Don't call from IRQ context.
3183 */
3184void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3185{
3186 unsigned int cpu;
3187
Tejun Heof3421792010-07-02 10:03:51 +02003188 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003189
3190 spin_lock(&workqueue_lock);
3191
3192 wq->saved_max_active = max_active;
3193
Tejun Heof3421792010-07-02 10:03:51 +02003194 for_each_cwq_cpu(cpu, wq) {
Tejun Heodcd989c2010-06-29 10:07:14 +02003195 struct global_cwq *gcwq = get_gcwq(cpu);
3196
3197 spin_lock_irq(&gcwq->lock);
3198
Tejun Heo58a69cb2011-02-16 09:25:31 +01003199 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heodcd989c2010-06-29 10:07:14 +02003200 !(gcwq->flags & GCWQ_FREEZING))
3201 get_cwq(gcwq->cpu, wq)->max_active = max_active;
3202
3203 spin_unlock_irq(&gcwq->lock);
3204 }
3205
3206 spin_unlock(&workqueue_lock);
3207}
3208EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3209
3210/**
3211 * workqueue_congested - test whether a workqueue is congested
3212 * @cpu: CPU in question
3213 * @wq: target workqueue
3214 *
3215 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3216 * no synchronization around this function and the test result is
3217 * unreliable and only useful as advisory hints or for debugging.
3218 *
3219 * RETURNS:
3220 * %true if congested, %false otherwise.
3221 */
3222bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3223{
3224 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3225
3226 return !list_empty(&cwq->delayed_works);
3227}
3228EXPORT_SYMBOL_GPL(workqueue_congested);
3229
3230/**
3231 * work_cpu - return the last known associated cpu for @work
3232 * @work: the work of interest
3233 *
3234 * RETURNS:
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003235 * CPU number if @work was ever queued. WORK_CPU_NONE otherwise.
Tejun Heodcd989c2010-06-29 10:07:14 +02003236 */
3237unsigned int work_cpu(struct work_struct *work)
3238{
3239 struct global_cwq *gcwq = get_work_gcwq(work);
3240
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003241 return gcwq ? gcwq->cpu : WORK_CPU_NONE;
Tejun Heodcd989c2010-06-29 10:07:14 +02003242}
3243EXPORT_SYMBOL_GPL(work_cpu);
3244
3245/**
3246 * work_busy - test whether a work is currently pending or running
3247 * @work: the work to be tested
3248 *
3249 * Test whether @work is currently pending or running. There is no
3250 * synchronization around this function and the test result is
3251 * unreliable and only useful as advisory hints or for debugging.
3252 * Especially for reentrant wqs, the pending state might hide the
3253 * running state.
3254 *
3255 * RETURNS:
3256 * OR'd bitmask of WORK_BUSY_* bits.
3257 */
3258unsigned int work_busy(struct work_struct *work)
3259{
3260 struct global_cwq *gcwq = get_work_gcwq(work);
3261 unsigned long flags;
3262 unsigned int ret = 0;
3263
3264 if (!gcwq)
3265 return false;
3266
3267 spin_lock_irqsave(&gcwq->lock, flags);
3268
3269 if (work_pending(work))
3270 ret |= WORK_BUSY_PENDING;
3271 if (find_worker_executing_work(gcwq, work))
3272 ret |= WORK_BUSY_RUNNING;
3273
3274 spin_unlock_irqrestore(&gcwq->lock, flags);
3275
3276 return ret;
3277}
3278EXPORT_SYMBOL_GPL(work_busy);
3279
Tejun Heodb7bccf2010-06-29 10:07:12 +02003280/*
3281 * CPU hotplug.
3282 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003283 * There are two challenges in supporting CPU hotplug. Firstly, there
3284 * are a lot of assumptions on strong associations among work, cwq and
3285 * gcwq which make migrating pending and scheduled works very
3286 * difficult to implement without impacting hot paths. Secondly,
3287 * gcwqs serve mix of short, long and very long running works making
3288 * blocked draining impractical.
3289 *
3290 * This is solved by allowing a gcwq to be detached from CPU, running
3291 * it with unbound (rogue) workers and allowing it to be reattached
3292 * later if the cpu comes back online. A separate thread is created
3293 * to govern a gcwq in such state and is called the trustee of the
3294 * gcwq.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003295 *
3296 * Trustee states and their descriptions.
3297 *
3298 * START Command state used on startup. On CPU_DOWN_PREPARE, a
3299 * new trustee is started with this state.
3300 *
3301 * IN_CHARGE Once started, trustee will enter this state after
Tejun Heoe22bee72010-06-29 10:07:14 +02003302 * assuming the manager role and making all existing
3303 * workers rogue. DOWN_PREPARE waits for trustee to
3304 * enter this state. After reaching IN_CHARGE, trustee
3305 * tries to execute the pending worklist until it's empty
3306 * and the state is set to BUTCHER, or the state is set
3307 * to RELEASE.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003308 *
3309 * BUTCHER Command state which is set by the cpu callback after
3310 * the cpu has went down. Once this state is set trustee
3311 * knows that there will be no new works on the worklist
3312 * and once the worklist is empty it can proceed to
3313 * killing idle workers.
3314 *
3315 * RELEASE Command state which is set by the cpu callback if the
3316 * cpu down has been canceled or it has come online
3317 * again. After recognizing this state, trustee stops
Tejun Heoe22bee72010-06-29 10:07:14 +02003318 * trying to drain or butcher and clears ROGUE, rebinds
3319 * all remaining workers back to the cpu and releases
3320 * manager role.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003321 *
3322 * DONE Trustee will enter this state after BUTCHER or RELEASE
3323 * is complete.
3324 *
3325 * trustee CPU draining
3326 * took over down complete
3327 * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE
3328 * | | ^
3329 * | CPU is back online v return workers |
3330 * ----------------> RELEASE --------------
3331 */
3332
3333/**
3334 * trustee_wait_event_timeout - timed event wait for trustee
3335 * @cond: condition to wait for
3336 * @timeout: timeout in jiffies
3337 *
3338 * wait_event_timeout() for trustee to use. Handles locking and
3339 * checks for RELEASE request.
3340 *
3341 * CONTEXT:
3342 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3343 * multiple times. To be used by trustee.
3344 *
3345 * RETURNS:
3346 * Positive indicating left time if @cond is satisfied, 0 if timed
3347 * out, -1 if canceled.
3348 */
3349#define trustee_wait_event_timeout(cond, timeout) ({ \
3350 long __ret = (timeout); \
3351 while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) && \
3352 __ret) { \
3353 spin_unlock_irq(&gcwq->lock); \
3354 __wait_event_timeout(gcwq->trustee_wait, (cond) || \
3355 (gcwq->trustee_state == TRUSTEE_RELEASE), \
3356 __ret); \
3357 spin_lock_irq(&gcwq->lock); \
3358 } \
3359 gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret); \
3360})
3361
3362/**
3363 * trustee_wait_event - event wait for trustee
3364 * @cond: condition to wait for
3365 *
3366 * wait_event() for trustee to use. Automatically handles locking and
3367 * checks for CANCEL request.
3368 *
3369 * CONTEXT:
3370 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3371 * multiple times. To be used by trustee.
3372 *
3373 * RETURNS:
3374 * 0 if @cond is satisfied, -1 if canceled.
3375 */
3376#define trustee_wait_event(cond) ({ \
3377 long __ret1; \
3378 __ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\
3379 __ret1 < 0 ? -1 : 0; \
3380})
3381
Tejun Heo9c6bae02012-07-13 22:16:44 -07003382static bool gcwq_is_managing_workers(struct global_cwq *gcwq)
3383{
3384 struct worker_pool *pool;
3385
3386 for_each_worker_pool(pool, gcwq)
3387 if (pool->flags & POOL_MANAGING_WORKERS)
3388 return true;
3389 return false;
3390}
3391
3392static bool gcwq_has_idle_workers(struct global_cwq *gcwq)
3393{
3394 struct worker_pool *pool;
3395
3396 for_each_worker_pool(pool, gcwq)
3397 if (!list_empty(&pool->idle_list))
3398 return true;
3399 return false;
3400}
3401
Tejun Heodb7bccf2010-06-29 10:07:12 +02003402static int __cpuinit trustee_thread(void *__gcwq)
3403{
3404 struct global_cwq *gcwq = __gcwq;
Tejun Heo9c6bae02012-07-13 22:16:44 -07003405 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003406 struct worker *worker;
Tejun Heoe22bee72010-06-29 10:07:14 +02003407 struct work_struct *work;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003408 struct hlist_node *pos;
Tejun Heoe22bee72010-06-29 10:07:14 +02003409 long rc;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003410 int i;
3411
3412 BUG_ON(gcwq->cpu != smp_processor_id());
3413
3414 spin_lock_irq(&gcwq->lock);
3415 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003416 * Claim the manager position and make all workers rogue.
3417 * Trustee must be bound to the target cpu and can't be
3418 * cancelled.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003419 */
3420 BUG_ON(gcwq->cpu != smp_processor_id());
Tejun Heo9c6bae02012-07-13 22:16:44 -07003421 rc = trustee_wait_event(!gcwq_is_managing_workers(gcwq));
Tejun Heoe22bee72010-06-29 10:07:14 +02003422 BUG_ON(rc < 0);
3423
Tejun Heo9c6bae02012-07-13 22:16:44 -07003424 for_each_worker_pool(pool, gcwq) {
3425 pool->flags |= POOL_MANAGING_WORKERS;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003426
Tejun Heo9c6bae02012-07-13 22:16:44 -07003427 list_for_each_entry(worker, &pool->idle_list, entry)
3428 worker->flags |= WORKER_ROGUE;
3429 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003430
3431 for_each_busy_worker(worker, i, pos, gcwq)
Tejun Heocb444762010-07-02 10:03:50 +02003432 worker->flags |= WORKER_ROGUE;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003433
3434 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003435 * Call schedule() so that we cross rq->lock and thus can
3436 * guarantee sched callbacks see the rogue flag. This is
3437 * necessary as scheduler callbacks may be invoked from other
3438 * cpus.
3439 */
3440 spin_unlock_irq(&gcwq->lock);
3441 schedule();
3442 spin_lock_irq(&gcwq->lock);
3443
3444 /*
Tejun Heocb444762010-07-02 10:03:50 +02003445 * Sched callbacks are disabled now. Zap nr_running. After
3446 * this, nr_running stays zero and need_more_worker() and
3447 * keep_working() are always true as long as the worklist is
3448 * not empty.
Tejun Heoe22bee72010-06-29 10:07:14 +02003449 */
Tejun Heo9c6bae02012-07-13 22:16:44 -07003450 for_each_worker_pool(pool, gcwq)
3451 atomic_set(get_pool_nr_running(pool), 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003452
3453 spin_unlock_irq(&gcwq->lock);
Tejun Heo9c6bae02012-07-13 22:16:44 -07003454 for_each_worker_pool(pool, gcwq)
3455 del_timer_sync(&pool->idle_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003456 spin_lock_irq(&gcwq->lock);
3457
3458 /*
Tejun Heodb7bccf2010-06-29 10:07:12 +02003459 * We're now in charge. Notify and proceed to drain. We need
3460 * to keep the gcwq running during the whole CPU down
3461 * procedure as other cpu hotunplug callbacks may need to
3462 * flush currently running tasks.
3463 */
3464 gcwq->trustee_state = TRUSTEE_IN_CHARGE;
3465 wake_up_all(&gcwq->trustee_wait);
3466
3467 /*
3468 * The original cpu is in the process of dying and may go away
3469 * anytime now. When that happens, we and all workers would
Tejun Heoe22bee72010-06-29 10:07:14 +02003470 * be migrated to other cpus. Try draining any left work. We
3471 * want to get it over with ASAP - spam rescuers, wake up as
3472 * many idlers as necessary and create new ones till the
3473 * worklist is empty. Note that if the gcwq is frozen, there
Tejun Heo58a69cb2011-02-16 09:25:31 +01003474 * may be frozen works in freezable cwqs. Don't declare
Tejun Heoe22bee72010-06-29 10:07:14 +02003475 * completion while frozen.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003476 */
Tejun Heo9c6bae02012-07-13 22:16:44 -07003477 while (true) {
3478 bool busy = false;
Tejun Heoe22bee72010-06-29 10:07:14 +02003479
Tejun Heo9c6bae02012-07-13 22:16:44 -07003480 for_each_worker_pool(pool, gcwq)
3481 busy |= pool->nr_workers != pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +02003482
Tejun Heo9c6bae02012-07-13 22:16:44 -07003483 if (!busy && !(gcwq->flags & GCWQ_FREEZING) &&
3484 gcwq->trustee_state != TRUSTEE_IN_CHARGE)
3485 break;
Tejun Heoe22bee72010-06-29 10:07:14 +02003486
Tejun Heo9c6bae02012-07-13 22:16:44 -07003487 for_each_worker_pool(pool, gcwq) {
3488 int nr_works = 0;
3489
3490 list_for_each_entry(work, &pool->worklist, entry) {
3491 send_mayday(work);
3492 nr_works++;
3493 }
3494
3495 list_for_each_entry(worker, &pool->idle_list, entry) {
3496 if (!nr_works--)
3497 break;
3498 wake_up_process(worker->task);
3499 }
3500
3501 if (need_to_create_worker(pool)) {
3502 spin_unlock_irq(&gcwq->lock);
3503 worker = create_worker(pool, false);
3504 spin_lock_irq(&gcwq->lock);
3505 if (worker) {
3506 worker->flags |= WORKER_ROGUE;
3507 start_worker(worker);
3508 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003509 }
3510 }
3511
Tejun Heodb7bccf2010-06-29 10:07:12 +02003512 /* give a breather */
3513 if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN) < 0)
3514 break;
3515 }
3516
Tejun Heoe22bee72010-06-29 10:07:14 +02003517 /*
3518 * Either all works have been scheduled and cpu is down, or
3519 * cpu down has already been canceled. Wait for and butcher
3520 * all workers till we're canceled.
3521 */
3522 do {
Tejun Heo9c6bae02012-07-13 22:16:44 -07003523 rc = trustee_wait_event(gcwq_has_idle_workers(gcwq));
3524
3525 i = 0;
3526 for_each_worker_pool(pool, gcwq) {
3527 while (!list_empty(&pool->idle_list)) {
3528 worker = list_first_entry(&pool->idle_list,
3529 struct worker, entry);
3530 destroy_worker(worker);
3531 }
3532 i |= pool->nr_workers;
3533 }
3534 } while (i && rc >= 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003535
3536 /*
3537 * At this point, either draining has completed and no worker
3538 * is left, or cpu down has been canceled or the cpu is being
3539 * brought back up. There shouldn't be any idle one left.
3540 * Tell the remaining busy ones to rebind once it finishes the
3541 * currently scheduled works by scheduling the rebind_work.
3542 */
Tejun Heo9c6bae02012-07-13 22:16:44 -07003543 for_each_worker_pool(pool, gcwq)
3544 WARN_ON(!list_empty(&pool->idle_list));
Tejun Heoe22bee72010-06-29 10:07:14 +02003545
3546 for_each_busy_worker(worker, i, pos, gcwq) {
3547 struct work_struct *rebind_work = &worker->rebind_work;
Lai Jiangshan6adebb02012-09-02 00:28:19 +08003548 unsigned long worker_flags = worker->flags;
Tejun Heoe22bee72010-06-29 10:07:14 +02003549
3550 /*
3551 * Rebind_work may race with future cpu hotplug
3552 * operations. Use a separate flag to mark that
Lai Jiangshan6adebb02012-09-02 00:28:19 +08003553 * rebinding is scheduled. The morphing should
3554 * be atomic.
Tejun Heoe22bee72010-06-29 10:07:14 +02003555 */
Lai Jiangshan6adebb02012-09-02 00:28:19 +08003556 worker_flags |= WORKER_REBIND;
3557 worker_flags &= ~WORKER_ROGUE;
3558 ACCESS_ONCE(worker->flags) = worker_flags;
Tejun Heoe22bee72010-06-29 10:07:14 +02003559
3560 /* queue rebind_work, wq doesn't matter, use the default one */
3561 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
3562 work_data_bits(rebind_work)))
3563 continue;
3564
3565 debug_work_activate(rebind_work);
Tejun Heod320c032010-06-29 10:07:14 +02003566 insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work,
Tejun Heoe22bee72010-06-29 10:07:14 +02003567 worker->scheduled.next,
3568 work_color_to_flags(WORK_NO_COLOR));
3569 }
3570
3571 /* relinquish manager role */
Tejun Heo9c6bae02012-07-13 22:16:44 -07003572 for_each_worker_pool(pool, gcwq)
3573 pool->flags &= ~POOL_MANAGING_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02003574
Tejun Heodb7bccf2010-06-29 10:07:12 +02003575 /* notify completion */
3576 gcwq->trustee = NULL;
3577 gcwq->trustee_state = TRUSTEE_DONE;
3578 wake_up_all(&gcwq->trustee_wait);
3579 spin_unlock_irq(&gcwq->lock);
3580 return 0;
3581}
3582
3583/**
3584 * wait_trustee_state - wait for trustee to enter the specified state
3585 * @gcwq: gcwq the trustee of interest belongs to
3586 * @state: target state to wait for
3587 *
3588 * Wait for the trustee to reach @state. DONE is already matched.
3589 *
3590 * CONTEXT:
3591 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3592 * multiple times. To be used by cpu_callback.
3593 */
3594static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09003595__releases(&gcwq->lock)
3596__acquires(&gcwq->lock)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003597{
3598 if (!(gcwq->trustee_state == state ||
3599 gcwq->trustee_state == TRUSTEE_DONE)) {
3600 spin_unlock_irq(&gcwq->lock);
3601 __wait_event(gcwq->trustee_wait,
3602 gcwq->trustee_state == state ||
3603 gcwq->trustee_state == TRUSTEE_DONE);
3604 spin_lock_irq(&gcwq->lock);
3605 }
3606}
3607
Oleg Nesterov3af244332007-05-09 02:34:09 -07003608static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
3609 unsigned long action,
3610 void *hcpu)
3611{
3612 unsigned int cpu = (unsigned long)hcpu;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003613 struct global_cwq *gcwq = get_gcwq(cpu);
3614 struct task_struct *new_trustee = NULL;
Tejun Heo9c6bae02012-07-13 22:16:44 -07003615 struct worker *new_workers[NR_WORKER_POOLS] = { };
3616 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003617 unsigned long flags;
Tejun Heo9c6bae02012-07-13 22:16:44 -07003618 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003620 action &= ~CPU_TASKS_FROZEN;
3621
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003623 case CPU_DOWN_PREPARE:
3624 new_trustee = kthread_create(trustee_thread, gcwq,
3625 "workqueue_trustee/%d\n", cpu);
3626 if (IS_ERR(new_trustee))
3627 return notifier_from_errno(PTR_ERR(new_trustee));
3628 kthread_bind(new_trustee, cpu);
Tejun Heoe22bee72010-06-29 10:07:14 +02003629 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 case CPU_UP_PREPARE:
Tejun Heo9c6bae02012-07-13 22:16:44 -07003631 i = 0;
3632 for_each_worker_pool(pool, gcwq) {
3633 BUG_ON(pool->first_idle);
3634 new_workers[i] = create_worker(pool, false);
3635 if (!new_workers[i++])
3636 goto err_destroy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 }
3639
Tejun Heodb7bccf2010-06-29 10:07:12 +02003640 /* some are called w/ irq disabled, don't disturb irq status */
3641 spin_lock_irqsave(&gcwq->lock, flags);
3642
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003643 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003644 case CPU_DOWN_PREPARE:
3645 /* initialize trustee and tell it to acquire the gcwq */
3646 BUG_ON(gcwq->trustee || gcwq->trustee_state != TRUSTEE_DONE);
3647 gcwq->trustee = new_trustee;
3648 gcwq->trustee_state = TRUSTEE_START;
3649 wake_up_process(gcwq->trustee);
3650 wait_trustee_state(gcwq, TRUSTEE_IN_CHARGE);
Tejun Heoe22bee72010-06-29 10:07:14 +02003651 /* fall through */
3652 case CPU_UP_PREPARE:
Tejun Heo9c6bae02012-07-13 22:16:44 -07003653 i = 0;
3654 for_each_worker_pool(pool, gcwq) {
3655 BUG_ON(pool->first_idle);
3656 pool->first_idle = new_workers[i++];
3657 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003658 break;
3659
3660 case CPU_DYING:
3661 /*
3662 * Before this, the trustee and all workers except for
3663 * the ones which are still executing works from
3664 * before the last CPU down must be on the cpu. After
3665 * this, they'll all be diasporas.
3666 */
3667 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003668 break;
3669
Oleg Nesterov3da1c842008-07-25 01:47:50 -07003670 case CPU_POST_DEAD:
Tejun Heodb7bccf2010-06-29 10:07:12 +02003671 gcwq->trustee_state = TRUSTEE_BUTCHER;
Tejun Heoe22bee72010-06-29 10:07:14 +02003672 /* fall through */
3673 case CPU_UP_CANCELED:
Tejun Heo9c6bae02012-07-13 22:16:44 -07003674 for_each_worker_pool(pool, gcwq) {
3675 destroy_worker(pool->first_idle);
3676 pool->first_idle = NULL;
3677 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003678 break;
3679
3680 case CPU_DOWN_FAILED:
3681 case CPU_ONLINE:
Tejun Heoe22bee72010-06-29 10:07:14 +02003682 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003683 if (gcwq->trustee_state != TRUSTEE_DONE) {
3684 gcwq->trustee_state = TRUSTEE_RELEASE;
3685 wake_up_process(gcwq->trustee);
3686 wait_trustee_state(gcwq, TRUSTEE_DONE);
3687 }
3688
Tejun Heoe22bee72010-06-29 10:07:14 +02003689 /*
3690 * Trustee is done and there might be no worker left.
3691 * Put the first_idle in and request a real manager to
3692 * take a look.
3693 */
Tejun Heo9c6bae02012-07-13 22:16:44 -07003694 for_each_worker_pool(pool, gcwq) {
3695 spin_unlock_irq(&gcwq->lock);
3696 kthread_bind(pool->first_idle->task, cpu);
3697 spin_lock_irq(&gcwq->lock);
3698 pool->flags |= POOL_MANAGE_WORKERS;
3699 start_worker(pool->first_idle);
3700 pool->first_idle = NULL;
3701 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003702 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003703 }
3704
Tejun Heodb7bccf2010-06-29 10:07:12 +02003705 spin_unlock_irqrestore(&gcwq->lock, flags);
3706
Tejun Heo15376632010-06-29 10:07:11 +02003707 return notifier_from_errno(0);
Tejun Heo9c6bae02012-07-13 22:16:44 -07003708
3709err_destroy:
3710 if (new_trustee)
3711 kthread_stop(new_trustee);
3712
3713 spin_lock_irqsave(&gcwq->lock, flags);
3714 for (i = 0; i < NR_WORKER_POOLS; i++)
3715 if (new_workers[i])
3716 destroy_worker(new_workers[i]);
3717 spin_unlock_irqrestore(&gcwq->lock, flags);
3718
3719 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721
Tejun Heod3b42542012-07-17 12:39:26 -07003722/*
3723 * Workqueues should be brought up before normal priority CPU notifiers.
3724 * This will be registered high priority CPU notifier.
3725 */
3726static int __devinit workqueue_cpu_up_callback(struct notifier_block *nfb,
3727 unsigned long action,
3728 void *hcpu)
3729{
3730 switch (action & ~CPU_TASKS_FROZEN) {
3731 case CPU_UP_PREPARE:
3732 case CPU_UP_CANCELED:
3733 case CPU_DOWN_FAILED:
3734 case CPU_ONLINE:
3735 return workqueue_cpu_callback(nfb, action, hcpu);
3736 }
3737 return NOTIFY_OK;
3738}
3739
3740/*
3741 * Workqueues should be brought down after normal priority CPU notifiers.
3742 * This will be registered as low priority CPU notifier.
3743 */
3744static int __devinit workqueue_cpu_down_callback(struct notifier_block *nfb,
3745 unsigned long action,
3746 void *hcpu)
3747{
3748 switch (action & ~CPU_TASKS_FROZEN) {
3749 case CPU_DOWN_PREPARE:
3750 case CPU_DYING:
3751 case CPU_POST_DEAD:
3752 return workqueue_cpu_callback(nfb, action, hcpu);
3753 }
3754 return NOTIFY_OK;
3755}
3756
Rusty Russell2d3854a2008-11-05 13:39:10 +11003757#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003758
Rusty Russell2d3854a2008-11-05 13:39:10 +11003759struct work_for_cpu {
Tejun Heofc7da7e2012-09-18 12:48:43 -07003760 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003761 long (*fn)(void *);
3762 void *arg;
3763 long ret;
3764};
3765
Tejun Heofc7da7e2012-09-18 12:48:43 -07003766static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003767{
Tejun Heofc7da7e2012-09-18 12:48:43 -07003768 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
3769
Rusty Russell2d3854a2008-11-05 13:39:10 +11003770 wfc->ret = wfc->fn(wfc->arg);
3771}
3772
3773/**
3774 * work_on_cpu - run a function in user context on a particular cpu
3775 * @cpu: the cpu to run on
3776 * @fn: the function to run
3777 * @arg: the function arg
3778 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003779 * This will return the value @fn returns.
3780 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06003781 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003782 */
3783long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3784{
Tejun Heofc7da7e2012-09-18 12:48:43 -07003785 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003786
Tejun Heofc7da7e2012-09-18 12:48:43 -07003787 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
3788 schedule_work_on(cpu, &wfc.work);
3789 flush_work(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003790 return wfc.ret;
3791}
3792EXPORT_SYMBOL_GPL(work_on_cpu);
3793#endif /* CONFIG_SMP */
3794
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003795#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303796
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003797/**
3798 * freeze_workqueues_begin - begin freezing workqueues
3799 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003800 * Start freezing workqueues. After this function returns, all freezable
3801 * workqueues will queue new works to their frozen_works list instead of
3802 * gcwq->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003803 *
3804 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003805 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003806 */
3807void freeze_workqueues_begin(void)
3808{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003809 unsigned int cpu;
3810
3811 spin_lock(&workqueue_lock);
3812
3813 BUG_ON(workqueue_freezing);
3814 workqueue_freezing = true;
3815
Tejun Heof3421792010-07-02 10:03:51 +02003816 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003817 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003818 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003819
3820 spin_lock_irq(&gcwq->lock);
3821
Tejun Heodb7bccf2010-06-29 10:07:12 +02003822 BUG_ON(gcwq->flags & GCWQ_FREEZING);
3823 gcwq->flags |= GCWQ_FREEZING;
3824
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003825 list_for_each_entry(wq, &workqueues, list) {
3826 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3827
Tejun Heo58a69cb2011-02-16 09:25:31 +01003828 if (cwq && wq->flags & WQ_FREEZABLE)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003829 cwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003830 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003831
3832 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003833 }
3834
3835 spin_unlock(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003837
3838/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003839 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003840 *
3841 * Check whether freezing is complete. This function must be called
3842 * between freeze_workqueues_begin() and thaw_workqueues().
3843 *
3844 * CONTEXT:
3845 * Grabs and releases workqueue_lock.
3846 *
3847 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01003848 * %true if some freezable workqueues are still busy. %false if freezing
3849 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003850 */
3851bool freeze_workqueues_busy(void)
3852{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003853 unsigned int cpu;
3854 bool busy = false;
3855
3856 spin_lock(&workqueue_lock);
3857
3858 BUG_ON(!workqueue_freezing);
3859
Tejun Heof3421792010-07-02 10:03:51 +02003860 for_each_gcwq_cpu(cpu) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003861 struct workqueue_struct *wq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003862 /*
3863 * nr_active is monotonically decreasing. It's safe
3864 * to peek without lock.
3865 */
3866 list_for_each_entry(wq, &workqueues, list) {
3867 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3868
Tejun Heo58a69cb2011-02-16 09:25:31 +01003869 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003870 continue;
3871
3872 BUG_ON(cwq->nr_active < 0);
3873 if (cwq->nr_active) {
3874 busy = true;
3875 goto out_unlock;
3876 }
3877 }
3878 }
3879out_unlock:
3880 spin_unlock(&workqueue_lock);
3881 return busy;
3882}
3883
3884/**
3885 * thaw_workqueues - thaw workqueues
3886 *
3887 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo7e116292010-06-29 10:07:13 +02003888 * frozen works are transferred to their respective gcwq worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003889 *
3890 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003891 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003892 */
3893void thaw_workqueues(void)
3894{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003895 unsigned int cpu;
3896
3897 spin_lock(&workqueue_lock);
3898
3899 if (!workqueue_freezing)
3900 goto out_unlock;
3901
Tejun Heof3421792010-07-02 10:03:51 +02003902 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003903 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo9c6bae02012-07-13 22:16:44 -07003904 struct worker_pool *pool;
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003905 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003906
3907 spin_lock_irq(&gcwq->lock);
3908
Tejun Heodb7bccf2010-06-29 10:07:12 +02003909 BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
3910 gcwq->flags &= ~GCWQ_FREEZING;
3911
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003912 list_for_each_entry(wq, &workqueues, list) {
3913 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3914
Tejun Heo58a69cb2011-02-16 09:25:31 +01003915 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003916 continue;
3917
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003918 /* restore max_active and repopulate worklist */
3919 cwq->max_active = wq->saved_max_active;
3920
3921 while (!list_empty(&cwq->delayed_works) &&
3922 cwq->nr_active < cwq->max_active)
3923 cwq_activate_first_delayed(cwq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003924 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003925
Tejun Heo9c6bae02012-07-13 22:16:44 -07003926 for_each_worker_pool(pool, gcwq)
3927 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02003928
Tejun Heo8b03ae32010-06-29 10:07:12 +02003929 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003930 }
3931
3932 workqueue_freezing = false;
3933out_unlock:
3934 spin_unlock(&workqueue_lock);
3935}
3936#endif /* CONFIG_FREEZER */
3937
Suresh Siddha6ee05782010-07-30 14:57:37 -07003938static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939{
Tejun Heoc34056a2010-06-29 10:07:11 +02003940 unsigned int cpu;
Tejun Heoc8e55f32010-06-29 10:07:12 +02003941 int i;
Tejun Heoc34056a2010-06-29 10:07:11 +02003942
Tejun Heod3b42542012-07-17 12:39:26 -07003943 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
3944 cpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003945
3946 /* initialize gcwqs */
Tejun Heof3421792010-07-02 10:03:51 +02003947 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003948 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo9c6bae02012-07-13 22:16:44 -07003949 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003950
3951 spin_lock_init(&gcwq->lock);
3952 gcwq->cpu = cpu;
Tejun Heo477a3c32010-08-31 10:54:35 +02003953 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003954
Tejun Heoc8e55f32010-06-29 10:07:12 +02003955 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
3956 INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
3957
Tejun Heo9c6bae02012-07-13 22:16:44 -07003958 for_each_worker_pool(pool, gcwq) {
3959 pool->gcwq = gcwq;
3960 INIT_LIST_HEAD(&pool->worklist);
3961 INIT_LIST_HEAD(&pool->idle_list);
Tejun Heoe22bee72010-06-29 10:07:14 +02003962
Tejun Heo9c6bae02012-07-13 22:16:44 -07003963 init_timer_deferrable(&pool->idle_timer);
3964 pool->idle_timer.function = idle_worker_timeout;
3965 pool->idle_timer.data = (unsigned long)pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003966
Tejun Heo9c6bae02012-07-13 22:16:44 -07003967 setup_timer(&pool->mayday_timer, gcwq_mayday_timeout,
3968 (unsigned long)pool);
3969
3970 ida_init(&pool->worker_ida);
3971 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003972
3973 gcwq->trustee_state = TRUSTEE_DONE;
3974 init_waitqueue_head(&gcwq->trustee_wait);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003975 }
3976
Tejun Heoe22bee72010-06-29 10:07:14 +02003977 /* create the initial worker */
Tejun Heof3421792010-07-02 10:03:51 +02003978 for_each_online_gcwq_cpu(cpu) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003979 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo9c6bae02012-07-13 22:16:44 -07003980 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003981
Tejun Heo477a3c32010-08-31 10:54:35 +02003982 if (cpu != WORK_CPU_UNBOUND)
3983 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heo9c6bae02012-07-13 22:16:44 -07003984
3985 for_each_worker_pool(pool, gcwq) {
3986 struct worker *worker;
3987
3988 worker = create_worker(pool, true);
3989 BUG_ON(!worker);
3990 spin_lock_irq(&gcwq->lock);
3991 start_worker(worker);
3992 spin_unlock_irq(&gcwq->lock);
3993 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003994 }
3995
Tejun Heod320c032010-06-29 10:07:14 +02003996 system_wq = alloc_workqueue("events", 0, 0);
3997 system_long_wq = alloc_workqueue("events_long", 0, 0);
3998 system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0);
Tejun Heof3421792010-07-02 10:03:51 +02003999 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
4000 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01004001 system_freezable_wq = alloc_workqueue("events_freezable",
4002 WQ_FREEZABLE, 0);
Alan Stern62d3c542012-03-02 10:51:00 +01004003 system_nrt_freezable_wq = alloc_workqueue("events_nrt_freezable",
4004 WQ_NON_REENTRANT | WQ_FREEZABLE, 0);
Viresh Kumar1adb4b62013-04-24 17:12:54 +05304005 system_power_efficient_wq = alloc_workqueue("events_power_efficient",
4006 WQ_POWER_EFFICIENT, 0);
4007 system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_power_efficient",
4008 WQ_FREEZABLE | WQ_POWER_EFFICIENT, 0);
Hitoshi Mitakee5cba242010-11-26 12:06:44 +01004009 BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq ||
Alan Stern62d3c542012-03-02 10:51:00 +01004010 !system_unbound_wq || !system_freezable_wq ||
Viresh Kumar1adb4b62013-04-24 17:12:54 +05304011 !system_nrt_freezable_wq || !system_power_efficient_wq ||
4012 !system_freezable_power_efficient_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07004013 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014}
Suresh Siddha6ee05782010-07-30 14:57:37 -07004015early_initcall(init_workqueues);