blob: 16fb6747276a150e3626f6c2b38bb15ed86528bd [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 Heo75ccf592013-03-12 11:30:04 -0700125 *
126 * FR: wq->flush_mutex and workqueue_lock protected for writes. Sched-RCU
127 * protected for reads.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200128 */
129
Tejun Heo2eaebdb2013-01-18 14:05:55 -0800130/* struct worker is defined in workqueue_internal.h */
Tejun Heoc34056a2010-06-29 10:07:11 +0200131
Tejun Heobd7bdd42012-07-12 14:46:37 -0700132struct worker_pool {
Tejun Heod565ed62013-01-24 11:01:33 -0800133 spinlock_t lock; /* the pool lock */
Tejun Heod84ff052013-03-12 11:29:59 -0700134 int cpu; /* I: the associated cpu */
Tejun Heo9daf9e62013-01-24 11:01:33 -0800135 int id; /* I: pool ID */
Tejun Heo11ebea52012-07-12 14:46:37 -0700136 unsigned int flags; /* X: flags */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700137
138 struct list_head worklist; /* L: list of pending works */
139 int nr_workers; /* L: total number of workers */
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700140
141 /* nr_idle includes the ones off idle_list for rebinding */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700142 int nr_idle; /* L: currently idle ones */
143
144 struct list_head idle_list; /* X: list of idle workers */
145 struct timer_list idle_timer; /* L: worker idle timeout */
146 struct timer_list mayday_timer; /* L: SOS timer for workers */
147
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800148 /* workers are chained either in busy_hash or idle_list */
149 DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER);
150 /* L: hash of busy workers */
151
Tejun Heo34a06bd2013-03-12 11:30:00 -0700152 struct mutex manager_arb; /* manager arbitration */
Tejun Heo24647572013-01-24 11:01:33 -0800153 struct mutex assoc_mutex; /* protect POOL_DISASSOCIATED */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700154 struct ida worker_ida; /* L: for worker IDs */
Tejun Heoe19e3972013-01-24 11:39:44 -0800155
Tejun Heo7a4e3442013-03-12 11:30:00 -0700156 struct workqueue_attrs *attrs; /* I: worker attributes */
Tejun Heo29c91e92013-03-12 11:30:03 -0700157 struct hlist_node hash_node; /* R: unbound_pool_hash node */
158 int refcnt; /* refcnt for unbound pools */
Tejun Heo7a4e3442013-03-12 11:30:00 -0700159
Tejun Heoe19e3972013-01-24 11:39:44 -0800160 /*
161 * The current concurrency level. As it's likely to be accessed
162 * from other CPUs during try_to_wake_up(), put it in a separate
163 * cacheline.
164 */
165 atomic_t nr_running ____cacheline_aligned_in_smp;
Tejun Heo29c91e92013-03-12 11:30:03 -0700166
167 /*
168 * Destruction of pool is sched-RCU protected to allow dereferences
169 * from get_work_pool().
170 */
171 struct rcu_head rcu;
Tejun Heo8b03ae32010-06-29 10:07:12 +0200172} ____cacheline_aligned_in_smp;
173
174/*
Tejun Heo112202d2013-02-13 19:29:12 -0800175 * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS
176 * of work_struct->data are used for flags and the remaining high bits
177 * point to the pwq; thus, pwqs need to be aligned at two's power of the
178 * number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 */
Tejun Heo112202d2013-02-13 19:29:12 -0800180struct pool_workqueue {
Tejun Heobd7bdd42012-07-12 14:46:37 -0700181 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200182 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200183 int work_color; /* L: current color */
184 int flush_color; /* L: flushing color */
Tejun Heo8864b4e2013-03-12 11:30:04 -0700185 int refcnt; /* L: reference count */
Tejun Heo73f53c42010-06-29 10:07:11 +0200186 int nr_in_flight[WORK_NR_COLORS];
187 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200188 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200189 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200190 struct list_head delayed_works; /* L: delayed works */
Tejun Heo75ccf592013-03-12 11:30:04 -0700191 struct list_head pwqs_node; /* FR: node on wq->pwqs */
Tejun Heo493a1722013-03-12 11:29:59 -0700192 struct list_head mayday_node; /* W: node on wq->maydays */
Tejun Heo8864b4e2013-03-12 11:30:04 -0700193
194 /*
195 * Release of unbound pwq is punted to system_wq. See put_pwq()
196 * and pwq_unbound_release_workfn() for details. pool_workqueue
197 * itself is also sched-RCU protected so that the first pwq can be
198 * determined without grabbing workqueue_lock.
199 */
200 struct work_struct unbound_release_work;
201 struct rcu_head rcu;
Tejun Heoe904e6c2013-03-12 11:29:57 -0700202} __aligned(1 << WORK_STRUCT_FLAG_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200205 * Structure used to wait for workqueue flush.
206 */
207struct wq_flusher {
208 struct list_head list; /* F: list of flushers */
209 int flush_color; /* F: flush color waiting for */
210 struct completion done; /* flush completion */
211};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Tejun Heo73f53c42010-06-29 10:07:11 +0200213/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 * The externally visible workqueue abstraction is an array of
215 * per-CPU workqueues:
216 */
217struct workqueue_struct {
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200218 unsigned int flags; /* W: WQ_* flags */
Tejun Heo420c0dd2013-03-12 11:29:59 -0700219 struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwq's */
Tejun Heo75ccf592013-03-12 11:30:04 -0700220 struct list_head pwqs; /* FR: all pwqs of this wq */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200221 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200222
223 struct mutex flush_mutex; /* protects wq flushing */
224 int work_color; /* F: current work color */
225 int flush_color; /* F: current flush color */
Tejun Heo112202d2013-02-13 19:29:12 -0800226 atomic_t nr_pwqs_to_flush; /* flush in progress */
Tejun Heo73f53c42010-06-29 10:07:11 +0200227 struct wq_flusher *first_flusher; /* F: first flusher */
228 struct list_head flusher_queue; /* F: flush waiters */
229 struct list_head flusher_overflow; /* F: flush overflow list */
230
Tejun Heo493a1722013-03-12 11:29:59 -0700231 struct list_head maydays; /* W: pwqs requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200232 struct worker *rescuer; /* I: rescue worker */
233
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200234 int nr_drainers; /* W: drain in progress */
Tejun Heo112202d2013-02-13 19:29:12 -0800235 int saved_max_active; /* W: saved pwq max_active */
Johannes Berg4e6045f2007-10-18 23:39:55 -0700236#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200237 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700238#endif
Tejun Heob196be82012-01-10 15:11:35 -0800239 char name[]; /* I: workqueue name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240};
241
Tejun Heoe904e6c2013-03-12 11:29:57 -0700242static struct kmem_cache *pwq_cache;
243
Tejun Heo29c91e92013-03-12 11:30:03 -0700244/* hash of all unbound pools keyed by pool->attrs */
245static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER);
246
247static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS];
248
Tejun Heod320c032010-06-29 10:07:14 +0200249struct workqueue_struct *system_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200250EXPORT_SYMBOL_GPL(system_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300251struct workqueue_struct *system_highpri_wq __read_mostly;
Joonsoo Kim1aabe902012-08-15 23:25:39 +0900252EXPORT_SYMBOL_GPL(system_highpri_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300253struct workqueue_struct *system_long_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200254EXPORT_SYMBOL_GPL(system_long_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300255struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200256EXPORT_SYMBOL_GPL(system_unbound_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300257struct workqueue_struct *system_freezable_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100258EXPORT_SYMBOL_GPL(system_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200259
Tejun Heo97bd2342010-10-05 10:41:14 +0200260#define CREATE_TRACE_POINTS
261#include <trace/events/workqueue.h>
262
Tejun Heo76af4d92013-03-12 11:30:00 -0700263#define assert_rcu_or_wq_lock() \
264 rcu_lockdep_assert(rcu_read_lock_sched_held() || \
265 lockdep_is_held(&workqueue_lock), \
266 "sched RCU or workqueue lock should be held")
267
Tejun Heof02ae732013-03-12 11:30:03 -0700268#define for_each_cpu_worker_pool(pool, cpu) \
269 for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \
270 (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \
Tejun Heo7a62c2c2013-03-12 11:30:03 -0700271 (pool)++)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700272
Sasha Levinb67bfe02013-02-27 17:06:00 -0800273#define for_each_busy_worker(worker, i, pool) \
274 hash_for_each(pool->busy_hash, i, worker, hentry)
Tejun Heodb7bccf2010-06-29 10:07:12 +0200275
Tejun Heo49e3cf42013-03-12 11:29:58 -0700276/**
Tejun Heo17116962013-03-12 11:29:58 -0700277 * for_each_pool - iterate through all worker_pools in the system
278 * @pool: iteration cursor
279 * @id: integer used for iteration
Tejun Heofa1b54e2013-03-12 11:30:00 -0700280 *
281 * This must be called either with workqueue_lock held or sched RCU read
282 * locked. If the pool needs to be used beyond the locking in effect, the
283 * caller is responsible for guaranteeing that the pool stays online.
284 *
285 * The if/else clause exists only for the lockdep assertion and can be
286 * ignored.
Tejun Heo17116962013-03-12 11:29:58 -0700287 */
288#define for_each_pool(pool, id) \
Tejun Heofa1b54e2013-03-12 11:30:00 -0700289 idr_for_each_entry(&worker_pool_idr, pool, id) \
290 if (({ assert_rcu_or_wq_lock(); false; })) { } \
291 else
Tejun Heo17116962013-03-12 11:29:58 -0700292
293/**
Tejun Heo49e3cf42013-03-12 11:29:58 -0700294 * for_each_pwq - iterate through all pool_workqueues of the specified workqueue
295 * @pwq: iteration cursor
296 * @wq: the target workqueue
Tejun Heo76af4d92013-03-12 11:30:00 -0700297 *
298 * This must be called either with workqueue_lock held or sched RCU read
299 * locked. If the pwq needs to be used beyond the locking in effect, the
300 * caller is responsible for guaranteeing that the pwq stays online.
301 *
302 * The if/else clause exists only for the lockdep assertion and can be
303 * ignored.
Tejun Heo49e3cf42013-03-12 11:29:58 -0700304 */
305#define for_each_pwq(pwq, wq) \
Tejun Heo76af4d92013-03-12 11:30:00 -0700306 list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node) \
307 if (({ assert_rcu_or_wq_lock(); false; })) { } \
308 else
Tejun Heof3421792010-07-02 10:03:51 +0200309
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900310#ifdef CONFIG_DEBUG_OBJECTS_WORK
311
312static struct debug_obj_descr work_debug_descr;
313
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100314static void *work_debug_hint(void *addr)
315{
316 return ((struct work_struct *) addr)->func;
317}
318
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900319/*
320 * fixup_init is called when:
321 * - an active object is initialized
322 */
323static int work_fixup_init(void *addr, enum debug_obj_state state)
324{
325 struct work_struct *work = addr;
326
327 switch (state) {
328 case ODEBUG_STATE_ACTIVE:
329 cancel_work_sync(work);
330 debug_object_init(work, &work_debug_descr);
331 return 1;
332 default:
333 return 0;
334 }
335}
336
337/*
338 * fixup_activate is called when:
339 * - an active object is activated
340 * - an unknown object is activated (might be a statically initialized object)
341 */
342static int work_fixup_activate(void *addr, enum debug_obj_state state)
343{
344 struct work_struct *work = addr;
345
346 switch (state) {
347
348 case ODEBUG_STATE_NOTAVAILABLE:
349 /*
350 * This is not really a fixup. The work struct was
351 * statically initialized. We just make sure that it
352 * is tracked in the object tracker.
353 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200354 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900355 debug_object_init(work, &work_debug_descr);
356 debug_object_activate(work, &work_debug_descr);
357 return 0;
358 }
359 WARN_ON_ONCE(1);
360 return 0;
361
362 case ODEBUG_STATE_ACTIVE:
363 WARN_ON(1);
364
365 default:
366 return 0;
367 }
368}
369
370/*
371 * fixup_free is called when:
372 * - an active object is freed
373 */
374static int work_fixup_free(void *addr, enum debug_obj_state state)
375{
376 struct work_struct *work = addr;
377
378 switch (state) {
379 case ODEBUG_STATE_ACTIVE:
380 cancel_work_sync(work);
381 debug_object_free(work, &work_debug_descr);
382 return 1;
383 default:
384 return 0;
385 }
386}
387
388static struct debug_obj_descr work_debug_descr = {
389 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100390 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900391 .fixup_init = work_fixup_init,
392 .fixup_activate = work_fixup_activate,
393 .fixup_free = work_fixup_free,
394};
395
396static inline void debug_work_activate(struct work_struct *work)
397{
398 debug_object_activate(work, &work_debug_descr);
399}
400
401static inline void debug_work_deactivate(struct work_struct *work)
402{
403 debug_object_deactivate(work, &work_debug_descr);
404}
405
406void __init_work(struct work_struct *work, int onstack)
407{
408 if (onstack)
409 debug_object_init_on_stack(work, &work_debug_descr);
410 else
411 debug_object_init(work, &work_debug_descr);
412}
413EXPORT_SYMBOL_GPL(__init_work);
414
415void destroy_work_on_stack(struct work_struct *work)
416{
417 debug_object_free(work, &work_debug_descr);
418}
419EXPORT_SYMBOL_GPL(destroy_work_on_stack);
420
421#else
422static inline void debug_work_activate(struct work_struct *work) { }
423static inline void debug_work_deactivate(struct work_struct *work) { }
424#endif
425
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100426/* Serializes the accesses to the list of workqueues. */
427static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428static LIST_HEAD(workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200429static bool workqueue_freezing; /* W: have wqs started freezing? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Oleg Nesterov14441962007-05-23 13:57:57 -0700431/*
Tejun Heoe19e3972013-01-24 11:39:44 -0800432 * The CPU and unbound standard worker pools. The unbound ones have
433 * POOL_DISASSOCIATED set, and their workers have WORKER_UNBOUND set.
Oleg Nesterov14441962007-05-23 13:57:57 -0700434 */
Tejun Heoe19e3972013-01-24 11:39:44 -0800435static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS],
Tejun Heof02ae732013-03-12 11:30:03 -0700436 cpu_worker_pools);
Tejun Heof3421792010-07-02 10:03:51 +0200437
Tejun Heofa1b54e2013-03-12 11:30:00 -0700438/*
439 * idr of all pools. Modifications are protected by workqueue_lock. Read
440 * accesses are protected by sched-RCU protected.
441 */
Tejun Heo9daf9e62013-01-24 11:01:33 -0800442static DEFINE_IDR(worker_pool_idr);
443
Tejun Heoc34056a2010-06-29 10:07:11 +0200444static int worker_thread(void *__worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Tejun Heo9daf9e62013-01-24 11:01:33 -0800446/* allocate ID and assign it to @pool */
447static int worker_pool_assign_id(struct worker_pool *pool)
448{
449 int ret;
450
Tejun Heofa1b54e2013-03-12 11:30:00 -0700451 do {
452 if (!idr_pre_get(&worker_pool_idr, GFP_KERNEL))
453 return -ENOMEM;
454
455 spin_lock_irq(&workqueue_lock);
456 ret = idr_get_new(&worker_pool_idr, pool, &pool->id);
457 spin_unlock_irq(&workqueue_lock);
458 } while (ret == -EAGAIN);
Tejun Heo9daf9e62013-01-24 11:01:33 -0800459
460 return ret;
461}
462
Tejun Heo76af4d92013-03-12 11:30:00 -0700463/**
464 * first_pwq - return the first pool_workqueue of the specified workqueue
465 * @wq: the target workqueue
466 *
467 * This must be called either with workqueue_lock held or sched RCU read
468 * locked. If the pwq needs to be used beyond the locking in effect, the
469 * caller is responsible for guaranteeing that the pwq stays online.
470 */
Tejun Heo7fb98ea2013-03-12 11:30:00 -0700471static struct pool_workqueue *first_pwq(struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700472{
Tejun Heo76af4d92013-03-12 11:30:00 -0700473 assert_rcu_or_wq_lock();
474 return list_first_or_null_rcu(&wq->pwqs, struct pool_workqueue,
475 pwqs_node);
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700476}
477
Tejun Heo73f53c42010-06-29 10:07:11 +0200478static unsigned int work_color_to_flags(int color)
479{
480 return color << WORK_STRUCT_COLOR_SHIFT;
481}
482
483static int get_work_color(struct work_struct *work)
484{
485 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
486 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
487}
488
489static int work_next_color(int color)
490{
491 return (color + 1) % WORK_NR_COLORS;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700492}
493
David Howells4594bf12006-12-07 11:33:26 +0000494/*
Tejun Heo112202d2013-02-13 19:29:12 -0800495 * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data
496 * contain the pointer to the queued pwq. Once execution starts, the flag
Tejun Heo7c3eed52013-01-24 11:01:33 -0800497 * is cleared and the high bits contain OFFQ flags and pool ID.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200498 *
Tejun Heo112202d2013-02-13 19:29:12 -0800499 * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling()
500 * and clear_work_data() can be used to set the pwq, pool or clear
Tejun Heobbb68df2012-08-03 10:30:46 -0700501 * work->data. These functions should only be called while the work is
502 * owned - ie. while the PENDING bit is set.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200503 *
Tejun Heo112202d2013-02-13 19:29:12 -0800504 * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq
Tejun Heo7c3eed52013-01-24 11:01:33 -0800505 * corresponding to a work. Pool is available once the work has been
Tejun Heo112202d2013-02-13 19:29:12 -0800506 * queued anywhere after initialization until it is sync canceled. pwq is
Tejun Heo7c3eed52013-01-24 11:01:33 -0800507 * available only while the work item is queued.
Tejun Heobbb68df2012-08-03 10:30:46 -0700508 *
509 * %WORK_OFFQ_CANCELING is used to mark a work item which is being
510 * canceled. While being canceled, a work item may have its PENDING set
511 * but stay off timer and worklist for arbitrarily long and nobody should
512 * try to steal the PENDING bit.
David Howells4594bf12006-12-07 11:33:26 +0000513 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200514static inline void set_work_data(struct work_struct *work, unsigned long data,
515 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000516{
Tejun Heo6183c002013-03-12 11:29:57 -0700517 WARN_ON_ONCE(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200518 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000519}
David Howells365970a2006-11-22 14:54:49 +0000520
Tejun Heo112202d2013-02-13 19:29:12 -0800521static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq,
Tejun Heo7a22ad72010-06-29 10:07:13 +0200522 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200523{
Tejun Heo112202d2013-02-13 19:29:12 -0800524 set_work_data(work, (unsigned long)pwq,
525 WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200526}
527
Lai Jiangshan4468a002013-02-06 18:04:53 -0800528static void set_work_pool_and_keep_pending(struct work_struct *work,
529 int pool_id)
530{
531 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT,
532 WORK_STRUCT_PENDING);
533}
534
Tejun Heo7c3eed52013-01-24 11:01:33 -0800535static void set_work_pool_and_clear_pending(struct work_struct *work,
536 int pool_id)
David Howells365970a2006-11-22 14:54:49 +0000537{
Tejun Heo23657bb2012-08-13 17:08:19 -0700538 /*
539 * The following wmb is paired with the implied mb in
540 * test_and_set_bit(PENDING) and ensures all updates to @work made
541 * here are visible to and precede any updates by the next PENDING
542 * owner.
543 */
544 smp_wmb();
Tejun Heo7c3eed52013-01-24 11:01:33 -0800545 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200546}
547
548static void clear_work_data(struct work_struct *work)
549{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800550 smp_wmb(); /* see set_work_pool_and_clear_pending() */
551 set_work_data(work, WORK_STRUCT_NO_POOL, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200552}
553
Tejun Heo112202d2013-02-13 19:29:12 -0800554static struct pool_workqueue *get_work_pwq(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200555{
Tejun Heoe1201532010-07-22 14:14:25 +0200556 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200557
Tejun Heo112202d2013-02-13 19:29:12 -0800558 if (data & WORK_STRUCT_PWQ)
Tejun Heoe1201532010-07-22 14:14:25 +0200559 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
560 else
561 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200562}
563
Tejun Heo7c3eed52013-01-24 11:01:33 -0800564/**
565 * get_work_pool - return the worker_pool a given work was associated with
566 * @work: the work item of interest
567 *
568 * Return the worker_pool @work was last associated with. %NULL if none.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700569 *
570 * Pools are created and destroyed under workqueue_lock, and allows read
571 * access under sched-RCU read lock. As such, this function should be
572 * called under workqueue_lock or with preemption disabled.
573 *
574 * All fields of the returned pool are accessible as long as the above
575 * mentioned locking is in effect. If the returned pool needs to be used
576 * beyond the critical section, the caller is responsible for ensuring the
577 * returned pool is and stays online.
Tejun Heo7c3eed52013-01-24 11:01:33 -0800578 */
579static struct worker_pool *get_work_pool(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200580{
Tejun Heoe1201532010-07-22 14:14:25 +0200581 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800582 int pool_id;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200583
Tejun Heofa1b54e2013-03-12 11:30:00 -0700584 assert_rcu_or_wq_lock();
585
Tejun Heo112202d2013-02-13 19:29:12 -0800586 if (data & WORK_STRUCT_PWQ)
587 return ((struct pool_workqueue *)
Tejun Heo7c3eed52013-01-24 11:01:33 -0800588 (data & WORK_STRUCT_WQ_DATA_MASK))->pool;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200589
Tejun Heo7c3eed52013-01-24 11:01:33 -0800590 pool_id = data >> WORK_OFFQ_POOL_SHIFT;
591 if (pool_id == WORK_OFFQ_POOL_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200592 return NULL;
593
Tejun Heofa1b54e2013-03-12 11:30:00 -0700594 return idr_find(&worker_pool_idr, pool_id);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800595}
596
597/**
598 * get_work_pool_id - return the worker pool ID a given work is associated with
599 * @work: the work item of interest
600 *
601 * Return the worker_pool ID @work was last associated with.
602 * %WORK_OFFQ_POOL_NONE if none.
603 */
604static int get_work_pool_id(struct work_struct *work)
605{
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800606 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800607
Tejun Heo112202d2013-02-13 19:29:12 -0800608 if (data & WORK_STRUCT_PWQ)
609 return ((struct pool_workqueue *)
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800610 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id;
611
612 return data >> WORK_OFFQ_POOL_SHIFT;
Tejun Heo7c3eed52013-01-24 11:01:33 -0800613}
614
Tejun Heobbb68df2012-08-03 10:30:46 -0700615static void mark_work_canceling(struct work_struct *work)
616{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800617 unsigned long pool_id = get_work_pool_id(work);
Tejun Heobbb68df2012-08-03 10:30:46 -0700618
Tejun Heo7c3eed52013-01-24 11:01:33 -0800619 pool_id <<= WORK_OFFQ_POOL_SHIFT;
620 set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700621}
622
623static bool work_is_canceling(struct work_struct *work)
624{
625 unsigned long data = atomic_long_read(&work->data);
626
Tejun Heo112202d2013-02-13 19:29:12 -0800627 return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700628}
629
David Howells365970a2006-11-22 14:54:49 +0000630/*
Tejun Heo32704762012-07-13 22:16:45 -0700631 * Policy functions. These define the policies on how the global worker
632 * pools are managed. Unless noted otherwise, these functions assume that
Tejun Heod565ed62013-01-24 11:01:33 -0800633 * they're being called with pool->lock held.
David Howells365970a2006-11-22 14:54:49 +0000634 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200635
Tejun Heo63d95a92012-07-12 14:46:37 -0700636static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000637{
Tejun Heoe19e3972013-01-24 11:39:44 -0800638 return !atomic_read(&pool->nr_running);
David Howells365970a2006-11-22 14:54:49 +0000639}
640
Tejun Heoe22bee72010-06-29 10:07:14 +0200641/*
642 * Need to wake up a worker? Called from anything but currently
643 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700644 *
645 * Note that, because unbound workers never contribute to nr_running, this
Tejun Heo706026c2013-01-24 11:01:34 -0800646 * function will always return %true for unbound pools as long as the
Tejun Heo974271c2012-07-12 14:46:37 -0700647 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200648 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700649static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000650{
Tejun Heo63d95a92012-07-12 14:46:37 -0700651 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000652}
653
Tejun Heoe22bee72010-06-29 10:07:14 +0200654/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700655static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200656{
Tejun Heo63d95a92012-07-12 14:46:37 -0700657 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200658}
659
660/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700661static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200662{
Tejun Heoe19e3972013-01-24 11:39:44 -0800663 return !list_empty(&pool->worklist) &&
664 atomic_read(&pool->nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200665}
666
667/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700668static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200669{
Tejun Heo63d95a92012-07-12 14:46:37 -0700670 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200671}
672
673/* Do I need to be the manager? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700674static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200675{
Tejun Heo63d95a92012-07-12 14:46:37 -0700676 return need_to_create_worker(pool) ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700677 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200678}
679
680/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700681static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200682{
Tejun Heo34a06bd2013-03-12 11:30:00 -0700683 bool managing = mutex_is_locked(&pool->manager_arb);
Tejun Heo63d95a92012-07-12 14:46:37 -0700684 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
685 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200686
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700687 /*
688 * nr_idle and idle_list may disagree if idle rebinding is in
689 * progress. Never return %true if idle_list is empty.
690 */
691 if (list_empty(&pool->idle_list))
692 return false;
693
Tejun Heoe22bee72010-06-29 10:07:14 +0200694 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
695}
696
697/*
698 * Wake up functions.
699 */
700
Tejun Heo7e116292010-06-29 10:07:13 +0200701/* Return the first worker. Safe with preemption disabled */
Tejun Heo63d95a92012-07-12 14:46:37 -0700702static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200703{
Tejun Heo63d95a92012-07-12 14:46:37 -0700704 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200705 return NULL;
706
Tejun Heo63d95a92012-07-12 14:46:37 -0700707 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200708}
709
710/**
711 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700712 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200713 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700714 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200715 *
716 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800717 * spin_lock_irq(pool->lock).
Tejun Heo7e116292010-06-29 10:07:13 +0200718 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700719static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200720{
Tejun Heo63d95a92012-07-12 14:46:37 -0700721 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200722
723 if (likely(worker))
724 wake_up_process(worker->task);
725}
726
Tejun Heo4690c4a2010-06-29 10:07:10 +0200727/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200728 * wq_worker_waking_up - a worker is waking up
729 * @task: task waking up
730 * @cpu: CPU @task is waking up to
731 *
732 * This function is called during try_to_wake_up() when a worker is
733 * being awoken.
734 *
735 * CONTEXT:
736 * spin_lock_irq(rq->lock)
737 */
Tejun Heod84ff052013-03-12 11:29:59 -0700738void wq_worker_waking_up(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200739{
740 struct worker *worker = kthread_data(task);
741
Joonsoo Kim36576002012-10-26 23:03:49 +0900742 if (!(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoec22ca52013-01-24 11:01:33 -0800743 WARN_ON_ONCE(worker->pool->cpu != cpu);
Tejun Heoe19e3972013-01-24 11:39:44 -0800744 atomic_inc(&worker->pool->nr_running);
Joonsoo Kim36576002012-10-26 23:03:49 +0900745 }
Tejun Heoe22bee72010-06-29 10:07:14 +0200746}
747
748/**
749 * wq_worker_sleeping - a worker is going to sleep
750 * @task: task going to sleep
751 * @cpu: CPU in question, must be the current CPU number
752 *
753 * This function is called during schedule() when a busy worker is
754 * going to sleep. Worker on the same cpu can be woken up by
755 * returning pointer to its task.
756 *
757 * CONTEXT:
758 * spin_lock_irq(rq->lock)
759 *
760 * RETURNS:
761 * Worker task on @cpu to wake up, %NULL if none.
762 */
Tejun Heod84ff052013-03-12 11:29:59 -0700763struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200764{
765 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heo111c2252013-01-17 17:16:24 -0800766 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200767
Tejun Heo111c2252013-01-17 17:16:24 -0800768 /*
769 * Rescuers, which may not have all the fields set up like normal
770 * workers, also reach here, let's not access anything before
771 * checking NOT_RUNNING.
772 */
Steven Rostedt2d646722010-12-03 23:12:33 -0500773 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200774 return NULL;
775
Tejun Heo111c2252013-01-17 17:16:24 -0800776 pool = worker->pool;
Tejun Heo111c2252013-01-17 17:16:24 -0800777
Tejun Heoe22bee72010-06-29 10:07:14 +0200778 /* this can only happen on the local cpu */
Tejun Heo6183c002013-03-12 11:29:57 -0700779 if (WARN_ON_ONCE(cpu != raw_smp_processor_id()))
780 return NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +0200781
782 /*
783 * The counterpart of the following dec_and_test, implied mb,
784 * worklist not empty test sequence is in insert_work().
785 * Please read comment there.
786 *
Tejun Heo628c78e2012-07-17 12:39:27 -0700787 * NOT_RUNNING is clear. This means that we're bound to and
788 * running on the local cpu w/ rq lock held and preemption
789 * disabled, which in turn means that none else could be
Tejun Heod565ed62013-01-24 11:01:33 -0800790 * manipulating idle_list, so dereferencing idle_list without pool
Tejun Heo628c78e2012-07-17 12:39:27 -0700791 * lock is safe.
Tejun Heoe22bee72010-06-29 10:07:14 +0200792 */
Tejun Heoe19e3972013-01-24 11:39:44 -0800793 if (atomic_dec_and_test(&pool->nr_running) &&
794 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700795 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200796 return to_wakeup ? to_wakeup->task : NULL;
797}
798
799/**
800 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200801 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200802 * @flags: flags to set
803 * @wakeup: wakeup an idle worker if necessary
804 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200805 * Set @flags in @worker->flags and adjust nr_running accordingly. If
806 * nr_running becomes zero and @wakeup is %true, an idle worker is
807 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200808 *
Tejun Heocb444762010-07-02 10:03:50 +0200809 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800810 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200811 */
812static inline void worker_set_flags(struct worker *worker, unsigned int flags,
813 bool wakeup)
814{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700815 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200816
Tejun Heocb444762010-07-02 10:03:50 +0200817 WARN_ON_ONCE(worker->task != current);
818
Tejun Heoe22bee72010-06-29 10:07:14 +0200819 /*
820 * If transitioning into NOT_RUNNING, adjust nr_running and
821 * wake up an idle worker as necessary if requested by
822 * @wakeup.
823 */
824 if ((flags & WORKER_NOT_RUNNING) &&
825 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoe22bee72010-06-29 10:07:14 +0200826 if (wakeup) {
Tejun Heoe19e3972013-01-24 11:39:44 -0800827 if (atomic_dec_and_test(&pool->nr_running) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -0700828 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700829 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200830 } else
Tejun Heoe19e3972013-01-24 11:39:44 -0800831 atomic_dec(&pool->nr_running);
Tejun Heoe22bee72010-06-29 10:07:14 +0200832 }
833
Tejun Heod302f012010-06-29 10:07:13 +0200834 worker->flags |= flags;
835}
836
837/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200838 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200839 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200840 * @flags: flags to clear
841 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200842 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200843 *
Tejun Heocb444762010-07-02 10:03:50 +0200844 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800845 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200846 */
847static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
848{
Tejun Heo63d95a92012-07-12 14:46:37 -0700849 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200850 unsigned int oflags = worker->flags;
851
Tejun Heocb444762010-07-02 10:03:50 +0200852 WARN_ON_ONCE(worker->task != current);
853
Tejun Heod302f012010-06-29 10:07:13 +0200854 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200855
Tejun Heo42c025f2011-01-11 15:58:49 +0100856 /*
857 * If transitioning out of NOT_RUNNING, increment nr_running. Note
858 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
859 * of multiple flags, not a single flag.
860 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200861 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
862 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heoe19e3972013-01-24 11:39:44 -0800863 atomic_inc(&pool->nr_running);
Tejun Heod302f012010-06-29 10:07:13 +0200864}
865
866/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200867 * find_worker_executing_work - find worker which is executing a work
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800868 * @pool: pool of interest
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200869 * @work: work to find worker for
870 *
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800871 * Find a worker which is executing @work on @pool by searching
872 * @pool->busy_hash which is keyed by the address of @work. For a worker
Tejun Heoa2c1c572012-12-18 10:35:02 -0800873 * to match, its current execution should match the address of @work and
874 * its work function. This is to avoid unwanted dependency between
875 * unrelated work executions through a work item being recycled while still
876 * being executed.
877 *
878 * This is a bit tricky. A work item may be freed once its execution
879 * starts and nothing prevents the freed area from being recycled for
880 * another work item. If the same work item address ends up being reused
881 * before the original execution finishes, workqueue will identify the
882 * recycled work item as currently executing and make it wait until the
883 * current execution finishes, introducing an unwanted dependency.
884 *
885 * This function checks the work item address, work function and workqueue
886 * to avoid false positives. Note that this isn't complete as one may
887 * construct a work function which can introduce dependency onto itself
888 * through a recycled work item. Well, if somebody wants to shoot oneself
889 * in the foot that badly, there's only so much we can do, and if such
890 * deadlock actually occurs, it should be easy to locate the culprit work
891 * function.
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200892 *
893 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800894 * spin_lock_irq(pool->lock).
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200895 *
896 * RETURNS:
897 * Pointer to worker which is executing @work if found, NULL
898 * otherwise.
899 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800900static struct worker *find_worker_executing_work(struct worker_pool *pool,
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200901 struct work_struct *work)
902{
Sasha Levin42f85702012-12-17 10:01:23 -0500903 struct worker *worker;
Sasha Levin42f85702012-12-17 10:01:23 -0500904
Sasha Levinb67bfe02013-02-27 17:06:00 -0800905 hash_for_each_possible(pool->busy_hash, worker, hentry,
Tejun Heoa2c1c572012-12-18 10:35:02 -0800906 (unsigned long)work)
907 if (worker->current_work == work &&
908 worker->current_func == work->func)
Sasha Levin42f85702012-12-17 10:01:23 -0500909 return worker;
910
911 return NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200912}
913
914/**
Tejun Heobf4ede02012-08-03 10:30:46 -0700915 * move_linked_works - move linked works to a list
916 * @work: start of series of works to be scheduled
917 * @head: target list to append @work to
918 * @nextp: out paramter for nested worklist walking
919 *
920 * Schedule linked works starting from @work to @head. Work series to
921 * be scheduled starts at @work and includes any consecutive work with
922 * WORK_STRUCT_LINKED set in its predecessor.
923 *
924 * If @nextp is not NULL, it's updated to point to the next work of
925 * the last scheduled work. This allows move_linked_works() to be
926 * nested inside outer list_for_each_entry_safe().
927 *
928 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800929 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -0700930 */
931static void move_linked_works(struct work_struct *work, struct list_head *head,
932 struct work_struct **nextp)
933{
934 struct work_struct *n;
935
936 /*
937 * Linked worklist will always end before the end of the list,
938 * use NULL for list head.
939 */
940 list_for_each_entry_safe_from(work, n, NULL, entry) {
941 list_move_tail(&work->entry, head);
942 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
943 break;
944 }
945
946 /*
947 * If we're already inside safe list traversal and have moved
948 * multiple works to the scheduled queue, the next position
949 * needs to be updated.
950 */
951 if (nextp)
952 *nextp = n;
953}
954
Tejun Heo8864b4e2013-03-12 11:30:04 -0700955/**
956 * get_pwq - get an extra reference on the specified pool_workqueue
957 * @pwq: pool_workqueue to get
958 *
959 * Obtain an extra reference on @pwq. The caller should guarantee that
960 * @pwq has positive refcnt and be holding the matching pool->lock.
961 */
962static void get_pwq(struct pool_workqueue *pwq)
963{
964 lockdep_assert_held(&pwq->pool->lock);
965 WARN_ON_ONCE(pwq->refcnt <= 0);
966 pwq->refcnt++;
967}
968
969/**
970 * put_pwq - put a pool_workqueue reference
971 * @pwq: pool_workqueue to put
972 *
973 * Drop a reference of @pwq. If its refcnt reaches zero, schedule its
974 * destruction. The caller should be holding the matching pool->lock.
975 */
976static void put_pwq(struct pool_workqueue *pwq)
977{
978 lockdep_assert_held(&pwq->pool->lock);
979 if (likely(--pwq->refcnt))
980 return;
981 if (WARN_ON_ONCE(!(pwq->wq->flags & WQ_UNBOUND)))
982 return;
983 /*
984 * @pwq can't be released under pool->lock, bounce to
985 * pwq_unbound_release_workfn(). This never recurses on the same
986 * pool->lock as this path is taken only for unbound workqueues and
987 * the release work item is scheduled on a per-cpu workqueue. To
988 * avoid lockdep warning, unbound pool->locks are given lockdep
989 * subclass of 1 in get_unbound_pool().
990 */
991 schedule_work(&pwq->unbound_release_work);
992}
993
Tejun Heo112202d2013-02-13 19:29:12 -0800994static void pwq_activate_delayed_work(struct work_struct *work)
Tejun Heobf4ede02012-08-03 10:30:46 -0700995{
Tejun Heo112202d2013-02-13 19:29:12 -0800996 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobf4ede02012-08-03 10:30:46 -0700997
998 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -0800999 move_linked_works(work, &pwq->pool->worklist, NULL);
Tejun Heobf4ede02012-08-03 10:30:46 -07001000 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo112202d2013-02-13 19:29:12 -08001001 pwq->nr_active++;
Tejun Heobf4ede02012-08-03 10:30:46 -07001002}
1003
Tejun Heo112202d2013-02-13 19:29:12 -08001004static void pwq_activate_first_delayed(struct pool_workqueue *pwq)
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001005{
Tejun Heo112202d2013-02-13 19:29:12 -08001006 struct work_struct *work = list_first_entry(&pwq->delayed_works,
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001007 struct work_struct, entry);
1008
Tejun Heo112202d2013-02-13 19:29:12 -08001009 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001010}
1011
Tejun Heobf4ede02012-08-03 10:30:46 -07001012/**
Tejun Heo112202d2013-02-13 19:29:12 -08001013 * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight
1014 * @pwq: pwq of interest
Tejun Heobf4ede02012-08-03 10:30:46 -07001015 * @color: color of work which left the queue
Tejun Heobf4ede02012-08-03 10:30:46 -07001016 *
1017 * A work either has completed or is removed from pending queue,
Tejun Heo112202d2013-02-13 19:29:12 -08001018 * decrement nr_in_flight of its pwq and handle workqueue flushing.
Tejun Heobf4ede02012-08-03 10:30:46 -07001019 *
1020 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001021 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -07001022 */
Tejun Heo112202d2013-02-13 19:29:12 -08001023static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color)
Tejun Heobf4ede02012-08-03 10:30:46 -07001024{
Tejun Heo8864b4e2013-03-12 11:30:04 -07001025 /* uncolored work items don't participate in flushing or nr_active */
Tejun Heobf4ede02012-08-03 10:30:46 -07001026 if (color == WORK_NO_COLOR)
Tejun Heo8864b4e2013-03-12 11:30:04 -07001027 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001028
Tejun Heo112202d2013-02-13 19:29:12 -08001029 pwq->nr_in_flight[color]--;
Tejun Heobf4ede02012-08-03 10:30:46 -07001030
Tejun Heo112202d2013-02-13 19:29:12 -08001031 pwq->nr_active--;
1032 if (!list_empty(&pwq->delayed_works)) {
Lai Jiangshanb3f9f402012-09-18 10:40:00 -07001033 /* one down, submit a delayed one */
Tejun Heo112202d2013-02-13 19:29:12 -08001034 if (pwq->nr_active < pwq->max_active)
1035 pwq_activate_first_delayed(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001036 }
1037
1038 /* is flush in progress and are we at the flushing tip? */
Tejun Heo112202d2013-02-13 19:29:12 -08001039 if (likely(pwq->flush_color != color))
Tejun Heo8864b4e2013-03-12 11:30:04 -07001040 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001041
1042 /* are there still in-flight works? */
Tejun Heo112202d2013-02-13 19:29:12 -08001043 if (pwq->nr_in_flight[color])
Tejun Heo8864b4e2013-03-12 11:30:04 -07001044 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001045
Tejun Heo112202d2013-02-13 19:29:12 -08001046 /* this pwq is done, clear flush_color */
1047 pwq->flush_color = -1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001048
1049 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001050 * If this was the last pwq, wake up the first flusher. It
Tejun Heobf4ede02012-08-03 10:30:46 -07001051 * will handle the rest.
1052 */
Tejun Heo112202d2013-02-13 19:29:12 -08001053 if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush))
1054 complete(&pwq->wq->first_flusher->done);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001055out_put:
1056 put_pwq(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001057}
1058
Tejun Heo36e227d2012-08-03 10:30:46 -07001059/**
Tejun Heobbb68df2012-08-03 10:30:46 -07001060 * try_to_grab_pending - steal work item from worklist and disable irq
Tejun Heo36e227d2012-08-03 10:30:46 -07001061 * @work: work item to steal
1062 * @is_dwork: @work is a delayed_work
Tejun Heobbb68df2012-08-03 10:30:46 -07001063 * @flags: place to store irq state
Tejun Heo36e227d2012-08-03 10:30:46 -07001064 *
1065 * Try to grab PENDING bit of @work. This function can handle @work in any
1066 * stable state - idle, on timer or on worklist. Return values are
1067 *
1068 * 1 if @work was pending and we successfully stole PENDING
1069 * 0 if @work was idle and we claimed PENDING
1070 * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry
Tejun Heobbb68df2012-08-03 10:30:46 -07001071 * -ENOENT if someone else is canceling @work, this state may persist
1072 * for arbitrarily long
Tejun Heo36e227d2012-08-03 10:30:46 -07001073 *
Tejun Heobbb68df2012-08-03 10:30:46 -07001074 * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001075 * interrupted while holding PENDING and @work off queue, irq must be
1076 * disabled on entry. This, combined with delayed_work->timer being
1077 * irqsafe, ensures that we return -EAGAIN for finite short period of time.
Tejun Heobbb68df2012-08-03 10:30:46 -07001078 *
1079 * On successful return, >= 0, irq is disabled and the caller is
1080 * responsible for releasing it using local_irq_restore(*@flags).
1081 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001082 * This function is safe to call from any context including IRQ handler.
Tejun Heobf4ede02012-08-03 10:30:46 -07001083 */
Tejun Heobbb68df2012-08-03 10:30:46 -07001084static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
1085 unsigned long *flags)
Tejun Heobf4ede02012-08-03 10:30:46 -07001086{
Tejun Heod565ed62013-01-24 11:01:33 -08001087 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08001088 struct pool_workqueue *pwq;
Tejun Heobf4ede02012-08-03 10:30:46 -07001089
Tejun Heobbb68df2012-08-03 10:30:46 -07001090 local_irq_save(*flags);
1091
Tejun Heo36e227d2012-08-03 10:30:46 -07001092 /* try to steal the timer if it exists */
1093 if (is_dwork) {
1094 struct delayed_work *dwork = to_delayed_work(work);
1095
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001096 /*
1097 * dwork->timer is irqsafe. If del_timer() fails, it's
1098 * guaranteed that the timer is not queued anywhere and not
1099 * running on the local CPU.
1100 */
Tejun Heo36e227d2012-08-03 10:30:46 -07001101 if (likely(del_timer(&dwork->timer)))
1102 return 1;
1103 }
1104
1105 /* try to claim PENDING the normal way */
Tejun Heobf4ede02012-08-03 10:30:46 -07001106 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1107 return 0;
1108
1109 /*
1110 * The queueing is in progress, or it is already queued. Try to
1111 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1112 */
Tejun Heod565ed62013-01-24 11:01:33 -08001113 pool = get_work_pool(work);
1114 if (!pool)
Tejun Heobbb68df2012-08-03 10:30:46 -07001115 goto fail;
Tejun Heobf4ede02012-08-03 10:30:46 -07001116
Tejun Heod565ed62013-01-24 11:01:33 -08001117 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001118 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001119 * work->data is guaranteed to point to pwq only while the work
1120 * item is queued on pwq->wq, and both updating work->data to point
1121 * to pwq on queueing and to pool on dequeueing are done under
1122 * pwq->pool->lock. This in turn guarantees that, if work->data
1123 * points to pwq which is associated with a locked pool, the work
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001124 * item is currently queued on that pool.
1125 */
Tejun Heo112202d2013-02-13 19:29:12 -08001126 pwq = get_work_pwq(work);
1127 if (pwq && pwq->pool == pool) {
Tejun Heo16062832013-02-06 18:04:53 -08001128 debug_work_deactivate(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001129
Tejun Heo16062832013-02-06 18:04:53 -08001130 /*
1131 * A delayed work item cannot be grabbed directly because
1132 * it might have linked NO_COLOR work items which, if left
Tejun Heo112202d2013-02-13 19:29:12 -08001133 * on the delayed_list, will confuse pwq->nr_active
Tejun Heo16062832013-02-06 18:04:53 -08001134 * management later on and cause stall. Make sure the work
1135 * item is activated before grabbing.
1136 */
1137 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
Tejun Heo112202d2013-02-13 19:29:12 -08001138 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001139
Tejun Heo16062832013-02-06 18:04:53 -08001140 list_del_init(&work->entry);
Tejun Heo112202d2013-02-13 19:29:12 -08001141 pwq_dec_nr_in_flight(get_work_pwq(work), get_work_color(work));
Tejun Heo36e227d2012-08-03 10:30:46 -07001142
Tejun Heo112202d2013-02-13 19:29:12 -08001143 /* work->data points to pwq iff queued, point to pool */
Tejun Heo16062832013-02-06 18:04:53 -08001144 set_work_pool_and_keep_pending(work, pool->id);
Lai Jiangshan4468a002013-02-06 18:04:53 -08001145
Tejun Heo16062832013-02-06 18:04:53 -08001146 spin_unlock(&pool->lock);
1147 return 1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001148 }
Tejun Heod565ed62013-01-24 11:01:33 -08001149 spin_unlock(&pool->lock);
Tejun Heobbb68df2012-08-03 10:30:46 -07001150fail:
1151 local_irq_restore(*flags);
1152 if (work_is_canceling(work))
1153 return -ENOENT;
1154 cpu_relax();
Tejun Heo36e227d2012-08-03 10:30:46 -07001155 return -EAGAIN;
Tejun Heobf4ede02012-08-03 10:30:46 -07001156}
1157
1158/**
Tejun Heo706026c2013-01-24 11:01:34 -08001159 * insert_work - insert a work into a pool
Tejun Heo112202d2013-02-13 19:29:12 -08001160 * @pwq: pwq @work belongs to
Tejun Heo4690c4a2010-06-29 10:07:10 +02001161 * @work: work to insert
1162 * @head: insertion point
1163 * @extra_flags: extra WORK_STRUCT_* flags to set
1164 *
Tejun Heo112202d2013-02-13 19:29:12 -08001165 * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to
Tejun Heo706026c2013-01-24 11:01:34 -08001166 * work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +02001167 *
1168 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001169 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02001170 */
Tejun Heo112202d2013-02-13 19:29:12 -08001171static void insert_work(struct pool_workqueue *pwq, struct work_struct *work,
1172 struct list_head *head, unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001173{
Tejun Heo112202d2013-02-13 19:29:12 -08001174 struct worker_pool *pool = pwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01001175
Tejun Heo4690c4a2010-06-29 10:07:10 +02001176 /* we own @work, set data and link */
Tejun Heo112202d2013-02-13 19:29:12 -08001177 set_work_pwq(work, pwq, extra_flags);
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -07001178 list_add_tail(&work->entry, head);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001179 get_pwq(pwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02001180
1181 /*
1182 * Ensure either worker_sched_deactivated() sees the above
1183 * list_add_tail() or we see zero nr_running to avoid workers
1184 * lying around lazily while there are works to be processed.
1185 */
1186 smp_mb();
1187
Tejun Heo63d95a92012-07-12 14:46:37 -07001188 if (__need_more_worker(pool))
1189 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001190}
1191
Tejun Heoc8efcc22010-12-20 19:32:04 +01001192/*
1193 * Test whether @work is being queued from another work executing on the
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001194 * same workqueue.
Tejun Heoc8efcc22010-12-20 19:32:04 +01001195 */
1196static bool is_chained_work(struct workqueue_struct *wq)
1197{
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001198 struct worker *worker;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001199
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001200 worker = current_wq_worker();
1201 /*
1202 * Return %true iff I'm a worker execuing a work item on @wq. If
1203 * I'm @worker, it's safe to dereference it without locking.
1204 */
Tejun Heo112202d2013-02-13 19:29:12 -08001205 return worker && worker->current_pwq->wq == wq;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001206}
1207
Tejun Heod84ff052013-03-12 11:29:59 -07001208static void __queue_work(int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 struct work_struct *work)
1210{
Tejun Heo112202d2013-02-13 19:29:12 -08001211 struct pool_workqueue *pwq;
Tejun Heoc9178082013-03-12 11:30:04 -07001212 struct worker_pool *last_pool;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001213 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001214 unsigned int work_flags;
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001215 unsigned int req_cpu = cpu;
Tejun Heo8930cab2012-08-03 10:30:45 -07001216
1217 /*
1218 * While a work item is PENDING && off queue, a task trying to
1219 * steal the PENDING will busy-loop waiting for it to either get
1220 * queued or lose PENDING. Grabbing PENDING and queueing should
1221 * happen with IRQ disabled.
1222 */
1223 WARN_ON_ONCE(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001225 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001226
Tejun Heoc8efcc22010-12-20 19:32:04 +01001227 /* if dying, only works from the same workqueue are allowed */
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02001228 if (unlikely(wq->flags & WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001229 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001230 return;
1231
Tejun Heoc9178082013-03-12 11:30:04 -07001232 /* pwq which will be used unless @work is executing elsewhere */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001233 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo57469822012-08-03 10:30:45 -07001234 if (cpu == WORK_CPU_UNBOUND)
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001235 cpu = raw_smp_processor_id();
Tejun Heo7fb98ea2013-03-12 11:30:00 -07001236 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heoc9178082013-03-12 11:30:04 -07001237 } else {
1238 pwq = first_pwq(wq);
1239 }
Tejun Heodbf25762012-08-20 14:51:23 -07001240
Tejun Heoc9178082013-03-12 11:30:04 -07001241 /*
1242 * If @work was previously on a different pool, it might still be
1243 * running there, in which case the work needs to be queued on that
1244 * pool to guarantee non-reentrancy.
1245 */
1246 last_pool = get_work_pool(work);
1247 if (last_pool && last_pool != pwq->pool) {
1248 struct worker *worker;
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001249
Tejun Heoc9178082013-03-12 11:30:04 -07001250 spin_lock(&last_pool->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001251
Tejun Heoc9178082013-03-12 11:30:04 -07001252 worker = find_worker_executing_work(last_pool, work);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001253
Tejun Heoc9178082013-03-12 11:30:04 -07001254 if (worker && worker->current_pwq->wq == wq) {
1255 pwq = worker->current_pwq;
Tejun Heo8930cab2012-08-03 10:30:45 -07001256 } else {
Tejun Heoc9178082013-03-12 11:30:04 -07001257 /* meh... not running there, queue here */
1258 spin_unlock(&last_pool->lock);
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 Heo112202d2013-02-13 19:29:12 -08001262 spin_lock(&pwq->pool->lock);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001263 }
1264
Tejun Heo112202d2013-02-13 19:29:12 -08001265 /* pwq determined, queue */
1266 trace_workqueue_queue_work(req_cpu, pwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001267
Dan Carpenterf5b25522012-04-13 22:06:58 +03001268 if (WARN_ON(!list_empty(&work->entry))) {
Tejun Heo112202d2013-02-13 19:29:12 -08001269 spin_unlock(&pwq->pool->lock);
Dan Carpenterf5b25522012-04-13 22:06:58 +03001270 return;
1271 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001272
Tejun Heo112202d2013-02-13 19:29:12 -08001273 pwq->nr_in_flight[pwq->work_color]++;
1274 work_flags = work_color_to_flags(pwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001275
Tejun Heo112202d2013-02-13 19:29:12 -08001276 if (likely(pwq->nr_active < pwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001277 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001278 pwq->nr_active++;
1279 worklist = &pwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001280 } else {
1281 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo112202d2013-02-13 19:29:12 -08001282 worklist = &pwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001283 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001284
Tejun Heo112202d2013-02-13 19:29:12 -08001285 insert_work(pwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001286
Tejun Heo112202d2013-02-13 19:29:12 -08001287 spin_unlock(&pwq->pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288}
1289
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001290/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07001291 * queue_work_on - queue work on specific cpu
1292 * @cpu: CPU number to execute work on
1293 * @wq: workqueue to use
1294 * @work: work to queue
1295 *
Tejun Heod4283e92012-08-03 10:30:44 -07001296 * Returns %false if @work was already on a queue, %true otherwise.
Zhang Ruic1a220e2008-07-23 21:28:39 -07001297 *
1298 * We queue the work to a specific CPU, the caller must ensure it
1299 * can't go away.
1300 */
Tejun Heod4283e92012-08-03 10:30:44 -07001301bool queue_work_on(int cpu, struct workqueue_struct *wq,
1302 struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07001303{
Tejun Heod4283e92012-08-03 10:30:44 -07001304 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001305 unsigned long flags;
1306
1307 local_irq_save(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001308
Tejun Heo22df02b2010-06-29 10:07:10 +02001309 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001310 __queue_work(cpu, wq, work);
Tejun Heod4283e92012-08-03 10:30:44 -07001311 ret = true;
Zhang Ruic1a220e2008-07-23 21:28:39 -07001312 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001313
1314 local_irq_restore(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001315 return ret;
1316}
1317EXPORT_SYMBOL_GPL(queue_work_on);
1318
Tejun Heo0a13c002012-08-03 10:30:44 -07001319/**
1320 * queue_work - queue work on a workqueue
1321 * @wq: workqueue to use
1322 * @work: work to queue
1323 *
Tejun Heod4283e92012-08-03 10:30:44 -07001324 * Returns %false if @work was already on a queue, %true otherwise.
Tejun Heo0a13c002012-08-03 10:30:44 -07001325 *
1326 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1327 * it can be processed by another CPU.
1328 */
Tejun Heod4283e92012-08-03 10:30:44 -07001329bool queue_work(struct workqueue_struct *wq, struct work_struct *work)
Tejun Heo0a13c002012-08-03 10:30:44 -07001330{
Tejun Heo57469822012-08-03 10:30:45 -07001331 return queue_work_on(WORK_CPU_UNBOUND, wq, work);
Tejun Heo0a13c002012-08-03 10:30:44 -07001332}
1333EXPORT_SYMBOL_GPL(queue_work);
1334
Tejun Heod8e794d2012-08-03 10:30:45 -07001335void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
David Howells52bad642006-11-22 14:54:01 +00001337 struct delayed_work *dwork = (struct delayed_work *)__data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001339 /* should have been called from irqsafe timer with irq already off */
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001340 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341}
Konstantin Khlebnikov1438ade52013-01-24 16:36:31 +04001342EXPORT_SYMBOL(delayed_work_timer_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001344static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
1345 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001347 struct timer_list *timer = &dwork->timer;
1348 struct work_struct *work = &dwork->work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001350 WARN_ON_ONCE(timer->function != delayed_work_timer_fn ||
1351 timer->data != (unsigned long)dwork);
Tejun Heofc4b5142012-12-04 07:40:39 -08001352 WARN_ON_ONCE(timer_pending(timer));
1353 WARN_ON_ONCE(!list_empty(&work->entry));
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001354
Tejun Heo8852aac2012-12-01 16:23:42 -08001355 /*
1356 * If @delay is 0, queue @dwork->work immediately. This is for
1357 * both optimization and correctness. The earliest @timer can
1358 * expire is on the closest next tick and delayed_work users depend
1359 * on that there's no such delay when @delay is 0.
1360 */
1361 if (!delay) {
1362 __queue_work(cpu, wq, &dwork->work);
1363 return;
1364 }
1365
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001366 timer_stats_timer_set_start_info(&dwork->timer);
1367
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001368 dwork->wq = wq;
Tejun Heo12650572012-08-08 09:38:42 -07001369 dwork->cpu = cpu;
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001370 timer->expires = jiffies + delay;
1371
1372 if (unlikely(cpu != WORK_CPU_UNBOUND))
1373 add_timer_on(timer, cpu);
1374 else
1375 add_timer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376}
1377
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001378/**
1379 * queue_delayed_work_on - queue work on specific CPU after delay
1380 * @cpu: CPU number to execute work on
1381 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001382 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001383 * @delay: number of jiffies to wait before queueing
1384 *
Tejun Heo715f1302012-08-03 10:30:46 -07001385 * Returns %false if @work was already on a queue, %true otherwise. If
1386 * @delay is zero and @dwork is idle, it will be scheduled for immediate
1387 * execution.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001388 */
Tejun Heod4283e92012-08-03 10:30:44 -07001389bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
1390 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001391{
David Howells52bad642006-11-22 14:54:01 +00001392 struct work_struct *work = &dwork->work;
Tejun Heod4283e92012-08-03 10:30:44 -07001393 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001394 unsigned long flags;
1395
1396 /* read the comment in __queue_work() */
1397 local_irq_save(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001398
Tejun Heo22df02b2010-06-29 10:07:10 +02001399 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001400 __queue_delayed_work(cpu, wq, dwork, delay);
Tejun Heod4283e92012-08-03 10:30:44 -07001401 ret = true;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001402 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001403
1404 local_irq_restore(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001405 return ret;
1406}
Dave Jonesae90dd52006-06-30 01:40:45 -04001407EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Tejun Heoc8e55f32010-06-29 10:07:12 +02001409/**
Tejun Heo0a13c002012-08-03 10:30:44 -07001410 * queue_delayed_work - queue work on a workqueue after delay
1411 * @wq: workqueue to use
1412 * @dwork: delayable work to queue
1413 * @delay: number of jiffies to wait before queueing
1414 *
Tejun Heo715f1302012-08-03 10:30:46 -07001415 * Equivalent to queue_delayed_work_on() but tries to use the local CPU.
Tejun Heo0a13c002012-08-03 10:30:44 -07001416 */
Tejun Heod4283e92012-08-03 10:30:44 -07001417bool queue_delayed_work(struct workqueue_struct *wq,
Tejun Heo0a13c002012-08-03 10:30:44 -07001418 struct delayed_work *dwork, unsigned long delay)
1419{
Tejun Heo57469822012-08-03 10:30:45 -07001420 return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
Tejun Heo0a13c002012-08-03 10:30:44 -07001421}
1422EXPORT_SYMBOL_GPL(queue_delayed_work);
1423
1424/**
Tejun Heo8376fe22012-08-03 10:30:47 -07001425 * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
1426 * @cpu: CPU number to execute work on
1427 * @wq: workqueue to use
1428 * @dwork: work to queue
1429 * @delay: number of jiffies to wait before queueing
1430 *
1431 * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
1432 * modify @dwork's timer so that it expires after @delay. If @delay is
1433 * zero, @work is guaranteed to be scheduled immediately regardless of its
1434 * current state.
1435 *
1436 * Returns %false if @dwork was idle and queued, %true if @dwork was
1437 * pending and its timer was modified.
1438 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001439 * This function is safe to call from any context including IRQ handler.
Tejun Heo8376fe22012-08-03 10:30:47 -07001440 * See try_to_grab_pending() for details.
1441 */
1442bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
1443 struct delayed_work *dwork, unsigned long delay)
1444{
1445 unsigned long flags;
1446 int ret;
1447
1448 do {
1449 ret = try_to_grab_pending(&dwork->work, true, &flags);
1450 } while (unlikely(ret == -EAGAIN));
1451
1452 if (likely(ret >= 0)) {
1453 __queue_delayed_work(cpu, wq, dwork, delay);
1454 local_irq_restore(flags);
1455 }
1456
1457 /* -ENOENT from try_to_grab_pending() becomes %true */
1458 return ret;
1459}
1460EXPORT_SYMBOL_GPL(mod_delayed_work_on);
1461
1462/**
1463 * mod_delayed_work - modify delay of or queue a delayed work
1464 * @wq: workqueue to use
1465 * @dwork: work to queue
1466 * @delay: number of jiffies to wait before queueing
1467 *
1468 * mod_delayed_work_on() on local CPU.
1469 */
1470bool mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dwork,
1471 unsigned long delay)
1472{
1473 return mod_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
1474}
1475EXPORT_SYMBOL_GPL(mod_delayed_work);
1476
1477/**
Tejun Heoc8e55f32010-06-29 10:07:12 +02001478 * worker_enter_idle - enter idle state
1479 * @worker: worker which is entering idle state
1480 *
1481 * @worker is entering idle state. Update stats and idle timer if
1482 * necessary.
1483 *
1484 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001485 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001486 */
1487static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001489 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Tejun Heo6183c002013-03-12 11:29:57 -07001491 if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) ||
1492 WARN_ON_ONCE(!list_empty(&worker->entry) &&
1493 (worker->hentry.next || worker->hentry.pprev)))
1494 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Tejun Heocb444762010-07-02 10:03:50 +02001496 /* can't use worker_set_flags(), also called from start_worker() */
1497 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001498 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001499 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001500
Tejun Heoc8e55f32010-06-29 10:07:12 +02001501 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001502 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001503
Tejun Heo628c78e2012-07-17 12:39:27 -07001504 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1505 mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
Tejun Heocb444762010-07-02 10:03:50 +02001506
Tejun Heo544ecf32012-05-14 15:04:50 -07001507 /*
Tejun Heo706026c2013-01-24 11:01:34 -08001508 * Sanity check nr_running. Because wq_unbind_fn() releases
Tejun Heod565ed62013-01-24 11:01:33 -08001509 * pool->lock between setting %WORKER_UNBOUND and zapping
Tejun Heo628c78e2012-07-17 12:39:27 -07001510 * nr_running, the warning may trigger spuriously. Check iff
1511 * unbind is not in progress.
Tejun Heo544ecf32012-05-14 15:04:50 -07001512 */
Tejun Heo24647572013-01-24 11:01:33 -08001513 WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001514 pool->nr_workers == pool->nr_idle &&
Tejun Heoe19e3972013-01-24 11:39:44 -08001515 atomic_read(&pool->nr_running));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516}
1517
Tejun Heoc8e55f32010-06-29 10:07:12 +02001518/**
1519 * worker_leave_idle - leave idle state
1520 * @worker: worker which is leaving idle state
1521 *
1522 * @worker is leaving idle state. Update stats.
1523 *
1524 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001525 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001526 */
1527static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001529 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Tejun Heo6183c002013-03-12 11:29:57 -07001531 if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE)))
1532 return;
Tejun Heod302f012010-06-29 10:07:13 +02001533 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001534 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001535 list_del_init(&worker->entry);
1536}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Tejun Heoe22bee72010-06-29 10:07:14 +02001538/**
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001539 * worker_maybe_bind_and_lock - try to bind %current to worker_pool and lock it
1540 * @pool: target worker_pool
1541 *
1542 * Bind %current to the cpu of @pool if it is associated and lock @pool.
Tejun Heoe22bee72010-06-29 10:07:14 +02001543 *
1544 * Works which are scheduled while the cpu is online must at least be
1545 * scheduled to a worker which is bound to the cpu so that if they are
1546 * flushed from cpu callbacks while cpu is going down, they are
1547 * guaranteed to execute on the cpu.
1548 *
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001549 * This function is to be used by unbound workers and rescuers to bind
Tejun Heoe22bee72010-06-29 10:07:14 +02001550 * themselves to the target cpu and may race with cpu going down or
1551 * coming online. kthread_bind() can't be used because it may put the
1552 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
Tejun Heo706026c2013-01-24 11:01:34 -08001553 * verbatim as it's best effort and blocking and pool may be
Tejun Heoe22bee72010-06-29 10:07:14 +02001554 * [dis]associated in the meantime.
1555 *
Tejun Heo706026c2013-01-24 11:01:34 -08001556 * This function tries set_cpus_allowed() and locks pool and verifies the
Tejun Heo24647572013-01-24 11:01:33 -08001557 * binding against %POOL_DISASSOCIATED which is set during
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001558 * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1559 * enters idle state or fetches works without dropping lock, it can
1560 * guarantee the scheduling requirement described in the first paragraph.
Tejun Heoe22bee72010-06-29 10:07:14 +02001561 *
1562 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001563 * Might sleep. Called without any lock but returns with pool->lock
Tejun Heoe22bee72010-06-29 10:07:14 +02001564 * held.
1565 *
1566 * RETURNS:
Tejun Heo706026c2013-01-24 11:01:34 -08001567 * %true if the associated pool is online (@worker is successfully
Tejun Heoe22bee72010-06-29 10:07:14 +02001568 * bound), %false if offline.
1569 */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001570static bool worker_maybe_bind_and_lock(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001571__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001572{
Tejun Heoe22bee72010-06-29 10:07:14 +02001573 while (true) {
1574 /*
1575 * The following call may fail, succeed or succeed
1576 * without actually migrating the task to the cpu if
1577 * it races with cpu hotunplug operation. Verify
Tejun Heo24647572013-01-24 11:01:33 -08001578 * against POOL_DISASSOCIATED.
Tejun Heoe22bee72010-06-29 10:07:14 +02001579 */
Tejun Heo24647572013-01-24 11:01:33 -08001580 if (!(pool->flags & POOL_DISASSOCIATED))
Tejun Heo7a4e3442013-03-12 11:30:00 -07001581 set_cpus_allowed_ptr(current, pool->attrs->cpumask);
Oleg Nesterov85f41862007-05-09 02:34:20 -07001582
Tejun Heod565ed62013-01-24 11:01:33 -08001583 spin_lock_irq(&pool->lock);
Tejun Heo24647572013-01-24 11:01:33 -08001584 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heoe22bee72010-06-29 10:07:14 +02001585 return false;
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001586 if (task_cpu(current) == pool->cpu &&
Tejun Heo7a4e3442013-03-12 11:30:00 -07001587 cpumask_equal(&current->cpus_allowed, pool->attrs->cpumask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001588 return true;
Tejun Heod565ed62013-01-24 11:01:33 -08001589 spin_unlock_irq(&pool->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001590
Tejun Heo5035b202011-04-29 18:08:37 +02001591 /*
1592 * We've raced with CPU hot[un]plug. Give it a breather
1593 * and retry migration. cond_resched() is required here;
1594 * otherwise, we might deadlock against cpu_stop trying to
1595 * bring down the CPU on non-preemptive kernel.
1596 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001597 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001598 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001599 }
1600}
1601
1602/*
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001603 * Rebind an idle @worker to its CPU. worker_thread() will test
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001604 * list_empty(@worker->entry) before leaving idle and call this function.
Tejun Heo25511a42012-07-17 12:39:27 -07001605 */
1606static void idle_worker_rebind(struct worker *worker)
1607{
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001608 /* CPU may go down again inbetween, clear UNBOUND only on success */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001609 if (worker_maybe_bind_and_lock(worker->pool))
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001610 worker_clr_flags(worker, WORKER_UNBOUND);
Tejun Heo25511a42012-07-17 12:39:27 -07001611
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001612 /* rebind complete, become available again */
1613 list_add(&worker->entry, &worker->pool->idle_list);
Tejun Heod565ed62013-01-24 11:01:33 -08001614 spin_unlock_irq(&worker->pool->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07001615}
1616
1617/*
1618 * Function for @worker->rebind.work used to rebind unbound busy workers to
Tejun Heo403c8212012-07-17 12:39:27 -07001619 * the associated cpu which is coming back online. This is scheduled by
1620 * cpu up but can race with other cpu hotplug operations and may be
1621 * executed twice without intervening cpu down.
Tejun Heoe22bee72010-06-29 10:07:14 +02001622 */
Tejun Heo25511a42012-07-17 12:39:27 -07001623static void busy_worker_rebind_fn(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001624{
1625 struct worker *worker = container_of(work, struct worker, rebind_work);
Tejun Heoe22bee72010-06-29 10:07:14 +02001626
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001627 if (worker_maybe_bind_and_lock(worker->pool))
Lai Jiangshaneab6d822012-09-18 09:59:22 -07001628 worker_clr_flags(worker, WORKER_UNBOUND);
Tejun Heoe22bee72010-06-29 10:07:14 +02001629
Tejun Heod565ed62013-01-24 11:01:33 -08001630 spin_unlock_irq(&worker->pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001631}
1632
Tejun Heo25511a42012-07-17 12:39:27 -07001633/**
Tejun Heo94cf58b2013-01-24 11:01:33 -08001634 * rebind_workers - rebind all workers of a pool to the associated CPU
1635 * @pool: pool of interest
Tejun Heo25511a42012-07-17 12:39:27 -07001636 *
Tejun Heo94cf58b2013-01-24 11:01:33 -08001637 * @pool->cpu is coming online. Rebind all workers to the CPU. Rebinding
Tejun Heo25511a42012-07-17 12:39:27 -07001638 * is different for idle and busy ones.
1639 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001640 * Idle ones will be removed from the idle_list and woken up. They will
1641 * add themselves back after completing rebind. This ensures that the
1642 * idle_list doesn't contain any unbound workers when re-bound busy workers
1643 * try to perform local wake-ups for concurrency management.
Tejun Heo25511a42012-07-17 12:39:27 -07001644 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001645 * Busy workers can rebind after they finish their current work items.
1646 * Queueing the rebind work item at the head of the scheduled list is
1647 * enough. Note that nr_running will be properly bumped as busy workers
1648 * rebind.
Tejun Heo25511a42012-07-17 12:39:27 -07001649 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001650 * On return, all non-manager workers are scheduled for rebind - see
1651 * manage_workers() for the manager special case. Any idle worker
1652 * including the manager will not appear on @idle_list until rebind is
1653 * complete, making local wake-ups safe.
Tejun Heo25511a42012-07-17 12:39:27 -07001654 */
Tejun Heo94cf58b2013-01-24 11:01:33 -08001655static void rebind_workers(struct worker_pool *pool)
Tejun Heo25511a42012-07-17 12:39:27 -07001656{
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001657 struct worker *worker, *n;
Tejun Heo25511a42012-07-17 12:39:27 -07001658 int i;
1659
Tejun Heo94cf58b2013-01-24 11:01:33 -08001660 lockdep_assert_held(&pool->assoc_mutex);
1661 lockdep_assert_held(&pool->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07001662
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001663 /* dequeue and kick idle ones */
Tejun Heo94cf58b2013-01-24 11:01:33 -08001664 list_for_each_entry_safe(worker, n, &pool->idle_list, entry) {
1665 /*
1666 * idle workers should be off @pool->idle_list until rebind
1667 * is complete to avoid receiving premature local wake-ups.
1668 */
1669 list_del_init(&worker->entry);
Lai Jiangshan96e65302012-09-02 00:28:19 +08001670
Tejun Heo94cf58b2013-01-24 11:01:33 -08001671 /*
1672 * worker_thread() will see the above dequeuing and call
1673 * idle_worker_rebind().
1674 */
1675 wake_up_process(worker->task);
1676 }
Tejun Heo25511a42012-07-17 12:39:27 -07001677
Tejun Heo94cf58b2013-01-24 11:01:33 -08001678 /* rebind busy workers */
Sasha Levinb67bfe02013-02-27 17:06:00 -08001679 for_each_busy_worker(worker, i, pool) {
Tejun Heo94cf58b2013-01-24 11:01:33 -08001680 struct work_struct *rebind_work = &worker->rebind_work;
1681 struct workqueue_struct *wq;
Tejun Heo25511a42012-07-17 12:39:27 -07001682
Tejun Heo94cf58b2013-01-24 11:01:33 -08001683 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
1684 work_data_bits(rebind_work)))
1685 continue;
Tejun Heo25511a42012-07-17 12:39:27 -07001686
Tejun Heo94cf58b2013-01-24 11:01:33 -08001687 debug_work_activate(rebind_work);
Tejun Heo90beca52012-09-04 23:12:33 -07001688
Tejun Heo94cf58b2013-01-24 11:01:33 -08001689 /*
1690 * wq doesn't really matter but let's keep @worker->pool
Tejun Heo112202d2013-02-13 19:29:12 -08001691 * and @pwq->pool consistent for sanity.
Tejun Heo94cf58b2013-01-24 11:01:33 -08001692 */
Tejun Heo7a4e3442013-03-12 11:30:00 -07001693 if (worker->pool->attrs->nice < 0)
Tejun Heo94cf58b2013-01-24 11:01:33 -08001694 wq = system_highpri_wq;
1695 else
1696 wq = system_wq;
Tejun Heoec588152012-09-04 23:16:32 -07001697
Tejun Heo7fb98ea2013-03-12 11:30:00 -07001698 insert_work(per_cpu_ptr(wq->cpu_pwqs, pool->cpu), rebind_work,
Tejun Heo94cf58b2013-01-24 11:01:33 -08001699 worker->scheduled.next,
1700 work_color_to_flags(WORK_NO_COLOR));
Tejun Heoec588152012-09-04 23:16:32 -07001701 }
Tejun Heo25511a42012-07-17 12:39:27 -07001702}
1703
Tejun Heoc34056a2010-06-29 10:07:11 +02001704static struct worker *alloc_worker(void)
1705{
1706 struct worker *worker;
1707
1708 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001709 if (worker) {
1710 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001711 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heo25511a42012-07-17 12:39:27 -07001712 INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn);
Tejun Heoe22bee72010-06-29 10:07:14 +02001713 /* on creation a worker is in !idle && prep state */
1714 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001715 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001716 return worker;
1717}
1718
1719/**
1720 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001721 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001722 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001723 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001724 * can be started by calling start_worker() or destroyed using
1725 * destroy_worker().
1726 *
1727 * CONTEXT:
1728 * Might sleep. Does GFP_KERNEL allocations.
1729 *
1730 * RETURNS:
1731 * Pointer to the newly created worker.
1732 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001733static struct worker *create_worker(struct worker_pool *pool)
Tejun Heoc34056a2010-06-29 10:07:11 +02001734{
Tejun Heo7a4e3442013-03-12 11:30:00 -07001735 const char *pri = pool->attrs->nice < 0 ? "H" : "";
Tejun Heoc34056a2010-06-29 10:07:11 +02001736 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001737 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001738
Tejun Heod565ed62013-01-24 11:01:33 -08001739 spin_lock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001740 while (ida_get_new(&pool->worker_ida, &id)) {
Tejun Heod565ed62013-01-24 11:01:33 -08001741 spin_unlock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001742 if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001743 goto fail;
Tejun Heod565ed62013-01-24 11:01:33 -08001744 spin_lock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001745 }
Tejun Heod565ed62013-01-24 11:01:33 -08001746 spin_unlock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001747
1748 worker = alloc_worker();
1749 if (!worker)
1750 goto fail;
1751
Tejun Heobd7bdd42012-07-12 14:46:37 -07001752 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001753 worker->id = id;
1754
Tejun Heo29c91e92013-03-12 11:30:03 -07001755 if (pool->cpu >= 0)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001756 worker->task = kthread_create_on_node(worker_thread,
Tejun Heoec22ca52013-01-24 11:01:33 -08001757 worker, cpu_to_node(pool->cpu),
Tejun Heod84ff052013-03-12 11:29:59 -07001758 "kworker/%d:%d%s", pool->cpu, id, pri);
Tejun Heof3421792010-07-02 10:03:51 +02001759 else
1760 worker->task = kthread_create(worker_thread, worker,
Tejun Heoac6104c2013-03-12 11:30:03 -07001761 "kworker/u%d:%d%s",
1762 pool->id, id, pri);
Tejun Heoc34056a2010-06-29 10:07:11 +02001763 if (IS_ERR(worker->task))
1764 goto fail;
1765
Tejun Heo7a4e3442013-03-12 11:30:00 -07001766 set_user_nice(worker->task, pool->attrs->nice);
1767 set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask);
Tejun Heo32704762012-07-13 22:16:45 -07001768
Tejun Heodb7bccf2010-06-29 10:07:12 +02001769 /*
Tejun Heo7a4e3442013-03-12 11:30:00 -07001770 * %PF_THREAD_BOUND is used to prevent userland from meddling with
1771 * cpumask of workqueue workers. This is an abuse. We need
1772 * %PF_NO_SETAFFINITY.
Tejun Heodb7bccf2010-06-29 10:07:12 +02001773 */
Tejun Heo7a4e3442013-03-12 11:30:00 -07001774 worker->task->flags |= PF_THREAD_BOUND;
1775
1776 /*
1777 * The caller is responsible for ensuring %POOL_DISASSOCIATED
1778 * remains stable across this function. See the comments above the
1779 * flag definition for details.
1780 */
1781 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001782 worker->flags |= WORKER_UNBOUND;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001783
Tejun Heoc34056a2010-06-29 10:07:11 +02001784 return worker;
1785fail:
1786 if (id >= 0) {
Tejun Heod565ed62013-01-24 11:01:33 -08001787 spin_lock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001788 ida_remove(&pool->worker_ida, id);
Tejun Heod565ed62013-01-24 11:01:33 -08001789 spin_unlock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001790 }
1791 kfree(worker);
1792 return NULL;
1793}
1794
1795/**
1796 * start_worker - start a newly created worker
1797 * @worker: worker to start
1798 *
Tejun Heo706026c2013-01-24 11:01:34 -08001799 * Make the pool aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001800 *
1801 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001802 * spin_lock_irq(pool->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001803 */
1804static void start_worker(struct worker *worker)
1805{
Tejun Heocb444762010-07-02 10:03:50 +02001806 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001807 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001808 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001809 wake_up_process(worker->task);
1810}
1811
1812/**
1813 * destroy_worker - destroy a workqueue worker
1814 * @worker: worker to be destroyed
1815 *
Tejun Heo706026c2013-01-24 11:01:34 -08001816 * Destroy @worker and adjust @pool stats accordingly.
Tejun Heoc8e55f32010-06-29 10:07:12 +02001817 *
1818 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001819 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001820 */
1821static void destroy_worker(struct worker *worker)
1822{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001823 struct worker_pool *pool = worker->pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001824 int id = worker->id;
1825
1826 /* sanity check frenzy */
Tejun Heo6183c002013-03-12 11:29:57 -07001827 if (WARN_ON(worker->current_work) ||
1828 WARN_ON(!list_empty(&worker->scheduled)))
1829 return;
Tejun Heoc34056a2010-06-29 10:07:11 +02001830
Tejun Heoc8e55f32010-06-29 10:07:12 +02001831 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001832 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001833 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001834 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001835
1836 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001837 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001838
Tejun Heod565ed62013-01-24 11:01:33 -08001839 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001840
Tejun Heoc34056a2010-06-29 10:07:11 +02001841 kthread_stop(worker->task);
1842 kfree(worker);
1843
Tejun Heod565ed62013-01-24 11:01:33 -08001844 spin_lock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001845 ida_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001846}
1847
Tejun Heo63d95a92012-07-12 14:46:37 -07001848static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001849{
Tejun Heo63d95a92012-07-12 14:46:37 -07001850 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001851
Tejun Heod565ed62013-01-24 11:01:33 -08001852 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001853
Tejun Heo63d95a92012-07-12 14:46:37 -07001854 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001855 struct worker *worker;
1856 unsigned long expires;
1857
1858 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001859 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001860 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1861
1862 if (time_before(jiffies, expires))
Tejun Heo63d95a92012-07-12 14:46:37 -07001863 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001864 else {
1865 /* it's been idle for too long, wake up manager */
Tejun Heo11ebea52012-07-12 14:46:37 -07001866 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -07001867 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001868 }
1869 }
1870
Tejun Heod565ed62013-01-24 11:01:33 -08001871 spin_unlock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001872}
1873
Tejun Heo493a1722013-03-12 11:29:59 -07001874static void send_mayday(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001875{
Tejun Heo112202d2013-02-13 19:29:12 -08001876 struct pool_workqueue *pwq = get_work_pwq(work);
1877 struct workqueue_struct *wq = pwq->wq;
Tejun Heo493a1722013-03-12 11:29:59 -07001878
1879 lockdep_assert_held(&workqueue_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001880
Tejun Heo493008a2013-03-12 11:30:03 -07001881 if (!wq->rescuer)
Tejun Heo493a1722013-03-12 11:29:59 -07001882 return;
Tejun Heoe22bee72010-06-29 10:07:14 +02001883
1884 /* mayday mayday mayday */
Tejun Heo493a1722013-03-12 11:29:59 -07001885 if (list_empty(&pwq->mayday_node)) {
1886 list_add_tail(&pwq->mayday_node, &wq->maydays);
Tejun Heoe22bee72010-06-29 10:07:14 +02001887 wake_up_process(wq->rescuer->task);
Tejun Heo493a1722013-03-12 11:29:59 -07001888 }
Tejun Heoe22bee72010-06-29 10:07:14 +02001889}
1890
Tejun Heo706026c2013-01-24 11:01:34 -08001891static void pool_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001892{
Tejun Heo63d95a92012-07-12 14:46:37 -07001893 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001894 struct work_struct *work;
1895
Tejun Heo493a1722013-03-12 11:29:59 -07001896 spin_lock_irq(&workqueue_lock); /* for wq->maydays */
1897 spin_lock(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001898
Tejun Heo63d95a92012-07-12 14:46:37 -07001899 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001900 /*
1901 * We've been trying to create a new worker but
1902 * haven't been successful. We might be hitting an
1903 * allocation deadlock. Send distress signals to
1904 * rescuers.
1905 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001906 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001907 send_mayday(work);
1908 }
1909
Tejun Heo493a1722013-03-12 11:29:59 -07001910 spin_unlock(&pool->lock);
1911 spin_unlock_irq(&workqueue_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001912
Tejun Heo63d95a92012-07-12 14:46:37 -07001913 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001914}
1915
1916/**
1917 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001918 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001919 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001920 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001921 * have at least one idle worker on return from this function. If
1922 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001923 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001924 * possible allocation deadlock.
1925 *
1926 * On return, need_to_create_worker() is guaranteed to be false and
1927 * may_start_working() true.
1928 *
1929 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001930 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001931 * multiple times. Does GFP_KERNEL allocations. Called only from
1932 * manager.
1933 *
1934 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08001935 * false if no action was taken and pool->lock stayed locked, true
Tejun Heoe22bee72010-06-29 10:07:14 +02001936 * otherwise.
1937 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001938static bool maybe_create_worker(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001939__releases(&pool->lock)
1940__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001941{
Tejun Heo63d95a92012-07-12 14:46:37 -07001942 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001943 return false;
1944restart:
Tejun Heod565ed62013-01-24 11:01:33 -08001945 spin_unlock_irq(&pool->lock);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001946
Tejun Heoe22bee72010-06-29 10:07:14 +02001947 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07001948 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001949
1950 while (true) {
1951 struct worker *worker;
1952
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001953 worker = create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001954 if (worker) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001955 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001956 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001957 start_worker(worker);
Tejun Heo6183c002013-03-12 11:29:57 -07001958 if (WARN_ON_ONCE(need_to_create_worker(pool)))
1959 goto restart;
Tejun Heoe22bee72010-06-29 10:07:14 +02001960 return true;
1961 }
1962
Tejun Heo63d95a92012-07-12 14:46:37 -07001963 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001964 break;
1965
Tejun Heoe22bee72010-06-29 10:07:14 +02001966 __set_current_state(TASK_INTERRUPTIBLE);
1967 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001968
Tejun Heo63d95a92012-07-12 14:46:37 -07001969 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001970 break;
1971 }
1972
Tejun Heo63d95a92012-07-12 14:46:37 -07001973 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001974 spin_lock_irq(&pool->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07001975 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001976 goto restart;
1977 return true;
1978}
1979
1980/**
1981 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo63d95a92012-07-12 14:46:37 -07001982 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001983 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001984 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02001985 * IDLE_WORKER_TIMEOUT.
1986 *
1987 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001988 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001989 * multiple times. Called only from manager.
1990 *
1991 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08001992 * false if no action was taken and pool->lock stayed locked, true
Tejun Heoe22bee72010-06-29 10:07:14 +02001993 * otherwise.
1994 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001995static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001996{
1997 bool ret = false;
1998
Tejun Heo63d95a92012-07-12 14:46:37 -07001999 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02002000 struct worker *worker;
2001 unsigned long expires;
2002
Tejun Heo63d95a92012-07-12 14:46:37 -07002003 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02002004 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
2005
2006 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07002007 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02002008 break;
2009 }
2010
2011 destroy_worker(worker);
2012 ret = true;
2013 }
2014
2015 return ret;
2016}
2017
2018/**
2019 * manage_workers - manage worker pool
2020 * @worker: self
2021 *
Tejun Heo706026c2013-01-24 11:01:34 -08002022 * Assume the manager role and manage the worker pool @worker belongs
Tejun Heoe22bee72010-06-29 10:07:14 +02002023 * to. At any given time, there can be only zero or one manager per
Tejun Heo706026c2013-01-24 11:01:34 -08002024 * pool. The exclusion is handled automatically by this function.
Tejun Heoe22bee72010-06-29 10:07:14 +02002025 *
2026 * The caller can safely start processing works on false return. On
2027 * true return, it's guaranteed that need_to_create_worker() is false
2028 * and may_start_working() is true.
2029 *
2030 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002031 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02002032 * multiple times. Does GFP_KERNEL allocations.
2033 *
2034 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08002035 * spin_lock_irq(pool->lock) which may be released and regrabbed
2036 * multiple times. Does GFP_KERNEL allocations.
Tejun Heoe22bee72010-06-29 10:07:14 +02002037 */
2038static bool manage_workers(struct worker *worker)
2039{
Tejun Heo63d95a92012-07-12 14:46:37 -07002040 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002041 bool ret = false;
2042
Tejun Heo34a06bd2013-03-12 11:30:00 -07002043 if (!mutex_trylock(&pool->manager_arb))
Tejun Heoe22bee72010-06-29 10:07:14 +02002044 return ret;
2045
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002046 /*
2047 * To simplify both worker management and CPU hotplug, hold off
2048 * management while hotplug is in progress. CPU hotplug path can't
Tejun Heo34a06bd2013-03-12 11:30:00 -07002049 * grab @pool->manager_arb to achieve this because that can lead to
2050 * idle worker depletion (all become busy thinking someone else is
2051 * managing) which in turn can result in deadlock under extreme
2052 * circumstances. Use @pool->assoc_mutex to synchronize manager
2053 * against CPU hotplug.
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002054 *
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002055 * assoc_mutex would always be free unless CPU hotplug is in
Tejun Heod565ed62013-01-24 11:01:33 -08002056 * progress. trylock first without dropping @pool->lock.
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002057 */
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002058 if (unlikely(!mutex_trylock(&pool->assoc_mutex))) {
Tejun Heod565ed62013-01-24 11:01:33 -08002059 spin_unlock_irq(&pool->lock);
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002060 mutex_lock(&pool->assoc_mutex);
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002061 /*
2062 * CPU hotplug could have happened while we were waiting
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002063 * for assoc_mutex. Hotplug itself can't handle us
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002064 * because manager isn't either on idle or busy list, and
Tejun Heo706026c2013-01-24 11:01:34 -08002065 * @pool's state and ours could have deviated.
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002066 *
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002067 * As hotplug is now excluded via assoc_mutex, we can
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002068 * simply try to bind. It will succeed or fail depending
Tejun Heo706026c2013-01-24 11:01:34 -08002069 * on @pool's current state. Try it and adjust
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002070 * %WORKER_UNBOUND accordingly.
2071 */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08002072 if (worker_maybe_bind_and_lock(pool))
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002073 worker->flags &= ~WORKER_UNBOUND;
2074 else
2075 worker->flags |= WORKER_UNBOUND;
2076
2077 ret = true;
2078 }
2079
Tejun Heo11ebea52012-07-12 14:46:37 -07002080 pool->flags &= ~POOL_MANAGE_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02002081
2082 /*
2083 * Destroy and then create so that may_start_working() is true
2084 * on return.
2085 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002086 ret |= maybe_destroy_workers(pool);
2087 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002088
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002089 mutex_unlock(&pool->assoc_mutex);
Tejun Heo34a06bd2013-03-12 11:30:00 -07002090 mutex_unlock(&pool->manager_arb);
Tejun Heoe22bee72010-06-29 10:07:14 +02002091 return ret;
2092}
2093
Tejun Heoa62428c2010-06-29 10:07:10 +02002094/**
2095 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02002096 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02002097 * @work: work to process
2098 *
2099 * Process @work. This function contains all the logics necessary to
2100 * process a single work including synchronization against and
2101 * interaction with other workers on the same cpu, queueing and
2102 * flushing. As long as context requirement is met, any worker can
2103 * call this function to process a work.
2104 *
2105 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002106 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02002107 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002108static void process_one_work(struct worker *worker, struct work_struct *work)
Tejun Heod565ed62013-01-24 11:01:33 -08002109__releases(&pool->lock)
2110__acquires(&pool->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02002111{
Tejun Heo112202d2013-02-13 19:29:12 -08002112 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002113 struct worker_pool *pool = worker->pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002114 bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02002115 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02002116 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02002117#ifdef CONFIG_LOCKDEP
2118 /*
2119 * It is permissible to free the struct work_struct from
2120 * inside the function that is called from it, this we need to
2121 * take into account for lockdep too. To avoid bogus "held
2122 * lock freed" warnings as well as problems when looking into
2123 * work->lockdep_map, make a copy and use that here.
2124 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07002125 struct lockdep_map lockdep_map;
2126
2127 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002128#endif
Tejun Heo6fec10a2012-07-22 10:16:34 -07002129 /*
2130 * Ensure we're on the correct CPU. DISASSOCIATED test is
2131 * necessary to avoid spurious warnings from rescuers servicing the
Tejun Heo24647572013-01-24 11:01:33 -08002132 * unbound or a disassociated pool.
Tejun Heo6fec10a2012-07-22 10:16:34 -07002133 */
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002134 WARN_ON_ONCE(!(worker->flags & WORKER_UNBOUND) &&
Tejun Heo24647572013-01-24 11:01:33 -08002135 !(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heoec22ca52013-01-24 11:01:33 -08002136 raw_smp_processor_id() != pool->cpu);
Tejun Heo25511a42012-07-17 12:39:27 -07002137
Tejun Heo7e116292010-06-29 10:07:13 +02002138 /*
2139 * A single work shouldn't be executed concurrently by
2140 * multiple workers on a single cpu. Check whether anyone is
2141 * already processing the work. If so, defer the work to the
2142 * currently executing one.
2143 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002144 collision = find_worker_executing_work(pool, work);
Tejun Heo7e116292010-06-29 10:07:13 +02002145 if (unlikely(collision)) {
2146 move_linked_works(work, &collision->scheduled, NULL);
2147 return;
2148 }
2149
Tejun Heo8930cab2012-08-03 10:30:45 -07002150 /* claim and dequeue */
Tejun Heoa62428c2010-06-29 10:07:10 +02002151 debug_work_deactivate(work);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002152 hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work);
Tejun Heoc34056a2010-06-29 10:07:11 +02002153 worker->current_work = work;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002154 worker->current_func = work->func;
Tejun Heo112202d2013-02-13 19:29:12 -08002155 worker->current_pwq = pwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002156 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002157
Tejun Heoa62428c2010-06-29 10:07:10 +02002158 list_del_init(&work->entry);
2159
Tejun Heo649027d2010-06-29 10:07:14 +02002160 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02002161 * CPU intensive works don't participate in concurrency
2162 * management. They're the scheduler's responsibility.
2163 */
2164 if (unlikely(cpu_intensive))
2165 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
2166
Tejun Heo974271c2012-07-12 14:46:37 -07002167 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002168 * Unbound pool isn't concurrency managed and work items should be
Tejun Heo974271c2012-07-12 14:46:37 -07002169 * executed ASAP. Wake up another worker if necessary.
2170 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002171 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
2172 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07002173
Tejun Heo8930cab2012-08-03 10:30:45 -07002174 /*
Tejun Heo7c3eed52013-01-24 11:01:33 -08002175 * Record the last pool and clear PENDING which should be the last
Tejun Heod565ed62013-01-24 11:01:33 -08002176 * update to @work. Also, do this inside @pool->lock so that
Tejun Heo23657bb2012-08-13 17:08:19 -07002177 * PENDING and queued state changes happen together while IRQ is
2178 * disabled.
Tejun Heo8930cab2012-08-03 10:30:45 -07002179 */
Tejun Heo7c3eed52013-01-24 11:01:33 -08002180 set_work_pool_and_clear_pending(work, pool->id);
Tejun Heoa62428c2010-06-29 10:07:10 +02002181
Tejun Heod565ed62013-01-24 11:01:33 -08002182 spin_unlock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002183
Tejun Heo112202d2013-02-13 19:29:12 -08002184 lock_map_acquire_read(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002185 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002186 trace_workqueue_execute_start(work);
Tejun Heoa2c1c572012-12-18 10:35:02 -08002187 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002188 /*
2189 * While we must be careful to not use "work" after this, the trace
2190 * point will only record its address.
2191 */
2192 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002193 lock_map_release(&lockdep_map);
Tejun Heo112202d2013-02-13 19:29:12 -08002194 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002195
2196 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Valentin Ilie044c7822012-08-19 00:52:42 +03002197 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
2198 " last function: %pf\n",
Tejun Heoa2c1c572012-12-18 10:35:02 -08002199 current->comm, preempt_count(), task_pid_nr(current),
2200 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02002201 debug_show_held_locks(current);
2202 dump_stack();
2203 }
2204
Tejun Heod565ed62013-01-24 11:01:33 -08002205 spin_lock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002206
Tejun Heofb0e7be2010-06-29 10:07:15 +02002207 /* clear cpu intensive status */
2208 if (unlikely(cpu_intensive))
2209 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2210
Tejun Heoa62428c2010-06-29 10:07:10 +02002211 /* we're done with it, release */
Sasha Levin42f85702012-12-17 10:01:23 -05002212 hash_del(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002213 worker->current_work = NULL;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002214 worker->current_func = NULL;
Tejun Heo112202d2013-02-13 19:29:12 -08002215 worker->current_pwq = NULL;
2216 pwq_dec_nr_in_flight(pwq, work_color);
Tejun Heoa62428c2010-06-29 10:07:10 +02002217}
2218
Tejun Heoaffee4b2010-06-29 10:07:12 +02002219/**
2220 * process_scheduled_works - process scheduled works
2221 * @worker: self
2222 *
2223 * Process all scheduled works. Please note that the scheduled list
2224 * may change while processing a work, so this function repeatedly
2225 * fetches a work from the top and executes it.
2226 *
2227 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002228 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02002229 * multiple times.
2230 */
2231static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002233 while (!list_empty(&worker->scheduled)) {
2234 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002236 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238}
2239
Tejun Heo4690c4a2010-06-29 10:07:10 +02002240/**
2241 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02002242 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02002243 *
Tejun Heo706026c2013-01-24 11:01:34 -08002244 * The worker thread function. There are NR_CPU_WORKER_POOLS dynamic pools
2245 * of these per each cpu. These workers process all works regardless of
Tejun Heoe22bee72010-06-29 10:07:14 +02002246 * their specific target workqueue. The only exception is works which
2247 * belong to workqueues with a rescuer which will be explained in
2248 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02002249 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002250static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251{
Tejun Heoc34056a2010-06-29 10:07:11 +02002252 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07002253 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
Tejun Heoe22bee72010-06-29 10:07:14 +02002255 /* tell the scheduler that this is a workqueue worker */
2256 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002257woke_up:
Tejun Heod565ed62013-01-24 11:01:33 -08002258 spin_lock_irq(&pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002260 /* we are off idle list if destruction or rebind is requested */
2261 if (unlikely(list_empty(&worker->entry))) {
Tejun Heod565ed62013-01-24 11:01:33 -08002262 spin_unlock_irq(&pool->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07002263
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002264 /* if DIE is set, destruction is requested */
Tejun Heo25511a42012-07-17 12:39:27 -07002265 if (worker->flags & WORKER_DIE) {
2266 worker->task->flags &= ~PF_WQ_WORKER;
2267 return 0;
2268 }
2269
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002270 /* otherwise, rebind */
Tejun Heo25511a42012-07-17 12:39:27 -07002271 idle_worker_rebind(worker);
2272 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 }
2274
Tejun Heoc8e55f32010-06-29 10:07:12 +02002275 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002276recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002277 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002278 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002279 goto sleep;
2280
2281 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002282 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002283 goto recheck;
2284
Tejun Heoc8e55f32010-06-29 10:07:12 +02002285 /*
2286 * ->scheduled list can only be filled while a worker is
2287 * preparing to process a work or actually processing it.
2288 * Make sure nobody diddled with it while I was sleeping.
2289 */
Tejun Heo6183c002013-03-12 11:29:57 -07002290 WARN_ON_ONCE(!list_empty(&worker->scheduled));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002291
Tejun Heoe22bee72010-06-29 10:07:14 +02002292 /*
2293 * When control reaches this point, we're guaranteed to have
2294 * at least one idle worker or that someone else has already
2295 * assumed the manager role.
2296 */
2297 worker_clr_flags(worker, WORKER_PREP);
2298
2299 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002300 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002301 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002302 struct work_struct, entry);
2303
2304 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2305 /* optimization path, not strictly necessary */
2306 process_one_work(worker, work);
2307 if (unlikely(!list_empty(&worker->scheduled)))
2308 process_scheduled_works(worker);
2309 } else {
2310 move_linked_works(work, &worker->scheduled, NULL);
2311 process_scheduled_works(worker);
2312 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002313 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002314
Tejun Heoe22bee72010-06-29 10:07:14 +02002315 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002316sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07002317 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002318 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002319
Tejun Heoc8e55f32010-06-29 10:07:12 +02002320 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002321 * pool->lock is held and there's no work to process and no need to
2322 * manage, sleep. Workers are woken up only while holding
2323 * pool->lock or from local cpu, so setting the current state
2324 * before releasing pool->lock is enough to prevent losing any
2325 * event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002326 */
2327 worker_enter_idle(worker);
2328 __set_current_state(TASK_INTERRUPTIBLE);
Tejun Heod565ed62013-01-24 11:01:33 -08002329 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02002330 schedule();
2331 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332}
2333
Tejun Heoe22bee72010-06-29 10:07:14 +02002334/**
2335 * rescuer_thread - the rescuer thread function
Tejun Heo111c2252013-01-17 17:16:24 -08002336 * @__rescuer: self
Tejun Heoe22bee72010-06-29 10:07:14 +02002337 *
2338 * Workqueue rescuer thread function. There's one rescuer for each
Tejun Heo493008a2013-03-12 11:30:03 -07002339 * workqueue which has WQ_MEM_RECLAIM set.
Tejun Heoe22bee72010-06-29 10:07:14 +02002340 *
Tejun Heo706026c2013-01-24 11:01:34 -08002341 * Regular work processing on a pool may block trying to create a new
Tejun Heoe22bee72010-06-29 10:07:14 +02002342 * worker which uses GFP_KERNEL allocation which has slight chance of
2343 * developing into deadlock if some works currently on the same queue
2344 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2345 * the problem rescuer solves.
2346 *
Tejun Heo706026c2013-01-24 11:01:34 -08002347 * When such condition is possible, the pool summons rescuers of all
2348 * workqueues which have works queued on the pool and let them process
Tejun Heoe22bee72010-06-29 10:07:14 +02002349 * those works so that forward progress can be guaranteed.
2350 *
2351 * This should happen rarely.
2352 */
Tejun Heo111c2252013-01-17 17:16:24 -08002353static int rescuer_thread(void *__rescuer)
Tejun Heoe22bee72010-06-29 10:07:14 +02002354{
Tejun Heo111c2252013-01-17 17:16:24 -08002355 struct worker *rescuer = __rescuer;
2356 struct workqueue_struct *wq = rescuer->rescue_wq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002357 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heoe22bee72010-06-29 10:07:14 +02002358
2359 set_user_nice(current, RESCUER_NICE_LEVEL);
Tejun Heo111c2252013-01-17 17:16:24 -08002360
2361 /*
2362 * Mark rescuer as worker too. As WORKER_PREP is never cleared, it
2363 * doesn't participate in concurrency management.
2364 */
2365 rescuer->task->flags |= PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002366repeat:
2367 set_current_state(TASK_INTERRUPTIBLE);
2368
Mike Galbraith412d32e2012-11-28 07:17:18 +01002369 if (kthread_should_stop()) {
2370 __set_current_state(TASK_RUNNING);
Tejun Heo111c2252013-01-17 17:16:24 -08002371 rescuer->task->flags &= ~PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002372 return 0;
Mike Galbraith412d32e2012-11-28 07:17:18 +01002373 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002374
Tejun Heo493a1722013-03-12 11:29:59 -07002375 /* see whether any pwq is asking for help */
2376 spin_lock_irq(&workqueue_lock);
2377
2378 while (!list_empty(&wq->maydays)) {
2379 struct pool_workqueue *pwq = list_first_entry(&wq->maydays,
2380 struct pool_workqueue, mayday_node);
Tejun Heo112202d2013-02-13 19:29:12 -08002381 struct worker_pool *pool = pwq->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002382 struct work_struct *work, *n;
2383
2384 __set_current_state(TASK_RUNNING);
Tejun Heo493a1722013-03-12 11:29:59 -07002385 list_del_init(&pwq->mayday_node);
2386
2387 spin_unlock_irq(&workqueue_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002388
2389 /* migrate to the target cpu if possible */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08002390 worker_maybe_bind_and_lock(pool);
Lai Jiangshanb3104102013-02-19 12:17:02 -08002391 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002392
2393 /*
2394 * Slurp in all works issued via this workqueue and
2395 * process'em.
2396 */
Tejun Heo6183c002013-03-12 11:29:57 -07002397 WARN_ON_ONCE(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002398 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heo112202d2013-02-13 19:29:12 -08002399 if (get_work_pwq(work) == pwq)
Tejun Heoe22bee72010-06-29 10:07:14 +02002400 move_linked_works(work, scheduled, &n);
2401
2402 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002403
2404 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002405 * Leave this pool. If keep_working() is %true, notify a
Tejun Heo75769582011-02-14 14:04:46 +01002406 * regular worker; otherwise, we end up with 0 concurrency
2407 * and stalling the execution.
2408 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002409 if (keep_working(pool))
2410 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002411
Lai Jiangshanb3104102013-02-19 12:17:02 -08002412 rescuer->pool = NULL;
Tejun Heo493a1722013-03-12 11:29:59 -07002413 spin_unlock(&pool->lock);
2414 spin_lock(&workqueue_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002415 }
2416
Tejun Heo493a1722013-03-12 11:29:59 -07002417 spin_unlock_irq(&workqueue_lock);
2418
Tejun Heo111c2252013-01-17 17:16:24 -08002419 /* rescuers should never participate in concurrency management */
2420 WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
Tejun Heoe22bee72010-06-29 10:07:14 +02002421 schedule();
2422 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423}
2424
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002425struct wq_barrier {
2426 struct work_struct work;
2427 struct completion done;
2428};
2429
2430static void wq_barrier_func(struct work_struct *work)
2431{
2432 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2433 complete(&barr->done);
2434}
2435
Tejun Heo4690c4a2010-06-29 10:07:10 +02002436/**
2437 * insert_wq_barrier - insert a barrier work
Tejun Heo112202d2013-02-13 19:29:12 -08002438 * @pwq: pwq to insert barrier into
Tejun Heo4690c4a2010-06-29 10:07:10 +02002439 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002440 * @target: target work to attach @barr to
2441 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002442 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002443 * @barr is linked to @target such that @barr is completed only after
2444 * @target finishes execution. Please note that the ordering
2445 * guarantee is observed only with respect to @target and on the local
2446 * cpu.
2447 *
2448 * Currently, a queued barrier can't be canceled. This is because
2449 * try_to_grab_pending() can't determine whether the work to be
2450 * grabbed is at the head of the queue and thus can't clear LINKED
2451 * flag of the previous work while there must be a valid next work
2452 * after a work with LINKED flag set.
2453 *
2454 * Note that when @worker is non-NULL, @target may be modified
Tejun Heo112202d2013-02-13 19:29:12 -08002455 * underneath us, so we can't reliably determine pwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002456 *
2457 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002458 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002459 */
Tejun Heo112202d2013-02-13 19:29:12 -08002460static void insert_wq_barrier(struct pool_workqueue *pwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002461 struct wq_barrier *barr,
2462 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002463{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002464 struct list_head *head;
2465 unsigned int linked = 0;
2466
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002467 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002468 * debugobject calls are safe here even with pool->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002469 * as we know for sure that this will not trigger any of the
2470 * checks and call back into the fixup functions where we
2471 * might deadlock.
2472 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002473 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002474 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002475 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002476
Tejun Heoaffee4b2010-06-29 10:07:12 +02002477 /*
2478 * If @target is currently being executed, schedule the
2479 * barrier to the worker; otherwise, put it after @target.
2480 */
2481 if (worker)
2482 head = worker->scheduled.next;
2483 else {
2484 unsigned long *bits = work_data_bits(target);
2485
2486 head = target->entry.next;
2487 /* there can already be other linked works, inherit and set */
2488 linked = *bits & WORK_STRUCT_LINKED;
2489 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2490 }
2491
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002492 debug_work_activate(&barr->work);
Tejun Heo112202d2013-02-13 19:29:12 -08002493 insert_work(pwq, &barr->work, head,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002494 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002495}
2496
Tejun Heo73f53c42010-06-29 10:07:11 +02002497/**
Tejun Heo112202d2013-02-13 19:29:12 -08002498 * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing
Tejun Heo73f53c42010-06-29 10:07:11 +02002499 * @wq: workqueue being flushed
2500 * @flush_color: new flush color, < 0 for no-op
2501 * @work_color: new work color, < 0 for no-op
2502 *
Tejun Heo112202d2013-02-13 19:29:12 -08002503 * Prepare pwqs for workqueue flushing.
Tejun Heo73f53c42010-06-29 10:07:11 +02002504 *
Tejun Heo112202d2013-02-13 19:29:12 -08002505 * If @flush_color is non-negative, flush_color on all pwqs should be
2506 * -1. If no pwq has in-flight commands at the specified color, all
2507 * pwq->flush_color's stay at -1 and %false is returned. If any pwq
2508 * has in flight commands, its pwq->flush_color is set to
2509 * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq
Tejun Heo73f53c42010-06-29 10:07:11 +02002510 * wakeup logic is armed and %true is returned.
2511 *
2512 * The caller should have initialized @wq->first_flusher prior to
2513 * calling this function with non-negative @flush_color. If
2514 * @flush_color is negative, no flush color update is done and %false
2515 * is returned.
2516 *
Tejun Heo112202d2013-02-13 19:29:12 -08002517 * If @work_color is non-negative, all pwqs should have the same
Tejun Heo73f53c42010-06-29 10:07:11 +02002518 * work_color which is previous to @work_color and all will be
2519 * advanced to @work_color.
2520 *
2521 * CONTEXT:
2522 * mutex_lock(wq->flush_mutex).
2523 *
2524 * RETURNS:
2525 * %true if @flush_color >= 0 and there's something to flush. %false
2526 * otherwise.
2527 */
Tejun Heo112202d2013-02-13 19:29:12 -08002528static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
Tejun Heo73f53c42010-06-29 10:07:11 +02002529 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530{
Tejun Heo73f53c42010-06-29 10:07:11 +02002531 bool wait = false;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002532 struct pool_workqueue *pwq;
Oleg Nesterov14441962007-05-23 13:57:57 -07002533
Tejun Heo73f53c42010-06-29 10:07:11 +02002534 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002535 WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush));
Tejun Heo112202d2013-02-13 19:29:12 -08002536 atomic_set(&wq->nr_pwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002537 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002538
Tejun Heo76af4d92013-03-12 11:30:00 -07002539 local_irq_disable();
2540
Tejun Heo49e3cf42013-03-12 11:29:58 -07002541 for_each_pwq(pwq, wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08002542 struct worker_pool *pool = pwq->pool;
Tejun Heo73f53c42010-06-29 10:07:11 +02002543
Tejun Heo76af4d92013-03-12 11:30:00 -07002544 spin_lock(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002545
2546 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002547 WARN_ON_ONCE(pwq->flush_color != -1);
Tejun Heo73f53c42010-06-29 10:07:11 +02002548
Tejun Heo112202d2013-02-13 19:29:12 -08002549 if (pwq->nr_in_flight[flush_color]) {
2550 pwq->flush_color = flush_color;
2551 atomic_inc(&wq->nr_pwqs_to_flush);
Tejun Heo73f53c42010-06-29 10:07:11 +02002552 wait = true;
2553 }
2554 }
2555
2556 if (work_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002557 WARN_ON_ONCE(work_color != work_next_color(pwq->work_color));
Tejun Heo112202d2013-02-13 19:29:12 -08002558 pwq->work_color = work_color;
Tejun Heo73f53c42010-06-29 10:07:11 +02002559 }
2560
Tejun Heo76af4d92013-03-12 11:30:00 -07002561 spin_unlock(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002562 }
2563
Tejun Heo76af4d92013-03-12 11:30:00 -07002564 local_irq_enable();
2565
Tejun Heo112202d2013-02-13 19:29:12 -08002566 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush))
Tejun Heo73f53c42010-06-29 10:07:11 +02002567 complete(&wq->first_flusher->done);
2568
2569 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570}
2571
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002572/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002574 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 *
2576 * Forces execution of the workqueue and blocks until its completion.
2577 * This is typically used in driver shutdown handlers.
2578 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002579 * We sleep until all works which were queued on entry have been handled,
2580 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002582void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583{
Tejun Heo73f53c42010-06-29 10:07:11 +02002584 struct wq_flusher this_flusher = {
2585 .list = LIST_HEAD_INIT(this_flusher.list),
2586 .flush_color = -1,
2587 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2588 };
2589 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002590
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002591 lock_map_acquire(&wq->lockdep_map);
2592 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002593
2594 mutex_lock(&wq->flush_mutex);
2595
2596 /*
2597 * Start-to-wait phase
2598 */
2599 next_color = work_next_color(wq->work_color);
2600
2601 if (next_color != wq->flush_color) {
2602 /*
2603 * Color space is not full. The current work_color
2604 * becomes our flush_color and work_color is advanced
2605 * by one.
2606 */
Tejun Heo6183c002013-03-12 11:29:57 -07002607 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow));
Tejun Heo73f53c42010-06-29 10:07:11 +02002608 this_flusher.flush_color = wq->work_color;
2609 wq->work_color = next_color;
2610
2611 if (!wq->first_flusher) {
2612 /* no flush in progress, become the first flusher */
Tejun Heo6183c002013-03-12 11:29:57 -07002613 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002614
2615 wq->first_flusher = &this_flusher;
2616
Tejun Heo112202d2013-02-13 19:29:12 -08002617 if (!flush_workqueue_prep_pwqs(wq, wq->flush_color,
Tejun Heo73f53c42010-06-29 10:07:11 +02002618 wq->work_color)) {
2619 /* nothing to flush, done */
2620 wq->flush_color = next_color;
2621 wq->first_flusher = NULL;
2622 goto out_unlock;
2623 }
2624 } else {
2625 /* wait in queue */
Tejun Heo6183c002013-03-12 11:29:57 -07002626 WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002627 list_add_tail(&this_flusher.list, &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002628 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002629 }
2630 } else {
2631 /*
2632 * Oops, color space is full, wait on overflow queue.
2633 * The next flush completion will assign us
2634 * flush_color and transfer to flusher_queue.
2635 */
2636 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2637 }
2638
2639 mutex_unlock(&wq->flush_mutex);
2640
2641 wait_for_completion(&this_flusher.done);
2642
2643 /*
2644 * Wake-up-and-cascade phase
2645 *
2646 * First flushers are responsible for cascading flushes and
2647 * handling overflow. Non-first flushers can simply return.
2648 */
2649 if (wq->first_flusher != &this_flusher)
2650 return;
2651
2652 mutex_lock(&wq->flush_mutex);
2653
Tejun Heo4ce48b32010-07-02 10:03:51 +02002654 /* we might have raced, check again with mutex held */
2655 if (wq->first_flusher != &this_flusher)
2656 goto out_unlock;
2657
Tejun Heo73f53c42010-06-29 10:07:11 +02002658 wq->first_flusher = NULL;
2659
Tejun Heo6183c002013-03-12 11:29:57 -07002660 WARN_ON_ONCE(!list_empty(&this_flusher.list));
2661 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002662
2663 while (true) {
2664 struct wq_flusher *next, *tmp;
2665
2666 /* complete all the flushers sharing the current flush color */
2667 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2668 if (next->flush_color != wq->flush_color)
2669 break;
2670 list_del_init(&next->list);
2671 complete(&next->done);
2672 }
2673
Tejun Heo6183c002013-03-12 11:29:57 -07002674 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) &&
2675 wq->flush_color != work_next_color(wq->work_color));
Tejun Heo73f53c42010-06-29 10:07:11 +02002676
2677 /* this flush_color is finished, advance by one */
2678 wq->flush_color = work_next_color(wq->flush_color);
2679
2680 /* one color has been freed, handle overflow queue */
2681 if (!list_empty(&wq->flusher_overflow)) {
2682 /*
2683 * Assign the same color to all overflowed
2684 * flushers, advance work_color and append to
2685 * flusher_queue. This is the start-to-wait
2686 * phase for these overflowed flushers.
2687 */
2688 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2689 tmp->flush_color = wq->work_color;
2690
2691 wq->work_color = work_next_color(wq->work_color);
2692
2693 list_splice_tail_init(&wq->flusher_overflow,
2694 &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002695 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002696 }
2697
2698 if (list_empty(&wq->flusher_queue)) {
Tejun Heo6183c002013-03-12 11:29:57 -07002699 WARN_ON_ONCE(wq->flush_color != wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002700 break;
2701 }
2702
2703 /*
2704 * Need to flush more colors. Make the next flusher
Tejun Heo112202d2013-02-13 19:29:12 -08002705 * the new first flusher and arm pwqs.
Tejun Heo73f53c42010-06-29 10:07:11 +02002706 */
Tejun Heo6183c002013-03-12 11:29:57 -07002707 WARN_ON_ONCE(wq->flush_color == wq->work_color);
2708 WARN_ON_ONCE(wq->flush_color != next->flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002709
2710 list_del_init(&next->list);
2711 wq->first_flusher = next;
2712
Tejun Heo112202d2013-02-13 19:29:12 -08002713 if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1))
Tejun Heo73f53c42010-06-29 10:07:11 +02002714 break;
2715
2716 /*
2717 * Meh... this color is already done, clear first
2718 * flusher and repeat cascading.
2719 */
2720 wq->first_flusher = NULL;
2721 }
2722
2723out_unlock:
2724 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725}
Dave Jonesae90dd52006-06-30 01:40:45 -04002726EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002728/**
2729 * drain_workqueue - drain a workqueue
2730 * @wq: workqueue to drain
2731 *
2732 * Wait until the workqueue becomes empty. While draining is in progress,
2733 * only chain queueing is allowed. IOW, only currently pending or running
2734 * work items on @wq can queue further work items on it. @wq is flushed
2735 * repeatedly until it becomes empty. The number of flushing is detemined
2736 * by the depth of chaining and should be relatively short. Whine if it
2737 * takes too long.
2738 */
2739void drain_workqueue(struct workqueue_struct *wq)
2740{
2741 unsigned int flush_cnt = 0;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002742 struct pool_workqueue *pwq;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002743
2744 /*
2745 * __queue_work() needs to test whether there are drainers, is much
2746 * hotter than drain_workqueue() and already looks at @wq->flags.
2747 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2748 */
Tejun Heoe98d5b12013-03-12 11:29:57 -07002749 spin_lock_irq(&workqueue_lock);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002750 if (!wq->nr_drainers++)
2751 wq->flags |= WQ_DRAINING;
Tejun Heoe98d5b12013-03-12 11:29:57 -07002752 spin_unlock_irq(&workqueue_lock);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002753reflush:
2754 flush_workqueue(wq);
2755
Tejun Heo76af4d92013-03-12 11:30:00 -07002756 local_irq_disable();
2757
Tejun Heo49e3cf42013-03-12 11:29:58 -07002758 for_each_pwq(pwq, wq) {
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002759 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002760
Tejun Heo76af4d92013-03-12 11:30:00 -07002761 spin_lock(&pwq->pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08002762 drained = !pwq->nr_active && list_empty(&pwq->delayed_works);
Tejun Heo76af4d92013-03-12 11:30:00 -07002763 spin_unlock(&pwq->pool->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002764
2765 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002766 continue;
2767
2768 if (++flush_cnt == 10 ||
2769 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
Valentin Ilie044c7822012-08-19 00:52:42 +03002770 pr_warn("workqueue %s: flush on destruction isn't complete after %u tries\n",
2771 wq->name, flush_cnt);
Tejun Heo76af4d92013-03-12 11:30:00 -07002772
2773 local_irq_enable();
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002774 goto reflush;
2775 }
2776
Tejun Heo76af4d92013-03-12 11:30:00 -07002777 spin_lock(&workqueue_lock);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002778 if (!--wq->nr_drainers)
2779 wq->flags &= ~WQ_DRAINING;
Tejun Heo76af4d92013-03-12 11:30:00 -07002780 spin_unlock(&workqueue_lock);
2781
2782 local_irq_enable();
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002783}
2784EXPORT_SYMBOL_GPL(drain_workqueue);
2785
Tejun Heo606a5022012-08-20 14:51:23 -07002786static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
Tejun Heobaf59022010-09-16 10:42:16 +02002787{
2788 struct worker *worker = NULL;
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002789 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002790 struct pool_workqueue *pwq;
Tejun Heobaf59022010-09-16 10:42:16 +02002791
2792 might_sleep();
Tejun Heobaf59022010-09-16 10:42:16 +02002793
Tejun Heofa1b54e2013-03-12 11:30:00 -07002794 local_irq_disable();
2795 pool = get_work_pool(work);
2796 if (!pool) {
2797 local_irq_enable();
2798 return false;
2799 }
2800
2801 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08002802 /* see the comment in try_to_grab_pending() with the same code */
Tejun Heo112202d2013-02-13 19:29:12 -08002803 pwq = get_work_pwq(work);
2804 if (pwq) {
2805 if (unlikely(pwq->pool != pool))
Tejun Heobaf59022010-09-16 10:42:16 +02002806 goto already_gone;
Tejun Heo606a5022012-08-20 14:51:23 -07002807 } else {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002808 worker = find_worker_executing_work(pool, work);
Tejun Heobaf59022010-09-16 10:42:16 +02002809 if (!worker)
2810 goto already_gone;
Tejun Heo112202d2013-02-13 19:29:12 -08002811 pwq = worker->current_pwq;
Tejun Heo606a5022012-08-20 14:51:23 -07002812 }
Tejun Heobaf59022010-09-16 10:42:16 +02002813
Tejun Heo112202d2013-02-13 19:29:12 -08002814 insert_wq_barrier(pwq, barr, work, worker);
Tejun Heod565ed62013-01-24 11:01:33 -08002815 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002816
Tejun Heoe1594892011-01-09 23:32:15 +01002817 /*
2818 * If @max_active is 1 or rescuer is in use, flushing another work
2819 * item on the same workqueue may lead to deadlock. Make sure the
2820 * flusher is not running on the same workqueue by verifying write
2821 * access.
2822 */
Tejun Heo493008a2013-03-12 11:30:03 -07002823 if (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)
Tejun Heo112202d2013-02-13 19:29:12 -08002824 lock_map_acquire(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002825 else
Tejun Heo112202d2013-02-13 19:29:12 -08002826 lock_map_acquire_read(&pwq->wq->lockdep_map);
2827 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002828
Tejun Heobaf59022010-09-16 10:42:16 +02002829 return true;
2830already_gone:
Tejun Heod565ed62013-01-24 11:01:33 -08002831 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002832 return false;
2833}
2834
Oleg Nesterovdb700892008-07-25 01:47:49 -07002835/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002836 * flush_work - wait for a work to finish executing the last queueing instance
2837 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002838 *
Tejun Heo606a5022012-08-20 14:51:23 -07002839 * Wait until @work has finished execution. @work is guaranteed to be idle
2840 * on return if it hasn't been requeued since flush started.
Tejun Heo401a8d02010-09-16 10:36:00 +02002841 *
2842 * RETURNS:
2843 * %true if flush_work() waited for the work to finish execution,
2844 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002845 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002846bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002847{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002848 struct wq_barrier barr;
2849
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002850 lock_map_acquire(&work->lockdep_map);
2851 lock_map_release(&work->lockdep_map);
2852
Tejun Heo606a5022012-08-20 14:51:23 -07002853 if (start_flush_work(work, &barr)) {
Tejun Heobaf59022010-09-16 10:42:16 +02002854 wait_for_completion(&barr.done);
2855 destroy_work_on_stack(&barr.work);
2856 return true;
Tejun Heo606a5022012-08-20 14:51:23 -07002857 } else {
Tejun Heobaf59022010-09-16 10:42:16 +02002858 return false;
Tejun Heo606a5022012-08-20 14:51:23 -07002859 }
Oleg Nesterovdb700892008-07-25 01:47:49 -07002860}
2861EXPORT_SYMBOL_GPL(flush_work);
2862
Tejun Heo36e227d2012-08-03 10:30:46 -07002863static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
Tejun Heo401a8d02010-09-16 10:36:00 +02002864{
Tejun Heobbb68df2012-08-03 10:30:46 -07002865 unsigned long flags;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002866 int ret;
2867
2868 do {
Tejun Heobbb68df2012-08-03 10:30:46 -07002869 ret = try_to_grab_pending(work, is_dwork, &flags);
2870 /*
2871 * If someone else is canceling, wait for the same event it
2872 * would be waiting for before retrying.
2873 */
2874 if (unlikely(ret == -ENOENT))
Tejun Heo606a5022012-08-20 14:51:23 -07002875 flush_work(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002876 } while (unlikely(ret < 0));
2877
Tejun Heobbb68df2012-08-03 10:30:46 -07002878 /* tell other tasks trying to grab @work to back off */
2879 mark_work_canceling(work);
2880 local_irq_restore(flags);
2881
Tejun Heo606a5022012-08-20 14:51:23 -07002882 flush_work(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002883 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002884 return ret;
2885}
2886
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002887/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002888 * cancel_work_sync - cancel a work and wait for it to finish
2889 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002890 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002891 * Cancel @work and wait for its execution to finish. This function
2892 * can be used even if the work re-queues itself or migrates to
2893 * another workqueue. On return from this function, @work is
2894 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002895 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002896 * cancel_work_sync(&delayed_work->work) must not be used for
2897 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002898 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002899 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002900 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002901 *
2902 * RETURNS:
2903 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002904 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002905bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002906{
Tejun Heo36e227d2012-08-03 10:30:46 -07002907 return __cancel_work_timer(work, false);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002908}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002909EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002910
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002911/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002912 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2913 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002914 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002915 * Delayed timer is cancelled and the pending work is queued for
2916 * immediate execution. Like flush_work(), this function only
2917 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002918 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002919 * RETURNS:
2920 * %true if flush_work() waited for the work to finish execution,
2921 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002922 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002923bool flush_delayed_work(struct delayed_work *dwork)
2924{
Tejun Heo8930cab2012-08-03 10:30:45 -07002925 local_irq_disable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002926 if (del_timer_sync(&dwork->timer))
Lai Jiangshan60c057b2013-02-06 18:04:53 -08002927 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Tejun Heo8930cab2012-08-03 10:30:45 -07002928 local_irq_enable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002929 return flush_work(&dwork->work);
2930}
2931EXPORT_SYMBOL(flush_delayed_work);
2932
2933/**
Tejun Heo57b30ae2012-08-21 13:18:24 -07002934 * cancel_delayed_work - cancel a delayed work
2935 * @dwork: delayed_work to cancel
Tejun Heo09383492010-09-16 10:48:29 +02002936 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002937 * Kill off a pending delayed_work. Returns %true if @dwork was pending
2938 * and canceled; %false if wasn't pending. Note that the work callback
2939 * function may still be running on return, unless it returns %true and the
2940 * work doesn't re-arm itself. Explicitly flush or use
2941 * cancel_delayed_work_sync() to wait on it.
Tejun Heo09383492010-09-16 10:48:29 +02002942 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002943 * This function is safe to call from any context including IRQ handler.
Tejun Heo09383492010-09-16 10:48:29 +02002944 */
Tejun Heo57b30ae2012-08-21 13:18:24 -07002945bool cancel_delayed_work(struct delayed_work *dwork)
Tejun Heo09383492010-09-16 10:48:29 +02002946{
Tejun Heo57b30ae2012-08-21 13:18:24 -07002947 unsigned long flags;
2948 int ret;
2949
2950 do {
2951 ret = try_to_grab_pending(&dwork->work, true, &flags);
2952 } while (unlikely(ret == -EAGAIN));
2953
2954 if (unlikely(ret < 0))
2955 return false;
2956
Tejun Heo7c3eed52013-01-24 11:01:33 -08002957 set_work_pool_and_clear_pending(&dwork->work,
2958 get_work_pool_id(&dwork->work));
Tejun Heo57b30ae2012-08-21 13:18:24 -07002959 local_irq_restore(flags);
Dan Magenheimerc0158ca2012-10-18 16:31:37 -07002960 return ret;
Tejun Heo09383492010-09-16 10:48:29 +02002961}
Tejun Heo57b30ae2012-08-21 13:18:24 -07002962EXPORT_SYMBOL(cancel_delayed_work);
Tejun Heo09383492010-09-16 10:48:29 +02002963
2964/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002965 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2966 * @dwork: the delayed work cancel
2967 *
2968 * This is cancel_work_sync() for delayed works.
2969 *
2970 * RETURNS:
2971 * %true if @dwork was pending, %false otherwise.
2972 */
2973bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002974{
Tejun Heo36e227d2012-08-03 10:30:46 -07002975 return __cancel_work_timer(&dwork->work, true);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002976}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002977EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002979/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07002980 * schedule_work_on - put work task on a specific cpu
2981 * @cpu: cpu to put the work task on
2982 * @work: job to be done
2983 *
2984 * This puts a job on a specific cpu
2985 */
Tejun Heod4283e92012-08-03 10:30:44 -07002986bool schedule_work_on(int cpu, struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07002987{
Tejun Heod320c032010-06-29 10:07:14 +02002988 return queue_work_on(cpu, system_wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07002989}
2990EXPORT_SYMBOL(schedule_work_on);
2991
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002992/**
Dave Jonesae90dd52006-06-30 01:40:45 -04002993 * schedule_work - put work task in global workqueue
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 * @work: job to be done
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 *
Tejun Heod4283e92012-08-03 10:30:44 -07002996 * Returns %false if @work was already on the kernel-global workqueue and
2997 * %true otherwise.
David Howells52bad642006-11-22 14:54:01 +00002998 *
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002999 * This puts a job in the kernel-global workqueue if it was not already
3000 * queued and leaves it in the same position on the kernel-global
3001 * workqueue otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 */
Tejun Heod4283e92012-08-03 10:30:44 -07003003bool schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004{
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003005 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006}
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003007EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003009/**
3010 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
3011 * @cpu: cpu to use
3012 * @dwork: job to be done
3013 * @delay: number of jiffies to wait
3014 *
3015 * After waiting for a given time this puts a job in the kernel-global
3016 * workqueue on the specified CPU.
3017 */
Tejun Heod4283e92012-08-03 10:30:44 -07003018bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork,
3019 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020{
Tejun Heod320c032010-06-29 10:07:14 +02003021 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022}
Dave Jonesae90dd52006-06-30 01:40:45 -04003023EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
Andrew Mortonb6136772006-06-25 05:47:49 -07003025/**
Tejun Heo0a13c002012-08-03 10:30:44 -07003026 * schedule_delayed_work - put work task in global workqueue after delay
3027 * @dwork: job to be done
3028 * @delay: number of jiffies to wait or 0 for immediate execution
3029 *
3030 * After waiting for a given time this puts a job in the kernel-global
3031 * workqueue.
3032 */
Tejun Heod4283e92012-08-03 10:30:44 -07003033bool schedule_delayed_work(struct delayed_work *dwork, unsigned long delay)
Tejun Heo0a13c002012-08-03 10:30:44 -07003034{
3035 return queue_delayed_work(system_wq, dwork, delay);
3036}
3037EXPORT_SYMBOL(schedule_delayed_work);
3038
3039/**
Tejun Heo31ddd872010-10-19 11:14:49 +02003040 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07003041 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07003042 *
Tejun Heo31ddd872010-10-19 11:14:49 +02003043 * schedule_on_each_cpu() executes @func on each online CPU using the
3044 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07003045 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02003046 *
3047 * RETURNS:
3048 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07003049 */
David Howells65f27f32006-11-22 14:55:48 +00003050int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08003051{
3052 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02003053 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08003054
Andrew Mortonb6136772006-06-25 05:47:49 -07003055 works = alloc_percpu(struct work_struct);
3056 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08003057 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07003058
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003059 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08003060
Christoph Lameter15316ba2006-01-08 01:00:43 -08003061 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01003062 struct work_struct *work = per_cpu_ptr(works, cpu);
3063
3064 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003065 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02003066 }
Tejun Heo93981802009-11-17 14:06:20 -08003067
3068 for_each_online_cpu(cpu)
3069 flush_work(per_cpu_ptr(works, cpu));
3070
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003071 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07003072 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08003073 return 0;
3074}
3075
Alan Sterneef6a7d2010-02-12 17:39:21 +09003076/**
3077 * flush_scheduled_work - ensure that any scheduled work has run to completion.
3078 *
3079 * Forces execution of the kernel-global workqueue and blocks until its
3080 * completion.
3081 *
3082 * Think twice before calling this function! It's very easy to get into
3083 * trouble if you don't take great care. Either of the following situations
3084 * will lead to deadlock:
3085 *
3086 * One of the work items currently on the workqueue needs to acquire
3087 * a lock held by your code or its caller.
3088 *
3089 * Your code is running in the context of a work routine.
3090 *
3091 * They will be detected by lockdep when they occur, but the first might not
3092 * occur very often. It depends on what work items are on the workqueue and
3093 * what locks they need, which you have no control over.
3094 *
3095 * In most situations flushing the entire workqueue is overkill; you merely
3096 * need to know that a particular work item isn't queued and isn't running.
3097 * In such cases you should use cancel_delayed_work_sync() or
3098 * cancel_work_sync() instead.
3099 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100void flush_scheduled_work(void)
3101{
Tejun Heod320c032010-06-29 10:07:14 +02003102 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103}
Dave Jonesae90dd52006-06-30 01:40:45 -04003104EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
3106/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06003107 * execute_in_process_context - reliably execute the routine with user context
3108 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06003109 * @ew: guaranteed storage for the execute work structure (must
3110 * be available when the work executes)
3111 *
3112 * Executes the function immediately if process context is available,
3113 * otherwise schedules the function for delayed execution.
3114 *
3115 * Returns: 0 - function was executed
3116 * 1 - function was scheduled for execution
3117 */
David Howells65f27f32006-11-22 14:55:48 +00003118int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06003119{
3120 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00003121 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003122 return 0;
3123 }
3124
David Howells65f27f32006-11-22 14:55:48 +00003125 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003126 schedule_work(&ew->work);
3127
3128 return 1;
3129}
3130EXPORT_SYMBOL_GPL(execute_in_process_context);
3131
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132int keventd_up(void)
3133{
Tejun Heod320c032010-06-29 10:07:14 +02003134 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135}
3136
Tejun Heo7a4e3442013-03-12 11:30:00 -07003137/**
3138 * free_workqueue_attrs - free a workqueue_attrs
3139 * @attrs: workqueue_attrs to free
3140 *
3141 * Undo alloc_workqueue_attrs().
3142 */
3143void free_workqueue_attrs(struct workqueue_attrs *attrs)
3144{
3145 if (attrs) {
3146 free_cpumask_var(attrs->cpumask);
3147 kfree(attrs);
3148 }
3149}
3150
3151/**
3152 * alloc_workqueue_attrs - allocate a workqueue_attrs
3153 * @gfp_mask: allocation mask to use
3154 *
3155 * Allocate a new workqueue_attrs, initialize with default settings and
3156 * return it. Returns NULL on failure.
3157 */
3158struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask)
3159{
3160 struct workqueue_attrs *attrs;
3161
3162 attrs = kzalloc(sizeof(*attrs), gfp_mask);
3163 if (!attrs)
3164 goto fail;
3165 if (!alloc_cpumask_var(&attrs->cpumask, gfp_mask))
3166 goto fail;
3167
3168 cpumask_setall(attrs->cpumask);
3169 return attrs;
3170fail:
3171 free_workqueue_attrs(attrs);
3172 return NULL;
3173}
3174
Tejun Heo29c91e92013-03-12 11:30:03 -07003175static void copy_workqueue_attrs(struct workqueue_attrs *to,
3176 const struct workqueue_attrs *from)
3177{
3178 to->nice = from->nice;
3179 cpumask_copy(to->cpumask, from->cpumask);
3180}
3181
3182/*
3183 * Hacky implementation of jhash of bitmaps which only considers the
3184 * specified number of bits. We probably want a proper implementation in
3185 * include/linux/jhash.h.
3186 */
3187static u32 jhash_bitmap(const unsigned long *bitmap, int bits, u32 hash)
3188{
3189 int nr_longs = bits / BITS_PER_LONG;
3190 int nr_leftover = bits % BITS_PER_LONG;
3191 unsigned long leftover = 0;
3192
3193 if (nr_longs)
3194 hash = jhash(bitmap, nr_longs * sizeof(long), hash);
3195 if (nr_leftover) {
3196 bitmap_copy(&leftover, bitmap + nr_longs, nr_leftover);
3197 hash = jhash(&leftover, sizeof(long), hash);
3198 }
3199 return hash;
3200}
3201
3202/* hash value of the content of @attr */
3203static u32 wqattrs_hash(const struct workqueue_attrs *attrs)
3204{
3205 u32 hash = 0;
3206
3207 hash = jhash_1word(attrs->nice, hash);
3208 hash = jhash_bitmap(cpumask_bits(attrs->cpumask), nr_cpu_ids, hash);
3209 return hash;
3210}
3211
3212/* content equality test */
3213static bool wqattrs_equal(const struct workqueue_attrs *a,
3214 const struct workqueue_attrs *b)
3215{
3216 if (a->nice != b->nice)
3217 return false;
3218 if (!cpumask_equal(a->cpumask, b->cpumask))
3219 return false;
3220 return true;
3221}
3222
Tejun Heo7a4e3442013-03-12 11:30:00 -07003223/**
3224 * init_worker_pool - initialize a newly zalloc'd worker_pool
3225 * @pool: worker_pool to initialize
3226 *
3227 * Initiailize a newly zalloc'd @pool. It also allocates @pool->attrs.
Tejun Heo29c91e92013-03-12 11:30:03 -07003228 * Returns 0 on success, -errno on failure. Even on failure, all fields
3229 * inside @pool proper are initialized and put_unbound_pool() can be called
3230 * on @pool safely to release it.
Tejun Heo7a4e3442013-03-12 11:30:00 -07003231 */
3232static int init_worker_pool(struct worker_pool *pool)
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003233{
3234 spin_lock_init(&pool->lock);
Tejun Heo29c91e92013-03-12 11:30:03 -07003235 pool->id = -1;
3236 pool->cpu = -1;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003237 pool->flags |= POOL_DISASSOCIATED;
3238 INIT_LIST_HEAD(&pool->worklist);
3239 INIT_LIST_HEAD(&pool->idle_list);
3240 hash_init(pool->busy_hash);
3241
3242 init_timer_deferrable(&pool->idle_timer);
3243 pool->idle_timer.function = idle_worker_timeout;
3244 pool->idle_timer.data = (unsigned long)pool;
3245
3246 setup_timer(&pool->mayday_timer, pool_mayday_timeout,
3247 (unsigned long)pool);
3248
3249 mutex_init(&pool->manager_arb);
3250 mutex_init(&pool->assoc_mutex);
3251 ida_init(&pool->worker_ida);
Tejun Heo7a4e3442013-03-12 11:30:00 -07003252
Tejun Heo29c91e92013-03-12 11:30:03 -07003253 INIT_HLIST_NODE(&pool->hash_node);
3254 pool->refcnt = 1;
3255
3256 /* shouldn't fail above this point */
Tejun Heo7a4e3442013-03-12 11:30:00 -07003257 pool->attrs = alloc_workqueue_attrs(GFP_KERNEL);
3258 if (!pool->attrs)
3259 return -ENOMEM;
3260 return 0;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003261}
3262
Tejun Heo29c91e92013-03-12 11:30:03 -07003263static void rcu_free_pool(struct rcu_head *rcu)
3264{
3265 struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu);
3266
3267 ida_destroy(&pool->worker_ida);
3268 free_workqueue_attrs(pool->attrs);
3269 kfree(pool);
3270}
3271
3272/**
3273 * put_unbound_pool - put a worker_pool
3274 * @pool: worker_pool to put
3275 *
3276 * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU
3277 * safe manner.
3278 */
3279static void put_unbound_pool(struct worker_pool *pool)
3280{
3281 struct worker *worker;
3282
3283 spin_lock_irq(&workqueue_lock);
3284 if (--pool->refcnt) {
3285 spin_unlock_irq(&workqueue_lock);
3286 return;
3287 }
3288
3289 /* sanity checks */
3290 if (WARN_ON(!(pool->flags & POOL_DISASSOCIATED)) ||
3291 WARN_ON(!list_empty(&pool->worklist))) {
3292 spin_unlock_irq(&workqueue_lock);
3293 return;
3294 }
3295
3296 /* release id and unhash */
3297 if (pool->id >= 0)
3298 idr_remove(&worker_pool_idr, pool->id);
3299 hash_del(&pool->hash_node);
3300
3301 spin_unlock_irq(&workqueue_lock);
3302
3303 /* lock out manager and destroy all workers */
3304 mutex_lock(&pool->manager_arb);
3305 spin_lock_irq(&pool->lock);
3306
3307 while ((worker = first_worker(pool)))
3308 destroy_worker(worker);
3309 WARN_ON(pool->nr_workers || pool->nr_idle);
3310
3311 spin_unlock_irq(&pool->lock);
3312 mutex_unlock(&pool->manager_arb);
3313
3314 /* shut down the timers */
3315 del_timer_sync(&pool->idle_timer);
3316 del_timer_sync(&pool->mayday_timer);
3317
3318 /* sched-RCU protected to allow dereferences from get_work_pool() */
3319 call_rcu_sched(&pool->rcu, rcu_free_pool);
3320}
3321
3322/**
3323 * get_unbound_pool - get a worker_pool with the specified attributes
3324 * @attrs: the attributes of the worker_pool to get
3325 *
3326 * Obtain a worker_pool which has the same attributes as @attrs, bump the
3327 * reference count and return it. If there already is a matching
3328 * worker_pool, it will be used; otherwise, this function attempts to
3329 * create a new one. On failure, returns NULL.
3330 */
3331static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs)
3332{
3333 static DEFINE_MUTEX(create_mutex);
3334 u32 hash = wqattrs_hash(attrs);
3335 struct worker_pool *pool;
3336 struct worker *worker;
3337
3338 mutex_lock(&create_mutex);
3339
3340 /* do we already have a matching pool? */
3341 spin_lock_irq(&workqueue_lock);
3342 hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) {
3343 if (wqattrs_equal(pool->attrs, attrs)) {
3344 pool->refcnt++;
3345 goto out_unlock;
3346 }
3347 }
3348 spin_unlock_irq(&workqueue_lock);
3349
3350 /* nope, create a new one */
3351 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
3352 if (!pool || init_worker_pool(pool) < 0)
3353 goto fail;
3354
Tejun Heo8864b4e2013-03-12 11:30:04 -07003355 lockdep_set_subclass(&pool->lock, 1); /* see put_pwq() */
Tejun Heo29c91e92013-03-12 11:30:03 -07003356 copy_workqueue_attrs(pool->attrs, attrs);
3357
3358 if (worker_pool_assign_id(pool) < 0)
3359 goto fail;
3360
3361 /* create and start the initial worker */
3362 worker = create_worker(pool);
3363 if (!worker)
3364 goto fail;
3365
3366 spin_lock_irq(&pool->lock);
3367 start_worker(worker);
3368 spin_unlock_irq(&pool->lock);
3369
3370 /* install */
3371 spin_lock_irq(&workqueue_lock);
3372 hash_add(unbound_pool_hash, &pool->hash_node, hash);
3373out_unlock:
3374 spin_unlock_irq(&workqueue_lock);
3375 mutex_unlock(&create_mutex);
3376 return pool;
3377fail:
3378 mutex_unlock(&create_mutex);
3379 if (pool)
3380 put_unbound_pool(pool);
3381 return NULL;
3382}
3383
Tejun Heo8864b4e2013-03-12 11:30:04 -07003384static void rcu_free_pwq(struct rcu_head *rcu)
3385{
3386 kmem_cache_free(pwq_cache,
3387 container_of(rcu, struct pool_workqueue, rcu));
3388}
3389
3390/*
3391 * Scheduled on system_wq by put_pwq() when an unbound pwq hits zero refcnt
3392 * and needs to be destroyed.
3393 */
3394static void pwq_unbound_release_workfn(struct work_struct *work)
3395{
3396 struct pool_workqueue *pwq = container_of(work, struct pool_workqueue,
3397 unbound_release_work);
3398 struct workqueue_struct *wq = pwq->wq;
3399 struct worker_pool *pool = pwq->pool;
3400
3401 if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)))
3402 return;
3403
Tejun Heo75ccf592013-03-12 11:30:04 -07003404 /*
3405 * Unlink @pwq. Synchronization against flush_mutex isn't strictly
3406 * necessary on release but do it anyway. It's easier to verify
3407 * and consistent with the linking path.
3408 */
3409 mutex_lock(&wq->flush_mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003410 spin_lock_irq(&workqueue_lock);
3411 list_del_rcu(&pwq->pwqs_node);
3412 spin_unlock_irq(&workqueue_lock);
Tejun Heo75ccf592013-03-12 11:30:04 -07003413 mutex_unlock(&wq->flush_mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003414
3415 put_unbound_pool(pool);
3416 call_rcu_sched(&pwq->rcu, rcu_free_pwq);
3417
3418 /*
3419 * If we're the last pwq going away, @wq is already dead and no one
3420 * is gonna access it anymore. Free it.
3421 */
3422 if (list_empty(&wq->pwqs))
3423 kfree(wq);
3424}
3425
Tejun Heod2c1d402013-03-12 11:30:04 -07003426static void init_and_link_pwq(struct pool_workqueue *pwq,
3427 struct workqueue_struct *wq,
3428 struct worker_pool *pool)
3429{
3430 BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK);
3431
3432 pwq->pool = pool;
3433 pwq->wq = wq;
3434 pwq->flush_color = -1;
Tejun Heo8864b4e2013-03-12 11:30:04 -07003435 pwq->refcnt = 1;
Tejun Heod2c1d402013-03-12 11:30:04 -07003436 pwq->max_active = wq->saved_max_active;
3437 INIT_LIST_HEAD(&pwq->delayed_works);
3438 INIT_LIST_HEAD(&pwq->mayday_node);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003439 INIT_WORK(&pwq->unbound_release_work, pwq_unbound_release_workfn);
Tejun Heod2c1d402013-03-12 11:30:04 -07003440
Tejun Heo75ccf592013-03-12 11:30:04 -07003441 /*
3442 * Link @pwq and set the matching work_color. This is synchronized
3443 * with flush_mutex to avoid confusing flush_workqueue().
3444 */
3445 mutex_lock(&wq->flush_mutex);
3446 spin_lock_irq(&workqueue_lock);
3447
3448 pwq->work_color = wq->work_color;
Tejun Heod2c1d402013-03-12 11:30:04 -07003449 list_add_tail_rcu(&pwq->pwqs_node, &wq->pwqs);
Tejun Heo75ccf592013-03-12 11:30:04 -07003450
3451 spin_unlock_irq(&workqueue_lock);
3452 mutex_unlock(&wq->flush_mutex);
Tejun Heod2c1d402013-03-12 11:30:04 -07003453}
3454
Tejun Heo30cdf242013-03-12 11:29:57 -07003455static int alloc_and_link_pwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003457 bool highpri = wq->flags & WQ_HIGHPRI;
Tejun Heo30cdf242013-03-12 11:29:57 -07003458 int cpu;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01003459
Tejun Heo30cdf242013-03-12 11:29:57 -07003460 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo420c0dd2013-03-12 11:29:59 -07003461 wq->cpu_pwqs = alloc_percpu(struct pool_workqueue);
3462 if (!wq->cpu_pwqs)
Tejun Heo30cdf242013-03-12 11:29:57 -07003463 return -ENOMEM;
3464
3465 for_each_possible_cpu(cpu) {
Tejun Heo7fb98ea2013-03-12 11:30:00 -07003466 struct pool_workqueue *pwq =
3467 per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heo7a62c2c2013-03-12 11:30:03 -07003468 struct worker_pool *cpu_pools =
Tejun Heof02ae732013-03-12 11:30:03 -07003469 per_cpu(cpu_worker_pools, cpu);
Tejun Heo30cdf242013-03-12 11:29:57 -07003470
Tejun Heod2c1d402013-03-12 11:30:04 -07003471 init_and_link_pwq(pwq, wq, &cpu_pools[highpri]);
Tejun Heo30cdf242013-03-12 11:29:57 -07003472 }
3473 } else {
3474 struct pool_workqueue *pwq;
Tejun Heod2c1d402013-03-12 11:30:04 -07003475 struct worker_pool *pool;
Tejun Heo30cdf242013-03-12 11:29:57 -07003476
3477 pwq = kmem_cache_zalloc(pwq_cache, GFP_KERNEL);
3478 if (!pwq)
3479 return -ENOMEM;
3480
Tejun Heod2c1d402013-03-12 11:30:04 -07003481 pool = get_unbound_pool(unbound_std_wq_attrs[highpri]);
3482 if (!pool) {
Tejun Heo29c91e92013-03-12 11:30:03 -07003483 kmem_cache_free(pwq_cache, pwq);
3484 return -ENOMEM;
3485 }
3486
Tejun Heod2c1d402013-03-12 11:30:04 -07003487 init_and_link_pwq(pwq, wq, pool);
Tejun Heo30cdf242013-03-12 11:29:57 -07003488 }
3489
3490 return 0;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003491}
3492
Tejun Heof3421792010-07-02 10:03:51 +02003493static int wq_clamp_max_active(int max_active, unsigned int flags,
3494 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003495{
Tejun Heof3421792010-07-02 10:03:51 +02003496 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3497
3498 if (max_active < 1 || max_active > lim)
Valentin Ilie044c7822012-08-19 00:52:42 +03003499 pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
3500 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003501
Tejun Heof3421792010-07-02 10:03:51 +02003502 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003503}
3504
Tejun Heob196be82012-01-10 15:11:35 -08003505struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02003506 unsigned int flags,
3507 int max_active,
3508 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08003509 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003510{
Tejun Heob196be82012-01-10 15:11:35 -08003511 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003512 struct workqueue_struct *wq;
Tejun Heo49e3cf42013-03-12 11:29:58 -07003513 struct pool_workqueue *pwq;
Tejun Heob196be82012-01-10 15:11:35 -08003514 size_t namelen;
3515
3516 /* determine namelen, allocate wq and format name */
3517 va_start(args, lock_name);
3518 va_copy(args1, args);
3519 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3520
3521 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3522 if (!wq)
Tejun Heod2c1d402013-03-12 11:30:04 -07003523 return NULL;
Tejun Heob196be82012-01-10 15:11:35 -08003524
3525 vsnprintf(wq->name, namelen, fmt, args1);
3526 va_end(args);
3527 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003528
Tejun Heod320c032010-06-29 10:07:14 +02003529 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003530 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003531
Tejun Heob196be82012-01-10 15:11:35 -08003532 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003533 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003534 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003535 mutex_init(&wq->flush_mutex);
Tejun Heo112202d2013-02-13 19:29:12 -08003536 atomic_set(&wq->nr_pwqs_to_flush, 0);
Tejun Heo30cdf242013-03-12 11:29:57 -07003537 INIT_LIST_HEAD(&wq->pwqs);
Tejun Heo73f53c42010-06-29 10:07:11 +02003538 INIT_LIST_HEAD(&wq->flusher_queue);
3539 INIT_LIST_HEAD(&wq->flusher_overflow);
Tejun Heo493a1722013-03-12 11:29:59 -07003540 INIT_LIST_HEAD(&wq->maydays);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003541
Johannes Bergeb13ba82008-01-16 09:51:58 +01003542 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003543 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003544
Tejun Heo30cdf242013-03-12 11:29:57 -07003545 if (alloc_and_link_pwqs(wq) < 0)
Tejun Heod2c1d402013-03-12 11:30:04 -07003546 goto err_free_wq;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003547
Tejun Heo493008a2013-03-12 11:30:03 -07003548 /*
3549 * Workqueues which may be used during memory reclaim should
3550 * have a rescuer to guarantee forward progress.
3551 */
3552 if (flags & WQ_MEM_RECLAIM) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003553 struct worker *rescuer;
3554
Tejun Heod2c1d402013-03-12 11:30:04 -07003555 rescuer = alloc_worker();
Tejun Heoe22bee72010-06-29 10:07:14 +02003556 if (!rescuer)
Tejun Heod2c1d402013-03-12 11:30:04 -07003557 goto err_destroy;
Tejun Heoe22bee72010-06-29 10:07:14 +02003558
Tejun Heo111c2252013-01-17 17:16:24 -08003559 rescuer->rescue_wq = wq;
3560 rescuer->task = kthread_create(rescuer_thread, rescuer, "%s",
Tejun Heob196be82012-01-10 15:11:35 -08003561 wq->name);
Tejun Heod2c1d402013-03-12 11:30:04 -07003562 if (IS_ERR(rescuer->task)) {
3563 kfree(rescuer);
3564 goto err_destroy;
3565 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003566
Tejun Heod2c1d402013-03-12 11:30:04 -07003567 wq->rescuer = rescuer;
Tejun Heoe22bee72010-06-29 10:07:14 +02003568 rescuer->task->flags |= PF_THREAD_BOUND;
3569 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003570 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003571
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003572 /*
3573 * workqueue_lock protects global freeze state and workqueues
3574 * list. Grab it, set max_active accordingly and add the new
3575 * workqueue to workqueues list.
3576 */
Tejun Heoe98d5b12013-03-12 11:29:57 -07003577 spin_lock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003578
Tejun Heo58a69cb2011-02-16 09:25:31 +01003579 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heo49e3cf42013-03-12 11:29:58 -07003580 for_each_pwq(pwq, wq)
3581 pwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003582
Tejun Heo15376632010-06-29 10:07:11 +02003583 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003584
Tejun Heoe98d5b12013-03-12 11:29:57 -07003585 spin_unlock_irq(&workqueue_lock);
Tejun Heo15376632010-06-29 10:07:11 +02003586
Oleg Nesterov3af244332007-05-09 02:34:09 -07003587 return wq;
Tejun Heod2c1d402013-03-12 11:30:04 -07003588
3589err_free_wq:
3590 kfree(wq);
3591 return NULL;
3592err_destroy:
3593 destroy_workqueue(wq);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003594 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003595}
Tejun Heod320c032010-06-29 10:07:14 +02003596EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003597
3598/**
3599 * destroy_workqueue - safely terminate a workqueue
3600 * @wq: target workqueue
3601 *
3602 * Safely destroy a workqueue. All work currently pending will be done first.
3603 */
3604void destroy_workqueue(struct workqueue_struct *wq)
3605{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003606 struct pool_workqueue *pwq;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003607
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003608 /* drain it before proceeding with destruction */
3609 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003610
Tejun Heo76af4d92013-03-12 11:30:00 -07003611 spin_lock_irq(&workqueue_lock);
3612
Tejun Heo6183c002013-03-12 11:29:57 -07003613 /* sanity checks */
Tejun Heo49e3cf42013-03-12 11:29:58 -07003614 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07003615 int i;
3616
Tejun Heo76af4d92013-03-12 11:30:00 -07003617 for (i = 0; i < WORK_NR_COLORS; i++) {
3618 if (WARN_ON(pwq->nr_in_flight[i])) {
3619 spin_unlock_irq(&workqueue_lock);
Tejun Heo6183c002013-03-12 11:29:57 -07003620 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07003621 }
3622 }
3623
Tejun Heo8864b4e2013-03-12 11:30:04 -07003624 if (WARN_ON(pwq->refcnt > 1) ||
3625 WARN_ON(pwq->nr_active) ||
Tejun Heo76af4d92013-03-12 11:30:00 -07003626 WARN_ON(!list_empty(&pwq->delayed_works))) {
3627 spin_unlock_irq(&workqueue_lock);
Tejun Heo6183c002013-03-12 11:29:57 -07003628 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07003629 }
Tejun Heo6183c002013-03-12 11:29:57 -07003630 }
3631
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003632 /*
3633 * wq list is used to freeze wq, remove from list after
3634 * flushing is complete in case freeze races us.
3635 */
Tejun Heod2c1d402013-03-12 11:30:04 -07003636 list_del_init(&wq->list);
Tejun Heo76af4d92013-03-12 11:30:00 -07003637
Tejun Heoe98d5b12013-03-12 11:29:57 -07003638 spin_unlock_irq(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003639
Tejun Heo493008a2013-03-12 11:30:03 -07003640 if (wq->rescuer) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003641 kthread_stop(wq->rescuer->task);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003642 kfree(wq->rescuer);
Tejun Heo493008a2013-03-12 11:30:03 -07003643 wq->rescuer = NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +02003644 }
3645
Tejun Heo8864b4e2013-03-12 11:30:04 -07003646 if (!(wq->flags & WQ_UNBOUND)) {
3647 /*
3648 * The base ref is never dropped on per-cpu pwqs. Directly
3649 * free the pwqs and wq.
3650 */
3651 free_percpu(wq->cpu_pwqs);
3652 kfree(wq);
3653 } else {
3654 /*
3655 * We're the sole accessor of @wq at this point. Directly
3656 * access the first pwq and put the base ref. As both pwqs
3657 * and pools are sched-RCU protected, the lock operations
3658 * are safe. @wq will be freed when the last pwq is
3659 * released.
3660 */
Tejun Heo29c91e92013-03-12 11:30:03 -07003661 pwq = list_first_entry(&wq->pwqs, struct pool_workqueue,
3662 pwqs_node);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003663 spin_lock_irq(&pwq->pool->lock);
3664 put_pwq(pwq);
3665 spin_unlock_irq(&pwq->pool->lock);
Tejun Heo29c91e92013-03-12 11:30:03 -07003666 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003667}
3668EXPORT_SYMBOL_GPL(destroy_workqueue);
3669
Tejun Heodcd989c2010-06-29 10:07:14 +02003670/**
Tejun Heo112202d2013-02-13 19:29:12 -08003671 * pwq_set_max_active - adjust max_active of a pwq
3672 * @pwq: target pool_workqueue
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003673 * @max_active: new max_active value.
3674 *
Tejun Heo112202d2013-02-13 19:29:12 -08003675 * Set @pwq->max_active to @max_active and activate delayed works if
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003676 * increased.
3677 *
3678 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08003679 * spin_lock_irq(pool->lock).
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003680 */
Tejun Heo112202d2013-02-13 19:29:12 -08003681static void pwq_set_max_active(struct pool_workqueue *pwq, int max_active)
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003682{
Tejun Heo112202d2013-02-13 19:29:12 -08003683 pwq->max_active = max_active;
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003684
Tejun Heo112202d2013-02-13 19:29:12 -08003685 while (!list_empty(&pwq->delayed_works) &&
3686 pwq->nr_active < pwq->max_active)
3687 pwq_activate_first_delayed(pwq);
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003688}
3689
3690/**
Tejun Heodcd989c2010-06-29 10:07:14 +02003691 * workqueue_set_max_active - adjust max_active of a workqueue
3692 * @wq: target workqueue
3693 * @max_active: new max_active value.
3694 *
3695 * Set max_active of @wq to @max_active.
3696 *
3697 * CONTEXT:
3698 * Don't call from IRQ context.
3699 */
3700void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3701{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003702 struct pool_workqueue *pwq;
Tejun Heodcd989c2010-06-29 10:07:14 +02003703
Tejun Heof3421792010-07-02 10:03:51 +02003704 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003705
Tejun Heoe98d5b12013-03-12 11:29:57 -07003706 spin_lock_irq(&workqueue_lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02003707
3708 wq->saved_max_active = max_active;
3709
Tejun Heo49e3cf42013-03-12 11:29:58 -07003710 for_each_pwq(pwq, wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08003711 struct worker_pool *pool = pwq->pool;
Tejun Heodcd989c2010-06-29 10:07:14 +02003712
Tejun Heoe98d5b12013-03-12 11:29:57 -07003713 spin_lock(&pool->lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02003714
Tejun Heo58a69cb2011-02-16 09:25:31 +01003715 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heo35b6bb62013-01-24 11:01:33 -08003716 !(pool->flags & POOL_FREEZING))
Tejun Heo112202d2013-02-13 19:29:12 -08003717 pwq_set_max_active(pwq, max_active);
Tejun Heodcd989c2010-06-29 10:07:14 +02003718
Tejun Heoe98d5b12013-03-12 11:29:57 -07003719 spin_unlock(&pool->lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02003720 }
3721
Tejun Heoe98d5b12013-03-12 11:29:57 -07003722 spin_unlock_irq(&workqueue_lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02003723}
3724EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3725
3726/**
3727 * workqueue_congested - test whether a workqueue is congested
3728 * @cpu: CPU in question
3729 * @wq: target workqueue
3730 *
3731 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3732 * no synchronization around this function and the test result is
3733 * unreliable and only useful as advisory hints or for debugging.
3734 *
3735 * RETURNS:
3736 * %true if congested, %false otherwise.
3737 */
Tejun Heod84ff052013-03-12 11:29:59 -07003738bool workqueue_congested(int cpu, struct workqueue_struct *wq)
Tejun Heodcd989c2010-06-29 10:07:14 +02003739{
Tejun Heo7fb98ea2013-03-12 11:30:00 -07003740 struct pool_workqueue *pwq;
Tejun Heo76af4d92013-03-12 11:30:00 -07003741 bool ret;
3742
3743 preempt_disable();
Tejun Heo7fb98ea2013-03-12 11:30:00 -07003744
3745 if (!(wq->flags & WQ_UNBOUND))
3746 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
3747 else
3748 pwq = first_pwq(wq);
Tejun Heodcd989c2010-06-29 10:07:14 +02003749
Tejun Heo76af4d92013-03-12 11:30:00 -07003750 ret = !list_empty(&pwq->delayed_works);
3751 preempt_enable();
3752
3753 return ret;
Tejun Heodcd989c2010-06-29 10:07:14 +02003754}
3755EXPORT_SYMBOL_GPL(workqueue_congested);
3756
3757/**
Tejun Heodcd989c2010-06-29 10:07:14 +02003758 * work_busy - test whether a work is currently pending or running
3759 * @work: the work to be tested
3760 *
3761 * Test whether @work is currently pending or running. There is no
3762 * synchronization around this function and the test result is
3763 * unreliable and only useful as advisory hints or for debugging.
Tejun Heodcd989c2010-06-29 10:07:14 +02003764 *
3765 * RETURNS:
3766 * OR'd bitmask of WORK_BUSY_* bits.
3767 */
3768unsigned int work_busy(struct work_struct *work)
3769{
Tejun Heofa1b54e2013-03-12 11:30:00 -07003770 struct worker_pool *pool;
Tejun Heodcd989c2010-06-29 10:07:14 +02003771 unsigned long flags;
3772 unsigned int ret = 0;
3773
Tejun Heodcd989c2010-06-29 10:07:14 +02003774 if (work_pending(work))
3775 ret |= WORK_BUSY_PENDING;
Tejun Heodcd989c2010-06-29 10:07:14 +02003776
Tejun Heofa1b54e2013-03-12 11:30:00 -07003777 local_irq_save(flags);
3778 pool = get_work_pool(work);
Lai Jiangshan038366c2013-02-06 18:04:53 -08003779 if (pool) {
Tejun Heofa1b54e2013-03-12 11:30:00 -07003780 spin_lock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08003781 if (find_worker_executing_work(pool, work))
3782 ret |= WORK_BUSY_RUNNING;
Tejun Heofa1b54e2013-03-12 11:30:00 -07003783 spin_unlock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08003784 }
Tejun Heofa1b54e2013-03-12 11:30:00 -07003785 local_irq_restore(flags);
Tejun Heodcd989c2010-06-29 10:07:14 +02003786
3787 return ret;
3788}
3789EXPORT_SYMBOL_GPL(work_busy);
3790
Tejun Heodb7bccf2010-06-29 10:07:12 +02003791/*
3792 * CPU hotplug.
3793 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003794 * There are two challenges in supporting CPU hotplug. Firstly, there
Tejun Heo112202d2013-02-13 19:29:12 -08003795 * are a lot of assumptions on strong associations among work, pwq and
Tejun Heo706026c2013-01-24 11:01:34 -08003796 * pool which make migrating pending and scheduled works very
Tejun Heoe22bee72010-06-29 10:07:14 +02003797 * difficult to implement without impacting hot paths. Secondly,
Tejun Heo94cf58b2013-01-24 11:01:33 -08003798 * worker pools serve mix of short, long and very long running works making
Tejun Heoe22bee72010-06-29 10:07:14 +02003799 * blocked draining impractical.
3800 *
Tejun Heo24647572013-01-24 11:01:33 -08003801 * This is solved by allowing the pools to be disassociated from the CPU
Tejun Heo628c78e2012-07-17 12:39:27 -07003802 * running as an unbound one and allowing it to be reattached later if the
3803 * cpu comes back online.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003804 */
3805
Tejun Heo706026c2013-01-24 11:01:34 -08003806static void wq_unbind_fn(struct work_struct *work)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003807{
Tejun Heo38db41d2013-01-24 11:01:34 -08003808 int cpu = smp_processor_id();
Tejun Heo4ce62e92012-07-13 22:16:44 -07003809 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003810 struct worker *worker;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003811 int i;
3812
Tejun Heof02ae732013-03-12 11:30:03 -07003813 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo6183c002013-03-12 11:29:57 -07003814 WARN_ON_ONCE(cpu != smp_processor_id());
Tejun Heo94cf58b2013-01-24 11:01:33 -08003815
3816 mutex_lock(&pool->assoc_mutex);
3817 spin_lock_irq(&pool->lock);
3818
3819 /*
3820 * We've claimed all manager positions. Make all workers
3821 * unbound and set DISASSOCIATED. Before this, all workers
3822 * except for the ones which are still executing works from
3823 * before the last CPU down must be on the cpu. After
3824 * this, they may become diasporas.
3825 */
Tejun Heo4ce62e92012-07-13 22:16:44 -07003826 list_for_each_entry(worker, &pool->idle_list, entry)
Tejun Heo403c8212012-07-17 12:39:27 -07003827 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003828
Sasha Levinb67bfe02013-02-27 17:06:00 -08003829 for_each_busy_worker(worker, i, pool)
Tejun Heoc9e7cf22013-01-24 11:01:33 -08003830 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003831
Tejun Heo24647572013-01-24 11:01:33 -08003832 pool->flags |= POOL_DISASSOCIATED;
Tejun Heof2d5a0e2012-07-17 12:39:26 -07003833
Tejun Heo94cf58b2013-01-24 11:01:33 -08003834 spin_unlock_irq(&pool->lock);
3835 mutex_unlock(&pool->assoc_mutex);
3836 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003837
3838 /*
Tejun Heo628c78e2012-07-17 12:39:27 -07003839 * Call schedule() so that we cross rq->lock and thus can guarantee
3840 * sched callbacks see the %WORKER_UNBOUND flag. This is necessary
3841 * as scheduler callbacks may be invoked from other cpus.
3842 */
3843 schedule();
3844
3845 /*
3846 * Sched callbacks are disabled now. Zap nr_running. After this,
3847 * nr_running stays zero and need_more_worker() and keep_working()
Tejun Heo38db41d2013-01-24 11:01:34 -08003848 * are always true as long as the worklist is not empty. Pools on
3849 * @cpu now behave as unbound (in terms of concurrency management)
3850 * pools which are served by workers tied to the CPU.
Tejun Heo628c78e2012-07-17 12:39:27 -07003851 *
3852 * On return from this function, the current worker would trigger
3853 * unbound chain execution of pending work items if other workers
3854 * didn't already.
Tejun Heoe22bee72010-06-29 10:07:14 +02003855 */
Tejun Heof02ae732013-03-12 11:30:03 -07003856 for_each_cpu_worker_pool(pool, cpu)
Tejun Heoe19e3972013-01-24 11:39:44 -08003857 atomic_set(&pool->nr_running, 0);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003858}
3859
Tejun Heo8db25e72012-07-17 12:39:28 -07003860/*
3861 * Workqueues should be brought up before normal priority CPU notifiers.
3862 * This will be registered high priority CPU notifier.
3863 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07003864static int __cpuinit workqueue_cpu_up_callback(struct notifier_block *nfb,
Tejun Heo8db25e72012-07-17 12:39:28 -07003865 unsigned long action,
3866 void *hcpu)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003867{
Tejun Heod84ff052013-03-12 11:29:59 -07003868 int cpu = (unsigned long)hcpu;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003869 struct worker_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870
Tejun Heo8db25e72012-07-17 12:39:28 -07003871 switch (action & ~CPU_TASKS_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 case CPU_UP_PREPARE:
Tejun Heof02ae732013-03-12 11:30:03 -07003873 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo3ce63372012-07-17 12:39:27 -07003874 struct worker *worker;
3875
3876 if (pool->nr_workers)
3877 continue;
3878
3879 worker = create_worker(pool);
3880 if (!worker)
3881 return NOTIFY_BAD;
3882
Tejun Heod565ed62013-01-24 11:01:33 -08003883 spin_lock_irq(&pool->lock);
Tejun Heo3ce63372012-07-17 12:39:27 -07003884 start_worker(worker);
Tejun Heod565ed62013-01-24 11:01:33 -08003885 spin_unlock_irq(&pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003887 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003888
Tejun Heo65758202012-07-17 12:39:26 -07003889 case CPU_DOWN_FAILED:
3890 case CPU_ONLINE:
Tejun Heof02ae732013-03-12 11:30:03 -07003891 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo94cf58b2013-01-24 11:01:33 -08003892 mutex_lock(&pool->assoc_mutex);
3893 spin_lock_irq(&pool->lock);
3894
Tejun Heo24647572013-01-24 11:01:33 -08003895 pool->flags &= ~POOL_DISASSOCIATED;
Tejun Heo94cf58b2013-01-24 11:01:33 -08003896 rebind_workers(pool);
3897
3898 spin_unlock_irq(&pool->lock);
3899 mutex_unlock(&pool->assoc_mutex);
3900 }
Tejun Heo8db25e72012-07-17 12:39:28 -07003901 break;
Tejun Heo65758202012-07-17 12:39:26 -07003902 }
3903 return NOTIFY_OK;
3904}
3905
3906/*
3907 * Workqueues should be brought down after normal priority CPU notifiers.
3908 * This will be registered as low priority CPU notifier.
3909 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07003910static int __cpuinit workqueue_cpu_down_callback(struct notifier_block *nfb,
Tejun Heo65758202012-07-17 12:39:26 -07003911 unsigned long action,
3912 void *hcpu)
3913{
Tejun Heod84ff052013-03-12 11:29:59 -07003914 int cpu = (unsigned long)hcpu;
Tejun Heo8db25e72012-07-17 12:39:28 -07003915 struct work_struct unbind_work;
3916
Tejun Heo65758202012-07-17 12:39:26 -07003917 switch (action & ~CPU_TASKS_FROZEN) {
3918 case CPU_DOWN_PREPARE:
Tejun Heo8db25e72012-07-17 12:39:28 -07003919 /* unbinding should happen on the local CPU */
Tejun Heo706026c2013-01-24 11:01:34 -08003920 INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn);
Joonsoo Kim7635d2f2012-08-15 23:25:41 +09003921 queue_work_on(cpu, system_highpri_wq, &unbind_work);
Tejun Heo8db25e72012-07-17 12:39:28 -07003922 flush_work(&unbind_work);
3923 break;
Tejun Heo65758202012-07-17 12:39:26 -07003924 }
3925 return NOTIFY_OK;
3926}
3927
Rusty Russell2d3854a2008-11-05 13:39:10 +11003928#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003929
Rusty Russell2d3854a2008-11-05 13:39:10 +11003930struct work_for_cpu {
Tejun Heoed48ece2012-09-18 12:48:43 -07003931 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003932 long (*fn)(void *);
3933 void *arg;
3934 long ret;
3935};
3936
Tejun Heoed48ece2012-09-18 12:48:43 -07003937static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003938{
Tejun Heoed48ece2012-09-18 12:48:43 -07003939 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
3940
Rusty Russell2d3854a2008-11-05 13:39:10 +11003941 wfc->ret = wfc->fn(wfc->arg);
3942}
3943
3944/**
3945 * work_on_cpu - run a function in user context on a particular cpu
3946 * @cpu: the cpu to run on
3947 * @fn: the function to run
3948 * @arg: the function arg
3949 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003950 * This will return the value @fn returns.
3951 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06003952 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003953 */
Tejun Heod84ff052013-03-12 11:29:59 -07003954long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003955{
Tejun Heoed48ece2012-09-18 12:48:43 -07003956 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003957
Tejun Heoed48ece2012-09-18 12:48:43 -07003958 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
3959 schedule_work_on(cpu, &wfc.work);
3960 flush_work(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003961 return wfc.ret;
3962}
3963EXPORT_SYMBOL_GPL(work_on_cpu);
3964#endif /* CONFIG_SMP */
3965
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003966#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303967
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003968/**
3969 * freeze_workqueues_begin - begin freezing workqueues
3970 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003971 * Start freezing workqueues. After this function returns, all freezable
3972 * workqueues will queue new works to their frozen_works list instead of
Tejun Heo706026c2013-01-24 11:01:34 -08003973 * pool->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003974 *
3975 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08003976 * Grabs and releases workqueue_lock and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003977 */
3978void freeze_workqueues_begin(void)
3979{
Tejun Heo17116962013-03-12 11:29:58 -07003980 struct worker_pool *pool;
Tejun Heo24b8a842013-03-12 11:29:58 -07003981 struct workqueue_struct *wq;
3982 struct pool_workqueue *pwq;
Tejun Heo17116962013-03-12 11:29:58 -07003983 int id;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003984
Tejun Heoe98d5b12013-03-12 11:29:57 -07003985 spin_lock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003986
Tejun Heo6183c002013-03-12 11:29:57 -07003987 WARN_ON_ONCE(workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003988 workqueue_freezing = true;
3989
Tejun Heo24b8a842013-03-12 11:29:58 -07003990 /* set FREEZING */
Tejun Heo17116962013-03-12 11:29:58 -07003991 for_each_pool(pool, id) {
Tejun Heo17116962013-03-12 11:29:58 -07003992 spin_lock(&pool->lock);
Tejun Heo17116962013-03-12 11:29:58 -07003993 WARN_ON_ONCE(pool->flags & POOL_FREEZING);
3994 pool->flags |= POOL_FREEZING;
Tejun Heo17116962013-03-12 11:29:58 -07003995 spin_unlock(&pool->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003996 }
3997
Tejun Heo24b8a842013-03-12 11:29:58 -07003998 /* suppress further executions by setting max_active to zero */
3999 list_for_each_entry(wq, &workqueues, list) {
4000 if (!(wq->flags & WQ_FREEZABLE))
4001 continue;
4002
4003 for_each_pwq(pwq, wq) {
4004 spin_lock(&pwq->pool->lock);
4005 pwq->max_active = 0;
4006 spin_unlock(&pwq->pool->lock);
4007 }
4008 }
4009
Tejun Heoe98d5b12013-03-12 11:29:57 -07004010 spin_unlock_irq(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004012
4013/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01004014 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004015 *
4016 * Check whether freezing is complete. This function must be called
4017 * between freeze_workqueues_begin() and thaw_workqueues().
4018 *
4019 * CONTEXT:
4020 * Grabs and releases workqueue_lock.
4021 *
4022 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01004023 * %true if some freezable workqueues are still busy. %false if freezing
4024 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004025 */
4026bool freeze_workqueues_busy(void)
4027{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004028 bool busy = false;
Tejun Heo24b8a842013-03-12 11:29:58 -07004029 struct workqueue_struct *wq;
4030 struct pool_workqueue *pwq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004031
Tejun Heoe98d5b12013-03-12 11:29:57 -07004032 spin_lock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004033
Tejun Heo6183c002013-03-12 11:29:57 -07004034 WARN_ON_ONCE(!workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004035
Tejun Heo24b8a842013-03-12 11:29:58 -07004036 list_for_each_entry(wq, &workqueues, list) {
4037 if (!(wq->flags & WQ_FREEZABLE))
4038 continue;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004039 /*
4040 * nr_active is monotonically decreasing. It's safe
4041 * to peek without lock.
4042 */
Tejun Heo24b8a842013-03-12 11:29:58 -07004043 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07004044 WARN_ON_ONCE(pwq->nr_active < 0);
Tejun Heo112202d2013-02-13 19:29:12 -08004045 if (pwq->nr_active) {
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004046 busy = true;
4047 goto out_unlock;
4048 }
4049 }
4050 }
4051out_unlock:
Tejun Heoe98d5b12013-03-12 11:29:57 -07004052 spin_unlock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004053 return busy;
4054}
4055
4056/**
4057 * thaw_workqueues - thaw workqueues
4058 *
4059 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo706026c2013-01-24 11:01:34 -08004060 * frozen works are transferred to their respective pool worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004061 *
4062 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08004063 * Grabs and releases workqueue_lock and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004064 */
4065void thaw_workqueues(void)
4066{
Tejun Heo24b8a842013-03-12 11:29:58 -07004067 struct workqueue_struct *wq;
4068 struct pool_workqueue *pwq;
4069 struct worker_pool *pool;
4070 int id;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004071
Tejun Heoe98d5b12013-03-12 11:29:57 -07004072 spin_lock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004073
4074 if (!workqueue_freezing)
4075 goto out_unlock;
4076
Tejun Heo24b8a842013-03-12 11:29:58 -07004077 /* clear FREEZING */
4078 for_each_pool(pool, id) {
4079 spin_lock(&pool->lock);
4080 WARN_ON_ONCE(!(pool->flags & POOL_FREEZING));
4081 pool->flags &= ~POOL_FREEZING;
4082 spin_unlock(&pool->lock);
4083 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02004084
Tejun Heo24b8a842013-03-12 11:29:58 -07004085 /* restore max_active and repopulate worklist */
4086 list_for_each_entry(wq, &workqueues, list) {
4087 if (!(wq->flags & WQ_FREEZABLE))
4088 continue;
Tejun Heod565ed62013-01-24 11:01:33 -08004089
Tejun Heo24b8a842013-03-12 11:29:58 -07004090 for_each_pwq(pwq, wq) {
4091 spin_lock(&pwq->pool->lock);
4092 pwq_set_max_active(pwq, wq->saved_max_active);
4093 spin_unlock(&pwq->pool->lock);
Tejun Heod565ed62013-01-24 11:01:33 -08004094 }
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004095 }
4096
Tejun Heo24b8a842013-03-12 11:29:58 -07004097 /* kick workers */
4098 for_each_pool(pool, id) {
4099 spin_lock(&pool->lock);
4100 wake_up_worker(pool);
4101 spin_unlock(&pool->lock);
4102 }
4103
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004104 workqueue_freezing = false;
4105out_unlock:
Tejun Heoe98d5b12013-03-12 11:29:57 -07004106 spin_unlock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004107}
4108#endif /* CONFIG_FREEZER */
4109
Suresh Siddha6ee05782010-07-30 14:57:37 -07004110static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111{
Tejun Heo7a4e3442013-03-12 11:30:00 -07004112 int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL };
4113 int i, cpu;
Tejun Heoc34056a2010-06-29 10:07:11 +02004114
Tejun Heo7c3eed52013-01-24 11:01:33 -08004115 /* make sure we have enough bits for OFFQ pool ID */
4116 BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) <
Lai Jiangshan6be19582013-02-06 18:04:53 -08004117 WORK_CPU_END * NR_STD_WORKER_POOLS);
Tejun Heob5490072012-08-03 10:30:46 -07004118
Tejun Heoe904e6c2013-03-12 11:29:57 -07004119 WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long));
4120
4121 pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC);
4122
Tejun Heo65758202012-07-17 12:39:26 -07004123 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
Lai Jiangshana5b4e572012-09-18 09:59:23 -07004124 hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02004125
Tejun Heo706026c2013-01-24 11:01:34 -08004126 /* initialize CPU pools */
Tejun Heo29c91e92013-03-12 11:30:03 -07004127 for_each_possible_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07004128 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02004129
Tejun Heo7a4e3442013-03-12 11:30:00 -07004130 i = 0;
Tejun Heof02ae732013-03-12 11:30:03 -07004131 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo7a4e3442013-03-12 11:30:00 -07004132 BUG_ON(init_worker_pool(pool));
Tejun Heoec22ca52013-01-24 11:01:33 -08004133 pool->cpu = cpu;
Tejun Heo29c91e92013-03-12 11:30:03 -07004134 cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu));
Tejun Heo7a4e3442013-03-12 11:30:00 -07004135 pool->attrs->nice = std_nice[i++];
4136
Tejun Heo9daf9e62013-01-24 11:01:33 -08004137 /* alloc pool ID */
4138 BUG_ON(worker_pool_assign_id(pool));
Tejun Heo4ce62e92012-07-13 22:16:44 -07004139 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02004140 }
4141
Tejun Heoe22bee72010-06-29 10:07:14 +02004142 /* create the initial worker */
Tejun Heo29c91e92013-03-12 11:30:03 -07004143 for_each_online_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07004144 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02004145
Tejun Heof02ae732013-03-12 11:30:03 -07004146 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07004147 struct worker *worker;
4148
Tejun Heo29c91e92013-03-12 11:30:03 -07004149 pool->flags &= ~POOL_DISASSOCIATED;
Tejun Heo24647572013-01-24 11:01:33 -08004150
Tejun Heobc2ae0f2012-07-17 12:39:27 -07004151 worker = create_worker(pool);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004152 BUG_ON(!worker);
Tejun Heod565ed62013-01-24 11:01:33 -08004153 spin_lock_irq(&pool->lock);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004154 start_worker(worker);
Tejun Heod565ed62013-01-24 11:01:33 -08004155 spin_unlock_irq(&pool->lock);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004156 }
Tejun Heoe22bee72010-06-29 10:07:14 +02004157 }
4158
Tejun Heo29c91e92013-03-12 11:30:03 -07004159 /* create default unbound wq attrs */
4160 for (i = 0; i < NR_STD_WORKER_POOLS; i++) {
4161 struct workqueue_attrs *attrs;
4162
4163 BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL)));
4164
4165 attrs->nice = std_nice[i];
4166 cpumask_setall(attrs->cpumask);
4167
4168 unbound_std_wq_attrs[i] = attrs;
4169 }
4170
Tejun Heod320c032010-06-29 10:07:14 +02004171 system_wq = alloc_workqueue("events", 0, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09004172 system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
Tejun Heod320c032010-06-29 10:07:14 +02004173 system_long_wq = alloc_workqueue("events_long", 0, 0);
Tejun Heof3421792010-07-02 10:03:51 +02004174 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
4175 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01004176 system_freezable_wq = alloc_workqueue("events_freezable",
4177 WQ_FREEZABLE, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09004178 BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
Tejun Heoae930e02012-08-20 14:51:23 -07004179 !system_unbound_wq || !system_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07004180 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181}
Suresh Siddha6ee05782010-07-30 14:57:37 -07004182early_initcall(init_workqueues);