blob: 6927fecae412327863d4f02530edfe377a3e1b39 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tejun Heoc54fce62010-09-10 16:51:36 +02002 * kernel/workqueue.c - generic async execution with shared worker pool
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Tejun Heoc54fce62010-09-10 16:51:36 +02004 * Copyright (C) 2002 Ingo Molnar
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Tejun Heoc54fce62010-09-10 16:51:36 +02006 * Derived from the taskqueue/keventd code by:
7 * David Woodhouse <dwmw2@infradead.org>
8 * Andrew Morton
9 * Kai Petzke <wpp@marie.physik.tu-berlin.de>
10 * Theodore Ts'o <tytso@mit.edu>
Christoph Lameter89ada672005-10-30 15:01:59 -080011 *
Christoph Lametercde53532008-07-04 09:59:22 -070012 * Made to use alloc_percpu by Christoph Lameter.
Tejun Heoc54fce62010-09-10 16:51:36 +020013 *
14 * Copyright (C) 2010 SUSE Linux Products GmbH
15 * Copyright (C) 2010 Tejun Heo <tj@kernel.org>
16 *
17 * This is the generic async execution mechanism. Work items as are
18 * executed in process context. The worker pool is shared and
19 * automatically managed. There is one worker pool for each CPU and
20 * one extra for works which are better served by workers which are
21 * not bound to any specific CPU.
22 *
23 * Please read Documentation/workqueue.txt for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 */
25
Paul Gortmaker9984de12011-05-23 14:51:41 -040026#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/kernel.h>
28#include <linux/sched.h>
29#include <linux/init.h>
30#include <linux/signal.h>
31#include <linux/completion.h>
32#include <linux/workqueue.h>
33#include <linux/slab.h>
34#include <linux/cpu.h>
35#include <linux/notifier.h>
36#include <linux/kthread.h>
James Bottomley1fa44ec2006-02-23 12:43:43 -060037#include <linux/hardirq.h>
Christoph Lameter46934022006-10-11 01:21:26 -070038#include <linux/mempolicy.h>
Rafael J. Wysocki341a5952006-12-06 20:34:49 -080039#include <linux/freezer.h>
Peter Zijlstrad5abe662006-12-06 20:37:26 -080040#include <linux/kallsyms.h>
41#include <linux/debug_locks.h>
Johannes Berg4e6045f2007-10-18 23:39:55 -070042#include <linux/lockdep.h>
Tejun Heoc34056a2010-06-29 10:07:11 +020043#include <linux/idr.h>
Tejun Heoe22bee72010-06-29 10:07:14 +020044
45#include "workqueue_sched.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Tejun Heoc8e55f32010-06-29 10:07:12 +020047enum {
Tejun Heobc2ae0f2012-07-17 12:39:27 -070048 /*
49 * global_cwq flags
50 *
51 * A bound gcwq is either associated or disassociated with its CPU.
52 * While associated (!DISASSOCIATED), all workers are bound to the
53 * CPU and none has %WORKER_UNBOUND set and concurrency management
54 * is in effect.
55 *
56 * While DISASSOCIATED, the cpu may be offline and all workers have
57 * %WORKER_UNBOUND set and concurrency management disabled, and may
58 * be executing on any CPU. The gcwq behaves as an unbound one.
59 *
60 * Note that DISASSOCIATED can be flipped only while holding
61 * managership of all pools on the gcwq to avoid changing binding
62 * state while create_worker() is in progress.
63 */
Tejun Heo11ebea52012-07-12 14:46:37 -070064 GCWQ_DISASSOCIATED = 1 << 0, /* cpu can't serve workers */
65 GCWQ_FREEZING = 1 << 1, /* freeze in progress */
66
67 /* pool flags */
68 POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
Tejun Heodb7bccf2010-06-29 10:07:12 +020069
Tejun Heoc8e55f32010-06-29 10:07:12 +020070 /* worker flags */
71 WORKER_STARTED = 1 << 0, /* started */
72 WORKER_DIE = 1 << 1, /* die die die */
73 WORKER_IDLE = 1 << 2, /* is idle */
Tejun Heoe22bee72010-06-29 10:07:14 +020074 WORKER_PREP = 1 << 3, /* preparing to run works */
Tejun Heoe22bee72010-06-29 10:07:14 +020075 WORKER_REBIND = 1 << 5, /* mom is home, come back */
Tejun Heofb0e7be2010-06-29 10:07:15 +020076 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
Tejun Heof3421792010-07-02 10:03:51 +020077 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
Tejun Heoe22bee72010-06-29 10:07:14 +020078
Tejun Heo403c8212012-07-17 12:39:27 -070079 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_REBIND | WORKER_UNBOUND |
80 WORKER_CPU_INTENSIVE,
Tejun Heodb7bccf2010-06-29 10:07:12 +020081
82 /* gcwq->trustee_state */
83 TRUSTEE_START = 0, /* start */
84 TRUSTEE_IN_CHARGE = 1, /* trustee in charge of gcwq */
85 TRUSTEE_BUTCHER = 2, /* butcher workers */
86 TRUSTEE_RELEASE = 3, /* release workers */
87 TRUSTEE_DONE = 4, /* trustee is done */
Tejun Heoc8e55f32010-06-29 10:07:12 +020088
Tejun Heo32704762012-07-13 22:16:45 -070089 NR_WORKER_POOLS = 2, /* # worker pools per gcwq */
Tejun Heo4ce62e92012-07-13 22:16:44 -070090
Tejun Heoc8e55f32010-06-29 10:07:12 +020091 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
92 BUSY_WORKER_HASH_SIZE = 1 << BUSY_WORKER_HASH_ORDER,
93 BUSY_WORKER_HASH_MASK = BUSY_WORKER_HASH_SIZE - 1,
Tejun Heodb7bccf2010-06-29 10:07:12 +020094
Tejun Heoe22bee72010-06-29 10:07:14 +020095 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
96 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
97
Tejun Heo3233cdb2011-02-16 18:10:19 +010098 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
99 /* call for help after 10ms
100 (min two ticks) */
Tejun Heoe22bee72010-06-29 10:07:14 +0200101 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
102 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200103 TRUSTEE_COOLDOWN = HZ / 10, /* for trustee draining */
Tejun Heoe22bee72010-06-29 10:07:14 +0200104
105 /*
106 * Rescue workers are used only on emergencies and shared by
107 * all cpus. Give -20.
108 */
109 RESCUER_NICE_LEVEL = -20,
Tejun Heo32704762012-07-13 22:16:45 -0700110 HIGHPRI_NICE_LEVEL = -20,
Tejun Heoc8e55f32010-06-29 10:07:12 +0200111};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113/*
Tejun Heo4690c4a2010-06-29 10:07:10 +0200114 * Structure fields follow one of the following exclusion rules.
115 *
Tejun Heoe41e7042010-08-24 14:22:47 +0200116 * I: Modifiable by initialization/destruction paths and read-only for
117 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200118 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200119 * P: Preemption protected. Disabling preemption is enough and should
120 * only be modified and accessed from the local cpu.
121 *
Tejun Heo8b03ae32010-06-29 10:07:12 +0200122 * L: gcwq->lock protected. Access with gcwq->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200123 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200124 * X: During normal operation, modification requires gcwq->lock and
125 * should be done only from local cpu. Either disabling preemption
126 * on local cpu or grabbing gcwq->lock is enough for read access.
Tejun Heof3421792010-07-02 10:03:51 +0200127 * If GCWQ_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200128 *
Tejun Heo73f53c42010-06-29 10:07:11 +0200129 * F: wq->flush_mutex protected.
130 *
Tejun Heo4690c4a2010-06-29 10:07:10 +0200131 * W: workqueue_lock protected.
132 */
133
Tejun Heo8b03ae32010-06-29 10:07:12 +0200134struct global_cwq;
Tejun Heobd7bdd42012-07-12 14:46:37 -0700135struct worker_pool;
Tejun Heo25511a42012-07-17 12:39:27 -0700136struct idle_rebind;
Tejun Heoc34056a2010-06-29 10:07:11 +0200137
Tejun Heoe22bee72010-06-29 10:07:14 +0200138/*
139 * The poor guys doing the actual heavy lifting. All on-duty workers
140 * are either serving the manager role, on idle list or on busy hash.
141 */
Tejun Heoc34056a2010-06-29 10:07:11 +0200142struct worker {
Tejun Heoc8e55f32010-06-29 10:07:12 +0200143 /* on idle list while idle, on busy hash table while busy */
144 union {
145 struct list_head entry; /* L: while idle */
146 struct hlist_node hentry; /* L: while busy */
147 };
148
Tejun Heoc34056a2010-06-29 10:07:11 +0200149 struct work_struct *current_work; /* L: work being processed */
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200150 struct cpu_workqueue_struct *current_cwq; /* L: current_work's cwq */
Tejun Heoaffee4b2010-06-29 10:07:12 +0200151 struct list_head scheduled; /* L: scheduled works */
Tejun Heoc34056a2010-06-29 10:07:11 +0200152 struct task_struct *task; /* I: worker task */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700153 struct worker_pool *pool; /* I: the associated pool */
Tejun Heoe22bee72010-06-29 10:07:14 +0200154 /* 64 bytes boundary on 64bit, 32 on 32bit */
155 unsigned long last_active; /* L: last active timestamp */
156 unsigned int flags; /* X: flags */
Tejun Heoc34056a2010-06-29 10:07:11 +0200157 int id; /* I: worker id */
Tejun Heo25511a42012-07-17 12:39:27 -0700158
159 /* for rebinding worker to CPU */
160 struct idle_rebind *idle_rebind; /* L: for idle worker */
161 struct work_struct rebind_work; /* L: for busy worker */
Tejun Heoc34056a2010-06-29 10:07:11 +0200162};
163
Tejun Heobd7bdd42012-07-12 14:46:37 -0700164struct worker_pool {
165 struct global_cwq *gcwq; /* I: the owning gcwq */
Tejun Heo11ebea52012-07-12 14:46:37 -0700166 unsigned int flags; /* X: flags */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700167
168 struct list_head worklist; /* L: list of pending works */
169 int nr_workers; /* L: total number of workers */
170 int nr_idle; /* L: currently idle ones */
171
172 struct list_head idle_list; /* X: list of idle workers */
173 struct timer_list idle_timer; /* L: worker idle timeout */
174 struct timer_list mayday_timer; /* L: SOS timer for workers */
175
Tejun Heo60373152012-07-17 12:39:27 -0700176 struct mutex manager_mutex; /* mutex manager should hold */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700177 struct ida worker_ida; /* L: for worker IDs */
178 struct worker *first_idle; /* L: first idle worker */
179};
180
Tejun Heo4690c4a2010-06-29 10:07:10 +0200181/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200182 * Global per-cpu workqueue. There's one and only one for each cpu
183 * and all works are queued and processed here regardless of their
184 * target workqueues.
Tejun Heo8b03ae32010-06-29 10:07:12 +0200185 */
186struct global_cwq {
187 spinlock_t lock; /* the gcwq lock */
188 unsigned int cpu; /* I: the associated cpu */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200189 unsigned int flags; /* L: GCWQ_* flags */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200190
Tejun Heobd7bdd42012-07-12 14:46:37 -0700191 /* workers are chained either in busy_hash or pool idle_list */
Tejun Heoc8e55f32010-06-29 10:07:12 +0200192 struct hlist_head busy_hash[BUSY_WORKER_HASH_SIZE];
193 /* L: hash of busy workers */
194
Tejun Heo32704762012-07-13 22:16:45 -0700195 struct worker_pool pools[2]; /* normal and highpri pools */
Tejun Heodb7bccf2010-06-29 10:07:12 +0200196
Tejun Heo25511a42012-07-17 12:39:27 -0700197 wait_queue_head_t rebind_hold; /* rebind hold wait */
198
Tejun Heodb7bccf2010-06-29 10:07:12 +0200199 struct task_struct *trustee; /* L: for gcwq shutdown */
200 unsigned int trustee_state; /* L: trustee state */
201 wait_queue_head_t trustee_wait; /* trustee wait */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200202} ____cacheline_aligned_in_smp;
203
204/*
Tejun Heo502ca9d2010-06-29 10:07:13 +0200205 * The per-CPU workqueue. The lower WORK_STRUCT_FLAG_BITS of
Tejun Heo0f900042010-06-29 10:07:11 +0200206 * work_struct->data are used for flags and thus cwqs need to be
207 * aligned at two's power of the number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 */
209struct cpu_workqueue_struct {
Tejun Heobd7bdd42012-07-12 14:46:37 -0700210 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200211 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200212 int work_color; /* L: current color */
213 int flush_color; /* L: flushing color */
214 int nr_in_flight[WORK_NR_COLORS];
215 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200216 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200217 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200218 struct list_head delayed_works; /* L: delayed works */
Tejun Heo0f900042010-06-29 10:07:11 +0200219};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200222 * Structure used to wait for workqueue flush.
223 */
224struct wq_flusher {
225 struct list_head list; /* F: list of flushers */
226 int flush_color; /* F: flush color waiting for */
227 struct completion done; /* flush completion */
228};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Tejun Heo73f53c42010-06-29 10:07:11 +0200230/*
Tejun Heof2e005a2010-07-20 15:59:09 +0200231 * All cpumasks are assumed to be always set on UP and thus can't be
232 * used to determine whether there's something to be done.
233 */
234#ifdef CONFIG_SMP
235typedef cpumask_var_t mayday_mask_t;
236#define mayday_test_and_set_cpu(cpu, mask) \
237 cpumask_test_and_set_cpu((cpu), (mask))
238#define mayday_clear_cpu(cpu, mask) cpumask_clear_cpu((cpu), (mask))
239#define for_each_mayday_cpu(cpu, mask) for_each_cpu((cpu), (mask))
Tejun Heo9c375472010-08-31 11:18:34 +0200240#define alloc_mayday_mask(maskp, gfp) zalloc_cpumask_var((maskp), (gfp))
Tejun Heof2e005a2010-07-20 15:59:09 +0200241#define free_mayday_mask(mask) free_cpumask_var((mask))
242#else
243typedef unsigned long mayday_mask_t;
244#define mayday_test_and_set_cpu(cpu, mask) test_and_set_bit(0, &(mask))
245#define mayday_clear_cpu(cpu, mask) clear_bit(0, &(mask))
246#define for_each_mayday_cpu(cpu, mask) if ((cpu) = 0, (mask))
247#define alloc_mayday_mask(maskp, gfp) true
248#define free_mayday_mask(mask) do { } while (0)
249#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251/*
252 * The externally visible workqueue abstraction is an array of
253 * per-CPU workqueues:
254 */
255struct workqueue_struct {
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200256 unsigned int flags; /* W: WQ_* flags */
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200257 union {
258 struct cpu_workqueue_struct __percpu *pcpu;
259 struct cpu_workqueue_struct *single;
260 unsigned long v;
261 } cpu_wq; /* I: cwq's */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200262 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200263
264 struct mutex flush_mutex; /* protects wq flushing */
265 int work_color; /* F: current work color */
266 int flush_color; /* F: current flush color */
267 atomic_t nr_cwqs_to_flush; /* flush in progress */
268 struct wq_flusher *first_flusher; /* F: first flusher */
269 struct list_head flusher_queue; /* F: flush waiters */
270 struct list_head flusher_overflow; /* F: flush overflow list */
271
Tejun Heof2e005a2010-07-20 15:59:09 +0200272 mayday_mask_t mayday_mask; /* cpus requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200273 struct worker *rescuer; /* I: rescue worker */
274
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200275 int nr_drainers; /* W: drain in progress */
Tejun Heodcd989c2010-06-29 10:07:14 +0200276 int saved_max_active; /* W: saved cwq max_active */
Johannes Berg4e6045f2007-10-18 23:39:55 -0700277#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200278 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700279#endif
Tejun Heob196be82012-01-10 15:11:35 -0800280 char name[]; /* I: workqueue name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281};
282
Tejun Heod320c032010-06-29 10:07:14 +0200283struct workqueue_struct *system_wq __read_mostly;
284struct workqueue_struct *system_long_wq __read_mostly;
285struct workqueue_struct *system_nrt_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200286struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100287struct workqueue_struct *system_freezable_wq __read_mostly;
Alan Stern62d3c542012-03-02 10:51:00 +0100288struct workqueue_struct *system_nrt_freezable_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200289EXPORT_SYMBOL_GPL(system_wq);
290EXPORT_SYMBOL_GPL(system_long_wq);
291EXPORT_SYMBOL_GPL(system_nrt_wq);
Tejun Heof3421792010-07-02 10:03:51 +0200292EXPORT_SYMBOL_GPL(system_unbound_wq);
Tejun Heo24d51ad2011-02-21 09:52:50 +0100293EXPORT_SYMBOL_GPL(system_freezable_wq);
Alan Stern62d3c542012-03-02 10:51:00 +0100294EXPORT_SYMBOL_GPL(system_nrt_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200295
Tejun Heo97bd2342010-10-05 10:41:14 +0200296#define CREATE_TRACE_POINTS
297#include <trace/events/workqueue.h>
298
Tejun Heo4ce62e92012-07-13 22:16:44 -0700299#define for_each_worker_pool(pool, gcwq) \
Tejun Heo32704762012-07-13 22:16:45 -0700300 for ((pool) = &(gcwq)->pools[0]; \
301 (pool) < &(gcwq)->pools[NR_WORKER_POOLS]; (pool)++)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700302
Tejun Heodb7bccf2010-06-29 10:07:12 +0200303#define for_each_busy_worker(worker, i, pos, gcwq) \
304 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \
305 hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry)
306
Tejun Heof3421792010-07-02 10:03:51 +0200307static inline int __next_gcwq_cpu(int cpu, const struct cpumask *mask,
308 unsigned int sw)
309{
310 if (cpu < nr_cpu_ids) {
311 if (sw & 1) {
312 cpu = cpumask_next(cpu, mask);
313 if (cpu < nr_cpu_ids)
314 return cpu;
315 }
316 if (sw & 2)
317 return WORK_CPU_UNBOUND;
318 }
319 return WORK_CPU_NONE;
320}
321
322static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
323 struct workqueue_struct *wq)
324{
325 return __next_gcwq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2);
326}
327
Tejun Heo09884952010-08-01 11:50:12 +0200328/*
329 * CPU iterators
330 *
331 * An extra gcwq is defined for an invalid cpu number
332 * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any
333 * specific CPU. The following iterators are similar to
334 * for_each_*_cpu() iterators but also considers the unbound gcwq.
335 *
336 * for_each_gcwq_cpu() : possible CPUs + WORK_CPU_UNBOUND
337 * for_each_online_gcwq_cpu() : online CPUs + WORK_CPU_UNBOUND
338 * for_each_cwq_cpu() : possible CPUs for bound workqueues,
339 * WORK_CPU_UNBOUND for unbound workqueues
340 */
Tejun Heof3421792010-07-02 10:03:51 +0200341#define for_each_gcwq_cpu(cpu) \
342 for ((cpu) = __next_gcwq_cpu(-1, cpu_possible_mask, 3); \
343 (cpu) < WORK_CPU_NONE; \
344 (cpu) = __next_gcwq_cpu((cpu), cpu_possible_mask, 3))
345
346#define for_each_online_gcwq_cpu(cpu) \
347 for ((cpu) = __next_gcwq_cpu(-1, cpu_online_mask, 3); \
348 (cpu) < WORK_CPU_NONE; \
349 (cpu) = __next_gcwq_cpu((cpu), cpu_online_mask, 3))
350
351#define for_each_cwq_cpu(cpu, wq) \
352 for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, (wq)); \
353 (cpu) < WORK_CPU_NONE; \
354 (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq)))
355
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900356#ifdef CONFIG_DEBUG_OBJECTS_WORK
357
358static struct debug_obj_descr work_debug_descr;
359
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100360static void *work_debug_hint(void *addr)
361{
362 return ((struct work_struct *) addr)->func;
363}
364
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900365/*
366 * fixup_init is called when:
367 * - an active object is initialized
368 */
369static int work_fixup_init(void *addr, enum debug_obj_state state)
370{
371 struct work_struct *work = addr;
372
373 switch (state) {
374 case ODEBUG_STATE_ACTIVE:
375 cancel_work_sync(work);
376 debug_object_init(work, &work_debug_descr);
377 return 1;
378 default:
379 return 0;
380 }
381}
382
383/*
384 * fixup_activate is called when:
385 * - an active object is activated
386 * - an unknown object is activated (might be a statically initialized object)
387 */
388static int work_fixup_activate(void *addr, enum debug_obj_state state)
389{
390 struct work_struct *work = addr;
391
392 switch (state) {
393
394 case ODEBUG_STATE_NOTAVAILABLE:
395 /*
396 * This is not really a fixup. The work struct was
397 * statically initialized. We just make sure that it
398 * is tracked in the object tracker.
399 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200400 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900401 debug_object_init(work, &work_debug_descr);
402 debug_object_activate(work, &work_debug_descr);
403 return 0;
404 }
405 WARN_ON_ONCE(1);
406 return 0;
407
408 case ODEBUG_STATE_ACTIVE:
409 WARN_ON(1);
410
411 default:
412 return 0;
413 }
414}
415
416/*
417 * fixup_free is called when:
418 * - an active object is freed
419 */
420static int work_fixup_free(void *addr, enum debug_obj_state state)
421{
422 struct work_struct *work = addr;
423
424 switch (state) {
425 case ODEBUG_STATE_ACTIVE:
426 cancel_work_sync(work);
427 debug_object_free(work, &work_debug_descr);
428 return 1;
429 default:
430 return 0;
431 }
432}
433
434static struct debug_obj_descr work_debug_descr = {
435 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100436 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900437 .fixup_init = work_fixup_init,
438 .fixup_activate = work_fixup_activate,
439 .fixup_free = work_fixup_free,
440};
441
442static inline void debug_work_activate(struct work_struct *work)
443{
444 debug_object_activate(work, &work_debug_descr);
445}
446
447static inline void debug_work_deactivate(struct work_struct *work)
448{
449 debug_object_deactivate(work, &work_debug_descr);
450}
451
452void __init_work(struct work_struct *work, int onstack)
453{
454 if (onstack)
455 debug_object_init_on_stack(work, &work_debug_descr);
456 else
457 debug_object_init(work, &work_debug_descr);
458}
459EXPORT_SYMBOL_GPL(__init_work);
460
461void destroy_work_on_stack(struct work_struct *work)
462{
463 debug_object_free(work, &work_debug_descr);
464}
465EXPORT_SYMBOL_GPL(destroy_work_on_stack);
466
467#else
468static inline void debug_work_activate(struct work_struct *work) { }
469static inline void debug_work_deactivate(struct work_struct *work) { }
470#endif
471
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100472/* Serializes the accesses to the list of workqueues. */
473static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474static LIST_HEAD(workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200475static bool workqueue_freezing; /* W: have wqs started freezing? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Oleg Nesterov14441962007-05-23 13:57:57 -0700477/*
Tejun Heoe22bee72010-06-29 10:07:14 +0200478 * The almighty global cpu workqueues. nr_running is the only field
479 * which is expected to be used frequently by other cpus via
480 * try_to_wake_up(). Put it in a separate cacheline.
Oleg Nesterov14441962007-05-23 13:57:57 -0700481 */
Tejun Heo8b03ae32010-06-29 10:07:12 +0200482static DEFINE_PER_CPU(struct global_cwq, global_cwq);
Tejun Heo4ce62e92012-07-13 22:16:44 -0700483static DEFINE_PER_CPU_SHARED_ALIGNED(atomic_t, pool_nr_running[NR_WORKER_POOLS]);
Nathan Lynchf756d5e2006-01-08 01:05:12 -0800484
Tejun Heof3421792010-07-02 10:03:51 +0200485/*
486 * Global cpu workqueue and nr_running counter for unbound gcwq. The
487 * gcwq is always online, has GCWQ_DISASSOCIATED set, and all its
488 * workers have WORKER_UNBOUND set.
489 */
490static struct global_cwq unbound_global_cwq;
Tejun Heo4ce62e92012-07-13 22:16:44 -0700491static atomic_t unbound_pool_nr_running[NR_WORKER_POOLS] = {
492 [0 ... NR_WORKER_POOLS - 1] = ATOMIC_INIT(0), /* always 0 */
493};
Tejun Heof3421792010-07-02 10:03:51 +0200494
Tejun Heoc34056a2010-06-29 10:07:11 +0200495static int worker_thread(void *__worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Tejun Heo32704762012-07-13 22:16:45 -0700497static int worker_pool_pri(struct worker_pool *pool)
498{
499 return pool - pool->gcwq->pools;
500}
501
Tejun Heo8b03ae32010-06-29 10:07:12 +0200502static struct global_cwq *get_gcwq(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Tejun Heof3421792010-07-02 10:03:51 +0200504 if (cpu != WORK_CPU_UNBOUND)
505 return &per_cpu(global_cwq, cpu);
506 else
507 return &unbound_global_cwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
Tejun Heo63d95a92012-07-12 14:46:37 -0700510static atomic_t *get_pool_nr_running(struct worker_pool *pool)
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700511{
Tejun Heo63d95a92012-07-12 14:46:37 -0700512 int cpu = pool->gcwq->cpu;
Tejun Heo32704762012-07-13 22:16:45 -0700513 int idx = worker_pool_pri(pool);
Tejun Heo63d95a92012-07-12 14:46:37 -0700514
Tejun Heof3421792010-07-02 10:03:51 +0200515 if (cpu != WORK_CPU_UNBOUND)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700516 return &per_cpu(pool_nr_running, cpu)[idx];
Tejun Heof3421792010-07-02 10:03:51 +0200517 else
Tejun Heo4ce62e92012-07-13 22:16:44 -0700518 return &unbound_pool_nr_running[idx];
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700519}
520
Tejun Heo4690c4a2010-06-29 10:07:10 +0200521static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
522 struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700523{
Tejun Heof3421792010-07-02 10:03:51 +0200524 if (!(wq->flags & WQ_UNBOUND)) {
Lai Jiangshane06ffa12012-03-09 18:03:20 +0800525 if (likely(cpu < nr_cpu_ids))
Tejun Heof3421792010-07-02 10:03:51 +0200526 return per_cpu_ptr(wq->cpu_wq.pcpu, cpu);
Tejun Heof3421792010-07-02 10:03:51 +0200527 } else if (likely(cpu == WORK_CPU_UNBOUND))
528 return wq->cpu_wq.single;
529 return NULL;
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700530}
531
Tejun Heo73f53c42010-06-29 10:07:11 +0200532static unsigned int work_color_to_flags(int color)
533{
534 return color << WORK_STRUCT_COLOR_SHIFT;
535}
536
537static int get_work_color(struct work_struct *work)
538{
539 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
540 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
541}
542
543static int work_next_color(int color)
544{
545 return (color + 1) % WORK_NR_COLORS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
David Howells4594bf12006-12-07 11:33:26 +0000548/*
Tejun Heoe1201532010-07-22 14:14:25 +0200549 * A work's data points to the cwq with WORK_STRUCT_CWQ set while the
550 * work is on queue. Once execution starts, WORK_STRUCT_CWQ is
551 * cleared and the work data contains the cpu number it was last on.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200552 *
553 * set_work_{cwq|cpu}() and clear_work_data() can be used to set the
554 * cwq, cpu or clear work->data. These functions should only be
555 * called while the work is owned - ie. while the PENDING bit is set.
556 *
557 * get_work_[g]cwq() can be used to obtain the gcwq or cwq
558 * corresponding to a work. gcwq is available once the work has been
559 * queued anywhere after initialization. cwq is available only from
560 * queueing until execution starts.
David Howells4594bf12006-12-07 11:33:26 +0000561 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200562static inline void set_work_data(struct work_struct *work, unsigned long data,
563 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000564{
David Howells4594bf12006-12-07 11:33:26 +0000565 BUG_ON(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200566 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000567}
David Howells365970a2006-11-22 14:54:49 +0000568
Tejun Heo7a22ad72010-06-29 10:07:13 +0200569static void set_work_cwq(struct work_struct *work,
570 struct cpu_workqueue_struct *cwq,
571 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200572{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200573 set_work_data(work, (unsigned long)cwq,
Tejun Heoe1201532010-07-22 14:14:25 +0200574 WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200575}
576
Tejun Heo7a22ad72010-06-29 10:07:13 +0200577static void set_work_cpu(struct work_struct *work, unsigned int cpu)
David Howells365970a2006-11-22 14:54:49 +0000578{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200579 set_work_data(work, cpu << WORK_STRUCT_FLAG_BITS, WORK_STRUCT_PENDING);
580}
581
582static void clear_work_data(struct work_struct *work)
583{
584 set_work_data(work, WORK_STRUCT_NO_CPU, 0);
585}
586
Tejun Heo7a22ad72010-06-29 10:07:13 +0200587static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work)
588{
Tejun Heoe1201532010-07-22 14:14:25 +0200589 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200590
Tejun Heoe1201532010-07-22 14:14:25 +0200591 if (data & WORK_STRUCT_CWQ)
592 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
593 else
594 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200595}
596
597static struct global_cwq *get_work_gcwq(struct work_struct *work)
598{
Tejun Heoe1201532010-07-22 14:14:25 +0200599 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200600 unsigned int cpu;
601
Tejun Heoe1201532010-07-22 14:14:25 +0200602 if (data & WORK_STRUCT_CWQ)
603 return ((struct cpu_workqueue_struct *)
Tejun Heobd7bdd42012-07-12 14:46:37 -0700604 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->gcwq;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200605
606 cpu = data >> WORK_STRUCT_FLAG_BITS;
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200607 if (cpu == WORK_CPU_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200608 return NULL;
609
Tejun Heof3421792010-07-02 10:03:51 +0200610 BUG_ON(cpu >= nr_cpu_ids && cpu != WORK_CPU_UNBOUND);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200611 return get_gcwq(cpu);
David Howells365970a2006-11-22 14:54:49 +0000612}
613
614/*
Tejun Heo32704762012-07-13 22:16:45 -0700615 * Policy functions. These define the policies on how the global worker
616 * pools are managed. Unless noted otherwise, these functions assume that
617 * they're being called with gcwq->lock held.
David Howells365970a2006-11-22 14:54:49 +0000618 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200619
Tejun Heo63d95a92012-07-12 14:46:37 -0700620static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000621{
Tejun Heo32704762012-07-13 22:16:45 -0700622 return !atomic_read(get_pool_nr_running(pool));
David Howells365970a2006-11-22 14:54:49 +0000623}
624
Tejun Heoe22bee72010-06-29 10:07:14 +0200625/*
626 * Need to wake up a worker? Called from anything but currently
627 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700628 *
629 * Note that, because unbound workers never contribute to nr_running, this
630 * function will always return %true for unbound gcwq as long as the
631 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200632 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700633static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000634{
Tejun Heo63d95a92012-07-12 14:46:37 -0700635 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000636}
637
Tejun Heoe22bee72010-06-29 10:07:14 +0200638/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700639static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200640{
Tejun Heo63d95a92012-07-12 14:46:37 -0700641 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200642}
643
644/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700645static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200646{
Tejun Heo63d95a92012-07-12 14:46:37 -0700647 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200648
Tejun Heo32704762012-07-13 22:16:45 -0700649 return !list_empty(&pool->worklist) && atomic_read(nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200650}
651
652/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700653static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200654{
Tejun Heo63d95a92012-07-12 14:46:37 -0700655 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200656}
657
658/* Do I need to be the manager? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700659static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200660{
Tejun Heo63d95a92012-07-12 14:46:37 -0700661 return need_to_create_worker(pool) ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700662 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200663}
664
665/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700666static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200667{
Tejun Heo60373152012-07-17 12:39:27 -0700668 bool managing = mutex_is_locked(&pool->manager_mutex);
Tejun Heo63d95a92012-07-12 14:46:37 -0700669 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
670 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200671
672 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
673}
674
675/*
676 * Wake up functions.
677 */
678
Tejun Heo7e116292010-06-29 10:07:13 +0200679/* Return the first worker. Safe with preemption disabled */
Tejun Heo63d95a92012-07-12 14:46:37 -0700680static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200681{
Tejun Heo63d95a92012-07-12 14:46:37 -0700682 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200683 return NULL;
684
Tejun Heo63d95a92012-07-12 14:46:37 -0700685 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200686}
687
688/**
689 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700690 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200691 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700692 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200693 *
694 * CONTEXT:
695 * spin_lock_irq(gcwq->lock).
696 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700697static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200698{
Tejun Heo63d95a92012-07-12 14:46:37 -0700699 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200700
701 if (likely(worker))
702 wake_up_process(worker->task);
703}
704
Tejun Heo4690c4a2010-06-29 10:07:10 +0200705/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200706 * wq_worker_waking_up - a worker is waking up
707 * @task: task waking up
708 * @cpu: CPU @task is waking up to
709 *
710 * This function is called during try_to_wake_up() when a worker is
711 * being awoken.
712 *
713 * CONTEXT:
714 * spin_lock_irq(rq->lock)
715 */
716void wq_worker_waking_up(struct task_struct *task, unsigned int cpu)
717{
718 struct worker *worker = kthread_data(task);
719
Steven Rostedt2d646722010-12-03 23:12:33 -0500720 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heo63d95a92012-07-12 14:46:37 -0700721 atomic_inc(get_pool_nr_running(worker->pool));
Tejun Heoe22bee72010-06-29 10:07:14 +0200722}
723
724/**
725 * wq_worker_sleeping - a worker is going to sleep
726 * @task: task going to sleep
727 * @cpu: CPU in question, must be the current CPU number
728 *
729 * This function is called during schedule() when a busy worker is
730 * going to sleep. Worker on the same cpu can be woken up by
731 * returning pointer to its task.
732 *
733 * CONTEXT:
734 * spin_lock_irq(rq->lock)
735 *
736 * RETURNS:
737 * Worker task on @cpu to wake up, %NULL if none.
738 */
739struct task_struct *wq_worker_sleeping(struct task_struct *task,
740 unsigned int cpu)
741{
742 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heobd7bdd42012-07-12 14:46:37 -0700743 struct worker_pool *pool = worker->pool;
Tejun Heo63d95a92012-07-12 14:46:37 -0700744 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200745
Steven Rostedt2d646722010-12-03 23:12:33 -0500746 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200747 return NULL;
748
749 /* this can only happen on the local cpu */
750 BUG_ON(cpu != raw_smp_processor_id());
751
752 /*
753 * The counterpart of the following dec_and_test, implied mb,
754 * worklist not empty test sequence is in insert_work().
755 * Please read comment there.
756 *
757 * NOT_RUNNING is clear. This means that trustee is not in
758 * charge and we're running on the local cpu w/ rq lock held
759 * and preemption disabled, which in turn means that none else
760 * could be manipulating idle_list, so dereferencing idle_list
761 * without gcwq lock is safe.
762 */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700763 if (atomic_dec_and_test(nr_running) && !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700764 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200765 return to_wakeup ? to_wakeup->task : NULL;
766}
767
768/**
769 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200770 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200771 * @flags: flags to set
772 * @wakeup: wakeup an idle worker if necessary
773 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200774 * Set @flags in @worker->flags and adjust nr_running accordingly. If
775 * nr_running becomes zero and @wakeup is %true, an idle worker is
776 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200777 *
Tejun Heocb444762010-07-02 10:03:50 +0200778 * CONTEXT:
779 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200780 */
781static inline void worker_set_flags(struct worker *worker, unsigned int flags,
782 bool wakeup)
783{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700784 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200785
Tejun Heocb444762010-07-02 10:03:50 +0200786 WARN_ON_ONCE(worker->task != current);
787
Tejun Heoe22bee72010-06-29 10:07:14 +0200788 /*
789 * If transitioning into NOT_RUNNING, adjust nr_running and
790 * wake up an idle worker as necessary if requested by
791 * @wakeup.
792 */
793 if ((flags & WORKER_NOT_RUNNING) &&
794 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heo63d95a92012-07-12 14:46:37 -0700795 atomic_t *nr_running = get_pool_nr_running(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200796
797 if (wakeup) {
798 if (atomic_dec_and_test(nr_running) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -0700799 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700800 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200801 } else
802 atomic_dec(nr_running);
803 }
804
Tejun Heod302f012010-06-29 10:07:13 +0200805 worker->flags |= flags;
806}
807
808/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200809 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200810 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200811 * @flags: flags to clear
812 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200813 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200814 *
Tejun Heocb444762010-07-02 10:03:50 +0200815 * CONTEXT:
816 * spin_lock_irq(gcwq->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200817 */
818static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
819{
Tejun Heo63d95a92012-07-12 14:46:37 -0700820 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200821 unsigned int oflags = worker->flags;
822
Tejun Heocb444762010-07-02 10:03:50 +0200823 WARN_ON_ONCE(worker->task != current);
824
Tejun Heod302f012010-06-29 10:07:13 +0200825 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200826
Tejun Heo42c025f2011-01-11 15:58:49 +0100827 /*
828 * If transitioning out of NOT_RUNNING, increment nr_running. Note
829 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
830 * of multiple flags, not a single flag.
831 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200832 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
833 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heo63d95a92012-07-12 14:46:37 -0700834 atomic_inc(get_pool_nr_running(pool));
Tejun Heod302f012010-06-29 10:07:13 +0200835}
836
837/**
Tejun Heoc8e55f32010-06-29 10:07:12 +0200838 * busy_worker_head - return the busy hash head for a work
839 * @gcwq: gcwq of interest
840 * @work: work to be hashed
841 *
842 * Return hash head of @gcwq for @work.
843 *
844 * CONTEXT:
845 * spin_lock_irq(gcwq->lock).
846 *
847 * RETURNS:
848 * Pointer to the hash head.
849 */
850static struct hlist_head *busy_worker_head(struct global_cwq *gcwq,
851 struct work_struct *work)
852{
853 const int base_shift = ilog2(sizeof(struct work_struct));
854 unsigned long v = (unsigned long)work;
855
856 /* simple shift and fold hash, do we need something better? */
857 v >>= base_shift;
858 v += v >> BUSY_WORKER_HASH_ORDER;
859 v &= BUSY_WORKER_HASH_MASK;
860
861 return &gcwq->busy_hash[v];
862}
863
864/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200865 * __find_worker_executing_work - find worker which is executing a work
866 * @gcwq: gcwq of interest
867 * @bwh: hash head as returned by busy_worker_head()
868 * @work: work to find worker for
869 *
870 * Find a worker which is executing @work on @gcwq. @bwh should be
871 * the hash head obtained by calling busy_worker_head() with the same
872 * work.
873 *
874 * CONTEXT:
875 * spin_lock_irq(gcwq->lock).
876 *
877 * RETURNS:
878 * Pointer to worker which is executing @work if found, NULL
879 * otherwise.
880 */
881static struct worker *__find_worker_executing_work(struct global_cwq *gcwq,
882 struct hlist_head *bwh,
883 struct work_struct *work)
884{
885 struct worker *worker;
886 struct hlist_node *tmp;
887
888 hlist_for_each_entry(worker, tmp, bwh, hentry)
889 if (worker->current_work == work)
890 return worker;
891 return NULL;
892}
893
894/**
895 * find_worker_executing_work - find worker which is executing a work
896 * @gcwq: gcwq of interest
897 * @work: work to find worker for
898 *
899 * Find a worker which is executing @work on @gcwq. This function is
900 * identical to __find_worker_executing_work() except that this
901 * function calculates @bwh itself.
902 *
903 * CONTEXT:
904 * spin_lock_irq(gcwq->lock).
905 *
906 * RETURNS:
907 * Pointer to worker which is executing @work if found, NULL
908 * otherwise.
909 */
910static struct worker *find_worker_executing_work(struct global_cwq *gcwq,
911 struct work_struct *work)
912{
913 return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work),
914 work);
915}
916
917/**
Tejun Heo7e116292010-06-29 10:07:13 +0200918 * insert_work - insert a work into gcwq
Tejun Heo4690c4a2010-06-29 10:07:10 +0200919 * @cwq: cwq @work belongs to
920 * @work: work to insert
921 * @head: insertion point
922 * @extra_flags: extra WORK_STRUCT_* flags to set
923 *
Tejun Heo7e116292010-06-29 10:07:13 +0200924 * Insert @work which belongs to @cwq into @gcwq after @head.
925 * @extra_flags is or'd to work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200926 *
927 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +0200928 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +0200929 */
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700930static void insert_work(struct cpu_workqueue_struct *cwq,
Tejun Heo4690c4a2010-06-29 10:07:10 +0200931 struct work_struct *work, struct list_head *head,
932 unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700933{
Tejun Heo63d95a92012-07-12 14:46:37 -0700934 struct worker_pool *pool = cwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100935
Tejun Heo4690c4a2010-06-29 10:07:10 +0200936 /* we own @work, set data and link */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200937 set_work_cwq(work, cwq, extra_flags);
Tejun Heo4690c4a2010-06-29 10:07:10 +0200938
Oleg Nesterov6e84d642007-05-09 02:34:46 -0700939 /*
940 * Ensure that we get the right work->data if we see the
941 * result of list_add() below, see try_to_grab_pending().
942 */
943 smp_wmb();
Tejun Heo4690c4a2010-06-29 10:07:10 +0200944
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -0700945 list_add_tail(&work->entry, head);
Tejun Heoe22bee72010-06-29 10:07:14 +0200946
947 /*
948 * Ensure either worker_sched_deactivated() sees the above
949 * list_add_tail() or we see zero nr_running to avoid workers
950 * lying around lazily while there are works to be processed.
951 */
952 smp_mb();
953
Tejun Heo63d95a92012-07-12 14:46:37 -0700954 if (__need_more_worker(pool))
955 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -0700956}
957
Tejun Heoc8efcc22010-12-20 19:32:04 +0100958/*
959 * Test whether @work is being queued from another work executing on the
960 * same workqueue. This is rather expensive and should only be used from
961 * cold paths.
962 */
963static bool is_chained_work(struct workqueue_struct *wq)
964{
965 unsigned long flags;
966 unsigned int cpu;
967
968 for_each_gcwq_cpu(cpu) {
969 struct global_cwq *gcwq = get_gcwq(cpu);
970 struct worker *worker;
971 struct hlist_node *pos;
972 int i;
973
974 spin_lock_irqsave(&gcwq->lock, flags);
975 for_each_busy_worker(worker, i, pos, gcwq) {
976 if (worker->task != current)
977 continue;
978 spin_unlock_irqrestore(&gcwq->lock, flags);
979 /*
980 * I'm @worker, no locking necessary. See if @work
981 * is headed to the same workqueue.
982 */
983 return worker->current_cwq->wq == wq;
984 }
985 spin_unlock_irqrestore(&gcwq->lock, flags);
986 }
987 return false;
988}
989
Tejun Heo4690c4a2010-06-29 10:07:10 +0200990static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 struct work_struct *work)
992{
Tejun Heo502ca9d2010-06-29 10:07:13 +0200993 struct global_cwq *gcwq;
994 struct cpu_workqueue_struct *cwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200995 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +0200996 unsigned int work_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 unsigned long flags;
998
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900999 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001000
Tejun Heoc8efcc22010-12-20 19:32:04 +01001001 /* if dying, only works from the same workqueue are allowed */
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02001002 if (unlikely(wq->flags & WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001003 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001004 return;
1005
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001006 /* determine gcwq to use */
1007 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001008 struct global_cwq *last_gcwq;
1009
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001010 if (unlikely(cpu == WORK_CPU_UNBOUND))
1011 cpu = raw_smp_processor_id();
1012
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001013 /*
1014 * It's multi cpu. If @wq is non-reentrant and @work
1015 * was previously on a different cpu, it might still
1016 * be running there, in which case the work needs to
1017 * be queued on that cpu to guarantee non-reentrance.
1018 */
Tejun Heo502ca9d2010-06-29 10:07:13 +02001019 gcwq = get_gcwq(cpu);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001020 if (wq->flags & WQ_NON_REENTRANT &&
1021 (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) {
1022 struct worker *worker;
1023
1024 spin_lock_irqsave(&last_gcwq->lock, flags);
1025
1026 worker = find_worker_executing_work(last_gcwq, work);
1027
1028 if (worker && worker->current_cwq->wq == wq)
1029 gcwq = last_gcwq;
1030 else {
1031 /* meh... not running there, queue here */
1032 spin_unlock_irqrestore(&last_gcwq->lock, flags);
1033 spin_lock_irqsave(&gcwq->lock, flags);
1034 }
1035 } else
1036 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heof3421792010-07-02 10:03:51 +02001037 } else {
1038 gcwq = get_gcwq(WORK_CPU_UNBOUND);
1039 spin_lock_irqsave(&gcwq->lock, flags);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001040 }
1041
1042 /* gcwq determined, get cwq and queue */
1043 cwq = get_cwq(gcwq->cpu, wq);
Tejun Heocdadf002010-10-05 10:49:55 +02001044 trace_workqueue_queue_work(cpu, cwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001045
Dan Carpenterf5b25522012-04-13 22:06:58 +03001046 if (WARN_ON(!list_empty(&work->entry))) {
1047 spin_unlock_irqrestore(&gcwq->lock, flags);
1048 return;
1049 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001050
Tejun Heo73f53c42010-06-29 10:07:11 +02001051 cwq->nr_in_flight[cwq->work_color]++;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001052 work_flags = work_color_to_flags(cwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001053
1054 if (likely(cwq->nr_active < cwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001055 trace_workqueue_activate_work(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001056 cwq->nr_active++;
Tejun Heo32704762012-07-13 22:16:45 -07001057 worklist = &cwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001058 } else {
1059 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001060 worklist = &cwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001061 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001062
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001063 insert_work(cwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001064
Tejun Heo8b03ae32010-06-29 10:07:12 +02001065 spin_unlock_irqrestore(&gcwq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
1067
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001068/**
1069 * queue_work - queue work on a workqueue
1070 * @wq: workqueue to use
1071 * @work: work to queue
1072 *
Alan Stern057647f2006-10-28 10:38:58 -07001073 * Returns 0 if @work was already on a queue, non-zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 *
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07001075 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1076 * it can be processed by another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001078int queue_work(struct workqueue_struct *wq, struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001080 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Oleg Nesterovef1ca232008-07-25 01:47:53 -07001082 ret = queue_work_on(get_cpu(), wq, work);
1083 put_cpu();
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 return ret;
1086}
Dave Jonesae90dd52006-06-30 01:40:45 -04001087EXPORT_SYMBOL_GPL(queue_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Zhang Ruic1a220e2008-07-23 21:28:39 -07001089/**
1090 * queue_work_on - queue work on specific cpu
1091 * @cpu: CPU number to execute work on
1092 * @wq: workqueue to use
1093 * @work: work to queue
1094 *
1095 * Returns 0 if @work was already on a queue, non-zero otherwise.
1096 *
1097 * We queue the work to a specific CPU, the caller must ensure it
1098 * can't go away.
1099 */
1100int
1101queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
1102{
1103 int ret = 0;
1104
Tejun Heo22df02b2010-06-29 10:07:10 +02001105 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001106 __queue_work(cpu, wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001107 ret = 1;
1108 }
1109 return ret;
1110}
1111EXPORT_SYMBOL_GPL(queue_work_on);
1112
Li Zefan6d141c32008-02-08 04:21:09 -08001113static void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
David Howells52bad642006-11-22 14:54:01 +00001115 struct delayed_work *dwork = (struct delayed_work *)__data;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001116 struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Tejun Heo4690c4a2010-06-29 10:07:10 +02001118 __queue_work(smp_processor_id(), cwq->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119}
1120
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001121/**
1122 * queue_delayed_work - queue work on a workqueue after delay
1123 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001124 * @dwork: delayable work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001125 * @delay: number of jiffies to wait before queueing
1126 *
Alan Stern057647f2006-10-28 10:38:58 -07001127 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001128 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001129int queue_delayed_work(struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001130 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
David Howells52bad642006-11-22 14:54:01 +00001132 if (delay == 0)
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001133 return queue_work(wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001135 return queue_delayed_work_on(-1, wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136}
Dave Jonesae90dd52006-06-30 01:40:45 -04001137EXPORT_SYMBOL_GPL(queue_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001139/**
1140 * queue_delayed_work_on - queue work on specific CPU after delay
1141 * @cpu: CPU number to execute work on
1142 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001143 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001144 * @delay: number of jiffies to wait before queueing
1145 *
Alan Stern057647f2006-10-28 10:38:58 -07001146 * Returns 0 if @work was already on a queue, non-zero otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001147 */
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001148int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
David Howells52bad642006-11-22 14:54:01 +00001149 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001150{
1151 int ret = 0;
David Howells52bad642006-11-22 14:54:01 +00001152 struct timer_list *timer = &dwork->timer;
1153 struct work_struct *work = &dwork->work;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001154
Tejun Heo22df02b2010-06-29 10:07:10 +02001155 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001156 unsigned int lcpu;
Tejun Heo7a22ad72010-06-29 10:07:13 +02001157
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001158 BUG_ON(timer_pending(timer));
1159 BUG_ON(!list_empty(&work->entry));
1160
Andrew Liu8a3e77c2008-05-01 04:35:14 -07001161 timer_stats_timer_set_start_info(&dwork->timer);
1162
Tejun Heo7a22ad72010-06-29 10:07:13 +02001163 /*
1164 * This stores cwq for the moment, for the timer_fn.
1165 * Note that the work's gcwq is preserved to allow
1166 * reentrance detection for delayed works.
1167 */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001168 if (!(wq->flags & WQ_UNBOUND)) {
1169 struct global_cwq *gcwq = get_work_gcwq(work);
1170
1171 if (gcwq && gcwq->cpu != WORK_CPU_UNBOUND)
1172 lcpu = gcwq->cpu;
1173 else
1174 lcpu = raw_smp_processor_id();
1175 } else
1176 lcpu = WORK_CPU_UNBOUND;
1177
Tejun Heo7a22ad72010-06-29 10:07:13 +02001178 set_work_cwq(work, get_cwq(lcpu, wq), 0);
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001179
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001180 timer->expires = jiffies + delay;
David Howells52bad642006-11-22 14:54:01 +00001181 timer->data = (unsigned long)dwork;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001182 timer->function = delayed_work_timer_fn;
Oleg Nesterov63bc0362007-05-09 02:34:16 -07001183
1184 if (unlikely(cpu >= 0))
1185 add_timer_on(timer, cpu);
1186 else
1187 add_timer(timer);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001188 ret = 1;
1189 }
1190 return ret;
1191}
Dave Jonesae90dd52006-06-30 01:40:45 -04001192EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Tejun Heoc8e55f32010-06-29 10:07:12 +02001194/**
1195 * worker_enter_idle - enter idle state
1196 * @worker: worker which is entering idle state
1197 *
1198 * @worker is entering idle state. Update stats and idle timer if
1199 * necessary.
1200 *
1201 * LOCKING:
1202 * spin_lock_irq(gcwq->lock).
1203 */
1204static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001206 struct worker_pool *pool = worker->pool;
1207 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Tejun Heoc8e55f32010-06-29 10:07:12 +02001209 BUG_ON(worker->flags & WORKER_IDLE);
1210 BUG_ON(!list_empty(&worker->entry) &&
1211 (worker->hentry.next || worker->hentry.pprev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Tejun Heocb444762010-07-02 10:03:50 +02001213 /* can't use worker_set_flags(), also called from start_worker() */
1214 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001215 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001216 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001217
Tejun Heoc8e55f32010-06-29 10:07:12 +02001218 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001219 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001220
Tejun Heo403c8212012-07-17 12:39:27 -07001221 if (likely(gcwq->trustee_state != TRUSTEE_DONE)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001222 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
Tejun Heobd7bdd42012-07-12 14:46:37 -07001223 mod_timer(&pool->idle_timer,
Tejun Heoe22bee72010-06-29 10:07:14 +02001224 jiffies + IDLE_WORKER_TIMEOUT);
1225 } else
Tejun Heodb7bccf2010-06-29 10:07:12 +02001226 wake_up_all(&gcwq->trustee_wait);
Tejun Heocb444762010-07-02 10:03:50 +02001227
Tejun Heo544ecf32012-05-14 15:04:50 -07001228 /*
1229 * Sanity check nr_running. Because trustee releases gcwq->lock
Tejun Heo403c8212012-07-17 12:39:27 -07001230 * between setting %WORKER_UNBOUND and zapping nr_running, the
Tejun Heo544ecf32012-05-14 15:04:50 -07001231 * warning may trigger spuriously. Check iff trustee is idle.
1232 */
1233 WARN_ON_ONCE(gcwq->trustee_state == TRUSTEE_DONE &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001234 pool->nr_workers == pool->nr_idle &&
Tejun Heo63d95a92012-07-12 14:46:37 -07001235 atomic_read(get_pool_nr_running(pool)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
Tejun Heoc8e55f32010-06-29 10:07:12 +02001238/**
1239 * worker_leave_idle - leave idle state
1240 * @worker: worker which is leaving idle state
1241 *
1242 * @worker is leaving idle state. Update stats.
1243 *
1244 * LOCKING:
1245 * spin_lock_irq(gcwq->lock).
1246 */
1247static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001249 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Tejun Heoc8e55f32010-06-29 10:07:12 +02001251 BUG_ON(!(worker->flags & WORKER_IDLE));
Tejun Heod302f012010-06-29 10:07:13 +02001252 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001253 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001254 list_del_init(&worker->entry);
1255}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Tejun Heoe22bee72010-06-29 10:07:14 +02001257/**
1258 * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq
1259 * @worker: self
1260 *
1261 * Works which are scheduled while the cpu is online must at least be
1262 * scheduled to a worker which is bound to the cpu so that if they are
1263 * flushed from cpu callbacks while cpu is going down, they are
1264 * guaranteed to execute on the cpu.
1265 *
1266 * This function is to be used by rogue workers and rescuers to bind
1267 * themselves to the target cpu and may race with cpu going down or
1268 * coming online. kthread_bind() can't be used because it may put the
1269 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1270 * verbatim as it's best effort and blocking and gcwq may be
1271 * [dis]associated in the meantime.
1272 *
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001273 * This function tries set_cpus_allowed() and locks gcwq and verifies the
1274 * binding against %GCWQ_DISASSOCIATED which is set during
1275 * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1276 * enters idle state or fetches works without dropping lock, it can
1277 * guarantee the scheduling requirement described in the first paragraph.
Tejun Heoe22bee72010-06-29 10:07:14 +02001278 *
1279 * CONTEXT:
1280 * Might sleep. Called without any lock but returns with gcwq->lock
1281 * held.
1282 *
1283 * RETURNS:
1284 * %true if the associated gcwq is online (@worker is successfully
1285 * bound), %false if offline.
1286 */
1287static bool worker_maybe_bind_and_lock(struct worker *worker)
Namhyung Kim972fa1c2010-08-22 23:19:43 +09001288__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001289{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001290 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001291 struct task_struct *task = worker->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Tejun Heoe22bee72010-06-29 10:07:14 +02001293 while (true) {
1294 /*
1295 * The following call may fail, succeed or succeed
1296 * without actually migrating the task to the cpu if
1297 * it races with cpu hotunplug operation. Verify
1298 * against GCWQ_DISASSOCIATED.
1299 */
Tejun Heof3421792010-07-02 10:03:51 +02001300 if (!(gcwq->flags & GCWQ_DISASSOCIATED))
1301 set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu));
Oleg Nesterov85f41862007-05-09 02:34:20 -07001302
Tejun Heoe22bee72010-06-29 10:07:14 +02001303 spin_lock_irq(&gcwq->lock);
1304 if (gcwq->flags & GCWQ_DISASSOCIATED)
1305 return false;
1306 if (task_cpu(task) == gcwq->cpu &&
1307 cpumask_equal(&current->cpus_allowed,
1308 get_cpu_mask(gcwq->cpu)))
1309 return true;
1310 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001311
Tejun Heo5035b202011-04-29 18:08:37 +02001312 /*
1313 * We've raced with CPU hot[un]plug. Give it a breather
1314 * and retry migration. cond_resched() is required here;
1315 * otherwise, we might deadlock against cpu_stop trying to
1316 * bring down the CPU on non-preemptive kernel.
1317 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001318 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001319 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001320 }
1321}
1322
Tejun Heo25511a42012-07-17 12:39:27 -07001323struct idle_rebind {
1324 int cnt; /* # workers to be rebound */
1325 struct completion done; /* all workers rebound */
1326};
1327
Tejun Heoe22bee72010-06-29 10:07:14 +02001328/*
Tejun Heo25511a42012-07-17 12:39:27 -07001329 * Rebind an idle @worker to its CPU. During CPU onlining, this has to
1330 * happen synchronously for idle workers. worker_thread() will test
1331 * %WORKER_REBIND before leaving idle and call this function.
1332 */
1333static void idle_worker_rebind(struct worker *worker)
1334{
1335 struct global_cwq *gcwq = worker->pool->gcwq;
1336
1337 /* CPU must be online at this point */
1338 WARN_ON(!worker_maybe_bind_and_lock(worker));
1339 if (!--worker->idle_rebind->cnt)
1340 complete(&worker->idle_rebind->done);
1341 spin_unlock_irq(&worker->pool->gcwq->lock);
1342
1343 /* we did our part, wait for rebind_workers() to finish up */
1344 wait_event(gcwq->rebind_hold, !(worker->flags & WORKER_REBIND));
1345}
1346
1347/*
1348 * Function for @worker->rebind.work used to rebind unbound busy workers to
Tejun Heo403c8212012-07-17 12:39:27 -07001349 * the associated cpu which is coming back online. This is scheduled by
1350 * cpu up but can race with other cpu hotplug operations and may be
1351 * executed twice without intervening cpu down.
Tejun Heoe22bee72010-06-29 10:07:14 +02001352 */
Tejun Heo25511a42012-07-17 12:39:27 -07001353static void busy_worker_rebind_fn(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001354{
1355 struct worker *worker = container_of(work, struct worker, rebind_work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001356 struct global_cwq *gcwq = worker->pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001357
1358 if (worker_maybe_bind_and_lock(worker))
1359 worker_clr_flags(worker, WORKER_REBIND);
1360
1361 spin_unlock_irq(&gcwq->lock);
1362}
1363
Tejun Heo25511a42012-07-17 12:39:27 -07001364/**
1365 * rebind_workers - rebind all workers of a gcwq to the associated CPU
1366 * @gcwq: gcwq of interest
1367 *
1368 * @gcwq->cpu is coming online. Rebind all workers to the CPU. Rebinding
1369 * is different for idle and busy ones.
1370 *
1371 * The idle ones should be rebound synchronously and idle rebinding should
1372 * be complete before any worker starts executing work items with
1373 * concurrency management enabled; otherwise, scheduler may oops trying to
1374 * wake up non-local idle worker from wq_worker_sleeping().
1375 *
1376 * This is achieved by repeatedly requesting rebinding until all idle
1377 * workers are known to have been rebound under @gcwq->lock and holding all
1378 * idle workers from becoming busy until idle rebinding is complete.
1379 *
1380 * Once idle workers are rebound, busy workers can be rebound as they
1381 * finish executing their current work items. Queueing the rebind work at
1382 * the head of their scheduled lists is enough. Note that nr_running will
1383 * be properbly bumped as busy workers rebind.
1384 *
1385 * On return, all workers are guaranteed to either be bound or have rebind
1386 * work item scheduled.
1387 */
1388static void rebind_workers(struct global_cwq *gcwq)
1389 __releases(&gcwq->lock) __acquires(&gcwq->lock)
1390{
1391 struct idle_rebind idle_rebind;
1392 struct worker_pool *pool;
1393 struct worker *worker;
1394 struct hlist_node *pos;
1395 int i;
1396
1397 lockdep_assert_held(&gcwq->lock);
1398
1399 for_each_worker_pool(pool, gcwq)
1400 lockdep_assert_held(&pool->manager_mutex);
1401
1402 /*
1403 * Rebind idle workers. Interlocked both ways. We wait for
1404 * workers to rebind via @idle_rebind.done. Workers will wait for
1405 * us to finish up by watching %WORKER_REBIND.
1406 */
1407 init_completion(&idle_rebind.done);
1408retry:
1409 idle_rebind.cnt = 1;
1410 INIT_COMPLETION(idle_rebind.done);
1411
1412 /* set REBIND and kick idle ones, we'll wait for these later */
1413 for_each_worker_pool(pool, gcwq) {
1414 list_for_each_entry(worker, &pool->idle_list, entry) {
1415 if (worker->flags & WORKER_REBIND)
1416 continue;
1417
1418 /* morph UNBOUND to REBIND */
1419 worker->flags &= ~WORKER_UNBOUND;
1420 worker->flags |= WORKER_REBIND;
1421
1422 idle_rebind.cnt++;
1423 worker->idle_rebind = &idle_rebind;
1424
1425 /* worker_thread() will call idle_worker_rebind() */
1426 wake_up_process(worker->task);
1427 }
1428 }
1429
1430 if (--idle_rebind.cnt) {
1431 spin_unlock_irq(&gcwq->lock);
1432 wait_for_completion(&idle_rebind.done);
1433 spin_lock_irq(&gcwq->lock);
1434 /* busy ones might have become idle while waiting, retry */
1435 goto retry;
1436 }
1437
1438 /*
1439 * All idle workers are rebound and waiting for %WORKER_REBIND to
1440 * be cleared inside idle_worker_rebind(). Clear and release.
1441 * Clearing %WORKER_REBIND from this foreign context is safe
1442 * because these workers are still guaranteed to be idle.
1443 */
1444 for_each_worker_pool(pool, gcwq)
1445 list_for_each_entry(worker, &pool->idle_list, entry)
1446 worker->flags &= ~WORKER_REBIND;
1447
1448 wake_up_all(&gcwq->rebind_hold);
1449
1450 /* rebind busy workers */
1451 for_each_busy_worker(worker, i, pos, gcwq) {
1452 struct work_struct *rebind_work = &worker->rebind_work;
1453
1454 /* morph UNBOUND to REBIND */
1455 worker->flags &= ~WORKER_UNBOUND;
1456 worker->flags |= WORKER_REBIND;
1457
1458 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
1459 work_data_bits(rebind_work)))
1460 continue;
1461
1462 /* wq doesn't matter, use the default one */
1463 debug_work_activate(rebind_work);
1464 insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work,
1465 worker->scheduled.next,
1466 work_color_to_flags(WORK_NO_COLOR));
1467 }
1468}
1469
Tejun Heoc34056a2010-06-29 10:07:11 +02001470static struct worker *alloc_worker(void)
1471{
1472 struct worker *worker;
1473
1474 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001475 if (worker) {
1476 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001477 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heo25511a42012-07-17 12:39:27 -07001478 INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn);
Tejun Heoe22bee72010-06-29 10:07:14 +02001479 /* on creation a worker is in !idle && prep state */
1480 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001481 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001482 return worker;
1483}
1484
1485/**
1486 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001487 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001488 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001489 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001490 * can be started by calling start_worker() or destroyed using
1491 * destroy_worker().
1492 *
1493 * CONTEXT:
1494 * Might sleep. Does GFP_KERNEL allocations.
1495 *
1496 * RETURNS:
1497 * Pointer to the newly created worker.
1498 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001499static struct worker *create_worker(struct worker_pool *pool)
Tejun Heoc34056a2010-06-29 10:07:11 +02001500{
Tejun Heo63d95a92012-07-12 14:46:37 -07001501 struct global_cwq *gcwq = pool->gcwq;
Tejun Heo32704762012-07-13 22:16:45 -07001502 const char *pri = worker_pool_pri(pool) ? "H" : "";
Tejun Heoc34056a2010-06-29 10:07:11 +02001503 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001504 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001505
Tejun Heo8b03ae32010-06-29 10:07:12 +02001506 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001507 while (ida_get_new(&pool->worker_ida, &id)) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001508 spin_unlock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001509 if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001510 goto fail;
Tejun Heo8b03ae32010-06-29 10:07:12 +02001511 spin_lock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001512 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02001513 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001514
1515 worker = alloc_worker();
1516 if (!worker)
1517 goto fail;
1518
Tejun Heobd7bdd42012-07-12 14:46:37 -07001519 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001520 worker->id = id;
1521
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001522 if (gcwq->cpu != WORK_CPU_UNBOUND)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001523 worker->task = kthread_create_on_node(worker_thread,
Tejun Heo32704762012-07-13 22:16:45 -07001524 worker, cpu_to_node(gcwq->cpu),
1525 "kworker/%u:%d%s", gcwq->cpu, id, pri);
Tejun Heof3421792010-07-02 10:03:51 +02001526 else
1527 worker->task = kthread_create(worker_thread, worker,
Tejun Heo32704762012-07-13 22:16:45 -07001528 "kworker/u:%d%s", id, pri);
Tejun Heoc34056a2010-06-29 10:07:11 +02001529 if (IS_ERR(worker->task))
1530 goto fail;
1531
Tejun Heo32704762012-07-13 22:16:45 -07001532 if (worker_pool_pri(pool))
1533 set_user_nice(worker->task, HIGHPRI_NICE_LEVEL);
1534
Tejun Heodb7bccf2010-06-29 10:07:12 +02001535 /*
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001536 * Determine CPU binding of the new worker depending on
1537 * %GCWQ_DISASSOCIATED. The caller is responsible for ensuring the
1538 * flag remains stable across this function. See the comments
1539 * above the flag definition for details.
1540 *
1541 * As an unbound worker may later become a regular one if CPU comes
1542 * online, make sure every worker has %PF_THREAD_BOUND set.
Tejun Heodb7bccf2010-06-29 10:07:12 +02001543 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001544 if (!(gcwq->flags & GCWQ_DISASSOCIATED)) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001545 kthread_bind(worker->task, gcwq->cpu);
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001546 } else {
Tejun Heodb7bccf2010-06-29 10:07:12 +02001547 worker->task->flags |= PF_THREAD_BOUND;
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001548 worker->flags |= WORKER_UNBOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001550
Tejun Heoc34056a2010-06-29 10:07:11 +02001551 return worker;
1552fail:
1553 if (id >= 0) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02001554 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001555 ida_remove(&pool->worker_ida, id);
Tejun Heo8b03ae32010-06-29 10:07:12 +02001556 spin_unlock_irq(&gcwq->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001557 }
1558 kfree(worker);
1559 return NULL;
1560}
1561
1562/**
1563 * start_worker - start a newly created worker
1564 * @worker: worker to start
1565 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001566 * Make the gcwq aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001567 *
1568 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001569 * spin_lock_irq(gcwq->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001570 */
1571static void start_worker(struct worker *worker)
1572{
Tejun Heocb444762010-07-02 10:03:50 +02001573 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001574 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001575 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001576 wake_up_process(worker->task);
1577}
1578
1579/**
1580 * destroy_worker - destroy a workqueue worker
1581 * @worker: worker to be destroyed
1582 *
Tejun Heoc8e55f32010-06-29 10:07:12 +02001583 * Destroy @worker and adjust @gcwq stats accordingly.
1584 *
1585 * CONTEXT:
1586 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001587 */
1588static void destroy_worker(struct worker *worker)
1589{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001590 struct worker_pool *pool = worker->pool;
1591 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc34056a2010-06-29 10:07:11 +02001592 int id = worker->id;
1593
1594 /* sanity check frenzy */
1595 BUG_ON(worker->current_work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001596 BUG_ON(!list_empty(&worker->scheduled));
Tejun Heoc34056a2010-06-29 10:07:11 +02001597
Tejun Heoc8e55f32010-06-29 10:07:12 +02001598 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001599 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001600 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001601 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001602
1603 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001604 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001605
1606 spin_unlock_irq(&gcwq->lock);
1607
Tejun Heoc34056a2010-06-29 10:07:11 +02001608 kthread_stop(worker->task);
1609 kfree(worker);
1610
Tejun Heo8b03ae32010-06-29 10:07:12 +02001611 spin_lock_irq(&gcwq->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001612 ida_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001613}
1614
Tejun Heo63d95a92012-07-12 14:46:37 -07001615static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001616{
Tejun Heo63d95a92012-07-12 14:46:37 -07001617 struct worker_pool *pool = (void *)__pool;
1618 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001619
1620 spin_lock_irq(&gcwq->lock);
1621
Tejun Heo63d95a92012-07-12 14:46:37 -07001622 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001623 struct worker *worker;
1624 unsigned long expires;
1625
1626 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001627 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001628 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1629
1630 if (time_before(jiffies, expires))
Tejun Heo63d95a92012-07-12 14:46:37 -07001631 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001632 else {
1633 /* it's been idle for too long, wake up manager */
Tejun Heo11ebea52012-07-12 14:46:37 -07001634 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -07001635 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001636 }
1637 }
1638
1639 spin_unlock_irq(&gcwq->lock);
1640}
1641
1642static bool send_mayday(struct work_struct *work)
1643{
1644 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1645 struct workqueue_struct *wq = cwq->wq;
Tejun Heof3421792010-07-02 10:03:51 +02001646 unsigned int cpu;
Tejun Heoe22bee72010-06-29 10:07:14 +02001647
1648 if (!(wq->flags & WQ_RESCUER))
1649 return false;
1650
1651 /* mayday mayday mayday */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001652 cpu = cwq->pool->gcwq->cpu;
Tejun Heof3421792010-07-02 10:03:51 +02001653 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1654 if (cpu == WORK_CPU_UNBOUND)
1655 cpu = 0;
Tejun Heof2e005a2010-07-20 15:59:09 +02001656 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001657 wake_up_process(wq->rescuer->task);
1658 return true;
1659}
1660
Tejun Heo63d95a92012-07-12 14:46:37 -07001661static void gcwq_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001662{
Tejun Heo63d95a92012-07-12 14:46:37 -07001663 struct worker_pool *pool = (void *)__pool;
1664 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02001665 struct work_struct *work;
1666
1667 spin_lock_irq(&gcwq->lock);
1668
Tejun Heo63d95a92012-07-12 14:46:37 -07001669 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001670 /*
1671 * We've been trying to create a new worker but
1672 * haven't been successful. We might be hitting an
1673 * allocation deadlock. Send distress signals to
1674 * rescuers.
1675 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001676 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001677 send_mayday(work);
1678 }
1679
1680 spin_unlock_irq(&gcwq->lock);
1681
Tejun Heo63d95a92012-07-12 14:46:37 -07001682 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001683}
1684
1685/**
1686 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001687 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001688 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001689 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001690 * have at least one idle worker on return from this function. If
1691 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001692 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001693 * possible allocation deadlock.
1694 *
1695 * On return, need_to_create_worker() is guaranteed to be false and
1696 * may_start_working() true.
1697 *
1698 * LOCKING:
1699 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1700 * multiple times. Does GFP_KERNEL allocations. Called only from
1701 * manager.
1702 *
1703 * RETURNS:
1704 * false if no action was taken and gcwq->lock stayed locked, true
1705 * otherwise.
1706 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001707static bool maybe_create_worker(struct worker_pool *pool)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001708__releases(&gcwq->lock)
1709__acquires(&gcwq->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001710{
Tejun Heo63d95a92012-07-12 14:46:37 -07001711 struct global_cwq *gcwq = pool->gcwq;
1712
1713 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001714 return false;
1715restart:
Tejun Heo9f9c2362010-07-14 11:31:20 +02001716 spin_unlock_irq(&gcwq->lock);
1717
Tejun Heoe22bee72010-06-29 10:07:14 +02001718 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07001719 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001720
1721 while (true) {
1722 struct worker *worker;
1723
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001724 worker = create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001725 if (worker) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001726 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001727 spin_lock_irq(&gcwq->lock);
1728 start_worker(worker);
Tejun Heo63d95a92012-07-12 14:46:37 -07001729 BUG_ON(need_to_create_worker(pool));
Tejun Heoe22bee72010-06-29 10:07:14 +02001730 return true;
1731 }
1732
Tejun Heo63d95a92012-07-12 14:46:37 -07001733 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001734 break;
1735
Tejun Heoe22bee72010-06-29 10:07:14 +02001736 __set_current_state(TASK_INTERRUPTIBLE);
1737 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001738
Tejun Heo63d95a92012-07-12 14:46:37 -07001739 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001740 break;
1741 }
1742
Tejun Heo63d95a92012-07-12 14:46:37 -07001743 del_timer_sync(&pool->mayday_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02001744 spin_lock_irq(&gcwq->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07001745 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001746 goto restart;
1747 return true;
1748}
1749
1750/**
1751 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo63d95a92012-07-12 14:46:37 -07001752 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001753 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001754 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02001755 * IDLE_WORKER_TIMEOUT.
1756 *
1757 * LOCKING:
1758 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1759 * multiple times. Called only from manager.
1760 *
1761 * RETURNS:
1762 * false if no action was taken and gcwq->lock stayed locked, true
1763 * otherwise.
1764 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001765static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001766{
1767 bool ret = false;
1768
Tejun Heo63d95a92012-07-12 14:46:37 -07001769 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001770 struct worker *worker;
1771 unsigned long expires;
1772
Tejun Heo63d95a92012-07-12 14:46:37 -07001773 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001774 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1775
1776 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001777 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001778 break;
1779 }
1780
1781 destroy_worker(worker);
1782 ret = true;
1783 }
1784
1785 return ret;
1786}
1787
1788/**
1789 * manage_workers - manage worker pool
1790 * @worker: self
1791 *
1792 * Assume the manager role and manage gcwq worker pool @worker belongs
1793 * to. At any given time, there can be only zero or one manager per
1794 * gcwq. The exclusion is handled automatically by this function.
1795 *
1796 * The caller can safely start processing works on false return. On
1797 * true return, it's guaranteed that need_to_create_worker() is false
1798 * and may_start_working() is true.
1799 *
1800 * CONTEXT:
1801 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1802 * multiple times. Does GFP_KERNEL allocations.
1803 *
1804 * RETURNS:
1805 * false if no action was taken and gcwq->lock stayed locked, true if
1806 * some action was taken.
1807 */
1808static bool manage_workers(struct worker *worker)
1809{
Tejun Heo63d95a92012-07-12 14:46:37 -07001810 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001811 bool ret = false;
1812
Tejun Heo60373152012-07-17 12:39:27 -07001813 if (!mutex_trylock(&pool->manager_mutex))
Tejun Heoe22bee72010-06-29 10:07:14 +02001814 return ret;
1815
Tejun Heo11ebea52012-07-12 14:46:37 -07001816 pool->flags &= ~POOL_MANAGE_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02001817
1818 /*
1819 * Destroy and then create so that may_start_working() is true
1820 * on return.
1821 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001822 ret |= maybe_destroy_workers(pool);
1823 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001824
Tejun Heo60373152012-07-17 12:39:27 -07001825 mutex_unlock(&pool->manager_mutex);
Tejun Heoe22bee72010-06-29 10:07:14 +02001826 return ret;
1827}
1828
Tejun Heoa62428c2010-06-29 10:07:10 +02001829/**
Tejun Heoaffee4b2010-06-29 10:07:12 +02001830 * move_linked_works - move linked works to a list
1831 * @work: start of series of works to be scheduled
1832 * @head: target list to append @work to
1833 * @nextp: out paramter for nested worklist walking
1834 *
1835 * Schedule linked works starting from @work to @head. Work series to
1836 * be scheduled starts at @work and includes any consecutive work with
1837 * WORK_STRUCT_LINKED set in its predecessor.
1838 *
1839 * If @nextp is not NULL, it's updated to point to the next work of
1840 * the last scheduled work. This allows move_linked_works() to be
1841 * nested inside outer list_for_each_entry_safe().
1842 *
1843 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001844 * spin_lock_irq(gcwq->lock).
Tejun Heoaffee4b2010-06-29 10:07:12 +02001845 */
1846static void move_linked_works(struct work_struct *work, struct list_head *head,
1847 struct work_struct **nextp)
1848{
1849 struct work_struct *n;
1850
1851 /*
1852 * Linked worklist will always end before the end of the list,
1853 * use NULL for list head.
1854 */
1855 list_for_each_entry_safe_from(work, n, NULL, entry) {
1856 list_move_tail(&work->entry, head);
1857 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1858 break;
1859 }
1860
1861 /*
1862 * If we're already inside safe list traversal and have moved
1863 * multiple works to the scheduled queue, the next position
1864 * needs to be updated.
1865 */
1866 if (nextp)
1867 *nextp = n;
1868}
1869
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001870static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
1871{
1872 struct work_struct *work = list_first_entry(&cwq->delayed_works,
1873 struct work_struct, entry);
1874
Tejun Heocdadf002010-10-05 10:49:55 +02001875 trace_workqueue_activate_work(work);
Tejun Heo32704762012-07-13 22:16:45 -07001876 move_linked_works(work, &cwq->pool->worklist, NULL);
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001877 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001878 cwq->nr_active++;
1879}
1880
Tejun Heoaffee4b2010-06-29 10:07:12 +02001881/**
Tejun Heo73f53c42010-06-29 10:07:11 +02001882 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
1883 * @cwq: cwq of interest
1884 * @color: color of work which left the queue
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001885 * @delayed: for a delayed work
Tejun Heo73f53c42010-06-29 10:07:11 +02001886 *
1887 * A work either has completed or is removed from pending queue,
1888 * decrement nr_in_flight of its cwq and handle workqueue flushing.
1889 *
1890 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001891 * spin_lock_irq(gcwq->lock).
Tejun Heo73f53c42010-06-29 10:07:11 +02001892 */
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001893static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color,
1894 bool delayed)
Tejun Heo73f53c42010-06-29 10:07:11 +02001895{
1896 /* ignore uncolored works */
1897 if (color == WORK_NO_COLOR)
1898 return;
1899
1900 cwq->nr_in_flight[color]--;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001901
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001902 if (!delayed) {
1903 cwq->nr_active--;
1904 if (!list_empty(&cwq->delayed_works)) {
1905 /* one down, submit a delayed one */
1906 if (cwq->nr_active < cwq->max_active)
1907 cwq_activate_first_delayed(cwq);
1908 }
Tejun Heo502ca9d2010-06-29 10:07:13 +02001909 }
Tejun Heo73f53c42010-06-29 10:07:11 +02001910
1911 /* is flush in progress and are we at the flushing tip? */
1912 if (likely(cwq->flush_color != color))
1913 return;
1914
1915 /* are there still in-flight works? */
1916 if (cwq->nr_in_flight[color])
1917 return;
1918
1919 /* this cwq is done, clear flush_color */
1920 cwq->flush_color = -1;
1921
1922 /*
1923 * If this was the last cwq, wake up the first flusher. It
1924 * will handle the rest.
1925 */
1926 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
1927 complete(&cwq->wq->first_flusher->done);
1928}
1929
1930/**
Tejun Heoa62428c2010-06-29 10:07:10 +02001931 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02001932 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02001933 * @work: work to process
1934 *
1935 * Process @work. This function contains all the logics necessary to
1936 * process a single work including synchronization against and
1937 * interaction with other workers on the same cpu, queueing and
1938 * flushing. As long as context requirement is met, any worker can
1939 * call this function to process a work.
1940 *
1941 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02001942 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02001943 */
Tejun Heoc34056a2010-06-29 10:07:11 +02001944static void process_one_work(struct worker *worker, struct work_struct *work)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09001945__releases(&gcwq->lock)
1946__acquires(&gcwq->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02001947{
Tejun Heo7e116292010-06-29 10:07:13 +02001948 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001949 struct worker_pool *pool = worker->pool;
1950 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001951 struct hlist_head *bwh = busy_worker_head(gcwq, work);
Tejun Heofb0e7be2010-06-29 10:07:15 +02001952 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heoa62428c2010-06-29 10:07:10 +02001953 work_func_t f = work->func;
Tejun Heo73f53c42010-06-29 10:07:11 +02001954 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02001955 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02001956#ifdef CONFIG_LOCKDEP
1957 /*
1958 * It is permissible to free the struct work_struct from
1959 * inside the function that is called from it, this we need to
1960 * take into account for lockdep too. To avoid bogus "held
1961 * lock freed" warnings as well as problems when looking into
1962 * work->lockdep_map, make a copy and use that here.
1963 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07001964 struct lockdep_map lockdep_map;
1965
1966 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02001967#endif
Tejun Heo25511a42012-07-17 12:39:27 -07001968 WARN_ON_ONCE(!(worker->flags & (WORKER_UNBOUND | WORKER_REBIND)) &&
1969 raw_smp_processor_id() != gcwq->cpu);
1970
Tejun Heo7e116292010-06-29 10:07:13 +02001971 /*
1972 * A single work shouldn't be executed concurrently by
1973 * multiple workers on a single cpu. Check whether anyone is
1974 * already processing the work. If so, defer the work to the
1975 * currently executing one.
1976 */
1977 collision = __find_worker_executing_work(gcwq, bwh, work);
1978 if (unlikely(collision)) {
1979 move_linked_works(work, &collision->scheduled, NULL);
1980 return;
1981 }
1982
Tejun Heoa62428c2010-06-29 10:07:10 +02001983 /* claim and process */
Tejun Heoa62428c2010-06-29 10:07:10 +02001984 debug_work_deactivate(work);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001985 hlist_add_head(&worker->hentry, bwh);
Tejun Heoc34056a2010-06-29 10:07:11 +02001986 worker->current_work = work;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02001987 worker->current_cwq = cwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02001988 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02001989
Tejun Heo7a22ad72010-06-29 10:07:13 +02001990 /* record the current cpu number in the work data and dequeue */
1991 set_work_cpu(work, gcwq->cpu);
Tejun Heoa62428c2010-06-29 10:07:10 +02001992 list_del_init(&work->entry);
1993
Tejun Heo649027d2010-06-29 10:07:14 +02001994 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02001995 * CPU intensive works don't participate in concurrency
1996 * management. They're the scheduler's responsibility.
1997 */
1998 if (unlikely(cpu_intensive))
1999 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
2000
Tejun Heo974271c2012-07-12 14:46:37 -07002001 /*
2002 * Unbound gcwq isn't concurrency managed and work items should be
2003 * executed ASAP. Wake up another worker if necessary.
2004 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002005 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
2006 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07002007
Tejun Heo8b03ae32010-06-29 10:07:12 +02002008 spin_unlock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002009
Tejun Heoa62428c2010-06-29 10:07:10 +02002010 work_clear_pending(work);
Tejun Heoe1594892011-01-09 23:32:15 +01002011 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002012 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002013 trace_workqueue_execute_start(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002014 f(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002015 /*
2016 * While we must be careful to not use "work" after this, the trace
2017 * point will only record its address.
2018 */
2019 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002020 lock_map_release(&lockdep_map);
2021 lock_map_release(&cwq->wq->lockdep_map);
2022
2023 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
2024 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
2025 "%s/0x%08x/%d\n",
2026 current->comm, preempt_count(), task_pid_nr(current));
2027 printk(KERN_ERR " last function: ");
2028 print_symbol("%s\n", (unsigned long)f);
2029 debug_show_held_locks(current);
2030 dump_stack();
2031 }
2032
Tejun Heo8b03ae32010-06-29 10:07:12 +02002033 spin_lock_irq(&gcwq->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002034
Tejun Heofb0e7be2010-06-29 10:07:15 +02002035 /* clear cpu intensive status */
2036 if (unlikely(cpu_intensive))
2037 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2038
Tejun Heoa62428c2010-06-29 10:07:10 +02002039 /* we're done with it, release */
Tejun Heoc8e55f32010-06-29 10:07:12 +02002040 hlist_del_init(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002041 worker->current_work = NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02002042 worker->current_cwq = NULL;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02002043 cwq_dec_nr_in_flight(cwq, work_color, false);
Tejun Heoa62428c2010-06-29 10:07:10 +02002044}
2045
Tejun Heoaffee4b2010-06-29 10:07:12 +02002046/**
2047 * process_scheduled_works - process scheduled works
2048 * @worker: self
2049 *
2050 * Process all scheduled works. Please note that the scheduled list
2051 * may change while processing a work, so this function repeatedly
2052 * fetches a work from the top and executes it.
2053 *
2054 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002055 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02002056 * multiple times.
2057 */
2058static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002060 while (!list_empty(&worker->scheduled)) {
2061 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002063 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065}
2066
Tejun Heo4690c4a2010-06-29 10:07:10 +02002067/**
2068 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02002069 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02002070 *
Tejun Heoe22bee72010-06-29 10:07:14 +02002071 * The gcwq worker thread function. There's a single dynamic pool of
2072 * these per each cpu. These workers process all works regardless of
2073 * their specific target workqueue. The only exception is works which
2074 * belong to workqueues with a rescuer which will be explained in
2075 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02002076 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002077static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
Tejun Heoc34056a2010-06-29 10:07:11 +02002079 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07002080 struct worker_pool *pool = worker->pool;
2081 struct global_cwq *gcwq = pool->gcwq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Tejun Heoe22bee72010-06-29 10:07:14 +02002083 /* tell the scheduler that this is a workqueue worker */
2084 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002085woke_up:
Tejun Heoc8e55f32010-06-29 10:07:12 +02002086 spin_lock_irq(&gcwq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Tejun Heo25511a42012-07-17 12:39:27 -07002088 /*
2089 * DIE can be set only while idle and REBIND set while busy has
2090 * @worker->rebind_work scheduled. Checking here is enough.
2091 */
2092 if (unlikely(worker->flags & (WORKER_REBIND | WORKER_DIE))) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02002093 spin_unlock_irq(&gcwq->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07002094
2095 if (worker->flags & WORKER_DIE) {
2096 worker->task->flags &= ~PF_WQ_WORKER;
2097 return 0;
2098 }
2099
2100 idle_worker_rebind(worker);
2101 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 }
2103
Tejun Heoc8e55f32010-06-29 10:07:12 +02002104 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002105recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002106 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002107 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002108 goto sleep;
2109
2110 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002111 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002112 goto recheck;
2113
Tejun Heoc8e55f32010-06-29 10:07:12 +02002114 /*
2115 * ->scheduled list can only be filled while a worker is
2116 * preparing to process a work or actually processing it.
2117 * Make sure nobody diddled with it while I was sleeping.
2118 */
2119 BUG_ON(!list_empty(&worker->scheduled));
2120
Tejun Heoe22bee72010-06-29 10:07:14 +02002121 /*
2122 * When control reaches this point, we're guaranteed to have
2123 * at least one idle worker or that someone else has already
2124 * assumed the manager role.
2125 */
2126 worker_clr_flags(worker, WORKER_PREP);
2127
2128 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002129 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002130 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002131 struct work_struct, entry);
2132
2133 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2134 /* optimization path, not strictly necessary */
2135 process_one_work(worker, work);
2136 if (unlikely(!list_empty(&worker->scheduled)))
2137 process_scheduled_works(worker);
2138 } else {
2139 move_linked_works(work, &worker->scheduled, NULL);
2140 process_scheduled_works(worker);
2141 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002142 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002143
Tejun Heoe22bee72010-06-29 10:07:14 +02002144 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002145sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07002146 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002147 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002148
Tejun Heoc8e55f32010-06-29 10:07:12 +02002149 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02002150 * gcwq->lock is held and there's no work to process and no
2151 * need to manage, sleep. Workers are woken up only while
2152 * holding gcwq->lock or from local cpu, so setting the
2153 * current state before releasing gcwq->lock is enough to
2154 * prevent losing any event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002155 */
2156 worker_enter_idle(worker);
2157 __set_current_state(TASK_INTERRUPTIBLE);
2158 spin_unlock_irq(&gcwq->lock);
2159 schedule();
2160 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161}
2162
Tejun Heoe22bee72010-06-29 10:07:14 +02002163/**
2164 * rescuer_thread - the rescuer thread function
2165 * @__wq: the associated workqueue
2166 *
2167 * Workqueue rescuer thread function. There's one rescuer for each
2168 * workqueue which has WQ_RESCUER set.
2169 *
2170 * Regular work processing on a gcwq may block trying to create a new
2171 * worker which uses GFP_KERNEL allocation which has slight chance of
2172 * developing into deadlock if some works currently on the same queue
2173 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2174 * the problem rescuer solves.
2175 *
2176 * When such condition is possible, the gcwq summons rescuers of all
2177 * workqueues which have works queued on the gcwq and let them process
2178 * those works so that forward progress can be guaranteed.
2179 *
2180 * This should happen rarely.
2181 */
2182static int rescuer_thread(void *__wq)
2183{
2184 struct workqueue_struct *wq = __wq;
2185 struct worker *rescuer = wq->rescuer;
2186 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heof3421792010-07-02 10:03:51 +02002187 bool is_unbound = wq->flags & WQ_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02002188 unsigned int cpu;
2189
2190 set_user_nice(current, RESCUER_NICE_LEVEL);
2191repeat:
2192 set_current_state(TASK_INTERRUPTIBLE);
2193
2194 if (kthread_should_stop())
2195 return 0;
2196
Tejun Heof3421792010-07-02 10:03:51 +02002197 /*
2198 * See whether any cpu is asking for help. Unbounded
2199 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
2200 */
Tejun Heof2e005a2010-07-20 15:59:09 +02002201 for_each_mayday_cpu(cpu, wq->mayday_mask) {
Tejun Heof3421792010-07-02 10:03:51 +02002202 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2203 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002204 struct worker_pool *pool = cwq->pool;
2205 struct global_cwq *gcwq = pool->gcwq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002206 struct work_struct *work, *n;
2207
2208 __set_current_state(TASK_RUNNING);
Tejun Heof2e005a2010-07-20 15:59:09 +02002209 mayday_clear_cpu(cpu, wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002210
2211 /* migrate to the target cpu if possible */
Tejun Heobd7bdd42012-07-12 14:46:37 -07002212 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002213 worker_maybe_bind_and_lock(rescuer);
2214
2215 /*
2216 * Slurp in all works issued via this workqueue and
2217 * process'em.
2218 */
2219 BUG_ON(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002220 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02002221 if (get_work_cwq(work) == cwq)
2222 move_linked_works(work, scheduled, &n);
2223
2224 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002225
2226 /*
2227 * Leave this gcwq. If keep_working() is %true, notify a
2228 * regular worker; otherwise, we end up with 0 concurrency
2229 * and stalling the execution.
2230 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002231 if (keep_working(pool))
2232 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002233
Tejun Heoe22bee72010-06-29 10:07:14 +02002234 spin_unlock_irq(&gcwq->lock);
2235 }
2236
2237 schedule();
2238 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239}
2240
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002241struct wq_barrier {
2242 struct work_struct work;
2243 struct completion done;
2244};
2245
2246static void wq_barrier_func(struct work_struct *work)
2247{
2248 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2249 complete(&barr->done);
2250}
2251
Tejun Heo4690c4a2010-06-29 10:07:10 +02002252/**
2253 * insert_wq_barrier - insert a barrier work
2254 * @cwq: cwq to insert barrier into
2255 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002256 * @target: target work to attach @barr to
2257 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002258 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002259 * @barr is linked to @target such that @barr is completed only after
2260 * @target finishes execution. Please note that the ordering
2261 * guarantee is observed only with respect to @target and on the local
2262 * cpu.
2263 *
2264 * Currently, a queued barrier can't be canceled. This is because
2265 * try_to_grab_pending() can't determine whether the work to be
2266 * grabbed is at the head of the queue and thus can't clear LINKED
2267 * flag of the previous work while there must be a valid next work
2268 * after a work with LINKED flag set.
2269 *
2270 * Note that when @worker is non-NULL, @target may be modified
2271 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002272 *
2273 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02002274 * spin_lock_irq(gcwq->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002275 */
Oleg Nesterov83c22522007-05-09 02:33:54 -07002276static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002277 struct wq_barrier *barr,
2278 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002279{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002280 struct list_head *head;
2281 unsigned int linked = 0;
2282
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002283 /*
Tejun Heo8b03ae32010-06-29 10:07:12 +02002284 * debugobject calls are safe here even with gcwq->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002285 * as we know for sure that this will not trigger any of the
2286 * checks and call back into the fixup functions where we
2287 * might deadlock.
2288 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002289 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002290 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002291 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002292
Tejun Heoaffee4b2010-06-29 10:07:12 +02002293 /*
2294 * If @target is currently being executed, schedule the
2295 * barrier to the worker; otherwise, put it after @target.
2296 */
2297 if (worker)
2298 head = worker->scheduled.next;
2299 else {
2300 unsigned long *bits = work_data_bits(target);
2301
2302 head = target->entry.next;
2303 /* there can already be other linked works, inherit and set */
2304 linked = *bits & WORK_STRUCT_LINKED;
2305 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2306 }
2307
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002308 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002309 insert_work(cwq, &barr->work, head,
2310 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002311}
2312
Tejun Heo73f53c42010-06-29 10:07:11 +02002313/**
2314 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2315 * @wq: workqueue being flushed
2316 * @flush_color: new flush color, < 0 for no-op
2317 * @work_color: new work color, < 0 for no-op
2318 *
2319 * Prepare cwqs for workqueue flushing.
2320 *
2321 * If @flush_color is non-negative, flush_color on all cwqs should be
2322 * -1. If no cwq has in-flight commands at the specified color, all
2323 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2324 * has in flight commands, its cwq->flush_color is set to
2325 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2326 * wakeup logic is armed and %true is returned.
2327 *
2328 * The caller should have initialized @wq->first_flusher prior to
2329 * calling this function with non-negative @flush_color. If
2330 * @flush_color is negative, no flush color update is done and %false
2331 * is returned.
2332 *
2333 * If @work_color is non-negative, all cwqs should have the same
2334 * work_color which is previous to @work_color and all will be
2335 * advanced to @work_color.
2336 *
2337 * CONTEXT:
2338 * mutex_lock(wq->flush_mutex).
2339 *
2340 * RETURNS:
2341 * %true if @flush_color >= 0 and there's something to flush. %false
2342 * otherwise.
2343 */
2344static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2345 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346{
Tejun Heo73f53c42010-06-29 10:07:11 +02002347 bool wait = false;
2348 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -07002349
Tejun Heo73f53c42010-06-29 10:07:11 +02002350 if (flush_color >= 0) {
2351 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2352 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002353 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002354
Tejun Heof3421792010-07-02 10:03:51 +02002355 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002356 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002357 struct global_cwq *gcwq = cwq->pool->gcwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002358
Tejun Heo8b03ae32010-06-29 10:07:12 +02002359 spin_lock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002360
2361 if (flush_color >= 0) {
2362 BUG_ON(cwq->flush_color != -1);
2363
2364 if (cwq->nr_in_flight[flush_color]) {
2365 cwq->flush_color = flush_color;
2366 atomic_inc(&wq->nr_cwqs_to_flush);
2367 wait = true;
2368 }
2369 }
2370
2371 if (work_color >= 0) {
2372 BUG_ON(work_color != work_next_color(cwq->work_color));
2373 cwq->work_color = work_color;
2374 }
2375
Tejun Heo8b03ae32010-06-29 10:07:12 +02002376 spin_unlock_irq(&gcwq->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002377 }
2378
2379 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2380 complete(&wq->first_flusher->done);
2381
2382 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383}
2384
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002385/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002387 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 *
2389 * Forces execution of the workqueue and blocks until its completion.
2390 * This is typically used in driver shutdown handlers.
2391 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002392 * We sleep until all works which were queued on entry have been handled,
2393 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002395void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396{
Tejun Heo73f53c42010-06-29 10:07:11 +02002397 struct wq_flusher this_flusher = {
2398 .list = LIST_HEAD_INIT(this_flusher.list),
2399 .flush_color = -1,
2400 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2401 };
2402 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002403
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002404 lock_map_acquire(&wq->lockdep_map);
2405 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002406
2407 mutex_lock(&wq->flush_mutex);
2408
2409 /*
2410 * Start-to-wait phase
2411 */
2412 next_color = work_next_color(wq->work_color);
2413
2414 if (next_color != wq->flush_color) {
2415 /*
2416 * Color space is not full. The current work_color
2417 * becomes our flush_color and work_color is advanced
2418 * by one.
2419 */
2420 BUG_ON(!list_empty(&wq->flusher_overflow));
2421 this_flusher.flush_color = wq->work_color;
2422 wq->work_color = next_color;
2423
2424 if (!wq->first_flusher) {
2425 /* no flush in progress, become the first flusher */
2426 BUG_ON(wq->flush_color != this_flusher.flush_color);
2427
2428 wq->first_flusher = &this_flusher;
2429
2430 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2431 wq->work_color)) {
2432 /* nothing to flush, done */
2433 wq->flush_color = next_color;
2434 wq->first_flusher = NULL;
2435 goto out_unlock;
2436 }
2437 } else {
2438 /* wait in queue */
2439 BUG_ON(wq->flush_color == this_flusher.flush_color);
2440 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2441 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2442 }
2443 } else {
2444 /*
2445 * Oops, color space is full, wait on overflow queue.
2446 * The next flush completion will assign us
2447 * flush_color and transfer to flusher_queue.
2448 */
2449 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2450 }
2451
2452 mutex_unlock(&wq->flush_mutex);
2453
2454 wait_for_completion(&this_flusher.done);
2455
2456 /*
2457 * Wake-up-and-cascade phase
2458 *
2459 * First flushers are responsible for cascading flushes and
2460 * handling overflow. Non-first flushers can simply return.
2461 */
2462 if (wq->first_flusher != &this_flusher)
2463 return;
2464
2465 mutex_lock(&wq->flush_mutex);
2466
Tejun Heo4ce48b32010-07-02 10:03:51 +02002467 /* we might have raced, check again with mutex held */
2468 if (wq->first_flusher != &this_flusher)
2469 goto out_unlock;
2470
Tejun Heo73f53c42010-06-29 10:07:11 +02002471 wq->first_flusher = NULL;
2472
2473 BUG_ON(!list_empty(&this_flusher.list));
2474 BUG_ON(wq->flush_color != this_flusher.flush_color);
2475
2476 while (true) {
2477 struct wq_flusher *next, *tmp;
2478
2479 /* complete all the flushers sharing the current flush color */
2480 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2481 if (next->flush_color != wq->flush_color)
2482 break;
2483 list_del_init(&next->list);
2484 complete(&next->done);
2485 }
2486
2487 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2488 wq->flush_color != work_next_color(wq->work_color));
2489
2490 /* this flush_color is finished, advance by one */
2491 wq->flush_color = work_next_color(wq->flush_color);
2492
2493 /* one color has been freed, handle overflow queue */
2494 if (!list_empty(&wq->flusher_overflow)) {
2495 /*
2496 * Assign the same color to all overflowed
2497 * flushers, advance work_color and append to
2498 * flusher_queue. This is the start-to-wait
2499 * phase for these overflowed flushers.
2500 */
2501 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2502 tmp->flush_color = wq->work_color;
2503
2504 wq->work_color = work_next_color(wq->work_color);
2505
2506 list_splice_tail_init(&wq->flusher_overflow,
2507 &wq->flusher_queue);
2508 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2509 }
2510
2511 if (list_empty(&wq->flusher_queue)) {
2512 BUG_ON(wq->flush_color != wq->work_color);
2513 break;
2514 }
2515
2516 /*
2517 * Need to flush more colors. Make the next flusher
2518 * the new first flusher and arm cwqs.
2519 */
2520 BUG_ON(wq->flush_color == wq->work_color);
2521 BUG_ON(wq->flush_color != next->flush_color);
2522
2523 list_del_init(&next->list);
2524 wq->first_flusher = next;
2525
2526 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2527 break;
2528
2529 /*
2530 * Meh... this color is already done, clear first
2531 * flusher and repeat cascading.
2532 */
2533 wq->first_flusher = NULL;
2534 }
2535
2536out_unlock:
2537 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538}
Dave Jonesae90dd52006-06-30 01:40:45 -04002539EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002541/**
2542 * drain_workqueue - drain a workqueue
2543 * @wq: workqueue to drain
2544 *
2545 * Wait until the workqueue becomes empty. While draining is in progress,
2546 * only chain queueing is allowed. IOW, only currently pending or running
2547 * work items on @wq can queue further work items on it. @wq is flushed
2548 * repeatedly until it becomes empty. The number of flushing is detemined
2549 * by the depth of chaining and should be relatively short. Whine if it
2550 * takes too long.
2551 */
2552void drain_workqueue(struct workqueue_struct *wq)
2553{
2554 unsigned int flush_cnt = 0;
2555 unsigned int cpu;
2556
2557 /*
2558 * __queue_work() needs to test whether there are drainers, is much
2559 * hotter than drain_workqueue() and already looks at @wq->flags.
2560 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2561 */
2562 spin_lock(&workqueue_lock);
2563 if (!wq->nr_drainers++)
2564 wq->flags |= WQ_DRAINING;
2565 spin_unlock(&workqueue_lock);
2566reflush:
2567 flush_workqueue(wq);
2568
2569 for_each_cwq_cpu(cpu, wq) {
2570 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002571 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002572
Tejun Heobd7bdd42012-07-12 14:46:37 -07002573 spin_lock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002574 drained = !cwq->nr_active && list_empty(&cwq->delayed_works);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002575 spin_unlock_irq(&cwq->pool->gcwq->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002576
2577 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002578 continue;
2579
2580 if (++flush_cnt == 10 ||
2581 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
2582 pr_warning("workqueue %s: flush on destruction isn't complete after %u tries\n",
2583 wq->name, flush_cnt);
2584 goto reflush;
2585 }
2586
2587 spin_lock(&workqueue_lock);
2588 if (!--wq->nr_drainers)
2589 wq->flags &= ~WQ_DRAINING;
2590 spin_unlock(&workqueue_lock);
2591}
2592EXPORT_SYMBOL_GPL(drain_workqueue);
2593
Tejun Heobaf59022010-09-16 10:42:16 +02002594static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr,
2595 bool wait_executing)
2596{
2597 struct worker *worker = NULL;
2598 struct global_cwq *gcwq;
2599 struct cpu_workqueue_struct *cwq;
2600
2601 might_sleep();
2602 gcwq = get_work_gcwq(work);
2603 if (!gcwq)
2604 return false;
2605
2606 spin_lock_irq(&gcwq->lock);
2607 if (!list_empty(&work->entry)) {
2608 /*
2609 * See the comment near try_to_grab_pending()->smp_rmb().
2610 * If it was re-queued to a different gcwq under us, we
2611 * are not going to wait.
2612 */
2613 smp_rmb();
2614 cwq = get_work_cwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002615 if (unlikely(!cwq || gcwq != cwq->pool->gcwq))
Tejun Heobaf59022010-09-16 10:42:16 +02002616 goto already_gone;
2617 } else if (wait_executing) {
2618 worker = find_worker_executing_work(gcwq, work);
2619 if (!worker)
2620 goto already_gone;
2621 cwq = worker->current_cwq;
2622 } else
2623 goto already_gone;
2624
2625 insert_wq_barrier(cwq, barr, work, worker);
2626 spin_unlock_irq(&gcwq->lock);
2627
Tejun Heoe1594892011-01-09 23:32:15 +01002628 /*
2629 * If @max_active is 1 or rescuer is in use, flushing another work
2630 * item on the same workqueue may lead to deadlock. Make sure the
2631 * flusher is not running on the same workqueue by verifying write
2632 * access.
2633 */
2634 if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER)
2635 lock_map_acquire(&cwq->wq->lockdep_map);
2636 else
2637 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heobaf59022010-09-16 10:42:16 +02002638 lock_map_release(&cwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002639
Tejun Heobaf59022010-09-16 10:42:16 +02002640 return true;
2641already_gone:
2642 spin_unlock_irq(&gcwq->lock);
2643 return false;
2644}
2645
Oleg Nesterovdb700892008-07-25 01:47:49 -07002646/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002647 * flush_work - wait for a work to finish executing the last queueing instance
2648 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002649 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002650 * Wait until @work has finished execution. This function considers
2651 * only the last queueing instance of @work. If @work has been
2652 * enqueued across different CPUs on a non-reentrant workqueue or on
2653 * multiple workqueues, @work might still be executing on return on
2654 * some of the CPUs from earlier queueing.
Oleg Nesterova67da702008-07-25 01:47:52 -07002655 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002656 * If @work was queued only on a non-reentrant, ordered or unbound
2657 * workqueue, @work is guaranteed to be idle on return if it hasn't
2658 * been requeued since flush started.
2659 *
2660 * RETURNS:
2661 * %true if flush_work() waited for the work to finish execution,
2662 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002663 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002664bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002665{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002666 struct wq_barrier barr;
2667
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002668 lock_map_acquire(&work->lockdep_map);
2669 lock_map_release(&work->lockdep_map);
2670
Tejun Heobaf59022010-09-16 10:42:16 +02002671 if (start_flush_work(work, &barr, true)) {
2672 wait_for_completion(&barr.done);
2673 destroy_work_on_stack(&barr.work);
2674 return true;
2675 } else
2676 return false;
Oleg Nesterovdb700892008-07-25 01:47:49 -07002677}
2678EXPORT_SYMBOL_GPL(flush_work);
2679
Tejun Heo401a8d02010-09-16 10:36:00 +02002680static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work)
2681{
2682 struct wq_barrier barr;
2683 struct worker *worker;
2684
2685 spin_lock_irq(&gcwq->lock);
2686
2687 worker = find_worker_executing_work(gcwq, work);
2688 if (unlikely(worker))
2689 insert_wq_barrier(worker->current_cwq, &barr, work, worker);
2690
2691 spin_unlock_irq(&gcwq->lock);
2692
2693 if (unlikely(worker)) {
2694 wait_for_completion(&barr.done);
2695 destroy_work_on_stack(&barr.work);
2696 return true;
2697 } else
2698 return false;
2699}
2700
2701static bool wait_on_work(struct work_struct *work)
2702{
2703 bool ret = false;
2704 int cpu;
2705
2706 might_sleep();
2707
2708 lock_map_acquire(&work->lockdep_map);
2709 lock_map_release(&work->lockdep_map);
2710
2711 for_each_gcwq_cpu(cpu)
2712 ret |= wait_on_cpu_work(get_gcwq(cpu), work);
2713 return ret;
2714}
2715
Tejun Heo09383492010-09-16 10:48:29 +02002716/**
2717 * flush_work_sync - wait until a work has finished execution
2718 * @work: the work to flush
2719 *
2720 * Wait until @work has finished execution. On return, it's
2721 * guaranteed that all queueing instances of @work which happened
2722 * before this function is called are finished. In other words, if
2723 * @work hasn't been requeued since this function was called, @work is
2724 * guaranteed to be idle on return.
2725 *
2726 * RETURNS:
2727 * %true if flush_work_sync() waited for the work to finish execution,
2728 * %false if it was already idle.
2729 */
2730bool flush_work_sync(struct work_struct *work)
2731{
2732 struct wq_barrier barr;
2733 bool pending, waited;
2734
2735 /* we'll wait for executions separately, queue barr only if pending */
2736 pending = start_flush_work(work, &barr, false);
2737
2738 /* wait for executions to finish */
2739 waited = wait_on_work(work);
2740
2741 /* wait for the pending one */
2742 if (pending) {
2743 wait_for_completion(&barr.done);
2744 destroy_work_on_stack(&barr.work);
2745 }
2746
2747 return pending || waited;
2748}
2749EXPORT_SYMBOL_GPL(flush_work_sync);
2750
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002751/*
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002752 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002753 * so this work can't be re-armed in any way.
2754 */
2755static int try_to_grab_pending(struct work_struct *work)
2756{
Tejun Heo8b03ae32010-06-29 10:07:12 +02002757 struct global_cwq *gcwq;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002758 int ret = -1;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002759
Tejun Heo22df02b2010-06-29 10:07:10 +02002760 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002761 return 0;
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002762
2763 /*
2764 * The queueing is in progress, or it is already queued. Try to
2765 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
2766 */
Tejun Heo7a22ad72010-06-29 10:07:13 +02002767 gcwq = get_work_gcwq(work);
2768 if (!gcwq)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002769 return ret;
2770
Tejun Heo8b03ae32010-06-29 10:07:12 +02002771 spin_lock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002772 if (!list_empty(&work->entry)) {
2773 /*
Tejun Heo7a22ad72010-06-29 10:07:13 +02002774 * This work is queued, but perhaps we locked the wrong gcwq.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002775 * In that case we must see the new value after rmb(), see
2776 * insert_work()->wmb().
2777 */
2778 smp_rmb();
Tejun Heo7a22ad72010-06-29 10:07:13 +02002779 if (gcwq == get_work_gcwq(work)) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002780 debug_work_deactivate(work);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002781 list_del_init(&work->entry);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002782 cwq_dec_nr_in_flight(get_work_cwq(work),
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02002783 get_work_color(work),
2784 *work_data_bits(work) & WORK_STRUCT_DELAYED);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002785 ret = 1;
2786 }
2787 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02002788 spin_unlock_irq(&gcwq->lock);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002789
2790 return ret;
2791}
2792
Tejun Heo401a8d02010-09-16 10:36:00 +02002793static bool __cancel_work_timer(struct work_struct *work,
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002794 struct timer_list* timer)
2795{
2796 int ret;
2797
2798 do {
2799 ret = (timer && likely(del_timer(timer)));
2800 if (!ret)
2801 ret = try_to_grab_pending(work);
2802 wait_on_work(work);
2803 } while (unlikely(ret < 0));
2804
Tejun Heo7a22ad72010-06-29 10:07:13 +02002805 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002806 return ret;
2807}
2808
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002809/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002810 * cancel_work_sync - cancel a work and wait for it to finish
2811 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002812 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002813 * Cancel @work and wait for its execution to finish. This function
2814 * can be used even if the work re-queues itself or migrates to
2815 * another workqueue. On return from this function, @work is
2816 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002817 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002818 * cancel_work_sync(&delayed_work->work) must not be used for
2819 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002820 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002821 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002822 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002823 *
2824 * RETURNS:
2825 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002826 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002827bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002828{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002829 return __cancel_work_timer(work, NULL);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002830}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002831EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002832
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002833/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002834 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2835 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002836 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002837 * Delayed timer is cancelled and the pending work is queued for
2838 * immediate execution. Like flush_work(), this function only
2839 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002840 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002841 * RETURNS:
2842 * %true if flush_work() waited for the work to finish execution,
2843 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002844 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002845bool flush_delayed_work(struct delayed_work *dwork)
2846{
2847 if (del_timer_sync(&dwork->timer))
2848 __queue_work(raw_smp_processor_id(),
2849 get_work_cwq(&dwork->work)->wq, &dwork->work);
2850 return flush_work(&dwork->work);
2851}
2852EXPORT_SYMBOL(flush_delayed_work);
2853
2854/**
Tejun Heo09383492010-09-16 10:48:29 +02002855 * flush_delayed_work_sync - wait for a dwork to finish
2856 * @dwork: the delayed work to flush
2857 *
2858 * Delayed timer is cancelled and the pending work is queued for
2859 * execution immediately. Other than timer handling, its behavior
2860 * is identical to flush_work_sync().
2861 *
2862 * RETURNS:
2863 * %true if flush_work_sync() waited for the work to finish execution,
2864 * %false if it was already idle.
2865 */
2866bool flush_delayed_work_sync(struct delayed_work *dwork)
2867{
2868 if (del_timer_sync(&dwork->timer))
2869 __queue_work(raw_smp_processor_id(),
2870 get_work_cwq(&dwork->work)->wq, &dwork->work);
2871 return flush_work_sync(&dwork->work);
2872}
2873EXPORT_SYMBOL(flush_delayed_work_sync);
2874
2875/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002876 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2877 * @dwork: the delayed work cancel
2878 *
2879 * This is cancel_work_sync() for delayed works.
2880 *
2881 * RETURNS:
2882 * %true if @dwork was pending, %false otherwise.
2883 */
2884bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002885{
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002886 return __cancel_work_timer(&dwork->work, &dwork->timer);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002887}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002888EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002890/**
2891 * schedule_work - put work task in global workqueue
2892 * @work: job to be done
2893 *
Bart Van Assche5b0f437d2009-07-30 19:00:53 +02002894 * Returns zero if @work was already on the kernel-global workqueue and
2895 * non-zero otherwise.
2896 *
2897 * This puts a job in the kernel-global workqueue if it was not already
2898 * queued and leaves it in the same position on the kernel-global
2899 * workqueue otherwise.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002900 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002901int schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902{
Tejun Heod320c032010-06-29 10:07:14 +02002903 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904}
Dave Jonesae90dd52006-06-30 01:40:45 -04002905EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
Zhang Ruic1a220e2008-07-23 21:28:39 -07002907/*
2908 * schedule_work_on - put work task on a specific cpu
2909 * @cpu: cpu to put the work task on
2910 * @work: job to be done
2911 *
2912 * This puts a job on a specific cpu
2913 */
2914int schedule_work_on(int cpu, struct work_struct *work)
2915{
Tejun Heod320c032010-06-29 10:07:14 +02002916 return queue_work_on(cpu, system_wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07002917}
2918EXPORT_SYMBOL(schedule_work_on);
2919
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002920/**
2921 * schedule_delayed_work - put work task in global workqueue after delay
David Howells52bad642006-11-22 14:54:01 +00002922 * @dwork: job to be done
2923 * @delay: number of jiffies to wait or 0 for immediate execution
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002924 *
2925 * After waiting for a given time this puts a job in the kernel-global
2926 * workqueue.
2927 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002928int schedule_delayed_work(struct delayed_work *dwork,
Ingo Molnar82f67cd2007-02-16 01:28:13 -08002929 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930{
Tejun Heod320c032010-06-29 10:07:14 +02002931 return queue_delayed_work(system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932}
Dave Jonesae90dd52006-06-30 01:40:45 -04002933EXPORT_SYMBOL(schedule_delayed_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002935/**
2936 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
2937 * @cpu: cpu to use
David Howells52bad642006-11-22 14:54:01 +00002938 * @dwork: job to be done
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002939 * @delay: number of jiffies to wait
2940 *
2941 * After waiting for a given time this puts a job in the kernel-global
2942 * workqueue on the specified CPU.
2943 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944int schedule_delayed_work_on(int cpu,
David Howells52bad642006-11-22 14:54:01 +00002945 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946{
Tejun Heod320c032010-06-29 10:07:14 +02002947 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948}
Dave Jonesae90dd52006-06-30 01:40:45 -04002949EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
Andrew Mortonb6136772006-06-25 05:47:49 -07002951/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002952 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002953 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002954 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002955 * schedule_on_each_cpu() executes @func on each online CPU using the
2956 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002957 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002958 *
2959 * RETURNS:
2960 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002961 */
David Howells65f27f32006-11-22 14:55:48 +00002962int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002963{
2964 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002965 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002966
Andrew Mortonb6136772006-06-25 05:47:49 -07002967 works = alloc_percpu(struct work_struct);
2968 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002969 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002970
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002971 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002972
Christoph Lameter15316ba2006-01-08 01:00:43 -08002973 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002974 struct work_struct *work = per_cpu_ptr(works, cpu);
2975
2976 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002977 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002978 }
Tejun Heo93981802009-11-17 14:06:20 -08002979
2980 for_each_online_cpu(cpu)
2981 flush_work(per_cpu_ptr(works, cpu));
2982
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002983 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002984 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002985 return 0;
2986}
2987
Alan Sterneef6a7d2010-02-12 17:39:21 +09002988/**
2989 * flush_scheduled_work - ensure that any scheduled work has run to completion.
2990 *
2991 * Forces execution of the kernel-global workqueue and blocks until its
2992 * completion.
2993 *
2994 * Think twice before calling this function! It's very easy to get into
2995 * trouble if you don't take great care. Either of the following situations
2996 * will lead to deadlock:
2997 *
2998 * One of the work items currently on the workqueue needs to acquire
2999 * a lock held by your code or its caller.
3000 *
3001 * Your code is running in the context of a work routine.
3002 *
3003 * They will be detected by lockdep when they occur, but the first might not
3004 * occur very often. It depends on what work items are on the workqueue and
3005 * what locks they need, which you have no control over.
3006 *
3007 * In most situations flushing the entire workqueue is overkill; you merely
3008 * need to know that a particular work item isn't queued and isn't running.
3009 * In such cases you should use cancel_delayed_work_sync() or
3010 * cancel_work_sync() instead.
3011 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012void flush_scheduled_work(void)
3013{
Tejun Heod320c032010-06-29 10:07:14 +02003014 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015}
Dave Jonesae90dd52006-06-30 01:40:45 -04003016EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
3018/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06003019 * execute_in_process_context - reliably execute the routine with user context
3020 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06003021 * @ew: guaranteed storage for the execute work structure (must
3022 * be available when the work executes)
3023 *
3024 * Executes the function immediately if process context is available,
3025 * otherwise schedules the function for delayed execution.
3026 *
3027 * Returns: 0 - function was executed
3028 * 1 - function was scheduled for execution
3029 */
David Howells65f27f32006-11-22 14:55:48 +00003030int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06003031{
3032 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00003033 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003034 return 0;
3035 }
3036
David Howells65f27f32006-11-22 14:55:48 +00003037 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003038 schedule_work(&ew->work);
3039
3040 return 1;
3041}
3042EXPORT_SYMBOL_GPL(execute_in_process_context);
3043
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044int keventd_up(void)
3045{
Tejun Heod320c032010-06-29 10:07:14 +02003046 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047}
3048
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003049static int alloc_cwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050{
Oleg Nesterov3af244332007-05-09 02:34:09 -07003051 /*
Tejun Heo0f900042010-06-29 10:07:11 +02003052 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
3053 * Make sure that the alignment isn't lower than that of
3054 * unsigned long long.
Oleg Nesterov3af244332007-05-09 02:34:09 -07003055 */
Tejun Heo0f900042010-06-29 10:07:11 +02003056 const size_t size = sizeof(struct cpu_workqueue_struct);
3057 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
3058 __alignof__(unsigned long long));
Oleg Nesterov3af244332007-05-09 02:34:09 -07003059
Lai Jiangshane06ffa12012-03-09 18:03:20 +08003060 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02003061 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
Tejun Heo931ac772010-07-20 11:07:48 +02003062 else {
Tejun Heof3421792010-07-02 10:03:51 +02003063 void *ptr;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01003064
Tejun Heof3421792010-07-02 10:03:51 +02003065 /*
3066 * Allocate enough room to align cwq and put an extra
3067 * pointer at the end pointing back to the originally
3068 * allocated pointer which will be used for free.
3069 */
3070 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
3071 if (ptr) {
3072 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
3073 *(void **)(wq->cpu_wq.single + 1) = ptr;
3074 }
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003075 }
Tejun Heof3421792010-07-02 10:03:51 +02003076
Tejun Heo0415b00d12011-03-24 18:50:09 +01003077 /* just in case, make sure it's actually aligned */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003078 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
3079 return wq->cpu_wq.v ? 0 : -ENOMEM;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003080}
3081
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003082static void free_cwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07003083{
Lai Jiangshane06ffa12012-03-09 18:03:20 +08003084 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02003085 free_percpu(wq->cpu_wq.pcpu);
3086 else if (wq->cpu_wq.single) {
3087 /* the pointer to free is stored right after the cwq */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003088 kfree(*(void **)(wq->cpu_wq.single + 1));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07003089 }
3090}
3091
Tejun Heof3421792010-07-02 10:03:51 +02003092static int wq_clamp_max_active(int max_active, unsigned int flags,
3093 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003094{
Tejun Heof3421792010-07-02 10:03:51 +02003095 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3096
3097 if (max_active < 1 || max_active > lim)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003098 printk(KERN_WARNING "workqueue: max_active %d requested for %s "
3099 "is out of range, clamping between %d and %d\n",
Tejun Heof3421792010-07-02 10:03:51 +02003100 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003101
Tejun Heof3421792010-07-02 10:03:51 +02003102 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003103}
3104
Tejun Heob196be82012-01-10 15:11:35 -08003105struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02003106 unsigned int flags,
3107 int max_active,
3108 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08003109 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003110{
Tejun Heob196be82012-01-10 15:11:35 -08003111 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003112 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02003113 unsigned int cpu;
Tejun Heob196be82012-01-10 15:11:35 -08003114 size_t namelen;
3115
3116 /* determine namelen, allocate wq and format name */
3117 va_start(args, lock_name);
3118 va_copy(args1, args);
3119 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3120
3121 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3122 if (!wq)
3123 goto err;
3124
3125 vsnprintf(wq->name, namelen, fmt, args1);
3126 va_end(args);
3127 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003128
Tejun Heof3421792010-07-02 10:03:51 +02003129 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02003130 * Workqueues which may be used during memory reclaim should
3131 * have a rescuer to guarantee forward progress.
3132 */
3133 if (flags & WQ_MEM_RECLAIM)
3134 flags |= WQ_RESCUER;
3135
Tejun Heod320c032010-06-29 10:07:14 +02003136 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003137 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003138
Tejun Heob196be82012-01-10 15:11:35 -08003139 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003140 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003141 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003142 mutex_init(&wq->flush_mutex);
3143 atomic_set(&wq->nr_cwqs_to_flush, 0);
3144 INIT_LIST_HEAD(&wq->flusher_queue);
3145 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003146
Johannes Bergeb13ba82008-01-16 09:51:58 +01003147 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003148 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003149
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003150 if (alloc_cwqs(wq) < 0)
3151 goto err;
3152
Tejun Heof3421792010-07-02 10:03:51 +02003153 for_each_cwq_cpu(cpu, wq) {
Tejun Heo15376632010-06-29 10:07:11 +02003154 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003155 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo32704762012-07-13 22:16:45 -07003156 int pool_idx = (bool)(flags & WQ_HIGHPRI);
Tejun Heo15376632010-06-29 10:07:11 +02003157
Tejun Heo0f900042010-06-29 10:07:11 +02003158 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heo32704762012-07-13 22:16:45 -07003159 cwq->pool = &gcwq->pools[pool_idx];
Tejun Heoc34056a2010-06-29 10:07:11 +02003160 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02003161 cwq->flush_color = -1;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003162 cwq->max_active = max_active;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003163 INIT_LIST_HEAD(&cwq->delayed_works);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003164 }
3165
Tejun Heoe22bee72010-06-29 10:07:14 +02003166 if (flags & WQ_RESCUER) {
3167 struct worker *rescuer;
3168
Tejun Heof2e005a2010-07-20 15:59:09 +02003169 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
Tejun Heoe22bee72010-06-29 10:07:14 +02003170 goto err;
3171
3172 wq->rescuer = rescuer = alloc_worker();
3173 if (!rescuer)
3174 goto err;
3175
Tejun Heob196be82012-01-10 15:11:35 -08003176 rescuer->task = kthread_create(rescuer_thread, wq, "%s",
3177 wq->name);
Tejun Heoe22bee72010-06-29 10:07:14 +02003178 if (IS_ERR(rescuer->task))
3179 goto err;
3180
Tejun Heoe22bee72010-06-29 10:07:14 +02003181 rescuer->task->flags |= PF_THREAD_BOUND;
3182 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003183 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003184
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003185 /*
3186 * workqueue_lock protects global freeze state and workqueues
3187 * list. Grab it, set max_active accordingly and add the new
3188 * workqueue to workqueues list.
3189 */
Tejun Heo15376632010-06-29 10:07:11 +02003190 spin_lock(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003191
Tejun Heo58a69cb2011-02-16 09:25:31 +01003192 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heof3421792010-07-02 10:03:51 +02003193 for_each_cwq_cpu(cpu, wq)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003194 get_cwq(cpu, wq)->max_active = 0;
3195
Tejun Heo15376632010-06-29 10:07:11 +02003196 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003197
Tejun Heo15376632010-06-29 10:07:11 +02003198 spin_unlock(&workqueue_lock);
3199
Oleg Nesterov3af244332007-05-09 02:34:09 -07003200 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02003201err:
3202 if (wq) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003203 free_cwqs(wq);
Tejun Heof2e005a2010-07-20 15:59:09 +02003204 free_mayday_mask(wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02003205 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003206 kfree(wq);
3207 }
3208 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003209}
Tejun Heod320c032010-06-29 10:07:14 +02003210EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003211
3212/**
3213 * destroy_workqueue - safely terminate a workqueue
3214 * @wq: target workqueue
3215 *
3216 * Safely destroy a workqueue. All work currently pending will be done first.
3217 */
3218void destroy_workqueue(struct workqueue_struct *wq)
3219{
Tejun Heoc8e55f32010-06-29 10:07:12 +02003220 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003221
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003222 /* drain it before proceeding with destruction */
3223 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003224
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003225 /*
3226 * wq list is used to freeze wq, remove from list after
3227 * flushing is complete in case freeze races us.
3228 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003229 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07003230 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003231 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003232
Tejun Heoe22bee72010-06-29 10:07:14 +02003233 /* sanity check */
Tejun Heof3421792010-07-02 10:03:51 +02003234 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02003235 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3236 int i;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003237
Tejun Heo73f53c42010-06-29 10:07:11 +02003238 for (i = 0; i < WORK_NR_COLORS; i++)
3239 BUG_ON(cwq->nr_in_flight[i]);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003240 BUG_ON(cwq->nr_active);
3241 BUG_ON(!list_empty(&cwq->delayed_works));
Tejun Heo73f53c42010-06-29 10:07:11 +02003242 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003243
Tejun Heoe22bee72010-06-29 10:07:14 +02003244 if (wq->flags & WQ_RESCUER) {
3245 kthread_stop(wq->rescuer->task);
Tejun Heof2e005a2010-07-20 15:59:09 +02003246 free_mayday_mask(wq->mayday_mask);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003247 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003248 }
3249
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003250 free_cwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003251 kfree(wq);
3252}
3253EXPORT_SYMBOL_GPL(destroy_workqueue);
3254
Tejun Heodcd989c2010-06-29 10:07:14 +02003255/**
3256 * workqueue_set_max_active - adjust max_active of a workqueue
3257 * @wq: target workqueue
3258 * @max_active: new max_active value.
3259 *
3260 * Set max_active of @wq to @max_active.
3261 *
3262 * CONTEXT:
3263 * Don't call from IRQ context.
3264 */
3265void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3266{
3267 unsigned int cpu;
3268
Tejun Heof3421792010-07-02 10:03:51 +02003269 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003270
3271 spin_lock(&workqueue_lock);
3272
3273 wq->saved_max_active = max_active;
3274
Tejun Heof3421792010-07-02 10:03:51 +02003275 for_each_cwq_cpu(cpu, wq) {
Tejun Heodcd989c2010-06-29 10:07:14 +02003276 struct global_cwq *gcwq = get_gcwq(cpu);
3277
3278 spin_lock_irq(&gcwq->lock);
3279
Tejun Heo58a69cb2011-02-16 09:25:31 +01003280 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heodcd989c2010-06-29 10:07:14 +02003281 !(gcwq->flags & GCWQ_FREEZING))
3282 get_cwq(gcwq->cpu, wq)->max_active = max_active;
3283
3284 spin_unlock_irq(&gcwq->lock);
3285 }
3286
3287 spin_unlock(&workqueue_lock);
3288}
3289EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3290
3291/**
3292 * workqueue_congested - test whether a workqueue is congested
3293 * @cpu: CPU in question
3294 * @wq: target workqueue
3295 *
3296 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3297 * no synchronization around this function and the test result is
3298 * unreliable and only useful as advisory hints or for debugging.
3299 *
3300 * RETURNS:
3301 * %true if congested, %false otherwise.
3302 */
3303bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3304{
3305 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3306
3307 return !list_empty(&cwq->delayed_works);
3308}
3309EXPORT_SYMBOL_GPL(workqueue_congested);
3310
3311/**
3312 * work_cpu - return the last known associated cpu for @work
3313 * @work: the work of interest
3314 *
3315 * RETURNS:
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003316 * CPU number if @work was ever queued. WORK_CPU_NONE otherwise.
Tejun Heodcd989c2010-06-29 10:07:14 +02003317 */
3318unsigned int work_cpu(struct work_struct *work)
3319{
3320 struct global_cwq *gcwq = get_work_gcwq(work);
3321
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003322 return gcwq ? gcwq->cpu : WORK_CPU_NONE;
Tejun Heodcd989c2010-06-29 10:07:14 +02003323}
3324EXPORT_SYMBOL_GPL(work_cpu);
3325
3326/**
3327 * work_busy - test whether a work is currently pending or running
3328 * @work: the work to be tested
3329 *
3330 * Test whether @work is currently pending or running. There is no
3331 * synchronization around this function and the test result is
3332 * unreliable and only useful as advisory hints or for debugging.
3333 * Especially for reentrant wqs, the pending state might hide the
3334 * running state.
3335 *
3336 * RETURNS:
3337 * OR'd bitmask of WORK_BUSY_* bits.
3338 */
3339unsigned int work_busy(struct work_struct *work)
3340{
3341 struct global_cwq *gcwq = get_work_gcwq(work);
3342 unsigned long flags;
3343 unsigned int ret = 0;
3344
3345 if (!gcwq)
3346 return false;
3347
3348 spin_lock_irqsave(&gcwq->lock, flags);
3349
3350 if (work_pending(work))
3351 ret |= WORK_BUSY_PENDING;
3352 if (find_worker_executing_work(gcwq, work))
3353 ret |= WORK_BUSY_RUNNING;
3354
3355 spin_unlock_irqrestore(&gcwq->lock, flags);
3356
3357 return ret;
3358}
3359EXPORT_SYMBOL_GPL(work_busy);
3360
Tejun Heodb7bccf2010-06-29 10:07:12 +02003361/*
3362 * CPU hotplug.
3363 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003364 * There are two challenges in supporting CPU hotplug. Firstly, there
3365 * are a lot of assumptions on strong associations among work, cwq and
3366 * gcwq which make migrating pending and scheduled works very
3367 * difficult to implement without impacting hot paths. Secondly,
3368 * gcwqs serve mix of short, long and very long running works making
3369 * blocked draining impractical.
3370 *
Tejun Heo403c8212012-07-17 12:39:27 -07003371 * This is solved by allowing a gcwq to be detached from CPU, running it
3372 * with unbound workers and allowing it to be reattached later if the cpu
3373 * comes back online. A separate thread is created to govern a gcwq in
3374 * such state and is called the trustee of the gcwq.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003375 *
3376 * Trustee states and their descriptions.
3377 *
3378 * START Command state used on startup. On CPU_DOWN_PREPARE, a
3379 * new trustee is started with this state.
3380 *
3381 * IN_CHARGE Once started, trustee will enter this state after
Tejun Heoe22bee72010-06-29 10:07:14 +02003382 * assuming the manager role and making all existing
3383 * workers rogue. DOWN_PREPARE waits for trustee to
3384 * enter this state. After reaching IN_CHARGE, trustee
3385 * tries to execute the pending worklist until it's empty
3386 * and the state is set to BUTCHER, or the state is set
3387 * to RELEASE.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003388 *
3389 * BUTCHER Command state which is set by the cpu callback after
3390 * the cpu has went down. Once this state is set trustee
3391 * knows that there will be no new works on the worklist
3392 * and once the worklist is empty it can proceed to
3393 * killing idle workers.
3394 *
3395 * RELEASE Command state which is set by the cpu callback if the
3396 * cpu down has been canceled or it has come online
3397 * again. After recognizing this state, trustee stops
Tejun Heoe22bee72010-06-29 10:07:14 +02003398 * trying to drain or butcher and clears ROGUE, rebinds
3399 * all remaining workers back to the cpu and releases
3400 * manager role.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003401 *
3402 * DONE Trustee will enter this state after BUTCHER or RELEASE
3403 * is complete.
3404 *
3405 * trustee CPU draining
3406 * took over down complete
3407 * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE
3408 * | | ^
3409 * | CPU is back online v return workers |
3410 * ----------------> RELEASE --------------
3411 */
3412
Tejun Heo60373152012-07-17 12:39:27 -07003413/* claim manager positions of all pools */
3414static void gcwq_claim_management(struct global_cwq *gcwq)
3415{
3416 struct worker_pool *pool;
3417
3418 for_each_worker_pool(pool, gcwq)
3419 mutex_lock_nested(&pool->manager_mutex, pool - gcwq->pools);
3420}
3421
3422/* release manager positions */
3423static void gcwq_release_management(struct global_cwq *gcwq)
3424{
3425 struct worker_pool *pool;
3426
3427 for_each_worker_pool(pool, gcwq)
3428 mutex_unlock(&pool->manager_mutex);
3429}
3430
Tejun Heodb7bccf2010-06-29 10:07:12 +02003431/**
3432 * trustee_wait_event_timeout - timed event wait for trustee
3433 * @cond: condition to wait for
3434 * @timeout: timeout in jiffies
3435 *
3436 * wait_event_timeout() for trustee to use. Handles locking and
3437 * checks for RELEASE request.
3438 *
3439 * CONTEXT:
3440 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3441 * multiple times. To be used by trustee.
3442 *
3443 * RETURNS:
3444 * Positive indicating left time if @cond is satisfied, 0 if timed
3445 * out, -1 if canceled.
3446 */
3447#define trustee_wait_event_timeout(cond, timeout) ({ \
3448 long __ret = (timeout); \
3449 while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) && \
3450 __ret) { \
3451 spin_unlock_irq(&gcwq->lock); \
3452 __wait_event_timeout(gcwq->trustee_wait, (cond) || \
3453 (gcwq->trustee_state == TRUSTEE_RELEASE), \
3454 __ret); \
3455 spin_lock_irq(&gcwq->lock); \
3456 } \
3457 gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret); \
3458})
3459
3460/**
3461 * trustee_wait_event - event wait for trustee
3462 * @cond: condition to wait for
3463 *
3464 * wait_event() for trustee to use. Automatically handles locking and
3465 * checks for CANCEL request.
3466 *
3467 * CONTEXT:
3468 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3469 * multiple times. To be used by trustee.
3470 *
3471 * RETURNS:
3472 * 0 if @cond is satisfied, -1 if canceled.
3473 */
3474#define trustee_wait_event(cond) ({ \
3475 long __ret1; \
3476 __ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\
3477 __ret1 < 0 ? -1 : 0; \
3478})
3479
Tejun Heo4ce62e92012-07-13 22:16:44 -07003480static bool gcwq_has_idle_workers(struct global_cwq *gcwq)
3481{
3482 struct worker_pool *pool;
3483
3484 for_each_worker_pool(pool, gcwq)
3485 if (!list_empty(&pool->idle_list))
3486 return true;
3487 return false;
3488}
3489
Tejun Heodb7bccf2010-06-29 10:07:12 +02003490static int __cpuinit trustee_thread(void *__gcwq)
3491{
3492 struct global_cwq *gcwq = __gcwq;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003493 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003494 struct worker *worker;
Tejun Heoe22bee72010-06-29 10:07:14 +02003495 struct work_struct *work;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003496 struct hlist_node *pos;
Tejun Heoe22bee72010-06-29 10:07:14 +02003497 long rc;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003498 int i;
3499
3500 BUG_ON(gcwq->cpu != smp_processor_id());
3501
Tejun Heo60373152012-07-17 12:39:27 -07003502 gcwq_claim_management(gcwq);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003503 spin_lock_irq(&gcwq->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02003504
Tejun Heof2d5a0e2012-07-17 12:39:26 -07003505 /*
3506 * We've claimed all manager positions. Make all workers unbound
3507 * and set DISASSOCIATED. Before this, all workers except for the
3508 * ones which are still executing works from before the last CPU
3509 * down must be on the cpu. After this, they may become diasporas.
3510 */
Tejun Heo60373152012-07-17 12:39:27 -07003511 for_each_worker_pool(pool, gcwq)
Tejun Heo4ce62e92012-07-13 22:16:44 -07003512 list_for_each_entry(worker, &pool->idle_list, entry)
Tejun Heo403c8212012-07-17 12:39:27 -07003513 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003514
3515 for_each_busy_worker(worker, i, pos, gcwq)
Tejun Heo403c8212012-07-17 12:39:27 -07003516 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003517
Tejun Heof2d5a0e2012-07-17 12:39:26 -07003518 gcwq->flags |= GCWQ_DISASSOCIATED;
3519
Tejun Heodb7bccf2010-06-29 10:07:12 +02003520 /*
Tejun Heo403c8212012-07-17 12:39:27 -07003521 * Call schedule() so that we cross rq->lock and thus can guarantee
3522 * sched callbacks see the unbound flag. This is necessary as
3523 * scheduler callbacks may be invoked from other cpus.
Tejun Heoe22bee72010-06-29 10:07:14 +02003524 */
3525 spin_unlock_irq(&gcwq->lock);
3526 schedule();
3527 spin_lock_irq(&gcwq->lock);
3528
3529 /*
Tejun Heocb444762010-07-02 10:03:50 +02003530 * Sched callbacks are disabled now. Zap nr_running. After
3531 * this, nr_running stays zero and need_more_worker() and
3532 * keep_working() are always true as long as the worklist is
3533 * not empty.
Tejun Heoe22bee72010-06-29 10:07:14 +02003534 */
Tejun Heo4ce62e92012-07-13 22:16:44 -07003535 for_each_worker_pool(pool, gcwq)
3536 atomic_set(get_pool_nr_running(pool), 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003537
3538 spin_unlock_irq(&gcwq->lock);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003539 for_each_worker_pool(pool, gcwq)
3540 del_timer_sync(&pool->idle_timer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003541 spin_lock_irq(&gcwq->lock);
3542
3543 /*
Tejun Heodb7bccf2010-06-29 10:07:12 +02003544 * We're now in charge. Notify and proceed to drain. We need
3545 * to keep the gcwq running during the whole CPU down
3546 * procedure as other cpu hotunplug callbacks may need to
3547 * flush currently running tasks.
3548 */
3549 gcwq->trustee_state = TRUSTEE_IN_CHARGE;
3550 wake_up_all(&gcwq->trustee_wait);
3551
3552 /*
3553 * The original cpu is in the process of dying and may go away
3554 * anytime now. When that happens, we and all workers would
Tejun Heoe22bee72010-06-29 10:07:14 +02003555 * be migrated to other cpus. Try draining any left work. We
3556 * want to get it over with ASAP - spam rescuers, wake up as
3557 * many idlers as necessary and create new ones till the
3558 * worklist is empty. Note that if the gcwq is frozen, there
Tejun Heo58a69cb2011-02-16 09:25:31 +01003559 * may be frozen works in freezable cwqs. Don't declare
Tejun Heoe22bee72010-06-29 10:07:14 +02003560 * completion while frozen.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003561 */
Tejun Heo4ce62e92012-07-13 22:16:44 -07003562 while (true) {
3563 bool busy = false;
Tejun Heoe22bee72010-06-29 10:07:14 +02003564
Tejun Heo4ce62e92012-07-13 22:16:44 -07003565 for_each_worker_pool(pool, gcwq)
3566 busy |= pool->nr_workers != pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +02003567
Tejun Heo4ce62e92012-07-13 22:16:44 -07003568 if (!busy && !(gcwq->flags & GCWQ_FREEZING) &&
3569 gcwq->trustee_state != TRUSTEE_IN_CHARGE)
3570 break;
Tejun Heoe22bee72010-06-29 10:07:14 +02003571
Tejun Heo4ce62e92012-07-13 22:16:44 -07003572 for_each_worker_pool(pool, gcwq) {
3573 int nr_works = 0;
3574
3575 list_for_each_entry(work, &pool->worklist, entry) {
3576 send_mayday(work);
3577 nr_works++;
3578 }
3579
3580 list_for_each_entry(worker, &pool->idle_list, entry) {
3581 if (!nr_works--)
3582 break;
3583 wake_up_process(worker->task);
3584 }
3585
3586 if (need_to_create_worker(pool)) {
3587 spin_unlock_irq(&gcwq->lock);
Tejun Heobc2ae0f2012-07-17 12:39:27 -07003588 worker = create_worker(pool);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003589 spin_lock_irq(&gcwq->lock);
Tejun Heobc2ae0f2012-07-17 12:39:27 -07003590 if (worker)
Tejun Heo4ce62e92012-07-13 22:16:44 -07003591 start_worker(worker);
Tejun Heoe22bee72010-06-29 10:07:14 +02003592 }
3593 }
3594
Tejun Heodb7bccf2010-06-29 10:07:12 +02003595 /* give a breather */
3596 if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN) < 0)
3597 break;
3598 }
3599
Tejun Heoe22bee72010-06-29 10:07:14 +02003600 /*
3601 * Either all works have been scheduled and cpu is down, or
3602 * cpu down has already been canceled. Wait for and butcher
3603 * all workers till we're canceled.
3604 */
3605 do {
Tejun Heo4ce62e92012-07-13 22:16:44 -07003606 rc = trustee_wait_event(gcwq_has_idle_workers(gcwq));
3607
3608 i = 0;
3609 for_each_worker_pool(pool, gcwq) {
3610 while (!list_empty(&pool->idle_list)) {
3611 worker = list_first_entry(&pool->idle_list,
3612 struct worker, entry);
3613 destroy_worker(worker);
3614 }
3615 i |= pool->nr_workers;
3616 }
3617 } while (i && rc >= 0);
Tejun Heoe22bee72010-06-29 10:07:14 +02003618
Tejun Heo60373152012-07-17 12:39:27 -07003619 gcwq_release_management(gcwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02003620
Tejun Heodb7bccf2010-06-29 10:07:12 +02003621 /* notify completion */
3622 gcwq->trustee = NULL;
3623 gcwq->trustee_state = TRUSTEE_DONE;
3624 wake_up_all(&gcwq->trustee_wait);
3625 spin_unlock_irq(&gcwq->lock);
3626 return 0;
3627}
3628
3629/**
3630 * wait_trustee_state - wait for trustee to enter the specified state
3631 * @gcwq: gcwq the trustee of interest belongs to
3632 * @state: target state to wait for
3633 *
3634 * Wait for the trustee to reach @state. DONE is already matched.
3635 *
3636 * CONTEXT:
3637 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3638 * multiple times. To be used by cpu_callback.
3639 */
3640static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state)
Namhyung Kim06bd6eb2010-08-22 23:19:42 +09003641__releases(&gcwq->lock)
3642__acquires(&gcwq->lock)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003643{
3644 if (!(gcwq->trustee_state == state ||
3645 gcwq->trustee_state == TRUSTEE_DONE)) {
3646 spin_unlock_irq(&gcwq->lock);
3647 __wait_event(gcwq->trustee_wait,
3648 gcwq->trustee_state == state ||
3649 gcwq->trustee_state == TRUSTEE_DONE);
3650 spin_lock_irq(&gcwq->lock);
3651 }
3652}
3653
Oleg Nesterov3af244332007-05-09 02:34:09 -07003654static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
3655 unsigned long action,
3656 void *hcpu)
3657{
3658 unsigned int cpu = (unsigned long)hcpu;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003659 struct global_cwq *gcwq = get_gcwq(cpu);
3660 struct task_struct *new_trustee = NULL;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003661 struct worker *new_workers[NR_WORKER_POOLS] = { };
3662 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003663 unsigned long flags;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003664 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003666 action &= ~CPU_TASKS_FROZEN;
3667
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003669 case CPU_DOWN_PREPARE:
3670 new_trustee = kthread_create(trustee_thread, gcwq,
3671 "workqueue_trustee/%d\n", cpu);
3672 if (IS_ERR(new_trustee))
3673 return notifier_from_errno(PTR_ERR(new_trustee));
3674 kthread_bind(new_trustee, cpu);
Tejun Heoe22bee72010-06-29 10:07:14 +02003675 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 case CPU_UP_PREPARE:
Tejun Heo4ce62e92012-07-13 22:16:44 -07003677 i = 0;
3678 for_each_worker_pool(pool, gcwq) {
3679 BUG_ON(pool->first_idle);
Tejun Heobc2ae0f2012-07-17 12:39:27 -07003680 new_workers[i] = create_worker(pool);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003681 if (!new_workers[i++])
3682 goto err_destroy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 }
3685
Tejun Heodb7bccf2010-06-29 10:07:12 +02003686 /* some are called w/ irq disabled, don't disturb irq status */
3687 spin_lock_irqsave(&gcwq->lock, flags);
3688
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003689 switch (action) {
Tejun Heodb7bccf2010-06-29 10:07:12 +02003690 case CPU_DOWN_PREPARE:
3691 /* initialize trustee and tell it to acquire the gcwq */
3692 BUG_ON(gcwq->trustee || gcwq->trustee_state != TRUSTEE_DONE);
3693 gcwq->trustee = new_trustee;
3694 gcwq->trustee_state = TRUSTEE_START;
3695 wake_up_process(gcwq->trustee);
3696 wait_trustee_state(gcwq, TRUSTEE_IN_CHARGE);
Tejun Heoe22bee72010-06-29 10:07:14 +02003697 /* fall through */
3698 case CPU_UP_PREPARE:
Tejun Heo4ce62e92012-07-13 22:16:44 -07003699 i = 0;
3700 for_each_worker_pool(pool, gcwq) {
3701 BUG_ON(pool->first_idle);
3702 pool->first_idle = new_workers[i++];
3703 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003704 break;
3705
Oleg Nesterov3da1c842008-07-25 01:47:50 -07003706 case CPU_POST_DEAD:
Tejun Heodb7bccf2010-06-29 10:07:12 +02003707 gcwq->trustee_state = TRUSTEE_BUTCHER;
Tejun Heoe22bee72010-06-29 10:07:14 +02003708 /* fall through */
3709 case CPU_UP_CANCELED:
Tejun Heo4ce62e92012-07-13 22:16:44 -07003710 for_each_worker_pool(pool, gcwq) {
3711 destroy_worker(pool->first_idle);
3712 pool->first_idle = NULL;
3713 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003714 break;
3715
3716 case CPU_DOWN_FAILED:
3717 case CPU_ONLINE:
3718 if (gcwq->trustee_state != TRUSTEE_DONE) {
3719 gcwq->trustee_state = TRUSTEE_RELEASE;
3720 wake_up_process(gcwq->trustee);
3721 wait_trustee_state(gcwq, TRUSTEE_DONE);
3722 }
3723
Tejun Heo25511a42012-07-17 12:39:27 -07003724 spin_unlock_irq(&gcwq->lock);
3725 gcwq_claim_management(gcwq);
3726 spin_lock_irq(&gcwq->lock);
3727
Tejun Heobc2ae0f2012-07-17 12:39:27 -07003728 gcwq->flags &= ~GCWQ_DISASSOCIATED;
3729
Tejun Heo25511a42012-07-17 12:39:27 -07003730 rebind_workers(gcwq);
3731
3732 gcwq_release_management(gcwq);
3733
Tejun Heobc2ae0f2012-07-17 12:39:27 -07003734 /*
Tejun Heoe22bee72010-06-29 10:07:14 +02003735 * Trustee is done and there might be no worker left.
3736 * Put the first_idle in and request a real manager to
3737 * take a look.
3738 */
Tejun Heo4ce62e92012-07-13 22:16:44 -07003739 for_each_worker_pool(pool, gcwq) {
3740 spin_unlock_irq(&gcwq->lock);
3741 kthread_bind(pool->first_idle->task, cpu);
3742 spin_lock_irq(&gcwq->lock);
3743 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heobc2ae0f2012-07-17 12:39:27 -07003744 pool->first_idle->flags &= ~WORKER_UNBOUND;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003745 start_worker(pool->first_idle);
3746 pool->first_idle = NULL;
3747 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003748 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003749 }
3750
Tejun Heodb7bccf2010-06-29 10:07:12 +02003751 spin_unlock_irqrestore(&gcwq->lock, flags);
3752
Tejun Heo15376632010-06-29 10:07:11 +02003753 return notifier_from_errno(0);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003754
3755err_destroy:
3756 if (new_trustee)
3757 kthread_stop(new_trustee);
3758
3759 spin_lock_irqsave(&gcwq->lock, flags);
3760 for (i = 0; i < NR_WORKER_POOLS; i++)
3761 if (new_workers[i])
3762 destroy_worker(new_workers[i]);
3763 spin_unlock_irqrestore(&gcwq->lock, flags);
3764
3765 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767
Tejun Heo65758202012-07-17 12:39:26 -07003768/*
3769 * Workqueues should be brought up before normal priority CPU notifiers.
3770 * This will be registered high priority CPU notifier.
3771 */
3772static int __devinit workqueue_cpu_up_callback(struct notifier_block *nfb,
3773 unsigned long action,
3774 void *hcpu)
3775{
3776 switch (action & ~CPU_TASKS_FROZEN) {
3777 case CPU_UP_PREPARE:
3778 case CPU_UP_CANCELED:
3779 case CPU_DOWN_FAILED:
3780 case CPU_ONLINE:
3781 return workqueue_cpu_callback(nfb, action, hcpu);
3782 }
3783 return NOTIFY_OK;
3784}
3785
3786/*
3787 * Workqueues should be brought down after normal priority CPU notifiers.
3788 * This will be registered as low priority CPU notifier.
3789 */
3790static int __devinit workqueue_cpu_down_callback(struct notifier_block *nfb,
3791 unsigned long action,
3792 void *hcpu)
3793{
3794 switch (action & ~CPU_TASKS_FROZEN) {
3795 case CPU_DOWN_PREPARE:
Tejun Heo65758202012-07-17 12:39:26 -07003796 case CPU_POST_DEAD:
3797 return workqueue_cpu_callback(nfb, action, hcpu);
3798 }
3799 return NOTIFY_OK;
3800}
3801
Rusty Russell2d3854a2008-11-05 13:39:10 +11003802#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003803
Rusty Russell2d3854a2008-11-05 13:39:10 +11003804struct work_for_cpu {
Andrew Morton6b440032009-04-09 09:50:37 -06003805 struct completion completion;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003806 long (*fn)(void *);
3807 void *arg;
3808 long ret;
3809};
3810
Andrew Morton6b440032009-04-09 09:50:37 -06003811static int do_work_for_cpu(void *_wfc)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003812{
Andrew Morton6b440032009-04-09 09:50:37 -06003813 struct work_for_cpu *wfc = _wfc;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003814 wfc->ret = wfc->fn(wfc->arg);
Andrew Morton6b440032009-04-09 09:50:37 -06003815 complete(&wfc->completion);
3816 return 0;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003817}
3818
3819/**
3820 * work_on_cpu - run a function in user context on a particular cpu
3821 * @cpu: the cpu to run on
3822 * @fn: the function to run
3823 * @arg: the function arg
3824 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003825 * This will return the value @fn returns.
3826 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06003827 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003828 */
3829long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3830{
Andrew Morton6b440032009-04-09 09:50:37 -06003831 struct task_struct *sub_thread;
3832 struct work_for_cpu wfc = {
3833 .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
3834 .fn = fn,
3835 .arg = arg,
3836 };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003837
Andrew Morton6b440032009-04-09 09:50:37 -06003838 sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
3839 if (IS_ERR(sub_thread))
3840 return PTR_ERR(sub_thread);
3841 kthread_bind(sub_thread, cpu);
3842 wake_up_process(sub_thread);
3843 wait_for_completion(&wfc.completion);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003844 return wfc.ret;
3845}
3846EXPORT_SYMBOL_GPL(work_on_cpu);
3847#endif /* CONFIG_SMP */
3848
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003849#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303850
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003851/**
3852 * freeze_workqueues_begin - begin freezing workqueues
3853 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003854 * Start freezing workqueues. After this function returns, all freezable
3855 * workqueues will queue new works to their frozen_works list instead of
3856 * gcwq->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003857 *
3858 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003859 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003860 */
3861void freeze_workqueues_begin(void)
3862{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003863 unsigned int cpu;
3864
3865 spin_lock(&workqueue_lock);
3866
3867 BUG_ON(workqueue_freezing);
3868 workqueue_freezing = true;
3869
Tejun Heof3421792010-07-02 10:03:51 +02003870 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003871 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003872 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003873
3874 spin_lock_irq(&gcwq->lock);
3875
Tejun Heodb7bccf2010-06-29 10:07:12 +02003876 BUG_ON(gcwq->flags & GCWQ_FREEZING);
3877 gcwq->flags |= GCWQ_FREEZING;
3878
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003879 list_for_each_entry(wq, &workqueues, list) {
3880 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3881
Tejun Heo58a69cb2011-02-16 09:25:31 +01003882 if (cwq && wq->flags & WQ_FREEZABLE)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003883 cwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003884 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003885
3886 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003887 }
3888
3889 spin_unlock(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003891
3892/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003893 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003894 *
3895 * Check whether freezing is complete. This function must be called
3896 * between freeze_workqueues_begin() and thaw_workqueues().
3897 *
3898 * CONTEXT:
3899 * Grabs and releases workqueue_lock.
3900 *
3901 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01003902 * %true if some freezable workqueues are still busy. %false if freezing
3903 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003904 */
3905bool freeze_workqueues_busy(void)
3906{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003907 unsigned int cpu;
3908 bool busy = false;
3909
3910 spin_lock(&workqueue_lock);
3911
3912 BUG_ON(!workqueue_freezing);
3913
Tejun Heof3421792010-07-02 10:03:51 +02003914 for_each_gcwq_cpu(cpu) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003915 struct workqueue_struct *wq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003916 /*
3917 * nr_active is monotonically decreasing. It's safe
3918 * to peek without lock.
3919 */
3920 list_for_each_entry(wq, &workqueues, list) {
3921 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3922
Tejun Heo58a69cb2011-02-16 09:25:31 +01003923 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003924 continue;
3925
3926 BUG_ON(cwq->nr_active < 0);
3927 if (cwq->nr_active) {
3928 busy = true;
3929 goto out_unlock;
3930 }
3931 }
3932 }
3933out_unlock:
3934 spin_unlock(&workqueue_lock);
3935 return busy;
3936}
3937
3938/**
3939 * thaw_workqueues - thaw workqueues
3940 *
3941 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo7e116292010-06-29 10:07:13 +02003942 * frozen works are transferred to their respective gcwq worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003943 *
3944 * CONTEXT:
Tejun Heo8b03ae32010-06-29 10:07:12 +02003945 * Grabs and releases workqueue_lock and gcwq->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003946 */
3947void thaw_workqueues(void)
3948{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003949 unsigned int cpu;
3950
3951 spin_lock(&workqueue_lock);
3952
3953 if (!workqueue_freezing)
3954 goto out_unlock;
3955
Tejun Heof3421792010-07-02 10:03:51 +02003956 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02003957 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003958 struct worker_pool *pool;
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003959 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003960
3961 spin_lock_irq(&gcwq->lock);
3962
Tejun Heodb7bccf2010-06-29 10:07:12 +02003963 BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
3964 gcwq->flags &= ~GCWQ_FREEZING;
3965
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003966 list_for_each_entry(wq, &workqueues, list) {
3967 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3968
Tejun Heo58a69cb2011-02-16 09:25:31 +01003969 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003970 continue;
3971
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003972 /* restore max_active and repopulate worklist */
3973 cwq->max_active = wq->saved_max_active;
3974
3975 while (!list_empty(&cwq->delayed_works) &&
3976 cwq->nr_active < cwq->max_active)
3977 cwq_activate_first_delayed(cwq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003978 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003979
Tejun Heo4ce62e92012-07-13 22:16:44 -07003980 for_each_worker_pool(pool, gcwq)
3981 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02003982
Tejun Heo8b03ae32010-06-29 10:07:12 +02003983 spin_unlock_irq(&gcwq->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003984 }
3985
3986 workqueue_freezing = false;
3987out_unlock:
3988 spin_unlock(&workqueue_lock);
3989}
3990#endif /* CONFIG_FREEZER */
3991
Suresh Siddha6ee05782010-07-30 14:57:37 -07003992static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993{
Tejun Heoc34056a2010-06-29 10:07:11 +02003994 unsigned int cpu;
Tejun Heoc8e55f32010-06-29 10:07:12 +02003995 int i;
Tejun Heoc34056a2010-06-29 10:07:11 +02003996
Tejun Heo65758202012-07-17 12:39:26 -07003997 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
3998 cpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003999
4000 /* initialize gcwqs */
Tejun Heof3421792010-07-02 10:03:51 +02004001 for_each_gcwq_cpu(cpu) {
Tejun Heo8b03ae32010-06-29 10:07:12 +02004002 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004003 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02004004
4005 spin_lock_init(&gcwq->lock);
4006 gcwq->cpu = cpu;
Tejun Heo477a3c32010-08-31 10:54:35 +02004007 gcwq->flags |= GCWQ_DISASSOCIATED;
Tejun Heo8b03ae32010-06-29 10:07:12 +02004008
Tejun Heoc8e55f32010-06-29 10:07:12 +02004009 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
4010 INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
4011
Tejun Heo4ce62e92012-07-13 22:16:44 -07004012 for_each_worker_pool(pool, gcwq) {
4013 pool->gcwq = gcwq;
4014 INIT_LIST_HEAD(&pool->worklist);
4015 INIT_LIST_HEAD(&pool->idle_list);
Tejun Heoe22bee72010-06-29 10:07:14 +02004016
Tejun Heo4ce62e92012-07-13 22:16:44 -07004017 init_timer_deferrable(&pool->idle_timer);
4018 pool->idle_timer.function = idle_worker_timeout;
4019 pool->idle_timer.data = (unsigned long)pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02004020
Tejun Heo4ce62e92012-07-13 22:16:44 -07004021 setup_timer(&pool->mayday_timer, gcwq_mayday_timeout,
4022 (unsigned long)pool);
4023
Tejun Heo60373152012-07-17 12:39:27 -07004024 mutex_init(&pool->manager_mutex);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004025 ida_init(&pool->worker_ida);
4026 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02004027
Tejun Heo25511a42012-07-17 12:39:27 -07004028 init_waitqueue_head(&gcwq->rebind_hold);
4029
Tejun Heodb7bccf2010-06-29 10:07:12 +02004030 gcwq->trustee_state = TRUSTEE_DONE;
4031 init_waitqueue_head(&gcwq->trustee_wait);
Tejun Heo8b03ae32010-06-29 10:07:12 +02004032 }
4033
Tejun Heoe22bee72010-06-29 10:07:14 +02004034 /* create the initial worker */
Tejun Heof3421792010-07-02 10:03:51 +02004035 for_each_online_gcwq_cpu(cpu) {
Tejun Heoe22bee72010-06-29 10:07:14 +02004036 struct global_cwq *gcwq = get_gcwq(cpu);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004037 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02004038
Tejun Heo477a3c32010-08-31 10:54:35 +02004039 if (cpu != WORK_CPU_UNBOUND)
4040 gcwq->flags &= ~GCWQ_DISASSOCIATED;
Tejun Heo4ce62e92012-07-13 22:16:44 -07004041
4042 for_each_worker_pool(pool, gcwq) {
4043 struct worker *worker;
4044
Tejun Heobc2ae0f2012-07-17 12:39:27 -07004045 worker = create_worker(pool);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004046 BUG_ON(!worker);
4047 spin_lock_irq(&gcwq->lock);
4048 start_worker(worker);
4049 spin_unlock_irq(&gcwq->lock);
4050 }
Tejun Heoe22bee72010-06-29 10:07:14 +02004051 }
4052
Tejun Heod320c032010-06-29 10:07:14 +02004053 system_wq = alloc_workqueue("events", 0, 0);
4054 system_long_wq = alloc_workqueue("events_long", 0, 0);
4055 system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0);
Tejun Heof3421792010-07-02 10:03:51 +02004056 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
4057 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01004058 system_freezable_wq = alloc_workqueue("events_freezable",
4059 WQ_FREEZABLE, 0);
Alan Stern62d3c542012-03-02 10:51:00 +01004060 system_nrt_freezable_wq = alloc_workqueue("events_nrt_freezable",
4061 WQ_NON_REENTRANT | WQ_FREEZABLE, 0);
Hitoshi Mitakee5cba242010-11-26 12:06:44 +01004062 BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq ||
Alan Stern62d3c542012-03-02 10:51:00 +01004063 !system_unbound_wq || !system_freezable_wq ||
4064 !system_nrt_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07004065 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066}
Suresh Siddha6ee05782010-07-30 14:57:37 -07004067early_initcall(init_workqueues);