blob: 7dd8e7bcec51f9e361de0a4a6c46ff3e911797ca [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tejun Heoc54fce62010-09-10 16:51:36 +02002 * kernel/workqueue.c - generic async execution with shared worker pool
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Tejun Heoc54fce62010-09-10 16:51:36 +02004 * Copyright (C) 2002 Ingo Molnar
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Tejun Heoc54fce62010-09-10 16:51:36 +02006 * Derived from the taskqueue/keventd code by:
7 * David Woodhouse <dwmw2@infradead.org>
8 * Andrew Morton
9 * Kai Petzke <wpp@marie.physik.tu-berlin.de>
10 * Theodore Ts'o <tytso@mit.edu>
Christoph Lameter89ada672005-10-30 15:01:59 -080011 *
Christoph Lametercde53532008-07-04 09:59:22 -070012 * Made to use alloc_percpu by Christoph Lameter.
Tejun Heoc54fce62010-09-10 16:51:36 +020013 *
14 * Copyright (C) 2010 SUSE Linux Products GmbH
15 * Copyright (C) 2010 Tejun Heo <tj@kernel.org>
16 *
17 * This is the generic async execution mechanism. Work items as are
18 * executed in process context. The worker pool is shared and
19 * automatically managed. There is one worker pool for each CPU and
20 * one extra for works which are better served by workers which are
21 * not bound to any specific CPU.
22 *
23 * Please read Documentation/workqueue.txt for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 */
25
Paul Gortmaker9984de12011-05-23 14:51:41 -040026#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/kernel.h>
28#include <linux/sched.h>
29#include <linux/init.h>
30#include <linux/signal.h>
31#include <linux/completion.h>
32#include <linux/workqueue.h>
33#include <linux/slab.h>
34#include <linux/cpu.h>
35#include <linux/notifier.h>
36#include <linux/kthread.h>
James Bottomley1fa44ec2006-02-23 12:43:43 -060037#include <linux/hardirq.h>
Christoph Lameter46934022006-10-11 01:21:26 -070038#include <linux/mempolicy.h>
Rafael J. Wysocki341a5952006-12-06 20:34:49 -080039#include <linux/freezer.h>
Peter Zijlstrad5abe662006-12-06 20:37:26 -080040#include <linux/kallsyms.h>
41#include <linux/debug_locks.h>
Johannes Berg4e6045f2007-10-18 23:39:55 -070042#include <linux/lockdep.h>
Tejun Heoc34056a2010-06-29 10:07:11 +020043#include <linux/idr.h>
Tejun Heo29c91e92013-03-12 11:30:03 -070044#include <linux/jhash.h>
Sasha Levin42f85702012-12-17 10:01:23 -050045#include <linux/hashtable.h>
Tejun Heo76af4d92013-03-12 11:30:00 -070046#include <linux/rculist.h>
Tejun Heoe22bee72010-06-29 10:07:14 +020047
Tejun Heoea138442013-01-18 14:05:55 -080048#include "workqueue_internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Tejun Heoc8e55f32010-06-29 10:07:12 +020050enum {
Tejun Heobc2ae0f2012-07-17 12:39:27 -070051 /*
Tejun Heo24647572013-01-24 11:01:33 -080052 * worker_pool flags
Tejun Heobc2ae0f2012-07-17 12:39:27 -070053 *
Tejun Heo24647572013-01-24 11:01:33 -080054 * A bound pool is either associated or disassociated with its CPU.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070055 * While associated (!DISASSOCIATED), all workers are bound to the
56 * CPU and none has %WORKER_UNBOUND set and concurrency management
57 * is in effect.
58 *
59 * While DISASSOCIATED, the cpu may be offline and all workers have
60 * %WORKER_UNBOUND set and concurrency management disabled, and may
Tejun Heo24647572013-01-24 11:01:33 -080061 * be executing on any CPU. The pool behaves as an unbound one.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070062 *
63 * Note that DISASSOCIATED can be flipped only while holding
Tejun Heo24647572013-01-24 11:01:33 -080064 * assoc_mutex to avoid changing binding state while
65 * create_worker() is in progress.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070066 */
Tejun Heo11ebea52012-07-12 14:46:37 -070067 POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
Tejun Heo24647572013-01-24 11:01:33 -080068 POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */
Tejun Heo35b6bb62013-01-24 11:01:33 -080069 POOL_FREEZING = 1 << 3, /* freeze in progress */
Tejun Heodb7bccf2010-06-29 10:07:12 +020070
Tejun Heoc8e55f32010-06-29 10:07:12 +020071 /* worker flags */
72 WORKER_STARTED = 1 << 0, /* started */
73 WORKER_DIE = 1 << 1, /* die die die */
74 WORKER_IDLE = 1 << 2, /* is idle */
Tejun Heoe22bee72010-06-29 10:07:14 +020075 WORKER_PREP = 1 << 3, /* preparing to run works */
Tejun Heofb0e7be2010-06-29 10:07:15 +020076 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
Tejun Heof3421792010-07-02 10:03:51 +020077 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
Tejun Heoe22bee72010-06-29 10:07:14 +020078
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -070079 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_UNBOUND |
Tejun Heo403c8212012-07-17 12:39:27 -070080 WORKER_CPU_INTENSIVE,
Tejun Heodb7bccf2010-06-29 10:07:12 +020081
Tejun Heoe34cdddb2013-01-24 11:01:33 -080082 NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */
Tejun Heo4ce62e92012-07-13 22:16:44 -070083
Tejun Heo29c91e92013-03-12 11:30:03 -070084 UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */
Tejun Heoc8e55f32010-06-29 10:07:12 +020085 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
Tejun Heodb7bccf2010-06-29 10:07:12 +020086
Tejun Heoe22bee72010-06-29 10:07:14 +020087 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
88 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
89
Tejun Heo3233cdb2011-02-16 18:10:19 +010090 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
91 /* call for help after 10ms
92 (min two ticks) */
Tejun Heoe22bee72010-06-29 10:07:14 +020093 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
94 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heoe22bee72010-06-29 10:07:14 +020095
96 /*
97 * Rescue workers are used only on emergencies and shared by
98 * all cpus. Give -20.
99 */
100 RESCUER_NICE_LEVEL = -20,
Tejun Heo32704762012-07-13 22:16:45 -0700101 HIGHPRI_NICE_LEVEL = -20,
Tejun Heoc8e55f32010-06-29 10:07:12 +0200102};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104/*
Tejun Heo4690c4a2010-06-29 10:07:10 +0200105 * Structure fields follow one of the following exclusion rules.
106 *
Tejun Heoe41e7042010-08-24 14:22:47 +0200107 * I: Modifiable by initialization/destruction paths and read-only for
108 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200109 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200110 * P: Preemption protected. Disabling preemption is enough and should
111 * only be modified and accessed from the local cpu.
112 *
Tejun Heod565ed62013-01-24 11:01:33 -0800113 * L: pool->lock protected. Access with pool->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200114 *
Tejun Heod565ed62013-01-24 11:01:33 -0800115 * X: During normal operation, modification requires pool->lock and should
116 * be done only from local cpu. Either disabling preemption on local
117 * cpu or grabbing pool->lock is enough for read access. If
118 * POOL_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200119 *
Tejun Heo73f53c42010-06-29 10:07:11 +0200120 * F: wq->flush_mutex protected.
121 *
Tejun Heo4690c4a2010-06-29 10:07:10 +0200122 * W: workqueue_lock protected.
Tejun Heo76af4d92013-03-12 11:30:00 -0700123 *
124 * R: workqueue_lock protected for writes. Sched-RCU protected for reads.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200125 */
126
Tejun Heo2eaebdb2013-01-18 14:05:55 -0800127/* struct worker is defined in workqueue_internal.h */
Tejun Heoc34056a2010-06-29 10:07:11 +0200128
Tejun Heobd7bdd42012-07-12 14:46:37 -0700129struct worker_pool {
Tejun Heod565ed62013-01-24 11:01:33 -0800130 spinlock_t lock; /* the pool lock */
Tejun Heod84ff052013-03-12 11:29:59 -0700131 int cpu; /* I: the associated cpu */
Tejun Heo9daf9e62013-01-24 11:01:33 -0800132 int id; /* I: pool ID */
Tejun Heo11ebea52012-07-12 14:46:37 -0700133 unsigned int flags; /* X: flags */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700134
135 struct list_head worklist; /* L: list of pending works */
136 int nr_workers; /* L: total number of workers */
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700137
138 /* nr_idle includes the ones off idle_list for rebinding */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700139 int nr_idle; /* L: currently idle ones */
140
141 struct list_head idle_list; /* X: list of idle workers */
142 struct timer_list idle_timer; /* L: worker idle timeout */
143 struct timer_list mayday_timer; /* L: SOS timer for workers */
144
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800145 /* workers are chained either in busy_hash or idle_list */
146 DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER);
147 /* L: hash of busy workers */
148
Tejun Heo34a06bd2013-03-12 11:30:00 -0700149 struct mutex manager_arb; /* manager arbitration */
Tejun Heo24647572013-01-24 11:01:33 -0800150 struct mutex assoc_mutex; /* protect POOL_DISASSOCIATED */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700151 struct ida worker_ida; /* L: for worker IDs */
Tejun Heoe19e3972013-01-24 11:39:44 -0800152
Tejun Heo7a4e3442013-03-12 11:30:00 -0700153 struct workqueue_attrs *attrs; /* I: worker attributes */
Tejun Heo29c91e92013-03-12 11:30:03 -0700154 struct hlist_node hash_node; /* R: unbound_pool_hash node */
155 int refcnt; /* refcnt for unbound pools */
Tejun Heo7a4e3442013-03-12 11:30:00 -0700156
Tejun Heoe19e3972013-01-24 11:39:44 -0800157 /*
158 * The current concurrency level. As it's likely to be accessed
159 * from other CPUs during try_to_wake_up(), put it in a separate
160 * cacheline.
161 */
162 atomic_t nr_running ____cacheline_aligned_in_smp;
Tejun Heo29c91e92013-03-12 11:30:03 -0700163
164 /*
165 * Destruction of pool is sched-RCU protected to allow dereferences
166 * from get_work_pool().
167 */
168 struct rcu_head rcu;
Tejun Heo8b03ae32010-06-29 10:07:12 +0200169} ____cacheline_aligned_in_smp;
170
171/*
Tejun Heo112202d2013-02-13 19:29:12 -0800172 * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS
173 * of work_struct->data are used for flags and the remaining high bits
174 * point to the pwq; thus, pwqs need to be aligned at two's power of the
175 * number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 */
Tejun Heo112202d2013-02-13 19:29:12 -0800177struct pool_workqueue {
Tejun Heobd7bdd42012-07-12 14:46:37 -0700178 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200179 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200180 int work_color; /* L: current color */
181 int flush_color; /* L: flushing color */
Tejun Heo8864b4e2013-03-12 11:30:04 -0700182 int refcnt; /* L: reference count */
Tejun Heo73f53c42010-06-29 10:07:11 +0200183 int nr_in_flight[WORK_NR_COLORS];
184 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200185 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200186 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200187 struct list_head delayed_works; /* L: delayed works */
Tejun Heo76af4d92013-03-12 11:30:00 -0700188 struct list_head pwqs_node; /* R: node on wq->pwqs */
Tejun Heo493a1722013-03-12 11:29:59 -0700189 struct list_head mayday_node; /* W: node on wq->maydays */
Tejun Heo8864b4e2013-03-12 11:30:04 -0700190
191 /*
192 * Release of unbound pwq is punted to system_wq. See put_pwq()
193 * and pwq_unbound_release_workfn() for details. pool_workqueue
194 * itself is also sched-RCU protected so that the first pwq can be
195 * determined without grabbing workqueue_lock.
196 */
197 struct work_struct unbound_release_work;
198 struct rcu_head rcu;
Tejun Heoe904e6c2013-03-12 11:29:57 -0700199} __aligned(1 << WORK_STRUCT_FLAG_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200202 * Structure used to wait for workqueue flush.
203 */
204struct wq_flusher {
205 struct list_head list; /* F: list of flushers */
206 int flush_color; /* F: flush color waiting for */
207 struct completion done; /* flush completion */
208};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Tejun Heo73f53c42010-06-29 10:07:11 +0200210/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 * The externally visible workqueue abstraction is an array of
212 * per-CPU workqueues:
213 */
214struct workqueue_struct {
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200215 unsigned int flags; /* W: WQ_* flags */
Tejun Heo420c0dd2013-03-12 11:29:59 -0700216 struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwq's */
Tejun Heo76af4d92013-03-12 11:30:00 -0700217 struct list_head pwqs; /* R: all pwqs of this wq */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200218 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200219
220 struct mutex flush_mutex; /* protects wq flushing */
221 int work_color; /* F: current work color */
222 int flush_color; /* F: current flush color */
Tejun Heo112202d2013-02-13 19:29:12 -0800223 atomic_t nr_pwqs_to_flush; /* flush in progress */
Tejun Heo73f53c42010-06-29 10:07:11 +0200224 struct wq_flusher *first_flusher; /* F: first flusher */
225 struct list_head flusher_queue; /* F: flush waiters */
226 struct list_head flusher_overflow; /* F: flush overflow list */
227
Tejun Heo493a1722013-03-12 11:29:59 -0700228 struct list_head maydays; /* W: pwqs requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200229 struct worker *rescuer; /* I: rescue worker */
230
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200231 int nr_drainers; /* W: drain in progress */
Tejun Heo112202d2013-02-13 19:29:12 -0800232 int saved_max_active; /* W: saved pwq max_active */
Johannes Berg4e6045f2007-10-18 23:39:55 -0700233#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200234 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700235#endif
Tejun Heob196be82012-01-10 15:11:35 -0800236 char name[]; /* I: workqueue name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237};
238
Tejun Heoe904e6c2013-03-12 11:29:57 -0700239static struct kmem_cache *pwq_cache;
240
Tejun Heo29c91e92013-03-12 11:30:03 -0700241/* hash of all unbound pools keyed by pool->attrs */
242static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER);
243
244static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS];
245
Tejun Heod320c032010-06-29 10:07:14 +0200246struct workqueue_struct *system_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200247EXPORT_SYMBOL_GPL(system_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300248struct workqueue_struct *system_highpri_wq __read_mostly;
Joonsoo Kim1aabe902012-08-15 23:25:39 +0900249EXPORT_SYMBOL_GPL(system_highpri_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300250struct workqueue_struct *system_long_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200251EXPORT_SYMBOL_GPL(system_long_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300252struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200253EXPORT_SYMBOL_GPL(system_unbound_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300254struct workqueue_struct *system_freezable_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100255EXPORT_SYMBOL_GPL(system_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200256
Tejun Heo97bd2342010-10-05 10:41:14 +0200257#define CREATE_TRACE_POINTS
258#include <trace/events/workqueue.h>
259
Tejun Heo76af4d92013-03-12 11:30:00 -0700260#define assert_rcu_or_wq_lock() \
261 rcu_lockdep_assert(rcu_read_lock_sched_held() || \
262 lockdep_is_held(&workqueue_lock), \
263 "sched RCU or workqueue lock should be held")
264
Tejun Heof02ae732013-03-12 11:30:03 -0700265#define for_each_cpu_worker_pool(pool, cpu) \
266 for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \
267 (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \
Tejun Heo7a62c2c2013-03-12 11:30:03 -0700268 (pool)++)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700269
Sasha Levinb67bfe02013-02-27 17:06:00 -0800270#define for_each_busy_worker(worker, i, pool) \
271 hash_for_each(pool->busy_hash, i, worker, hentry)
Tejun Heodb7bccf2010-06-29 10:07:12 +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
Tejun Heofa1b54e2013-03-12 11:30:00 -0700277 *
278 * This must be called either with workqueue_lock held or sched RCU read
279 * locked. If the pool needs to be used beyond the locking in effect, the
280 * caller is responsible for guaranteeing that the pool stays online.
281 *
282 * The if/else clause exists only for the lockdep assertion and can be
283 * ignored.
Tejun Heo17116962013-03-12 11:29:58 -0700284 */
285#define for_each_pool(pool, id) \
Tejun Heofa1b54e2013-03-12 11:30:00 -0700286 idr_for_each_entry(&worker_pool_idr, pool, id) \
287 if (({ assert_rcu_or_wq_lock(); false; })) { } \
288 else
Tejun Heo17116962013-03-12 11:29:58 -0700289
290/**
Tejun Heo49e3cf42013-03-12 11:29:58 -0700291 * for_each_pwq - iterate through all pool_workqueues of the specified workqueue
292 * @pwq: iteration cursor
293 * @wq: the target workqueue
Tejun Heo76af4d92013-03-12 11:30:00 -0700294 *
295 * This must be called either with workqueue_lock held or sched RCU read
296 * locked. If the pwq needs to be used beyond the locking in effect, the
297 * caller is responsible for guaranteeing that the pwq stays online.
298 *
299 * The if/else clause exists only for the lockdep assertion and can be
300 * ignored.
Tejun Heo49e3cf42013-03-12 11:29:58 -0700301 */
302#define for_each_pwq(pwq, wq) \
Tejun Heo76af4d92013-03-12 11:30:00 -0700303 list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node) \
304 if (({ assert_rcu_or_wq_lock(); false; })) { } \
305 else
Tejun Heof3421792010-07-02 10:03:51 +0200306
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900307#ifdef CONFIG_DEBUG_OBJECTS_WORK
308
309static struct debug_obj_descr work_debug_descr;
310
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100311static void *work_debug_hint(void *addr)
312{
313 return ((struct work_struct *) addr)->func;
314}
315
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900316/*
317 * fixup_init is called when:
318 * - an active object is initialized
319 */
320static int work_fixup_init(void *addr, enum debug_obj_state state)
321{
322 struct work_struct *work = addr;
323
324 switch (state) {
325 case ODEBUG_STATE_ACTIVE:
326 cancel_work_sync(work);
327 debug_object_init(work, &work_debug_descr);
328 return 1;
329 default:
330 return 0;
331 }
332}
333
334/*
335 * fixup_activate is called when:
336 * - an active object is activated
337 * - an unknown object is activated (might be a statically initialized object)
338 */
339static int work_fixup_activate(void *addr, enum debug_obj_state state)
340{
341 struct work_struct *work = addr;
342
343 switch (state) {
344
345 case ODEBUG_STATE_NOTAVAILABLE:
346 /*
347 * This is not really a fixup. The work struct was
348 * statically initialized. We just make sure that it
349 * is tracked in the object tracker.
350 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200351 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900352 debug_object_init(work, &work_debug_descr);
353 debug_object_activate(work, &work_debug_descr);
354 return 0;
355 }
356 WARN_ON_ONCE(1);
357 return 0;
358
359 case ODEBUG_STATE_ACTIVE:
360 WARN_ON(1);
361
362 default:
363 return 0;
364 }
365}
366
367/*
368 * fixup_free is called when:
369 * - an active object is freed
370 */
371static int work_fixup_free(void *addr, enum debug_obj_state state)
372{
373 struct work_struct *work = addr;
374
375 switch (state) {
376 case ODEBUG_STATE_ACTIVE:
377 cancel_work_sync(work);
378 debug_object_free(work, &work_debug_descr);
379 return 1;
380 default:
381 return 0;
382 }
383}
384
385static struct debug_obj_descr work_debug_descr = {
386 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100387 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900388 .fixup_init = work_fixup_init,
389 .fixup_activate = work_fixup_activate,
390 .fixup_free = work_fixup_free,
391};
392
393static inline void debug_work_activate(struct work_struct *work)
394{
395 debug_object_activate(work, &work_debug_descr);
396}
397
398static inline void debug_work_deactivate(struct work_struct *work)
399{
400 debug_object_deactivate(work, &work_debug_descr);
401}
402
403void __init_work(struct work_struct *work, int onstack)
404{
405 if (onstack)
406 debug_object_init_on_stack(work, &work_debug_descr);
407 else
408 debug_object_init(work, &work_debug_descr);
409}
410EXPORT_SYMBOL_GPL(__init_work);
411
412void destroy_work_on_stack(struct work_struct *work)
413{
414 debug_object_free(work, &work_debug_descr);
415}
416EXPORT_SYMBOL_GPL(destroy_work_on_stack);
417
418#else
419static inline void debug_work_activate(struct work_struct *work) { }
420static inline void debug_work_deactivate(struct work_struct *work) { }
421#endif
422
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100423/* Serializes the accesses to the list of workqueues. */
424static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425static LIST_HEAD(workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200426static bool workqueue_freezing; /* W: have wqs started freezing? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Oleg Nesterov14441962007-05-23 13:57:57 -0700428/*
Tejun Heoe19e3972013-01-24 11:39:44 -0800429 * The CPU and unbound standard worker pools. The unbound ones have
430 * POOL_DISASSOCIATED set, and their workers have WORKER_UNBOUND set.
Oleg Nesterov14441962007-05-23 13:57:57 -0700431 */
Tejun Heoe19e3972013-01-24 11:39:44 -0800432static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS],
Tejun Heof02ae732013-03-12 11:30:03 -0700433 cpu_worker_pools);
Tejun Heof3421792010-07-02 10:03:51 +0200434
Tejun Heofa1b54e2013-03-12 11:30:00 -0700435/*
436 * idr of all pools. Modifications are protected by workqueue_lock. Read
437 * accesses are protected by sched-RCU protected.
438 */
Tejun Heo9daf9e62013-01-24 11:01:33 -0800439static DEFINE_IDR(worker_pool_idr);
440
Tejun Heoc34056a2010-06-29 10:07:11 +0200441static int worker_thread(void *__worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Tejun Heo9daf9e62013-01-24 11:01:33 -0800443/* allocate ID and assign it to @pool */
444static int worker_pool_assign_id(struct worker_pool *pool)
445{
446 int ret;
447
Tejun Heofa1b54e2013-03-12 11:30:00 -0700448 do {
449 if (!idr_pre_get(&worker_pool_idr, GFP_KERNEL))
450 return -ENOMEM;
451
452 spin_lock_irq(&workqueue_lock);
453 ret = idr_get_new(&worker_pool_idr, pool, &pool->id);
454 spin_unlock_irq(&workqueue_lock);
455 } while (ret == -EAGAIN);
Tejun Heo9daf9e62013-01-24 11:01:33 -0800456
457 return ret;
458}
459
Tejun Heo76af4d92013-03-12 11:30:00 -0700460/**
461 * first_pwq - return the first pool_workqueue of the specified workqueue
462 * @wq: the target workqueue
463 *
464 * This must be called either with workqueue_lock held or sched RCU read
465 * locked. If the pwq needs to be used beyond the locking in effect, the
466 * caller is responsible for guaranteeing that the pwq stays online.
467 */
Tejun Heo7fb98ea2013-03-12 11:30:00 -0700468static struct pool_workqueue *first_pwq(struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700469{
Tejun Heo76af4d92013-03-12 11:30:00 -0700470 assert_rcu_or_wq_lock();
471 return list_first_or_null_rcu(&wq->pwqs, struct pool_workqueue,
472 pwqs_node);
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700473}
474
Tejun Heo73f53c42010-06-29 10:07:11 +0200475static unsigned int work_color_to_flags(int color)
476{
477 return color << WORK_STRUCT_COLOR_SHIFT;
478}
479
480static int get_work_color(struct work_struct *work)
481{
482 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
483 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
484}
485
486static int work_next_color(int color)
487{
488 return (color + 1) % WORK_NR_COLORS;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700489}
490
David Howells4594bf12006-12-07 11:33:26 +0000491/*
Tejun Heo112202d2013-02-13 19:29:12 -0800492 * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data
493 * contain the pointer to the queued pwq. Once execution starts, the flag
Tejun Heo7c3eed52013-01-24 11:01:33 -0800494 * is cleared and the high bits contain OFFQ flags and pool ID.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200495 *
Tejun Heo112202d2013-02-13 19:29:12 -0800496 * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling()
497 * and clear_work_data() can be used to set the pwq, pool or clear
Tejun Heobbb68df2012-08-03 10:30:46 -0700498 * work->data. These functions should only be called while the work is
499 * owned - ie. while the PENDING bit is set.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200500 *
Tejun Heo112202d2013-02-13 19:29:12 -0800501 * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq
Tejun Heo7c3eed52013-01-24 11:01:33 -0800502 * corresponding to a work. Pool is available once the work has been
Tejun Heo112202d2013-02-13 19:29:12 -0800503 * queued anywhere after initialization until it is sync canceled. pwq is
Tejun Heo7c3eed52013-01-24 11:01:33 -0800504 * available only while the work item is queued.
Tejun Heobbb68df2012-08-03 10:30:46 -0700505 *
506 * %WORK_OFFQ_CANCELING is used to mark a work item which is being
507 * canceled. While being canceled, a work item may have its PENDING set
508 * but stay off timer and worklist for arbitrarily long and nobody should
509 * try to steal the PENDING bit.
David Howells4594bf12006-12-07 11:33:26 +0000510 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200511static inline void set_work_data(struct work_struct *work, unsigned long data,
512 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000513{
Tejun Heo6183c002013-03-12 11:29:57 -0700514 WARN_ON_ONCE(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200515 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000516}
David Howells365970a2006-11-22 14:54:49 +0000517
Tejun Heo112202d2013-02-13 19:29:12 -0800518static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq,
Tejun Heo7a22ad72010-06-29 10:07:13 +0200519 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200520{
Tejun Heo112202d2013-02-13 19:29:12 -0800521 set_work_data(work, (unsigned long)pwq,
522 WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200523}
524
Lai Jiangshan4468a002013-02-06 18:04:53 -0800525static void set_work_pool_and_keep_pending(struct work_struct *work,
526 int pool_id)
527{
528 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT,
529 WORK_STRUCT_PENDING);
530}
531
Tejun Heo7c3eed52013-01-24 11:01:33 -0800532static void set_work_pool_and_clear_pending(struct work_struct *work,
533 int pool_id)
David Howells365970a2006-11-22 14:54:49 +0000534{
Tejun Heo23657bb2012-08-13 17:08:19 -0700535 /*
536 * The following wmb is paired with the implied mb in
537 * test_and_set_bit(PENDING) and ensures all updates to @work made
538 * here are visible to and precede any updates by the next PENDING
539 * owner.
540 */
541 smp_wmb();
Tejun Heo7c3eed52013-01-24 11:01:33 -0800542 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200543}
544
545static void clear_work_data(struct work_struct *work)
546{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800547 smp_wmb(); /* see set_work_pool_and_clear_pending() */
548 set_work_data(work, WORK_STRUCT_NO_POOL, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200549}
550
Tejun Heo112202d2013-02-13 19:29:12 -0800551static struct pool_workqueue *get_work_pwq(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200552{
Tejun Heoe1201532010-07-22 14:14:25 +0200553 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200554
Tejun Heo112202d2013-02-13 19:29:12 -0800555 if (data & WORK_STRUCT_PWQ)
Tejun Heoe1201532010-07-22 14:14:25 +0200556 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
557 else
558 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200559}
560
Tejun Heo7c3eed52013-01-24 11:01:33 -0800561/**
562 * get_work_pool - return the worker_pool a given work was associated with
563 * @work: the work item of interest
564 *
565 * Return the worker_pool @work was last associated with. %NULL if none.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700566 *
567 * Pools are created and destroyed under workqueue_lock, and allows read
568 * access under sched-RCU read lock. As such, this function should be
569 * called under workqueue_lock or with preemption disabled.
570 *
571 * All fields of the returned pool are accessible as long as the above
572 * mentioned locking is in effect. If the returned pool needs to be used
573 * beyond the critical section, the caller is responsible for ensuring the
574 * returned pool is and stays online.
Tejun Heo7c3eed52013-01-24 11:01:33 -0800575 */
576static struct worker_pool *get_work_pool(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200577{
Tejun Heoe1201532010-07-22 14:14:25 +0200578 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800579 int pool_id;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200580
Tejun Heofa1b54e2013-03-12 11:30:00 -0700581 assert_rcu_or_wq_lock();
582
Tejun Heo112202d2013-02-13 19:29:12 -0800583 if (data & WORK_STRUCT_PWQ)
584 return ((struct pool_workqueue *)
Tejun Heo7c3eed52013-01-24 11:01:33 -0800585 (data & WORK_STRUCT_WQ_DATA_MASK))->pool;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200586
Tejun Heo7c3eed52013-01-24 11:01:33 -0800587 pool_id = data >> WORK_OFFQ_POOL_SHIFT;
588 if (pool_id == WORK_OFFQ_POOL_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200589 return NULL;
590
Tejun Heofa1b54e2013-03-12 11:30:00 -0700591 return idr_find(&worker_pool_idr, pool_id);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800592}
593
594/**
595 * get_work_pool_id - return the worker pool ID a given work is associated with
596 * @work: the work item of interest
597 *
598 * Return the worker_pool ID @work was last associated with.
599 * %WORK_OFFQ_POOL_NONE if none.
600 */
601static int get_work_pool_id(struct work_struct *work)
602{
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800603 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800604
Tejun Heo112202d2013-02-13 19:29:12 -0800605 if (data & WORK_STRUCT_PWQ)
606 return ((struct pool_workqueue *)
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800607 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id;
608
609 return data >> WORK_OFFQ_POOL_SHIFT;
Tejun Heo7c3eed52013-01-24 11:01:33 -0800610}
611
Tejun Heobbb68df2012-08-03 10:30:46 -0700612static void mark_work_canceling(struct work_struct *work)
613{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800614 unsigned long pool_id = get_work_pool_id(work);
Tejun Heobbb68df2012-08-03 10:30:46 -0700615
Tejun Heo7c3eed52013-01-24 11:01:33 -0800616 pool_id <<= WORK_OFFQ_POOL_SHIFT;
617 set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700618}
619
620static bool work_is_canceling(struct work_struct *work)
621{
622 unsigned long data = atomic_long_read(&work->data);
623
Tejun Heo112202d2013-02-13 19:29:12 -0800624 return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700625}
626
David Howells365970a2006-11-22 14:54:49 +0000627/*
Tejun Heo32704762012-07-13 22:16:45 -0700628 * Policy functions. These define the policies on how the global worker
629 * pools are managed. Unless noted otherwise, these functions assume that
Tejun Heod565ed62013-01-24 11:01:33 -0800630 * they're being called with pool->lock held.
David Howells365970a2006-11-22 14:54:49 +0000631 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200632
Tejun Heo63d95a92012-07-12 14:46:37 -0700633static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000634{
Tejun Heoe19e3972013-01-24 11:39:44 -0800635 return !atomic_read(&pool->nr_running);
David Howells365970a2006-11-22 14:54:49 +0000636}
637
Tejun Heoe22bee72010-06-29 10:07:14 +0200638/*
639 * Need to wake up a worker? Called from anything but currently
640 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700641 *
642 * Note that, because unbound workers never contribute to nr_running, this
Tejun Heo706026c2013-01-24 11:01:34 -0800643 * function will always return %true for unbound pools as long as the
Tejun Heo974271c2012-07-12 14:46:37 -0700644 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200645 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700646static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000647{
Tejun Heo63d95a92012-07-12 14:46:37 -0700648 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000649}
650
Tejun Heoe22bee72010-06-29 10:07:14 +0200651/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700652static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200653{
Tejun Heo63d95a92012-07-12 14:46:37 -0700654 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200655}
656
657/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700658static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200659{
Tejun Heoe19e3972013-01-24 11:39:44 -0800660 return !list_empty(&pool->worklist) &&
661 atomic_read(&pool->nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200662}
663
664/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700665static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200666{
Tejun Heo63d95a92012-07-12 14:46:37 -0700667 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200668}
669
670/* Do I need to be the manager? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700671static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200672{
Tejun Heo63d95a92012-07-12 14:46:37 -0700673 return need_to_create_worker(pool) ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700674 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200675}
676
677/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700678static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200679{
Tejun Heo34a06bd2013-03-12 11:30:00 -0700680 bool managing = mutex_is_locked(&pool->manager_arb);
Tejun Heo63d95a92012-07-12 14:46:37 -0700681 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
682 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200683
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700684 /*
685 * nr_idle and idle_list may disagree if idle rebinding is in
686 * progress. Never return %true if idle_list is empty.
687 */
688 if (list_empty(&pool->idle_list))
689 return false;
690
Tejun Heoe22bee72010-06-29 10:07:14 +0200691 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
692}
693
694/*
695 * Wake up functions.
696 */
697
Tejun Heo7e116292010-06-29 10:07:13 +0200698/* Return the first worker. Safe with preemption disabled */
Tejun Heo63d95a92012-07-12 14:46:37 -0700699static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200700{
Tejun Heo63d95a92012-07-12 14:46:37 -0700701 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200702 return NULL;
703
Tejun Heo63d95a92012-07-12 14:46:37 -0700704 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200705}
706
707/**
708 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700709 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200710 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700711 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200712 *
713 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800714 * spin_lock_irq(pool->lock).
Tejun Heo7e116292010-06-29 10:07:13 +0200715 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700716static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200717{
Tejun Heo63d95a92012-07-12 14:46:37 -0700718 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200719
720 if (likely(worker))
721 wake_up_process(worker->task);
722}
723
Tejun Heo4690c4a2010-06-29 10:07:10 +0200724/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200725 * wq_worker_waking_up - a worker is waking up
726 * @task: task waking up
727 * @cpu: CPU @task is waking up to
728 *
729 * This function is called during try_to_wake_up() when a worker is
730 * being awoken.
731 *
732 * CONTEXT:
733 * spin_lock_irq(rq->lock)
734 */
Tejun Heod84ff052013-03-12 11:29:59 -0700735void wq_worker_waking_up(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200736{
737 struct worker *worker = kthread_data(task);
738
Joonsoo Kim36576002012-10-26 23:03:49 +0900739 if (!(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoec22ca52013-01-24 11:01:33 -0800740 WARN_ON_ONCE(worker->pool->cpu != cpu);
Tejun Heoe19e3972013-01-24 11:39:44 -0800741 atomic_inc(&worker->pool->nr_running);
Joonsoo Kim36576002012-10-26 23:03:49 +0900742 }
Tejun Heoe22bee72010-06-29 10:07:14 +0200743}
744
745/**
746 * wq_worker_sleeping - a worker is going to sleep
747 * @task: task going to sleep
748 * @cpu: CPU in question, must be the current CPU number
749 *
750 * This function is called during schedule() when a busy worker is
751 * going to sleep. Worker on the same cpu can be woken up by
752 * returning pointer to its task.
753 *
754 * CONTEXT:
755 * spin_lock_irq(rq->lock)
756 *
757 * RETURNS:
758 * Worker task on @cpu to wake up, %NULL if none.
759 */
Tejun Heod84ff052013-03-12 11:29:59 -0700760struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200761{
762 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heo111c2252013-01-17 17:16:24 -0800763 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200764
Tejun Heo111c2252013-01-17 17:16:24 -0800765 /*
766 * Rescuers, which may not have all the fields set up like normal
767 * workers, also reach here, let's not access anything before
768 * checking NOT_RUNNING.
769 */
Steven Rostedt2d646722010-12-03 23:12:33 -0500770 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200771 return NULL;
772
Tejun Heo111c2252013-01-17 17:16:24 -0800773 pool = worker->pool;
Tejun Heo111c2252013-01-17 17:16:24 -0800774
Tejun Heoe22bee72010-06-29 10:07:14 +0200775 /* this can only happen on the local cpu */
Tejun Heo6183c002013-03-12 11:29:57 -0700776 if (WARN_ON_ONCE(cpu != raw_smp_processor_id()))
777 return NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +0200778
779 /*
780 * The counterpart of the following dec_and_test, implied mb,
781 * worklist not empty test sequence is in insert_work().
782 * Please read comment there.
783 *
Tejun Heo628c78e2012-07-17 12:39:27 -0700784 * NOT_RUNNING is clear. This means that we're bound to and
785 * running on the local cpu w/ rq lock held and preemption
786 * disabled, which in turn means that none else could be
Tejun Heod565ed62013-01-24 11:01:33 -0800787 * manipulating idle_list, so dereferencing idle_list without pool
Tejun Heo628c78e2012-07-17 12:39:27 -0700788 * lock is safe.
Tejun Heoe22bee72010-06-29 10:07:14 +0200789 */
Tejun Heoe19e3972013-01-24 11:39:44 -0800790 if (atomic_dec_and_test(&pool->nr_running) &&
791 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700792 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200793 return to_wakeup ? to_wakeup->task : NULL;
794}
795
796/**
797 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200798 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200799 * @flags: flags to set
800 * @wakeup: wakeup an idle worker if necessary
801 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200802 * Set @flags in @worker->flags and adjust nr_running accordingly. If
803 * nr_running becomes zero and @wakeup is %true, an idle worker is
804 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200805 *
Tejun Heocb444762010-07-02 10:03:50 +0200806 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800807 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200808 */
809static inline void worker_set_flags(struct worker *worker, unsigned int flags,
810 bool wakeup)
811{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700812 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200813
Tejun Heocb444762010-07-02 10:03:50 +0200814 WARN_ON_ONCE(worker->task != current);
815
Tejun Heoe22bee72010-06-29 10:07:14 +0200816 /*
817 * If transitioning into NOT_RUNNING, adjust nr_running and
818 * wake up an idle worker as necessary if requested by
819 * @wakeup.
820 */
821 if ((flags & WORKER_NOT_RUNNING) &&
822 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoe22bee72010-06-29 10:07:14 +0200823 if (wakeup) {
Tejun Heoe19e3972013-01-24 11:39:44 -0800824 if (atomic_dec_and_test(&pool->nr_running) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -0700825 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700826 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200827 } else
Tejun Heoe19e3972013-01-24 11:39:44 -0800828 atomic_dec(&pool->nr_running);
Tejun Heoe22bee72010-06-29 10:07:14 +0200829 }
830
Tejun Heod302f012010-06-29 10:07:13 +0200831 worker->flags |= flags;
832}
833
834/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200835 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200836 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200837 * @flags: flags to clear
838 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200839 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200840 *
Tejun Heocb444762010-07-02 10:03:50 +0200841 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800842 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200843 */
844static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
845{
Tejun Heo63d95a92012-07-12 14:46:37 -0700846 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200847 unsigned int oflags = worker->flags;
848
Tejun Heocb444762010-07-02 10:03:50 +0200849 WARN_ON_ONCE(worker->task != current);
850
Tejun Heod302f012010-06-29 10:07:13 +0200851 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200852
Tejun Heo42c025f2011-01-11 15:58:49 +0100853 /*
854 * If transitioning out of NOT_RUNNING, increment nr_running. Note
855 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
856 * of multiple flags, not a single flag.
857 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200858 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
859 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heoe19e3972013-01-24 11:39:44 -0800860 atomic_inc(&pool->nr_running);
Tejun Heod302f012010-06-29 10:07:13 +0200861}
862
863/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200864 * find_worker_executing_work - find worker which is executing a work
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800865 * @pool: pool of interest
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200866 * @work: work to find worker for
867 *
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800868 * Find a worker which is executing @work on @pool by searching
869 * @pool->busy_hash which is keyed by the address of @work. For a worker
Tejun Heoa2c1c572012-12-18 10:35:02 -0800870 * to match, its current execution should match the address of @work and
871 * its work function. This is to avoid unwanted dependency between
872 * unrelated work executions through a work item being recycled while still
873 * being executed.
874 *
875 * This is a bit tricky. A work item may be freed once its execution
876 * starts and nothing prevents the freed area from being recycled for
877 * another work item. If the same work item address ends up being reused
878 * before the original execution finishes, workqueue will identify the
879 * recycled work item as currently executing and make it wait until the
880 * current execution finishes, introducing an unwanted dependency.
881 *
882 * This function checks the work item address, work function and workqueue
883 * to avoid false positives. Note that this isn't complete as one may
884 * construct a work function which can introduce dependency onto itself
885 * through a recycled work item. Well, if somebody wants to shoot oneself
886 * in the foot that badly, there's only so much we can do, and if such
887 * deadlock actually occurs, it should be easy to locate the culprit work
888 * function.
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200889 *
890 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800891 * spin_lock_irq(pool->lock).
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200892 *
893 * RETURNS:
894 * Pointer to worker which is executing @work if found, NULL
895 * otherwise.
896 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800897static struct worker *find_worker_executing_work(struct worker_pool *pool,
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200898 struct work_struct *work)
899{
Sasha Levin42f85702012-12-17 10:01:23 -0500900 struct worker *worker;
Sasha Levin42f85702012-12-17 10:01:23 -0500901
Sasha Levinb67bfe02013-02-27 17:06:00 -0800902 hash_for_each_possible(pool->busy_hash, worker, hentry,
Tejun Heoa2c1c572012-12-18 10:35:02 -0800903 (unsigned long)work)
904 if (worker->current_work == work &&
905 worker->current_func == work->func)
Sasha Levin42f85702012-12-17 10:01:23 -0500906 return worker;
907
908 return NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200909}
910
911/**
Tejun Heobf4ede02012-08-03 10:30:46 -0700912 * move_linked_works - move linked works to a list
913 * @work: start of series of works to be scheduled
914 * @head: target list to append @work to
915 * @nextp: out paramter for nested worklist walking
916 *
917 * Schedule linked works starting from @work to @head. Work series to
918 * be scheduled starts at @work and includes any consecutive work with
919 * WORK_STRUCT_LINKED set in its predecessor.
920 *
921 * If @nextp is not NULL, it's updated to point to the next work of
922 * the last scheduled work. This allows move_linked_works() to be
923 * nested inside outer list_for_each_entry_safe().
924 *
925 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800926 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -0700927 */
928static void move_linked_works(struct work_struct *work, struct list_head *head,
929 struct work_struct **nextp)
930{
931 struct work_struct *n;
932
933 /*
934 * Linked worklist will always end before the end of the list,
935 * use NULL for list head.
936 */
937 list_for_each_entry_safe_from(work, n, NULL, entry) {
938 list_move_tail(&work->entry, head);
939 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
940 break;
941 }
942
943 /*
944 * If we're already inside safe list traversal and have moved
945 * multiple works to the scheduled queue, the next position
946 * needs to be updated.
947 */
948 if (nextp)
949 *nextp = n;
950}
951
Tejun Heo8864b4e2013-03-12 11:30:04 -0700952/**
953 * get_pwq - get an extra reference on the specified pool_workqueue
954 * @pwq: pool_workqueue to get
955 *
956 * Obtain an extra reference on @pwq. The caller should guarantee that
957 * @pwq has positive refcnt and be holding the matching pool->lock.
958 */
959static void get_pwq(struct pool_workqueue *pwq)
960{
961 lockdep_assert_held(&pwq->pool->lock);
962 WARN_ON_ONCE(pwq->refcnt <= 0);
963 pwq->refcnt++;
964}
965
966/**
967 * put_pwq - put a pool_workqueue reference
968 * @pwq: pool_workqueue to put
969 *
970 * Drop a reference of @pwq. If its refcnt reaches zero, schedule its
971 * destruction. The caller should be holding the matching pool->lock.
972 */
973static void put_pwq(struct pool_workqueue *pwq)
974{
975 lockdep_assert_held(&pwq->pool->lock);
976 if (likely(--pwq->refcnt))
977 return;
978 if (WARN_ON_ONCE(!(pwq->wq->flags & WQ_UNBOUND)))
979 return;
980 /*
981 * @pwq can't be released under pool->lock, bounce to
982 * pwq_unbound_release_workfn(). This never recurses on the same
983 * pool->lock as this path is taken only for unbound workqueues and
984 * the release work item is scheduled on a per-cpu workqueue. To
985 * avoid lockdep warning, unbound pool->locks are given lockdep
986 * subclass of 1 in get_unbound_pool().
987 */
988 schedule_work(&pwq->unbound_release_work);
989}
990
Tejun Heo112202d2013-02-13 19:29:12 -0800991static void pwq_activate_delayed_work(struct work_struct *work)
Tejun Heobf4ede02012-08-03 10:30:46 -0700992{
Tejun Heo112202d2013-02-13 19:29:12 -0800993 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobf4ede02012-08-03 10:30:46 -0700994
995 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -0800996 move_linked_works(work, &pwq->pool->worklist, NULL);
Tejun Heobf4ede02012-08-03 10:30:46 -0700997 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo112202d2013-02-13 19:29:12 -0800998 pwq->nr_active++;
Tejun Heobf4ede02012-08-03 10:30:46 -0700999}
1000
Tejun Heo112202d2013-02-13 19:29:12 -08001001static void pwq_activate_first_delayed(struct pool_workqueue *pwq)
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001002{
Tejun Heo112202d2013-02-13 19:29:12 -08001003 struct work_struct *work = list_first_entry(&pwq->delayed_works,
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001004 struct work_struct, entry);
1005
Tejun Heo112202d2013-02-13 19:29:12 -08001006 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001007}
1008
Tejun Heobf4ede02012-08-03 10:30:46 -07001009/**
Tejun Heo112202d2013-02-13 19:29:12 -08001010 * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight
1011 * @pwq: pwq of interest
Tejun Heobf4ede02012-08-03 10:30:46 -07001012 * @color: color of work which left the queue
Tejun Heobf4ede02012-08-03 10:30:46 -07001013 *
1014 * A work either has completed or is removed from pending queue,
Tejun Heo112202d2013-02-13 19:29:12 -08001015 * decrement nr_in_flight of its pwq and handle workqueue flushing.
Tejun Heobf4ede02012-08-03 10:30:46 -07001016 *
1017 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001018 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -07001019 */
Tejun Heo112202d2013-02-13 19:29:12 -08001020static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color)
Tejun Heobf4ede02012-08-03 10:30:46 -07001021{
Tejun Heo8864b4e2013-03-12 11:30:04 -07001022 /* uncolored work items don't participate in flushing or nr_active */
Tejun Heobf4ede02012-08-03 10:30:46 -07001023 if (color == WORK_NO_COLOR)
Tejun Heo8864b4e2013-03-12 11:30:04 -07001024 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001025
Tejun Heo112202d2013-02-13 19:29:12 -08001026 pwq->nr_in_flight[color]--;
Tejun Heobf4ede02012-08-03 10:30:46 -07001027
Tejun Heo112202d2013-02-13 19:29:12 -08001028 pwq->nr_active--;
1029 if (!list_empty(&pwq->delayed_works)) {
Lai Jiangshanb3f9f402012-09-18 10:40:00 -07001030 /* one down, submit a delayed one */
Tejun Heo112202d2013-02-13 19:29:12 -08001031 if (pwq->nr_active < pwq->max_active)
1032 pwq_activate_first_delayed(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001033 }
1034
1035 /* is flush in progress and are we at the flushing tip? */
Tejun Heo112202d2013-02-13 19:29:12 -08001036 if (likely(pwq->flush_color != color))
Tejun Heo8864b4e2013-03-12 11:30:04 -07001037 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001038
1039 /* are there still in-flight works? */
Tejun Heo112202d2013-02-13 19:29:12 -08001040 if (pwq->nr_in_flight[color])
Tejun Heo8864b4e2013-03-12 11:30:04 -07001041 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001042
Tejun Heo112202d2013-02-13 19:29:12 -08001043 /* this pwq is done, clear flush_color */
1044 pwq->flush_color = -1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001045
1046 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001047 * If this was the last pwq, wake up the first flusher. It
Tejun Heobf4ede02012-08-03 10:30:46 -07001048 * will handle the rest.
1049 */
Tejun Heo112202d2013-02-13 19:29:12 -08001050 if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush))
1051 complete(&pwq->wq->first_flusher->done);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001052out_put:
1053 put_pwq(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001054}
1055
Tejun Heo36e227d2012-08-03 10:30:46 -07001056/**
Tejun Heobbb68df2012-08-03 10:30:46 -07001057 * try_to_grab_pending - steal work item from worklist and disable irq
Tejun Heo36e227d2012-08-03 10:30:46 -07001058 * @work: work item to steal
1059 * @is_dwork: @work is a delayed_work
Tejun Heobbb68df2012-08-03 10:30:46 -07001060 * @flags: place to store irq state
Tejun Heo36e227d2012-08-03 10:30:46 -07001061 *
1062 * Try to grab PENDING bit of @work. This function can handle @work in any
1063 * stable state - idle, on timer or on worklist. Return values are
1064 *
1065 * 1 if @work was pending and we successfully stole PENDING
1066 * 0 if @work was idle and we claimed PENDING
1067 * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry
Tejun Heobbb68df2012-08-03 10:30:46 -07001068 * -ENOENT if someone else is canceling @work, this state may persist
1069 * for arbitrarily long
Tejun Heo36e227d2012-08-03 10:30:46 -07001070 *
Tejun Heobbb68df2012-08-03 10:30:46 -07001071 * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001072 * interrupted while holding PENDING and @work off queue, irq must be
1073 * disabled on entry. This, combined with delayed_work->timer being
1074 * irqsafe, ensures that we return -EAGAIN for finite short period of time.
Tejun Heobbb68df2012-08-03 10:30:46 -07001075 *
1076 * On successful return, >= 0, irq is disabled and the caller is
1077 * responsible for releasing it using local_irq_restore(*@flags).
1078 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001079 * This function is safe to call from any context including IRQ handler.
Tejun Heobf4ede02012-08-03 10:30:46 -07001080 */
Tejun Heobbb68df2012-08-03 10:30:46 -07001081static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
1082 unsigned long *flags)
Tejun Heobf4ede02012-08-03 10:30:46 -07001083{
Tejun Heod565ed62013-01-24 11:01:33 -08001084 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08001085 struct pool_workqueue *pwq;
Tejun Heobf4ede02012-08-03 10:30:46 -07001086
Tejun Heobbb68df2012-08-03 10:30:46 -07001087 local_irq_save(*flags);
1088
Tejun Heo36e227d2012-08-03 10:30:46 -07001089 /* try to steal the timer if it exists */
1090 if (is_dwork) {
1091 struct delayed_work *dwork = to_delayed_work(work);
1092
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001093 /*
1094 * dwork->timer is irqsafe. If del_timer() fails, it's
1095 * guaranteed that the timer is not queued anywhere and not
1096 * running on the local CPU.
1097 */
Tejun Heo36e227d2012-08-03 10:30:46 -07001098 if (likely(del_timer(&dwork->timer)))
1099 return 1;
1100 }
1101
1102 /* try to claim PENDING the normal way */
Tejun Heobf4ede02012-08-03 10:30:46 -07001103 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1104 return 0;
1105
1106 /*
1107 * The queueing is in progress, or it is already queued. Try to
1108 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1109 */
Tejun Heod565ed62013-01-24 11:01:33 -08001110 pool = get_work_pool(work);
1111 if (!pool)
Tejun Heobbb68df2012-08-03 10:30:46 -07001112 goto fail;
Tejun Heobf4ede02012-08-03 10:30:46 -07001113
Tejun Heod565ed62013-01-24 11:01:33 -08001114 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001115 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001116 * work->data is guaranteed to point to pwq only while the work
1117 * item is queued on pwq->wq, and both updating work->data to point
1118 * to pwq on queueing and to pool on dequeueing are done under
1119 * pwq->pool->lock. This in turn guarantees that, if work->data
1120 * points to pwq which is associated with a locked pool, the work
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001121 * item is currently queued on that pool.
1122 */
Tejun Heo112202d2013-02-13 19:29:12 -08001123 pwq = get_work_pwq(work);
1124 if (pwq && pwq->pool == pool) {
Tejun Heo16062832013-02-06 18:04:53 -08001125 debug_work_deactivate(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001126
Tejun Heo16062832013-02-06 18:04:53 -08001127 /*
1128 * A delayed work item cannot be grabbed directly because
1129 * it might have linked NO_COLOR work items which, if left
Tejun Heo112202d2013-02-13 19:29:12 -08001130 * on the delayed_list, will confuse pwq->nr_active
Tejun Heo16062832013-02-06 18:04:53 -08001131 * management later on and cause stall. Make sure the work
1132 * item is activated before grabbing.
1133 */
1134 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
Tejun Heo112202d2013-02-13 19:29:12 -08001135 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001136
Tejun Heo16062832013-02-06 18:04:53 -08001137 list_del_init(&work->entry);
Tejun Heo112202d2013-02-13 19:29:12 -08001138 pwq_dec_nr_in_flight(get_work_pwq(work), get_work_color(work));
Tejun Heo36e227d2012-08-03 10:30:46 -07001139
Tejun Heo112202d2013-02-13 19:29:12 -08001140 /* work->data points to pwq iff queued, point to pool */
Tejun Heo16062832013-02-06 18:04:53 -08001141 set_work_pool_and_keep_pending(work, pool->id);
Lai Jiangshan4468a002013-02-06 18:04:53 -08001142
Tejun Heo16062832013-02-06 18:04:53 -08001143 spin_unlock(&pool->lock);
1144 return 1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001145 }
Tejun Heod565ed62013-01-24 11:01:33 -08001146 spin_unlock(&pool->lock);
Tejun Heobbb68df2012-08-03 10:30:46 -07001147fail:
1148 local_irq_restore(*flags);
1149 if (work_is_canceling(work))
1150 return -ENOENT;
1151 cpu_relax();
Tejun Heo36e227d2012-08-03 10:30:46 -07001152 return -EAGAIN;
Tejun Heobf4ede02012-08-03 10:30:46 -07001153}
1154
1155/**
Tejun Heo706026c2013-01-24 11:01:34 -08001156 * insert_work - insert a work into a pool
Tejun Heo112202d2013-02-13 19:29:12 -08001157 * @pwq: pwq @work belongs to
Tejun Heo4690c4a2010-06-29 10:07:10 +02001158 * @work: work to insert
1159 * @head: insertion point
1160 * @extra_flags: extra WORK_STRUCT_* flags to set
1161 *
Tejun Heo112202d2013-02-13 19:29:12 -08001162 * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to
Tejun Heo706026c2013-01-24 11:01:34 -08001163 * work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +02001164 *
1165 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001166 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02001167 */
Tejun Heo112202d2013-02-13 19:29:12 -08001168static void insert_work(struct pool_workqueue *pwq, struct work_struct *work,
1169 struct list_head *head, unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001170{
Tejun Heo112202d2013-02-13 19:29:12 -08001171 struct worker_pool *pool = pwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01001172
Tejun Heo4690c4a2010-06-29 10:07:10 +02001173 /* we own @work, set data and link */
Tejun Heo112202d2013-02-13 19:29:12 -08001174 set_work_pwq(work, pwq, extra_flags);
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -07001175 list_add_tail(&work->entry, head);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001176 get_pwq(pwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02001177
1178 /*
1179 * Ensure either worker_sched_deactivated() sees the above
1180 * list_add_tail() or we see zero nr_running to avoid workers
1181 * lying around lazily while there are works to be processed.
1182 */
1183 smp_mb();
1184
Tejun Heo63d95a92012-07-12 14:46:37 -07001185 if (__need_more_worker(pool))
1186 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001187}
1188
Tejun Heoc8efcc22010-12-20 19:32:04 +01001189/*
1190 * Test whether @work is being queued from another work executing on the
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001191 * same workqueue.
Tejun Heoc8efcc22010-12-20 19:32:04 +01001192 */
1193static bool is_chained_work(struct workqueue_struct *wq)
1194{
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001195 struct worker *worker;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001196
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001197 worker = current_wq_worker();
1198 /*
1199 * Return %true iff I'm a worker execuing a work item on @wq. If
1200 * I'm @worker, it's safe to dereference it without locking.
1201 */
Tejun Heo112202d2013-02-13 19:29:12 -08001202 return worker && worker->current_pwq->wq == wq;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001203}
1204
Tejun Heod84ff052013-03-12 11:29:59 -07001205static void __queue_work(int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 struct work_struct *work)
1207{
Tejun Heo112202d2013-02-13 19:29:12 -08001208 struct pool_workqueue *pwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001209 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001210 unsigned int work_flags;
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001211 unsigned int req_cpu = cpu;
Tejun Heo8930cab2012-08-03 10:30:45 -07001212
1213 /*
1214 * While a work item is PENDING && off queue, a task trying to
1215 * steal the PENDING will busy-loop waiting for it to either get
1216 * queued or lose PENDING. Grabbing PENDING and queueing should
1217 * happen with IRQ disabled.
1218 */
1219 WARN_ON_ONCE(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001221 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001222
Tejun Heoc8efcc22010-12-20 19:32:04 +01001223 /* if dying, only works from the same workqueue are allowed */
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02001224 if (unlikely(wq->flags & WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001225 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001226 return;
1227
Tejun Heo112202d2013-02-13 19:29:12 -08001228 /* determine the pwq to use */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001229 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001230 struct worker_pool *last_pool;
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001231
Tejun Heo57469822012-08-03 10:30:45 -07001232 if (cpu == WORK_CPU_UNBOUND)
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001233 cpu = raw_smp_processor_id();
1234
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001235 /*
Tejun Heodbf25762012-08-20 14:51:23 -07001236 * It's multi cpu. If @work was previously on a different
1237 * cpu, it might still be running there, in which case the
1238 * work needs to be queued on that cpu to guarantee
1239 * non-reentrancy.
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001240 */
Tejun Heo7fb98ea2013-03-12 11:30:00 -07001241 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001242 last_pool = get_work_pool(work);
Tejun Heodbf25762012-08-20 14:51:23 -07001243
Tejun Heo112202d2013-02-13 19:29:12 -08001244 if (last_pool && last_pool != pwq->pool) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001245 struct worker *worker;
1246
Tejun Heod565ed62013-01-24 11:01:33 -08001247 spin_lock(&last_pool->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001248
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001249 worker = find_worker_executing_work(last_pool, work);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001250
Tejun Heo112202d2013-02-13 19:29:12 -08001251 if (worker && worker->current_pwq->wq == wq) {
Tejun Heo7fb98ea2013-03-12 11:30:00 -07001252 pwq = per_cpu_ptr(wq->cpu_pwqs, last_pool->cpu);
Lai Jiangshan8594fad2013-02-07 13:14:20 -08001253 } else {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001254 /* meh... not running there, queue here */
Tejun Heod565ed62013-01-24 11:01:33 -08001255 spin_unlock(&last_pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08001256 spin_lock(&pwq->pool->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001257 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001258 } else {
Tejun Heo112202d2013-02-13 19:29:12 -08001259 spin_lock(&pwq->pool->lock);
Tejun Heo8930cab2012-08-03 10:30:45 -07001260 }
Tejun Heof3421792010-07-02 10:03:51 +02001261 } else {
Tejun Heo7fb98ea2013-03-12 11:30:00 -07001262 pwq = first_pwq(wq);
Tejun Heo112202d2013-02-13 19:29:12 -08001263 spin_lock(&pwq->pool->lock);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001264 }
1265
Tejun Heo112202d2013-02-13 19:29:12 -08001266 /* pwq determined, queue */
1267 trace_workqueue_queue_work(req_cpu, pwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001268
Dan Carpenterf5b25522012-04-13 22:06:58 +03001269 if (WARN_ON(!list_empty(&work->entry))) {
Tejun Heo112202d2013-02-13 19:29:12 -08001270 spin_unlock(&pwq->pool->lock);
Dan Carpenterf5b25522012-04-13 22:06:58 +03001271 return;
1272 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001273
Tejun Heo112202d2013-02-13 19:29:12 -08001274 pwq->nr_in_flight[pwq->work_color]++;
1275 work_flags = work_color_to_flags(pwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001276
Tejun Heo112202d2013-02-13 19:29:12 -08001277 if (likely(pwq->nr_active < pwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001278 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001279 pwq->nr_active++;
1280 worklist = &pwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001281 } else {
1282 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo112202d2013-02-13 19:29:12 -08001283 worklist = &pwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001284 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001285
Tejun Heo112202d2013-02-13 19:29:12 -08001286 insert_work(pwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001287
Tejun Heo112202d2013-02-13 19:29:12 -08001288 spin_unlock(&pwq->pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289}
1290
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001291/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07001292 * queue_work_on - queue work on specific cpu
1293 * @cpu: CPU number to execute work on
1294 * @wq: workqueue to use
1295 * @work: work to queue
1296 *
Tejun Heod4283e92012-08-03 10:30:44 -07001297 * Returns %false if @work was already on a queue, %true otherwise.
Zhang Ruic1a220e2008-07-23 21:28:39 -07001298 *
1299 * We queue the work to a specific CPU, the caller must ensure it
1300 * can't go away.
1301 */
Tejun Heod4283e92012-08-03 10:30:44 -07001302bool queue_work_on(int cpu, struct workqueue_struct *wq,
1303 struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07001304{
Tejun Heod4283e92012-08-03 10:30:44 -07001305 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001306 unsigned long flags;
1307
1308 local_irq_save(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001309
Tejun Heo22df02b2010-06-29 10:07:10 +02001310 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001311 __queue_work(cpu, wq, work);
Tejun Heod4283e92012-08-03 10:30:44 -07001312 ret = true;
Zhang Ruic1a220e2008-07-23 21:28:39 -07001313 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001314
1315 local_irq_restore(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001316 return ret;
1317}
1318EXPORT_SYMBOL_GPL(queue_work_on);
1319
Tejun Heo0a13c002012-08-03 10:30:44 -07001320/**
1321 * queue_work - queue work on a workqueue
1322 * @wq: workqueue to use
1323 * @work: work to queue
1324 *
Tejun Heod4283e92012-08-03 10:30:44 -07001325 * Returns %false if @work was already on a queue, %true otherwise.
Tejun Heo0a13c002012-08-03 10:30:44 -07001326 *
1327 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1328 * it can be processed by another CPU.
1329 */
Tejun Heod4283e92012-08-03 10:30:44 -07001330bool queue_work(struct workqueue_struct *wq, struct work_struct *work)
Tejun Heo0a13c002012-08-03 10:30:44 -07001331{
Tejun Heo57469822012-08-03 10:30:45 -07001332 return queue_work_on(WORK_CPU_UNBOUND, wq, work);
Tejun Heo0a13c002012-08-03 10:30:44 -07001333}
1334EXPORT_SYMBOL_GPL(queue_work);
1335
Tejun Heod8e794d2012-08-03 10:30:45 -07001336void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
David Howells52bad642006-11-22 14:54:01 +00001338 struct delayed_work *dwork = (struct delayed_work *)__data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001340 /* should have been called from irqsafe timer with irq already off */
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001341 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342}
Konstantin Khlebnikov1438ade52013-01-24 16:36:31 +04001343EXPORT_SYMBOL(delayed_work_timer_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001345static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
1346 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001348 struct timer_list *timer = &dwork->timer;
1349 struct work_struct *work = &dwork->work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001351 WARN_ON_ONCE(timer->function != delayed_work_timer_fn ||
1352 timer->data != (unsigned long)dwork);
Tejun Heofc4b5142012-12-04 07:40:39 -08001353 WARN_ON_ONCE(timer_pending(timer));
1354 WARN_ON_ONCE(!list_empty(&work->entry));
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001355
Tejun Heo8852aac2012-12-01 16:23:42 -08001356 /*
1357 * If @delay is 0, queue @dwork->work immediately. This is for
1358 * both optimization and correctness. The earliest @timer can
1359 * expire is on the closest next tick and delayed_work users depend
1360 * on that there's no such delay when @delay is 0.
1361 */
1362 if (!delay) {
1363 __queue_work(cpu, wq, &dwork->work);
1364 return;
1365 }
1366
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001367 timer_stats_timer_set_start_info(&dwork->timer);
1368
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001369 dwork->wq = wq;
Tejun Heo12650572012-08-08 09:38:42 -07001370 dwork->cpu = cpu;
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001371 timer->expires = jiffies + delay;
1372
1373 if (unlikely(cpu != WORK_CPU_UNBOUND))
1374 add_timer_on(timer, cpu);
1375 else
1376 add_timer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377}
1378
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001379/**
1380 * queue_delayed_work_on - queue work on specific CPU after delay
1381 * @cpu: CPU number to execute work on
1382 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001383 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001384 * @delay: number of jiffies to wait before queueing
1385 *
Tejun Heo715f1302012-08-03 10:30:46 -07001386 * Returns %false if @work was already on a queue, %true otherwise. If
1387 * @delay is zero and @dwork is idle, it will be scheduled for immediate
1388 * execution.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001389 */
Tejun Heod4283e92012-08-03 10:30:44 -07001390bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
1391 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001392{
David Howells52bad642006-11-22 14:54:01 +00001393 struct work_struct *work = &dwork->work;
Tejun Heod4283e92012-08-03 10:30:44 -07001394 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001395 unsigned long flags;
1396
1397 /* read the comment in __queue_work() */
1398 local_irq_save(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001399
Tejun Heo22df02b2010-06-29 10:07:10 +02001400 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001401 __queue_delayed_work(cpu, wq, dwork, delay);
Tejun Heod4283e92012-08-03 10:30:44 -07001402 ret = true;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001403 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001404
1405 local_irq_restore(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001406 return ret;
1407}
Dave Jonesae90dd52006-06-30 01:40:45 -04001408EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Tejun Heoc8e55f32010-06-29 10:07:12 +02001410/**
Tejun Heo0a13c002012-08-03 10:30:44 -07001411 * queue_delayed_work - queue work on a workqueue after delay
1412 * @wq: workqueue to use
1413 * @dwork: delayable work to queue
1414 * @delay: number of jiffies to wait before queueing
1415 *
Tejun Heo715f1302012-08-03 10:30:46 -07001416 * Equivalent to queue_delayed_work_on() but tries to use the local CPU.
Tejun Heo0a13c002012-08-03 10:30:44 -07001417 */
Tejun Heod4283e92012-08-03 10:30:44 -07001418bool queue_delayed_work(struct workqueue_struct *wq,
Tejun Heo0a13c002012-08-03 10:30:44 -07001419 struct delayed_work *dwork, unsigned long delay)
1420{
Tejun Heo57469822012-08-03 10:30:45 -07001421 return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
Tejun Heo0a13c002012-08-03 10:30:44 -07001422}
1423EXPORT_SYMBOL_GPL(queue_delayed_work);
1424
1425/**
Tejun Heo8376fe22012-08-03 10:30:47 -07001426 * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
1427 * @cpu: CPU number to execute work on
1428 * @wq: workqueue to use
1429 * @dwork: work to queue
1430 * @delay: number of jiffies to wait before queueing
1431 *
1432 * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
1433 * modify @dwork's timer so that it expires after @delay. If @delay is
1434 * zero, @work is guaranteed to be scheduled immediately regardless of its
1435 * current state.
1436 *
1437 * Returns %false if @dwork was idle and queued, %true if @dwork was
1438 * pending and its timer was modified.
1439 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001440 * This function is safe to call from any context including IRQ handler.
Tejun Heo8376fe22012-08-03 10:30:47 -07001441 * See try_to_grab_pending() for details.
1442 */
1443bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
1444 struct delayed_work *dwork, unsigned long delay)
1445{
1446 unsigned long flags;
1447 int ret;
1448
1449 do {
1450 ret = try_to_grab_pending(&dwork->work, true, &flags);
1451 } while (unlikely(ret == -EAGAIN));
1452
1453 if (likely(ret >= 0)) {
1454 __queue_delayed_work(cpu, wq, dwork, delay);
1455 local_irq_restore(flags);
1456 }
1457
1458 /* -ENOENT from try_to_grab_pending() becomes %true */
1459 return ret;
1460}
1461EXPORT_SYMBOL_GPL(mod_delayed_work_on);
1462
1463/**
1464 * mod_delayed_work - modify delay of or queue a delayed work
1465 * @wq: workqueue to use
1466 * @dwork: work to queue
1467 * @delay: number of jiffies to wait before queueing
1468 *
1469 * mod_delayed_work_on() on local CPU.
1470 */
1471bool mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dwork,
1472 unsigned long delay)
1473{
1474 return mod_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
1475}
1476EXPORT_SYMBOL_GPL(mod_delayed_work);
1477
1478/**
Tejun Heoc8e55f32010-06-29 10:07:12 +02001479 * worker_enter_idle - enter idle state
1480 * @worker: worker which is entering idle state
1481 *
1482 * @worker is entering idle state. Update stats and idle timer if
1483 * necessary.
1484 *
1485 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001486 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001487 */
1488static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001490 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Tejun Heo6183c002013-03-12 11:29:57 -07001492 if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) ||
1493 WARN_ON_ONCE(!list_empty(&worker->entry) &&
1494 (worker->hentry.next || worker->hentry.pprev)))
1495 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Tejun Heocb444762010-07-02 10:03:50 +02001497 /* can't use worker_set_flags(), also called from start_worker() */
1498 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001499 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001500 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001501
Tejun Heoc8e55f32010-06-29 10:07:12 +02001502 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001503 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001504
Tejun Heo628c78e2012-07-17 12:39:27 -07001505 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1506 mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
Tejun Heocb444762010-07-02 10:03:50 +02001507
Tejun Heo544ecf32012-05-14 15:04:50 -07001508 /*
Tejun Heo706026c2013-01-24 11:01:34 -08001509 * Sanity check nr_running. Because wq_unbind_fn() releases
Tejun Heod565ed62013-01-24 11:01:33 -08001510 * pool->lock between setting %WORKER_UNBOUND and zapping
Tejun Heo628c78e2012-07-17 12:39:27 -07001511 * nr_running, the warning may trigger spuriously. Check iff
1512 * unbind is not in progress.
Tejun Heo544ecf32012-05-14 15:04:50 -07001513 */
Tejun Heo24647572013-01-24 11:01:33 -08001514 WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001515 pool->nr_workers == pool->nr_idle &&
Tejun Heoe19e3972013-01-24 11:39:44 -08001516 atomic_read(&pool->nr_running));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517}
1518
Tejun Heoc8e55f32010-06-29 10:07:12 +02001519/**
1520 * worker_leave_idle - leave idle state
1521 * @worker: worker which is leaving idle state
1522 *
1523 * @worker is leaving idle state. Update stats.
1524 *
1525 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001526 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001527 */
1528static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001530 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Tejun Heo6183c002013-03-12 11:29:57 -07001532 if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE)))
1533 return;
Tejun Heod302f012010-06-29 10:07:13 +02001534 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001535 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001536 list_del_init(&worker->entry);
1537}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Tejun Heoe22bee72010-06-29 10:07:14 +02001539/**
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001540 * worker_maybe_bind_and_lock - try to bind %current to worker_pool and lock it
1541 * @pool: target worker_pool
1542 *
1543 * Bind %current to the cpu of @pool if it is associated and lock @pool.
Tejun Heoe22bee72010-06-29 10:07:14 +02001544 *
1545 * Works which are scheduled while the cpu is online must at least be
1546 * scheduled to a worker which is bound to the cpu so that if they are
1547 * flushed from cpu callbacks while cpu is going down, they are
1548 * guaranteed to execute on the cpu.
1549 *
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001550 * This function is to be used by unbound workers and rescuers to bind
Tejun Heoe22bee72010-06-29 10:07:14 +02001551 * themselves to the target cpu and may race with cpu going down or
1552 * coming online. kthread_bind() can't be used because it may put the
1553 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
Tejun Heo706026c2013-01-24 11:01:34 -08001554 * verbatim as it's best effort and blocking and pool may be
Tejun Heoe22bee72010-06-29 10:07:14 +02001555 * [dis]associated in the meantime.
1556 *
Tejun Heo706026c2013-01-24 11:01:34 -08001557 * This function tries set_cpus_allowed() and locks pool and verifies the
Tejun Heo24647572013-01-24 11:01:33 -08001558 * binding against %POOL_DISASSOCIATED which is set during
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001559 * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1560 * enters idle state or fetches works without dropping lock, it can
1561 * guarantee the scheduling requirement described in the first paragraph.
Tejun Heoe22bee72010-06-29 10:07:14 +02001562 *
1563 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001564 * Might sleep. Called without any lock but returns with pool->lock
Tejun Heoe22bee72010-06-29 10:07:14 +02001565 * held.
1566 *
1567 * RETURNS:
Tejun Heo706026c2013-01-24 11:01:34 -08001568 * %true if the associated pool is online (@worker is successfully
Tejun Heoe22bee72010-06-29 10:07:14 +02001569 * bound), %false if offline.
1570 */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001571static bool worker_maybe_bind_and_lock(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001572__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001573{
Tejun Heoe22bee72010-06-29 10:07:14 +02001574 while (true) {
1575 /*
1576 * The following call may fail, succeed or succeed
1577 * without actually migrating the task to the cpu if
1578 * it races with cpu hotunplug operation. Verify
Tejun Heo24647572013-01-24 11:01:33 -08001579 * against POOL_DISASSOCIATED.
Tejun Heoe22bee72010-06-29 10:07:14 +02001580 */
Tejun Heo24647572013-01-24 11:01:33 -08001581 if (!(pool->flags & POOL_DISASSOCIATED))
Tejun Heo7a4e3442013-03-12 11:30:00 -07001582 set_cpus_allowed_ptr(current, pool->attrs->cpumask);
Oleg Nesterov85f41862007-05-09 02:34:20 -07001583
Tejun Heod565ed62013-01-24 11:01:33 -08001584 spin_lock_irq(&pool->lock);
Tejun Heo24647572013-01-24 11:01:33 -08001585 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heoe22bee72010-06-29 10:07:14 +02001586 return false;
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001587 if (task_cpu(current) == pool->cpu &&
Tejun Heo7a4e3442013-03-12 11:30:00 -07001588 cpumask_equal(&current->cpus_allowed, pool->attrs->cpumask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001589 return true;
Tejun Heod565ed62013-01-24 11:01:33 -08001590 spin_unlock_irq(&pool->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001591
Tejun Heo5035b202011-04-29 18:08:37 +02001592 /*
1593 * We've raced with CPU hot[un]plug. Give it a breather
1594 * and retry migration. cond_resched() is required here;
1595 * otherwise, we might deadlock against cpu_stop trying to
1596 * bring down the CPU on non-preemptive kernel.
1597 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001598 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001599 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001600 }
1601}
1602
1603/*
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001604 * Rebind an idle @worker to its CPU. worker_thread() will test
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001605 * list_empty(@worker->entry) before leaving idle and call this function.
Tejun Heo25511a42012-07-17 12:39:27 -07001606 */
1607static void idle_worker_rebind(struct worker *worker)
1608{
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001609 /* CPU may go down again inbetween, clear UNBOUND only on success */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001610 if (worker_maybe_bind_and_lock(worker->pool))
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001611 worker_clr_flags(worker, WORKER_UNBOUND);
Tejun Heo25511a42012-07-17 12:39:27 -07001612
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001613 /* rebind complete, become available again */
1614 list_add(&worker->entry, &worker->pool->idle_list);
Tejun Heod565ed62013-01-24 11:01:33 -08001615 spin_unlock_irq(&worker->pool->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07001616}
1617
1618/*
1619 * Function for @worker->rebind.work used to rebind unbound busy workers to
Tejun Heo403c8212012-07-17 12:39:27 -07001620 * the associated cpu which is coming back online. This is scheduled by
1621 * cpu up but can race with other cpu hotplug operations and may be
1622 * executed twice without intervening cpu down.
Tejun Heoe22bee72010-06-29 10:07:14 +02001623 */
Tejun Heo25511a42012-07-17 12:39:27 -07001624static void busy_worker_rebind_fn(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001625{
1626 struct worker *worker = container_of(work, struct worker, rebind_work);
Tejun Heoe22bee72010-06-29 10:07:14 +02001627
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001628 if (worker_maybe_bind_and_lock(worker->pool))
Lai Jiangshaneab6d822012-09-18 09:59:22 -07001629 worker_clr_flags(worker, WORKER_UNBOUND);
Tejun Heoe22bee72010-06-29 10:07:14 +02001630
Tejun Heod565ed62013-01-24 11:01:33 -08001631 spin_unlock_irq(&worker->pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001632}
1633
Tejun Heo25511a42012-07-17 12:39:27 -07001634/**
Tejun Heo94cf58b2013-01-24 11:01:33 -08001635 * rebind_workers - rebind all workers of a pool to the associated CPU
1636 * @pool: pool of interest
Tejun Heo25511a42012-07-17 12:39:27 -07001637 *
Tejun Heo94cf58b2013-01-24 11:01:33 -08001638 * @pool->cpu is coming online. Rebind all workers to the CPU. Rebinding
Tejun Heo25511a42012-07-17 12:39:27 -07001639 * is different for idle and busy ones.
1640 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001641 * Idle ones will be removed from the idle_list and woken up. They will
1642 * add themselves back after completing rebind. This ensures that the
1643 * idle_list doesn't contain any unbound workers when re-bound busy workers
1644 * try to perform local wake-ups for concurrency management.
Tejun Heo25511a42012-07-17 12:39:27 -07001645 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001646 * Busy workers can rebind after they finish their current work items.
1647 * Queueing the rebind work item at the head of the scheduled list is
1648 * enough. Note that nr_running will be properly bumped as busy workers
1649 * rebind.
Tejun Heo25511a42012-07-17 12:39:27 -07001650 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001651 * On return, all non-manager workers are scheduled for rebind - see
1652 * manage_workers() for the manager special case. Any idle worker
1653 * including the manager will not appear on @idle_list until rebind is
1654 * complete, making local wake-ups safe.
Tejun Heo25511a42012-07-17 12:39:27 -07001655 */
Tejun Heo94cf58b2013-01-24 11:01:33 -08001656static void rebind_workers(struct worker_pool *pool)
Tejun Heo25511a42012-07-17 12:39:27 -07001657{
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001658 struct worker *worker, *n;
Tejun Heo25511a42012-07-17 12:39:27 -07001659 int i;
1660
Tejun Heo94cf58b2013-01-24 11:01:33 -08001661 lockdep_assert_held(&pool->assoc_mutex);
1662 lockdep_assert_held(&pool->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07001663
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001664 /* dequeue and kick idle ones */
Tejun Heo94cf58b2013-01-24 11:01:33 -08001665 list_for_each_entry_safe(worker, n, &pool->idle_list, entry) {
1666 /*
1667 * idle workers should be off @pool->idle_list until rebind
1668 * is complete to avoid receiving premature local wake-ups.
1669 */
1670 list_del_init(&worker->entry);
Lai Jiangshan96e65302012-09-02 00:28:19 +08001671
Tejun Heo94cf58b2013-01-24 11:01:33 -08001672 /*
1673 * worker_thread() will see the above dequeuing and call
1674 * idle_worker_rebind().
1675 */
1676 wake_up_process(worker->task);
1677 }
Tejun Heo25511a42012-07-17 12:39:27 -07001678
Tejun Heo94cf58b2013-01-24 11:01:33 -08001679 /* rebind busy workers */
Sasha Levinb67bfe02013-02-27 17:06:00 -08001680 for_each_busy_worker(worker, i, pool) {
Tejun Heo94cf58b2013-01-24 11:01:33 -08001681 struct work_struct *rebind_work = &worker->rebind_work;
1682 struct workqueue_struct *wq;
Tejun Heo25511a42012-07-17 12:39:27 -07001683
Tejun Heo94cf58b2013-01-24 11:01:33 -08001684 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
1685 work_data_bits(rebind_work)))
1686 continue;
Tejun Heo25511a42012-07-17 12:39:27 -07001687
Tejun Heo94cf58b2013-01-24 11:01:33 -08001688 debug_work_activate(rebind_work);
Tejun Heo90beca52012-09-04 23:12:33 -07001689
Tejun Heo94cf58b2013-01-24 11:01:33 -08001690 /*
1691 * wq doesn't really matter but let's keep @worker->pool
Tejun Heo112202d2013-02-13 19:29:12 -08001692 * and @pwq->pool consistent for sanity.
Tejun Heo94cf58b2013-01-24 11:01:33 -08001693 */
Tejun Heo7a4e3442013-03-12 11:30:00 -07001694 if (worker->pool->attrs->nice < 0)
Tejun Heo94cf58b2013-01-24 11:01:33 -08001695 wq = system_highpri_wq;
1696 else
1697 wq = system_wq;
Tejun Heoec588152012-09-04 23:16:32 -07001698
Tejun Heo7fb98ea2013-03-12 11:30:00 -07001699 insert_work(per_cpu_ptr(wq->cpu_pwqs, pool->cpu), rebind_work,
Tejun Heo94cf58b2013-01-24 11:01:33 -08001700 worker->scheduled.next,
1701 work_color_to_flags(WORK_NO_COLOR));
Tejun Heoec588152012-09-04 23:16:32 -07001702 }
Tejun Heo25511a42012-07-17 12:39:27 -07001703}
1704
Tejun Heoc34056a2010-06-29 10:07:11 +02001705static struct worker *alloc_worker(void)
1706{
1707 struct worker *worker;
1708
1709 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001710 if (worker) {
1711 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001712 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heo25511a42012-07-17 12:39:27 -07001713 INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn);
Tejun Heoe22bee72010-06-29 10:07:14 +02001714 /* on creation a worker is in !idle && prep state */
1715 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001716 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001717 return worker;
1718}
1719
1720/**
1721 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001722 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001723 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001724 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001725 * can be started by calling start_worker() or destroyed using
1726 * destroy_worker().
1727 *
1728 * CONTEXT:
1729 * Might sleep. Does GFP_KERNEL allocations.
1730 *
1731 * RETURNS:
1732 * Pointer to the newly created worker.
1733 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001734static struct worker *create_worker(struct worker_pool *pool)
Tejun Heoc34056a2010-06-29 10:07:11 +02001735{
Tejun Heo7a4e3442013-03-12 11:30:00 -07001736 const char *pri = pool->attrs->nice < 0 ? "H" : "";
Tejun Heoc34056a2010-06-29 10:07:11 +02001737 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001738 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001739
Tejun Heod565ed62013-01-24 11:01:33 -08001740 spin_lock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001741 while (ida_get_new(&pool->worker_ida, &id)) {
Tejun Heod565ed62013-01-24 11:01:33 -08001742 spin_unlock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001743 if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001744 goto fail;
Tejun Heod565ed62013-01-24 11:01:33 -08001745 spin_lock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001746 }
Tejun Heod565ed62013-01-24 11:01:33 -08001747 spin_unlock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001748
1749 worker = alloc_worker();
1750 if (!worker)
1751 goto fail;
1752
Tejun Heobd7bdd42012-07-12 14:46:37 -07001753 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001754 worker->id = id;
1755
Tejun Heo29c91e92013-03-12 11:30:03 -07001756 if (pool->cpu >= 0)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001757 worker->task = kthread_create_on_node(worker_thread,
Tejun Heoec22ca52013-01-24 11:01:33 -08001758 worker, cpu_to_node(pool->cpu),
Tejun Heod84ff052013-03-12 11:29:59 -07001759 "kworker/%d:%d%s", pool->cpu, id, pri);
Tejun Heof3421792010-07-02 10:03:51 +02001760 else
1761 worker->task = kthread_create(worker_thread, worker,
Tejun Heoac6104c2013-03-12 11:30:03 -07001762 "kworker/u%d:%d%s",
1763 pool->id, id, pri);
Tejun Heoc34056a2010-06-29 10:07:11 +02001764 if (IS_ERR(worker->task))
1765 goto fail;
1766
Tejun Heo7a4e3442013-03-12 11:30:00 -07001767 set_user_nice(worker->task, pool->attrs->nice);
1768 set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask);
Tejun Heo32704762012-07-13 22:16:45 -07001769
Tejun Heodb7bccf2010-06-29 10:07:12 +02001770 /*
Tejun Heo7a4e3442013-03-12 11:30:00 -07001771 * %PF_THREAD_BOUND is used to prevent userland from meddling with
1772 * cpumask of workqueue workers. This is an abuse. We need
1773 * %PF_NO_SETAFFINITY.
Tejun Heodb7bccf2010-06-29 10:07:12 +02001774 */
Tejun Heo7a4e3442013-03-12 11:30:00 -07001775 worker->task->flags |= PF_THREAD_BOUND;
1776
1777 /*
1778 * The caller is responsible for ensuring %POOL_DISASSOCIATED
1779 * remains stable across this function. See the comments above the
1780 * flag definition for details.
1781 */
1782 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001783 worker->flags |= WORKER_UNBOUND;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001784
Tejun Heoc34056a2010-06-29 10:07:11 +02001785 return worker;
1786fail:
1787 if (id >= 0) {
Tejun Heod565ed62013-01-24 11:01:33 -08001788 spin_lock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001789 ida_remove(&pool->worker_ida, id);
Tejun Heod565ed62013-01-24 11:01:33 -08001790 spin_unlock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001791 }
1792 kfree(worker);
1793 return NULL;
1794}
1795
1796/**
1797 * start_worker - start a newly created worker
1798 * @worker: worker to start
1799 *
Tejun Heo706026c2013-01-24 11:01:34 -08001800 * Make the pool aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001801 *
1802 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001803 * spin_lock_irq(pool->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001804 */
1805static void start_worker(struct worker *worker)
1806{
Tejun Heocb444762010-07-02 10:03:50 +02001807 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001808 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001809 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001810 wake_up_process(worker->task);
1811}
1812
1813/**
1814 * destroy_worker - destroy a workqueue worker
1815 * @worker: worker to be destroyed
1816 *
Tejun Heo706026c2013-01-24 11:01:34 -08001817 * Destroy @worker and adjust @pool stats accordingly.
Tejun Heoc8e55f32010-06-29 10:07:12 +02001818 *
1819 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001820 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001821 */
1822static void destroy_worker(struct worker *worker)
1823{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001824 struct worker_pool *pool = worker->pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001825 int id = worker->id;
1826
1827 /* sanity check frenzy */
Tejun Heo6183c002013-03-12 11:29:57 -07001828 if (WARN_ON(worker->current_work) ||
1829 WARN_ON(!list_empty(&worker->scheduled)))
1830 return;
Tejun Heoc34056a2010-06-29 10:07:11 +02001831
Tejun Heoc8e55f32010-06-29 10:07:12 +02001832 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001833 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001834 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001835 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001836
1837 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001838 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001839
Tejun Heod565ed62013-01-24 11:01:33 -08001840 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001841
Tejun Heoc34056a2010-06-29 10:07:11 +02001842 kthread_stop(worker->task);
1843 kfree(worker);
1844
Tejun Heod565ed62013-01-24 11:01:33 -08001845 spin_lock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001846 ida_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001847}
1848
Tejun Heo63d95a92012-07-12 14:46:37 -07001849static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001850{
Tejun Heo63d95a92012-07-12 14:46:37 -07001851 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001852
Tejun Heod565ed62013-01-24 11:01:33 -08001853 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001854
Tejun Heo63d95a92012-07-12 14:46:37 -07001855 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001856 struct worker *worker;
1857 unsigned long expires;
1858
1859 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001860 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001861 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1862
1863 if (time_before(jiffies, expires))
Tejun Heo63d95a92012-07-12 14:46:37 -07001864 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001865 else {
1866 /* it's been idle for too long, wake up manager */
Tejun Heo11ebea52012-07-12 14:46:37 -07001867 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -07001868 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001869 }
1870 }
1871
Tejun Heod565ed62013-01-24 11:01:33 -08001872 spin_unlock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001873}
1874
Tejun Heo493a1722013-03-12 11:29:59 -07001875static void send_mayday(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001876{
Tejun Heo112202d2013-02-13 19:29:12 -08001877 struct pool_workqueue *pwq = get_work_pwq(work);
1878 struct workqueue_struct *wq = pwq->wq;
Tejun Heo493a1722013-03-12 11:29:59 -07001879
1880 lockdep_assert_held(&workqueue_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001881
Tejun Heo493008a2013-03-12 11:30:03 -07001882 if (!wq->rescuer)
Tejun Heo493a1722013-03-12 11:29:59 -07001883 return;
Tejun Heoe22bee72010-06-29 10:07:14 +02001884
1885 /* mayday mayday mayday */
Tejun Heo493a1722013-03-12 11:29:59 -07001886 if (list_empty(&pwq->mayday_node)) {
1887 list_add_tail(&pwq->mayday_node, &wq->maydays);
Tejun Heoe22bee72010-06-29 10:07:14 +02001888 wake_up_process(wq->rescuer->task);
Tejun Heo493a1722013-03-12 11:29:59 -07001889 }
Tejun Heoe22bee72010-06-29 10:07:14 +02001890}
1891
Tejun Heo706026c2013-01-24 11:01:34 -08001892static void pool_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001893{
Tejun Heo63d95a92012-07-12 14:46:37 -07001894 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001895 struct work_struct *work;
1896
Tejun Heo493a1722013-03-12 11:29:59 -07001897 spin_lock_irq(&workqueue_lock); /* for wq->maydays */
1898 spin_lock(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001899
Tejun Heo63d95a92012-07-12 14:46:37 -07001900 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001901 /*
1902 * We've been trying to create a new worker but
1903 * haven't been successful. We might be hitting an
1904 * allocation deadlock. Send distress signals to
1905 * rescuers.
1906 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001907 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001908 send_mayday(work);
1909 }
1910
Tejun Heo493a1722013-03-12 11:29:59 -07001911 spin_unlock(&pool->lock);
1912 spin_unlock_irq(&workqueue_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001913
Tejun Heo63d95a92012-07-12 14:46:37 -07001914 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001915}
1916
1917/**
1918 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001919 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001920 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001921 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001922 * have at least one idle worker on return from this function. If
1923 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001924 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001925 * possible allocation deadlock.
1926 *
1927 * On return, need_to_create_worker() is guaranteed to be false and
1928 * may_start_working() true.
1929 *
1930 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001931 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001932 * multiple times. Does GFP_KERNEL allocations. Called only from
1933 * manager.
1934 *
1935 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08001936 * false if no action was taken and pool->lock stayed locked, true
Tejun Heoe22bee72010-06-29 10:07:14 +02001937 * otherwise.
1938 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001939static bool maybe_create_worker(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001940__releases(&pool->lock)
1941__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001942{
Tejun Heo63d95a92012-07-12 14:46:37 -07001943 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001944 return false;
1945restart:
Tejun Heod565ed62013-01-24 11:01:33 -08001946 spin_unlock_irq(&pool->lock);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001947
Tejun Heoe22bee72010-06-29 10:07:14 +02001948 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07001949 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001950
1951 while (true) {
1952 struct worker *worker;
1953
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001954 worker = create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001955 if (worker) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001956 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001957 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001958 start_worker(worker);
Tejun Heo6183c002013-03-12 11:29:57 -07001959 if (WARN_ON_ONCE(need_to_create_worker(pool)))
1960 goto restart;
Tejun Heoe22bee72010-06-29 10:07:14 +02001961 return true;
1962 }
1963
Tejun Heo63d95a92012-07-12 14:46:37 -07001964 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001965 break;
1966
Tejun Heoe22bee72010-06-29 10:07:14 +02001967 __set_current_state(TASK_INTERRUPTIBLE);
1968 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001969
Tejun Heo63d95a92012-07-12 14:46:37 -07001970 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001971 break;
1972 }
1973
Tejun Heo63d95a92012-07-12 14:46:37 -07001974 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001975 spin_lock_irq(&pool->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07001976 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001977 goto restart;
1978 return true;
1979}
1980
1981/**
1982 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo63d95a92012-07-12 14:46:37 -07001983 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001984 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001985 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02001986 * IDLE_WORKER_TIMEOUT.
1987 *
1988 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001989 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001990 * multiple times. Called only from manager.
1991 *
1992 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08001993 * false if no action was taken and pool->lock stayed locked, true
Tejun Heoe22bee72010-06-29 10:07:14 +02001994 * otherwise.
1995 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001996static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001997{
1998 bool ret = false;
1999
Tejun Heo63d95a92012-07-12 14:46:37 -07002000 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02002001 struct worker *worker;
2002 unsigned long expires;
2003
Tejun Heo63d95a92012-07-12 14:46:37 -07002004 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02002005 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
2006
2007 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07002008 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02002009 break;
2010 }
2011
2012 destroy_worker(worker);
2013 ret = true;
2014 }
2015
2016 return ret;
2017}
2018
2019/**
2020 * manage_workers - manage worker pool
2021 * @worker: self
2022 *
Tejun Heo706026c2013-01-24 11:01:34 -08002023 * Assume the manager role and manage the worker pool @worker belongs
Tejun Heoe22bee72010-06-29 10:07:14 +02002024 * to. At any given time, there can be only zero or one manager per
Tejun Heo706026c2013-01-24 11:01:34 -08002025 * pool. The exclusion is handled automatically by this function.
Tejun Heoe22bee72010-06-29 10:07:14 +02002026 *
2027 * The caller can safely start processing works on false return. On
2028 * true return, it's guaranteed that need_to_create_worker() is false
2029 * and may_start_working() is true.
2030 *
2031 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002032 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02002033 * multiple times. Does GFP_KERNEL allocations.
2034 *
2035 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08002036 * spin_lock_irq(pool->lock) which may be released and regrabbed
2037 * multiple times. Does GFP_KERNEL allocations.
Tejun Heoe22bee72010-06-29 10:07:14 +02002038 */
2039static bool manage_workers(struct worker *worker)
2040{
Tejun Heo63d95a92012-07-12 14:46:37 -07002041 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002042 bool ret = false;
2043
Tejun Heo34a06bd2013-03-12 11:30:00 -07002044 if (!mutex_trylock(&pool->manager_arb))
Tejun Heoe22bee72010-06-29 10:07:14 +02002045 return ret;
2046
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002047 /*
2048 * To simplify both worker management and CPU hotplug, hold off
2049 * management while hotplug is in progress. CPU hotplug path can't
Tejun Heo34a06bd2013-03-12 11:30:00 -07002050 * grab @pool->manager_arb to achieve this because that can lead to
2051 * idle worker depletion (all become busy thinking someone else is
2052 * managing) which in turn can result in deadlock under extreme
2053 * circumstances. Use @pool->assoc_mutex to synchronize manager
2054 * against CPU hotplug.
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002055 *
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002056 * assoc_mutex would always be free unless CPU hotplug is in
Tejun Heod565ed62013-01-24 11:01:33 -08002057 * progress. trylock first without dropping @pool->lock.
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002058 */
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002059 if (unlikely(!mutex_trylock(&pool->assoc_mutex))) {
Tejun Heod565ed62013-01-24 11:01:33 -08002060 spin_unlock_irq(&pool->lock);
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002061 mutex_lock(&pool->assoc_mutex);
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002062 /*
2063 * CPU hotplug could have happened while we were waiting
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002064 * for assoc_mutex. Hotplug itself can't handle us
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002065 * because manager isn't either on idle or busy list, and
Tejun Heo706026c2013-01-24 11:01:34 -08002066 * @pool's state and ours could have deviated.
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002067 *
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002068 * As hotplug is now excluded via assoc_mutex, we can
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002069 * simply try to bind. It will succeed or fail depending
Tejun Heo706026c2013-01-24 11:01:34 -08002070 * on @pool's current state. Try it and adjust
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002071 * %WORKER_UNBOUND accordingly.
2072 */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08002073 if (worker_maybe_bind_and_lock(pool))
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002074 worker->flags &= ~WORKER_UNBOUND;
2075 else
2076 worker->flags |= WORKER_UNBOUND;
2077
2078 ret = true;
2079 }
2080
Tejun Heo11ebea52012-07-12 14:46:37 -07002081 pool->flags &= ~POOL_MANAGE_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02002082
2083 /*
2084 * Destroy and then create so that may_start_working() is true
2085 * on return.
2086 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002087 ret |= maybe_destroy_workers(pool);
2088 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002089
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002090 mutex_unlock(&pool->assoc_mutex);
Tejun Heo34a06bd2013-03-12 11:30:00 -07002091 mutex_unlock(&pool->manager_arb);
Tejun Heoe22bee72010-06-29 10:07:14 +02002092 return ret;
2093}
2094
Tejun Heoa62428c2010-06-29 10:07:10 +02002095/**
2096 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02002097 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02002098 * @work: work to process
2099 *
2100 * Process @work. This function contains all the logics necessary to
2101 * process a single work including synchronization against and
2102 * interaction with other workers on the same cpu, queueing and
2103 * flushing. As long as context requirement is met, any worker can
2104 * call this function to process a work.
2105 *
2106 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002107 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02002108 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002109static void process_one_work(struct worker *worker, struct work_struct *work)
Tejun Heod565ed62013-01-24 11:01:33 -08002110__releases(&pool->lock)
2111__acquires(&pool->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02002112{
Tejun Heo112202d2013-02-13 19:29:12 -08002113 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002114 struct worker_pool *pool = worker->pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002115 bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02002116 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02002117 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02002118#ifdef CONFIG_LOCKDEP
2119 /*
2120 * It is permissible to free the struct work_struct from
2121 * inside the function that is called from it, this we need to
2122 * take into account for lockdep too. To avoid bogus "held
2123 * lock freed" warnings as well as problems when looking into
2124 * work->lockdep_map, make a copy and use that here.
2125 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07002126 struct lockdep_map lockdep_map;
2127
2128 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002129#endif
Tejun Heo6fec10a2012-07-22 10:16:34 -07002130 /*
2131 * Ensure we're on the correct CPU. DISASSOCIATED test is
2132 * necessary to avoid spurious warnings from rescuers servicing the
Tejun Heo24647572013-01-24 11:01:33 -08002133 * unbound or a disassociated pool.
Tejun Heo6fec10a2012-07-22 10:16:34 -07002134 */
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002135 WARN_ON_ONCE(!(worker->flags & WORKER_UNBOUND) &&
Tejun Heo24647572013-01-24 11:01:33 -08002136 !(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heoec22ca52013-01-24 11:01:33 -08002137 raw_smp_processor_id() != pool->cpu);
Tejun Heo25511a42012-07-17 12:39:27 -07002138
Tejun Heo7e116292010-06-29 10:07:13 +02002139 /*
2140 * A single work shouldn't be executed concurrently by
2141 * multiple workers on a single cpu. Check whether anyone is
2142 * already processing the work. If so, defer the work to the
2143 * currently executing one.
2144 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002145 collision = find_worker_executing_work(pool, work);
Tejun Heo7e116292010-06-29 10:07:13 +02002146 if (unlikely(collision)) {
2147 move_linked_works(work, &collision->scheduled, NULL);
2148 return;
2149 }
2150
Tejun Heo8930cab2012-08-03 10:30:45 -07002151 /* claim and dequeue */
Tejun Heoa62428c2010-06-29 10:07:10 +02002152 debug_work_deactivate(work);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002153 hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work);
Tejun Heoc34056a2010-06-29 10:07:11 +02002154 worker->current_work = work;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002155 worker->current_func = work->func;
Tejun Heo112202d2013-02-13 19:29:12 -08002156 worker->current_pwq = pwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002157 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002158
Tejun Heoa62428c2010-06-29 10:07:10 +02002159 list_del_init(&work->entry);
2160
Tejun Heo649027d2010-06-29 10:07:14 +02002161 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02002162 * CPU intensive works don't participate in concurrency
2163 * management. They're the scheduler's responsibility.
2164 */
2165 if (unlikely(cpu_intensive))
2166 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
2167
Tejun Heo974271c2012-07-12 14:46:37 -07002168 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002169 * Unbound pool isn't concurrency managed and work items should be
Tejun Heo974271c2012-07-12 14:46:37 -07002170 * executed ASAP. Wake up another worker if necessary.
2171 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002172 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
2173 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07002174
Tejun Heo8930cab2012-08-03 10:30:45 -07002175 /*
Tejun Heo7c3eed52013-01-24 11:01:33 -08002176 * Record the last pool and clear PENDING which should be the last
Tejun Heod565ed62013-01-24 11:01:33 -08002177 * update to @work. Also, do this inside @pool->lock so that
Tejun Heo23657bb2012-08-13 17:08:19 -07002178 * PENDING and queued state changes happen together while IRQ is
2179 * disabled.
Tejun Heo8930cab2012-08-03 10:30:45 -07002180 */
Tejun Heo7c3eed52013-01-24 11:01:33 -08002181 set_work_pool_and_clear_pending(work, pool->id);
Tejun Heoa62428c2010-06-29 10:07:10 +02002182
Tejun Heod565ed62013-01-24 11:01:33 -08002183 spin_unlock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002184
Tejun Heo112202d2013-02-13 19:29:12 -08002185 lock_map_acquire_read(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002186 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002187 trace_workqueue_execute_start(work);
Tejun Heoa2c1c572012-12-18 10:35:02 -08002188 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002189 /*
2190 * While we must be careful to not use "work" after this, the trace
2191 * point will only record its address.
2192 */
2193 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002194 lock_map_release(&lockdep_map);
Tejun Heo112202d2013-02-13 19:29:12 -08002195 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002196
2197 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Valentin Ilie044c7822012-08-19 00:52:42 +03002198 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
2199 " last function: %pf\n",
Tejun Heoa2c1c572012-12-18 10:35:02 -08002200 current->comm, preempt_count(), task_pid_nr(current),
2201 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02002202 debug_show_held_locks(current);
2203 dump_stack();
2204 }
2205
Tejun Heod565ed62013-01-24 11:01:33 -08002206 spin_lock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002207
Tejun Heofb0e7be2010-06-29 10:07:15 +02002208 /* clear cpu intensive status */
2209 if (unlikely(cpu_intensive))
2210 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2211
Tejun Heoa62428c2010-06-29 10:07:10 +02002212 /* we're done with it, release */
Sasha Levin42f85702012-12-17 10:01:23 -05002213 hash_del(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002214 worker->current_work = NULL;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002215 worker->current_func = NULL;
Tejun Heo112202d2013-02-13 19:29:12 -08002216 worker->current_pwq = NULL;
2217 pwq_dec_nr_in_flight(pwq, work_color);
Tejun Heoa62428c2010-06-29 10:07:10 +02002218}
2219
Tejun Heoaffee4b2010-06-29 10:07:12 +02002220/**
2221 * process_scheduled_works - process scheduled works
2222 * @worker: self
2223 *
2224 * Process all scheduled works. Please note that the scheduled list
2225 * may change while processing a work, so this function repeatedly
2226 * fetches a work from the top and executes it.
2227 *
2228 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002229 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02002230 * multiple times.
2231 */
2232static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002234 while (!list_empty(&worker->scheduled)) {
2235 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002237 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239}
2240
Tejun Heo4690c4a2010-06-29 10:07:10 +02002241/**
2242 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02002243 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02002244 *
Tejun Heo706026c2013-01-24 11:01:34 -08002245 * The worker thread function. There are NR_CPU_WORKER_POOLS dynamic pools
2246 * of these per each cpu. These workers process all works regardless of
Tejun Heoe22bee72010-06-29 10:07:14 +02002247 * their specific target workqueue. The only exception is works which
2248 * belong to workqueues with a rescuer which will be explained in
2249 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02002250 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002251static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252{
Tejun Heoc34056a2010-06-29 10:07:11 +02002253 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07002254 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
Tejun Heoe22bee72010-06-29 10:07:14 +02002256 /* tell the scheduler that this is a workqueue worker */
2257 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002258woke_up:
Tejun Heod565ed62013-01-24 11:01:33 -08002259 spin_lock_irq(&pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002261 /* we are off idle list if destruction or rebind is requested */
2262 if (unlikely(list_empty(&worker->entry))) {
Tejun Heod565ed62013-01-24 11:01:33 -08002263 spin_unlock_irq(&pool->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07002264
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002265 /* if DIE is set, destruction is requested */
Tejun Heo25511a42012-07-17 12:39:27 -07002266 if (worker->flags & WORKER_DIE) {
2267 worker->task->flags &= ~PF_WQ_WORKER;
2268 return 0;
2269 }
2270
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002271 /* otherwise, rebind */
Tejun Heo25511a42012-07-17 12:39:27 -07002272 idle_worker_rebind(worker);
2273 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 }
2275
Tejun Heoc8e55f32010-06-29 10:07:12 +02002276 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002277recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002278 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002279 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002280 goto sleep;
2281
2282 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002283 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002284 goto recheck;
2285
Tejun Heoc8e55f32010-06-29 10:07:12 +02002286 /*
2287 * ->scheduled list can only be filled while a worker is
2288 * preparing to process a work or actually processing it.
2289 * Make sure nobody diddled with it while I was sleeping.
2290 */
Tejun Heo6183c002013-03-12 11:29:57 -07002291 WARN_ON_ONCE(!list_empty(&worker->scheduled));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002292
Tejun Heoe22bee72010-06-29 10:07:14 +02002293 /*
2294 * When control reaches this point, we're guaranteed to have
2295 * at least one idle worker or that someone else has already
2296 * assumed the manager role.
2297 */
2298 worker_clr_flags(worker, WORKER_PREP);
2299
2300 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002301 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002302 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002303 struct work_struct, entry);
2304
2305 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2306 /* optimization path, not strictly necessary */
2307 process_one_work(worker, work);
2308 if (unlikely(!list_empty(&worker->scheduled)))
2309 process_scheduled_works(worker);
2310 } else {
2311 move_linked_works(work, &worker->scheduled, NULL);
2312 process_scheduled_works(worker);
2313 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002314 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002315
Tejun Heoe22bee72010-06-29 10:07:14 +02002316 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002317sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07002318 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002319 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002320
Tejun Heoc8e55f32010-06-29 10:07:12 +02002321 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002322 * pool->lock is held and there's no work to process and no need to
2323 * manage, sleep. Workers are woken up only while holding
2324 * pool->lock or from local cpu, so setting the current state
2325 * before releasing pool->lock is enough to prevent losing any
2326 * event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002327 */
2328 worker_enter_idle(worker);
2329 __set_current_state(TASK_INTERRUPTIBLE);
Tejun Heod565ed62013-01-24 11:01:33 -08002330 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02002331 schedule();
2332 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333}
2334
Tejun Heoe22bee72010-06-29 10:07:14 +02002335/**
2336 * rescuer_thread - the rescuer thread function
Tejun Heo111c2252013-01-17 17:16:24 -08002337 * @__rescuer: self
Tejun Heoe22bee72010-06-29 10:07:14 +02002338 *
2339 * Workqueue rescuer thread function. There's one rescuer for each
Tejun Heo493008a2013-03-12 11:30:03 -07002340 * workqueue which has WQ_MEM_RECLAIM set.
Tejun Heoe22bee72010-06-29 10:07:14 +02002341 *
Tejun Heo706026c2013-01-24 11:01:34 -08002342 * Regular work processing on a pool may block trying to create a new
Tejun Heoe22bee72010-06-29 10:07:14 +02002343 * worker which uses GFP_KERNEL allocation which has slight chance of
2344 * developing into deadlock if some works currently on the same queue
2345 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2346 * the problem rescuer solves.
2347 *
Tejun Heo706026c2013-01-24 11:01:34 -08002348 * When such condition is possible, the pool summons rescuers of all
2349 * workqueues which have works queued on the pool and let them process
Tejun Heoe22bee72010-06-29 10:07:14 +02002350 * those works so that forward progress can be guaranteed.
2351 *
2352 * This should happen rarely.
2353 */
Tejun Heo111c2252013-01-17 17:16:24 -08002354static int rescuer_thread(void *__rescuer)
Tejun Heoe22bee72010-06-29 10:07:14 +02002355{
Tejun Heo111c2252013-01-17 17:16:24 -08002356 struct worker *rescuer = __rescuer;
2357 struct workqueue_struct *wq = rescuer->rescue_wq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002358 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heoe22bee72010-06-29 10:07:14 +02002359
2360 set_user_nice(current, RESCUER_NICE_LEVEL);
Tejun Heo111c2252013-01-17 17:16:24 -08002361
2362 /*
2363 * Mark rescuer as worker too. As WORKER_PREP is never cleared, it
2364 * doesn't participate in concurrency management.
2365 */
2366 rescuer->task->flags |= PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002367repeat:
2368 set_current_state(TASK_INTERRUPTIBLE);
2369
Mike Galbraith412d32e2012-11-28 07:17:18 +01002370 if (kthread_should_stop()) {
2371 __set_current_state(TASK_RUNNING);
Tejun Heo111c2252013-01-17 17:16:24 -08002372 rescuer->task->flags &= ~PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002373 return 0;
Mike Galbraith412d32e2012-11-28 07:17:18 +01002374 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002375
Tejun Heo493a1722013-03-12 11:29:59 -07002376 /* see whether any pwq is asking for help */
2377 spin_lock_irq(&workqueue_lock);
2378
2379 while (!list_empty(&wq->maydays)) {
2380 struct pool_workqueue *pwq = list_first_entry(&wq->maydays,
2381 struct pool_workqueue, mayday_node);
Tejun Heo112202d2013-02-13 19:29:12 -08002382 struct worker_pool *pool = pwq->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002383 struct work_struct *work, *n;
2384
2385 __set_current_state(TASK_RUNNING);
Tejun Heo493a1722013-03-12 11:29:59 -07002386 list_del_init(&pwq->mayday_node);
2387
2388 spin_unlock_irq(&workqueue_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002389
2390 /* migrate to the target cpu if possible */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08002391 worker_maybe_bind_and_lock(pool);
Lai Jiangshanb3104102013-02-19 12:17:02 -08002392 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002393
2394 /*
2395 * Slurp in all works issued via this workqueue and
2396 * process'em.
2397 */
Tejun Heo6183c002013-03-12 11:29:57 -07002398 WARN_ON_ONCE(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002399 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heo112202d2013-02-13 19:29:12 -08002400 if (get_work_pwq(work) == pwq)
Tejun Heoe22bee72010-06-29 10:07:14 +02002401 move_linked_works(work, scheduled, &n);
2402
2403 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002404
2405 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002406 * Leave this pool. If keep_working() is %true, notify a
Tejun Heo75769582011-02-14 14:04:46 +01002407 * regular worker; otherwise, we end up with 0 concurrency
2408 * and stalling the execution.
2409 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002410 if (keep_working(pool))
2411 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002412
Lai Jiangshanb3104102013-02-19 12:17:02 -08002413 rescuer->pool = NULL;
Tejun Heo493a1722013-03-12 11:29:59 -07002414 spin_unlock(&pool->lock);
2415 spin_lock(&workqueue_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002416 }
2417
Tejun Heo493a1722013-03-12 11:29:59 -07002418 spin_unlock_irq(&workqueue_lock);
2419
Tejun Heo111c2252013-01-17 17:16:24 -08002420 /* rescuers should never participate in concurrency management */
2421 WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
Tejun Heoe22bee72010-06-29 10:07:14 +02002422 schedule();
2423 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424}
2425
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002426struct wq_barrier {
2427 struct work_struct work;
2428 struct completion done;
2429};
2430
2431static void wq_barrier_func(struct work_struct *work)
2432{
2433 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2434 complete(&barr->done);
2435}
2436
Tejun Heo4690c4a2010-06-29 10:07:10 +02002437/**
2438 * insert_wq_barrier - insert a barrier work
Tejun Heo112202d2013-02-13 19:29:12 -08002439 * @pwq: pwq to insert barrier into
Tejun Heo4690c4a2010-06-29 10:07:10 +02002440 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002441 * @target: target work to attach @barr to
2442 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002443 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002444 * @barr is linked to @target such that @barr is completed only after
2445 * @target finishes execution. Please note that the ordering
2446 * guarantee is observed only with respect to @target and on the local
2447 * cpu.
2448 *
2449 * Currently, a queued barrier can't be canceled. This is because
2450 * try_to_grab_pending() can't determine whether the work to be
2451 * grabbed is at the head of the queue and thus can't clear LINKED
2452 * flag of the previous work while there must be a valid next work
2453 * after a work with LINKED flag set.
2454 *
2455 * Note that when @worker is non-NULL, @target may be modified
Tejun Heo112202d2013-02-13 19:29:12 -08002456 * underneath us, so we can't reliably determine pwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002457 *
2458 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002459 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002460 */
Tejun Heo112202d2013-02-13 19:29:12 -08002461static void insert_wq_barrier(struct pool_workqueue *pwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002462 struct wq_barrier *barr,
2463 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002464{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002465 struct list_head *head;
2466 unsigned int linked = 0;
2467
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002468 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002469 * debugobject calls are safe here even with pool->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002470 * as we know for sure that this will not trigger any of the
2471 * checks and call back into the fixup functions where we
2472 * might deadlock.
2473 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002474 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002475 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002476 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002477
Tejun Heoaffee4b2010-06-29 10:07:12 +02002478 /*
2479 * If @target is currently being executed, schedule the
2480 * barrier to the worker; otherwise, put it after @target.
2481 */
2482 if (worker)
2483 head = worker->scheduled.next;
2484 else {
2485 unsigned long *bits = work_data_bits(target);
2486
2487 head = target->entry.next;
2488 /* there can already be other linked works, inherit and set */
2489 linked = *bits & WORK_STRUCT_LINKED;
2490 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2491 }
2492
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002493 debug_work_activate(&barr->work);
Tejun Heo112202d2013-02-13 19:29:12 -08002494 insert_work(pwq, &barr->work, head,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002495 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002496}
2497
Tejun Heo73f53c42010-06-29 10:07:11 +02002498/**
Tejun Heo112202d2013-02-13 19:29:12 -08002499 * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing
Tejun Heo73f53c42010-06-29 10:07:11 +02002500 * @wq: workqueue being flushed
2501 * @flush_color: new flush color, < 0 for no-op
2502 * @work_color: new work color, < 0 for no-op
2503 *
Tejun Heo112202d2013-02-13 19:29:12 -08002504 * Prepare pwqs for workqueue flushing.
Tejun Heo73f53c42010-06-29 10:07:11 +02002505 *
Tejun Heo112202d2013-02-13 19:29:12 -08002506 * If @flush_color is non-negative, flush_color on all pwqs should be
2507 * -1. If no pwq has in-flight commands at the specified color, all
2508 * pwq->flush_color's stay at -1 and %false is returned. If any pwq
2509 * has in flight commands, its pwq->flush_color is set to
2510 * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq
Tejun Heo73f53c42010-06-29 10:07:11 +02002511 * wakeup logic is armed and %true is returned.
2512 *
2513 * The caller should have initialized @wq->first_flusher prior to
2514 * calling this function with non-negative @flush_color. If
2515 * @flush_color is negative, no flush color update is done and %false
2516 * is returned.
2517 *
Tejun Heo112202d2013-02-13 19:29:12 -08002518 * If @work_color is non-negative, all pwqs should have the same
Tejun Heo73f53c42010-06-29 10:07:11 +02002519 * work_color which is previous to @work_color and all will be
2520 * advanced to @work_color.
2521 *
2522 * CONTEXT:
2523 * mutex_lock(wq->flush_mutex).
2524 *
2525 * RETURNS:
2526 * %true if @flush_color >= 0 and there's something to flush. %false
2527 * otherwise.
2528 */
Tejun Heo112202d2013-02-13 19:29:12 -08002529static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
Tejun Heo73f53c42010-06-29 10:07:11 +02002530 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531{
Tejun Heo73f53c42010-06-29 10:07:11 +02002532 bool wait = false;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002533 struct pool_workqueue *pwq;
Oleg Nesterov14441962007-05-23 13:57:57 -07002534
Tejun Heo73f53c42010-06-29 10:07:11 +02002535 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002536 WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush));
Tejun Heo112202d2013-02-13 19:29:12 -08002537 atomic_set(&wq->nr_pwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002538 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002539
Tejun Heo76af4d92013-03-12 11:30:00 -07002540 local_irq_disable();
2541
Tejun Heo49e3cf42013-03-12 11:29:58 -07002542 for_each_pwq(pwq, wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08002543 struct worker_pool *pool = pwq->pool;
Tejun Heo73f53c42010-06-29 10:07:11 +02002544
Tejun Heo76af4d92013-03-12 11:30:00 -07002545 spin_lock(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002546
2547 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002548 WARN_ON_ONCE(pwq->flush_color != -1);
Tejun Heo73f53c42010-06-29 10:07:11 +02002549
Tejun Heo112202d2013-02-13 19:29:12 -08002550 if (pwq->nr_in_flight[flush_color]) {
2551 pwq->flush_color = flush_color;
2552 atomic_inc(&wq->nr_pwqs_to_flush);
Tejun Heo73f53c42010-06-29 10:07:11 +02002553 wait = true;
2554 }
2555 }
2556
2557 if (work_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002558 WARN_ON_ONCE(work_color != work_next_color(pwq->work_color));
Tejun Heo112202d2013-02-13 19:29:12 -08002559 pwq->work_color = work_color;
Tejun Heo73f53c42010-06-29 10:07:11 +02002560 }
2561
Tejun Heo76af4d92013-03-12 11:30:00 -07002562 spin_unlock(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002563 }
2564
Tejun Heo76af4d92013-03-12 11:30:00 -07002565 local_irq_enable();
2566
Tejun Heo112202d2013-02-13 19:29:12 -08002567 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush))
Tejun Heo73f53c42010-06-29 10:07:11 +02002568 complete(&wq->first_flusher->done);
2569
2570 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571}
2572
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002573/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002575 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 *
2577 * Forces execution of the workqueue and blocks until its completion.
2578 * This is typically used in driver shutdown handlers.
2579 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002580 * We sleep until all works which were queued on entry have been handled,
2581 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002583void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584{
Tejun Heo73f53c42010-06-29 10:07:11 +02002585 struct wq_flusher this_flusher = {
2586 .list = LIST_HEAD_INIT(this_flusher.list),
2587 .flush_color = -1,
2588 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2589 };
2590 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002591
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002592 lock_map_acquire(&wq->lockdep_map);
2593 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002594
2595 mutex_lock(&wq->flush_mutex);
2596
2597 /*
2598 * Start-to-wait phase
2599 */
2600 next_color = work_next_color(wq->work_color);
2601
2602 if (next_color != wq->flush_color) {
2603 /*
2604 * Color space is not full. The current work_color
2605 * becomes our flush_color and work_color is advanced
2606 * by one.
2607 */
Tejun Heo6183c002013-03-12 11:29:57 -07002608 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow));
Tejun Heo73f53c42010-06-29 10:07:11 +02002609 this_flusher.flush_color = wq->work_color;
2610 wq->work_color = next_color;
2611
2612 if (!wq->first_flusher) {
2613 /* no flush in progress, become the first flusher */
Tejun Heo6183c002013-03-12 11:29:57 -07002614 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002615
2616 wq->first_flusher = &this_flusher;
2617
Tejun Heo112202d2013-02-13 19:29:12 -08002618 if (!flush_workqueue_prep_pwqs(wq, wq->flush_color,
Tejun Heo73f53c42010-06-29 10:07:11 +02002619 wq->work_color)) {
2620 /* nothing to flush, done */
2621 wq->flush_color = next_color;
2622 wq->first_flusher = NULL;
2623 goto out_unlock;
2624 }
2625 } else {
2626 /* wait in queue */
Tejun Heo6183c002013-03-12 11:29:57 -07002627 WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002628 list_add_tail(&this_flusher.list, &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002629 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002630 }
2631 } else {
2632 /*
2633 * Oops, color space is full, wait on overflow queue.
2634 * The next flush completion will assign us
2635 * flush_color and transfer to flusher_queue.
2636 */
2637 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2638 }
2639
2640 mutex_unlock(&wq->flush_mutex);
2641
2642 wait_for_completion(&this_flusher.done);
2643
2644 /*
2645 * Wake-up-and-cascade phase
2646 *
2647 * First flushers are responsible for cascading flushes and
2648 * handling overflow. Non-first flushers can simply return.
2649 */
2650 if (wq->first_flusher != &this_flusher)
2651 return;
2652
2653 mutex_lock(&wq->flush_mutex);
2654
Tejun Heo4ce48b32010-07-02 10:03:51 +02002655 /* we might have raced, check again with mutex held */
2656 if (wq->first_flusher != &this_flusher)
2657 goto out_unlock;
2658
Tejun Heo73f53c42010-06-29 10:07:11 +02002659 wq->first_flusher = NULL;
2660
Tejun Heo6183c002013-03-12 11:29:57 -07002661 WARN_ON_ONCE(!list_empty(&this_flusher.list));
2662 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002663
2664 while (true) {
2665 struct wq_flusher *next, *tmp;
2666
2667 /* complete all the flushers sharing the current flush color */
2668 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2669 if (next->flush_color != wq->flush_color)
2670 break;
2671 list_del_init(&next->list);
2672 complete(&next->done);
2673 }
2674
Tejun Heo6183c002013-03-12 11:29:57 -07002675 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) &&
2676 wq->flush_color != work_next_color(wq->work_color));
Tejun Heo73f53c42010-06-29 10:07:11 +02002677
2678 /* this flush_color is finished, advance by one */
2679 wq->flush_color = work_next_color(wq->flush_color);
2680
2681 /* one color has been freed, handle overflow queue */
2682 if (!list_empty(&wq->flusher_overflow)) {
2683 /*
2684 * Assign the same color to all overflowed
2685 * flushers, advance work_color and append to
2686 * flusher_queue. This is the start-to-wait
2687 * phase for these overflowed flushers.
2688 */
2689 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2690 tmp->flush_color = wq->work_color;
2691
2692 wq->work_color = work_next_color(wq->work_color);
2693
2694 list_splice_tail_init(&wq->flusher_overflow,
2695 &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002696 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002697 }
2698
2699 if (list_empty(&wq->flusher_queue)) {
Tejun Heo6183c002013-03-12 11:29:57 -07002700 WARN_ON_ONCE(wq->flush_color != wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002701 break;
2702 }
2703
2704 /*
2705 * Need to flush more colors. Make the next flusher
Tejun Heo112202d2013-02-13 19:29:12 -08002706 * the new first flusher and arm pwqs.
Tejun Heo73f53c42010-06-29 10:07:11 +02002707 */
Tejun Heo6183c002013-03-12 11:29:57 -07002708 WARN_ON_ONCE(wq->flush_color == wq->work_color);
2709 WARN_ON_ONCE(wq->flush_color != next->flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002710
2711 list_del_init(&next->list);
2712 wq->first_flusher = next;
2713
Tejun Heo112202d2013-02-13 19:29:12 -08002714 if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1))
Tejun Heo73f53c42010-06-29 10:07:11 +02002715 break;
2716
2717 /*
2718 * Meh... this color is already done, clear first
2719 * flusher and repeat cascading.
2720 */
2721 wq->first_flusher = NULL;
2722 }
2723
2724out_unlock:
2725 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726}
Dave Jonesae90dd52006-06-30 01:40:45 -04002727EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002729/**
2730 * drain_workqueue - drain a workqueue
2731 * @wq: workqueue to drain
2732 *
2733 * Wait until the workqueue becomes empty. While draining is in progress,
2734 * only chain queueing is allowed. IOW, only currently pending or running
2735 * work items on @wq can queue further work items on it. @wq is flushed
2736 * repeatedly until it becomes empty. The number of flushing is detemined
2737 * by the depth of chaining and should be relatively short. Whine if it
2738 * takes too long.
2739 */
2740void drain_workqueue(struct workqueue_struct *wq)
2741{
2742 unsigned int flush_cnt = 0;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002743 struct pool_workqueue *pwq;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002744
2745 /*
2746 * __queue_work() needs to test whether there are drainers, is much
2747 * hotter than drain_workqueue() and already looks at @wq->flags.
2748 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2749 */
Tejun Heoe98d5b12013-03-12 11:29:57 -07002750 spin_lock_irq(&workqueue_lock);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002751 if (!wq->nr_drainers++)
2752 wq->flags |= WQ_DRAINING;
Tejun Heoe98d5b12013-03-12 11:29:57 -07002753 spin_unlock_irq(&workqueue_lock);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002754reflush:
2755 flush_workqueue(wq);
2756
Tejun Heo76af4d92013-03-12 11:30:00 -07002757 local_irq_disable();
2758
Tejun Heo49e3cf42013-03-12 11:29:58 -07002759 for_each_pwq(pwq, wq) {
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002760 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002761
Tejun Heo76af4d92013-03-12 11:30:00 -07002762 spin_lock(&pwq->pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08002763 drained = !pwq->nr_active && list_empty(&pwq->delayed_works);
Tejun Heo76af4d92013-03-12 11:30:00 -07002764 spin_unlock(&pwq->pool->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002765
2766 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002767 continue;
2768
2769 if (++flush_cnt == 10 ||
2770 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
Valentin Ilie044c7822012-08-19 00:52:42 +03002771 pr_warn("workqueue %s: flush on destruction isn't complete after %u tries\n",
2772 wq->name, flush_cnt);
Tejun Heo76af4d92013-03-12 11:30:00 -07002773
2774 local_irq_enable();
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002775 goto reflush;
2776 }
2777
Tejun Heo76af4d92013-03-12 11:30:00 -07002778 spin_lock(&workqueue_lock);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002779 if (!--wq->nr_drainers)
2780 wq->flags &= ~WQ_DRAINING;
Tejun Heo76af4d92013-03-12 11:30:00 -07002781 spin_unlock(&workqueue_lock);
2782
2783 local_irq_enable();
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002784}
2785EXPORT_SYMBOL_GPL(drain_workqueue);
2786
Tejun Heo606a5022012-08-20 14:51:23 -07002787static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
Tejun Heobaf59022010-09-16 10:42:16 +02002788{
2789 struct worker *worker = NULL;
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002790 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002791 struct pool_workqueue *pwq;
Tejun Heobaf59022010-09-16 10:42:16 +02002792
2793 might_sleep();
Tejun Heobaf59022010-09-16 10:42:16 +02002794
Tejun Heofa1b54e2013-03-12 11:30:00 -07002795 local_irq_disable();
2796 pool = get_work_pool(work);
2797 if (!pool) {
2798 local_irq_enable();
2799 return false;
2800 }
2801
2802 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08002803 /* see the comment in try_to_grab_pending() with the same code */
Tejun Heo112202d2013-02-13 19:29:12 -08002804 pwq = get_work_pwq(work);
2805 if (pwq) {
2806 if (unlikely(pwq->pool != pool))
Tejun Heobaf59022010-09-16 10:42:16 +02002807 goto already_gone;
Tejun Heo606a5022012-08-20 14:51:23 -07002808 } else {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002809 worker = find_worker_executing_work(pool, work);
Tejun Heobaf59022010-09-16 10:42:16 +02002810 if (!worker)
2811 goto already_gone;
Tejun Heo112202d2013-02-13 19:29:12 -08002812 pwq = worker->current_pwq;
Tejun Heo606a5022012-08-20 14:51:23 -07002813 }
Tejun Heobaf59022010-09-16 10:42:16 +02002814
Tejun Heo112202d2013-02-13 19:29:12 -08002815 insert_wq_barrier(pwq, barr, work, worker);
Tejun Heod565ed62013-01-24 11:01:33 -08002816 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002817
Tejun Heoe1594892011-01-09 23:32:15 +01002818 /*
2819 * If @max_active is 1 or rescuer is in use, flushing another work
2820 * item on the same workqueue may lead to deadlock. Make sure the
2821 * flusher is not running on the same workqueue by verifying write
2822 * access.
2823 */
Tejun Heo493008a2013-03-12 11:30:03 -07002824 if (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)
Tejun Heo112202d2013-02-13 19:29:12 -08002825 lock_map_acquire(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002826 else
Tejun Heo112202d2013-02-13 19:29:12 -08002827 lock_map_acquire_read(&pwq->wq->lockdep_map);
2828 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002829
Tejun Heobaf59022010-09-16 10:42:16 +02002830 return true;
2831already_gone:
Tejun Heod565ed62013-01-24 11:01:33 -08002832 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002833 return false;
2834}
2835
Oleg Nesterovdb700892008-07-25 01:47:49 -07002836/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002837 * flush_work - wait for a work to finish executing the last queueing instance
2838 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002839 *
Tejun Heo606a5022012-08-20 14:51:23 -07002840 * Wait until @work has finished execution. @work is guaranteed to be idle
2841 * on return if it hasn't been requeued since flush started.
Tejun Heo401a8d02010-09-16 10:36:00 +02002842 *
2843 * RETURNS:
2844 * %true if flush_work() waited for the work to finish execution,
2845 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002846 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002847bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002848{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002849 struct wq_barrier barr;
2850
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002851 lock_map_acquire(&work->lockdep_map);
2852 lock_map_release(&work->lockdep_map);
2853
Tejun Heo606a5022012-08-20 14:51:23 -07002854 if (start_flush_work(work, &barr)) {
Tejun Heobaf59022010-09-16 10:42:16 +02002855 wait_for_completion(&barr.done);
2856 destroy_work_on_stack(&barr.work);
2857 return true;
Tejun Heo606a5022012-08-20 14:51:23 -07002858 } else {
Tejun Heobaf59022010-09-16 10:42:16 +02002859 return false;
Tejun Heo606a5022012-08-20 14:51:23 -07002860 }
Oleg Nesterovdb700892008-07-25 01:47:49 -07002861}
2862EXPORT_SYMBOL_GPL(flush_work);
2863
Tejun Heo36e227d2012-08-03 10:30:46 -07002864static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
Tejun Heo401a8d02010-09-16 10:36:00 +02002865{
Tejun Heobbb68df2012-08-03 10:30:46 -07002866 unsigned long flags;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002867 int ret;
2868
2869 do {
Tejun Heobbb68df2012-08-03 10:30:46 -07002870 ret = try_to_grab_pending(work, is_dwork, &flags);
2871 /*
2872 * If someone else is canceling, wait for the same event it
2873 * would be waiting for before retrying.
2874 */
2875 if (unlikely(ret == -ENOENT))
Tejun Heo606a5022012-08-20 14:51:23 -07002876 flush_work(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002877 } while (unlikely(ret < 0));
2878
Tejun Heobbb68df2012-08-03 10:30:46 -07002879 /* tell other tasks trying to grab @work to back off */
2880 mark_work_canceling(work);
2881 local_irq_restore(flags);
2882
Tejun Heo606a5022012-08-20 14:51:23 -07002883 flush_work(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002884 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002885 return ret;
2886}
2887
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002888/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002889 * cancel_work_sync - cancel a work and wait for it to finish
2890 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002891 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002892 * Cancel @work and wait for its execution to finish. This function
2893 * can be used even if the work re-queues itself or migrates to
2894 * another workqueue. On return from this function, @work is
2895 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002896 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002897 * cancel_work_sync(&delayed_work->work) must not be used for
2898 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002899 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002900 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002901 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002902 *
2903 * RETURNS:
2904 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002905 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002906bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002907{
Tejun Heo36e227d2012-08-03 10:30:46 -07002908 return __cancel_work_timer(work, false);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002909}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002910EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002911
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002912/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002913 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2914 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002915 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002916 * Delayed timer is cancelled and the pending work is queued for
2917 * immediate execution. Like flush_work(), this function only
2918 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002919 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002920 * RETURNS:
2921 * %true if flush_work() waited for the work to finish execution,
2922 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002923 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002924bool flush_delayed_work(struct delayed_work *dwork)
2925{
Tejun Heo8930cab2012-08-03 10:30:45 -07002926 local_irq_disable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002927 if (del_timer_sync(&dwork->timer))
Lai Jiangshan60c057b2013-02-06 18:04:53 -08002928 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Tejun Heo8930cab2012-08-03 10:30:45 -07002929 local_irq_enable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002930 return flush_work(&dwork->work);
2931}
2932EXPORT_SYMBOL(flush_delayed_work);
2933
2934/**
Tejun Heo57b30ae2012-08-21 13:18:24 -07002935 * cancel_delayed_work - cancel a delayed work
2936 * @dwork: delayed_work to cancel
Tejun Heo09383492010-09-16 10:48:29 +02002937 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002938 * Kill off a pending delayed_work. Returns %true if @dwork was pending
2939 * and canceled; %false if wasn't pending. Note that the work callback
2940 * function may still be running on return, unless it returns %true and the
2941 * work doesn't re-arm itself. Explicitly flush or use
2942 * cancel_delayed_work_sync() to wait on it.
Tejun Heo09383492010-09-16 10:48:29 +02002943 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002944 * This function is safe to call from any context including IRQ handler.
Tejun Heo09383492010-09-16 10:48:29 +02002945 */
Tejun Heo57b30ae2012-08-21 13:18:24 -07002946bool cancel_delayed_work(struct delayed_work *dwork)
Tejun Heo09383492010-09-16 10:48:29 +02002947{
Tejun Heo57b30ae2012-08-21 13:18:24 -07002948 unsigned long flags;
2949 int ret;
2950
2951 do {
2952 ret = try_to_grab_pending(&dwork->work, true, &flags);
2953 } while (unlikely(ret == -EAGAIN));
2954
2955 if (unlikely(ret < 0))
2956 return false;
2957
Tejun Heo7c3eed52013-01-24 11:01:33 -08002958 set_work_pool_and_clear_pending(&dwork->work,
2959 get_work_pool_id(&dwork->work));
Tejun Heo57b30ae2012-08-21 13:18:24 -07002960 local_irq_restore(flags);
Dan Magenheimerc0158ca2012-10-18 16:31:37 -07002961 return ret;
Tejun Heo09383492010-09-16 10:48:29 +02002962}
Tejun Heo57b30ae2012-08-21 13:18:24 -07002963EXPORT_SYMBOL(cancel_delayed_work);
Tejun Heo09383492010-09-16 10:48:29 +02002964
2965/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002966 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2967 * @dwork: the delayed work cancel
2968 *
2969 * This is cancel_work_sync() for delayed works.
2970 *
2971 * RETURNS:
2972 * %true if @dwork was pending, %false otherwise.
2973 */
2974bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002975{
Tejun Heo36e227d2012-08-03 10:30:46 -07002976 return __cancel_work_timer(&dwork->work, true);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002977}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002978EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002980/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07002981 * schedule_work_on - put work task on a specific cpu
2982 * @cpu: cpu to put the work task on
2983 * @work: job to be done
2984 *
2985 * This puts a job on a specific cpu
2986 */
Tejun Heod4283e92012-08-03 10:30:44 -07002987bool schedule_work_on(int cpu, struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07002988{
Tejun Heod320c032010-06-29 10:07:14 +02002989 return queue_work_on(cpu, system_wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07002990}
2991EXPORT_SYMBOL(schedule_work_on);
2992
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002993/**
Dave Jonesae90dd52006-06-30 01:40:45 -04002994 * schedule_work - put work task in global workqueue
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 * @work: job to be done
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 *
Tejun Heod4283e92012-08-03 10:30:44 -07002997 * Returns %false if @work was already on the kernel-global workqueue and
2998 * %true otherwise.
David Howells52bad642006-11-22 14:54:01 +00002999 *
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003000 * This puts a job in the kernel-global workqueue if it was not already
3001 * queued and leaves it in the same position on the kernel-global
3002 * workqueue otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 */
Tejun Heod4283e92012-08-03 10:30:44 -07003004bool schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005{
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003006 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007}
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003008EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003010/**
3011 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
3012 * @cpu: cpu to use
3013 * @dwork: job to be done
3014 * @delay: number of jiffies to wait
3015 *
3016 * After waiting for a given time this puts a job in the kernel-global
3017 * workqueue on the specified CPU.
3018 */
Tejun Heod4283e92012-08-03 10:30:44 -07003019bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork,
3020 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021{
Tejun Heod320c032010-06-29 10:07:14 +02003022 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023}
Dave Jonesae90dd52006-06-30 01:40:45 -04003024EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
Andrew Mortonb6136772006-06-25 05:47:49 -07003026/**
Tejun Heo0a13c002012-08-03 10:30:44 -07003027 * schedule_delayed_work - put work task in global workqueue after delay
3028 * @dwork: job to be done
3029 * @delay: number of jiffies to wait or 0 for immediate execution
3030 *
3031 * After waiting for a given time this puts a job in the kernel-global
3032 * workqueue.
3033 */
Tejun Heod4283e92012-08-03 10:30:44 -07003034bool schedule_delayed_work(struct delayed_work *dwork, unsigned long delay)
Tejun Heo0a13c002012-08-03 10:30:44 -07003035{
3036 return queue_delayed_work(system_wq, dwork, delay);
3037}
3038EXPORT_SYMBOL(schedule_delayed_work);
3039
3040/**
Tejun Heo31ddd872010-10-19 11:14:49 +02003041 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07003042 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07003043 *
Tejun Heo31ddd872010-10-19 11:14:49 +02003044 * schedule_on_each_cpu() executes @func on each online CPU using the
3045 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07003046 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02003047 *
3048 * RETURNS:
3049 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07003050 */
David Howells65f27f32006-11-22 14:55:48 +00003051int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08003052{
3053 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02003054 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08003055
Andrew Mortonb6136772006-06-25 05:47:49 -07003056 works = alloc_percpu(struct work_struct);
3057 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08003058 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07003059
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003060 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08003061
Christoph Lameter15316ba2006-01-08 01:00:43 -08003062 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01003063 struct work_struct *work = per_cpu_ptr(works, cpu);
3064
3065 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003066 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02003067 }
Tejun Heo93981802009-11-17 14:06:20 -08003068
3069 for_each_online_cpu(cpu)
3070 flush_work(per_cpu_ptr(works, cpu));
3071
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003072 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07003073 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08003074 return 0;
3075}
3076
Alan Sterneef6a7d2010-02-12 17:39:21 +09003077/**
3078 * flush_scheduled_work - ensure that any scheduled work has run to completion.
3079 *
3080 * Forces execution of the kernel-global workqueue and blocks until its
3081 * completion.
3082 *
3083 * Think twice before calling this function! It's very easy to get into
3084 * trouble if you don't take great care. Either of the following situations
3085 * will lead to deadlock:
3086 *
3087 * One of the work items currently on the workqueue needs to acquire
3088 * a lock held by your code or its caller.
3089 *
3090 * Your code is running in the context of a work routine.
3091 *
3092 * They will be detected by lockdep when they occur, but the first might not
3093 * occur very often. It depends on what work items are on the workqueue and
3094 * what locks they need, which you have no control over.
3095 *
3096 * In most situations flushing the entire workqueue is overkill; you merely
3097 * need to know that a particular work item isn't queued and isn't running.
3098 * In such cases you should use cancel_delayed_work_sync() or
3099 * cancel_work_sync() instead.
3100 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101void flush_scheduled_work(void)
3102{
Tejun Heod320c032010-06-29 10:07:14 +02003103 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104}
Dave Jonesae90dd52006-06-30 01:40:45 -04003105EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
3107/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06003108 * execute_in_process_context - reliably execute the routine with user context
3109 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06003110 * @ew: guaranteed storage for the execute work structure (must
3111 * be available when the work executes)
3112 *
3113 * Executes the function immediately if process context is available,
3114 * otherwise schedules the function for delayed execution.
3115 *
3116 * Returns: 0 - function was executed
3117 * 1 - function was scheduled for execution
3118 */
David Howells65f27f32006-11-22 14:55:48 +00003119int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06003120{
3121 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00003122 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003123 return 0;
3124 }
3125
David Howells65f27f32006-11-22 14:55:48 +00003126 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003127 schedule_work(&ew->work);
3128
3129 return 1;
3130}
3131EXPORT_SYMBOL_GPL(execute_in_process_context);
3132
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133int keventd_up(void)
3134{
Tejun Heod320c032010-06-29 10:07:14 +02003135 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136}
3137
Tejun Heo7a4e3442013-03-12 11:30:00 -07003138/**
3139 * free_workqueue_attrs - free a workqueue_attrs
3140 * @attrs: workqueue_attrs to free
3141 *
3142 * Undo alloc_workqueue_attrs().
3143 */
3144void free_workqueue_attrs(struct workqueue_attrs *attrs)
3145{
3146 if (attrs) {
3147 free_cpumask_var(attrs->cpumask);
3148 kfree(attrs);
3149 }
3150}
3151
3152/**
3153 * alloc_workqueue_attrs - allocate a workqueue_attrs
3154 * @gfp_mask: allocation mask to use
3155 *
3156 * Allocate a new workqueue_attrs, initialize with default settings and
3157 * return it. Returns NULL on failure.
3158 */
3159struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask)
3160{
3161 struct workqueue_attrs *attrs;
3162
3163 attrs = kzalloc(sizeof(*attrs), gfp_mask);
3164 if (!attrs)
3165 goto fail;
3166 if (!alloc_cpumask_var(&attrs->cpumask, gfp_mask))
3167 goto fail;
3168
3169 cpumask_setall(attrs->cpumask);
3170 return attrs;
3171fail:
3172 free_workqueue_attrs(attrs);
3173 return NULL;
3174}
3175
Tejun Heo29c91e92013-03-12 11:30:03 -07003176static void copy_workqueue_attrs(struct workqueue_attrs *to,
3177 const struct workqueue_attrs *from)
3178{
3179 to->nice = from->nice;
3180 cpumask_copy(to->cpumask, from->cpumask);
3181}
3182
3183/*
3184 * Hacky implementation of jhash of bitmaps which only considers the
3185 * specified number of bits. We probably want a proper implementation in
3186 * include/linux/jhash.h.
3187 */
3188static u32 jhash_bitmap(const unsigned long *bitmap, int bits, u32 hash)
3189{
3190 int nr_longs = bits / BITS_PER_LONG;
3191 int nr_leftover = bits % BITS_PER_LONG;
3192 unsigned long leftover = 0;
3193
3194 if (nr_longs)
3195 hash = jhash(bitmap, nr_longs * sizeof(long), hash);
3196 if (nr_leftover) {
3197 bitmap_copy(&leftover, bitmap + nr_longs, nr_leftover);
3198 hash = jhash(&leftover, sizeof(long), hash);
3199 }
3200 return hash;
3201}
3202
3203/* hash value of the content of @attr */
3204static u32 wqattrs_hash(const struct workqueue_attrs *attrs)
3205{
3206 u32 hash = 0;
3207
3208 hash = jhash_1word(attrs->nice, hash);
3209 hash = jhash_bitmap(cpumask_bits(attrs->cpumask), nr_cpu_ids, hash);
3210 return hash;
3211}
3212
3213/* content equality test */
3214static bool wqattrs_equal(const struct workqueue_attrs *a,
3215 const struct workqueue_attrs *b)
3216{
3217 if (a->nice != b->nice)
3218 return false;
3219 if (!cpumask_equal(a->cpumask, b->cpumask))
3220 return false;
3221 return true;
3222}
3223
Tejun Heo7a4e3442013-03-12 11:30:00 -07003224/**
3225 * init_worker_pool - initialize a newly zalloc'd worker_pool
3226 * @pool: worker_pool to initialize
3227 *
3228 * Initiailize a newly zalloc'd @pool. It also allocates @pool->attrs.
Tejun Heo29c91e92013-03-12 11:30:03 -07003229 * Returns 0 on success, -errno on failure. Even on failure, all fields
3230 * inside @pool proper are initialized and put_unbound_pool() can be called
3231 * on @pool safely to release it.
Tejun Heo7a4e3442013-03-12 11:30:00 -07003232 */
3233static int init_worker_pool(struct worker_pool *pool)
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003234{
3235 spin_lock_init(&pool->lock);
Tejun Heo29c91e92013-03-12 11:30:03 -07003236 pool->id = -1;
3237 pool->cpu = -1;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003238 pool->flags |= POOL_DISASSOCIATED;
3239 INIT_LIST_HEAD(&pool->worklist);
3240 INIT_LIST_HEAD(&pool->idle_list);
3241 hash_init(pool->busy_hash);
3242
3243 init_timer_deferrable(&pool->idle_timer);
3244 pool->idle_timer.function = idle_worker_timeout;
3245 pool->idle_timer.data = (unsigned long)pool;
3246
3247 setup_timer(&pool->mayday_timer, pool_mayday_timeout,
3248 (unsigned long)pool);
3249
3250 mutex_init(&pool->manager_arb);
3251 mutex_init(&pool->assoc_mutex);
3252 ida_init(&pool->worker_ida);
Tejun Heo7a4e3442013-03-12 11:30:00 -07003253
Tejun Heo29c91e92013-03-12 11:30:03 -07003254 INIT_HLIST_NODE(&pool->hash_node);
3255 pool->refcnt = 1;
3256
3257 /* shouldn't fail above this point */
Tejun Heo7a4e3442013-03-12 11:30:00 -07003258 pool->attrs = alloc_workqueue_attrs(GFP_KERNEL);
3259 if (!pool->attrs)
3260 return -ENOMEM;
3261 return 0;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003262}
3263
Tejun Heo29c91e92013-03-12 11:30:03 -07003264static void rcu_free_pool(struct rcu_head *rcu)
3265{
3266 struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu);
3267
3268 ida_destroy(&pool->worker_ida);
3269 free_workqueue_attrs(pool->attrs);
3270 kfree(pool);
3271}
3272
3273/**
3274 * put_unbound_pool - put a worker_pool
3275 * @pool: worker_pool to put
3276 *
3277 * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU
3278 * safe manner.
3279 */
3280static void put_unbound_pool(struct worker_pool *pool)
3281{
3282 struct worker *worker;
3283
3284 spin_lock_irq(&workqueue_lock);
3285 if (--pool->refcnt) {
3286 spin_unlock_irq(&workqueue_lock);
3287 return;
3288 }
3289
3290 /* sanity checks */
3291 if (WARN_ON(!(pool->flags & POOL_DISASSOCIATED)) ||
3292 WARN_ON(!list_empty(&pool->worklist))) {
3293 spin_unlock_irq(&workqueue_lock);
3294 return;
3295 }
3296
3297 /* release id and unhash */
3298 if (pool->id >= 0)
3299 idr_remove(&worker_pool_idr, pool->id);
3300 hash_del(&pool->hash_node);
3301
3302 spin_unlock_irq(&workqueue_lock);
3303
3304 /* lock out manager and destroy all workers */
3305 mutex_lock(&pool->manager_arb);
3306 spin_lock_irq(&pool->lock);
3307
3308 while ((worker = first_worker(pool)))
3309 destroy_worker(worker);
3310 WARN_ON(pool->nr_workers || pool->nr_idle);
3311
3312 spin_unlock_irq(&pool->lock);
3313 mutex_unlock(&pool->manager_arb);
3314
3315 /* shut down the timers */
3316 del_timer_sync(&pool->idle_timer);
3317 del_timer_sync(&pool->mayday_timer);
3318
3319 /* sched-RCU protected to allow dereferences from get_work_pool() */
3320 call_rcu_sched(&pool->rcu, rcu_free_pool);
3321}
3322
3323/**
3324 * get_unbound_pool - get a worker_pool with the specified attributes
3325 * @attrs: the attributes of the worker_pool to get
3326 *
3327 * Obtain a worker_pool which has the same attributes as @attrs, bump the
3328 * reference count and return it. If there already is a matching
3329 * worker_pool, it will be used; otherwise, this function attempts to
3330 * create a new one. On failure, returns NULL.
3331 */
3332static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs)
3333{
3334 static DEFINE_MUTEX(create_mutex);
3335 u32 hash = wqattrs_hash(attrs);
3336 struct worker_pool *pool;
3337 struct worker *worker;
3338
3339 mutex_lock(&create_mutex);
3340
3341 /* do we already have a matching pool? */
3342 spin_lock_irq(&workqueue_lock);
3343 hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) {
3344 if (wqattrs_equal(pool->attrs, attrs)) {
3345 pool->refcnt++;
3346 goto out_unlock;
3347 }
3348 }
3349 spin_unlock_irq(&workqueue_lock);
3350
3351 /* nope, create a new one */
3352 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
3353 if (!pool || init_worker_pool(pool) < 0)
3354 goto fail;
3355
Tejun Heo8864b4e2013-03-12 11:30:04 -07003356 lockdep_set_subclass(&pool->lock, 1); /* see put_pwq() */
Tejun Heo29c91e92013-03-12 11:30:03 -07003357 copy_workqueue_attrs(pool->attrs, attrs);
3358
3359 if (worker_pool_assign_id(pool) < 0)
3360 goto fail;
3361
3362 /* create and start the initial worker */
3363 worker = create_worker(pool);
3364 if (!worker)
3365 goto fail;
3366
3367 spin_lock_irq(&pool->lock);
3368 start_worker(worker);
3369 spin_unlock_irq(&pool->lock);
3370
3371 /* install */
3372 spin_lock_irq(&workqueue_lock);
3373 hash_add(unbound_pool_hash, &pool->hash_node, hash);
3374out_unlock:
3375 spin_unlock_irq(&workqueue_lock);
3376 mutex_unlock(&create_mutex);
3377 return pool;
3378fail:
3379 mutex_unlock(&create_mutex);
3380 if (pool)
3381 put_unbound_pool(pool);
3382 return NULL;
3383}
3384
Tejun Heo8864b4e2013-03-12 11:30:04 -07003385static void rcu_free_pwq(struct rcu_head *rcu)
3386{
3387 kmem_cache_free(pwq_cache,
3388 container_of(rcu, struct pool_workqueue, rcu));
3389}
3390
3391/*
3392 * Scheduled on system_wq by put_pwq() when an unbound pwq hits zero refcnt
3393 * and needs to be destroyed.
3394 */
3395static void pwq_unbound_release_workfn(struct work_struct *work)
3396{
3397 struct pool_workqueue *pwq = container_of(work, struct pool_workqueue,
3398 unbound_release_work);
3399 struct workqueue_struct *wq = pwq->wq;
3400 struct worker_pool *pool = pwq->pool;
3401
3402 if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)))
3403 return;
3404
3405 spin_lock_irq(&workqueue_lock);
3406 list_del_rcu(&pwq->pwqs_node);
3407 spin_unlock_irq(&workqueue_lock);
3408
3409 put_unbound_pool(pool);
3410 call_rcu_sched(&pwq->rcu, rcu_free_pwq);
3411
3412 /*
3413 * If we're the last pwq going away, @wq is already dead and no one
3414 * is gonna access it anymore. Free it.
3415 */
3416 if (list_empty(&wq->pwqs))
3417 kfree(wq);
3418}
3419
Tejun Heod2c1d402013-03-12 11:30:04 -07003420static void init_and_link_pwq(struct pool_workqueue *pwq,
3421 struct workqueue_struct *wq,
3422 struct worker_pool *pool)
3423{
3424 BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK);
3425
3426 pwq->pool = pool;
3427 pwq->wq = wq;
3428 pwq->flush_color = -1;
Tejun Heo8864b4e2013-03-12 11:30:04 -07003429 pwq->refcnt = 1;
Tejun Heod2c1d402013-03-12 11:30:04 -07003430 pwq->max_active = wq->saved_max_active;
3431 INIT_LIST_HEAD(&pwq->delayed_works);
3432 INIT_LIST_HEAD(&pwq->mayday_node);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003433 INIT_WORK(&pwq->unbound_release_work, pwq_unbound_release_workfn);
Tejun Heod2c1d402013-03-12 11:30:04 -07003434
3435 list_add_tail_rcu(&pwq->pwqs_node, &wq->pwqs);
3436}
3437
Tejun Heo30cdf242013-03-12 11:29:57 -07003438static int alloc_and_link_pwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003440 bool highpri = wq->flags & WQ_HIGHPRI;
Tejun Heo30cdf242013-03-12 11:29:57 -07003441 int cpu;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01003442
Tejun Heo30cdf242013-03-12 11:29:57 -07003443 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo420c0dd2013-03-12 11:29:59 -07003444 wq->cpu_pwqs = alloc_percpu(struct pool_workqueue);
3445 if (!wq->cpu_pwqs)
Tejun Heo30cdf242013-03-12 11:29:57 -07003446 return -ENOMEM;
3447
3448 for_each_possible_cpu(cpu) {
Tejun Heo7fb98ea2013-03-12 11:30:00 -07003449 struct pool_workqueue *pwq =
3450 per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heo7a62c2c2013-03-12 11:30:03 -07003451 struct worker_pool *cpu_pools =
Tejun Heof02ae732013-03-12 11:30:03 -07003452 per_cpu(cpu_worker_pools, cpu);
Tejun Heo30cdf242013-03-12 11:29:57 -07003453
Tejun Heod2c1d402013-03-12 11:30:04 -07003454 init_and_link_pwq(pwq, wq, &cpu_pools[highpri]);
Tejun Heo30cdf242013-03-12 11:29:57 -07003455 }
3456 } else {
3457 struct pool_workqueue *pwq;
Tejun Heod2c1d402013-03-12 11:30:04 -07003458 struct worker_pool *pool;
Tejun Heo30cdf242013-03-12 11:29:57 -07003459
3460 pwq = kmem_cache_zalloc(pwq_cache, GFP_KERNEL);
3461 if (!pwq)
3462 return -ENOMEM;
3463
Tejun Heod2c1d402013-03-12 11:30:04 -07003464 pool = get_unbound_pool(unbound_std_wq_attrs[highpri]);
3465 if (!pool) {
Tejun Heo29c91e92013-03-12 11:30:03 -07003466 kmem_cache_free(pwq_cache, pwq);
3467 return -ENOMEM;
3468 }
3469
Tejun Heod2c1d402013-03-12 11:30:04 -07003470 init_and_link_pwq(pwq, wq, pool);
Tejun Heo30cdf242013-03-12 11:29:57 -07003471 }
3472
3473 return 0;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003474}
3475
Tejun Heof3421792010-07-02 10:03:51 +02003476static int wq_clamp_max_active(int max_active, unsigned int flags,
3477 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003478{
Tejun Heof3421792010-07-02 10:03:51 +02003479 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3480
3481 if (max_active < 1 || max_active > lim)
Valentin Ilie044c7822012-08-19 00:52:42 +03003482 pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
3483 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003484
Tejun Heof3421792010-07-02 10:03:51 +02003485 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003486}
3487
Tejun Heob196be82012-01-10 15:11:35 -08003488struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02003489 unsigned int flags,
3490 int max_active,
3491 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08003492 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003493{
Tejun Heob196be82012-01-10 15:11:35 -08003494 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003495 struct workqueue_struct *wq;
Tejun Heo49e3cf42013-03-12 11:29:58 -07003496 struct pool_workqueue *pwq;
Tejun Heob196be82012-01-10 15:11:35 -08003497 size_t namelen;
3498
3499 /* determine namelen, allocate wq and format name */
3500 va_start(args, lock_name);
3501 va_copy(args1, args);
3502 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3503
3504 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3505 if (!wq)
Tejun Heod2c1d402013-03-12 11:30:04 -07003506 return NULL;
Tejun Heob196be82012-01-10 15:11:35 -08003507
3508 vsnprintf(wq->name, namelen, fmt, args1);
3509 va_end(args);
3510 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003511
Tejun Heod320c032010-06-29 10:07:14 +02003512 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003513 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003514
Tejun Heob196be82012-01-10 15:11:35 -08003515 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003516 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003517 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003518 mutex_init(&wq->flush_mutex);
Tejun Heo112202d2013-02-13 19:29:12 -08003519 atomic_set(&wq->nr_pwqs_to_flush, 0);
Tejun Heo30cdf242013-03-12 11:29:57 -07003520 INIT_LIST_HEAD(&wq->pwqs);
Tejun Heo73f53c42010-06-29 10:07:11 +02003521 INIT_LIST_HEAD(&wq->flusher_queue);
3522 INIT_LIST_HEAD(&wq->flusher_overflow);
Tejun Heo493a1722013-03-12 11:29:59 -07003523 INIT_LIST_HEAD(&wq->maydays);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003524
Johannes Bergeb13ba82008-01-16 09:51:58 +01003525 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003526 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003527
Tejun Heo30cdf242013-03-12 11:29:57 -07003528 if (alloc_and_link_pwqs(wq) < 0)
Tejun Heod2c1d402013-03-12 11:30:04 -07003529 goto err_free_wq;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003530
Tejun Heo493008a2013-03-12 11:30:03 -07003531 /*
3532 * Workqueues which may be used during memory reclaim should
3533 * have a rescuer to guarantee forward progress.
3534 */
3535 if (flags & WQ_MEM_RECLAIM) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003536 struct worker *rescuer;
3537
Tejun Heod2c1d402013-03-12 11:30:04 -07003538 rescuer = alloc_worker();
Tejun Heoe22bee72010-06-29 10:07:14 +02003539 if (!rescuer)
Tejun Heod2c1d402013-03-12 11:30:04 -07003540 goto err_destroy;
Tejun Heoe22bee72010-06-29 10:07:14 +02003541
Tejun Heo111c2252013-01-17 17:16:24 -08003542 rescuer->rescue_wq = wq;
3543 rescuer->task = kthread_create(rescuer_thread, rescuer, "%s",
Tejun Heob196be82012-01-10 15:11:35 -08003544 wq->name);
Tejun Heod2c1d402013-03-12 11:30:04 -07003545 if (IS_ERR(rescuer->task)) {
3546 kfree(rescuer);
3547 goto err_destroy;
3548 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003549
Tejun Heod2c1d402013-03-12 11:30:04 -07003550 wq->rescuer = rescuer;
Tejun Heoe22bee72010-06-29 10:07:14 +02003551 rescuer->task->flags |= PF_THREAD_BOUND;
3552 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003553 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003554
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003555 /*
3556 * workqueue_lock protects global freeze state and workqueues
3557 * list. Grab it, set max_active accordingly and add the new
3558 * workqueue to workqueues list.
3559 */
Tejun Heoe98d5b12013-03-12 11:29:57 -07003560 spin_lock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003561
Tejun Heo58a69cb2011-02-16 09:25:31 +01003562 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heo49e3cf42013-03-12 11:29:58 -07003563 for_each_pwq(pwq, wq)
3564 pwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003565
Tejun Heo15376632010-06-29 10:07:11 +02003566 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003567
Tejun Heoe98d5b12013-03-12 11:29:57 -07003568 spin_unlock_irq(&workqueue_lock);
Tejun Heo15376632010-06-29 10:07:11 +02003569
Oleg Nesterov3af244332007-05-09 02:34:09 -07003570 return wq;
Tejun Heod2c1d402013-03-12 11:30:04 -07003571
3572err_free_wq:
3573 kfree(wq);
3574 return NULL;
3575err_destroy:
3576 destroy_workqueue(wq);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003577 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003578}
Tejun Heod320c032010-06-29 10:07:14 +02003579EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003580
3581/**
3582 * destroy_workqueue - safely terminate a workqueue
3583 * @wq: target workqueue
3584 *
3585 * Safely destroy a workqueue. All work currently pending will be done first.
3586 */
3587void destroy_workqueue(struct workqueue_struct *wq)
3588{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003589 struct pool_workqueue *pwq;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003590
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003591 /* drain it before proceeding with destruction */
3592 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003593
Tejun Heo76af4d92013-03-12 11:30:00 -07003594 spin_lock_irq(&workqueue_lock);
3595
Tejun Heo6183c002013-03-12 11:29:57 -07003596 /* sanity checks */
Tejun Heo49e3cf42013-03-12 11:29:58 -07003597 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07003598 int i;
3599
Tejun Heo76af4d92013-03-12 11:30:00 -07003600 for (i = 0; i < WORK_NR_COLORS; i++) {
3601 if (WARN_ON(pwq->nr_in_flight[i])) {
3602 spin_unlock_irq(&workqueue_lock);
Tejun Heo6183c002013-03-12 11:29:57 -07003603 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07003604 }
3605 }
3606
Tejun Heo8864b4e2013-03-12 11:30:04 -07003607 if (WARN_ON(pwq->refcnt > 1) ||
3608 WARN_ON(pwq->nr_active) ||
Tejun Heo76af4d92013-03-12 11:30:00 -07003609 WARN_ON(!list_empty(&pwq->delayed_works))) {
3610 spin_unlock_irq(&workqueue_lock);
Tejun Heo6183c002013-03-12 11:29:57 -07003611 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07003612 }
Tejun Heo6183c002013-03-12 11:29:57 -07003613 }
3614
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003615 /*
3616 * wq list is used to freeze wq, remove from list after
3617 * flushing is complete in case freeze races us.
3618 */
Tejun Heod2c1d402013-03-12 11:30:04 -07003619 list_del_init(&wq->list);
Tejun Heo76af4d92013-03-12 11:30:00 -07003620
Tejun Heoe98d5b12013-03-12 11:29:57 -07003621 spin_unlock_irq(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003622
Tejun Heo493008a2013-03-12 11:30:03 -07003623 if (wq->rescuer) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003624 kthread_stop(wq->rescuer->task);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003625 kfree(wq->rescuer);
Tejun Heo493008a2013-03-12 11:30:03 -07003626 wq->rescuer = NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +02003627 }
3628
Tejun Heo8864b4e2013-03-12 11:30:04 -07003629 if (!(wq->flags & WQ_UNBOUND)) {
3630 /*
3631 * The base ref is never dropped on per-cpu pwqs. Directly
3632 * free the pwqs and wq.
3633 */
3634 free_percpu(wq->cpu_pwqs);
3635 kfree(wq);
3636 } else {
3637 /*
3638 * We're the sole accessor of @wq at this point. Directly
3639 * access the first pwq and put the base ref. As both pwqs
3640 * and pools are sched-RCU protected, the lock operations
3641 * are safe. @wq will be freed when the last pwq is
3642 * released.
3643 */
Tejun Heo29c91e92013-03-12 11:30:03 -07003644 pwq = list_first_entry(&wq->pwqs, struct pool_workqueue,
3645 pwqs_node);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003646 spin_lock_irq(&pwq->pool->lock);
3647 put_pwq(pwq);
3648 spin_unlock_irq(&pwq->pool->lock);
Tejun Heo29c91e92013-03-12 11:30:03 -07003649 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003650}
3651EXPORT_SYMBOL_GPL(destroy_workqueue);
3652
Tejun Heodcd989c2010-06-29 10:07:14 +02003653/**
Tejun Heo112202d2013-02-13 19:29:12 -08003654 * pwq_set_max_active - adjust max_active of a pwq
3655 * @pwq: target pool_workqueue
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003656 * @max_active: new max_active value.
3657 *
Tejun Heo112202d2013-02-13 19:29:12 -08003658 * Set @pwq->max_active to @max_active and activate delayed works if
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003659 * increased.
3660 *
3661 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08003662 * spin_lock_irq(pool->lock).
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003663 */
Tejun Heo112202d2013-02-13 19:29:12 -08003664static void pwq_set_max_active(struct pool_workqueue *pwq, int max_active)
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003665{
Tejun Heo112202d2013-02-13 19:29:12 -08003666 pwq->max_active = max_active;
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003667
Tejun Heo112202d2013-02-13 19:29:12 -08003668 while (!list_empty(&pwq->delayed_works) &&
3669 pwq->nr_active < pwq->max_active)
3670 pwq_activate_first_delayed(pwq);
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003671}
3672
3673/**
Tejun Heodcd989c2010-06-29 10:07:14 +02003674 * workqueue_set_max_active - adjust max_active of a workqueue
3675 * @wq: target workqueue
3676 * @max_active: new max_active value.
3677 *
3678 * Set max_active of @wq to @max_active.
3679 *
3680 * CONTEXT:
3681 * Don't call from IRQ context.
3682 */
3683void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3684{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003685 struct pool_workqueue *pwq;
Tejun Heodcd989c2010-06-29 10:07:14 +02003686
Tejun Heof3421792010-07-02 10:03:51 +02003687 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003688
Tejun Heoe98d5b12013-03-12 11:29:57 -07003689 spin_lock_irq(&workqueue_lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02003690
3691 wq->saved_max_active = max_active;
3692
Tejun Heo49e3cf42013-03-12 11:29:58 -07003693 for_each_pwq(pwq, wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08003694 struct worker_pool *pool = pwq->pool;
Tejun Heodcd989c2010-06-29 10:07:14 +02003695
Tejun Heoe98d5b12013-03-12 11:29:57 -07003696 spin_lock(&pool->lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02003697
Tejun Heo58a69cb2011-02-16 09:25:31 +01003698 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heo35b6bb62013-01-24 11:01:33 -08003699 !(pool->flags & POOL_FREEZING))
Tejun Heo112202d2013-02-13 19:29:12 -08003700 pwq_set_max_active(pwq, max_active);
Tejun Heodcd989c2010-06-29 10:07:14 +02003701
Tejun Heoe98d5b12013-03-12 11:29:57 -07003702 spin_unlock(&pool->lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02003703 }
3704
Tejun Heoe98d5b12013-03-12 11:29:57 -07003705 spin_unlock_irq(&workqueue_lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02003706}
3707EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3708
3709/**
3710 * workqueue_congested - test whether a workqueue is congested
3711 * @cpu: CPU in question
3712 * @wq: target workqueue
3713 *
3714 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3715 * no synchronization around this function and the test result is
3716 * unreliable and only useful as advisory hints or for debugging.
3717 *
3718 * RETURNS:
3719 * %true if congested, %false otherwise.
3720 */
Tejun Heod84ff052013-03-12 11:29:59 -07003721bool workqueue_congested(int cpu, struct workqueue_struct *wq)
Tejun Heodcd989c2010-06-29 10:07:14 +02003722{
Tejun Heo7fb98ea2013-03-12 11:30:00 -07003723 struct pool_workqueue *pwq;
Tejun Heo76af4d92013-03-12 11:30:00 -07003724 bool ret;
3725
3726 preempt_disable();
Tejun Heo7fb98ea2013-03-12 11:30:00 -07003727
3728 if (!(wq->flags & WQ_UNBOUND))
3729 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
3730 else
3731 pwq = first_pwq(wq);
Tejun Heodcd989c2010-06-29 10:07:14 +02003732
Tejun Heo76af4d92013-03-12 11:30:00 -07003733 ret = !list_empty(&pwq->delayed_works);
3734 preempt_enable();
3735
3736 return ret;
Tejun Heodcd989c2010-06-29 10:07:14 +02003737}
3738EXPORT_SYMBOL_GPL(workqueue_congested);
3739
3740/**
Tejun Heodcd989c2010-06-29 10:07:14 +02003741 * work_busy - test whether a work is currently pending or running
3742 * @work: the work to be tested
3743 *
3744 * Test whether @work is currently pending or running. There is no
3745 * synchronization around this function and the test result is
3746 * unreliable and only useful as advisory hints or for debugging.
Tejun Heodcd989c2010-06-29 10:07:14 +02003747 *
3748 * RETURNS:
3749 * OR'd bitmask of WORK_BUSY_* bits.
3750 */
3751unsigned int work_busy(struct work_struct *work)
3752{
Tejun Heofa1b54e2013-03-12 11:30:00 -07003753 struct worker_pool *pool;
Tejun Heodcd989c2010-06-29 10:07:14 +02003754 unsigned long flags;
3755 unsigned int ret = 0;
3756
Tejun Heodcd989c2010-06-29 10:07:14 +02003757 if (work_pending(work))
3758 ret |= WORK_BUSY_PENDING;
Tejun Heodcd989c2010-06-29 10:07:14 +02003759
Tejun Heofa1b54e2013-03-12 11:30:00 -07003760 local_irq_save(flags);
3761 pool = get_work_pool(work);
Lai Jiangshan038366c2013-02-06 18:04:53 -08003762 if (pool) {
Tejun Heofa1b54e2013-03-12 11:30:00 -07003763 spin_lock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08003764 if (find_worker_executing_work(pool, work))
3765 ret |= WORK_BUSY_RUNNING;
Tejun Heofa1b54e2013-03-12 11:30:00 -07003766 spin_unlock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08003767 }
Tejun Heofa1b54e2013-03-12 11:30:00 -07003768 local_irq_restore(flags);
Tejun Heodcd989c2010-06-29 10:07:14 +02003769
3770 return ret;
3771}
3772EXPORT_SYMBOL_GPL(work_busy);
3773
Tejun Heodb7bccf2010-06-29 10:07:12 +02003774/*
3775 * CPU hotplug.
3776 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003777 * There are two challenges in supporting CPU hotplug. Firstly, there
Tejun Heo112202d2013-02-13 19:29:12 -08003778 * are a lot of assumptions on strong associations among work, pwq and
Tejun Heo706026c2013-01-24 11:01:34 -08003779 * pool which make migrating pending and scheduled works very
Tejun Heoe22bee72010-06-29 10:07:14 +02003780 * difficult to implement without impacting hot paths. Secondly,
Tejun Heo94cf58b2013-01-24 11:01:33 -08003781 * worker pools serve mix of short, long and very long running works making
Tejun Heoe22bee72010-06-29 10:07:14 +02003782 * blocked draining impractical.
3783 *
Tejun Heo24647572013-01-24 11:01:33 -08003784 * This is solved by allowing the pools to be disassociated from the CPU
Tejun Heo628c78e2012-07-17 12:39:27 -07003785 * running as an unbound one and allowing it to be reattached later if the
3786 * cpu comes back online.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003787 */
3788
Tejun Heo706026c2013-01-24 11:01:34 -08003789static void wq_unbind_fn(struct work_struct *work)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003790{
Tejun Heo38db41d2013-01-24 11:01:34 -08003791 int cpu = smp_processor_id();
Tejun Heo4ce62e92012-07-13 22:16:44 -07003792 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003793 struct worker *worker;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003794 int i;
3795
Tejun Heof02ae732013-03-12 11:30:03 -07003796 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo6183c002013-03-12 11:29:57 -07003797 WARN_ON_ONCE(cpu != smp_processor_id());
Tejun Heo94cf58b2013-01-24 11:01:33 -08003798
3799 mutex_lock(&pool->assoc_mutex);
3800 spin_lock_irq(&pool->lock);
3801
3802 /*
3803 * We've claimed all manager positions. Make all workers
3804 * unbound and set DISASSOCIATED. Before this, all workers
3805 * except for the ones which are still executing works from
3806 * before the last CPU down must be on the cpu. After
3807 * this, they may become diasporas.
3808 */
Tejun Heo4ce62e92012-07-13 22:16:44 -07003809 list_for_each_entry(worker, &pool->idle_list, entry)
Tejun Heo403c8212012-07-17 12:39:27 -07003810 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003811
Sasha Levinb67bfe02013-02-27 17:06:00 -08003812 for_each_busy_worker(worker, i, pool)
Tejun Heoc9e7cf22013-01-24 11:01:33 -08003813 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003814
Tejun Heo24647572013-01-24 11:01:33 -08003815 pool->flags |= POOL_DISASSOCIATED;
Tejun Heof2d5a0e2012-07-17 12:39:26 -07003816
Tejun Heo94cf58b2013-01-24 11:01:33 -08003817 spin_unlock_irq(&pool->lock);
3818 mutex_unlock(&pool->assoc_mutex);
3819 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003820
3821 /*
Tejun Heo628c78e2012-07-17 12:39:27 -07003822 * Call schedule() so that we cross rq->lock and thus can guarantee
3823 * sched callbacks see the %WORKER_UNBOUND flag. This is necessary
3824 * as scheduler callbacks may be invoked from other cpus.
3825 */
3826 schedule();
3827
3828 /*
3829 * Sched callbacks are disabled now. Zap nr_running. After this,
3830 * nr_running stays zero and need_more_worker() and keep_working()
Tejun Heo38db41d2013-01-24 11:01:34 -08003831 * are always true as long as the worklist is not empty. Pools on
3832 * @cpu now behave as unbound (in terms of concurrency management)
3833 * pools which are served by workers tied to the CPU.
Tejun Heo628c78e2012-07-17 12:39:27 -07003834 *
3835 * On return from this function, the current worker would trigger
3836 * unbound chain execution of pending work items if other workers
3837 * didn't already.
Tejun Heoe22bee72010-06-29 10:07:14 +02003838 */
Tejun Heof02ae732013-03-12 11:30:03 -07003839 for_each_cpu_worker_pool(pool, cpu)
Tejun Heoe19e3972013-01-24 11:39:44 -08003840 atomic_set(&pool->nr_running, 0);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003841}
3842
Tejun Heo8db25e72012-07-17 12:39:28 -07003843/*
3844 * Workqueues should be brought up before normal priority CPU notifiers.
3845 * This will be registered high priority CPU notifier.
3846 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07003847static int __cpuinit workqueue_cpu_up_callback(struct notifier_block *nfb,
Tejun Heo8db25e72012-07-17 12:39:28 -07003848 unsigned long action,
3849 void *hcpu)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003850{
Tejun Heod84ff052013-03-12 11:29:59 -07003851 int cpu = (unsigned long)hcpu;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003852 struct worker_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853
Tejun Heo8db25e72012-07-17 12:39:28 -07003854 switch (action & ~CPU_TASKS_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 case CPU_UP_PREPARE:
Tejun Heof02ae732013-03-12 11:30:03 -07003856 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo3ce63372012-07-17 12:39:27 -07003857 struct worker *worker;
3858
3859 if (pool->nr_workers)
3860 continue;
3861
3862 worker = create_worker(pool);
3863 if (!worker)
3864 return NOTIFY_BAD;
3865
Tejun Heod565ed62013-01-24 11:01:33 -08003866 spin_lock_irq(&pool->lock);
Tejun Heo3ce63372012-07-17 12:39:27 -07003867 start_worker(worker);
Tejun Heod565ed62013-01-24 11:01:33 -08003868 spin_unlock_irq(&pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003870 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003871
Tejun Heo65758202012-07-17 12:39:26 -07003872 case CPU_DOWN_FAILED:
3873 case CPU_ONLINE:
Tejun Heof02ae732013-03-12 11:30:03 -07003874 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo94cf58b2013-01-24 11:01:33 -08003875 mutex_lock(&pool->assoc_mutex);
3876 spin_lock_irq(&pool->lock);
3877
Tejun Heo24647572013-01-24 11:01:33 -08003878 pool->flags &= ~POOL_DISASSOCIATED;
Tejun Heo94cf58b2013-01-24 11:01:33 -08003879 rebind_workers(pool);
3880
3881 spin_unlock_irq(&pool->lock);
3882 mutex_unlock(&pool->assoc_mutex);
3883 }
Tejun Heo8db25e72012-07-17 12:39:28 -07003884 break;
Tejun Heo65758202012-07-17 12:39:26 -07003885 }
3886 return NOTIFY_OK;
3887}
3888
3889/*
3890 * Workqueues should be brought down after normal priority CPU notifiers.
3891 * This will be registered as low priority CPU notifier.
3892 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07003893static int __cpuinit workqueue_cpu_down_callback(struct notifier_block *nfb,
Tejun Heo65758202012-07-17 12:39:26 -07003894 unsigned long action,
3895 void *hcpu)
3896{
Tejun Heod84ff052013-03-12 11:29:59 -07003897 int cpu = (unsigned long)hcpu;
Tejun Heo8db25e72012-07-17 12:39:28 -07003898 struct work_struct unbind_work;
3899
Tejun Heo65758202012-07-17 12:39:26 -07003900 switch (action & ~CPU_TASKS_FROZEN) {
3901 case CPU_DOWN_PREPARE:
Tejun Heo8db25e72012-07-17 12:39:28 -07003902 /* unbinding should happen on the local CPU */
Tejun Heo706026c2013-01-24 11:01:34 -08003903 INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn);
Joonsoo Kim7635d2f2012-08-15 23:25:41 +09003904 queue_work_on(cpu, system_highpri_wq, &unbind_work);
Tejun Heo8db25e72012-07-17 12:39:28 -07003905 flush_work(&unbind_work);
3906 break;
Tejun Heo65758202012-07-17 12:39:26 -07003907 }
3908 return NOTIFY_OK;
3909}
3910
Rusty Russell2d3854a2008-11-05 13:39:10 +11003911#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003912
Rusty Russell2d3854a2008-11-05 13:39:10 +11003913struct work_for_cpu {
Tejun Heoed48ece2012-09-18 12:48:43 -07003914 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003915 long (*fn)(void *);
3916 void *arg;
3917 long ret;
3918};
3919
Tejun Heoed48ece2012-09-18 12:48:43 -07003920static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003921{
Tejun Heoed48ece2012-09-18 12:48:43 -07003922 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
3923
Rusty Russell2d3854a2008-11-05 13:39:10 +11003924 wfc->ret = wfc->fn(wfc->arg);
3925}
3926
3927/**
3928 * work_on_cpu - run a function in user context on a particular cpu
3929 * @cpu: the cpu to run on
3930 * @fn: the function to run
3931 * @arg: the function arg
3932 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003933 * This will return the value @fn returns.
3934 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b44003e2009-04-09 09:50:37 -06003935 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003936 */
Tejun Heod84ff052013-03-12 11:29:59 -07003937long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003938{
Tejun Heoed48ece2012-09-18 12:48:43 -07003939 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003940
Tejun Heoed48ece2012-09-18 12:48:43 -07003941 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
3942 schedule_work_on(cpu, &wfc.work);
3943 flush_work(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003944 return wfc.ret;
3945}
3946EXPORT_SYMBOL_GPL(work_on_cpu);
3947#endif /* CONFIG_SMP */
3948
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003949#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303950
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003951/**
3952 * freeze_workqueues_begin - begin freezing workqueues
3953 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003954 * Start freezing workqueues. After this function returns, all freezable
3955 * workqueues will queue new works to their frozen_works list instead of
Tejun Heo706026c2013-01-24 11:01:34 -08003956 * pool->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003957 *
3958 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08003959 * Grabs and releases workqueue_lock and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003960 */
3961void freeze_workqueues_begin(void)
3962{
Tejun Heo17116962013-03-12 11:29:58 -07003963 struct worker_pool *pool;
Tejun Heo24b8a842013-03-12 11:29:58 -07003964 struct workqueue_struct *wq;
3965 struct pool_workqueue *pwq;
Tejun Heo17116962013-03-12 11:29:58 -07003966 int id;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003967
Tejun Heoe98d5b12013-03-12 11:29:57 -07003968 spin_lock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003969
Tejun Heo6183c002013-03-12 11:29:57 -07003970 WARN_ON_ONCE(workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003971 workqueue_freezing = true;
3972
Tejun Heo24b8a842013-03-12 11:29:58 -07003973 /* set FREEZING */
Tejun Heo17116962013-03-12 11:29:58 -07003974 for_each_pool(pool, id) {
Tejun Heo17116962013-03-12 11:29:58 -07003975 spin_lock(&pool->lock);
Tejun Heo17116962013-03-12 11:29:58 -07003976 WARN_ON_ONCE(pool->flags & POOL_FREEZING);
3977 pool->flags |= POOL_FREEZING;
Tejun Heo17116962013-03-12 11:29:58 -07003978 spin_unlock(&pool->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003979 }
3980
Tejun Heo24b8a842013-03-12 11:29:58 -07003981 /* suppress further executions by setting max_active to zero */
3982 list_for_each_entry(wq, &workqueues, list) {
3983 if (!(wq->flags & WQ_FREEZABLE))
3984 continue;
3985
3986 for_each_pwq(pwq, wq) {
3987 spin_lock(&pwq->pool->lock);
3988 pwq->max_active = 0;
3989 spin_unlock(&pwq->pool->lock);
3990 }
3991 }
3992
Tejun Heoe98d5b12013-03-12 11:29:57 -07003993 spin_unlock_irq(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003995
3996/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003997 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003998 *
3999 * Check whether freezing is complete. This function must be called
4000 * between freeze_workqueues_begin() and thaw_workqueues().
4001 *
4002 * CONTEXT:
4003 * Grabs and releases workqueue_lock.
4004 *
4005 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01004006 * %true if some freezable workqueues are still busy. %false if freezing
4007 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004008 */
4009bool freeze_workqueues_busy(void)
4010{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004011 bool busy = false;
Tejun Heo24b8a842013-03-12 11:29:58 -07004012 struct workqueue_struct *wq;
4013 struct pool_workqueue *pwq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004014
Tejun Heoe98d5b12013-03-12 11:29:57 -07004015 spin_lock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004016
Tejun Heo6183c002013-03-12 11:29:57 -07004017 WARN_ON_ONCE(!workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004018
Tejun Heo24b8a842013-03-12 11:29:58 -07004019 list_for_each_entry(wq, &workqueues, list) {
4020 if (!(wq->flags & WQ_FREEZABLE))
4021 continue;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004022 /*
4023 * nr_active is monotonically decreasing. It's safe
4024 * to peek without lock.
4025 */
Tejun Heo24b8a842013-03-12 11:29:58 -07004026 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07004027 WARN_ON_ONCE(pwq->nr_active < 0);
Tejun Heo112202d2013-02-13 19:29:12 -08004028 if (pwq->nr_active) {
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004029 busy = true;
4030 goto out_unlock;
4031 }
4032 }
4033 }
4034out_unlock:
Tejun Heoe98d5b12013-03-12 11:29:57 -07004035 spin_unlock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004036 return busy;
4037}
4038
4039/**
4040 * thaw_workqueues - thaw workqueues
4041 *
4042 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo706026c2013-01-24 11:01:34 -08004043 * frozen works are transferred to their respective pool worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004044 *
4045 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08004046 * Grabs and releases workqueue_lock and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004047 */
4048void thaw_workqueues(void)
4049{
Tejun Heo24b8a842013-03-12 11:29:58 -07004050 struct workqueue_struct *wq;
4051 struct pool_workqueue *pwq;
4052 struct worker_pool *pool;
4053 int id;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004054
Tejun Heoe98d5b12013-03-12 11:29:57 -07004055 spin_lock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004056
4057 if (!workqueue_freezing)
4058 goto out_unlock;
4059
Tejun Heo24b8a842013-03-12 11:29:58 -07004060 /* clear FREEZING */
4061 for_each_pool(pool, id) {
4062 spin_lock(&pool->lock);
4063 WARN_ON_ONCE(!(pool->flags & POOL_FREEZING));
4064 pool->flags &= ~POOL_FREEZING;
4065 spin_unlock(&pool->lock);
4066 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02004067
Tejun Heo24b8a842013-03-12 11:29:58 -07004068 /* restore max_active and repopulate worklist */
4069 list_for_each_entry(wq, &workqueues, list) {
4070 if (!(wq->flags & WQ_FREEZABLE))
4071 continue;
Tejun Heod565ed62013-01-24 11:01:33 -08004072
Tejun Heo24b8a842013-03-12 11:29:58 -07004073 for_each_pwq(pwq, wq) {
4074 spin_lock(&pwq->pool->lock);
4075 pwq_set_max_active(pwq, wq->saved_max_active);
4076 spin_unlock(&pwq->pool->lock);
Tejun Heod565ed62013-01-24 11:01:33 -08004077 }
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004078 }
4079
Tejun Heo24b8a842013-03-12 11:29:58 -07004080 /* kick workers */
4081 for_each_pool(pool, id) {
4082 spin_lock(&pool->lock);
4083 wake_up_worker(pool);
4084 spin_unlock(&pool->lock);
4085 }
4086
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004087 workqueue_freezing = false;
4088out_unlock:
Tejun Heoe98d5b12013-03-12 11:29:57 -07004089 spin_unlock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004090}
4091#endif /* CONFIG_FREEZER */
4092
Suresh Siddha6ee05782010-07-30 14:57:37 -07004093static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094{
Tejun Heo7a4e3442013-03-12 11:30:00 -07004095 int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL };
4096 int i, cpu;
Tejun Heoc34056a2010-06-29 10:07:11 +02004097
Tejun Heo7c3eed52013-01-24 11:01:33 -08004098 /* make sure we have enough bits for OFFQ pool ID */
4099 BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) <
Lai Jiangshan6be19582013-02-06 18:04:53 -08004100 WORK_CPU_END * NR_STD_WORKER_POOLS);
Tejun Heob5490072012-08-03 10:30:46 -07004101
Tejun Heoe904e6c2013-03-12 11:29:57 -07004102 WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long));
4103
4104 pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC);
4105
Tejun Heo65758202012-07-17 12:39:26 -07004106 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
Lai Jiangshana5b4e572012-09-18 09:59:23 -07004107 hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02004108
Tejun Heo706026c2013-01-24 11:01:34 -08004109 /* initialize CPU pools */
Tejun Heo29c91e92013-03-12 11:30:03 -07004110 for_each_possible_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07004111 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02004112
Tejun Heo7a4e3442013-03-12 11:30:00 -07004113 i = 0;
Tejun Heof02ae732013-03-12 11:30:03 -07004114 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo7a4e3442013-03-12 11:30:00 -07004115 BUG_ON(init_worker_pool(pool));
Tejun Heoec22ca52013-01-24 11:01:33 -08004116 pool->cpu = cpu;
Tejun Heo29c91e92013-03-12 11:30:03 -07004117 cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu));
Tejun Heo7a4e3442013-03-12 11:30:00 -07004118 pool->attrs->nice = std_nice[i++];
4119
Tejun Heo9daf9e62013-01-24 11:01:33 -08004120 /* alloc pool ID */
4121 BUG_ON(worker_pool_assign_id(pool));
Tejun Heo4ce62e92012-07-13 22:16:44 -07004122 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02004123 }
4124
Tejun Heoe22bee72010-06-29 10:07:14 +02004125 /* create the initial worker */
Tejun Heo29c91e92013-03-12 11:30:03 -07004126 for_each_online_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07004127 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02004128
Tejun Heof02ae732013-03-12 11:30:03 -07004129 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07004130 struct worker *worker;
4131
Tejun Heo29c91e92013-03-12 11:30:03 -07004132 pool->flags &= ~POOL_DISASSOCIATED;
Tejun Heo24647572013-01-24 11:01:33 -08004133
Tejun Heobc2ae0f2012-07-17 12:39:27 -07004134 worker = create_worker(pool);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004135 BUG_ON(!worker);
Tejun Heod565ed62013-01-24 11:01:33 -08004136 spin_lock_irq(&pool->lock);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004137 start_worker(worker);
Tejun Heod565ed62013-01-24 11:01:33 -08004138 spin_unlock_irq(&pool->lock);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004139 }
Tejun Heoe22bee72010-06-29 10:07:14 +02004140 }
4141
Tejun Heo29c91e92013-03-12 11:30:03 -07004142 /* create default unbound wq attrs */
4143 for (i = 0; i < NR_STD_WORKER_POOLS; i++) {
4144 struct workqueue_attrs *attrs;
4145
4146 BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL)));
4147
4148 attrs->nice = std_nice[i];
4149 cpumask_setall(attrs->cpumask);
4150
4151 unbound_std_wq_attrs[i] = attrs;
4152 }
4153
Tejun Heod320c032010-06-29 10:07:14 +02004154 system_wq = alloc_workqueue("events", 0, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09004155 system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
Tejun Heod320c032010-06-29 10:07:14 +02004156 system_long_wq = alloc_workqueue("events_long", 0, 0);
Tejun Heof3421792010-07-02 10:03:51 +02004157 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
4158 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01004159 system_freezable_wq = alloc_workqueue("events_freezable",
4160 WQ_FREEZABLE, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09004161 BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
Tejun Heoae930e02012-08-20 14:51:23 -07004162 !system_unbound_wq || !system_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07004163 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164}
Suresh Siddha6ee05782010-07-30 14:57:37 -07004165early_initcall(init_workqueues);