blob: 577ac719eaece7a7d82826c1869ab02f2eb5be13 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tejun Heoc54fce62010-09-10 16:51:36 +02002 * kernel/workqueue.c - generic async execution with shared worker pool
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Tejun Heoc54fce62010-09-10 16:51:36 +02004 * Copyright (C) 2002 Ingo Molnar
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Tejun Heoc54fce62010-09-10 16:51:36 +02006 * Derived from the taskqueue/keventd code by:
7 * David Woodhouse <dwmw2@infradead.org>
8 * Andrew Morton
9 * Kai Petzke <wpp@marie.physik.tu-berlin.de>
10 * Theodore Ts'o <tytso@mit.edu>
Christoph Lameter89ada672005-10-30 15:01:59 -080011 *
Christoph Lametercde53532008-07-04 09:59:22 -070012 * Made to use alloc_percpu by Christoph Lameter.
Tejun Heoc54fce62010-09-10 16:51:36 +020013 *
14 * Copyright (C) 2010 SUSE Linux Products GmbH
15 * Copyright (C) 2010 Tejun Heo <tj@kernel.org>
16 *
17 * This is the generic async execution mechanism. Work items as are
18 * executed in process context. The worker pool is shared and
19 * automatically managed. There is one worker pool for each CPU and
20 * one extra for works which are better served by workers which are
21 * not bound to any specific CPU.
22 *
23 * Please read Documentation/workqueue.txt for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 */
25
Paul Gortmaker9984de12011-05-23 14:51:41 -040026#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/kernel.h>
28#include <linux/sched.h>
29#include <linux/init.h>
30#include <linux/signal.h>
31#include <linux/completion.h>
32#include <linux/workqueue.h>
33#include <linux/slab.h>
34#include <linux/cpu.h>
35#include <linux/notifier.h>
36#include <linux/kthread.h>
James Bottomley1fa44ec2006-02-23 12:43:43 -060037#include <linux/hardirq.h>
Christoph Lameter46934022006-10-11 01:21:26 -070038#include <linux/mempolicy.h>
Rafael J. Wysocki341a5952006-12-06 20:34:49 -080039#include <linux/freezer.h>
Peter Zijlstrad5abe662006-12-06 20:37:26 -080040#include <linux/kallsyms.h>
41#include <linux/debug_locks.h>
Johannes Berg4e6045f2007-10-18 23:39:55 -070042#include <linux/lockdep.h>
Tejun Heoc34056a2010-06-29 10:07:11 +020043#include <linux/idr.h>
Sasha Levin42f85702012-12-17 10:01:23 -050044#include <linux/hashtable.h>
Tejun Heoe22bee72010-06-29 10:07:14 +020045
Tejun Heoea138442013-01-18 14:05:55 -080046#include "workqueue_internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Tejun Heoc8e55f32010-06-29 10:07:12 +020048enum {
Tejun Heobc2ae0f2012-07-17 12:39:27 -070049 /*
Tejun Heo24647572013-01-24 11:01:33 -080050 * worker_pool flags
Tejun Heobc2ae0f2012-07-17 12:39:27 -070051 *
Tejun Heo24647572013-01-24 11:01:33 -080052 * A bound pool is either associated or disassociated with its CPU.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070053 * While associated (!DISASSOCIATED), all workers are bound to the
54 * CPU and none has %WORKER_UNBOUND set and concurrency management
55 * is in effect.
56 *
57 * While DISASSOCIATED, the cpu may be offline and all workers have
58 * %WORKER_UNBOUND set and concurrency management disabled, and may
Tejun Heo24647572013-01-24 11:01:33 -080059 * be executing on any CPU. The pool behaves as an unbound one.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070060 *
61 * Note that DISASSOCIATED can be flipped only while holding
Tejun Heo24647572013-01-24 11:01:33 -080062 * assoc_mutex to avoid changing binding state while
63 * create_worker() is in progress.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070064 */
Tejun Heo11ebea52012-07-12 14:46:37 -070065 POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
Lai Jiangshan552a37e2012-09-10 10:03:33 -070066 POOL_MANAGING_WORKERS = 1 << 1, /* managing workers */
Tejun Heo24647572013-01-24 11:01:33 -080067 POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */
Tejun Heo35b6bb62013-01-24 11:01:33 -080068 POOL_FREEZING = 1 << 3, /* freeze in progress */
Tejun Heodb7bccf2010-06-29 10:07:12 +020069
Tejun Heoc8e55f32010-06-29 10:07:12 +020070 /* worker flags */
71 WORKER_STARTED = 1 << 0, /* started */
72 WORKER_DIE = 1 << 1, /* die die die */
73 WORKER_IDLE = 1 << 2, /* is idle */
Tejun Heoe22bee72010-06-29 10:07:14 +020074 WORKER_PREP = 1 << 3, /* preparing to run works */
Tejun Heofb0e7be2010-06-29 10:07:15 +020075 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
Tejun Heof3421792010-07-02 10:03:51 +020076 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
Tejun Heoe22bee72010-06-29 10:07:14 +020077
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -070078 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_UNBOUND |
Tejun Heo403c8212012-07-17 12:39:27 -070079 WORKER_CPU_INTENSIVE,
Tejun Heodb7bccf2010-06-29 10:07:12 +020080
Tejun Heoe34cdddb2013-01-24 11:01:33 -080081 NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */
Tejun Heo4ce62e92012-07-13 22:16:44 -070082
Tejun Heoc8e55f32010-06-29 10:07:12 +020083 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
Tejun Heodb7bccf2010-06-29 10:07:12 +020084
Tejun Heoe22bee72010-06-29 10:07:14 +020085 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
86 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
87
Tejun Heo3233cdb2011-02-16 18:10:19 +010088 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
89 /* call for help after 10ms
90 (min two ticks) */
Tejun Heoe22bee72010-06-29 10:07:14 +020091 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
92 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heoe22bee72010-06-29 10:07:14 +020093
94 /*
95 * Rescue workers are used only on emergencies and shared by
96 * all cpus. Give -20.
97 */
98 RESCUER_NICE_LEVEL = -20,
Tejun Heo32704762012-07-13 22:16:45 -070099 HIGHPRI_NICE_LEVEL = -20,
Tejun Heoc8e55f32010-06-29 10:07:12 +0200100};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102/*
Tejun Heo4690c4a2010-06-29 10:07:10 +0200103 * Structure fields follow one of the following exclusion rules.
104 *
Tejun Heoe41e7042010-08-24 14:22:47 +0200105 * I: Modifiable by initialization/destruction paths and read-only for
106 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200107 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200108 * P: Preemption protected. Disabling preemption is enough and should
109 * only be modified and accessed from the local cpu.
110 *
Tejun Heod565ed62013-01-24 11:01:33 -0800111 * L: pool->lock protected. Access with pool->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200112 *
Tejun Heod565ed62013-01-24 11:01:33 -0800113 * X: During normal operation, modification requires pool->lock and should
114 * be done only from local cpu. Either disabling preemption on local
115 * cpu or grabbing pool->lock is enough for read access. If
116 * POOL_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200117 *
Tejun Heo73f53c42010-06-29 10:07:11 +0200118 * F: wq->flush_mutex protected.
119 *
Tejun Heo4690c4a2010-06-29 10:07:10 +0200120 * W: workqueue_lock protected.
121 */
122
Tejun Heo2eaebdb2013-01-18 14:05:55 -0800123/* struct worker is defined in workqueue_internal.h */
Tejun Heoc34056a2010-06-29 10:07:11 +0200124
Tejun Heobd7bdd42012-07-12 14:46:37 -0700125struct worker_pool {
Tejun Heod565ed62013-01-24 11:01:33 -0800126 spinlock_t lock; /* the pool lock */
Tejun Heod84ff052013-03-12 11:29:59 -0700127 int cpu; /* I: the associated cpu */
Tejun Heo9daf9e62013-01-24 11:01:33 -0800128 int id; /* I: pool ID */
Tejun Heo11ebea52012-07-12 14:46:37 -0700129 unsigned int flags; /* X: flags */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700130
131 struct list_head worklist; /* L: list of pending works */
132 int nr_workers; /* L: total number of workers */
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700133
134 /* nr_idle includes the ones off idle_list for rebinding */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700135 int nr_idle; /* L: currently idle ones */
136
137 struct list_head idle_list; /* X: list of idle workers */
138 struct timer_list idle_timer; /* L: worker idle timeout */
139 struct timer_list mayday_timer; /* L: SOS timer for workers */
140
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800141 /* workers are chained either in busy_hash or idle_list */
142 DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER);
143 /* L: hash of busy workers */
144
Tejun Heo24647572013-01-24 11:01:33 -0800145 struct mutex assoc_mutex; /* protect POOL_DISASSOCIATED */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700146 struct ida worker_ida; /* L: for worker IDs */
Tejun Heoe19e3972013-01-24 11:39:44 -0800147
148 /*
149 * The current concurrency level. As it's likely to be accessed
150 * from other CPUs during try_to_wake_up(), put it in a separate
151 * cacheline.
152 */
153 atomic_t nr_running ____cacheline_aligned_in_smp;
Tejun Heo8b03ae32010-06-29 10:07:12 +0200154} ____cacheline_aligned_in_smp;
155
156/*
Tejun Heo112202d2013-02-13 19:29:12 -0800157 * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS
158 * of work_struct->data are used for flags and the remaining high bits
159 * point to the pwq; thus, pwqs need to be aligned at two's power of the
160 * number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 */
Tejun Heo112202d2013-02-13 19:29:12 -0800162struct pool_workqueue {
Tejun Heobd7bdd42012-07-12 14:46:37 -0700163 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200164 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200165 int work_color; /* L: current color */
166 int flush_color; /* L: flushing color */
167 int nr_in_flight[WORK_NR_COLORS];
168 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200169 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200170 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200171 struct list_head delayed_works; /* L: delayed works */
Tejun Heo30cdf242013-03-12 11:29:57 -0700172 struct list_head pwqs_node; /* I: node on wq->pwqs */
Tejun Heo493a1722013-03-12 11:29:59 -0700173 struct list_head mayday_node; /* W: node on wq->maydays */
Tejun Heoe904e6c2013-03-12 11:29:57 -0700174} __aligned(1 << WORK_STRUCT_FLAG_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200177 * Structure used to wait for workqueue flush.
178 */
179struct wq_flusher {
180 struct list_head list; /* F: list of flushers */
181 int flush_color; /* F: flush color waiting for */
182 struct completion done; /* flush completion */
183};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Tejun Heo73f53c42010-06-29 10:07:11 +0200185/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 * The externally visible workqueue abstraction is an array of
187 * per-CPU workqueues:
188 */
189struct workqueue_struct {
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200190 unsigned int flags; /* W: WQ_* flags */
Tejun Heo420c0dd2013-03-12 11:29:59 -0700191 struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwq's */
Tejun Heo30cdf242013-03-12 11:29:57 -0700192 struct list_head pwqs; /* I: all pwqs of this wq */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200193 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200194
195 struct mutex flush_mutex; /* protects wq flushing */
196 int work_color; /* F: current work color */
197 int flush_color; /* F: current flush color */
Tejun Heo112202d2013-02-13 19:29:12 -0800198 atomic_t nr_pwqs_to_flush; /* flush in progress */
Tejun Heo73f53c42010-06-29 10:07:11 +0200199 struct wq_flusher *first_flusher; /* F: first flusher */
200 struct list_head flusher_queue; /* F: flush waiters */
201 struct list_head flusher_overflow; /* F: flush overflow list */
202
Tejun Heo493a1722013-03-12 11:29:59 -0700203 struct list_head maydays; /* W: pwqs requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200204 struct worker *rescuer; /* I: rescue worker */
205
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200206 int nr_drainers; /* W: drain in progress */
Tejun Heo112202d2013-02-13 19:29:12 -0800207 int saved_max_active; /* W: saved pwq max_active */
Johannes Berg4e6045f2007-10-18 23:39:55 -0700208#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200209 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700210#endif
Tejun Heob196be82012-01-10 15:11:35 -0800211 char name[]; /* I: workqueue name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212};
213
Tejun Heoe904e6c2013-03-12 11:29:57 -0700214static struct kmem_cache *pwq_cache;
215
Tejun Heod320c032010-06-29 10:07:14 +0200216struct workqueue_struct *system_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200217EXPORT_SYMBOL_GPL(system_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300218struct workqueue_struct *system_highpri_wq __read_mostly;
Joonsoo Kim1aabe902012-08-15 23:25:39 +0900219EXPORT_SYMBOL_GPL(system_highpri_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300220struct workqueue_struct *system_long_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200221EXPORT_SYMBOL_GPL(system_long_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300222struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200223EXPORT_SYMBOL_GPL(system_unbound_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300224struct workqueue_struct *system_freezable_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100225EXPORT_SYMBOL_GPL(system_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200226
Tejun Heo97bd2342010-10-05 10:41:14 +0200227#define CREATE_TRACE_POINTS
228#include <trace/events/workqueue.h>
229
Tejun Heo38db41d2013-01-24 11:01:34 -0800230#define for_each_std_worker_pool(pool, cpu) \
Tejun Heoa60dc392013-01-24 11:01:34 -0800231 for ((pool) = &std_worker_pools(cpu)[0]; \
232 (pool) < &std_worker_pools(cpu)[NR_STD_WORKER_POOLS]; (pool)++)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700233
Sasha Levinb67bfe02013-02-27 17:06:00 -0800234#define for_each_busy_worker(worker, i, pool) \
235 hash_for_each(pool->busy_hash, i, worker, hentry)
Tejun Heodb7bccf2010-06-29 10:07:12 +0200236
Tejun Heo706026c2013-01-24 11:01:34 -0800237static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
238 unsigned int sw)
Tejun Heof3421792010-07-02 10:03:51 +0200239{
240 if (cpu < nr_cpu_ids) {
241 if (sw & 1) {
242 cpu = cpumask_next(cpu, mask);
243 if (cpu < nr_cpu_ids)
244 return cpu;
245 }
246 if (sw & 2)
247 return WORK_CPU_UNBOUND;
248 }
Lai Jiangshan6be19582013-02-06 18:04:53 -0800249 return WORK_CPU_END;
Tejun Heof3421792010-07-02 10:03:51 +0200250}
251
Tejun Heo09884952010-08-01 11:50:12 +0200252/*
253 * CPU iterators
254 *
Tejun Heo706026c2013-01-24 11:01:34 -0800255 * An extra cpu number is defined using an invalid cpu number
Tejun Heo09884952010-08-01 11:50:12 +0200256 * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any
Tejun Heo706026c2013-01-24 11:01:34 -0800257 * specific CPU. The following iterators are similar to for_each_*_cpu()
258 * iterators but also considers the unbound CPU.
Tejun Heo09884952010-08-01 11:50:12 +0200259 *
Tejun Heo706026c2013-01-24 11:01:34 -0800260 * for_each_wq_cpu() : possible CPUs + WORK_CPU_UNBOUND
261 * for_each_online_wq_cpu() : online CPUs + WORK_CPU_UNBOUND
Tejun Heo09884952010-08-01 11:50:12 +0200262 */
Tejun Heo706026c2013-01-24 11:01:34 -0800263#define for_each_wq_cpu(cpu) \
264 for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, 3); \
Lai Jiangshan6be19582013-02-06 18:04:53 -0800265 (cpu) < WORK_CPU_END; \
Tejun Heo706026c2013-01-24 11:01:34 -0800266 (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, 3))
Tejun Heof3421792010-07-02 10:03:51 +0200267
Tejun Heo706026c2013-01-24 11:01:34 -0800268#define for_each_online_wq_cpu(cpu) \
269 for ((cpu) = __next_wq_cpu(-1, cpu_online_mask, 3); \
Lai Jiangshan6be19582013-02-06 18:04:53 -0800270 (cpu) < WORK_CPU_END; \
Tejun Heo706026c2013-01-24 11:01:34 -0800271 (cpu) = __next_wq_cpu((cpu), cpu_online_mask, 3))
Tejun Heof3421792010-07-02 10:03:51 +0200272
Tejun Heo49e3cf42013-03-12 11:29:58 -0700273/**
Tejun Heo17116962013-03-12 11:29:58 -0700274 * for_each_pool - iterate through all worker_pools in the system
275 * @pool: iteration cursor
276 * @id: integer used for iteration
277 */
278#define for_each_pool(pool, id) \
279 idr_for_each_entry(&worker_pool_idr, pool, id)
280
281/**
Tejun Heo49e3cf42013-03-12 11:29:58 -0700282 * for_each_pwq - iterate through all pool_workqueues of the specified workqueue
283 * @pwq: iteration cursor
284 * @wq: the target workqueue
285 */
286#define for_each_pwq(pwq, wq) \
287 list_for_each_entry((pwq), &(wq)->pwqs, pwqs_node)
Tejun Heof3421792010-07-02 10:03:51 +0200288
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900289#ifdef CONFIG_DEBUG_OBJECTS_WORK
290
291static struct debug_obj_descr work_debug_descr;
292
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100293static void *work_debug_hint(void *addr)
294{
295 return ((struct work_struct *) addr)->func;
296}
297
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900298/*
299 * fixup_init is called when:
300 * - an active object is initialized
301 */
302static int work_fixup_init(void *addr, enum debug_obj_state state)
303{
304 struct work_struct *work = addr;
305
306 switch (state) {
307 case ODEBUG_STATE_ACTIVE:
308 cancel_work_sync(work);
309 debug_object_init(work, &work_debug_descr);
310 return 1;
311 default:
312 return 0;
313 }
314}
315
316/*
317 * fixup_activate is called when:
318 * - an active object is activated
319 * - an unknown object is activated (might be a statically initialized object)
320 */
321static int work_fixup_activate(void *addr, enum debug_obj_state state)
322{
323 struct work_struct *work = addr;
324
325 switch (state) {
326
327 case ODEBUG_STATE_NOTAVAILABLE:
328 /*
329 * This is not really a fixup. The work struct was
330 * statically initialized. We just make sure that it
331 * is tracked in the object tracker.
332 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200333 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900334 debug_object_init(work, &work_debug_descr);
335 debug_object_activate(work, &work_debug_descr);
336 return 0;
337 }
338 WARN_ON_ONCE(1);
339 return 0;
340
341 case ODEBUG_STATE_ACTIVE:
342 WARN_ON(1);
343
344 default:
345 return 0;
346 }
347}
348
349/*
350 * fixup_free is called when:
351 * - an active object is freed
352 */
353static int work_fixup_free(void *addr, enum debug_obj_state state)
354{
355 struct work_struct *work = addr;
356
357 switch (state) {
358 case ODEBUG_STATE_ACTIVE:
359 cancel_work_sync(work);
360 debug_object_free(work, &work_debug_descr);
361 return 1;
362 default:
363 return 0;
364 }
365}
366
367static struct debug_obj_descr work_debug_descr = {
368 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100369 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900370 .fixup_init = work_fixup_init,
371 .fixup_activate = work_fixup_activate,
372 .fixup_free = work_fixup_free,
373};
374
375static inline void debug_work_activate(struct work_struct *work)
376{
377 debug_object_activate(work, &work_debug_descr);
378}
379
380static inline void debug_work_deactivate(struct work_struct *work)
381{
382 debug_object_deactivate(work, &work_debug_descr);
383}
384
385void __init_work(struct work_struct *work, int onstack)
386{
387 if (onstack)
388 debug_object_init_on_stack(work, &work_debug_descr);
389 else
390 debug_object_init(work, &work_debug_descr);
391}
392EXPORT_SYMBOL_GPL(__init_work);
393
394void destroy_work_on_stack(struct work_struct *work)
395{
396 debug_object_free(work, &work_debug_descr);
397}
398EXPORT_SYMBOL_GPL(destroy_work_on_stack);
399
400#else
401static inline void debug_work_activate(struct work_struct *work) { }
402static inline void debug_work_deactivate(struct work_struct *work) { }
403#endif
404
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100405/* Serializes the accesses to the list of workqueues. */
406static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407static LIST_HEAD(workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200408static bool workqueue_freezing; /* W: have wqs started freezing? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Oleg Nesterov14441962007-05-23 13:57:57 -0700410/*
Tejun Heoe19e3972013-01-24 11:39:44 -0800411 * The CPU and unbound standard worker pools. The unbound ones have
412 * POOL_DISASSOCIATED set, and their workers have WORKER_UNBOUND set.
Oleg Nesterov14441962007-05-23 13:57:57 -0700413 */
Tejun Heoe19e3972013-01-24 11:39:44 -0800414static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS],
415 cpu_std_worker_pools);
Tejun Heoa60dc392013-01-24 11:01:34 -0800416static struct worker_pool unbound_std_worker_pools[NR_STD_WORKER_POOLS];
Tejun Heof3421792010-07-02 10:03:51 +0200417
Tejun Heo9daf9e62013-01-24 11:01:33 -0800418/* idr of all pools */
419static DEFINE_MUTEX(worker_pool_idr_mutex);
420static DEFINE_IDR(worker_pool_idr);
421
Tejun Heoc34056a2010-06-29 10:07:11 +0200422static int worker_thread(void *__worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Tejun Heoa60dc392013-01-24 11:01:34 -0800424static struct worker_pool *std_worker_pools(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
Tejun Heof3421792010-07-02 10:03:51 +0200426 if (cpu != WORK_CPU_UNBOUND)
Tejun Heoa60dc392013-01-24 11:01:34 -0800427 return per_cpu(cpu_std_worker_pools, cpu);
Tejun Heof3421792010-07-02 10:03:51 +0200428 else
Tejun Heoa60dc392013-01-24 11:01:34 -0800429 return unbound_std_worker_pools;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
Tejun Heo4e8f0a62013-01-24 11:01:34 -0800432static int std_worker_pool_pri(struct worker_pool *pool)
433{
Tejun Heoa60dc392013-01-24 11:01:34 -0800434 return pool - std_worker_pools(pool->cpu);
Tejun Heo4e8f0a62013-01-24 11:01:34 -0800435}
436
Tejun Heo9daf9e62013-01-24 11:01:33 -0800437/* allocate ID and assign it to @pool */
438static int worker_pool_assign_id(struct worker_pool *pool)
439{
440 int ret;
441
442 mutex_lock(&worker_pool_idr_mutex);
443 idr_pre_get(&worker_pool_idr, GFP_KERNEL);
444 ret = idr_get_new(&worker_pool_idr, pool, &pool->id);
445 mutex_unlock(&worker_pool_idr_mutex);
446
447 return ret;
448}
449
Tejun Heo7c3eed52013-01-24 11:01:33 -0800450/*
451 * Lookup worker_pool by id. The idr currently is built during boot and
452 * never modified. Don't worry about locking for now.
453 */
454static struct worker_pool *worker_pool_by_id(int pool_id)
455{
456 return idr_find(&worker_pool_idr, pool_id);
457}
458
Tejun Heod565ed62013-01-24 11:01:33 -0800459static struct worker_pool *get_std_worker_pool(int cpu, bool highpri)
460{
Tejun Heoa60dc392013-01-24 11:01:34 -0800461 struct worker_pool *pools = std_worker_pools(cpu);
Tejun Heod565ed62013-01-24 11:01:33 -0800462
Tejun Heoa60dc392013-01-24 11:01:34 -0800463 return &pools[highpri];
Tejun Heod565ed62013-01-24 11:01:33 -0800464}
465
Tejun Heo7fb98ea2013-03-12 11:30:00 -0700466static struct pool_workqueue *first_pwq(struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700467{
Tejun Heo7fb98ea2013-03-12 11:30:00 -0700468 return list_first_entry(&wq->pwqs, struct pool_workqueue, pwqs_node);
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700469}
470
Tejun Heo73f53c42010-06-29 10:07:11 +0200471static unsigned int work_color_to_flags(int color)
472{
473 return color << WORK_STRUCT_COLOR_SHIFT;
474}
475
476static int get_work_color(struct work_struct *work)
477{
478 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
479 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
480}
481
482static int work_next_color(int color)
483{
484 return (color + 1) % WORK_NR_COLORS;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700485}
486
David Howells4594bf12006-12-07 11:33:26 +0000487/*
Tejun Heo112202d2013-02-13 19:29:12 -0800488 * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data
489 * contain the pointer to the queued pwq. Once execution starts, the flag
Tejun Heo7c3eed52013-01-24 11:01:33 -0800490 * is cleared and the high bits contain OFFQ flags and pool ID.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200491 *
Tejun Heo112202d2013-02-13 19:29:12 -0800492 * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling()
493 * and clear_work_data() can be used to set the pwq, pool or clear
Tejun Heobbb68df2012-08-03 10:30:46 -0700494 * work->data. These functions should only be called while the work is
495 * owned - ie. while the PENDING bit is set.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200496 *
Tejun Heo112202d2013-02-13 19:29:12 -0800497 * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq
Tejun Heo7c3eed52013-01-24 11:01:33 -0800498 * corresponding to a work. Pool is available once the work has been
Tejun Heo112202d2013-02-13 19:29:12 -0800499 * queued anywhere after initialization until it is sync canceled. pwq is
Tejun Heo7c3eed52013-01-24 11:01:33 -0800500 * available only while the work item is queued.
Tejun Heobbb68df2012-08-03 10:30:46 -0700501 *
502 * %WORK_OFFQ_CANCELING is used to mark a work item which is being
503 * canceled. While being canceled, a work item may have its PENDING set
504 * but stay off timer and worklist for arbitrarily long and nobody should
505 * try to steal the PENDING bit.
David Howells4594bf12006-12-07 11:33:26 +0000506 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200507static inline void set_work_data(struct work_struct *work, unsigned long data,
508 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000509{
Tejun Heo6183c002013-03-12 11:29:57 -0700510 WARN_ON_ONCE(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200511 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000512}
David Howells365970a2006-11-22 14:54:49 +0000513
Tejun Heo112202d2013-02-13 19:29:12 -0800514static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq,
Tejun Heo7a22ad72010-06-29 10:07:13 +0200515 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200516{
Tejun Heo112202d2013-02-13 19:29:12 -0800517 set_work_data(work, (unsigned long)pwq,
518 WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200519}
520
Lai Jiangshan4468a002013-02-06 18:04:53 -0800521static void set_work_pool_and_keep_pending(struct work_struct *work,
522 int pool_id)
523{
524 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT,
525 WORK_STRUCT_PENDING);
526}
527
Tejun Heo7c3eed52013-01-24 11:01:33 -0800528static void set_work_pool_and_clear_pending(struct work_struct *work,
529 int pool_id)
David Howells365970a2006-11-22 14:54:49 +0000530{
Tejun Heo23657bb2012-08-13 17:08:19 -0700531 /*
532 * The following wmb is paired with the implied mb in
533 * test_and_set_bit(PENDING) and ensures all updates to @work made
534 * here are visible to and precede any updates by the next PENDING
535 * owner.
536 */
537 smp_wmb();
Tejun Heo7c3eed52013-01-24 11:01:33 -0800538 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200539}
540
541static void clear_work_data(struct work_struct *work)
542{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800543 smp_wmb(); /* see set_work_pool_and_clear_pending() */
544 set_work_data(work, WORK_STRUCT_NO_POOL, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200545}
546
Tejun Heo112202d2013-02-13 19:29:12 -0800547static struct pool_workqueue *get_work_pwq(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200548{
Tejun Heoe1201532010-07-22 14:14:25 +0200549 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200550
Tejun Heo112202d2013-02-13 19:29:12 -0800551 if (data & WORK_STRUCT_PWQ)
Tejun Heoe1201532010-07-22 14:14:25 +0200552 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
553 else
554 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200555}
556
Tejun Heo7c3eed52013-01-24 11:01:33 -0800557/**
558 * get_work_pool - return the worker_pool a given work was associated with
559 * @work: the work item of interest
560 *
561 * Return the worker_pool @work was last associated with. %NULL if none.
562 */
563static struct worker_pool *get_work_pool(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200564{
Tejun Heoe1201532010-07-22 14:14:25 +0200565 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800566 struct worker_pool *pool;
567 int pool_id;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200568
Tejun Heo112202d2013-02-13 19:29:12 -0800569 if (data & WORK_STRUCT_PWQ)
570 return ((struct pool_workqueue *)
Tejun Heo7c3eed52013-01-24 11:01:33 -0800571 (data & WORK_STRUCT_WQ_DATA_MASK))->pool;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200572
Tejun Heo7c3eed52013-01-24 11:01:33 -0800573 pool_id = data >> WORK_OFFQ_POOL_SHIFT;
574 if (pool_id == WORK_OFFQ_POOL_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200575 return NULL;
576
Tejun Heo7c3eed52013-01-24 11:01:33 -0800577 pool = worker_pool_by_id(pool_id);
578 WARN_ON_ONCE(!pool);
579 return pool;
580}
581
582/**
583 * get_work_pool_id - return the worker pool ID a given work is associated with
584 * @work: the work item of interest
585 *
586 * Return the worker_pool ID @work was last associated with.
587 * %WORK_OFFQ_POOL_NONE if none.
588 */
589static int get_work_pool_id(struct work_struct *work)
590{
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800591 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800592
Tejun Heo112202d2013-02-13 19:29:12 -0800593 if (data & WORK_STRUCT_PWQ)
594 return ((struct pool_workqueue *)
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800595 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id;
596
597 return data >> WORK_OFFQ_POOL_SHIFT;
Tejun Heo7c3eed52013-01-24 11:01:33 -0800598}
599
Tejun Heobbb68df2012-08-03 10:30:46 -0700600static void mark_work_canceling(struct work_struct *work)
601{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800602 unsigned long pool_id = get_work_pool_id(work);
Tejun Heobbb68df2012-08-03 10:30:46 -0700603
Tejun Heo7c3eed52013-01-24 11:01:33 -0800604 pool_id <<= WORK_OFFQ_POOL_SHIFT;
605 set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700606}
607
608static bool work_is_canceling(struct work_struct *work)
609{
610 unsigned long data = atomic_long_read(&work->data);
611
Tejun Heo112202d2013-02-13 19:29:12 -0800612 return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700613}
614
David Howells365970a2006-11-22 14:54:49 +0000615/*
Tejun Heo32704762012-07-13 22:16:45 -0700616 * Policy functions. These define the policies on how the global worker
617 * pools are managed. Unless noted otherwise, these functions assume that
Tejun Heod565ed62013-01-24 11:01:33 -0800618 * they're being called with pool->lock held.
David Howells365970a2006-11-22 14:54:49 +0000619 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200620
Tejun Heo63d95a92012-07-12 14:46:37 -0700621static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000622{
Tejun Heoe19e3972013-01-24 11:39:44 -0800623 return !atomic_read(&pool->nr_running);
David Howells365970a2006-11-22 14:54:49 +0000624}
625
Tejun Heoe22bee72010-06-29 10:07:14 +0200626/*
627 * Need to wake up a worker? Called from anything but currently
628 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700629 *
630 * Note that, because unbound workers never contribute to nr_running, this
Tejun Heo706026c2013-01-24 11:01:34 -0800631 * function will always return %true for unbound pools as long as the
Tejun Heo974271c2012-07-12 14:46:37 -0700632 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200633 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700634static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000635{
Tejun Heo63d95a92012-07-12 14:46:37 -0700636 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000637}
638
Tejun Heoe22bee72010-06-29 10:07:14 +0200639/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700640static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200641{
Tejun Heo63d95a92012-07-12 14:46:37 -0700642 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200643}
644
645/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700646static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200647{
Tejun Heoe19e3972013-01-24 11:39:44 -0800648 return !list_empty(&pool->worklist) &&
649 atomic_read(&pool->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{
Lai Jiangshan552a37e2012-09-10 10:03:33 -0700668 bool managing = pool->flags & POOL_MANAGING_WORKERS;
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
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700672 /*
673 * nr_idle and idle_list may disagree if idle rebinding is in
674 * progress. Never return %true if idle_list is empty.
675 */
676 if (list_empty(&pool->idle_list))
677 return false;
678
Tejun Heoe22bee72010-06-29 10:07:14 +0200679 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
680}
681
682/*
683 * Wake up functions.
684 */
685
Tejun Heo7e116292010-06-29 10:07:13 +0200686/* Return the first worker. Safe with preemption disabled */
Tejun Heo63d95a92012-07-12 14:46:37 -0700687static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200688{
Tejun Heo63d95a92012-07-12 14:46:37 -0700689 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200690 return NULL;
691
Tejun Heo63d95a92012-07-12 14:46:37 -0700692 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200693}
694
695/**
696 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700697 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200698 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700699 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200700 *
701 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800702 * spin_lock_irq(pool->lock).
Tejun Heo7e116292010-06-29 10:07:13 +0200703 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700704static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200705{
Tejun Heo63d95a92012-07-12 14:46:37 -0700706 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200707
708 if (likely(worker))
709 wake_up_process(worker->task);
710}
711
Tejun Heo4690c4a2010-06-29 10:07:10 +0200712/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200713 * wq_worker_waking_up - a worker is waking up
714 * @task: task waking up
715 * @cpu: CPU @task is waking up to
716 *
717 * This function is called during try_to_wake_up() when a worker is
718 * being awoken.
719 *
720 * CONTEXT:
721 * spin_lock_irq(rq->lock)
722 */
Tejun Heod84ff052013-03-12 11:29:59 -0700723void wq_worker_waking_up(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200724{
725 struct worker *worker = kthread_data(task);
726
Joonsoo Kim36576002012-10-26 23:03:49 +0900727 if (!(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoec22ca52013-01-24 11:01:33 -0800728 WARN_ON_ONCE(worker->pool->cpu != cpu);
Tejun Heoe19e3972013-01-24 11:39:44 -0800729 atomic_inc(&worker->pool->nr_running);
Joonsoo Kim36576002012-10-26 23:03:49 +0900730 }
Tejun Heoe22bee72010-06-29 10:07:14 +0200731}
732
733/**
734 * wq_worker_sleeping - a worker is going to sleep
735 * @task: task going to sleep
736 * @cpu: CPU in question, must be the current CPU number
737 *
738 * This function is called during schedule() when a busy worker is
739 * going to sleep. Worker on the same cpu can be woken up by
740 * returning pointer to its task.
741 *
742 * CONTEXT:
743 * spin_lock_irq(rq->lock)
744 *
745 * RETURNS:
746 * Worker task on @cpu to wake up, %NULL if none.
747 */
Tejun Heod84ff052013-03-12 11:29:59 -0700748struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200749{
750 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heo111c2252013-01-17 17:16:24 -0800751 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200752
Tejun Heo111c2252013-01-17 17:16:24 -0800753 /*
754 * Rescuers, which may not have all the fields set up like normal
755 * workers, also reach here, let's not access anything before
756 * checking NOT_RUNNING.
757 */
Steven Rostedt2d646722010-12-03 23:12:33 -0500758 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200759 return NULL;
760
Tejun Heo111c2252013-01-17 17:16:24 -0800761 pool = worker->pool;
Tejun Heo111c2252013-01-17 17:16:24 -0800762
Tejun Heoe22bee72010-06-29 10:07:14 +0200763 /* this can only happen on the local cpu */
Tejun Heo6183c002013-03-12 11:29:57 -0700764 if (WARN_ON_ONCE(cpu != raw_smp_processor_id()))
765 return NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +0200766
767 /*
768 * The counterpart of the following dec_and_test, implied mb,
769 * worklist not empty test sequence is in insert_work().
770 * Please read comment there.
771 *
Tejun Heo628c78e2012-07-17 12:39:27 -0700772 * NOT_RUNNING is clear. This means that we're bound to and
773 * running on the local cpu w/ rq lock held and preemption
774 * disabled, which in turn means that none else could be
Tejun Heod565ed62013-01-24 11:01:33 -0800775 * manipulating idle_list, so dereferencing idle_list without pool
Tejun Heo628c78e2012-07-17 12:39:27 -0700776 * lock is safe.
Tejun Heoe22bee72010-06-29 10:07:14 +0200777 */
Tejun Heoe19e3972013-01-24 11:39:44 -0800778 if (atomic_dec_and_test(&pool->nr_running) &&
779 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700780 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200781 return to_wakeup ? to_wakeup->task : NULL;
782}
783
784/**
785 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200786 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200787 * @flags: flags to set
788 * @wakeup: wakeup an idle worker if necessary
789 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200790 * Set @flags in @worker->flags and adjust nr_running accordingly. If
791 * nr_running becomes zero and @wakeup is %true, an idle worker is
792 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200793 *
Tejun Heocb444762010-07-02 10:03:50 +0200794 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800795 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200796 */
797static inline void worker_set_flags(struct worker *worker, unsigned int flags,
798 bool wakeup)
799{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700800 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200801
Tejun Heocb444762010-07-02 10:03:50 +0200802 WARN_ON_ONCE(worker->task != current);
803
Tejun Heoe22bee72010-06-29 10:07:14 +0200804 /*
805 * If transitioning into NOT_RUNNING, adjust nr_running and
806 * wake up an idle worker as necessary if requested by
807 * @wakeup.
808 */
809 if ((flags & WORKER_NOT_RUNNING) &&
810 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoe22bee72010-06-29 10:07:14 +0200811 if (wakeup) {
Tejun Heoe19e3972013-01-24 11:39:44 -0800812 if (atomic_dec_and_test(&pool->nr_running) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -0700813 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700814 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200815 } else
Tejun Heoe19e3972013-01-24 11:39:44 -0800816 atomic_dec(&pool->nr_running);
Tejun Heoe22bee72010-06-29 10:07:14 +0200817 }
818
Tejun Heod302f012010-06-29 10:07:13 +0200819 worker->flags |= flags;
820}
821
822/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200823 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200824 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200825 * @flags: flags to clear
826 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200827 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200828 *
Tejun Heocb444762010-07-02 10:03:50 +0200829 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800830 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200831 */
832static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
833{
Tejun Heo63d95a92012-07-12 14:46:37 -0700834 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200835 unsigned int oflags = worker->flags;
836
Tejun Heocb444762010-07-02 10:03:50 +0200837 WARN_ON_ONCE(worker->task != current);
838
Tejun Heod302f012010-06-29 10:07:13 +0200839 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200840
Tejun Heo42c025f2011-01-11 15:58:49 +0100841 /*
842 * If transitioning out of NOT_RUNNING, increment nr_running. Note
843 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
844 * of multiple flags, not a single flag.
845 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200846 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
847 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heoe19e3972013-01-24 11:39:44 -0800848 atomic_inc(&pool->nr_running);
Tejun Heod302f012010-06-29 10:07:13 +0200849}
850
851/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200852 * find_worker_executing_work - find worker which is executing a work
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800853 * @pool: pool of interest
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200854 * @work: work to find worker for
855 *
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800856 * Find a worker which is executing @work on @pool by searching
857 * @pool->busy_hash which is keyed by the address of @work. For a worker
Tejun Heoa2c1c572012-12-18 10:35:02 -0800858 * to match, its current execution should match the address of @work and
859 * its work function. This is to avoid unwanted dependency between
860 * unrelated work executions through a work item being recycled while still
861 * being executed.
862 *
863 * This is a bit tricky. A work item may be freed once its execution
864 * starts and nothing prevents the freed area from being recycled for
865 * another work item. If the same work item address ends up being reused
866 * before the original execution finishes, workqueue will identify the
867 * recycled work item as currently executing and make it wait until the
868 * current execution finishes, introducing an unwanted dependency.
869 *
870 * This function checks the work item address, work function and workqueue
871 * to avoid false positives. Note that this isn't complete as one may
872 * construct a work function which can introduce dependency onto itself
873 * through a recycled work item. Well, if somebody wants to shoot oneself
874 * in the foot that badly, there's only so much we can do, and if such
875 * deadlock actually occurs, it should be easy to locate the culprit work
876 * function.
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200877 *
878 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800879 * spin_lock_irq(pool->lock).
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200880 *
881 * RETURNS:
882 * Pointer to worker which is executing @work if found, NULL
883 * otherwise.
884 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800885static struct worker *find_worker_executing_work(struct worker_pool *pool,
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200886 struct work_struct *work)
887{
Sasha Levin42f85702012-12-17 10:01:23 -0500888 struct worker *worker;
Sasha Levin42f85702012-12-17 10:01:23 -0500889
Sasha Levinb67bfe02013-02-27 17:06:00 -0800890 hash_for_each_possible(pool->busy_hash, worker, hentry,
Tejun Heoa2c1c572012-12-18 10:35:02 -0800891 (unsigned long)work)
892 if (worker->current_work == work &&
893 worker->current_func == work->func)
Sasha Levin42f85702012-12-17 10:01:23 -0500894 return worker;
895
896 return NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200897}
898
899/**
Tejun Heobf4ede02012-08-03 10:30:46 -0700900 * move_linked_works - move linked works to a list
901 * @work: start of series of works to be scheduled
902 * @head: target list to append @work to
903 * @nextp: out paramter for nested worklist walking
904 *
905 * Schedule linked works starting from @work to @head. Work series to
906 * be scheduled starts at @work and includes any consecutive work with
907 * WORK_STRUCT_LINKED set in its predecessor.
908 *
909 * If @nextp is not NULL, it's updated to point to the next work of
910 * the last scheduled work. This allows move_linked_works() to be
911 * nested inside outer list_for_each_entry_safe().
912 *
913 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800914 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -0700915 */
916static void move_linked_works(struct work_struct *work, struct list_head *head,
917 struct work_struct **nextp)
918{
919 struct work_struct *n;
920
921 /*
922 * Linked worklist will always end before the end of the list,
923 * use NULL for list head.
924 */
925 list_for_each_entry_safe_from(work, n, NULL, entry) {
926 list_move_tail(&work->entry, head);
927 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
928 break;
929 }
930
931 /*
932 * If we're already inside safe list traversal and have moved
933 * multiple works to the scheduled queue, the next position
934 * needs to be updated.
935 */
936 if (nextp)
937 *nextp = n;
938}
939
Tejun Heo112202d2013-02-13 19:29:12 -0800940static void pwq_activate_delayed_work(struct work_struct *work)
Tejun Heobf4ede02012-08-03 10:30:46 -0700941{
Tejun Heo112202d2013-02-13 19:29:12 -0800942 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobf4ede02012-08-03 10:30:46 -0700943
944 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -0800945 move_linked_works(work, &pwq->pool->worklist, NULL);
Tejun Heobf4ede02012-08-03 10:30:46 -0700946 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo112202d2013-02-13 19:29:12 -0800947 pwq->nr_active++;
Tejun Heobf4ede02012-08-03 10:30:46 -0700948}
949
Tejun Heo112202d2013-02-13 19:29:12 -0800950static void pwq_activate_first_delayed(struct pool_workqueue *pwq)
Lai Jiangshan3aa62492012-09-18 10:40:00 -0700951{
Tejun Heo112202d2013-02-13 19:29:12 -0800952 struct work_struct *work = list_first_entry(&pwq->delayed_works,
Lai Jiangshan3aa62492012-09-18 10:40:00 -0700953 struct work_struct, entry);
954
Tejun Heo112202d2013-02-13 19:29:12 -0800955 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -0700956}
957
Tejun Heobf4ede02012-08-03 10:30:46 -0700958/**
Tejun Heo112202d2013-02-13 19:29:12 -0800959 * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight
960 * @pwq: pwq of interest
Tejun Heobf4ede02012-08-03 10:30:46 -0700961 * @color: color of work which left the queue
Tejun Heobf4ede02012-08-03 10:30:46 -0700962 *
963 * A work either has completed or is removed from pending queue,
Tejun Heo112202d2013-02-13 19:29:12 -0800964 * decrement nr_in_flight of its pwq and handle workqueue flushing.
Tejun Heobf4ede02012-08-03 10:30:46 -0700965 *
966 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800967 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -0700968 */
Tejun Heo112202d2013-02-13 19:29:12 -0800969static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color)
Tejun Heobf4ede02012-08-03 10:30:46 -0700970{
971 /* ignore uncolored works */
972 if (color == WORK_NO_COLOR)
973 return;
974
Tejun Heo112202d2013-02-13 19:29:12 -0800975 pwq->nr_in_flight[color]--;
Tejun Heobf4ede02012-08-03 10:30:46 -0700976
Tejun Heo112202d2013-02-13 19:29:12 -0800977 pwq->nr_active--;
978 if (!list_empty(&pwq->delayed_works)) {
Lai Jiangshanb3f9f402012-09-18 10:40:00 -0700979 /* one down, submit a delayed one */
Tejun Heo112202d2013-02-13 19:29:12 -0800980 if (pwq->nr_active < pwq->max_active)
981 pwq_activate_first_delayed(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -0700982 }
983
984 /* is flush in progress and are we at the flushing tip? */
Tejun Heo112202d2013-02-13 19:29:12 -0800985 if (likely(pwq->flush_color != color))
Tejun Heobf4ede02012-08-03 10:30:46 -0700986 return;
987
988 /* are there still in-flight works? */
Tejun Heo112202d2013-02-13 19:29:12 -0800989 if (pwq->nr_in_flight[color])
Tejun Heobf4ede02012-08-03 10:30:46 -0700990 return;
991
Tejun Heo112202d2013-02-13 19:29:12 -0800992 /* this pwq is done, clear flush_color */
993 pwq->flush_color = -1;
Tejun Heobf4ede02012-08-03 10:30:46 -0700994
995 /*
Tejun Heo112202d2013-02-13 19:29:12 -0800996 * If this was the last pwq, wake up the first flusher. It
Tejun Heobf4ede02012-08-03 10:30:46 -0700997 * will handle the rest.
998 */
Tejun Heo112202d2013-02-13 19:29:12 -0800999 if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush))
1000 complete(&pwq->wq->first_flusher->done);
Tejun Heobf4ede02012-08-03 10:30:46 -07001001}
1002
Tejun Heo36e227d2012-08-03 10:30:46 -07001003/**
Tejun Heobbb68df2012-08-03 10:30:46 -07001004 * try_to_grab_pending - steal work item from worklist and disable irq
Tejun Heo36e227d2012-08-03 10:30:46 -07001005 * @work: work item to steal
1006 * @is_dwork: @work is a delayed_work
Tejun Heobbb68df2012-08-03 10:30:46 -07001007 * @flags: place to store irq state
Tejun Heo36e227d2012-08-03 10:30:46 -07001008 *
1009 * Try to grab PENDING bit of @work. This function can handle @work in any
1010 * stable state - idle, on timer or on worklist. Return values are
1011 *
1012 * 1 if @work was pending and we successfully stole PENDING
1013 * 0 if @work was idle and we claimed PENDING
1014 * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry
Tejun Heobbb68df2012-08-03 10:30:46 -07001015 * -ENOENT if someone else is canceling @work, this state may persist
1016 * for arbitrarily long
Tejun Heo36e227d2012-08-03 10:30:46 -07001017 *
Tejun Heobbb68df2012-08-03 10:30:46 -07001018 * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001019 * interrupted while holding PENDING and @work off queue, irq must be
1020 * disabled on entry. This, combined with delayed_work->timer being
1021 * irqsafe, ensures that we return -EAGAIN for finite short period of time.
Tejun Heobbb68df2012-08-03 10:30:46 -07001022 *
1023 * On successful return, >= 0, irq is disabled and the caller is
1024 * responsible for releasing it using local_irq_restore(*@flags).
1025 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001026 * This function is safe to call from any context including IRQ handler.
Tejun Heobf4ede02012-08-03 10:30:46 -07001027 */
Tejun Heobbb68df2012-08-03 10:30:46 -07001028static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
1029 unsigned long *flags)
Tejun Heobf4ede02012-08-03 10:30:46 -07001030{
Tejun Heod565ed62013-01-24 11:01:33 -08001031 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08001032 struct pool_workqueue *pwq;
Tejun Heobf4ede02012-08-03 10:30:46 -07001033
Tejun Heobbb68df2012-08-03 10:30:46 -07001034 local_irq_save(*flags);
1035
Tejun Heo36e227d2012-08-03 10:30:46 -07001036 /* try to steal the timer if it exists */
1037 if (is_dwork) {
1038 struct delayed_work *dwork = to_delayed_work(work);
1039
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001040 /*
1041 * dwork->timer is irqsafe. If del_timer() fails, it's
1042 * guaranteed that the timer is not queued anywhere and not
1043 * running on the local CPU.
1044 */
Tejun Heo36e227d2012-08-03 10:30:46 -07001045 if (likely(del_timer(&dwork->timer)))
1046 return 1;
1047 }
1048
1049 /* try to claim PENDING the normal way */
Tejun Heobf4ede02012-08-03 10:30:46 -07001050 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1051 return 0;
1052
1053 /*
1054 * The queueing is in progress, or it is already queued. Try to
1055 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1056 */
Tejun Heod565ed62013-01-24 11:01:33 -08001057 pool = get_work_pool(work);
1058 if (!pool)
Tejun Heobbb68df2012-08-03 10:30:46 -07001059 goto fail;
Tejun Heobf4ede02012-08-03 10:30:46 -07001060
Tejun Heod565ed62013-01-24 11:01:33 -08001061 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001062 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001063 * work->data is guaranteed to point to pwq only while the work
1064 * item is queued on pwq->wq, and both updating work->data to point
1065 * to pwq on queueing and to pool on dequeueing are done under
1066 * pwq->pool->lock. This in turn guarantees that, if work->data
1067 * points to pwq which is associated with a locked pool, the work
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001068 * item is currently queued on that pool.
1069 */
Tejun Heo112202d2013-02-13 19:29:12 -08001070 pwq = get_work_pwq(work);
1071 if (pwq && pwq->pool == pool) {
Tejun Heo16062832013-02-06 18:04:53 -08001072 debug_work_deactivate(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001073
Tejun Heo16062832013-02-06 18:04:53 -08001074 /*
1075 * A delayed work item cannot be grabbed directly because
1076 * it might have linked NO_COLOR work items which, if left
Tejun Heo112202d2013-02-13 19:29:12 -08001077 * on the delayed_list, will confuse pwq->nr_active
Tejun Heo16062832013-02-06 18:04:53 -08001078 * management later on and cause stall. Make sure the work
1079 * item is activated before grabbing.
1080 */
1081 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
Tejun Heo112202d2013-02-13 19:29:12 -08001082 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001083
Tejun Heo16062832013-02-06 18:04:53 -08001084 list_del_init(&work->entry);
Tejun Heo112202d2013-02-13 19:29:12 -08001085 pwq_dec_nr_in_flight(get_work_pwq(work), get_work_color(work));
Tejun Heo36e227d2012-08-03 10:30:46 -07001086
Tejun Heo112202d2013-02-13 19:29:12 -08001087 /* work->data points to pwq iff queued, point to pool */
Tejun Heo16062832013-02-06 18:04:53 -08001088 set_work_pool_and_keep_pending(work, pool->id);
Lai Jiangshan4468a002013-02-06 18:04:53 -08001089
Tejun Heo16062832013-02-06 18:04:53 -08001090 spin_unlock(&pool->lock);
1091 return 1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001092 }
Tejun Heod565ed62013-01-24 11:01:33 -08001093 spin_unlock(&pool->lock);
Tejun Heobbb68df2012-08-03 10:30:46 -07001094fail:
1095 local_irq_restore(*flags);
1096 if (work_is_canceling(work))
1097 return -ENOENT;
1098 cpu_relax();
Tejun Heo36e227d2012-08-03 10:30:46 -07001099 return -EAGAIN;
Tejun Heobf4ede02012-08-03 10:30:46 -07001100}
1101
1102/**
Tejun Heo706026c2013-01-24 11:01:34 -08001103 * insert_work - insert a work into a pool
Tejun Heo112202d2013-02-13 19:29:12 -08001104 * @pwq: pwq @work belongs to
Tejun Heo4690c4a2010-06-29 10:07:10 +02001105 * @work: work to insert
1106 * @head: insertion point
1107 * @extra_flags: extra WORK_STRUCT_* flags to set
1108 *
Tejun Heo112202d2013-02-13 19:29:12 -08001109 * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to
Tejun Heo706026c2013-01-24 11:01:34 -08001110 * work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +02001111 *
1112 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001113 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02001114 */
Tejun Heo112202d2013-02-13 19:29:12 -08001115static void insert_work(struct pool_workqueue *pwq, struct work_struct *work,
1116 struct list_head *head, unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001117{
Tejun Heo112202d2013-02-13 19:29:12 -08001118 struct worker_pool *pool = pwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01001119
Tejun Heo4690c4a2010-06-29 10:07:10 +02001120 /* we own @work, set data and link */
Tejun Heo112202d2013-02-13 19:29:12 -08001121 set_work_pwq(work, pwq, extra_flags);
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -07001122 list_add_tail(&work->entry, head);
Tejun Heoe22bee72010-06-29 10:07:14 +02001123
1124 /*
1125 * Ensure either worker_sched_deactivated() sees the above
1126 * list_add_tail() or we see zero nr_running to avoid workers
1127 * lying around lazily while there are works to be processed.
1128 */
1129 smp_mb();
1130
Tejun Heo63d95a92012-07-12 14:46:37 -07001131 if (__need_more_worker(pool))
1132 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001133}
1134
Tejun Heoc8efcc22010-12-20 19:32:04 +01001135/*
1136 * Test whether @work is being queued from another work executing on the
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001137 * same workqueue.
Tejun Heoc8efcc22010-12-20 19:32:04 +01001138 */
1139static bool is_chained_work(struct workqueue_struct *wq)
1140{
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001141 struct worker *worker;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001142
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001143 worker = current_wq_worker();
1144 /*
1145 * Return %true iff I'm a worker execuing a work item on @wq. If
1146 * I'm @worker, it's safe to dereference it without locking.
1147 */
Tejun Heo112202d2013-02-13 19:29:12 -08001148 return worker && worker->current_pwq->wq == wq;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001149}
1150
Tejun Heod84ff052013-03-12 11:29:59 -07001151static void __queue_work(int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 struct work_struct *work)
1153{
Tejun Heo112202d2013-02-13 19:29:12 -08001154 struct pool_workqueue *pwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001155 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001156 unsigned int work_flags;
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001157 unsigned int req_cpu = cpu;
Tejun Heo8930cab2012-08-03 10:30:45 -07001158
1159 /*
1160 * While a work item is PENDING && off queue, a task trying to
1161 * steal the PENDING will busy-loop waiting for it to either get
1162 * queued or lose PENDING. Grabbing PENDING and queueing should
1163 * happen with IRQ disabled.
1164 */
1165 WARN_ON_ONCE(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001167 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001168
Tejun Heoc8efcc22010-12-20 19:32:04 +01001169 /* if dying, only works from the same workqueue are allowed */
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02001170 if (unlikely(wq->flags & WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001171 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001172 return;
1173
Tejun Heo112202d2013-02-13 19:29:12 -08001174 /* determine the pwq to use */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001175 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001176 struct worker_pool *last_pool;
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001177
Tejun Heo57469822012-08-03 10:30:45 -07001178 if (cpu == WORK_CPU_UNBOUND)
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001179 cpu = raw_smp_processor_id();
1180
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001181 /*
Tejun Heodbf25762012-08-20 14:51:23 -07001182 * It's multi cpu. If @work was previously on a different
1183 * cpu, it might still be running there, in which case the
1184 * work needs to be queued on that cpu to guarantee
1185 * non-reentrancy.
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001186 */
Tejun Heo7fb98ea2013-03-12 11:30:00 -07001187 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001188 last_pool = get_work_pool(work);
Tejun Heodbf25762012-08-20 14:51:23 -07001189
Tejun Heo112202d2013-02-13 19:29:12 -08001190 if (last_pool && last_pool != pwq->pool) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001191 struct worker *worker;
1192
Tejun Heod565ed62013-01-24 11:01:33 -08001193 spin_lock(&last_pool->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001194
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001195 worker = find_worker_executing_work(last_pool, work);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001196
Tejun Heo112202d2013-02-13 19:29:12 -08001197 if (worker && worker->current_pwq->wq == wq) {
Tejun Heo7fb98ea2013-03-12 11:30:00 -07001198 pwq = per_cpu_ptr(wq->cpu_pwqs, last_pool->cpu);
Lai Jiangshan8594fad2013-02-07 13:14:20 -08001199 } else {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001200 /* meh... not running there, queue here */
Tejun Heod565ed62013-01-24 11:01:33 -08001201 spin_unlock(&last_pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08001202 spin_lock(&pwq->pool->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001203 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001204 } else {
Tejun Heo112202d2013-02-13 19:29:12 -08001205 spin_lock(&pwq->pool->lock);
Tejun Heo8930cab2012-08-03 10:30:45 -07001206 }
Tejun Heof3421792010-07-02 10:03:51 +02001207 } else {
Tejun Heo7fb98ea2013-03-12 11:30:00 -07001208 pwq = first_pwq(wq);
Tejun Heo112202d2013-02-13 19:29:12 -08001209 spin_lock(&pwq->pool->lock);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001210 }
1211
Tejun Heo112202d2013-02-13 19:29:12 -08001212 /* pwq determined, queue */
1213 trace_workqueue_queue_work(req_cpu, pwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001214
Dan Carpenterf5b25522012-04-13 22:06:58 +03001215 if (WARN_ON(!list_empty(&work->entry))) {
Tejun Heo112202d2013-02-13 19:29:12 -08001216 spin_unlock(&pwq->pool->lock);
Dan Carpenterf5b25522012-04-13 22:06:58 +03001217 return;
1218 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001219
Tejun Heo112202d2013-02-13 19:29:12 -08001220 pwq->nr_in_flight[pwq->work_color]++;
1221 work_flags = work_color_to_flags(pwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001222
Tejun Heo112202d2013-02-13 19:29:12 -08001223 if (likely(pwq->nr_active < pwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001224 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001225 pwq->nr_active++;
1226 worklist = &pwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001227 } else {
1228 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo112202d2013-02-13 19:29:12 -08001229 worklist = &pwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001230 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001231
Tejun Heo112202d2013-02-13 19:29:12 -08001232 insert_work(pwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001233
Tejun Heo112202d2013-02-13 19:29:12 -08001234 spin_unlock(&pwq->pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001237/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07001238 * queue_work_on - queue work on specific cpu
1239 * @cpu: CPU number to execute work on
1240 * @wq: workqueue to use
1241 * @work: work to queue
1242 *
Tejun Heod4283e92012-08-03 10:30:44 -07001243 * Returns %false if @work was already on a queue, %true otherwise.
Zhang Ruic1a220e2008-07-23 21:28:39 -07001244 *
1245 * We queue the work to a specific CPU, the caller must ensure it
1246 * can't go away.
1247 */
Tejun Heod4283e92012-08-03 10:30:44 -07001248bool queue_work_on(int cpu, struct workqueue_struct *wq,
1249 struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07001250{
Tejun Heod4283e92012-08-03 10:30:44 -07001251 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001252 unsigned long flags;
1253
1254 local_irq_save(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001255
Tejun Heo22df02b2010-06-29 10:07:10 +02001256 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001257 __queue_work(cpu, wq, work);
Tejun Heod4283e92012-08-03 10:30:44 -07001258 ret = true;
Zhang Ruic1a220e2008-07-23 21:28:39 -07001259 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001260
1261 local_irq_restore(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001262 return ret;
1263}
1264EXPORT_SYMBOL_GPL(queue_work_on);
1265
Tejun Heo0a13c002012-08-03 10:30:44 -07001266/**
1267 * queue_work - queue work on a workqueue
1268 * @wq: workqueue to use
1269 * @work: work to queue
1270 *
Tejun Heod4283e92012-08-03 10:30:44 -07001271 * Returns %false if @work was already on a queue, %true otherwise.
Tejun Heo0a13c002012-08-03 10:30:44 -07001272 *
1273 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1274 * it can be processed by another CPU.
1275 */
Tejun Heod4283e92012-08-03 10:30:44 -07001276bool queue_work(struct workqueue_struct *wq, struct work_struct *work)
Tejun Heo0a13c002012-08-03 10:30:44 -07001277{
Tejun Heo57469822012-08-03 10:30:45 -07001278 return queue_work_on(WORK_CPU_UNBOUND, wq, work);
Tejun Heo0a13c002012-08-03 10:30:44 -07001279}
1280EXPORT_SYMBOL_GPL(queue_work);
1281
Tejun Heod8e794d2012-08-03 10:30:45 -07001282void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
David Howells52bad642006-11-22 14:54:01 +00001284 struct delayed_work *dwork = (struct delayed_work *)__data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001286 /* should have been called from irqsafe timer with irq already off */
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001287 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288}
Konstantin Khlebnikov1438ade52013-01-24 16:36:31 +04001289EXPORT_SYMBOL(delayed_work_timer_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001291static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
1292 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001294 struct timer_list *timer = &dwork->timer;
1295 struct work_struct *work = &dwork->work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001297 WARN_ON_ONCE(timer->function != delayed_work_timer_fn ||
1298 timer->data != (unsigned long)dwork);
Tejun Heofc4b5142012-12-04 07:40:39 -08001299 WARN_ON_ONCE(timer_pending(timer));
1300 WARN_ON_ONCE(!list_empty(&work->entry));
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001301
Tejun Heo8852aac2012-12-01 16:23:42 -08001302 /*
1303 * If @delay is 0, queue @dwork->work immediately. This is for
1304 * both optimization and correctness. The earliest @timer can
1305 * expire is on the closest next tick and delayed_work users depend
1306 * on that there's no such delay when @delay is 0.
1307 */
1308 if (!delay) {
1309 __queue_work(cpu, wq, &dwork->work);
1310 return;
1311 }
1312
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001313 timer_stats_timer_set_start_info(&dwork->timer);
1314
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001315 dwork->wq = wq;
Tejun Heo12650572012-08-08 09:38:42 -07001316 dwork->cpu = cpu;
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001317 timer->expires = jiffies + delay;
1318
1319 if (unlikely(cpu != WORK_CPU_UNBOUND))
1320 add_timer_on(timer, cpu);
1321 else
1322 add_timer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323}
1324
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001325/**
1326 * queue_delayed_work_on - queue work on specific CPU after delay
1327 * @cpu: CPU number to execute work on
1328 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001329 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001330 * @delay: number of jiffies to wait before queueing
1331 *
Tejun Heo715f1302012-08-03 10:30:46 -07001332 * Returns %false if @work was already on a queue, %true otherwise. If
1333 * @delay is zero and @dwork is idle, it will be scheduled for immediate
1334 * execution.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001335 */
Tejun Heod4283e92012-08-03 10:30:44 -07001336bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
1337 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001338{
David Howells52bad642006-11-22 14:54:01 +00001339 struct work_struct *work = &dwork->work;
Tejun Heod4283e92012-08-03 10:30:44 -07001340 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001341 unsigned long flags;
1342
1343 /* read the comment in __queue_work() */
1344 local_irq_save(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001345
Tejun Heo22df02b2010-06-29 10:07:10 +02001346 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001347 __queue_delayed_work(cpu, wq, dwork, delay);
Tejun Heod4283e92012-08-03 10:30:44 -07001348 ret = true;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001349 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001350
1351 local_irq_restore(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001352 return ret;
1353}
Dave Jonesae90dd52006-06-30 01:40:45 -04001354EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Tejun Heoc8e55f32010-06-29 10:07:12 +02001356/**
Tejun Heo0a13c002012-08-03 10:30:44 -07001357 * queue_delayed_work - queue work on a workqueue after delay
1358 * @wq: workqueue to use
1359 * @dwork: delayable work to queue
1360 * @delay: number of jiffies to wait before queueing
1361 *
Tejun Heo715f1302012-08-03 10:30:46 -07001362 * Equivalent to queue_delayed_work_on() but tries to use the local CPU.
Tejun Heo0a13c002012-08-03 10:30:44 -07001363 */
Tejun Heod4283e92012-08-03 10:30:44 -07001364bool queue_delayed_work(struct workqueue_struct *wq,
Tejun Heo0a13c002012-08-03 10:30:44 -07001365 struct delayed_work *dwork, unsigned long delay)
1366{
Tejun Heo57469822012-08-03 10:30:45 -07001367 return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
Tejun Heo0a13c002012-08-03 10:30:44 -07001368}
1369EXPORT_SYMBOL_GPL(queue_delayed_work);
1370
1371/**
Tejun Heo8376fe22012-08-03 10:30:47 -07001372 * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
1373 * @cpu: CPU number to execute work on
1374 * @wq: workqueue to use
1375 * @dwork: work to queue
1376 * @delay: number of jiffies to wait before queueing
1377 *
1378 * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
1379 * modify @dwork's timer so that it expires after @delay. If @delay is
1380 * zero, @work is guaranteed to be scheduled immediately regardless of its
1381 * current state.
1382 *
1383 * Returns %false if @dwork was idle and queued, %true if @dwork was
1384 * pending and its timer was modified.
1385 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001386 * This function is safe to call from any context including IRQ handler.
Tejun Heo8376fe22012-08-03 10:30:47 -07001387 * See try_to_grab_pending() for details.
1388 */
1389bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
1390 struct delayed_work *dwork, unsigned long delay)
1391{
1392 unsigned long flags;
1393 int ret;
1394
1395 do {
1396 ret = try_to_grab_pending(&dwork->work, true, &flags);
1397 } while (unlikely(ret == -EAGAIN));
1398
1399 if (likely(ret >= 0)) {
1400 __queue_delayed_work(cpu, wq, dwork, delay);
1401 local_irq_restore(flags);
1402 }
1403
1404 /* -ENOENT from try_to_grab_pending() becomes %true */
1405 return ret;
1406}
1407EXPORT_SYMBOL_GPL(mod_delayed_work_on);
1408
1409/**
1410 * mod_delayed_work - modify delay of or queue a delayed work
1411 * @wq: workqueue to use
1412 * @dwork: work to queue
1413 * @delay: number of jiffies to wait before queueing
1414 *
1415 * mod_delayed_work_on() on local CPU.
1416 */
1417bool mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dwork,
1418 unsigned long delay)
1419{
1420 return mod_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
1421}
1422EXPORT_SYMBOL_GPL(mod_delayed_work);
1423
1424/**
Tejun Heoc8e55f32010-06-29 10:07:12 +02001425 * worker_enter_idle - enter idle state
1426 * @worker: worker which is entering idle state
1427 *
1428 * @worker is entering idle state. Update stats and idle timer if
1429 * necessary.
1430 *
1431 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001432 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001433 */
1434static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001436 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Tejun Heo6183c002013-03-12 11:29:57 -07001438 if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) ||
1439 WARN_ON_ONCE(!list_empty(&worker->entry) &&
1440 (worker->hentry.next || worker->hentry.pprev)))
1441 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Tejun Heocb444762010-07-02 10:03:50 +02001443 /* can't use worker_set_flags(), also called from start_worker() */
1444 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001445 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001446 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001447
Tejun Heoc8e55f32010-06-29 10:07:12 +02001448 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001449 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001450
Tejun Heo628c78e2012-07-17 12:39:27 -07001451 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1452 mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
Tejun Heocb444762010-07-02 10:03:50 +02001453
Tejun Heo544ecf32012-05-14 15:04:50 -07001454 /*
Tejun Heo706026c2013-01-24 11:01:34 -08001455 * Sanity check nr_running. Because wq_unbind_fn() releases
Tejun Heod565ed62013-01-24 11:01:33 -08001456 * pool->lock between setting %WORKER_UNBOUND and zapping
Tejun Heo628c78e2012-07-17 12:39:27 -07001457 * nr_running, the warning may trigger spuriously. Check iff
1458 * unbind is not in progress.
Tejun Heo544ecf32012-05-14 15:04:50 -07001459 */
Tejun Heo24647572013-01-24 11:01:33 -08001460 WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001461 pool->nr_workers == pool->nr_idle &&
Tejun Heoe19e3972013-01-24 11:39:44 -08001462 atomic_read(&pool->nr_running));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463}
1464
Tejun Heoc8e55f32010-06-29 10:07:12 +02001465/**
1466 * worker_leave_idle - leave idle state
1467 * @worker: worker which is leaving idle state
1468 *
1469 * @worker is leaving idle state. Update stats.
1470 *
1471 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001472 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001473 */
1474static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001476 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Tejun Heo6183c002013-03-12 11:29:57 -07001478 if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE)))
1479 return;
Tejun Heod302f012010-06-29 10:07:13 +02001480 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001481 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001482 list_del_init(&worker->entry);
1483}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Tejun Heoe22bee72010-06-29 10:07:14 +02001485/**
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001486 * worker_maybe_bind_and_lock - try to bind %current to worker_pool and lock it
1487 * @pool: target worker_pool
1488 *
1489 * Bind %current to the cpu of @pool if it is associated and lock @pool.
Tejun Heoe22bee72010-06-29 10:07:14 +02001490 *
1491 * Works which are scheduled while the cpu is online must at least be
1492 * scheduled to a worker which is bound to the cpu so that if they are
1493 * flushed from cpu callbacks while cpu is going down, they are
1494 * guaranteed to execute on the cpu.
1495 *
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001496 * This function is to be used by unbound workers and rescuers to bind
Tejun Heoe22bee72010-06-29 10:07:14 +02001497 * themselves to the target cpu and may race with cpu going down or
1498 * coming online. kthread_bind() can't be used because it may put the
1499 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
Tejun Heo706026c2013-01-24 11:01:34 -08001500 * verbatim as it's best effort and blocking and pool may be
Tejun Heoe22bee72010-06-29 10:07:14 +02001501 * [dis]associated in the meantime.
1502 *
Tejun Heo706026c2013-01-24 11:01:34 -08001503 * This function tries set_cpus_allowed() and locks pool and verifies the
Tejun Heo24647572013-01-24 11:01:33 -08001504 * binding against %POOL_DISASSOCIATED which is set during
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001505 * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1506 * enters idle state or fetches works without dropping lock, it can
1507 * guarantee the scheduling requirement described in the first paragraph.
Tejun Heoe22bee72010-06-29 10:07:14 +02001508 *
1509 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001510 * Might sleep. Called without any lock but returns with pool->lock
Tejun Heoe22bee72010-06-29 10:07:14 +02001511 * held.
1512 *
1513 * RETURNS:
Tejun Heo706026c2013-01-24 11:01:34 -08001514 * %true if the associated pool is online (@worker is successfully
Tejun Heoe22bee72010-06-29 10:07:14 +02001515 * bound), %false if offline.
1516 */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001517static bool worker_maybe_bind_and_lock(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001518__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001519{
Tejun Heoe22bee72010-06-29 10:07:14 +02001520 while (true) {
1521 /*
1522 * The following call may fail, succeed or succeed
1523 * without actually migrating the task to the cpu if
1524 * it races with cpu hotunplug operation. Verify
Tejun Heo24647572013-01-24 11:01:33 -08001525 * against POOL_DISASSOCIATED.
Tejun Heoe22bee72010-06-29 10:07:14 +02001526 */
Tejun Heo24647572013-01-24 11:01:33 -08001527 if (!(pool->flags & POOL_DISASSOCIATED))
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001528 set_cpus_allowed_ptr(current, get_cpu_mask(pool->cpu));
Oleg Nesterov85f41862007-05-09 02:34:20 -07001529
Tejun Heod565ed62013-01-24 11:01:33 -08001530 spin_lock_irq(&pool->lock);
Tejun Heo24647572013-01-24 11:01:33 -08001531 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heoe22bee72010-06-29 10:07:14 +02001532 return false;
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001533 if (task_cpu(current) == pool->cpu &&
Tejun Heoe22bee72010-06-29 10:07:14 +02001534 cpumask_equal(&current->cpus_allowed,
Tejun Heoec22ca52013-01-24 11:01:33 -08001535 get_cpu_mask(pool->cpu)))
Tejun Heoe22bee72010-06-29 10:07:14 +02001536 return true;
Tejun Heod565ed62013-01-24 11:01:33 -08001537 spin_unlock_irq(&pool->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001538
Tejun Heo5035b202011-04-29 18:08:37 +02001539 /*
1540 * We've raced with CPU hot[un]plug. Give it a breather
1541 * and retry migration. cond_resched() is required here;
1542 * otherwise, we might deadlock against cpu_stop trying to
1543 * bring down the CPU on non-preemptive kernel.
1544 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001545 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001546 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001547 }
1548}
1549
1550/*
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001551 * Rebind an idle @worker to its CPU. worker_thread() will test
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001552 * list_empty(@worker->entry) before leaving idle and call this function.
Tejun Heo25511a42012-07-17 12:39:27 -07001553 */
1554static void idle_worker_rebind(struct worker *worker)
1555{
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001556 /* CPU may go down again inbetween, clear UNBOUND only on success */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001557 if (worker_maybe_bind_and_lock(worker->pool))
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001558 worker_clr_flags(worker, WORKER_UNBOUND);
Tejun Heo25511a42012-07-17 12:39:27 -07001559
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001560 /* rebind complete, become available again */
1561 list_add(&worker->entry, &worker->pool->idle_list);
Tejun Heod565ed62013-01-24 11:01:33 -08001562 spin_unlock_irq(&worker->pool->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07001563}
1564
1565/*
1566 * Function for @worker->rebind.work used to rebind unbound busy workers to
Tejun Heo403c8212012-07-17 12:39:27 -07001567 * the associated cpu which is coming back online. This is scheduled by
1568 * cpu up but can race with other cpu hotplug operations and may be
1569 * executed twice without intervening cpu down.
Tejun Heoe22bee72010-06-29 10:07:14 +02001570 */
Tejun Heo25511a42012-07-17 12:39:27 -07001571static void busy_worker_rebind_fn(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001572{
1573 struct worker *worker = container_of(work, struct worker, rebind_work);
Tejun Heoe22bee72010-06-29 10:07:14 +02001574
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001575 if (worker_maybe_bind_and_lock(worker->pool))
Lai Jiangshaneab6d822012-09-18 09:59:22 -07001576 worker_clr_flags(worker, WORKER_UNBOUND);
Tejun Heoe22bee72010-06-29 10:07:14 +02001577
Tejun Heod565ed62013-01-24 11:01:33 -08001578 spin_unlock_irq(&worker->pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001579}
1580
Tejun Heo25511a42012-07-17 12:39:27 -07001581/**
Tejun Heo94cf58b2013-01-24 11:01:33 -08001582 * rebind_workers - rebind all workers of a pool to the associated CPU
1583 * @pool: pool of interest
Tejun Heo25511a42012-07-17 12:39:27 -07001584 *
Tejun Heo94cf58b2013-01-24 11:01:33 -08001585 * @pool->cpu is coming online. Rebind all workers to the CPU. Rebinding
Tejun Heo25511a42012-07-17 12:39:27 -07001586 * is different for idle and busy ones.
1587 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001588 * Idle ones will be removed from the idle_list and woken up. They will
1589 * add themselves back after completing rebind. This ensures that the
1590 * idle_list doesn't contain any unbound workers when re-bound busy workers
1591 * try to perform local wake-ups for concurrency management.
Tejun Heo25511a42012-07-17 12:39:27 -07001592 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001593 * Busy workers can rebind after they finish their current work items.
1594 * Queueing the rebind work item at the head of the scheduled list is
1595 * enough. Note that nr_running will be properly bumped as busy workers
1596 * rebind.
Tejun Heo25511a42012-07-17 12:39:27 -07001597 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001598 * On return, all non-manager workers are scheduled for rebind - see
1599 * manage_workers() for the manager special case. Any idle worker
1600 * including the manager will not appear on @idle_list until rebind is
1601 * complete, making local wake-ups safe.
Tejun Heo25511a42012-07-17 12:39:27 -07001602 */
Tejun Heo94cf58b2013-01-24 11:01:33 -08001603static void rebind_workers(struct worker_pool *pool)
Tejun Heo25511a42012-07-17 12:39:27 -07001604{
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001605 struct worker *worker, *n;
Tejun Heo25511a42012-07-17 12:39:27 -07001606 int i;
1607
Tejun Heo94cf58b2013-01-24 11:01:33 -08001608 lockdep_assert_held(&pool->assoc_mutex);
1609 lockdep_assert_held(&pool->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07001610
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001611 /* dequeue and kick idle ones */
Tejun Heo94cf58b2013-01-24 11:01:33 -08001612 list_for_each_entry_safe(worker, n, &pool->idle_list, entry) {
1613 /*
1614 * idle workers should be off @pool->idle_list until rebind
1615 * is complete to avoid receiving premature local wake-ups.
1616 */
1617 list_del_init(&worker->entry);
Lai Jiangshan96e65302012-09-02 00:28:19 +08001618
Tejun Heo94cf58b2013-01-24 11:01:33 -08001619 /*
1620 * worker_thread() will see the above dequeuing and call
1621 * idle_worker_rebind().
1622 */
1623 wake_up_process(worker->task);
1624 }
Tejun Heo25511a42012-07-17 12:39:27 -07001625
Tejun Heo94cf58b2013-01-24 11:01:33 -08001626 /* rebind busy workers */
Sasha Levinb67bfe02013-02-27 17:06:00 -08001627 for_each_busy_worker(worker, i, pool) {
Tejun Heo94cf58b2013-01-24 11:01:33 -08001628 struct work_struct *rebind_work = &worker->rebind_work;
1629 struct workqueue_struct *wq;
Tejun Heo25511a42012-07-17 12:39:27 -07001630
Tejun Heo94cf58b2013-01-24 11:01:33 -08001631 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
1632 work_data_bits(rebind_work)))
1633 continue;
Tejun Heo25511a42012-07-17 12:39:27 -07001634
Tejun Heo94cf58b2013-01-24 11:01:33 -08001635 debug_work_activate(rebind_work);
Tejun Heo90beca52012-09-04 23:12:33 -07001636
Tejun Heo94cf58b2013-01-24 11:01:33 -08001637 /*
1638 * wq doesn't really matter but let's keep @worker->pool
Tejun Heo112202d2013-02-13 19:29:12 -08001639 * and @pwq->pool consistent for sanity.
Tejun Heo94cf58b2013-01-24 11:01:33 -08001640 */
1641 if (std_worker_pool_pri(worker->pool))
1642 wq = system_highpri_wq;
1643 else
1644 wq = system_wq;
Tejun Heoec588152012-09-04 23:16:32 -07001645
Tejun Heo7fb98ea2013-03-12 11:30:00 -07001646 insert_work(per_cpu_ptr(wq->cpu_pwqs, pool->cpu), rebind_work,
Tejun Heo94cf58b2013-01-24 11:01:33 -08001647 worker->scheduled.next,
1648 work_color_to_flags(WORK_NO_COLOR));
Tejun Heoec588152012-09-04 23:16:32 -07001649 }
Tejun Heo25511a42012-07-17 12:39:27 -07001650}
1651
Tejun Heoc34056a2010-06-29 10:07:11 +02001652static struct worker *alloc_worker(void)
1653{
1654 struct worker *worker;
1655
1656 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001657 if (worker) {
1658 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001659 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heo25511a42012-07-17 12:39:27 -07001660 INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn);
Tejun Heoe22bee72010-06-29 10:07:14 +02001661 /* on creation a worker is in !idle && prep state */
1662 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001663 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001664 return worker;
1665}
1666
1667/**
1668 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001669 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001670 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001671 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001672 * can be started by calling start_worker() or destroyed using
1673 * destroy_worker().
1674 *
1675 * CONTEXT:
1676 * Might sleep. Does GFP_KERNEL allocations.
1677 *
1678 * RETURNS:
1679 * Pointer to the newly created worker.
1680 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001681static struct worker *create_worker(struct worker_pool *pool)
Tejun Heoc34056a2010-06-29 10:07:11 +02001682{
Tejun Heoe34cdddb2013-01-24 11:01:33 -08001683 const char *pri = std_worker_pool_pri(pool) ? "H" : "";
Tejun Heoc34056a2010-06-29 10:07:11 +02001684 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001685 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001686
Tejun Heod565ed62013-01-24 11:01:33 -08001687 spin_lock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001688 while (ida_get_new(&pool->worker_ida, &id)) {
Tejun Heod565ed62013-01-24 11:01:33 -08001689 spin_unlock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001690 if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001691 goto fail;
Tejun Heod565ed62013-01-24 11:01:33 -08001692 spin_lock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001693 }
Tejun Heod565ed62013-01-24 11:01:33 -08001694 spin_unlock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001695
1696 worker = alloc_worker();
1697 if (!worker)
1698 goto fail;
1699
Tejun Heobd7bdd42012-07-12 14:46:37 -07001700 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001701 worker->id = id;
1702
Tejun Heoec22ca52013-01-24 11:01:33 -08001703 if (pool->cpu != WORK_CPU_UNBOUND)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001704 worker->task = kthread_create_on_node(worker_thread,
Tejun Heoec22ca52013-01-24 11:01:33 -08001705 worker, cpu_to_node(pool->cpu),
Tejun Heod84ff052013-03-12 11:29:59 -07001706 "kworker/%d:%d%s", pool->cpu, id, pri);
Tejun Heof3421792010-07-02 10:03:51 +02001707 else
1708 worker->task = kthread_create(worker_thread, worker,
Tejun Heo32704762012-07-13 22:16:45 -07001709 "kworker/u:%d%s", id, pri);
Tejun Heoc34056a2010-06-29 10:07:11 +02001710 if (IS_ERR(worker->task))
1711 goto fail;
1712
Tejun Heoe34cdddb2013-01-24 11:01:33 -08001713 if (std_worker_pool_pri(pool))
Tejun Heo32704762012-07-13 22:16:45 -07001714 set_user_nice(worker->task, HIGHPRI_NICE_LEVEL);
1715
Tejun Heodb7bccf2010-06-29 10:07:12 +02001716 /*
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001717 * Determine CPU binding of the new worker depending on
Tejun Heo24647572013-01-24 11:01:33 -08001718 * %POOL_DISASSOCIATED. The caller is responsible for ensuring the
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001719 * flag remains stable across this function. See the comments
1720 * above the flag definition for details.
1721 *
1722 * As an unbound worker may later become a regular one if CPU comes
1723 * online, make sure every worker has %PF_THREAD_BOUND set.
Tejun Heodb7bccf2010-06-29 10:07:12 +02001724 */
Tejun Heo24647572013-01-24 11:01:33 -08001725 if (!(pool->flags & POOL_DISASSOCIATED)) {
Tejun Heoec22ca52013-01-24 11:01:33 -08001726 kthread_bind(worker->task, pool->cpu);
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001727 } else {
Tejun Heodb7bccf2010-06-29 10:07:12 +02001728 worker->task->flags |= PF_THREAD_BOUND;
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001729 worker->flags |= WORKER_UNBOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001731
Tejun Heoc34056a2010-06-29 10:07:11 +02001732 return worker;
1733fail:
1734 if (id >= 0) {
Tejun Heod565ed62013-01-24 11:01:33 -08001735 spin_lock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001736 ida_remove(&pool->worker_ida, id);
Tejun Heod565ed62013-01-24 11:01:33 -08001737 spin_unlock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001738 }
1739 kfree(worker);
1740 return NULL;
1741}
1742
1743/**
1744 * start_worker - start a newly created worker
1745 * @worker: worker to start
1746 *
Tejun Heo706026c2013-01-24 11:01:34 -08001747 * Make the pool aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001748 *
1749 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001750 * spin_lock_irq(pool->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001751 */
1752static void start_worker(struct worker *worker)
1753{
Tejun Heocb444762010-07-02 10:03:50 +02001754 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001755 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001756 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001757 wake_up_process(worker->task);
1758}
1759
1760/**
1761 * destroy_worker - destroy a workqueue worker
1762 * @worker: worker to be destroyed
1763 *
Tejun Heo706026c2013-01-24 11:01:34 -08001764 * Destroy @worker and adjust @pool stats accordingly.
Tejun Heoc8e55f32010-06-29 10:07:12 +02001765 *
1766 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001767 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001768 */
1769static void destroy_worker(struct worker *worker)
1770{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001771 struct worker_pool *pool = worker->pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001772 int id = worker->id;
1773
1774 /* sanity check frenzy */
Tejun Heo6183c002013-03-12 11:29:57 -07001775 if (WARN_ON(worker->current_work) ||
1776 WARN_ON(!list_empty(&worker->scheduled)))
1777 return;
Tejun Heoc34056a2010-06-29 10:07:11 +02001778
Tejun Heoc8e55f32010-06-29 10:07:12 +02001779 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001780 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001781 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001782 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001783
1784 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001785 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001786
Tejun Heod565ed62013-01-24 11:01:33 -08001787 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001788
Tejun Heoc34056a2010-06-29 10:07:11 +02001789 kthread_stop(worker->task);
1790 kfree(worker);
1791
Tejun Heod565ed62013-01-24 11:01:33 -08001792 spin_lock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001793 ida_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001794}
1795
Tejun Heo63d95a92012-07-12 14:46:37 -07001796static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001797{
Tejun Heo63d95a92012-07-12 14:46:37 -07001798 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001799
Tejun Heod565ed62013-01-24 11:01:33 -08001800 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001801
Tejun Heo63d95a92012-07-12 14:46:37 -07001802 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001803 struct worker *worker;
1804 unsigned long expires;
1805
1806 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001807 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001808 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1809
1810 if (time_before(jiffies, expires))
Tejun Heo63d95a92012-07-12 14:46:37 -07001811 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001812 else {
1813 /* it's been idle for too long, wake up manager */
Tejun Heo11ebea52012-07-12 14:46:37 -07001814 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -07001815 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001816 }
1817 }
1818
Tejun Heod565ed62013-01-24 11:01:33 -08001819 spin_unlock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001820}
1821
Tejun Heo493a1722013-03-12 11:29:59 -07001822static void send_mayday(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001823{
Tejun Heo112202d2013-02-13 19:29:12 -08001824 struct pool_workqueue *pwq = get_work_pwq(work);
1825 struct workqueue_struct *wq = pwq->wq;
Tejun Heo493a1722013-03-12 11:29:59 -07001826
1827 lockdep_assert_held(&workqueue_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001828
1829 if (!(wq->flags & WQ_RESCUER))
Tejun Heo493a1722013-03-12 11:29:59 -07001830 return;
Tejun Heoe22bee72010-06-29 10:07:14 +02001831
1832 /* mayday mayday mayday */
Tejun Heo493a1722013-03-12 11:29:59 -07001833 if (list_empty(&pwq->mayday_node)) {
1834 list_add_tail(&pwq->mayday_node, &wq->maydays);
Tejun Heoe22bee72010-06-29 10:07:14 +02001835 wake_up_process(wq->rescuer->task);
Tejun Heo493a1722013-03-12 11:29:59 -07001836 }
Tejun Heoe22bee72010-06-29 10:07:14 +02001837}
1838
Tejun Heo706026c2013-01-24 11:01:34 -08001839static void pool_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001840{
Tejun Heo63d95a92012-07-12 14:46:37 -07001841 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001842 struct work_struct *work;
1843
Tejun Heo493a1722013-03-12 11:29:59 -07001844 spin_lock_irq(&workqueue_lock); /* for wq->maydays */
1845 spin_lock(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001846
Tejun Heo63d95a92012-07-12 14:46:37 -07001847 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001848 /*
1849 * We've been trying to create a new worker but
1850 * haven't been successful. We might be hitting an
1851 * allocation deadlock. Send distress signals to
1852 * rescuers.
1853 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001854 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001855 send_mayday(work);
1856 }
1857
Tejun Heo493a1722013-03-12 11:29:59 -07001858 spin_unlock(&pool->lock);
1859 spin_unlock_irq(&workqueue_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001860
Tejun Heo63d95a92012-07-12 14:46:37 -07001861 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001862}
1863
1864/**
1865 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001866 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001867 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001868 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001869 * have at least one idle worker on return from this function. If
1870 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001871 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001872 * possible allocation deadlock.
1873 *
1874 * On return, need_to_create_worker() is guaranteed to be false and
1875 * may_start_working() true.
1876 *
1877 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001878 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001879 * multiple times. Does GFP_KERNEL allocations. Called only from
1880 * manager.
1881 *
1882 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08001883 * false if no action was taken and pool->lock stayed locked, true
Tejun Heoe22bee72010-06-29 10:07:14 +02001884 * otherwise.
1885 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001886static bool maybe_create_worker(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001887__releases(&pool->lock)
1888__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001889{
Tejun Heo63d95a92012-07-12 14:46:37 -07001890 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001891 return false;
1892restart:
Tejun Heod565ed62013-01-24 11:01:33 -08001893 spin_unlock_irq(&pool->lock);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001894
Tejun Heoe22bee72010-06-29 10:07:14 +02001895 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07001896 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001897
1898 while (true) {
1899 struct worker *worker;
1900
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001901 worker = create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001902 if (worker) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001903 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001904 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001905 start_worker(worker);
Tejun Heo6183c002013-03-12 11:29:57 -07001906 if (WARN_ON_ONCE(need_to_create_worker(pool)))
1907 goto restart;
Tejun Heoe22bee72010-06-29 10:07:14 +02001908 return true;
1909 }
1910
Tejun Heo63d95a92012-07-12 14:46:37 -07001911 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001912 break;
1913
Tejun Heoe22bee72010-06-29 10:07:14 +02001914 __set_current_state(TASK_INTERRUPTIBLE);
1915 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001916
Tejun Heo63d95a92012-07-12 14:46:37 -07001917 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001918 break;
1919 }
1920
Tejun Heo63d95a92012-07-12 14:46:37 -07001921 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001922 spin_lock_irq(&pool->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07001923 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001924 goto restart;
1925 return true;
1926}
1927
1928/**
1929 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo63d95a92012-07-12 14:46:37 -07001930 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001931 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001932 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02001933 * IDLE_WORKER_TIMEOUT.
1934 *
1935 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001936 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001937 * multiple times. Called only from manager.
1938 *
1939 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08001940 * false if no action was taken and pool->lock stayed locked, true
Tejun Heoe22bee72010-06-29 10:07:14 +02001941 * otherwise.
1942 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001943static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001944{
1945 bool ret = false;
1946
Tejun Heo63d95a92012-07-12 14:46:37 -07001947 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001948 struct worker *worker;
1949 unsigned long expires;
1950
Tejun Heo63d95a92012-07-12 14:46:37 -07001951 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001952 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1953
1954 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001955 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001956 break;
1957 }
1958
1959 destroy_worker(worker);
1960 ret = true;
1961 }
1962
1963 return ret;
1964}
1965
1966/**
1967 * manage_workers - manage worker pool
1968 * @worker: self
1969 *
Tejun Heo706026c2013-01-24 11:01:34 -08001970 * Assume the manager role and manage the worker pool @worker belongs
Tejun Heoe22bee72010-06-29 10:07:14 +02001971 * to. At any given time, there can be only zero or one manager per
Tejun Heo706026c2013-01-24 11:01:34 -08001972 * pool. The exclusion is handled automatically by this function.
Tejun Heoe22bee72010-06-29 10:07:14 +02001973 *
1974 * The caller can safely start processing works on false return. On
1975 * true return, it's guaranteed that need_to_create_worker() is false
1976 * and may_start_working() is true.
1977 *
1978 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001979 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001980 * multiple times. Does GFP_KERNEL allocations.
1981 *
1982 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08001983 * spin_lock_irq(pool->lock) which may be released and regrabbed
1984 * multiple times. Does GFP_KERNEL allocations.
Tejun Heoe22bee72010-06-29 10:07:14 +02001985 */
1986static bool manage_workers(struct worker *worker)
1987{
Tejun Heo63d95a92012-07-12 14:46:37 -07001988 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001989 bool ret = false;
1990
Lai Jiangshanee378aa2012-09-10 10:03:44 -07001991 if (pool->flags & POOL_MANAGING_WORKERS)
Tejun Heoe22bee72010-06-29 10:07:14 +02001992 return ret;
1993
Lai Jiangshan552a37e2012-09-10 10:03:33 -07001994 pool->flags |= POOL_MANAGING_WORKERS;
Lai Jiangshanee378aa2012-09-10 10:03:44 -07001995
1996 /*
1997 * To simplify both worker management and CPU hotplug, hold off
1998 * management while hotplug is in progress. CPU hotplug path can't
1999 * grab %POOL_MANAGING_WORKERS to achieve this because that can
2000 * lead to idle worker depletion (all become busy thinking someone
2001 * else is managing) which in turn can result in deadlock under
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002002 * extreme circumstances. Use @pool->assoc_mutex to synchronize
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002003 * manager against CPU hotplug.
2004 *
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002005 * assoc_mutex would always be free unless CPU hotplug is in
Tejun Heod565ed62013-01-24 11:01:33 -08002006 * progress. trylock first without dropping @pool->lock.
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002007 */
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002008 if (unlikely(!mutex_trylock(&pool->assoc_mutex))) {
Tejun Heod565ed62013-01-24 11:01:33 -08002009 spin_unlock_irq(&pool->lock);
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002010 mutex_lock(&pool->assoc_mutex);
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002011 /*
2012 * CPU hotplug could have happened while we were waiting
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002013 * for assoc_mutex. Hotplug itself can't handle us
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002014 * because manager isn't either on idle or busy list, and
Tejun Heo706026c2013-01-24 11:01:34 -08002015 * @pool's state and ours could have deviated.
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002016 *
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002017 * As hotplug is now excluded via assoc_mutex, we can
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002018 * simply try to bind. It will succeed or fail depending
Tejun Heo706026c2013-01-24 11:01:34 -08002019 * on @pool's current state. Try it and adjust
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002020 * %WORKER_UNBOUND accordingly.
2021 */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08002022 if (worker_maybe_bind_and_lock(pool))
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002023 worker->flags &= ~WORKER_UNBOUND;
2024 else
2025 worker->flags |= WORKER_UNBOUND;
2026
2027 ret = true;
2028 }
2029
Tejun Heo11ebea52012-07-12 14:46:37 -07002030 pool->flags &= ~POOL_MANAGE_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02002031
2032 /*
2033 * Destroy and then create so that may_start_working() is true
2034 * on return.
2035 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002036 ret |= maybe_destroy_workers(pool);
2037 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002038
Lai Jiangshan552a37e2012-09-10 10:03:33 -07002039 pool->flags &= ~POOL_MANAGING_WORKERS;
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002040 mutex_unlock(&pool->assoc_mutex);
Tejun Heoe22bee72010-06-29 10:07:14 +02002041 return ret;
2042}
2043
Tejun Heoa62428c2010-06-29 10:07:10 +02002044/**
2045 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02002046 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02002047 * @work: work to process
2048 *
2049 * Process @work. This function contains all the logics necessary to
2050 * process a single work including synchronization against and
2051 * interaction with other workers on the same cpu, queueing and
2052 * flushing. As long as context requirement is met, any worker can
2053 * call this function to process a work.
2054 *
2055 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002056 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02002057 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002058static void process_one_work(struct worker *worker, struct work_struct *work)
Tejun Heod565ed62013-01-24 11:01:33 -08002059__releases(&pool->lock)
2060__acquires(&pool->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02002061{
Tejun Heo112202d2013-02-13 19:29:12 -08002062 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002063 struct worker_pool *pool = worker->pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002064 bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02002065 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02002066 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02002067#ifdef CONFIG_LOCKDEP
2068 /*
2069 * It is permissible to free the struct work_struct from
2070 * inside the function that is called from it, this we need to
2071 * take into account for lockdep too. To avoid bogus "held
2072 * lock freed" warnings as well as problems when looking into
2073 * work->lockdep_map, make a copy and use that here.
2074 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07002075 struct lockdep_map lockdep_map;
2076
2077 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002078#endif
Tejun Heo6fec10a2012-07-22 10:16:34 -07002079 /*
2080 * Ensure we're on the correct CPU. DISASSOCIATED test is
2081 * necessary to avoid spurious warnings from rescuers servicing the
Tejun Heo24647572013-01-24 11:01:33 -08002082 * unbound or a disassociated pool.
Tejun Heo6fec10a2012-07-22 10:16:34 -07002083 */
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002084 WARN_ON_ONCE(!(worker->flags & WORKER_UNBOUND) &&
Tejun Heo24647572013-01-24 11:01:33 -08002085 !(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heoec22ca52013-01-24 11:01:33 -08002086 raw_smp_processor_id() != pool->cpu);
Tejun Heo25511a42012-07-17 12:39:27 -07002087
Tejun Heo7e116292010-06-29 10:07:13 +02002088 /*
2089 * A single work shouldn't be executed concurrently by
2090 * multiple workers on a single cpu. Check whether anyone is
2091 * already processing the work. If so, defer the work to the
2092 * currently executing one.
2093 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002094 collision = find_worker_executing_work(pool, work);
Tejun Heo7e116292010-06-29 10:07:13 +02002095 if (unlikely(collision)) {
2096 move_linked_works(work, &collision->scheduled, NULL);
2097 return;
2098 }
2099
Tejun Heo8930cab2012-08-03 10:30:45 -07002100 /* claim and dequeue */
Tejun Heoa62428c2010-06-29 10:07:10 +02002101 debug_work_deactivate(work);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002102 hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work);
Tejun Heoc34056a2010-06-29 10:07:11 +02002103 worker->current_work = work;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002104 worker->current_func = work->func;
Tejun Heo112202d2013-02-13 19:29:12 -08002105 worker->current_pwq = pwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002106 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002107
Tejun Heoa62428c2010-06-29 10:07:10 +02002108 list_del_init(&work->entry);
2109
Tejun Heo649027d2010-06-29 10:07:14 +02002110 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02002111 * CPU intensive works don't participate in concurrency
2112 * management. They're the scheduler's responsibility.
2113 */
2114 if (unlikely(cpu_intensive))
2115 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
2116
Tejun Heo974271c2012-07-12 14:46:37 -07002117 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002118 * Unbound pool isn't concurrency managed and work items should be
Tejun Heo974271c2012-07-12 14:46:37 -07002119 * executed ASAP. Wake up another worker if necessary.
2120 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002121 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
2122 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07002123
Tejun Heo8930cab2012-08-03 10:30:45 -07002124 /*
Tejun Heo7c3eed52013-01-24 11:01:33 -08002125 * Record the last pool and clear PENDING which should be the last
Tejun Heod565ed62013-01-24 11:01:33 -08002126 * update to @work. Also, do this inside @pool->lock so that
Tejun Heo23657bb2012-08-13 17:08:19 -07002127 * PENDING and queued state changes happen together while IRQ is
2128 * disabled.
Tejun Heo8930cab2012-08-03 10:30:45 -07002129 */
Tejun Heo7c3eed52013-01-24 11:01:33 -08002130 set_work_pool_and_clear_pending(work, pool->id);
Tejun Heoa62428c2010-06-29 10:07:10 +02002131
Tejun Heod565ed62013-01-24 11:01:33 -08002132 spin_unlock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002133
Tejun Heo112202d2013-02-13 19:29:12 -08002134 lock_map_acquire_read(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002135 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002136 trace_workqueue_execute_start(work);
Tejun Heoa2c1c572012-12-18 10:35:02 -08002137 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002138 /*
2139 * While we must be careful to not use "work" after this, the trace
2140 * point will only record its address.
2141 */
2142 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002143 lock_map_release(&lockdep_map);
Tejun Heo112202d2013-02-13 19:29:12 -08002144 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002145
2146 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Valentin Ilie044c7822012-08-19 00:52:42 +03002147 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
2148 " last function: %pf\n",
Tejun Heoa2c1c572012-12-18 10:35:02 -08002149 current->comm, preempt_count(), task_pid_nr(current),
2150 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02002151 debug_show_held_locks(current);
2152 dump_stack();
2153 }
2154
Tejun Heod565ed62013-01-24 11:01:33 -08002155 spin_lock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002156
Tejun Heofb0e7be2010-06-29 10:07:15 +02002157 /* clear cpu intensive status */
2158 if (unlikely(cpu_intensive))
2159 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2160
Tejun Heoa62428c2010-06-29 10:07:10 +02002161 /* we're done with it, release */
Sasha Levin42f85702012-12-17 10:01:23 -05002162 hash_del(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002163 worker->current_work = NULL;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002164 worker->current_func = NULL;
Tejun Heo112202d2013-02-13 19:29:12 -08002165 worker->current_pwq = NULL;
2166 pwq_dec_nr_in_flight(pwq, work_color);
Tejun Heoa62428c2010-06-29 10:07:10 +02002167}
2168
Tejun Heoaffee4b2010-06-29 10:07:12 +02002169/**
2170 * process_scheduled_works - process scheduled works
2171 * @worker: self
2172 *
2173 * Process all scheduled works. Please note that the scheduled list
2174 * may change while processing a work, so this function repeatedly
2175 * fetches a work from the top and executes it.
2176 *
2177 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002178 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02002179 * multiple times.
2180 */
2181static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002183 while (!list_empty(&worker->scheduled)) {
2184 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002186 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188}
2189
Tejun Heo4690c4a2010-06-29 10:07:10 +02002190/**
2191 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02002192 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02002193 *
Tejun Heo706026c2013-01-24 11:01:34 -08002194 * The worker thread function. There are NR_CPU_WORKER_POOLS dynamic pools
2195 * of these per each cpu. These workers process all works regardless of
Tejun Heoe22bee72010-06-29 10:07:14 +02002196 * their specific target workqueue. The only exception is works which
2197 * belong to workqueues with a rescuer which will be explained in
2198 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02002199 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002200static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
Tejun Heoc34056a2010-06-29 10:07:11 +02002202 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07002203 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Tejun Heoe22bee72010-06-29 10:07:14 +02002205 /* tell the scheduler that this is a workqueue worker */
2206 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002207woke_up:
Tejun Heod565ed62013-01-24 11:01:33 -08002208 spin_lock_irq(&pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002210 /* we are off idle list if destruction or rebind is requested */
2211 if (unlikely(list_empty(&worker->entry))) {
Tejun Heod565ed62013-01-24 11:01:33 -08002212 spin_unlock_irq(&pool->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07002213
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002214 /* if DIE is set, destruction is requested */
Tejun Heo25511a42012-07-17 12:39:27 -07002215 if (worker->flags & WORKER_DIE) {
2216 worker->task->flags &= ~PF_WQ_WORKER;
2217 return 0;
2218 }
2219
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002220 /* otherwise, rebind */
Tejun Heo25511a42012-07-17 12:39:27 -07002221 idle_worker_rebind(worker);
2222 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 }
2224
Tejun Heoc8e55f32010-06-29 10:07:12 +02002225 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002226recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002227 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002228 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002229 goto sleep;
2230
2231 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002232 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002233 goto recheck;
2234
Tejun Heoc8e55f32010-06-29 10:07:12 +02002235 /*
2236 * ->scheduled list can only be filled while a worker is
2237 * preparing to process a work or actually processing it.
2238 * Make sure nobody diddled with it while I was sleeping.
2239 */
Tejun Heo6183c002013-03-12 11:29:57 -07002240 WARN_ON_ONCE(!list_empty(&worker->scheduled));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002241
Tejun Heoe22bee72010-06-29 10:07:14 +02002242 /*
2243 * When control reaches this point, we're guaranteed to have
2244 * at least one idle worker or that someone else has already
2245 * assumed the manager role.
2246 */
2247 worker_clr_flags(worker, WORKER_PREP);
2248
2249 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002250 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002251 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002252 struct work_struct, entry);
2253
2254 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2255 /* optimization path, not strictly necessary */
2256 process_one_work(worker, work);
2257 if (unlikely(!list_empty(&worker->scheduled)))
2258 process_scheduled_works(worker);
2259 } else {
2260 move_linked_works(work, &worker->scheduled, NULL);
2261 process_scheduled_works(worker);
2262 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002263 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002264
Tejun Heoe22bee72010-06-29 10:07:14 +02002265 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002266sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07002267 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002268 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002269
Tejun Heoc8e55f32010-06-29 10:07:12 +02002270 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002271 * pool->lock is held and there's no work to process and no need to
2272 * manage, sleep. Workers are woken up only while holding
2273 * pool->lock or from local cpu, so setting the current state
2274 * before releasing pool->lock is enough to prevent losing any
2275 * event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002276 */
2277 worker_enter_idle(worker);
2278 __set_current_state(TASK_INTERRUPTIBLE);
Tejun Heod565ed62013-01-24 11:01:33 -08002279 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02002280 schedule();
2281 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282}
2283
Tejun Heoe22bee72010-06-29 10:07:14 +02002284/**
2285 * rescuer_thread - the rescuer thread function
Tejun Heo111c2252013-01-17 17:16:24 -08002286 * @__rescuer: self
Tejun Heoe22bee72010-06-29 10:07:14 +02002287 *
2288 * Workqueue rescuer thread function. There's one rescuer for each
2289 * workqueue which has WQ_RESCUER set.
2290 *
Tejun Heo706026c2013-01-24 11:01:34 -08002291 * Regular work processing on a pool may block trying to create a new
Tejun Heoe22bee72010-06-29 10:07:14 +02002292 * worker which uses GFP_KERNEL allocation which has slight chance of
2293 * developing into deadlock if some works currently on the same queue
2294 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2295 * the problem rescuer solves.
2296 *
Tejun Heo706026c2013-01-24 11:01:34 -08002297 * When such condition is possible, the pool summons rescuers of all
2298 * workqueues which have works queued on the pool and let them process
Tejun Heoe22bee72010-06-29 10:07:14 +02002299 * those works so that forward progress can be guaranteed.
2300 *
2301 * This should happen rarely.
2302 */
Tejun Heo111c2252013-01-17 17:16:24 -08002303static int rescuer_thread(void *__rescuer)
Tejun Heoe22bee72010-06-29 10:07:14 +02002304{
Tejun Heo111c2252013-01-17 17:16:24 -08002305 struct worker *rescuer = __rescuer;
2306 struct workqueue_struct *wq = rescuer->rescue_wq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002307 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heoe22bee72010-06-29 10:07:14 +02002308
2309 set_user_nice(current, RESCUER_NICE_LEVEL);
Tejun Heo111c2252013-01-17 17:16:24 -08002310
2311 /*
2312 * Mark rescuer as worker too. As WORKER_PREP is never cleared, it
2313 * doesn't participate in concurrency management.
2314 */
2315 rescuer->task->flags |= PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002316repeat:
2317 set_current_state(TASK_INTERRUPTIBLE);
2318
Mike Galbraith412d32e2012-11-28 07:17:18 +01002319 if (kthread_should_stop()) {
2320 __set_current_state(TASK_RUNNING);
Tejun Heo111c2252013-01-17 17:16:24 -08002321 rescuer->task->flags &= ~PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002322 return 0;
Mike Galbraith412d32e2012-11-28 07:17:18 +01002323 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002324
Tejun Heo493a1722013-03-12 11:29:59 -07002325 /* see whether any pwq is asking for help */
2326 spin_lock_irq(&workqueue_lock);
2327
2328 while (!list_empty(&wq->maydays)) {
2329 struct pool_workqueue *pwq = list_first_entry(&wq->maydays,
2330 struct pool_workqueue, mayday_node);
Tejun Heo112202d2013-02-13 19:29:12 -08002331 struct worker_pool *pool = pwq->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002332 struct work_struct *work, *n;
2333
2334 __set_current_state(TASK_RUNNING);
Tejun Heo493a1722013-03-12 11:29:59 -07002335 list_del_init(&pwq->mayday_node);
2336
2337 spin_unlock_irq(&workqueue_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002338
2339 /* migrate to the target cpu if possible */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08002340 worker_maybe_bind_and_lock(pool);
Lai Jiangshanb3104102013-02-19 12:17:02 -08002341 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002342
2343 /*
2344 * Slurp in all works issued via this workqueue and
2345 * process'em.
2346 */
Tejun Heo6183c002013-03-12 11:29:57 -07002347 WARN_ON_ONCE(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002348 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heo112202d2013-02-13 19:29:12 -08002349 if (get_work_pwq(work) == pwq)
Tejun Heoe22bee72010-06-29 10:07:14 +02002350 move_linked_works(work, scheduled, &n);
2351
2352 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002353
2354 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002355 * Leave this pool. If keep_working() is %true, notify a
Tejun Heo75769582011-02-14 14:04:46 +01002356 * regular worker; otherwise, we end up with 0 concurrency
2357 * and stalling the execution.
2358 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002359 if (keep_working(pool))
2360 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002361
Lai Jiangshanb3104102013-02-19 12:17:02 -08002362 rescuer->pool = NULL;
Tejun Heo493a1722013-03-12 11:29:59 -07002363 spin_unlock(&pool->lock);
2364 spin_lock(&workqueue_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002365 }
2366
Tejun Heo493a1722013-03-12 11:29:59 -07002367 spin_unlock_irq(&workqueue_lock);
2368
Tejun Heo111c2252013-01-17 17:16:24 -08002369 /* rescuers should never participate in concurrency management */
2370 WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
Tejun Heoe22bee72010-06-29 10:07:14 +02002371 schedule();
2372 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373}
2374
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002375struct wq_barrier {
2376 struct work_struct work;
2377 struct completion done;
2378};
2379
2380static void wq_barrier_func(struct work_struct *work)
2381{
2382 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2383 complete(&barr->done);
2384}
2385
Tejun Heo4690c4a2010-06-29 10:07:10 +02002386/**
2387 * insert_wq_barrier - insert a barrier work
Tejun Heo112202d2013-02-13 19:29:12 -08002388 * @pwq: pwq to insert barrier into
Tejun Heo4690c4a2010-06-29 10:07:10 +02002389 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002390 * @target: target work to attach @barr to
2391 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002392 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002393 * @barr is linked to @target such that @barr is completed only after
2394 * @target finishes execution. Please note that the ordering
2395 * guarantee is observed only with respect to @target and on the local
2396 * cpu.
2397 *
2398 * Currently, a queued barrier can't be canceled. This is because
2399 * try_to_grab_pending() can't determine whether the work to be
2400 * grabbed is at the head of the queue and thus can't clear LINKED
2401 * flag of the previous work while there must be a valid next work
2402 * after a work with LINKED flag set.
2403 *
2404 * Note that when @worker is non-NULL, @target may be modified
Tejun Heo112202d2013-02-13 19:29:12 -08002405 * underneath us, so we can't reliably determine pwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002406 *
2407 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002408 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002409 */
Tejun Heo112202d2013-02-13 19:29:12 -08002410static void insert_wq_barrier(struct pool_workqueue *pwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002411 struct wq_barrier *barr,
2412 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002413{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002414 struct list_head *head;
2415 unsigned int linked = 0;
2416
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002417 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002418 * debugobject calls are safe here even with pool->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002419 * as we know for sure that this will not trigger any of the
2420 * checks and call back into the fixup functions where we
2421 * might deadlock.
2422 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002423 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002424 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002425 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002426
Tejun Heoaffee4b2010-06-29 10:07:12 +02002427 /*
2428 * If @target is currently being executed, schedule the
2429 * barrier to the worker; otherwise, put it after @target.
2430 */
2431 if (worker)
2432 head = worker->scheduled.next;
2433 else {
2434 unsigned long *bits = work_data_bits(target);
2435
2436 head = target->entry.next;
2437 /* there can already be other linked works, inherit and set */
2438 linked = *bits & WORK_STRUCT_LINKED;
2439 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2440 }
2441
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002442 debug_work_activate(&barr->work);
Tejun Heo112202d2013-02-13 19:29:12 -08002443 insert_work(pwq, &barr->work, head,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002444 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002445}
2446
Tejun Heo73f53c42010-06-29 10:07:11 +02002447/**
Tejun Heo112202d2013-02-13 19:29:12 -08002448 * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing
Tejun Heo73f53c42010-06-29 10:07:11 +02002449 * @wq: workqueue being flushed
2450 * @flush_color: new flush color, < 0 for no-op
2451 * @work_color: new work color, < 0 for no-op
2452 *
Tejun Heo112202d2013-02-13 19:29:12 -08002453 * Prepare pwqs for workqueue flushing.
Tejun Heo73f53c42010-06-29 10:07:11 +02002454 *
Tejun Heo112202d2013-02-13 19:29:12 -08002455 * If @flush_color is non-negative, flush_color on all pwqs should be
2456 * -1. If no pwq has in-flight commands at the specified color, all
2457 * pwq->flush_color's stay at -1 and %false is returned. If any pwq
2458 * has in flight commands, its pwq->flush_color is set to
2459 * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq
Tejun Heo73f53c42010-06-29 10:07:11 +02002460 * wakeup logic is armed and %true is returned.
2461 *
2462 * The caller should have initialized @wq->first_flusher prior to
2463 * calling this function with non-negative @flush_color. If
2464 * @flush_color is negative, no flush color update is done and %false
2465 * is returned.
2466 *
Tejun Heo112202d2013-02-13 19:29:12 -08002467 * If @work_color is non-negative, all pwqs should have the same
Tejun Heo73f53c42010-06-29 10:07:11 +02002468 * work_color which is previous to @work_color and all will be
2469 * advanced to @work_color.
2470 *
2471 * CONTEXT:
2472 * mutex_lock(wq->flush_mutex).
2473 *
2474 * RETURNS:
2475 * %true if @flush_color >= 0 and there's something to flush. %false
2476 * otherwise.
2477 */
Tejun Heo112202d2013-02-13 19:29:12 -08002478static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
Tejun Heo73f53c42010-06-29 10:07:11 +02002479 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480{
Tejun Heo73f53c42010-06-29 10:07:11 +02002481 bool wait = false;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002482 struct pool_workqueue *pwq;
Oleg Nesterov14441962007-05-23 13:57:57 -07002483
Tejun Heo73f53c42010-06-29 10:07:11 +02002484 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002485 WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush));
Tejun Heo112202d2013-02-13 19:29:12 -08002486 atomic_set(&wq->nr_pwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002487 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002488
Tejun Heo49e3cf42013-03-12 11:29:58 -07002489 for_each_pwq(pwq, wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08002490 struct worker_pool *pool = pwq->pool;
Tejun Heo73f53c42010-06-29 10:07:11 +02002491
Tejun Heod565ed62013-01-24 11:01:33 -08002492 spin_lock_irq(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002493
2494 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002495 WARN_ON_ONCE(pwq->flush_color != -1);
Tejun Heo73f53c42010-06-29 10:07:11 +02002496
Tejun Heo112202d2013-02-13 19:29:12 -08002497 if (pwq->nr_in_flight[flush_color]) {
2498 pwq->flush_color = flush_color;
2499 atomic_inc(&wq->nr_pwqs_to_flush);
Tejun Heo73f53c42010-06-29 10:07:11 +02002500 wait = true;
2501 }
2502 }
2503
2504 if (work_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002505 WARN_ON_ONCE(work_color != work_next_color(pwq->work_color));
Tejun Heo112202d2013-02-13 19:29:12 -08002506 pwq->work_color = work_color;
Tejun Heo73f53c42010-06-29 10:07:11 +02002507 }
2508
Tejun Heod565ed62013-01-24 11:01:33 -08002509 spin_unlock_irq(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002510 }
2511
Tejun Heo112202d2013-02-13 19:29:12 -08002512 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush))
Tejun Heo73f53c42010-06-29 10:07:11 +02002513 complete(&wq->first_flusher->done);
2514
2515 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516}
2517
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002518/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002520 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 *
2522 * Forces execution of the workqueue and blocks until its completion.
2523 * This is typically used in driver shutdown handlers.
2524 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002525 * We sleep until all works which were queued on entry have been handled,
2526 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002528void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529{
Tejun Heo73f53c42010-06-29 10:07:11 +02002530 struct wq_flusher this_flusher = {
2531 .list = LIST_HEAD_INIT(this_flusher.list),
2532 .flush_color = -1,
2533 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2534 };
2535 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002536
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002537 lock_map_acquire(&wq->lockdep_map);
2538 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002539
2540 mutex_lock(&wq->flush_mutex);
2541
2542 /*
2543 * Start-to-wait phase
2544 */
2545 next_color = work_next_color(wq->work_color);
2546
2547 if (next_color != wq->flush_color) {
2548 /*
2549 * Color space is not full. The current work_color
2550 * becomes our flush_color and work_color is advanced
2551 * by one.
2552 */
Tejun Heo6183c002013-03-12 11:29:57 -07002553 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow));
Tejun Heo73f53c42010-06-29 10:07:11 +02002554 this_flusher.flush_color = wq->work_color;
2555 wq->work_color = next_color;
2556
2557 if (!wq->first_flusher) {
2558 /* no flush in progress, become the first flusher */
Tejun Heo6183c002013-03-12 11:29:57 -07002559 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002560
2561 wq->first_flusher = &this_flusher;
2562
Tejun Heo112202d2013-02-13 19:29:12 -08002563 if (!flush_workqueue_prep_pwqs(wq, wq->flush_color,
Tejun Heo73f53c42010-06-29 10:07:11 +02002564 wq->work_color)) {
2565 /* nothing to flush, done */
2566 wq->flush_color = next_color;
2567 wq->first_flusher = NULL;
2568 goto out_unlock;
2569 }
2570 } else {
2571 /* wait in queue */
Tejun Heo6183c002013-03-12 11:29:57 -07002572 WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002573 list_add_tail(&this_flusher.list, &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002574 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002575 }
2576 } else {
2577 /*
2578 * Oops, color space is full, wait on overflow queue.
2579 * The next flush completion will assign us
2580 * flush_color and transfer to flusher_queue.
2581 */
2582 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2583 }
2584
2585 mutex_unlock(&wq->flush_mutex);
2586
2587 wait_for_completion(&this_flusher.done);
2588
2589 /*
2590 * Wake-up-and-cascade phase
2591 *
2592 * First flushers are responsible for cascading flushes and
2593 * handling overflow. Non-first flushers can simply return.
2594 */
2595 if (wq->first_flusher != &this_flusher)
2596 return;
2597
2598 mutex_lock(&wq->flush_mutex);
2599
Tejun Heo4ce48b32010-07-02 10:03:51 +02002600 /* we might have raced, check again with mutex held */
2601 if (wq->first_flusher != &this_flusher)
2602 goto out_unlock;
2603
Tejun Heo73f53c42010-06-29 10:07:11 +02002604 wq->first_flusher = NULL;
2605
Tejun Heo6183c002013-03-12 11:29:57 -07002606 WARN_ON_ONCE(!list_empty(&this_flusher.list));
2607 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002608
2609 while (true) {
2610 struct wq_flusher *next, *tmp;
2611
2612 /* complete all the flushers sharing the current flush color */
2613 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2614 if (next->flush_color != wq->flush_color)
2615 break;
2616 list_del_init(&next->list);
2617 complete(&next->done);
2618 }
2619
Tejun Heo6183c002013-03-12 11:29:57 -07002620 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) &&
2621 wq->flush_color != work_next_color(wq->work_color));
Tejun Heo73f53c42010-06-29 10:07:11 +02002622
2623 /* this flush_color is finished, advance by one */
2624 wq->flush_color = work_next_color(wq->flush_color);
2625
2626 /* one color has been freed, handle overflow queue */
2627 if (!list_empty(&wq->flusher_overflow)) {
2628 /*
2629 * Assign the same color to all overflowed
2630 * flushers, advance work_color and append to
2631 * flusher_queue. This is the start-to-wait
2632 * phase for these overflowed flushers.
2633 */
2634 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2635 tmp->flush_color = wq->work_color;
2636
2637 wq->work_color = work_next_color(wq->work_color);
2638
2639 list_splice_tail_init(&wq->flusher_overflow,
2640 &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002641 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002642 }
2643
2644 if (list_empty(&wq->flusher_queue)) {
Tejun Heo6183c002013-03-12 11:29:57 -07002645 WARN_ON_ONCE(wq->flush_color != wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002646 break;
2647 }
2648
2649 /*
2650 * Need to flush more colors. Make the next flusher
Tejun Heo112202d2013-02-13 19:29:12 -08002651 * the new first flusher and arm pwqs.
Tejun Heo73f53c42010-06-29 10:07:11 +02002652 */
Tejun Heo6183c002013-03-12 11:29:57 -07002653 WARN_ON_ONCE(wq->flush_color == wq->work_color);
2654 WARN_ON_ONCE(wq->flush_color != next->flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002655
2656 list_del_init(&next->list);
2657 wq->first_flusher = next;
2658
Tejun Heo112202d2013-02-13 19:29:12 -08002659 if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1))
Tejun Heo73f53c42010-06-29 10:07:11 +02002660 break;
2661
2662 /*
2663 * Meh... this color is already done, clear first
2664 * flusher and repeat cascading.
2665 */
2666 wq->first_flusher = NULL;
2667 }
2668
2669out_unlock:
2670 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671}
Dave Jonesae90dd52006-06-30 01:40:45 -04002672EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002674/**
2675 * drain_workqueue - drain a workqueue
2676 * @wq: workqueue to drain
2677 *
2678 * Wait until the workqueue becomes empty. While draining is in progress,
2679 * only chain queueing is allowed. IOW, only currently pending or running
2680 * work items on @wq can queue further work items on it. @wq is flushed
2681 * repeatedly until it becomes empty. The number of flushing is detemined
2682 * by the depth of chaining and should be relatively short. Whine if it
2683 * takes too long.
2684 */
2685void drain_workqueue(struct workqueue_struct *wq)
2686{
2687 unsigned int flush_cnt = 0;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002688 struct pool_workqueue *pwq;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002689
2690 /*
2691 * __queue_work() needs to test whether there are drainers, is much
2692 * hotter than drain_workqueue() and already looks at @wq->flags.
2693 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2694 */
Tejun Heoe98d5b12013-03-12 11:29:57 -07002695 spin_lock_irq(&workqueue_lock);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002696 if (!wq->nr_drainers++)
2697 wq->flags |= WQ_DRAINING;
Tejun Heoe98d5b12013-03-12 11:29:57 -07002698 spin_unlock_irq(&workqueue_lock);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002699reflush:
2700 flush_workqueue(wq);
2701
Tejun Heo49e3cf42013-03-12 11:29:58 -07002702 for_each_pwq(pwq, wq) {
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002703 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002704
Tejun Heo112202d2013-02-13 19:29:12 -08002705 spin_lock_irq(&pwq->pool->lock);
2706 drained = !pwq->nr_active && list_empty(&pwq->delayed_works);
2707 spin_unlock_irq(&pwq->pool->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002708
2709 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002710 continue;
2711
2712 if (++flush_cnt == 10 ||
2713 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
Valentin Ilie044c7822012-08-19 00:52:42 +03002714 pr_warn("workqueue %s: flush on destruction isn't complete after %u tries\n",
2715 wq->name, flush_cnt);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002716 goto reflush;
2717 }
2718
Tejun Heoe98d5b12013-03-12 11:29:57 -07002719 spin_lock_irq(&workqueue_lock);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002720 if (!--wq->nr_drainers)
2721 wq->flags &= ~WQ_DRAINING;
Tejun Heoe98d5b12013-03-12 11:29:57 -07002722 spin_unlock_irq(&workqueue_lock);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002723}
2724EXPORT_SYMBOL_GPL(drain_workqueue);
2725
Tejun Heo606a5022012-08-20 14:51:23 -07002726static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
Tejun Heobaf59022010-09-16 10:42:16 +02002727{
2728 struct worker *worker = NULL;
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002729 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002730 struct pool_workqueue *pwq;
Tejun Heobaf59022010-09-16 10:42:16 +02002731
2732 might_sleep();
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002733 pool = get_work_pool(work);
2734 if (!pool)
Tejun Heobaf59022010-09-16 10:42:16 +02002735 return false;
2736
Tejun Heod565ed62013-01-24 11:01:33 -08002737 spin_lock_irq(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08002738 /* see the comment in try_to_grab_pending() with the same code */
Tejun Heo112202d2013-02-13 19:29:12 -08002739 pwq = get_work_pwq(work);
2740 if (pwq) {
2741 if (unlikely(pwq->pool != pool))
Tejun Heobaf59022010-09-16 10:42:16 +02002742 goto already_gone;
Tejun Heo606a5022012-08-20 14:51:23 -07002743 } else {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002744 worker = find_worker_executing_work(pool, work);
Tejun Heobaf59022010-09-16 10:42:16 +02002745 if (!worker)
2746 goto already_gone;
Tejun Heo112202d2013-02-13 19:29:12 -08002747 pwq = worker->current_pwq;
Tejun Heo606a5022012-08-20 14:51:23 -07002748 }
Tejun Heobaf59022010-09-16 10:42:16 +02002749
Tejun Heo112202d2013-02-13 19:29:12 -08002750 insert_wq_barrier(pwq, barr, work, worker);
Tejun Heod565ed62013-01-24 11:01:33 -08002751 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002752
Tejun Heoe1594892011-01-09 23:32:15 +01002753 /*
2754 * If @max_active is 1 or rescuer is in use, flushing another work
2755 * item on the same workqueue may lead to deadlock. Make sure the
2756 * flusher is not running on the same workqueue by verifying write
2757 * access.
2758 */
Tejun Heo112202d2013-02-13 19:29:12 -08002759 if (pwq->wq->saved_max_active == 1 || pwq->wq->flags & WQ_RESCUER)
2760 lock_map_acquire(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002761 else
Tejun Heo112202d2013-02-13 19:29:12 -08002762 lock_map_acquire_read(&pwq->wq->lockdep_map);
2763 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002764
Tejun Heobaf59022010-09-16 10:42:16 +02002765 return true;
2766already_gone:
Tejun Heod565ed62013-01-24 11:01:33 -08002767 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002768 return false;
2769}
2770
Oleg Nesterovdb700892008-07-25 01:47:49 -07002771/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002772 * flush_work - wait for a work to finish executing the last queueing instance
2773 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002774 *
Tejun Heo606a5022012-08-20 14:51:23 -07002775 * Wait until @work has finished execution. @work is guaranteed to be idle
2776 * on return if it hasn't been requeued since flush started.
Tejun Heo401a8d02010-09-16 10:36:00 +02002777 *
2778 * RETURNS:
2779 * %true if flush_work() waited for the work to finish execution,
2780 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002781 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002782bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002783{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002784 struct wq_barrier barr;
2785
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002786 lock_map_acquire(&work->lockdep_map);
2787 lock_map_release(&work->lockdep_map);
2788
Tejun Heo606a5022012-08-20 14:51:23 -07002789 if (start_flush_work(work, &barr)) {
Tejun Heobaf59022010-09-16 10:42:16 +02002790 wait_for_completion(&barr.done);
2791 destroy_work_on_stack(&barr.work);
2792 return true;
Tejun Heo606a5022012-08-20 14:51:23 -07002793 } else {
Tejun Heobaf59022010-09-16 10:42:16 +02002794 return false;
Tejun Heo606a5022012-08-20 14:51:23 -07002795 }
Oleg Nesterovdb700892008-07-25 01:47:49 -07002796}
2797EXPORT_SYMBOL_GPL(flush_work);
2798
Tejun Heo36e227d2012-08-03 10:30:46 -07002799static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
Tejun Heo401a8d02010-09-16 10:36:00 +02002800{
Tejun Heobbb68df2012-08-03 10:30:46 -07002801 unsigned long flags;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002802 int ret;
2803
2804 do {
Tejun Heobbb68df2012-08-03 10:30:46 -07002805 ret = try_to_grab_pending(work, is_dwork, &flags);
2806 /*
2807 * If someone else is canceling, wait for the same event it
2808 * would be waiting for before retrying.
2809 */
2810 if (unlikely(ret == -ENOENT))
Tejun Heo606a5022012-08-20 14:51:23 -07002811 flush_work(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002812 } while (unlikely(ret < 0));
2813
Tejun Heobbb68df2012-08-03 10:30:46 -07002814 /* tell other tasks trying to grab @work to back off */
2815 mark_work_canceling(work);
2816 local_irq_restore(flags);
2817
Tejun Heo606a5022012-08-20 14:51:23 -07002818 flush_work(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002819 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002820 return ret;
2821}
2822
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002823/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002824 * cancel_work_sync - cancel a work and wait for it to finish
2825 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002826 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002827 * Cancel @work and wait for its execution to finish. This function
2828 * can be used even if the work re-queues itself or migrates to
2829 * another workqueue. On return from this function, @work is
2830 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002831 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002832 * cancel_work_sync(&delayed_work->work) must not be used for
2833 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002834 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002835 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002836 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002837 *
2838 * RETURNS:
2839 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002840 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002841bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002842{
Tejun Heo36e227d2012-08-03 10:30:46 -07002843 return __cancel_work_timer(work, false);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002844}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002845EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002846
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002847/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002848 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2849 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002850 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002851 * Delayed timer is cancelled and the pending work is queued for
2852 * immediate execution. Like flush_work(), this function only
2853 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002854 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002855 * RETURNS:
2856 * %true if flush_work() waited for the work to finish execution,
2857 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002858 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002859bool flush_delayed_work(struct delayed_work *dwork)
2860{
Tejun Heo8930cab2012-08-03 10:30:45 -07002861 local_irq_disable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002862 if (del_timer_sync(&dwork->timer))
Lai Jiangshan60c057b2013-02-06 18:04:53 -08002863 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Tejun Heo8930cab2012-08-03 10:30:45 -07002864 local_irq_enable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002865 return flush_work(&dwork->work);
2866}
2867EXPORT_SYMBOL(flush_delayed_work);
2868
2869/**
Tejun Heo57b30ae2012-08-21 13:18:24 -07002870 * cancel_delayed_work - cancel a delayed work
2871 * @dwork: delayed_work to cancel
Tejun Heo09383492010-09-16 10:48:29 +02002872 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002873 * Kill off a pending delayed_work. Returns %true if @dwork was pending
2874 * and canceled; %false if wasn't pending. Note that the work callback
2875 * function may still be running on return, unless it returns %true and the
2876 * work doesn't re-arm itself. Explicitly flush or use
2877 * cancel_delayed_work_sync() to wait on it.
Tejun Heo09383492010-09-16 10:48:29 +02002878 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002879 * This function is safe to call from any context including IRQ handler.
Tejun Heo09383492010-09-16 10:48:29 +02002880 */
Tejun Heo57b30ae2012-08-21 13:18:24 -07002881bool cancel_delayed_work(struct delayed_work *dwork)
Tejun Heo09383492010-09-16 10:48:29 +02002882{
Tejun Heo57b30ae2012-08-21 13:18:24 -07002883 unsigned long flags;
2884 int ret;
2885
2886 do {
2887 ret = try_to_grab_pending(&dwork->work, true, &flags);
2888 } while (unlikely(ret == -EAGAIN));
2889
2890 if (unlikely(ret < 0))
2891 return false;
2892
Tejun Heo7c3eed52013-01-24 11:01:33 -08002893 set_work_pool_and_clear_pending(&dwork->work,
2894 get_work_pool_id(&dwork->work));
Tejun Heo57b30ae2012-08-21 13:18:24 -07002895 local_irq_restore(flags);
Dan Magenheimerc0158ca2012-10-18 16:31:37 -07002896 return ret;
Tejun Heo09383492010-09-16 10:48:29 +02002897}
Tejun Heo57b30ae2012-08-21 13:18:24 -07002898EXPORT_SYMBOL(cancel_delayed_work);
Tejun Heo09383492010-09-16 10:48:29 +02002899
2900/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002901 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2902 * @dwork: the delayed work cancel
2903 *
2904 * This is cancel_work_sync() for delayed works.
2905 *
2906 * RETURNS:
2907 * %true if @dwork was pending, %false otherwise.
2908 */
2909bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002910{
Tejun Heo36e227d2012-08-03 10:30:46 -07002911 return __cancel_work_timer(&dwork->work, true);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002912}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002913EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002915/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07002916 * schedule_work_on - put work task on a specific cpu
2917 * @cpu: cpu to put the work task on
2918 * @work: job to be done
2919 *
2920 * This puts a job on a specific cpu
2921 */
Tejun Heod4283e92012-08-03 10:30:44 -07002922bool schedule_work_on(int cpu, struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07002923{
Tejun Heod320c032010-06-29 10:07:14 +02002924 return queue_work_on(cpu, system_wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07002925}
2926EXPORT_SYMBOL(schedule_work_on);
2927
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002928/**
Dave Jonesae90dd52006-06-30 01:40:45 -04002929 * schedule_work - put work task in global workqueue
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 * @work: job to be done
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 *
Tejun Heod4283e92012-08-03 10:30:44 -07002932 * Returns %false if @work was already on the kernel-global workqueue and
2933 * %true otherwise.
David Howells52bad642006-11-22 14:54:01 +00002934 *
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002935 * This puts a job in the kernel-global workqueue if it was not already
2936 * queued and leaves it in the same position on the kernel-global
2937 * workqueue otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 */
Tejun Heod4283e92012-08-03 10:30:44 -07002939bool schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940{
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002941 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942}
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002943EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002945/**
2946 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
2947 * @cpu: cpu to use
2948 * @dwork: job to be done
2949 * @delay: number of jiffies to wait
2950 *
2951 * After waiting for a given time this puts a job in the kernel-global
2952 * workqueue on the specified CPU.
2953 */
Tejun Heod4283e92012-08-03 10:30:44 -07002954bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork,
2955 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956{
Tejun Heod320c032010-06-29 10:07:14 +02002957 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958}
Dave Jonesae90dd52006-06-30 01:40:45 -04002959EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960
Andrew Mortonb6136772006-06-25 05:47:49 -07002961/**
Tejun Heo0a13c002012-08-03 10:30:44 -07002962 * schedule_delayed_work - put work task in global workqueue after delay
2963 * @dwork: job to be done
2964 * @delay: number of jiffies to wait or 0 for immediate execution
2965 *
2966 * After waiting for a given time this puts a job in the kernel-global
2967 * workqueue.
2968 */
Tejun Heod4283e92012-08-03 10:30:44 -07002969bool schedule_delayed_work(struct delayed_work *dwork, unsigned long delay)
Tejun Heo0a13c002012-08-03 10:30:44 -07002970{
2971 return queue_delayed_work(system_wq, dwork, delay);
2972}
2973EXPORT_SYMBOL(schedule_delayed_work);
2974
2975/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002976 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002977 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002978 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002979 * schedule_on_each_cpu() executes @func on each online CPU using the
2980 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002981 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002982 *
2983 * RETURNS:
2984 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002985 */
David Howells65f27f32006-11-22 14:55:48 +00002986int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002987{
2988 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002989 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002990
Andrew Mortonb6136772006-06-25 05:47:49 -07002991 works = alloc_percpu(struct work_struct);
2992 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002993 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002994
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002995 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002996
Christoph Lameter15316ba2006-01-08 01:00:43 -08002997 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002998 struct work_struct *work = per_cpu_ptr(works, cpu);
2999
3000 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003001 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02003002 }
Tejun Heo93981802009-11-17 14:06:20 -08003003
3004 for_each_online_cpu(cpu)
3005 flush_work(per_cpu_ptr(works, cpu));
3006
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003007 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07003008 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08003009 return 0;
3010}
3011
Alan Sterneef6a7d2010-02-12 17:39:21 +09003012/**
3013 * flush_scheduled_work - ensure that any scheduled work has run to completion.
3014 *
3015 * Forces execution of the kernel-global workqueue and blocks until its
3016 * completion.
3017 *
3018 * Think twice before calling this function! It's very easy to get into
3019 * trouble if you don't take great care. Either of the following situations
3020 * will lead to deadlock:
3021 *
3022 * One of the work items currently on the workqueue needs to acquire
3023 * a lock held by your code or its caller.
3024 *
3025 * Your code is running in the context of a work routine.
3026 *
3027 * They will be detected by lockdep when they occur, but the first might not
3028 * occur very often. It depends on what work items are on the workqueue and
3029 * what locks they need, which you have no control over.
3030 *
3031 * In most situations flushing the entire workqueue is overkill; you merely
3032 * need to know that a particular work item isn't queued and isn't running.
3033 * In such cases you should use cancel_delayed_work_sync() or
3034 * cancel_work_sync() instead.
3035 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036void flush_scheduled_work(void)
3037{
Tejun Heod320c032010-06-29 10:07:14 +02003038 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039}
Dave Jonesae90dd52006-06-30 01:40:45 -04003040EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
3042/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06003043 * execute_in_process_context - reliably execute the routine with user context
3044 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06003045 * @ew: guaranteed storage for the execute work structure (must
3046 * be available when the work executes)
3047 *
3048 * Executes the function immediately if process context is available,
3049 * otherwise schedules the function for delayed execution.
3050 *
3051 * Returns: 0 - function was executed
3052 * 1 - function was scheduled for execution
3053 */
David Howells65f27f32006-11-22 14:55:48 +00003054int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06003055{
3056 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00003057 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003058 return 0;
3059 }
3060
David Howells65f27f32006-11-22 14:55:48 +00003061 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003062 schedule_work(&ew->work);
3063
3064 return 1;
3065}
3066EXPORT_SYMBOL_GPL(execute_in_process_context);
3067
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068int keventd_up(void)
3069{
Tejun Heod320c032010-06-29 10:07:14 +02003070 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071}
3072
Tejun Heo30cdf242013-03-12 11:29:57 -07003073static int alloc_and_link_pwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003075 bool highpri = wq->flags & WQ_HIGHPRI;
Tejun Heo30cdf242013-03-12 11:29:57 -07003076 int cpu;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01003077
Tejun Heo30cdf242013-03-12 11:29:57 -07003078 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo420c0dd2013-03-12 11:29:59 -07003079 wq->cpu_pwqs = alloc_percpu(struct pool_workqueue);
3080 if (!wq->cpu_pwqs)
Tejun Heo30cdf242013-03-12 11:29:57 -07003081 return -ENOMEM;
3082
3083 for_each_possible_cpu(cpu) {
Tejun Heo7fb98ea2013-03-12 11:30:00 -07003084 struct pool_workqueue *pwq =
3085 per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heo30cdf242013-03-12 11:29:57 -07003086
Tejun Heo49e3cf42013-03-12 11:29:58 -07003087 pwq->pool = get_std_worker_pool(cpu, highpri);
Tejun Heo30cdf242013-03-12 11:29:57 -07003088 list_add_tail(&pwq->pwqs_node, &wq->pwqs);
3089 }
3090 } else {
3091 struct pool_workqueue *pwq;
3092
3093 pwq = kmem_cache_zalloc(pwq_cache, GFP_KERNEL);
3094 if (!pwq)
3095 return -ENOMEM;
3096
Tejun Heo49e3cf42013-03-12 11:29:58 -07003097 pwq->pool = get_std_worker_pool(WORK_CPU_UNBOUND, highpri);
Tejun Heo30cdf242013-03-12 11:29:57 -07003098 list_add_tail(&pwq->pwqs_node, &wq->pwqs);
3099 }
3100
3101 return 0;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003102}
3103
Tejun Heo112202d2013-02-13 19:29:12 -08003104static void free_pwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07003105{
Lai Jiangshane06ffa12012-03-09 18:03:20 +08003106 if (!(wq->flags & WQ_UNBOUND))
Tejun Heo420c0dd2013-03-12 11:29:59 -07003107 free_percpu(wq->cpu_pwqs);
3108 else if (!list_empty(&wq->pwqs))
3109 kmem_cache_free(pwq_cache, list_first_entry(&wq->pwqs,
3110 struct pool_workqueue, pwqs_node));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07003111}
3112
Tejun Heof3421792010-07-02 10:03:51 +02003113static int wq_clamp_max_active(int max_active, unsigned int flags,
3114 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003115{
Tejun Heof3421792010-07-02 10:03:51 +02003116 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3117
3118 if (max_active < 1 || max_active > lim)
Valentin Ilie044c7822012-08-19 00:52:42 +03003119 pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
3120 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003121
Tejun Heof3421792010-07-02 10:03:51 +02003122 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003123}
3124
Tejun Heob196be82012-01-10 15:11:35 -08003125struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02003126 unsigned int flags,
3127 int max_active,
3128 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08003129 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003130{
Tejun Heob196be82012-01-10 15:11:35 -08003131 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003132 struct workqueue_struct *wq;
Tejun Heo49e3cf42013-03-12 11:29:58 -07003133 struct pool_workqueue *pwq;
Tejun Heob196be82012-01-10 15:11:35 -08003134 size_t namelen;
3135
3136 /* determine namelen, allocate wq and format name */
3137 va_start(args, lock_name);
3138 va_copy(args1, args);
3139 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3140
3141 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3142 if (!wq)
3143 goto err;
3144
3145 vsnprintf(wq->name, namelen, fmt, args1);
3146 va_end(args);
3147 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003148
Tejun Heof3421792010-07-02 10:03:51 +02003149 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02003150 * Workqueues which may be used during memory reclaim should
3151 * have a rescuer to guarantee forward progress.
3152 */
3153 if (flags & WQ_MEM_RECLAIM)
3154 flags |= WQ_RESCUER;
3155
Tejun Heod320c032010-06-29 10:07:14 +02003156 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003157 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003158
Tejun Heob196be82012-01-10 15:11:35 -08003159 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003160 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003161 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003162 mutex_init(&wq->flush_mutex);
Tejun Heo112202d2013-02-13 19:29:12 -08003163 atomic_set(&wq->nr_pwqs_to_flush, 0);
Tejun Heo30cdf242013-03-12 11:29:57 -07003164 INIT_LIST_HEAD(&wq->pwqs);
Tejun Heo73f53c42010-06-29 10:07:11 +02003165 INIT_LIST_HEAD(&wq->flusher_queue);
3166 INIT_LIST_HEAD(&wq->flusher_overflow);
Tejun Heo493a1722013-03-12 11:29:59 -07003167 INIT_LIST_HEAD(&wq->maydays);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003168
Johannes Bergeb13ba82008-01-16 09:51:58 +01003169 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003170 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003171
Tejun Heo30cdf242013-03-12 11:29:57 -07003172 if (alloc_and_link_pwqs(wq) < 0)
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003173 goto err;
3174
Tejun Heo49e3cf42013-03-12 11:29:58 -07003175 for_each_pwq(pwq, wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08003176 BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK);
Tejun Heo112202d2013-02-13 19:29:12 -08003177 pwq->wq = wq;
3178 pwq->flush_color = -1;
3179 pwq->max_active = max_active;
3180 INIT_LIST_HEAD(&pwq->delayed_works);
Tejun Heo493a1722013-03-12 11:29:59 -07003181 INIT_LIST_HEAD(&pwq->mayday_node);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003182 }
3183
Tejun Heoe22bee72010-06-29 10:07:14 +02003184 if (flags & WQ_RESCUER) {
3185 struct worker *rescuer;
3186
Tejun Heoe22bee72010-06-29 10:07:14 +02003187 wq->rescuer = rescuer = alloc_worker();
3188 if (!rescuer)
3189 goto err;
3190
Tejun Heo111c2252013-01-17 17:16:24 -08003191 rescuer->rescue_wq = wq;
3192 rescuer->task = kthread_create(rescuer_thread, rescuer, "%s",
Tejun Heob196be82012-01-10 15:11:35 -08003193 wq->name);
Tejun Heoe22bee72010-06-29 10:07:14 +02003194 if (IS_ERR(rescuer->task))
3195 goto err;
3196
Tejun Heoe22bee72010-06-29 10:07:14 +02003197 rescuer->task->flags |= PF_THREAD_BOUND;
3198 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003199 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003200
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003201 /*
3202 * workqueue_lock protects global freeze state and workqueues
3203 * list. Grab it, set max_active accordingly and add the new
3204 * workqueue to workqueues list.
3205 */
Tejun Heoe98d5b12013-03-12 11:29:57 -07003206 spin_lock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003207
Tejun Heo58a69cb2011-02-16 09:25:31 +01003208 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heo49e3cf42013-03-12 11:29:58 -07003209 for_each_pwq(pwq, wq)
3210 pwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003211
Tejun Heo15376632010-06-29 10:07:11 +02003212 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003213
Tejun Heoe98d5b12013-03-12 11:29:57 -07003214 spin_unlock_irq(&workqueue_lock);
Tejun Heo15376632010-06-29 10:07:11 +02003215
Oleg Nesterov3af244332007-05-09 02:34:09 -07003216 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02003217err:
3218 if (wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08003219 free_pwqs(wq);
Tejun Heoe22bee72010-06-29 10:07:14 +02003220 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003221 kfree(wq);
3222 }
3223 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003224}
Tejun Heod320c032010-06-29 10:07:14 +02003225EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003226
3227/**
3228 * destroy_workqueue - safely terminate a workqueue
3229 * @wq: target workqueue
3230 *
3231 * Safely destroy a workqueue. All work currently pending will be done first.
3232 */
3233void destroy_workqueue(struct workqueue_struct *wq)
3234{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003235 struct pool_workqueue *pwq;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003236
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003237 /* drain it before proceeding with destruction */
3238 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003239
Tejun Heo6183c002013-03-12 11:29:57 -07003240 /* sanity checks */
Tejun Heo49e3cf42013-03-12 11:29:58 -07003241 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07003242 int i;
3243
3244 for (i = 0; i < WORK_NR_COLORS; i++)
3245 if (WARN_ON(pwq->nr_in_flight[i]))
3246 return;
3247 if (WARN_ON(pwq->nr_active) ||
3248 WARN_ON(!list_empty(&pwq->delayed_works)))
3249 return;
3250 }
3251
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003252 /*
3253 * wq list is used to freeze wq, remove from list after
3254 * flushing is complete in case freeze races us.
3255 */
Tejun Heoe98d5b12013-03-12 11:29:57 -07003256 spin_lock_irq(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07003257 list_del(&wq->list);
Tejun Heoe98d5b12013-03-12 11:29:57 -07003258 spin_unlock_irq(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003259
Tejun Heoe22bee72010-06-29 10:07:14 +02003260 if (wq->flags & WQ_RESCUER) {
3261 kthread_stop(wq->rescuer->task);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003262 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003263 }
3264
Tejun Heo112202d2013-02-13 19:29:12 -08003265 free_pwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003266 kfree(wq);
3267}
3268EXPORT_SYMBOL_GPL(destroy_workqueue);
3269
Tejun Heodcd989c2010-06-29 10:07:14 +02003270/**
Tejun Heo112202d2013-02-13 19:29:12 -08003271 * pwq_set_max_active - adjust max_active of a pwq
3272 * @pwq: target pool_workqueue
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003273 * @max_active: new max_active value.
3274 *
Tejun Heo112202d2013-02-13 19:29:12 -08003275 * Set @pwq->max_active to @max_active and activate delayed works if
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003276 * increased.
3277 *
3278 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08003279 * spin_lock_irq(pool->lock).
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003280 */
Tejun Heo112202d2013-02-13 19:29:12 -08003281static void pwq_set_max_active(struct pool_workqueue *pwq, int max_active)
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003282{
Tejun Heo112202d2013-02-13 19:29:12 -08003283 pwq->max_active = max_active;
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003284
Tejun Heo112202d2013-02-13 19:29:12 -08003285 while (!list_empty(&pwq->delayed_works) &&
3286 pwq->nr_active < pwq->max_active)
3287 pwq_activate_first_delayed(pwq);
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003288}
3289
3290/**
Tejun Heodcd989c2010-06-29 10:07:14 +02003291 * workqueue_set_max_active - adjust max_active of a workqueue
3292 * @wq: target workqueue
3293 * @max_active: new max_active value.
3294 *
3295 * Set max_active of @wq to @max_active.
3296 *
3297 * CONTEXT:
3298 * Don't call from IRQ context.
3299 */
3300void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3301{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003302 struct pool_workqueue *pwq;
Tejun Heodcd989c2010-06-29 10:07:14 +02003303
Tejun Heof3421792010-07-02 10:03:51 +02003304 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003305
Tejun Heoe98d5b12013-03-12 11:29:57 -07003306 spin_lock_irq(&workqueue_lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02003307
3308 wq->saved_max_active = max_active;
3309
Tejun Heo49e3cf42013-03-12 11:29:58 -07003310 for_each_pwq(pwq, wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08003311 struct worker_pool *pool = pwq->pool;
Tejun Heodcd989c2010-06-29 10:07:14 +02003312
Tejun Heoe98d5b12013-03-12 11:29:57 -07003313 spin_lock(&pool->lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02003314
Tejun Heo58a69cb2011-02-16 09:25:31 +01003315 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heo35b6bb62013-01-24 11:01:33 -08003316 !(pool->flags & POOL_FREEZING))
Tejun Heo112202d2013-02-13 19:29:12 -08003317 pwq_set_max_active(pwq, max_active);
Tejun Heodcd989c2010-06-29 10:07:14 +02003318
Tejun Heoe98d5b12013-03-12 11:29:57 -07003319 spin_unlock(&pool->lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02003320 }
3321
Tejun Heoe98d5b12013-03-12 11:29:57 -07003322 spin_unlock_irq(&workqueue_lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02003323}
3324EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3325
3326/**
3327 * workqueue_congested - test whether a workqueue is congested
3328 * @cpu: CPU in question
3329 * @wq: target workqueue
3330 *
3331 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3332 * no synchronization around this function and the test result is
3333 * unreliable and only useful as advisory hints or for debugging.
3334 *
3335 * RETURNS:
3336 * %true if congested, %false otherwise.
3337 */
Tejun Heod84ff052013-03-12 11:29:59 -07003338bool workqueue_congested(int cpu, struct workqueue_struct *wq)
Tejun Heodcd989c2010-06-29 10:07:14 +02003339{
Tejun Heo7fb98ea2013-03-12 11:30:00 -07003340 struct pool_workqueue *pwq;
3341
3342 if (!(wq->flags & WQ_UNBOUND))
3343 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
3344 else
3345 pwq = first_pwq(wq);
Tejun Heodcd989c2010-06-29 10:07:14 +02003346
Tejun Heo112202d2013-02-13 19:29:12 -08003347 return !list_empty(&pwq->delayed_works);
Tejun Heodcd989c2010-06-29 10:07:14 +02003348}
3349EXPORT_SYMBOL_GPL(workqueue_congested);
3350
3351/**
Tejun Heodcd989c2010-06-29 10:07:14 +02003352 * work_busy - test whether a work is currently pending or running
3353 * @work: the work to be tested
3354 *
3355 * Test whether @work is currently pending or running. There is no
3356 * synchronization around this function and the test result is
3357 * unreliable and only useful as advisory hints or for debugging.
Tejun Heodcd989c2010-06-29 10:07:14 +02003358 *
3359 * RETURNS:
3360 * OR'd bitmask of WORK_BUSY_* bits.
3361 */
3362unsigned int work_busy(struct work_struct *work)
3363{
Tejun Heoc9e7cf22013-01-24 11:01:33 -08003364 struct worker_pool *pool = get_work_pool(work);
Tejun Heodcd989c2010-06-29 10:07:14 +02003365 unsigned long flags;
3366 unsigned int ret = 0;
3367
Tejun Heodcd989c2010-06-29 10:07:14 +02003368 if (work_pending(work))
3369 ret |= WORK_BUSY_PENDING;
Tejun Heodcd989c2010-06-29 10:07:14 +02003370
Lai Jiangshan038366c2013-02-06 18:04:53 -08003371 if (pool) {
3372 spin_lock_irqsave(&pool->lock, flags);
3373 if (find_worker_executing_work(pool, work))
3374 ret |= WORK_BUSY_RUNNING;
3375 spin_unlock_irqrestore(&pool->lock, flags);
3376 }
Tejun Heodcd989c2010-06-29 10:07:14 +02003377
3378 return ret;
3379}
3380EXPORT_SYMBOL_GPL(work_busy);
3381
Tejun Heodb7bccf2010-06-29 10:07:12 +02003382/*
3383 * CPU hotplug.
3384 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003385 * There are two challenges in supporting CPU hotplug. Firstly, there
Tejun Heo112202d2013-02-13 19:29:12 -08003386 * are a lot of assumptions on strong associations among work, pwq and
Tejun Heo706026c2013-01-24 11:01:34 -08003387 * pool which make migrating pending and scheduled works very
Tejun Heoe22bee72010-06-29 10:07:14 +02003388 * difficult to implement without impacting hot paths. Secondly,
Tejun Heo94cf58b2013-01-24 11:01:33 -08003389 * worker pools serve mix of short, long and very long running works making
Tejun Heoe22bee72010-06-29 10:07:14 +02003390 * blocked draining impractical.
3391 *
Tejun Heo24647572013-01-24 11:01:33 -08003392 * This is solved by allowing the pools to be disassociated from the CPU
Tejun Heo628c78e2012-07-17 12:39:27 -07003393 * running as an unbound one and allowing it to be reattached later if the
3394 * cpu comes back online.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003395 */
3396
Tejun Heo706026c2013-01-24 11:01:34 -08003397static void wq_unbind_fn(struct work_struct *work)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003398{
Tejun Heo38db41d2013-01-24 11:01:34 -08003399 int cpu = smp_processor_id();
Tejun Heo4ce62e92012-07-13 22:16:44 -07003400 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003401 struct worker *worker;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003402 int i;
3403
Tejun Heo38db41d2013-01-24 11:01:34 -08003404 for_each_std_worker_pool(pool, cpu) {
Tejun Heo6183c002013-03-12 11:29:57 -07003405 WARN_ON_ONCE(cpu != smp_processor_id());
Tejun Heo94cf58b2013-01-24 11:01:33 -08003406
3407 mutex_lock(&pool->assoc_mutex);
3408 spin_lock_irq(&pool->lock);
3409
3410 /*
3411 * We've claimed all manager positions. Make all workers
3412 * unbound and set DISASSOCIATED. Before this, all workers
3413 * except for the ones which are still executing works from
3414 * before the last CPU down must be on the cpu. After
3415 * this, they may become diasporas.
3416 */
Tejun Heo4ce62e92012-07-13 22:16:44 -07003417 list_for_each_entry(worker, &pool->idle_list, entry)
Tejun Heo403c8212012-07-17 12:39:27 -07003418 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003419
Sasha Levinb67bfe02013-02-27 17:06:00 -08003420 for_each_busy_worker(worker, i, pool)
Tejun Heoc9e7cf22013-01-24 11:01:33 -08003421 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003422
Tejun Heo24647572013-01-24 11:01:33 -08003423 pool->flags |= POOL_DISASSOCIATED;
Tejun Heof2d5a0e2012-07-17 12:39:26 -07003424
Tejun Heo94cf58b2013-01-24 11:01:33 -08003425 spin_unlock_irq(&pool->lock);
3426 mutex_unlock(&pool->assoc_mutex);
3427 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003428
3429 /*
Tejun Heo628c78e2012-07-17 12:39:27 -07003430 * Call schedule() so that we cross rq->lock and thus can guarantee
3431 * sched callbacks see the %WORKER_UNBOUND flag. This is necessary
3432 * as scheduler callbacks may be invoked from other cpus.
3433 */
3434 schedule();
3435
3436 /*
3437 * Sched callbacks are disabled now. Zap nr_running. After this,
3438 * nr_running stays zero and need_more_worker() and keep_working()
Tejun Heo38db41d2013-01-24 11:01:34 -08003439 * are always true as long as the worklist is not empty. Pools on
3440 * @cpu now behave as unbound (in terms of concurrency management)
3441 * pools which are served by workers tied to the CPU.
Tejun Heo628c78e2012-07-17 12:39:27 -07003442 *
3443 * On return from this function, the current worker would trigger
3444 * unbound chain execution of pending work items if other workers
3445 * didn't already.
Tejun Heoe22bee72010-06-29 10:07:14 +02003446 */
Tejun Heo38db41d2013-01-24 11:01:34 -08003447 for_each_std_worker_pool(pool, cpu)
Tejun Heoe19e3972013-01-24 11:39:44 -08003448 atomic_set(&pool->nr_running, 0);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003449}
3450
Tejun Heo8db25e72012-07-17 12:39:28 -07003451/*
3452 * Workqueues should be brought up before normal priority CPU notifiers.
3453 * This will be registered high priority CPU notifier.
3454 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07003455static int __cpuinit workqueue_cpu_up_callback(struct notifier_block *nfb,
Tejun Heo8db25e72012-07-17 12:39:28 -07003456 unsigned long action,
3457 void *hcpu)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003458{
Tejun Heod84ff052013-03-12 11:29:59 -07003459 int cpu = (unsigned long)hcpu;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003460 struct worker_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461
Tejun Heo8db25e72012-07-17 12:39:28 -07003462 switch (action & ~CPU_TASKS_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 case CPU_UP_PREPARE:
Tejun Heo38db41d2013-01-24 11:01:34 -08003464 for_each_std_worker_pool(pool, cpu) {
Tejun Heo3ce63372012-07-17 12:39:27 -07003465 struct worker *worker;
3466
3467 if (pool->nr_workers)
3468 continue;
3469
3470 worker = create_worker(pool);
3471 if (!worker)
3472 return NOTIFY_BAD;
3473
Tejun Heod565ed62013-01-24 11:01:33 -08003474 spin_lock_irq(&pool->lock);
Tejun Heo3ce63372012-07-17 12:39:27 -07003475 start_worker(worker);
Tejun Heod565ed62013-01-24 11:01:33 -08003476 spin_unlock_irq(&pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003478 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003479
Tejun Heo65758202012-07-17 12:39:26 -07003480 case CPU_DOWN_FAILED:
3481 case CPU_ONLINE:
Tejun Heo38db41d2013-01-24 11:01:34 -08003482 for_each_std_worker_pool(pool, cpu) {
Tejun Heo94cf58b2013-01-24 11:01:33 -08003483 mutex_lock(&pool->assoc_mutex);
3484 spin_lock_irq(&pool->lock);
3485
Tejun Heo24647572013-01-24 11:01:33 -08003486 pool->flags &= ~POOL_DISASSOCIATED;
Tejun Heo94cf58b2013-01-24 11:01:33 -08003487 rebind_workers(pool);
3488
3489 spin_unlock_irq(&pool->lock);
3490 mutex_unlock(&pool->assoc_mutex);
3491 }
Tejun Heo8db25e72012-07-17 12:39:28 -07003492 break;
Tejun Heo65758202012-07-17 12:39:26 -07003493 }
3494 return NOTIFY_OK;
3495}
3496
3497/*
3498 * Workqueues should be brought down after normal priority CPU notifiers.
3499 * This will be registered as low priority CPU notifier.
3500 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07003501static int __cpuinit workqueue_cpu_down_callback(struct notifier_block *nfb,
Tejun Heo65758202012-07-17 12:39:26 -07003502 unsigned long action,
3503 void *hcpu)
3504{
Tejun Heod84ff052013-03-12 11:29:59 -07003505 int cpu = (unsigned long)hcpu;
Tejun Heo8db25e72012-07-17 12:39:28 -07003506 struct work_struct unbind_work;
3507
Tejun Heo65758202012-07-17 12:39:26 -07003508 switch (action & ~CPU_TASKS_FROZEN) {
3509 case CPU_DOWN_PREPARE:
Tejun Heo8db25e72012-07-17 12:39:28 -07003510 /* unbinding should happen on the local CPU */
Tejun Heo706026c2013-01-24 11:01:34 -08003511 INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn);
Joonsoo Kim7635d2f2012-08-15 23:25:41 +09003512 queue_work_on(cpu, system_highpri_wq, &unbind_work);
Tejun Heo8db25e72012-07-17 12:39:28 -07003513 flush_work(&unbind_work);
3514 break;
Tejun Heo65758202012-07-17 12:39:26 -07003515 }
3516 return NOTIFY_OK;
3517}
3518
Rusty Russell2d3854a2008-11-05 13:39:10 +11003519#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003520
Rusty Russell2d3854a2008-11-05 13:39:10 +11003521struct work_for_cpu {
Tejun Heoed48ece2012-09-18 12:48:43 -07003522 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003523 long (*fn)(void *);
3524 void *arg;
3525 long ret;
3526};
3527
Tejun Heoed48ece2012-09-18 12:48:43 -07003528static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003529{
Tejun Heoed48ece2012-09-18 12:48:43 -07003530 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
3531
Rusty Russell2d3854a2008-11-05 13:39:10 +11003532 wfc->ret = wfc->fn(wfc->arg);
3533}
3534
3535/**
3536 * work_on_cpu - run a function in user context on a particular cpu
3537 * @cpu: the cpu to run on
3538 * @fn: the function to run
3539 * @arg: the function arg
3540 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003541 * This will return the value @fn returns.
3542 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06003543 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003544 */
Tejun Heod84ff052013-03-12 11:29:59 -07003545long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003546{
Tejun Heoed48ece2012-09-18 12:48:43 -07003547 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003548
Tejun Heoed48ece2012-09-18 12:48:43 -07003549 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
3550 schedule_work_on(cpu, &wfc.work);
3551 flush_work(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003552 return wfc.ret;
3553}
3554EXPORT_SYMBOL_GPL(work_on_cpu);
3555#endif /* CONFIG_SMP */
3556
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003557#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303558
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003559/**
3560 * freeze_workqueues_begin - begin freezing workqueues
3561 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003562 * Start freezing workqueues. After this function returns, all freezable
3563 * workqueues will queue new works to their frozen_works list instead of
Tejun Heo706026c2013-01-24 11:01:34 -08003564 * pool->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003565 *
3566 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08003567 * Grabs and releases workqueue_lock and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003568 */
3569void freeze_workqueues_begin(void)
3570{
Tejun Heo17116962013-03-12 11:29:58 -07003571 struct worker_pool *pool;
Tejun Heo24b8a842013-03-12 11:29:58 -07003572 struct workqueue_struct *wq;
3573 struct pool_workqueue *pwq;
Tejun Heo17116962013-03-12 11:29:58 -07003574 int id;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003575
Tejun Heoe98d5b12013-03-12 11:29:57 -07003576 spin_lock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003577
Tejun Heo6183c002013-03-12 11:29:57 -07003578 WARN_ON_ONCE(workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003579 workqueue_freezing = true;
3580
Tejun Heo24b8a842013-03-12 11:29:58 -07003581 /* set FREEZING */
Tejun Heo17116962013-03-12 11:29:58 -07003582 for_each_pool(pool, id) {
Tejun Heo17116962013-03-12 11:29:58 -07003583 spin_lock(&pool->lock);
Tejun Heo17116962013-03-12 11:29:58 -07003584 WARN_ON_ONCE(pool->flags & POOL_FREEZING);
3585 pool->flags |= POOL_FREEZING;
Tejun Heo17116962013-03-12 11:29:58 -07003586 spin_unlock(&pool->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003587 }
3588
Tejun Heo24b8a842013-03-12 11:29:58 -07003589 /* suppress further executions by setting max_active to zero */
3590 list_for_each_entry(wq, &workqueues, list) {
3591 if (!(wq->flags & WQ_FREEZABLE))
3592 continue;
3593
3594 for_each_pwq(pwq, wq) {
3595 spin_lock(&pwq->pool->lock);
3596 pwq->max_active = 0;
3597 spin_unlock(&pwq->pool->lock);
3598 }
3599 }
3600
Tejun Heoe98d5b12013-03-12 11:29:57 -07003601 spin_unlock_irq(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003603
3604/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003605 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003606 *
3607 * Check whether freezing is complete. This function must be called
3608 * between freeze_workqueues_begin() and thaw_workqueues().
3609 *
3610 * CONTEXT:
3611 * Grabs and releases workqueue_lock.
3612 *
3613 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01003614 * %true if some freezable workqueues are still busy. %false if freezing
3615 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003616 */
3617bool freeze_workqueues_busy(void)
3618{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003619 bool busy = false;
Tejun Heo24b8a842013-03-12 11:29:58 -07003620 struct workqueue_struct *wq;
3621 struct pool_workqueue *pwq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003622
Tejun Heoe98d5b12013-03-12 11:29:57 -07003623 spin_lock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003624
Tejun Heo6183c002013-03-12 11:29:57 -07003625 WARN_ON_ONCE(!workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003626
Tejun Heo24b8a842013-03-12 11:29:58 -07003627 list_for_each_entry(wq, &workqueues, list) {
3628 if (!(wq->flags & WQ_FREEZABLE))
3629 continue;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003630 /*
3631 * nr_active is monotonically decreasing. It's safe
3632 * to peek without lock.
3633 */
Tejun Heo24b8a842013-03-12 11:29:58 -07003634 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07003635 WARN_ON_ONCE(pwq->nr_active < 0);
Tejun Heo112202d2013-02-13 19:29:12 -08003636 if (pwq->nr_active) {
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003637 busy = true;
3638 goto out_unlock;
3639 }
3640 }
3641 }
3642out_unlock:
Tejun Heoe98d5b12013-03-12 11:29:57 -07003643 spin_unlock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003644 return busy;
3645}
3646
3647/**
3648 * thaw_workqueues - thaw workqueues
3649 *
3650 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo706026c2013-01-24 11:01:34 -08003651 * frozen works are transferred to their respective pool worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003652 *
3653 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08003654 * Grabs and releases workqueue_lock and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003655 */
3656void thaw_workqueues(void)
3657{
Tejun Heo24b8a842013-03-12 11:29:58 -07003658 struct workqueue_struct *wq;
3659 struct pool_workqueue *pwq;
3660 struct worker_pool *pool;
3661 int id;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003662
Tejun Heoe98d5b12013-03-12 11:29:57 -07003663 spin_lock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003664
3665 if (!workqueue_freezing)
3666 goto out_unlock;
3667
Tejun Heo24b8a842013-03-12 11:29:58 -07003668 /* clear FREEZING */
3669 for_each_pool(pool, id) {
3670 spin_lock(&pool->lock);
3671 WARN_ON_ONCE(!(pool->flags & POOL_FREEZING));
3672 pool->flags &= ~POOL_FREEZING;
3673 spin_unlock(&pool->lock);
3674 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003675
Tejun Heo24b8a842013-03-12 11:29:58 -07003676 /* restore max_active and repopulate worklist */
3677 list_for_each_entry(wq, &workqueues, list) {
3678 if (!(wq->flags & WQ_FREEZABLE))
3679 continue;
Tejun Heod565ed62013-01-24 11:01:33 -08003680
Tejun Heo24b8a842013-03-12 11:29:58 -07003681 for_each_pwq(pwq, wq) {
3682 spin_lock(&pwq->pool->lock);
3683 pwq_set_max_active(pwq, wq->saved_max_active);
3684 spin_unlock(&pwq->pool->lock);
Tejun Heod565ed62013-01-24 11:01:33 -08003685 }
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003686 }
3687
Tejun Heo24b8a842013-03-12 11:29:58 -07003688 /* kick workers */
3689 for_each_pool(pool, id) {
3690 spin_lock(&pool->lock);
3691 wake_up_worker(pool);
3692 spin_unlock(&pool->lock);
3693 }
3694
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003695 workqueue_freezing = false;
3696out_unlock:
Tejun Heoe98d5b12013-03-12 11:29:57 -07003697 spin_unlock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003698}
3699#endif /* CONFIG_FREEZER */
3700
Suresh Siddha6ee05782010-07-30 14:57:37 -07003701static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702{
Tejun Heod84ff052013-03-12 11:29:59 -07003703 int cpu;
Tejun Heoc34056a2010-06-29 10:07:11 +02003704
Tejun Heo7c3eed52013-01-24 11:01:33 -08003705 /* make sure we have enough bits for OFFQ pool ID */
3706 BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) <
Lai Jiangshan6be19582013-02-06 18:04:53 -08003707 WORK_CPU_END * NR_STD_WORKER_POOLS);
Tejun Heob5490072012-08-03 10:30:46 -07003708
Tejun Heoe904e6c2013-03-12 11:29:57 -07003709 WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long));
3710
3711 pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC);
3712
Tejun Heo65758202012-07-17 12:39:26 -07003713 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
Lai Jiangshana5b4e572012-09-18 09:59:23 -07003714 hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003715
Tejun Heo706026c2013-01-24 11:01:34 -08003716 /* initialize CPU pools */
3717 for_each_wq_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07003718 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003719
Tejun Heo38db41d2013-01-24 11:01:34 -08003720 for_each_std_worker_pool(pool, cpu) {
Tejun Heod565ed62013-01-24 11:01:33 -08003721 spin_lock_init(&pool->lock);
Tejun Heoec22ca52013-01-24 11:01:33 -08003722 pool->cpu = cpu;
Tejun Heo24647572013-01-24 11:01:33 -08003723 pool->flags |= POOL_DISASSOCIATED;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003724 INIT_LIST_HEAD(&pool->worklist);
3725 INIT_LIST_HEAD(&pool->idle_list);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08003726 hash_init(pool->busy_hash);
Tejun Heoe22bee72010-06-29 10:07:14 +02003727
Tejun Heo4ce62e92012-07-13 22:16:44 -07003728 init_timer_deferrable(&pool->idle_timer);
3729 pool->idle_timer.function = idle_worker_timeout;
3730 pool->idle_timer.data = (unsigned long)pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003731
Tejun Heo706026c2013-01-24 11:01:34 -08003732 setup_timer(&pool->mayday_timer, pool_mayday_timeout,
Tejun Heo4ce62e92012-07-13 22:16:44 -07003733 (unsigned long)pool);
3734
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07003735 mutex_init(&pool->assoc_mutex);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003736 ida_init(&pool->worker_ida);
Tejun Heo9daf9e62013-01-24 11:01:33 -08003737
3738 /* alloc pool ID */
3739 BUG_ON(worker_pool_assign_id(pool));
Tejun Heo4ce62e92012-07-13 22:16:44 -07003740 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003741 }
3742
Tejun Heoe22bee72010-06-29 10:07:14 +02003743 /* create the initial worker */
Tejun Heo706026c2013-01-24 11:01:34 -08003744 for_each_online_wq_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07003745 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003746
Tejun Heo38db41d2013-01-24 11:01:34 -08003747 for_each_std_worker_pool(pool, cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07003748 struct worker *worker;
3749
Tejun Heo24647572013-01-24 11:01:33 -08003750 if (cpu != WORK_CPU_UNBOUND)
3751 pool->flags &= ~POOL_DISASSOCIATED;
3752
Tejun Heobc2ae0f2012-07-17 12:39:27 -07003753 worker = create_worker(pool);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003754 BUG_ON(!worker);
Tejun Heod565ed62013-01-24 11:01:33 -08003755 spin_lock_irq(&pool->lock);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003756 start_worker(worker);
Tejun Heod565ed62013-01-24 11:01:33 -08003757 spin_unlock_irq(&pool->lock);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003758 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003759 }
3760
Tejun Heod320c032010-06-29 10:07:14 +02003761 system_wq = alloc_workqueue("events", 0, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09003762 system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
Tejun Heod320c032010-06-29 10:07:14 +02003763 system_long_wq = alloc_workqueue("events_long", 0, 0);
Tejun Heof3421792010-07-02 10:03:51 +02003764 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3765 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01003766 system_freezable_wq = alloc_workqueue("events_freezable",
3767 WQ_FREEZABLE, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09003768 BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
Tejun Heoae930e02012-08-20 14:51:23 -07003769 !system_unbound_wq || !system_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07003770 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771}
Suresh Siddha6ee05782010-07-30 14:57:37 -07003772early_initcall(init_workqueues);