blob: cac710646cbc86518c77754ee623967d94bdd7bb [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 *
Tejun Heobc3a1af2013-03-13 19:47:39 -070063 * Note that DISASSOCIATED should be flipped only while holding
64 * manager_mutex to avoid changing binding state while
Tejun Heo24647572013-01-24 11:01:33 -080065 * 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 Heoc5aa87b2013-03-13 16:51:36 -0700148 /* a workers is either on busy_hash or idle_list, or the manager */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800149 DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER);
150 /* L: hash of busy workers */
151
Tejun Heobc3a1af2013-03-13 19:47:39 -0700152 /* see manage_workers() for details on the two manager mutexes */
Tejun Heo34a06bd2013-03-12 11:30:00 -0700153 struct mutex manager_arb; /* manager arbitration */
Tejun Heobc3a1af2013-03-13 19:47:39 -0700154 struct mutex manager_mutex; /* manager exclusion */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700155 struct ida worker_ida; /* L: for worker IDs */
Tejun Heoe19e3972013-01-24 11:39:44 -0800156
Tejun Heo7a4e3442013-03-12 11:30:00 -0700157 struct workqueue_attrs *attrs; /* I: worker attributes */
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700158 struct hlist_node hash_node; /* W: unbound_pool_hash node */
159 int refcnt; /* W: refcnt for unbound pools */
Tejun Heo7a4e3442013-03-12 11:30:00 -0700160
Tejun Heoe19e3972013-01-24 11:39:44 -0800161 /*
162 * The current concurrency level. As it's likely to be accessed
163 * from other CPUs during try_to_wake_up(), put it in a separate
164 * cacheline.
165 */
166 atomic_t nr_running ____cacheline_aligned_in_smp;
Tejun Heo29c91e92013-03-12 11:30:03 -0700167
168 /*
169 * Destruction of pool is sched-RCU protected to allow dereferences
170 * from get_work_pool().
171 */
172 struct rcu_head rcu;
Tejun Heo8b03ae32010-06-29 10:07:12 +0200173} ____cacheline_aligned_in_smp;
174
175/*
Tejun Heo112202d2013-02-13 19:29:12 -0800176 * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS
177 * of work_struct->data are used for flags and the remaining high bits
178 * point to the pwq; thus, pwqs need to be aligned at two's power of the
179 * number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 */
Tejun Heo112202d2013-02-13 19:29:12 -0800181struct pool_workqueue {
Tejun Heobd7bdd42012-07-12 14:46:37 -0700182 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200183 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200184 int work_color; /* L: current color */
185 int flush_color; /* L: flushing color */
Tejun Heo8864b4e2013-03-12 11:30:04 -0700186 int refcnt; /* L: reference count */
Tejun Heo73f53c42010-06-29 10:07:11 +0200187 int nr_in_flight[WORK_NR_COLORS];
188 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200189 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200190 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200191 struct list_head delayed_works; /* L: delayed works */
Tejun Heo75ccf592013-03-12 11:30:04 -0700192 struct list_head pwqs_node; /* FR: node on wq->pwqs */
Tejun Heo493a1722013-03-12 11:29:59 -0700193 struct list_head mayday_node; /* W: node on wq->maydays */
Tejun Heo8864b4e2013-03-12 11:30:04 -0700194
195 /*
196 * Release of unbound pwq is punted to system_wq. See put_pwq()
197 * and pwq_unbound_release_workfn() for details. pool_workqueue
198 * itself is also sched-RCU protected so that the first pwq can be
199 * determined without grabbing workqueue_lock.
200 */
201 struct work_struct unbound_release_work;
202 struct rcu_head rcu;
Tejun Heoe904e6c2013-03-12 11:29:57 -0700203} __aligned(1 << WORK_STRUCT_FLAG_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200206 * Structure used to wait for workqueue flush.
207 */
208struct wq_flusher {
209 struct list_head list; /* F: list of flushers */
210 int flush_color; /* F: flush color waiting for */
211 struct completion done; /* flush completion */
212};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Tejun Heo226223a2013-03-12 11:30:05 -0700214struct wq_device;
215
Tejun Heo73f53c42010-06-29 10:07:11 +0200216/*
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700217 * The externally visible workqueue. It relays the issued work items to
218 * the appropriate worker_pool through its pool_workqueues.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 */
220struct workqueue_struct {
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200221 unsigned int flags; /* W: WQ_* flags */
Tejun Heo420c0dd2013-03-12 11:29:59 -0700222 struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwq's */
Tejun Heo75ccf592013-03-12 11:30:04 -0700223 struct list_head pwqs; /* FR: all pwqs of this wq */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200224 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200225
226 struct mutex flush_mutex; /* protects wq flushing */
227 int work_color; /* F: current work color */
228 int flush_color; /* F: current flush color */
Tejun Heo112202d2013-02-13 19:29:12 -0800229 atomic_t nr_pwqs_to_flush; /* flush in progress */
Tejun Heo73f53c42010-06-29 10:07:11 +0200230 struct wq_flusher *first_flusher; /* F: first flusher */
231 struct list_head flusher_queue; /* F: flush waiters */
232 struct list_head flusher_overflow; /* F: flush overflow list */
233
Tejun Heo493a1722013-03-12 11:29:59 -0700234 struct list_head maydays; /* W: pwqs requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200235 struct worker *rescuer; /* I: rescue worker */
236
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200237 int nr_drainers; /* W: drain in progress */
Tejun Heo112202d2013-02-13 19:29:12 -0800238 int saved_max_active; /* W: saved pwq max_active */
Tejun Heo226223a2013-03-12 11:30:05 -0700239
240#ifdef CONFIG_SYSFS
241 struct wq_device *wq_dev; /* I: for sysfs interface */
242#endif
Johannes Berg4e6045f2007-10-18 23:39:55 -0700243#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200244 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700245#endif
Tejun Heob196be82012-01-10 15:11:35 -0800246 char name[]; /* I: workqueue name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247};
248
Tejun Heoe904e6c2013-03-12 11:29:57 -0700249static struct kmem_cache *pwq_cache;
250
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700251/* W: hash of all unbound pools keyed by pool->attrs */
Tejun Heo29c91e92013-03-12 11:30:03 -0700252static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER);
253
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700254/* I: attributes used when instantiating standard unbound pools on demand */
Tejun Heo29c91e92013-03-12 11:30:03 -0700255static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS];
256
Tejun Heod320c032010-06-29 10:07:14 +0200257struct workqueue_struct *system_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200258EXPORT_SYMBOL_GPL(system_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300259struct workqueue_struct *system_highpri_wq __read_mostly;
Joonsoo Kim1aabe902012-08-15 23:25:39 +0900260EXPORT_SYMBOL_GPL(system_highpri_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300261struct workqueue_struct *system_long_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200262EXPORT_SYMBOL_GPL(system_long_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300263struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200264EXPORT_SYMBOL_GPL(system_unbound_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300265struct workqueue_struct *system_freezable_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100266EXPORT_SYMBOL_GPL(system_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200267
Tejun Heo97bd2342010-10-05 10:41:14 +0200268#define CREATE_TRACE_POINTS
269#include <trace/events/workqueue.h>
270
Tejun Heo76af4d92013-03-12 11:30:00 -0700271#define assert_rcu_or_wq_lock() \
272 rcu_lockdep_assert(rcu_read_lock_sched_held() || \
273 lockdep_is_held(&workqueue_lock), \
274 "sched RCU or workqueue lock should be held")
275
Tejun Heof02ae732013-03-12 11:30:03 -0700276#define for_each_cpu_worker_pool(pool, cpu) \
277 for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \
278 (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \
Tejun Heo7a62c2c2013-03-12 11:30:03 -0700279 (pool)++)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700280
Sasha Levinb67bfe02013-02-27 17:06:00 -0800281#define for_each_busy_worker(worker, i, pool) \
282 hash_for_each(pool->busy_hash, i, worker, hentry)
Tejun Heodb7bccf2010-06-29 10:07:12 +0200283
Tejun Heo49e3cf42013-03-12 11:29:58 -0700284/**
Tejun Heo17116962013-03-12 11:29:58 -0700285 * for_each_pool - iterate through all worker_pools in the system
286 * @pool: iteration cursor
Tejun Heo611c92a2013-03-13 16:51:36 -0700287 * @pi: integer used for iteration
Tejun Heofa1b54e2013-03-12 11:30:00 -0700288 *
289 * This must be called either with workqueue_lock held or sched RCU read
290 * locked. If the pool needs to be used beyond the locking in effect, the
291 * caller is responsible for guaranteeing that the pool stays online.
292 *
293 * The if/else clause exists only for the lockdep assertion and can be
294 * ignored.
Tejun Heo17116962013-03-12 11:29:58 -0700295 */
Tejun Heo611c92a2013-03-13 16:51:36 -0700296#define for_each_pool(pool, pi) \
297 idr_for_each_entry(&worker_pool_idr, pool, pi) \
Tejun Heofa1b54e2013-03-12 11:30:00 -0700298 if (({ assert_rcu_or_wq_lock(); false; })) { } \
299 else
Tejun Heo17116962013-03-12 11:29:58 -0700300
301/**
Tejun Heo49e3cf42013-03-12 11:29:58 -0700302 * for_each_pwq - iterate through all pool_workqueues of the specified workqueue
303 * @pwq: iteration cursor
304 * @wq: the target workqueue
Tejun Heo76af4d92013-03-12 11:30:00 -0700305 *
306 * This must be called either with workqueue_lock held or sched RCU read
307 * locked. If the pwq needs to be used beyond the locking in effect, the
308 * caller is responsible for guaranteeing that the pwq stays online.
309 *
310 * The if/else clause exists only for the lockdep assertion and can be
311 * ignored.
Tejun Heo49e3cf42013-03-12 11:29:58 -0700312 */
313#define for_each_pwq(pwq, wq) \
Tejun Heo76af4d92013-03-12 11:30:00 -0700314 list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node) \
315 if (({ assert_rcu_or_wq_lock(); false; })) { } \
316 else
Tejun Heof3421792010-07-02 10:03:51 +0200317
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900318#ifdef CONFIG_DEBUG_OBJECTS_WORK
319
320static struct debug_obj_descr work_debug_descr;
321
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100322static void *work_debug_hint(void *addr)
323{
324 return ((struct work_struct *) addr)->func;
325}
326
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900327/*
328 * fixup_init is called when:
329 * - an active object is initialized
330 */
331static int work_fixup_init(void *addr, enum debug_obj_state state)
332{
333 struct work_struct *work = addr;
334
335 switch (state) {
336 case ODEBUG_STATE_ACTIVE:
337 cancel_work_sync(work);
338 debug_object_init(work, &work_debug_descr);
339 return 1;
340 default:
341 return 0;
342 }
343}
344
345/*
346 * fixup_activate is called when:
347 * - an active object is activated
348 * - an unknown object is activated (might be a statically initialized object)
349 */
350static int work_fixup_activate(void *addr, enum debug_obj_state state)
351{
352 struct work_struct *work = addr;
353
354 switch (state) {
355
356 case ODEBUG_STATE_NOTAVAILABLE:
357 /*
358 * This is not really a fixup. The work struct was
359 * statically initialized. We just make sure that it
360 * is tracked in the object tracker.
361 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200362 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900363 debug_object_init(work, &work_debug_descr);
364 debug_object_activate(work, &work_debug_descr);
365 return 0;
366 }
367 WARN_ON_ONCE(1);
368 return 0;
369
370 case ODEBUG_STATE_ACTIVE:
371 WARN_ON(1);
372
373 default:
374 return 0;
375 }
376}
377
378/*
379 * fixup_free is called when:
380 * - an active object is freed
381 */
382static int work_fixup_free(void *addr, enum debug_obj_state state)
383{
384 struct work_struct *work = addr;
385
386 switch (state) {
387 case ODEBUG_STATE_ACTIVE:
388 cancel_work_sync(work);
389 debug_object_free(work, &work_debug_descr);
390 return 1;
391 default:
392 return 0;
393 }
394}
395
396static struct debug_obj_descr work_debug_descr = {
397 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100398 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900399 .fixup_init = work_fixup_init,
400 .fixup_activate = work_fixup_activate,
401 .fixup_free = work_fixup_free,
402};
403
404static inline void debug_work_activate(struct work_struct *work)
405{
406 debug_object_activate(work, &work_debug_descr);
407}
408
409static inline void debug_work_deactivate(struct work_struct *work)
410{
411 debug_object_deactivate(work, &work_debug_descr);
412}
413
414void __init_work(struct work_struct *work, int onstack)
415{
416 if (onstack)
417 debug_object_init_on_stack(work, &work_debug_descr);
418 else
419 debug_object_init(work, &work_debug_descr);
420}
421EXPORT_SYMBOL_GPL(__init_work);
422
423void destroy_work_on_stack(struct work_struct *work)
424{
425 debug_object_free(work, &work_debug_descr);
426}
427EXPORT_SYMBOL_GPL(destroy_work_on_stack);
428
429#else
430static inline void debug_work_activate(struct work_struct *work) { }
431static inline void debug_work_deactivate(struct work_struct *work) { }
432#endif
433
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100434/* Serializes the accesses to the list of workqueues. */
435static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436static LIST_HEAD(workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200437static bool workqueue_freezing; /* W: have wqs started freezing? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700439/* the per-cpu worker pools */
Tejun Heoe19e3972013-01-24 11:39:44 -0800440static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS],
Tejun Heof02ae732013-03-12 11:30:03 -0700441 cpu_worker_pools);
Tejun Heof3421792010-07-02 10:03:51 +0200442
Tejun Heofa1b54e2013-03-12 11:30:00 -0700443/*
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700444 * R: idr of all pools. Modifications are protected by workqueue_lock.
445 * Read accesses are protected by sched-RCU protected.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700446 */
Tejun Heo9daf9e62013-01-24 11:01:33 -0800447static DEFINE_IDR(worker_pool_idr);
448
Tejun Heoc34056a2010-06-29 10:07:11 +0200449static int worker_thread(void *__worker);
Tejun Heo226223a2013-03-12 11:30:05 -0700450static void copy_workqueue_attrs(struct workqueue_attrs *to,
451 const struct workqueue_attrs *from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Tejun Heo9daf9e62013-01-24 11:01:33 -0800453/* allocate ID and assign it to @pool */
454static int worker_pool_assign_id(struct worker_pool *pool)
455{
456 int ret;
457
Tejun Heofa1b54e2013-03-12 11:30:00 -0700458 do {
459 if (!idr_pre_get(&worker_pool_idr, GFP_KERNEL))
460 return -ENOMEM;
461
462 spin_lock_irq(&workqueue_lock);
463 ret = idr_get_new(&worker_pool_idr, pool, &pool->id);
464 spin_unlock_irq(&workqueue_lock);
465 } while (ret == -EAGAIN);
Tejun Heo9daf9e62013-01-24 11:01:33 -0800466
467 return ret;
468}
469
Tejun Heo76af4d92013-03-12 11:30:00 -0700470/**
471 * first_pwq - return the first pool_workqueue of the specified workqueue
472 * @wq: the target workqueue
473 *
474 * This must be called either with workqueue_lock held or sched RCU read
475 * locked. If the pwq needs to be used beyond the locking in effect, the
476 * caller is responsible for guaranteeing that the pwq stays online.
477 */
Tejun Heo7fb98ea2013-03-12 11:30:00 -0700478static struct pool_workqueue *first_pwq(struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700479{
Tejun Heo76af4d92013-03-12 11:30:00 -0700480 assert_rcu_or_wq_lock();
481 return list_first_or_null_rcu(&wq->pwqs, struct pool_workqueue,
482 pwqs_node);
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700483}
484
Tejun Heo73f53c42010-06-29 10:07:11 +0200485static unsigned int work_color_to_flags(int color)
486{
487 return color << WORK_STRUCT_COLOR_SHIFT;
488}
489
490static int get_work_color(struct work_struct *work)
491{
492 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
493 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
494}
495
496static int work_next_color(int color)
497{
498 return (color + 1) % WORK_NR_COLORS;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700499}
500
David Howells4594bf12006-12-07 11:33:26 +0000501/*
Tejun Heo112202d2013-02-13 19:29:12 -0800502 * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data
503 * contain the pointer to the queued pwq. Once execution starts, the flag
Tejun Heo7c3eed52013-01-24 11:01:33 -0800504 * is cleared and the high bits contain OFFQ flags and pool ID.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200505 *
Tejun Heo112202d2013-02-13 19:29:12 -0800506 * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling()
507 * and clear_work_data() can be used to set the pwq, pool or clear
Tejun Heobbb68df2012-08-03 10:30:46 -0700508 * work->data. These functions should only be called while the work is
509 * owned - ie. while the PENDING bit is set.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200510 *
Tejun Heo112202d2013-02-13 19:29:12 -0800511 * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq
Tejun Heo7c3eed52013-01-24 11:01:33 -0800512 * corresponding to a work. Pool is available once the work has been
Tejun Heo112202d2013-02-13 19:29:12 -0800513 * queued anywhere after initialization until it is sync canceled. pwq is
Tejun Heo7c3eed52013-01-24 11:01:33 -0800514 * available only while the work item is queued.
Tejun Heobbb68df2012-08-03 10:30:46 -0700515 *
516 * %WORK_OFFQ_CANCELING is used to mark a work item which is being
517 * canceled. While being canceled, a work item may have its PENDING set
518 * but stay off timer and worklist for arbitrarily long and nobody should
519 * try to steal the PENDING bit.
David Howells4594bf12006-12-07 11:33:26 +0000520 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200521static inline void set_work_data(struct work_struct *work, unsigned long data,
522 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000523{
Tejun Heo6183c002013-03-12 11:29:57 -0700524 WARN_ON_ONCE(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200525 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000526}
David Howells365970a2006-11-22 14:54:49 +0000527
Tejun Heo112202d2013-02-13 19:29:12 -0800528static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq,
Tejun Heo7a22ad72010-06-29 10:07:13 +0200529 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200530{
Tejun Heo112202d2013-02-13 19:29:12 -0800531 set_work_data(work, (unsigned long)pwq,
532 WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200533}
534
Lai Jiangshan4468a002013-02-06 18:04:53 -0800535static void set_work_pool_and_keep_pending(struct work_struct *work,
536 int pool_id)
537{
538 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT,
539 WORK_STRUCT_PENDING);
540}
541
Tejun Heo7c3eed52013-01-24 11:01:33 -0800542static void set_work_pool_and_clear_pending(struct work_struct *work,
543 int pool_id)
David Howells365970a2006-11-22 14:54:49 +0000544{
Tejun Heo23657bb2012-08-13 17:08:19 -0700545 /*
546 * The following wmb is paired with the implied mb in
547 * test_and_set_bit(PENDING) and ensures all updates to @work made
548 * here are visible to and precede any updates by the next PENDING
549 * owner.
550 */
551 smp_wmb();
Tejun Heo7c3eed52013-01-24 11:01:33 -0800552 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200553}
554
555static void clear_work_data(struct work_struct *work)
556{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800557 smp_wmb(); /* see set_work_pool_and_clear_pending() */
558 set_work_data(work, WORK_STRUCT_NO_POOL, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200559}
560
Tejun Heo112202d2013-02-13 19:29:12 -0800561static struct pool_workqueue *get_work_pwq(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200562{
Tejun Heoe1201532010-07-22 14:14:25 +0200563 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200564
Tejun Heo112202d2013-02-13 19:29:12 -0800565 if (data & WORK_STRUCT_PWQ)
Tejun Heoe1201532010-07-22 14:14:25 +0200566 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
567 else
568 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200569}
570
Tejun Heo7c3eed52013-01-24 11:01:33 -0800571/**
572 * get_work_pool - return the worker_pool a given work was associated with
573 * @work: the work item of interest
574 *
575 * Return the worker_pool @work was last associated with. %NULL if none.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700576 *
577 * Pools are created and destroyed under workqueue_lock, and allows read
578 * access under sched-RCU read lock. As such, this function should be
579 * called under workqueue_lock or with preemption disabled.
580 *
581 * All fields of the returned pool are accessible as long as the above
582 * mentioned locking is in effect. If the returned pool needs to be used
583 * beyond the critical section, the caller is responsible for ensuring the
584 * returned pool is and stays online.
Tejun Heo7c3eed52013-01-24 11:01:33 -0800585 */
586static struct worker_pool *get_work_pool(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200587{
Tejun Heoe1201532010-07-22 14:14:25 +0200588 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800589 int pool_id;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200590
Tejun Heofa1b54e2013-03-12 11:30:00 -0700591 assert_rcu_or_wq_lock();
592
Tejun Heo112202d2013-02-13 19:29:12 -0800593 if (data & WORK_STRUCT_PWQ)
594 return ((struct pool_workqueue *)
Tejun Heo7c3eed52013-01-24 11:01:33 -0800595 (data & WORK_STRUCT_WQ_DATA_MASK))->pool;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200596
Tejun Heo7c3eed52013-01-24 11:01:33 -0800597 pool_id = data >> WORK_OFFQ_POOL_SHIFT;
598 if (pool_id == WORK_OFFQ_POOL_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200599 return NULL;
600
Tejun Heofa1b54e2013-03-12 11:30:00 -0700601 return idr_find(&worker_pool_idr, pool_id);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800602}
603
604/**
605 * get_work_pool_id - return the worker pool ID a given work is associated with
606 * @work: the work item of interest
607 *
608 * Return the worker_pool ID @work was last associated with.
609 * %WORK_OFFQ_POOL_NONE if none.
610 */
611static int get_work_pool_id(struct work_struct *work)
612{
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800613 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800614
Tejun Heo112202d2013-02-13 19:29:12 -0800615 if (data & WORK_STRUCT_PWQ)
616 return ((struct pool_workqueue *)
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800617 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id;
618
619 return data >> WORK_OFFQ_POOL_SHIFT;
Tejun Heo7c3eed52013-01-24 11:01:33 -0800620}
621
Tejun Heobbb68df2012-08-03 10:30:46 -0700622static void mark_work_canceling(struct work_struct *work)
623{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800624 unsigned long pool_id = get_work_pool_id(work);
Tejun Heobbb68df2012-08-03 10:30:46 -0700625
Tejun Heo7c3eed52013-01-24 11:01:33 -0800626 pool_id <<= WORK_OFFQ_POOL_SHIFT;
627 set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700628}
629
630static bool work_is_canceling(struct work_struct *work)
631{
632 unsigned long data = atomic_long_read(&work->data);
633
Tejun Heo112202d2013-02-13 19:29:12 -0800634 return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700635}
636
David Howells365970a2006-11-22 14:54:49 +0000637/*
Tejun Heo32704762012-07-13 22:16:45 -0700638 * Policy functions. These define the policies on how the global worker
639 * pools are managed. Unless noted otherwise, these functions assume that
Tejun Heod565ed62013-01-24 11:01:33 -0800640 * they're being called with pool->lock held.
David Howells365970a2006-11-22 14:54:49 +0000641 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200642
Tejun Heo63d95a92012-07-12 14:46:37 -0700643static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000644{
Tejun Heoe19e3972013-01-24 11:39:44 -0800645 return !atomic_read(&pool->nr_running);
David Howells365970a2006-11-22 14:54:49 +0000646}
647
Tejun Heoe22bee72010-06-29 10:07:14 +0200648/*
649 * Need to wake up a worker? Called from anything but currently
650 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700651 *
652 * Note that, because unbound workers never contribute to nr_running, this
Tejun Heo706026c2013-01-24 11:01:34 -0800653 * function will always return %true for unbound pools as long as the
Tejun Heo974271c2012-07-12 14:46:37 -0700654 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200655 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700656static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000657{
Tejun Heo63d95a92012-07-12 14:46:37 -0700658 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000659}
660
Tejun Heoe22bee72010-06-29 10:07:14 +0200661/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700662static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200663{
Tejun Heo63d95a92012-07-12 14:46:37 -0700664 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200665}
666
667/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700668static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200669{
Tejun Heoe19e3972013-01-24 11:39:44 -0800670 return !list_empty(&pool->worklist) &&
671 atomic_read(&pool->nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200672}
673
674/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700675static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200676{
Tejun Heo63d95a92012-07-12 14:46:37 -0700677 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200678}
679
680/* Do I need to be the manager? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700681static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200682{
Tejun Heo63d95a92012-07-12 14:46:37 -0700683 return need_to_create_worker(pool) ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700684 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200685}
686
687/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700688static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200689{
Tejun Heo34a06bd2013-03-12 11:30:00 -0700690 bool managing = mutex_is_locked(&pool->manager_arb);
Tejun Heo63d95a92012-07-12 14:46:37 -0700691 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
692 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200693
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700694 /*
695 * nr_idle and idle_list may disagree if idle rebinding is in
696 * progress. Never return %true if idle_list is empty.
697 */
698 if (list_empty(&pool->idle_list))
699 return false;
700
Tejun Heoe22bee72010-06-29 10:07:14 +0200701 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
702}
703
704/*
705 * Wake up functions.
706 */
707
Tejun Heo7e116292010-06-29 10:07:13 +0200708/* Return the first worker. Safe with preemption disabled */
Tejun Heo63d95a92012-07-12 14:46:37 -0700709static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200710{
Tejun Heo63d95a92012-07-12 14:46:37 -0700711 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200712 return NULL;
713
Tejun Heo63d95a92012-07-12 14:46:37 -0700714 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200715}
716
717/**
718 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700719 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200720 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700721 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200722 *
723 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800724 * spin_lock_irq(pool->lock).
Tejun Heo7e116292010-06-29 10:07:13 +0200725 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700726static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200727{
Tejun Heo63d95a92012-07-12 14:46:37 -0700728 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200729
730 if (likely(worker))
731 wake_up_process(worker->task);
732}
733
Tejun Heo4690c4a2010-06-29 10:07:10 +0200734/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200735 * wq_worker_waking_up - a worker is waking up
736 * @task: task waking up
737 * @cpu: CPU @task is waking up to
738 *
739 * This function is called during try_to_wake_up() when a worker is
740 * being awoken.
741 *
742 * CONTEXT:
743 * spin_lock_irq(rq->lock)
744 */
Tejun Heod84ff052013-03-12 11:29:59 -0700745void wq_worker_waking_up(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200746{
747 struct worker *worker = kthread_data(task);
748
Joonsoo Kim36576002012-10-26 23:03:49 +0900749 if (!(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoec22ca52013-01-24 11:01:33 -0800750 WARN_ON_ONCE(worker->pool->cpu != cpu);
Tejun Heoe19e3972013-01-24 11:39:44 -0800751 atomic_inc(&worker->pool->nr_running);
Joonsoo Kim36576002012-10-26 23:03:49 +0900752 }
Tejun Heoe22bee72010-06-29 10:07:14 +0200753}
754
755/**
756 * wq_worker_sleeping - a worker is going to sleep
757 * @task: task going to sleep
758 * @cpu: CPU in question, must be the current CPU number
759 *
760 * This function is called during schedule() when a busy worker is
761 * going to sleep. Worker on the same cpu can be woken up by
762 * returning pointer to its task.
763 *
764 * CONTEXT:
765 * spin_lock_irq(rq->lock)
766 *
767 * RETURNS:
768 * Worker task on @cpu to wake up, %NULL if none.
769 */
Tejun Heod84ff052013-03-12 11:29:59 -0700770struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200771{
772 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heo111c2252013-01-17 17:16:24 -0800773 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200774
Tejun Heo111c2252013-01-17 17:16:24 -0800775 /*
776 * Rescuers, which may not have all the fields set up like normal
777 * workers, also reach here, let's not access anything before
778 * checking NOT_RUNNING.
779 */
Steven Rostedt2d646722010-12-03 23:12:33 -0500780 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200781 return NULL;
782
Tejun Heo111c2252013-01-17 17:16:24 -0800783 pool = worker->pool;
Tejun Heo111c2252013-01-17 17:16:24 -0800784
Tejun Heoe22bee72010-06-29 10:07:14 +0200785 /* this can only happen on the local cpu */
Tejun Heo6183c002013-03-12 11:29:57 -0700786 if (WARN_ON_ONCE(cpu != raw_smp_processor_id()))
787 return NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +0200788
789 /*
790 * The counterpart of the following dec_and_test, implied mb,
791 * worklist not empty test sequence is in insert_work().
792 * Please read comment there.
793 *
Tejun Heo628c78e2012-07-17 12:39:27 -0700794 * NOT_RUNNING is clear. This means that we're bound to and
795 * running on the local cpu w/ rq lock held and preemption
796 * disabled, which in turn means that none else could be
Tejun Heod565ed62013-01-24 11:01:33 -0800797 * manipulating idle_list, so dereferencing idle_list without pool
Tejun Heo628c78e2012-07-17 12:39:27 -0700798 * lock is safe.
Tejun Heoe22bee72010-06-29 10:07:14 +0200799 */
Tejun Heoe19e3972013-01-24 11:39:44 -0800800 if (atomic_dec_and_test(&pool->nr_running) &&
801 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700802 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200803 return to_wakeup ? to_wakeup->task : NULL;
804}
805
806/**
807 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200808 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200809 * @flags: flags to set
810 * @wakeup: wakeup an idle worker if necessary
811 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200812 * Set @flags in @worker->flags and adjust nr_running accordingly. If
813 * nr_running becomes zero and @wakeup is %true, an idle worker is
814 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200815 *
Tejun Heocb444762010-07-02 10:03:50 +0200816 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800817 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200818 */
819static inline void worker_set_flags(struct worker *worker, unsigned int flags,
820 bool wakeup)
821{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700822 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200823
Tejun Heocb444762010-07-02 10:03:50 +0200824 WARN_ON_ONCE(worker->task != current);
825
Tejun Heoe22bee72010-06-29 10:07:14 +0200826 /*
827 * If transitioning into NOT_RUNNING, adjust nr_running and
828 * wake up an idle worker as necessary if requested by
829 * @wakeup.
830 */
831 if ((flags & WORKER_NOT_RUNNING) &&
832 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoe22bee72010-06-29 10:07:14 +0200833 if (wakeup) {
Tejun Heoe19e3972013-01-24 11:39:44 -0800834 if (atomic_dec_and_test(&pool->nr_running) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -0700835 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700836 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200837 } else
Tejun Heoe19e3972013-01-24 11:39:44 -0800838 atomic_dec(&pool->nr_running);
Tejun Heoe22bee72010-06-29 10:07:14 +0200839 }
840
Tejun Heod302f012010-06-29 10:07:13 +0200841 worker->flags |= flags;
842}
843
844/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200845 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200846 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200847 * @flags: flags to clear
848 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200849 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200850 *
Tejun Heocb444762010-07-02 10:03:50 +0200851 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800852 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200853 */
854static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
855{
Tejun Heo63d95a92012-07-12 14:46:37 -0700856 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200857 unsigned int oflags = worker->flags;
858
Tejun Heocb444762010-07-02 10:03:50 +0200859 WARN_ON_ONCE(worker->task != current);
860
Tejun Heod302f012010-06-29 10:07:13 +0200861 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200862
Tejun Heo42c025f2011-01-11 15:58:49 +0100863 /*
864 * If transitioning out of NOT_RUNNING, increment nr_running. Note
865 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
866 * of multiple flags, not a single flag.
867 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200868 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
869 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heoe19e3972013-01-24 11:39:44 -0800870 atomic_inc(&pool->nr_running);
Tejun Heod302f012010-06-29 10:07:13 +0200871}
872
873/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200874 * find_worker_executing_work - find worker which is executing a work
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800875 * @pool: pool of interest
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200876 * @work: work to find worker for
877 *
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800878 * Find a worker which is executing @work on @pool by searching
879 * @pool->busy_hash which is keyed by the address of @work. For a worker
Tejun Heoa2c1c572012-12-18 10:35:02 -0800880 * to match, its current execution should match the address of @work and
881 * its work function. This is to avoid unwanted dependency between
882 * unrelated work executions through a work item being recycled while still
883 * being executed.
884 *
885 * This is a bit tricky. A work item may be freed once its execution
886 * starts and nothing prevents the freed area from being recycled for
887 * another work item. If the same work item address ends up being reused
888 * before the original execution finishes, workqueue will identify the
889 * recycled work item as currently executing and make it wait until the
890 * current execution finishes, introducing an unwanted dependency.
891 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700892 * This function checks the work item address and work function to avoid
893 * false positives. Note that this isn't complete as one may construct a
894 * work function which can introduce dependency onto itself through a
895 * recycled work item. Well, if somebody wants to shoot oneself in the
896 * foot that badly, there's only so much we can do, and if such deadlock
897 * actually occurs, it should be easy to locate the culprit work function.
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200898 *
899 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800900 * spin_lock_irq(pool->lock).
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200901 *
902 * RETURNS:
903 * Pointer to worker which is executing @work if found, NULL
904 * otherwise.
905 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800906static struct worker *find_worker_executing_work(struct worker_pool *pool,
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200907 struct work_struct *work)
908{
Sasha Levin42f85702012-12-17 10:01:23 -0500909 struct worker *worker;
Sasha Levin42f85702012-12-17 10:01:23 -0500910
Sasha Levinb67bfe02013-02-27 17:06:00 -0800911 hash_for_each_possible(pool->busy_hash, worker, hentry,
Tejun Heoa2c1c572012-12-18 10:35:02 -0800912 (unsigned long)work)
913 if (worker->current_work == work &&
914 worker->current_func == work->func)
Sasha Levin42f85702012-12-17 10:01:23 -0500915 return worker;
916
917 return NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200918}
919
920/**
Tejun Heobf4ede02012-08-03 10:30:46 -0700921 * move_linked_works - move linked works to a list
922 * @work: start of series of works to be scheduled
923 * @head: target list to append @work to
924 * @nextp: out paramter for nested worklist walking
925 *
926 * Schedule linked works starting from @work to @head. Work series to
927 * be scheduled starts at @work and includes any consecutive work with
928 * WORK_STRUCT_LINKED set in its predecessor.
929 *
930 * If @nextp is not NULL, it's updated to point to the next work of
931 * the last scheduled work. This allows move_linked_works() to be
932 * nested inside outer list_for_each_entry_safe().
933 *
934 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800935 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -0700936 */
937static void move_linked_works(struct work_struct *work, struct list_head *head,
938 struct work_struct **nextp)
939{
940 struct work_struct *n;
941
942 /*
943 * Linked worklist will always end before the end of the list,
944 * use NULL for list head.
945 */
946 list_for_each_entry_safe_from(work, n, NULL, entry) {
947 list_move_tail(&work->entry, head);
948 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
949 break;
950 }
951
952 /*
953 * If we're already inside safe list traversal and have moved
954 * multiple works to the scheduled queue, the next position
955 * needs to be updated.
956 */
957 if (nextp)
958 *nextp = n;
959}
960
Tejun Heo8864b4e2013-03-12 11:30:04 -0700961/**
962 * get_pwq - get an extra reference on the specified pool_workqueue
963 * @pwq: pool_workqueue to get
964 *
965 * Obtain an extra reference on @pwq. The caller should guarantee that
966 * @pwq has positive refcnt and be holding the matching pool->lock.
967 */
968static void get_pwq(struct pool_workqueue *pwq)
969{
970 lockdep_assert_held(&pwq->pool->lock);
971 WARN_ON_ONCE(pwq->refcnt <= 0);
972 pwq->refcnt++;
973}
974
975/**
976 * put_pwq - put a pool_workqueue reference
977 * @pwq: pool_workqueue to put
978 *
979 * Drop a reference of @pwq. If its refcnt reaches zero, schedule its
980 * destruction. The caller should be holding the matching pool->lock.
981 */
982static void put_pwq(struct pool_workqueue *pwq)
983{
984 lockdep_assert_held(&pwq->pool->lock);
985 if (likely(--pwq->refcnt))
986 return;
987 if (WARN_ON_ONCE(!(pwq->wq->flags & WQ_UNBOUND)))
988 return;
989 /*
990 * @pwq can't be released under pool->lock, bounce to
991 * pwq_unbound_release_workfn(). This never recurses on the same
992 * pool->lock as this path is taken only for unbound workqueues and
993 * the release work item is scheduled on a per-cpu workqueue. To
994 * avoid lockdep warning, unbound pool->locks are given lockdep
995 * subclass of 1 in get_unbound_pool().
996 */
997 schedule_work(&pwq->unbound_release_work);
998}
999
Tejun Heo112202d2013-02-13 19:29:12 -08001000static void pwq_activate_delayed_work(struct work_struct *work)
Tejun Heobf4ede02012-08-03 10:30:46 -07001001{
Tejun Heo112202d2013-02-13 19:29:12 -08001002 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobf4ede02012-08-03 10:30:46 -07001003
1004 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001005 move_linked_works(work, &pwq->pool->worklist, NULL);
Tejun Heobf4ede02012-08-03 10:30:46 -07001006 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo112202d2013-02-13 19:29:12 -08001007 pwq->nr_active++;
Tejun Heobf4ede02012-08-03 10:30:46 -07001008}
1009
Tejun Heo112202d2013-02-13 19:29:12 -08001010static void pwq_activate_first_delayed(struct pool_workqueue *pwq)
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001011{
Tejun Heo112202d2013-02-13 19:29:12 -08001012 struct work_struct *work = list_first_entry(&pwq->delayed_works,
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001013 struct work_struct, entry);
1014
Tejun Heo112202d2013-02-13 19:29:12 -08001015 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001016}
1017
Tejun Heobf4ede02012-08-03 10:30:46 -07001018/**
Tejun Heo112202d2013-02-13 19:29:12 -08001019 * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight
1020 * @pwq: pwq of interest
Tejun Heobf4ede02012-08-03 10:30:46 -07001021 * @color: color of work which left the queue
Tejun Heobf4ede02012-08-03 10:30:46 -07001022 *
1023 * A work either has completed or is removed from pending queue,
Tejun Heo112202d2013-02-13 19:29:12 -08001024 * decrement nr_in_flight of its pwq and handle workqueue flushing.
Tejun Heobf4ede02012-08-03 10:30:46 -07001025 *
1026 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001027 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -07001028 */
Tejun Heo112202d2013-02-13 19:29:12 -08001029static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color)
Tejun Heobf4ede02012-08-03 10:30:46 -07001030{
Tejun Heo8864b4e2013-03-12 11:30:04 -07001031 /* uncolored work items don't participate in flushing or nr_active */
Tejun Heobf4ede02012-08-03 10:30:46 -07001032 if (color == WORK_NO_COLOR)
Tejun Heo8864b4e2013-03-12 11:30:04 -07001033 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001034
Tejun Heo112202d2013-02-13 19:29:12 -08001035 pwq->nr_in_flight[color]--;
Tejun Heobf4ede02012-08-03 10:30:46 -07001036
Tejun Heo112202d2013-02-13 19:29:12 -08001037 pwq->nr_active--;
1038 if (!list_empty(&pwq->delayed_works)) {
Lai Jiangshanb3f9f402012-09-18 10:40:00 -07001039 /* one down, submit a delayed one */
Tejun Heo112202d2013-02-13 19:29:12 -08001040 if (pwq->nr_active < pwq->max_active)
1041 pwq_activate_first_delayed(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001042 }
1043
1044 /* is flush in progress and are we at the flushing tip? */
Tejun Heo112202d2013-02-13 19:29:12 -08001045 if (likely(pwq->flush_color != color))
Tejun Heo8864b4e2013-03-12 11:30:04 -07001046 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001047
1048 /* are there still in-flight works? */
Tejun Heo112202d2013-02-13 19:29:12 -08001049 if (pwq->nr_in_flight[color])
Tejun Heo8864b4e2013-03-12 11:30:04 -07001050 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001051
Tejun Heo112202d2013-02-13 19:29:12 -08001052 /* this pwq is done, clear flush_color */
1053 pwq->flush_color = -1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001054
1055 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001056 * If this was the last pwq, wake up the first flusher. It
Tejun Heobf4ede02012-08-03 10:30:46 -07001057 * will handle the rest.
1058 */
Tejun Heo112202d2013-02-13 19:29:12 -08001059 if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush))
1060 complete(&pwq->wq->first_flusher->done);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001061out_put:
1062 put_pwq(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001063}
1064
Tejun Heo36e227d2012-08-03 10:30:46 -07001065/**
Tejun Heobbb68df2012-08-03 10:30:46 -07001066 * try_to_grab_pending - steal work item from worklist and disable irq
Tejun Heo36e227d2012-08-03 10:30:46 -07001067 * @work: work item to steal
1068 * @is_dwork: @work is a delayed_work
Tejun Heobbb68df2012-08-03 10:30:46 -07001069 * @flags: place to store irq state
Tejun Heo36e227d2012-08-03 10:30:46 -07001070 *
1071 * Try to grab PENDING bit of @work. This function can handle @work in any
1072 * stable state - idle, on timer or on worklist. Return values are
1073 *
1074 * 1 if @work was pending and we successfully stole PENDING
1075 * 0 if @work was idle and we claimed PENDING
1076 * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry
Tejun Heobbb68df2012-08-03 10:30:46 -07001077 * -ENOENT if someone else is canceling @work, this state may persist
1078 * for arbitrarily long
Tejun Heo36e227d2012-08-03 10:30:46 -07001079 *
Tejun Heobbb68df2012-08-03 10:30:46 -07001080 * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001081 * interrupted while holding PENDING and @work off queue, irq must be
1082 * disabled on entry. This, combined with delayed_work->timer being
1083 * irqsafe, ensures that we return -EAGAIN for finite short period of time.
Tejun Heobbb68df2012-08-03 10:30:46 -07001084 *
1085 * On successful return, >= 0, irq is disabled and the caller is
1086 * responsible for releasing it using local_irq_restore(*@flags).
1087 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001088 * This function is safe to call from any context including IRQ handler.
Tejun Heobf4ede02012-08-03 10:30:46 -07001089 */
Tejun Heobbb68df2012-08-03 10:30:46 -07001090static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
1091 unsigned long *flags)
Tejun Heobf4ede02012-08-03 10:30:46 -07001092{
Tejun Heod565ed62013-01-24 11:01:33 -08001093 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08001094 struct pool_workqueue *pwq;
Tejun Heobf4ede02012-08-03 10:30:46 -07001095
Tejun Heobbb68df2012-08-03 10:30:46 -07001096 local_irq_save(*flags);
1097
Tejun Heo36e227d2012-08-03 10:30:46 -07001098 /* try to steal the timer if it exists */
1099 if (is_dwork) {
1100 struct delayed_work *dwork = to_delayed_work(work);
1101
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001102 /*
1103 * dwork->timer is irqsafe. If del_timer() fails, it's
1104 * guaranteed that the timer is not queued anywhere and not
1105 * running on the local CPU.
1106 */
Tejun Heo36e227d2012-08-03 10:30:46 -07001107 if (likely(del_timer(&dwork->timer)))
1108 return 1;
1109 }
1110
1111 /* try to claim PENDING the normal way */
Tejun Heobf4ede02012-08-03 10:30:46 -07001112 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1113 return 0;
1114
1115 /*
1116 * The queueing is in progress, or it is already queued. Try to
1117 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1118 */
Tejun Heod565ed62013-01-24 11:01:33 -08001119 pool = get_work_pool(work);
1120 if (!pool)
Tejun Heobbb68df2012-08-03 10:30:46 -07001121 goto fail;
Tejun Heobf4ede02012-08-03 10:30:46 -07001122
Tejun Heod565ed62013-01-24 11:01:33 -08001123 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001124 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001125 * work->data is guaranteed to point to pwq only while the work
1126 * item is queued on pwq->wq, and both updating work->data to point
1127 * to pwq on queueing and to pool on dequeueing are done under
1128 * pwq->pool->lock. This in turn guarantees that, if work->data
1129 * points to pwq which is associated with a locked pool, the work
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001130 * item is currently queued on that pool.
1131 */
Tejun Heo112202d2013-02-13 19:29:12 -08001132 pwq = get_work_pwq(work);
1133 if (pwq && pwq->pool == pool) {
Tejun Heo16062832013-02-06 18:04:53 -08001134 debug_work_deactivate(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001135
Tejun Heo16062832013-02-06 18:04:53 -08001136 /*
1137 * A delayed work item cannot be grabbed directly because
1138 * it might have linked NO_COLOR work items which, if left
Tejun Heo112202d2013-02-13 19:29:12 -08001139 * on the delayed_list, will confuse pwq->nr_active
Tejun Heo16062832013-02-06 18:04:53 -08001140 * management later on and cause stall. Make sure the work
1141 * item is activated before grabbing.
1142 */
1143 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
Tejun Heo112202d2013-02-13 19:29:12 -08001144 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001145
Tejun Heo16062832013-02-06 18:04:53 -08001146 list_del_init(&work->entry);
Tejun Heo112202d2013-02-13 19:29:12 -08001147 pwq_dec_nr_in_flight(get_work_pwq(work), get_work_color(work));
Tejun Heo36e227d2012-08-03 10:30:46 -07001148
Tejun Heo112202d2013-02-13 19:29:12 -08001149 /* work->data points to pwq iff queued, point to pool */
Tejun Heo16062832013-02-06 18:04:53 -08001150 set_work_pool_and_keep_pending(work, pool->id);
Lai Jiangshan4468a002013-02-06 18:04:53 -08001151
Tejun Heo16062832013-02-06 18:04:53 -08001152 spin_unlock(&pool->lock);
1153 return 1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001154 }
Tejun Heod565ed62013-01-24 11:01:33 -08001155 spin_unlock(&pool->lock);
Tejun Heobbb68df2012-08-03 10:30:46 -07001156fail:
1157 local_irq_restore(*flags);
1158 if (work_is_canceling(work))
1159 return -ENOENT;
1160 cpu_relax();
Tejun Heo36e227d2012-08-03 10:30:46 -07001161 return -EAGAIN;
Tejun Heobf4ede02012-08-03 10:30:46 -07001162}
1163
1164/**
Tejun Heo706026c2013-01-24 11:01:34 -08001165 * insert_work - insert a work into a pool
Tejun Heo112202d2013-02-13 19:29:12 -08001166 * @pwq: pwq @work belongs to
Tejun Heo4690c4a2010-06-29 10:07:10 +02001167 * @work: work to insert
1168 * @head: insertion point
1169 * @extra_flags: extra WORK_STRUCT_* flags to set
1170 *
Tejun Heo112202d2013-02-13 19:29:12 -08001171 * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to
Tejun Heo706026c2013-01-24 11:01:34 -08001172 * work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +02001173 *
1174 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001175 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02001176 */
Tejun Heo112202d2013-02-13 19:29:12 -08001177static void insert_work(struct pool_workqueue *pwq, struct work_struct *work,
1178 struct list_head *head, unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001179{
Tejun Heo112202d2013-02-13 19:29:12 -08001180 struct worker_pool *pool = pwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01001181
Tejun Heo4690c4a2010-06-29 10:07:10 +02001182 /* we own @work, set data and link */
Tejun Heo112202d2013-02-13 19:29:12 -08001183 set_work_pwq(work, pwq, extra_flags);
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -07001184 list_add_tail(&work->entry, head);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001185 get_pwq(pwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02001186
1187 /*
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001188 * Ensure either wq_worker_sleeping() sees the above
1189 * list_add_tail() or we see zero nr_running to avoid workers lying
1190 * around lazily while there are works to be processed.
Tejun Heoe22bee72010-06-29 10:07:14 +02001191 */
1192 smp_mb();
1193
Tejun Heo63d95a92012-07-12 14:46:37 -07001194 if (__need_more_worker(pool))
1195 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001196}
1197
Tejun Heoc8efcc22010-12-20 19:32:04 +01001198/*
1199 * Test whether @work is being queued from another work executing on the
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001200 * same workqueue.
Tejun Heoc8efcc22010-12-20 19:32:04 +01001201 */
1202static bool is_chained_work(struct workqueue_struct *wq)
1203{
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001204 struct worker *worker;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001205
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001206 worker = current_wq_worker();
1207 /*
1208 * Return %true iff I'm a worker execuing a work item on @wq. If
1209 * I'm @worker, it's safe to dereference it without locking.
1210 */
Tejun Heo112202d2013-02-13 19:29:12 -08001211 return worker && worker->current_pwq->wq == wq;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001212}
1213
Tejun Heod84ff052013-03-12 11:29:59 -07001214static void __queue_work(int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 struct work_struct *work)
1216{
Tejun Heo112202d2013-02-13 19:29:12 -08001217 struct pool_workqueue *pwq;
Tejun Heoc9178082013-03-12 11:30:04 -07001218 struct worker_pool *last_pool;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001219 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001220 unsigned int work_flags;
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001221 unsigned int req_cpu = cpu;
Tejun Heo8930cab2012-08-03 10:30:45 -07001222
1223 /*
1224 * While a work item is PENDING && off queue, a task trying to
1225 * steal the PENDING will busy-loop waiting for it to either get
1226 * queued or lose PENDING. Grabbing PENDING and queueing should
1227 * happen with IRQ disabled.
1228 */
1229 WARN_ON_ONCE(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001231 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001232
Tejun Heoc8efcc22010-12-20 19:32:04 +01001233 /* if dying, only works from the same workqueue are allowed */
Tejun Heo618b01e2013-03-12 11:30:04 -07001234 if (unlikely(wq->flags & __WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001235 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001236 return;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001237retry:
Tejun Heoc9178082013-03-12 11:30:04 -07001238 /* pwq which will be used unless @work is executing elsewhere */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001239 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo57469822012-08-03 10:30:45 -07001240 if (cpu == WORK_CPU_UNBOUND)
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001241 cpu = raw_smp_processor_id();
Tejun Heo7fb98ea2013-03-12 11:30:00 -07001242 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heoc9178082013-03-12 11:30:04 -07001243 } else {
1244 pwq = first_pwq(wq);
1245 }
Tejun Heodbf25762012-08-20 14:51:23 -07001246
Tejun Heoc9178082013-03-12 11:30:04 -07001247 /*
1248 * If @work was previously on a different pool, it might still be
1249 * running there, in which case the work needs to be queued on that
1250 * pool to guarantee non-reentrancy.
1251 */
1252 last_pool = get_work_pool(work);
1253 if (last_pool && last_pool != pwq->pool) {
1254 struct worker *worker;
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001255
Tejun Heoc9178082013-03-12 11:30:04 -07001256 spin_lock(&last_pool->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001257
Tejun Heoc9178082013-03-12 11:30:04 -07001258 worker = find_worker_executing_work(last_pool, work);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001259
Tejun Heoc9178082013-03-12 11:30:04 -07001260 if (worker && worker->current_pwq->wq == wq) {
1261 pwq = worker->current_pwq;
Tejun Heo8930cab2012-08-03 10:30:45 -07001262 } else {
Tejun Heoc9178082013-03-12 11:30:04 -07001263 /* meh... not running there, queue here */
1264 spin_unlock(&last_pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08001265 spin_lock(&pwq->pool->lock);
Tejun Heo8930cab2012-08-03 10:30:45 -07001266 }
Tejun Heof3421792010-07-02 10:03:51 +02001267 } else {
Tejun Heo112202d2013-02-13 19:29:12 -08001268 spin_lock(&pwq->pool->lock);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001269 }
1270
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001271 /*
1272 * pwq is determined and locked. For unbound pools, we could have
1273 * raced with pwq release and it could already be dead. If its
1274 * refcnt is zero, repeat pwq selection. Note that pwqs never die
1275 * without another pwq replacing it as the first pwq or while a
1276 * work item is executing on it, so the retying is guaranteed to
1277 * make forward-progress.
1278 */
1279 if (unlikely(!pwq->refcnt)) {
1280 if (wq->flags & WQ_UNBOUND) {
1281 spin_unlock(&pwq->pool->lock);
1282 cpu_relax();
1283 goto retry;
1284 }
1285 /* oops */
1286 WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt",
1287 wq->name, cpu);
1288 }
1289
Tejun Heo112202d2013-02-13 19:29:12 -08001290 /* pwq determined, queue */
1291 trace_workqueue_queue_work(req_cpu, pwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001292
Dan Carpenterf5b25522012-04-13 22:06:58 +03001293 if (WARN_ON(!list_empty(&work->entry))) {
Tejun Heo112202d2013-02-13 19:29:12 -08001294 spin_unlock(&pwq->pool->lock);
Dan Carpenterf5b25522012-04-13 22:06:58 +03001295 return;
1296 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001297
Tejun Heo112202d2013-02-13 19:29:12 -08001298 pwq->nr_in_flight[pwq->work_color]++;
1299 work_flags = work_color_to_flags(pwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001300
Tejun Heo112202d2013-02-13 19:29:12 -08001301 if (likely(pwq->nr_active < pwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001302 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001303 pwq->nr_active++;
1304 worklist = &pwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001305 } else {
1306 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo112202d2013-02-13 19:29:12 -08001307 worklist = &pwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001308 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001309
Tejun Heo112202d2013-02-13 19:29:12 -08001310 insert_work(pwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001311
Tejun Heo112202d2013-02-13 19:29:12 -08001312 spin_unlock(&pwq->pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313}
1314
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001315/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07001316 * queue_work_on - queue work on specific cpu
1317 * @cpu: CPU number to execute work on
1318 * @wq: workqueue to use
1319 * @work: work to queue
1320 *
Tejun Heod4283e92012-08-03 10:30:44 -07001321 * Returns %false if @work was already on a queue, %true otherwise.
Zhang Ruic1a220e2008-07-23 21:28:39 -07001322 *
1323 * We queue the work to a specific CPU, the caller must ensure it
1324 * can't go away.
1325 */
Tejun Heod4283e92012-08-03 10:30:44 -07001326bool queue_work_on(int cpu, struct workqueue_struct *wq,
1327 struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07001328{
Tejun Heod4283e92012-08-03 10:30:44 -07001329 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001330 unsigned long flags;
1331
1332 local_irq_save(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001333
Tejun Heo22df02b2010-06-29 10:07:10 +02001334 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001335 __queue_work(cpu, wq, work);
Tejun Heod4283e92012-08-03 10:30:44 -07001336 ret = true;
Zhang Ruic1a220e2008-07-23 21:28:39 -07001337 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001338
1339 local_irq_restore(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001340 return ret;
1341}
1342EXPORT_SYMBOL_GPL(queue_work_on);
1343
Tejun Heod8e794d2012-08-03 10:30:45 -07001344void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
David Howells52bad642006-11-22 14:54:01 +00001346 struct delayed_work *dwork = (struct delayed_work *)__data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001348 /* should have been called from irqsafe timer with irq already off */
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001349 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350}
Konstantin Khlebnikov1438ade52013-01-24 16:36:31 +04001351EXPORT_SYMBOL(delayed_work_timer_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001353static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
1354 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001356 struct timer_list *timer = &dwork->timer;
1357 struct work_struct *work = &dwork->work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001359 WARN_ON_ONCE(timer->function != delayed_work_timer_fn ||
1360 timer->data != (unsigned long)dwork);
Tejun Heofc4b5142012-12-04 07:40:39 -08001361 WARN_ON_ONCE(timer_pending(timer));
1362 WARN_ON_ONCE(!list_empty(&work->entry));
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001363
Tejun Heo8852aac2012-12-01 16:23:42 -08001364 /*
1365 * If @delay is 0, queue @dwork->work immediately. This is for
1366 * both optimization and correctness. The earliest @timer can
1367 * expire is on the closest next tick and delayed_work users depend
1368 * on that there's no such delay when @delay is 0.
1369 */
1370 if (!delay) {
1371 __queue_work(cpu, wq, &dwork->work);
1372 return;
1373 }
1374
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001375 timer_stats_timer_set_start_info(&dwork->timer);
1376
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001377 dwork->wq = wq;
Tejun Heo12650572012-08-08 09:38:42 -07001378 dwork->cpu = cpu;
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001379 timer->expires = jiffies + delay;
1380
1381 if (unlikely(cpu != WORK_CPU_UNBOUND))
1382 add_timer_on(timer, cpu);
1383 else
1384 add_timer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385}
1386
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001387/**
1388 * queue_delayed_work_on - queue work on specific CPU after delay
1389 * @cpu: CPU number to execute work on
1390 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001391 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001392 * @delay: number of jiffies to wait before queueing
1393 *
Tejun Heo715f1302012-08-03 10:30:46 -07001394 * Returns %false if @work was already on a queue, %true otherwise. If
1395 * @delay is zero and @dwork is idle, it will be scheduled for immediate
1396 * execution.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001397 */
Tejun Heod4283e92012-08-03 10:30:44 -07001398bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
1399 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001400{
David Howells52bad642006-11-22 14:54:01 +00001401 struct work_struct *work = &dwork->work;
Tejun Heod4283e92012-08-03 10:30:44 -07001402 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001403 unsigned long flags;
1404
1405 /* read the comment in __queue_work() */
1406 local_irq_save(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001407
Tejun Heo22df02b2010-06-29 10:07:10 +02001408 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001409 __queue_delayed_work(cpu, wq, dwork, delay);
Tejun Heod4283e92012-08-03 10:30:44 -07001410 ret = true;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001411 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001412
1413 local_irq_restore(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001414 return ret;
1415}
Dave Jonesae90dd52006-06-30 01:40:45 -04001416EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Tejun Heoc8e55f32010-06-29 10:07:12 +02001418/**
Tejun Heo8376fe22012-08-03 10:30:47 -07001419 * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
1420 * @cpu: CPU number to execute work on
1421 * @wq: workqueue to use
1422 * @dwork: work to queue
1423 * @delay: number of jiffies to wait before queueing
1424 *
1425 * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
1426 * modify @dwork's timer so that it expires after @delay. If @delay is
1427 * zero, @work is guaranteed to be scheduled immediately regardless of its
1428 * current state.
1429 *
1430 * Returns %false if @dwork was idle and queued, %true if @dwork was
1431 * pending and its timer was modified.
1432 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001433 * This function is safe to call from any context including IRQ handler.
Tejun Heo8376fe22012-08-03 10:30:47 -07001434 * See try_to_grab_pending() for details.
1435 */
1436bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
1437 struct delayed_work *dwork, unsigned long delay)
1438{
1439 unsigned long flags;
1440 int ret;
1441
1442 do {
1443 ret = try_to_grab_pending(&dwork->work, true, &flags);
1444 } while (unlikely(ret == -EAGAIN));
1445
1446 if (likely(ret >= 0)) {
1447 __queue_delayed_work(cpu, wq, dwork, delay);
1448 local_irq_restore(flags);
1449 }
1450
1451 /* -ENOENT from try_to_grab_pending() becomes %true */
1452 return ret;
1453}
1454EXPORT_SYMBOL_GPL(mod_delayed_work_on);
1455
1456/**
Tejun Heoc8e55f32010-06-29 10:07:12 +02001457 * worker_enter_idle - enter idle state
1458 * @worker: worker which is entering idle state
1459 *
1460 * @worker is entering idle state. Update stats and idle timer if
1461 * necessary.
1462 *
1463 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001464 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001465 */
1466static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001468 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Tejun Heo6183c002013-03-12 11:29:57 -07001470 if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) ||
1471 WARN_ON_ONCE(!list_empty(&worker->entry) &&
1472 (worker->hentry.next || worker->hentry.pprev)))
1473 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Tejun Heocb444762010-07-02 10:03:50 +02001475 /* can't use worker_set_flags(), also called from start_worker() */
1476 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001477 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001478 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001479
Tejun Heoc8e55f32010-06-29 10:07:12 +02001480 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001481 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001482
Tejun Heo628c78e2012-07-17 12:39:27 -07001483 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1484 mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
Tejun Heocb444762010-07-02 10:03:50 +02001485
Tejun Heo544ecf32012-05-14 15:04:50 -07001486 /*
Tejun Heo706026c2013-01-24 11:01:34 -08001487 * Sanity check nr_running. Because wq_unbind_fn() releases
Tejun Heod565ed62013-01-24 11:01:33 -08001488 * pool->lock between setting %WORKER_UNBOUND and zapping
Tejun Heo628c78e2012-07-17 12:39:27 -07001489 * nr_running, the warning may trigger spuriously. Check iff
1490 * unbind is not in progress.
Tejun Heo544ecf32012-05-14 15:04:50 -07001491 */
Tejun Heo24647572013-01-24 11:01:33 -08001492 WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001493 pool->nr_workers == pool->nr_idle &&
Tejun Heoe19e3972013-01-24 11:39:44 -08001494 atomic_read(&pool->nr_running));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495}
1496
Tejun Heoc8e55f32010-06-29 10:07:12 +02001497/**
1498 * worker_leave_idle - leave idle state
1499 * @worker: worker which is leaving idle state
1500 *
1501 * @worker is leaving idle state. Update stats.
1502 *
1503 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001504 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001505 */
1506static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001508 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Tejun Heo6183c002013-03-12 11:29:57 -07001510 if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE)))
1511 return;
Tejun Heod302f012010-06-29 10:07:13 +02001512 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001513 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001514 list_del_init(&worker->entry);
1515}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Tejun Heoe22bee72010-06-29 10:07:14 +02001517/**
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001518 * worker_maybe_bind_and_lock - try to bind %current to worker_pool and lock it
1519 * @pool: target worker_pool
1520 *
1521 * Bind %current to the cpu of @pool if it is associated and lock @pool.
Tejun Heoe22bee72010-06-29 10:07:14 +02001522 *
1523 * Works which are scheduled while the cpu is online must at least be
1524 * scheduled to a worker which is bound to the cpu so that if they are
1525 * flushed from cpu callbacks while cpu is going down, they are
1526 * guaranteed to execute on the cpu.
1527 *
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001528 * This function is to be used by unbound workers and rescuers to bind
Tejun Heoe22bee72010-06-29 10:07:14 +02001529 * themselves to the target cpu and may race with cpu going down or
1530 * coming online. kthread_bind() can't be used because it may put the
1531 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
Tejun Heo706026c2013-01-24 11:01:34 -08001532 * verbatim as it's best effort and blocking and pool may be
Tejun Heoe22bee72010-06-29 10:07:14 +02001533 * [dis]associated in the meantime.
1534 *
Tejun Heo706026c2013-01-24 11:01:34 -08001535 * This function tries set_cpus_allowed() and locks pool and verifies the
Tejun Heo24647572013-01-24 11:01:33 -08001536 * binding against %POOL_DISASSOCIATED which is set during
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001537 * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1538 * enters idle state or fetches works without dropping lock, it can
1539 * guarantee the scheduling requirement described in the first paragraph.
Tejun Heoe22bee72010-06-29 10:07:14 +02001540 *
1541 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001542 * Might sleep. Called without any lock but returns with pool->lock
Tejun Heoe22bee72010-06-29 10:07:14 +02001543 * held.
1544 *
1545 * RETURNS:
Tejun Heo706026c2013-01-24 11:01:34 -08001546 * %true if the associated pool is online (@worker is successfully
Tejun Heoe22bee72010-06-29 10:07:14 +02001547 * bound), %false if offline.
1548 */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001549static bool worker_maybe_bind_and_lock(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001550__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001551{
Tejun Heoe22bee72010-06-29 10:07:14 +02001552 while (true) {
1553 /*
1554 * The following call may fail, succeed or succeed
1555 * without actually migrating the task to the cpu if
1556 * it races with cpu hotunplug operation. Verify
Tejun Heo24647572013-01-24 11:01:33 -08001557 * against POOL_DISASSOCIATED.
Tejun Heoe22bee72010-06-29 10:07:14 +02001558 */
Tejun Heo24647572013-01-24 11:01:33 -08001559 if (!(pool->flags & POOL_DISASSOCIATED))
Tejun Heo7a4e3442013-03-12 11:30:00 -07001560 set_cpus_allowed_ptr(current, pool->attrs->cpumask);
Oleg Nesterov85f41862007-05-09 02:34:20 -07001561
Tejun Heod565ed62013-01-24 11:01:33 -08001562 spin_lock_irq(&pool->lock);
Tejun Heo24647572013-01-24 11:01:33 -08001563 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heoe22bee72010-06-29 10:07:14 +02001564 return false;
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001565 if (task_cpu(current) == pool->cpu &&
Tejun Heo7a4e3442013-03-12 11:30:00 -07001566 cpumask_equal(&current->cpus_allowed, pool->attrs->cpumask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001567 return true;
Tejun Heod565ed62013-01-24 11:01:33 -08001568 spin_unlock_irq(&pool->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001569
Tejun Heo5035b202011-04-29 18:08:37 +02001570 /*
1571 * We've raced with CPU hot[un]plug. Give it a breather
1572 * and retry migration. cond_resched() is required here;
1573 * otherwise, we might deadlock against cpu_stop trying to
1574 * bring down the CPU on non-preemptive kernel.
1575 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001576 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001577 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001578 }
1579}
1580
1581/*
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001582 * Rebind an idle @worker to its CPU. worker_thread() will test
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001583 * list_empty(@worker->entry) before leaving idle and call this function.
Tejun Heo25511a42012-07-17 12:39:27 -07001584 */
1585static void idle_worker_rebind(struct worker *worker)
1586{
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001587 /* CPU may go down again inbetween, clear UNBOUND only on success */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001588 if (worker_maybe_bind_and_lock(worker->pool))
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001589 worker_clr_flags(worker, WORKER_UNBOUND);
Tejun Heo25511a42012-07-17 12:39:27 -07001590
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001591 /* rebind complete, become available again */
1592 list_add(&worker->entry, &worker->pool->idle_list);
Tejun Heod565ed62013-01-24 11:01:33 -08001593 spin_unlock_irq(&worker->pool->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07001594}
1595
1596/*
1597 * Function for @worker->rebind.work used to rebind unbound busy workers to
Tejun Heo403c8212012-07-17 12:39:27 -07001598 * the associated cpu which is coming back online. This is scheduled by
1599 * cpu up but can race with other cpu hotplug operations and may be
1600 * executed twice without intervening cpu down.
Tejun Heoe22bee72010-06-29 10:07:14 +02001601 */
Tejun Heo25511a42012-07-17 12:39:27 -07001602static void busy_worker_rebind_fn(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001603{
1604 struct worker *worker = container_of(work, struct worker, rebind_work);
Tejun Heoe22bee72010-06-29 10:07:14 +02001605
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001606 if (worker_maybe_bind_and_lock(worker->pool))
Lai Jiangshaneab6d822012-09-18 09:59:22 -07001607 worker_clr_flags(worker, WORKER_UNBOUND);
Tejun Heoe22bee72010-06-29 10:07:14 +02001608
Tejun Heod565ed62013-01-24 11:01:33 -08001609 spin_unlock_irq(&worker->pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001610}
1611
Tejun Heo25511a42012-07-17 12:39:27 -07001612/**
Tejun Heo94cf58b2013-01-24 11:01:33 -08001613 * rebind_workers - rebind all workers of a pool to the associated CPU
1614 * @pool: pool of interest
Tejun Heo25511a42012-07-17 12:39:27 -07001615 *
Tejun Heo94cf58b2013-01-24 11:01:33 -08001616 * @pool->cpu is coming online. Rebind all workers to the CPU. Rebinding
Tejun Heo25511a42012-07-17 12:39:27 -07001617 * is different for idle and busy ones.
1618 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001619 * Idle ones will be removed from the idle_list and woken up. They will
1620 * add themselves back after completing rebind. This ensures that the
1621 * idle_list doesn't contain any unbound workers when re-bound busy workers
1622 * try to perform local wake-ups for concurrency management.
Tejun Heo25511a42012-07-17 12:39:27 -07001623 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001624 * Busy workers can rebind after they finish their current work items.
1625 * Queueing the rebind work item at the head of the scheduled list is
1626 * enough. Note that nr_running will be properly bumped as busy workers
1627 * rebind.
Tejun Heo25511a42012-07-17 12:39:27 -07001628 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001629 * On return, all non-manager workers are scheduled for rebind - see
1630 * manage_workers() for the manager special case. Any idle worker
1631 * including the manager will not appear on @idle_list until rebind is
1632 * complete, making local wake-ups safe.
Tejun Heo25511a42012-07-17 12:39:27 -07001633 */
Tejun Heo94cf58b2013-01-24 11:01:33 -08001634static void rebind_workers(struct worker_pool *pool)
Tejun Heo25511a42012-07-17 12:39:27 -07001635{
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001636 struct worker *worker, *n;
Tejun Heo25511a42012-07-17 12:39:27 -07001637 int i;
1638
Tejun Heobc3a1af2013-03-13 19:47:39 -07001639 lockdep_assert_held(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08001640 lockdep_assert_held(&pool->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07001641
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001642 /* dequeue and kick idle ones */
Tejun Heo94cf58b2013-01-24 11:01:33 -08001643 list_for_each_entry_safe(worker, n, &pool->idle_list, entry) {
1644 /*
1645 * idle workers should be off @pool->idle_list until rebind
1646 * is complete to avoid receiving premature local wake-ups.
1647 */
1648 list_del_init(&worker->entry);
Lai Jiangshan96e65302012-09-02 00:28:19 +08001649
Tejun Heo94cf58b2013-01-24 11:01:33 -08001650 /*
1651 * worker_thread() will see the above dequeuing and call
1652 * idle_worker_rebind().
1653 */
1654 wake_up_process(worker->task);
1655 }
Tejun Heo25511a42012-07-17 12:39:27 -07001656
Tejun Heo94cf58b2013-01-24 11:01:33 -08001657 /* rebind busy workers */
Sasha Levinb67bfe02013-02-27 17:06:00 -08001658 for_each_busy_worker(worker, i, pool) {
Tejun Heo94cf58b2013-01-24 11:01:33 -08001659 struct work_struct *rebind_work = &worker->rebind_work;
1660 struct workqueue_struct *wq;
Tejun Heo25511a42012-07-17 12:39:27 -07001661
Tejun Heo94cf58b2013-01-24 11:01:33 -08001662 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
1663 work_data_bits(rebind_work)))
1664 continue;
Tejun Heo25511a42012-07-17 12:39:27 -07001665
Tejun Heo94cf58b2013-01-24 11:01:33 -08001666 debug_work_activate(rebind_work);
Tejun Heo90beca52012-09-04 23:12:33 -07001667
Tejun Heo94cf58b2013-01-24 11:01:33 -08001668 /*
1669 * wq doesn't really matter but let's keep @worker->pool
Tejun Heo112202d2013-02-13 19:29:12 -08001670 * and @pwq->pool consistent for sanity.
Tejun Heo94cf58b2013-01-24 11:01:33 -08001671 */
Tejun Heo7a4e3442013-03-12 11:30:00 -07001672 if (worker->pool->attrs->nice < 0)
Tejun Heo94cf58b2013-01-24 11:01:33 -08001673 wq = system_highpri_wq;
1674 else
1675 wq = system_wq;
Tejun Heoec588152012-09-04 23:16:32 -07001676
Tejun Heo7fb98ea2013-03-12 11:30:00 -07001677 insert_work(per_cpu_ptr(wq->cpu_pwqs, pool->cpu), rebind_work,
Tejun Heo94cf58b2013-01-24 11:01:33 -08001678 worker->scheduled.next,
1679 work_color_to_flags(WORK_NO_COLOR));
Tejun Heoec588152012-09-04 23:16:32 -07001680 }
Tejun Heo25511a42012-07-17 12:39:27 -07001681}
1682
Tejun Heoc34056a2010-06-29 10:07:11 +02001683static struct worker *alloc_worker(void)
1684{
1685 struct worker *worker;
1686
1687 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001688 if (worker) {
1689 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001690 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heo25511a42012-07-17 12:39:27 -07001691 INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn);
Tejun Heoe22bee72010-06-29 10:07:14 +02001692 /* on creation a worker is in !idle && prep state */
1693 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001694 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001695 return worker;
1696}
1697
1698/**
1699 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001700 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001701 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001702 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001703 * can be started by calling start_worker() or destroyed using
1704 * destroy_worker().
1705 *
1706 * CONTEXT:
1707 * Might sleep. Does GFP_KERNEL allocations.
1708 *
1709 * RETURNS:
1710 * Pointer to the newly created worker.
1711 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001712static struct worker *create_worker(struct worker_pool *pool)
Tejun Heoc34056a2010-06-29 10:07:11 +02001713{
Tejun Heo7a4e3442013-03-12 11:30:00 -07001714 const char *pri = pool->attrs->nice < 0 ? "H" : "";
Tejun Heoc34056a2010-06-29 10:07:11 +02001715 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001716 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001717
Tejun Heod565ed62013-01-24 11:01:33 -08001718 spin_lock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001719 while (ida_get_new(&pool->worker_ida, &id)) {
Tejun Heod565ed62013-01-24 11:01:33 -08001720 spin_unlock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001721 if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001722 goto fail;
Tejun Heod565ed62013-01-24 11:01:33 -08001723 spin_lock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001724 }
Tejun Heod565ed62013-01-24 11:01:33 -08001725 spin_unlock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001726
1727 worker = alloc_worker();
1728 if (!worker)
1729 goto fail;
1730
Tejun Heobd7bdd42012-07-12 14:46:37 -07001731 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001732 worker->id = id;
1733
Tejun Heo29c91e92013-03-12 11:30:03 -07001734 if (pool->cpu >= 0)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001735 worker->task = kthread_create_on_node(worker_thread,
Tejun Heoec22ca52013-01-24 11:01:33 -08001736 worker, cpu_to_node(pool->cpu),
Tejun Heod84ff052013-03-12 11:29:59 -07001737 "kworker/%d:%d%s", pool->cpu, id, pri);
Tejun Heof3421792010-07-02 10:03:51 +02001738 else
1739 worker->task = kthread_create(worker_thread, worker,
Tejun Heoac6104c2013-03-12 11:30:03 -07001740 "kworker/u%d:%d%s",
1741 pool->id, id, pri);
Tejun Heoc34056a2010-06-29 10:07:11 +02001742 if (IS_ERR(worker->task))
1743 goto fail;
1744
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001745 /*
1746 * set_cpus_allowed_ptr() will fail if the cpumask doesn't have any
1747 * online CPUs. It'll be re-applied when any of the CPUs come up.
1748 */
Tejun Heo7a4e3442013-03-12 11:30:00 -07001749 set_user_nice(worker->task, pool->attrs->nice);
1750 set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask);
Tejun Heo32704762012-07-13 22:16:45 -07001751
Tejun Heodb7bccf2010-06-29 10:07:12 +02001752 /*
Tejun Heo7a4e3442013-03-12 11:30:00 -07001753 * %PF_THREAD_BOUND is used to prevent userland from meddling with
1754 * cpumask of workqueue workers. This is an abuse. We need
1755 * %PF_NO_SETAFFINITY.
Tejun Heodb7bccf2010-06-29 10:07:12 +02001756 */
Tejun Heo7a4e3442013-03-12 11:30:00 -07001757 worker->task->flags |= PF_THREAD_BOUND;
1758
1759 /*
1760 * The caller is responsible for ensuring %POOL_DISASSOCIATED
1761 * remains stable across this function. See the comments above the
1762 * flag definition for details.
1763 */
1764 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001765 worker->flags |= WORKER_UNBOUND;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001766
Tejun Heoc34056a2010-06-29 10:07:11 +02001767 return worker;
1768fail:
1769 if (id >= 0) {
Tejun Heod565ed62013-01-24 11:01:33 -08001770 spin_lock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001771 ida_remove(&pool->worker_ida, id);
Tejun Heod565ed62013-01-24 11:01:33 -08001772 spin_unlock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001773 }
1774 kfree(worker);
1775 return NULL;
1776}
1777
1778/**
1779 * start_worker - start a newly created worker
1780 * @worker: worker to start
1781 *
Tejun Heo706026c2013-01-24 11:01:34 -08001782 * Make the pool aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001783 *
1784 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001785 * spin_lock_irq(pool->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001786 */
1787static void start_worker(struct worker *worker)
1788{
Tejun Heocb444762010-07-02 10:03:50 +02001789 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001790 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001791 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001792 wake_up_process(worker->task);
1793}
1794
1795/**
Tejun Heoebf44d12013-03-13 19:47:39 -07001796 * create_and_start_worker - create and start a worker for a pool
1797 * @pool: the target pool
1798 *
1799 * Create and start a new worker for @pool.
1800 */
1801static int create_and_start_worker(struct worker_pool *pool)
1802{
1803 struct worker *worker;
1804
1805 worker = create_worker(pool);
1806 if (worker) {
1807 spin_lock_irq(&pool->lock);
1808 start_worker(worker);
1809 spin_unlock_irq(&pool->lock);
1810 }
1811
1812 return worker ? 0 : -ENOMEM;
1813}
1814
1815/**
Tejun Heoc34056a2010-06-29 10:07:11 +02001816 * destroy_worker - destroy a workqueue worker
1817 * @worker: worker to be destroyed
1818 *
Tejun Heo706026c2013-01-24 11:01:34 -08001819 * Destroy @worker and adjust @pool stats accordingly.
Tejun Heoc8e55f32010-06-29 10:07:12 +02001820 *
1821 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001822 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001823 */
1824static void destroy_worker(struct worker *worker)
1825{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001826 struct worker_pool *pool = worker->pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001827 int id = worker->id;
1828
1829 /* sanity check frenzy */
Tejun Heo6183c002013-03-12 11:29:57 -07001830 if (WARN_ON(worker->current_work) ||
1831 WARN_ON(!list_empty(&worker->scheduled)))
1832 return;
Tejun Heoc34056a2010-06-29 10:07:11 +02001833
Tejun Heoc8e55f32010-06-29 10:07:12 +02001834 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001835 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001836 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001837 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001838
1839 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001840 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001841
Tejun Heod565ed62013-01-24 11:01:33 -08001842 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001843
Tejun Heoc34056a2010-06-29 10:07:11 +02001844 kthread_stop(worker->task);
1845 kfree(worker);
1846
Tejun Heod565ed62013-01-24 11:01:33 -08001847 spin_lock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001848 ida_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001849}
1850
Tejun Heo63d95a92012-07-12 14:46:37 -07001851static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001852{
Tejun Heo63d95a92012-07-12 14:46:37 -07001853 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001854
Tejun Heod565ed62013-01-24 11:01:33 -08001855 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001856
Tejun Heo63d95a92012-07-12 14:46:37 -07001857 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001858 struct worker *worker;
1859 unsigned long expires;
1860
1861 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001862 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001863 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1864
1865 if (time_before(jiffies, expires))
Tejun Heo63d95a92012-07-12 14:46:37 -07001866 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001867 else {
1868 /* it's been idle for too long, wake up manager */
Tejun Heo11ebea52012-07-12 14:46:37 -07001869 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -07001870 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001871 }
1872 }
1873
Tejun Heod565ed62013-01-24 11:01:33 -08001874 spin_unlock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001875}
1876
Tejun Heo493a1722013-03-12 11:29:59 -07001877static void send_mayday(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001878{
Tejun Heo112202d2013-02-13 19:29:12 -08001879 struct pool_workqueue *pwq = get_work_pwq(work);
1880 struct workqueue_struct *wq = pwq->wq;
Tejun Heo493a1722013-03-12 11:29:59 -07001881
1882 lockdep_assert_held(&workqueue_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001883
Tejun Heo493008a2013-03-12 11:30:03 -07001884 if (!wq->rescuer)
Tejun Heo493a1722013-03-12 11:29:59 -07001885 return;
Tejun Heoe22bee72010-06-29 10:07:14 +02001886
1887 /* mayday mayday mayday */
Tejun Heo493a1722013-03-12 11:29:59 -07001888 if (list_empty(&pwq->mayday_node)) {
1889 list_add_tail(&pwq->mayday_node, &wq->maydays);
Tejun Heoe22bee72010-06-29 10:07:14 +02001890 wake_up_process(wq->rescuer->task);
Tejun Heo493a1722013-03-12 11:29:59 -07001891 }
Tejun Heoe22bee72010-06-29 10:07:14 +02001892}
1893
Tejun Heo706026c2013-01-24 11:01:34 -08001894static void pool_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001895{
Tejun Heo63d95a92012-07-12 14:46:37 -07001896 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001897 struct work_struct *work;
1898
Tejun Heo493a1722013-03-12 11:29:59 -07001899 spin_lock_irq(&workqueue_lock); /* for wq->maydays */
1900 spin_lock(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001901
Tejun Heo63d95a92012-07-12 14:46:37 -07001902 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001903 /*
1904 * We've been trying to create a new worker but
1905 * haven't been successful. We might be hitting an
1906 * allocation deadlock. Send distress signals to
1907 * rescuers.
1908 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001909 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001910 send_mayday(work);
1911 }
1912
Tejun Heo493a1722013-03-12 11:29:59 -07001913 spin_unlock(&pool->lock);
1914 spin_unlock_irq(&workqueue_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001915
Tejun Heo63d95a92012-07-12 14:46:37 -07001916 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001917}
1918
1919/**
1920 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001921 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001922 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001923 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001924 * have at least one idle worker on return from this function. If
1925 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001926 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001927 * possible allocation deadlock.
1928 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001929 * On return, need_to_create_worker() is guaranteed to be %false and
1930 * may_start_working() %true.
Tejun Heoe22bee72010-06-29 10:07:14 +02001931 *
1932 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001933 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001934 * multiple times. Does GFP_KERNEL allocations. Called only from
1935 * manager.
1936 *
1937 * RETURNS:
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001938 * %false if no action was taken and pool->lock stayed locked, %true
Tejun Heoe22bee72010-06-29 10:07:14 +02001939 * otherwise.
1940 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001941static bool maybe_create_worker(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001942__releases(&pool->lock)
1943__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001944{
Tejun Heo63d95a92012-07-12 14:46:37 -07001945 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001946 return false;
1947restart:
Tejun Heod565ed62013-01-24 11:01:33 -08001948 spin_unlock_irq(&pool->lock);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001949
Tejun Heoe22bee72010-06-29 10:07:14 +02001950 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07001951 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001952
1953 while (true) {
1954 struct worker *worker;
1955
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001956 worker = create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001957 if (worker) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001958 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001959 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001960 start_worker(worker);
Tejun Heo6183c002013-03-12 11:29:57 -07001961 if (WARN_ON_ONCE(need_to_create_worker(pool)))
1962 goto restart;
Tejun Heoe22bee72010-06-29 10:07:14 +02001963 return true;
1964 }
1965
Tejun Heo63d95a92012-07-12 14:46:37 -07001966 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001967 break;
1968
Tejun Heoe22bee72010-06-29 10:07:14 +02001969 __set_current_state(TASK_INTERRUPTIBLE);
1970 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001971
Tejun Heo63d95a92012-07-12 14:46:37 -07001972 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001973 break;
1974 }
1975
Tejun Heo63d95a92012-07-12 14:46:37 -07001976 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001977 spin_lock_irq(&pool->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07001978 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001979 goto restart;
1980 return true;
1981}
1982
1983/**
1984 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo63d95a92012-07-12 14:46:37 -07001985 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001986 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001987 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02001988 * IDLE_WORKER_TIMEOUT.
1989 *
1990 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001991 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001992 * multiple times. Called only from manager.
1993 *
1994 * RETURNS:
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001995 * %false if no action was taken and pool->lock stayed locked, %true
Tejun Heoe22bee72010-06-29 10:07:14 +02001996 * otherwise.
1997 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001998static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001999{
2000 bool ret = false;
2001
Tejun Heo63d95a92012-07-12 14:46:37 -07002002 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02002003 struct worker *worker;
2004 unsigned long expires;
2005
Tejun Heo63d95a92012-07-12 14:46:37 -07002006 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02002007 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
2008
2009 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07002010 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02002011 break;
2012 }
2013
2014 destroy_worker(worker);
2015 ret = true;
2016 }
2017
2018 return ret;
2019}
2020
2021/**
2022 * manage_workers - manage worker pool
2023 * @worker: self
2024 *
Tejun Heo706026c2013-01-24 11:01:34 -08002025 * Assume the manager role and manage the worker pool @worker belongs
Tejun Heoe22bee72010-06-29 10:07:14 +02002026 * to. At any given time, there can be only zero or one manager per
Tejun Heo706026c2013-01-24 11:01:34 -08002027 * pool. The exclusion is handled automatically by this function.
Tejun Heoe22bee72010-06-29 10:07:14 +02002028 *
2029 * The caller can safely start processing works on false return. On
2030 * true return, it's guaranteed that need_to_create_worker() is false
2031 * and may_start_working() is true.
2032 *
2033 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002034 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02002035 * multiple times. Does GFP_KERNEL allocations.
2036 *
2037 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08002038 * spin_lock_irq(pool->lock) which may be released and regrabbed
2039 * multiple times. Does GFP_KERNEL allocations.
Tejun Heoe22bee72010-06-29 10:07:14 +02002040 */
2041static bool manage_workers(struct worker *worker)
2042{
Tejun Heo63d95a92012-07-12 14:46:37 -07002043 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002044 bool ret = false;
2045
Tejun Heobc3a1af2013-03-13 19:47:39 -07002046 /*
2047 * Managership is governed by two mutexes - manager_arb and
2048 * manager_mutex. manager_arb handles arbitration of manager role.
2049 * Anyone who successfully grabs manager_arb wins the arbitration
2050 * and becomes the manager. mutex_trylock() on pool->manager_arb
2051 * failure while holding pool->lock reliably indicates that someone
2052 * else is managing the pool and the worker which failed trylock
2053 * can proceed to executing work items. This means that anyone
2054 * grabbing manager_arb is responsible for actually performing
2055 * manager duties. If manager_arb is grabbed and released without
2056 * actual management, the pool may stall indefinitely.
2057 *
2058 * manager_mutex is used for exclusion of actual management
2059 * operations. The holder of manager_mutex can be sure that none
2060 * of management operations, including creation and destruction of
2061 * workers, won't take place until the mutex is released. Because
2062 * manager_mutex doesn't interfere with manager role arbitration,
2063 * it is guaranteed that the pool's management, while may be
2064 * delayed, won't be disturbed by someone else grabbing
2065 * manager_mutex.
2066 */
Tejun Heo34a06bd2013-03-12 11:30:00 -07002067 if (!mutex_trylock(&pool->manager_arb))
Tejun Heoe22bee72010-06-29 10:07:14 +02002068 return ret;
2069
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002070 /*
Tejun Heobc3a1af2013-03-13 19:47:39 -07002071 * With manager arbitration won, manager_mutex would be free in
2072 * most cases. trylock first without dropping @pool->lock.
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002073 */
Tejun Heobc3a1af2013-03-13 19:47:39 -07002074 if (unlikely(!mutex_trylock(&pool->manager_mutex))) {
Tejun Heod565ed62013-01-24 11:01:33 -08002075 spin_unlock_irq(&pool->lock);
Tejun Heobc3a1af2013-03-13 19:47:39 -07002076 mutex_lock(&pool->manager_mutex);
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002077 /*
2078 * CPU hotplug could have happened while we were waiting
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002079 * for assoc_mutex. Hotplug itself can't handle us
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002080 * because manager isn't either on idle or busy list, and
Tejun Heo706026c2013-01-24 11:01:34 -08002081 * @pool's state and ours could have deviated.
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002082 *
Tejun Heobc3a1af2013-03-13 19:47:39 -07002083 * As hotplug is now excluded via manager_mutex, we can
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002084 * simply try to bind. It will succeed or fail depending
Tejun Heo706026c2013-01-24 11:01:34 -08002085 * on @pool's current state. Try it and adjust
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002086 * %WORKER_UNBOUND accordingly.
2087 */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08002088 if (worker_maybe_bind_and_lock(pool))
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002089 worker->flags &= ~WORKER_UNBOUND;
2090 else
2091 worker->flags |= WORKER_UNBOUND;
2092
2093 ret = true;
2094 }
2095
Tejun Heo11ebea52012-07-12 14:46:37 -07002096 pool->flags &= ~POOL_MANAGE_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02002097
2098 /*
2099 * Destroy and then create so that may_start_working() is true
2100 * on return.
2101 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002102 ret |= maybe_destroy_workers(pool);
2103 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002104
Tejun Heobc3a1af2013-03-13 19:47:39 -07002105 mutex_unlock(&pool->manager_mutex);
Tejun Heo34a06bd2013-03-12 11:30:00 -07002106 mutex_unlock(&pool->manager_arb);
Tejun Heoe22bee72010-06-29 10:07:14 +02002107 return ret;
2108}
2109
Tejun Heoa62428c2010-06-29 10:07:10 +02002110/**
2111 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02002112 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02002113 * @work: work to process
2114 *
2115 * Process @work. This function contains all the logics necessary to
2116 * process a single work including synchronization against and
2117 * interaction with other workers on the same cpu, queueing and
2118 * flushing. As long as context requirement is met, any worker can
2119 * call this function to process a work.
2120 *
2121 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002122 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02002123 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002124static void process_one_work(struct worker *worker, struct work_struct *work)
Tejun Heod565ed62013-01-24 11:01:33 -08002125__releases(&pool->lock)
2126__acquires(&pool->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02002127{
Tejun Heo112202d2013-02-13 19:29:12 -08002128 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002129 struct worker_pool *pool = worker->pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002130 bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02002131 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02002132 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02002133#ifdef CONFIG_LOCKDEP
2134 /*
2135 * It is permissible to free the struct work_struct from
2136 * inside the function that is called from it, this we need to
2137 * take into account for lockdep too. To avoid bogus "held
2138 * lock freed" warnings as well as problems when looking into
2139 * work->lockdep_map, make a copy and use that here.
2140 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07002141 struct lockdep_map lockdep_map;
2142
2143 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002144#endif
Tejun Heo6fec10a2012-07-22 10:16:34 -07002145 /*
2146 * Ensure we're on the correct CPU. DISASSOCIATED test is
2147 * necessary to avoid spurious warnings from rescuers servicing the
Tejun Heo24647572013-01-24 11:01:33 -08002148 * unbound or a disassociated pool.
Tejun Heo6fec10a2012-07-22 10:16:34 -07002149 */
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002150 WARN_ON_ONCE(!(worker->flags & WORKER_UNBOUND) &&
Tejun Heo24647572013-01-24 11:01:33 -08002151 !(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heoec22ca52013-01-24 11:01:33 -08002152 raw_smp_processor_id() != pool->cpu);
Tejun Heo25511a42012-07-17 12:39:27 -07002153
Tejun Heo7e116292010-06-29 10:07:13 +02002154 /*
2155 * A single work shouldn't be executed concurrently by
2156 * multiple workers on a single cpu. Check whether anyone is
2157 * already processing the work. If so, defer the work to the
2158 * currently executing one.
2159 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002160 collision = find_worker_executing_work(pool, work);
Tejun Heo7e116292010-06-29 10:07:13 +02002161 if (unlikely(collision)) {
2162 move_linked_works(work, &collision->scheduled, NULL);
2163 return;
2164 }
2165
Tejun Heo8930cab2012-08-03 10:30:45 -07002166 /* claim and dequeue */
Tejun Heoa62428c2010-06-29 10:07:10 +02002167 debug_work_deactivate(work);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002168 hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work);
Tejun Heoc34056a2010-06-29 10:07:11 +02002169 worker->current_work = work;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002170 worker->current_func = work->func;
Tejun Heo112202d2013-02-13 19:29:12 -08002171 worker->current_pwq = pwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002172 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002173
Tejun Heoa62428c2010-06-29 10:07:10 +02002174 list_del_init(&work->entry);
2175
Tejun Heo649027d2010-06-29 10:07:14 +02002176 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02002177 * CPU intensive works don't participate in concurrency
2178 * management. They're the scheduler's responsibility.
2179 */
2180 if (unlikely(cpu_intensive))
2181 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
2182
Tejun Heo974271c2012-07-12 14:46:37 -07002183 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002184 * Unbound pool isn't concurrency managed and work items should be
Tejun Heo974271c2012-07-12 14:46:37 -07002185 * executed ASAP. Wake up another worker if necessary.
2186 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002187 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
2188 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07002189
Tejun Heo8930cab2012-08-03 10:30:45 -07002190 /*
Tejun Heo7c3eed52013-01-24 11:01:33 -08002191 * Record the last pool and clear PENDING which should be the last
Tejun Heod565ed62013-01-24 11:01:33 -08002192 * update to @work. Also, do this inside @pool->lock so that
Tejun Heo23657bb2012-08-13 17:08:19 -07002193 * PENDING and queued state changes happen together while IRQ is
2194 * disabled.
Tejun Heo8930cab2012-08-03 10:30:45 -07002195 */
Tejun Heo7c3eed52013-01-24 11:01:33 -08002196 set_work_pool_and_clear_pending(work, pool->id);
Tejun Heoa62428c2010-06-29 10:07:10 +02002197
Tejun Heod565ed62013-01-24 11:01:33 -08002198 spin_unlock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002199
Tejun Heo112202d2013-02-13 19:29:12 -08002200 lock_map_acquire_read(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002201 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002202 trace_workqueue_execute_start(work);
Tejun Heoa2c1c572012-12-18 10:35:02 -08002203 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002204 /*
2205 * While we must be careful to not use "work" after this, the trace
2206 * point will only record its address.
2207 */
2208 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002209 lock_map_release(&lockdep_map);
Tejun Heo112202d2013-02-13 19:29:12 -08002210 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002211
2212 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Valentin Ilie044c7822012-08-19 00:52:42 +03002213 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
2214 " last function: %pf\n",
Tejun Heoa2c1c572012-12-18 10:35:02 -08002215 current->comm, preempt_count(), task_pid_nr(current),
2216 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02002217 debug_show_held_locks(current);
2218 dump_stack();
2219 }
2220
Tejun Heod565ed62013-01-24 11:01:33 -08002221 spin_lock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002222
Tejun Heofb0e7be2010-06-29 10:07:15 +02002223 /* clear cpu intensive status */
2224 if (unlikely(cpu_intensive))
2225 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2226
Tejun Heoa62428c2010-06-29 10:07:10 +02002227 /* we're done with it, release */
Sasha Levin42f85702012-12-17 10:01:23 -05002228 hash_del(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002229 worker->current_work = NULL;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002230 worker->current_func = NULL;
Tejun Heo112202d2013-02-13 19:29:12 -08002231 worker->current_pwq = NULL;
2232 pwq_dec_nr_in_flight(pwq, work_color);
Tejun Heoa62428c2010-06-29 10:07:10 +02002233}
2234
Tejun Heoaffee4b2010-06-29 10:07:12 +02002235/**
2236 * process_scheduled_works - process scheduled works
2237 * @worker: self
2238 *
2239 * Process all scheduled works. Please note that the scheduled list
2240 * may change while processing a work, so this function repeatedly
2241 * fetches a work from the top and executes it.
2242 *
2243 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002244 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02002245 * multiple times.
2246 */
2247static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002249 while (!list_empty(&worker->scheduled)) {
2250 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002252 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254}
2255
Tejun Heo4690c4a2010-06-29 10:07:10 +02002256/**
2257 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02002258 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02002259 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002260 * The worker thread function. All workers belong to a worker_pool -
2261 * either a per-cpu one or dynamic unbound one. These workers process all
2262 * work items regardless of their specific target workqueue. The only
2263 * exception is work items which belong to workqueues with a rescuer which
2264 * will be explained in rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02002265 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002266static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267{
Tejun Heoc34056a2010-06-29 10:07:11 +02002268 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07002269 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
Tejun Heoe22bee72010-06-29 10:07:14 +02002271 /* tell the scheduler that this is a workqueue worker */
2272 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002273woke_up:
Tejun Heod565ed62013-01-24 11:01:33 -08002274 spin_lock_irq(&pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002276 /* we are off idle list if destruction or rebind is requested */
2277 if (unlikely(list_empty(&worker->entry))) {
Tejun Heod565ed62013-01-24 11:01:33 -08002278 spin_unlock_irq(&pool->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07002279
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002280 /* if DIE is set, destruction is requested */
Tejun Heo25511a42012-07-17 12:39:27 -07002281 if (worker->flags & WORKER_DIE) {
2282 worker->task->flags &= ~PF_WQ_WORKER;
2283 return 0;
2284 }
2285
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002286 /* otherwise, rebind */
Tejun Heo25511a42012-07-17 12:39:27 -07002287 idle_worker_rebind(worker);
2288 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 }
2290
Tejun Heoc8e55f32010-06-29 10:07:12 +02002291 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002292recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002293 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002294 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002295 goto sleep;
2296
2297 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002298 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002299 goto recheck;
2300
Tejun Heoc8e55f32010-06-29 10:07:12 +02002301 /*
2302 * ->scheduled list can only be filled while a worker is
2303 * preparing to process a work or actually processing it.
2304 * Make sure nobody diddled with it while I was sleeping.
2305 */
Tejun Heo6183c002013-03-12 11:29:57 -07002306 WARN_ON_ONCE(!list_empty(&worker->scheduled));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002307
Tejun Heoe22bee72010-06-29 10:07:14 +02002308 /*
2309 * When control reaches this point, we're guaranteed to have
2310 * at least one idle worker or that someone else has already
2311 * assumed the manager role.
2312 */
2313 worker_clr_flags(worker, WORKER_PREP);
2314
2315 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002316 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002317 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002318 struct work_struct, entry);
2319
2320 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2321 /* optimization path, not strictly necessary */
2322 process_one_work(worker, work);
2323 if (unlikely(!list_empty(&worker->scheduled)))
2324 process_scheduled_works(worker);
2325 } else {
2326 move_linked_works(work, &worker->scheduled, NULL);
2327 process_scheduled_works(worker);
2328 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002329 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002330
Tejun Heoe22bee72010-06-29 10:07:14 +02002331 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002332sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07002333 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002334 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002335
Tejun Heoc8e55f32010-06-29 10:07:12 +02002336 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002337 * pool->lock is held and there's no work to process and no need to
2338 * manage, sleep. Workers are woken up only while holding
2339 * pool->lock or from local cpu, so setting the current state
2340 * before releasing pool->lock is enough to prevent losing any
2341 * event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002342 */
2343 worker_enter_idle(worker);
2344 __set_current_state(TASK_INTERRUPTIBLE);
Tejun Heod565ed62013-01-24 11:01:33 -08002345 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02002346 schedule();
2347 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348}
2349
Tejun Heoe22bee72010-06-29 10:07:14 +02002350/**
2351 * rescuer_thread - the rescuer thread function
Tejun Heo111c2252013-01-17 17:16:24 -08002352 * @__rescuer: self
Tejun Heoe22bee72010-06-29 10:07:14 +02002353 *
2354 * Workqueue rescuer thread function. There's one rescuer for each
Tejun Heo493008a2013-03-12 11:30:03 -07002355 * workqueue which has WQ_MEM_RECLAIM set.
Tejun Heoe22bee72010-06-29 10:07:14 +02002356 *
Tejun Heo706026c2013-01-24 11:01:34 -08002357 * Regular work processing on a pool may block trying to create a new
Tejun Heoe22bee72010-06-29 10:07:14 +02002358 * worker which uses GFP_KERNEL allocation which has slight chance of
2359 * developing into deadlock if some works currently on the same queue
2360 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2361 * the problem rescuer solves.
2362 *
Tejun Heo706026c2013-01-24 11:01:34 -08002363 * When such condition is possible, the pool summons rescuers of all
2364 * workqueues which have works queued on the pool and let them process
Tejun Heoe22bee72010-06-29 10:07:14 +02002365 * those works so that forward progress can be guaranteed.
2366 *
2367 * This should happen rarely.
2368 */
Tejun Heo111c2252013-01-17 17:16:24 -08002369static int rescuer_thread(void *__rescuer)
Tejun Heoe22bee72010-06-29 10:07:14 +02002370{
Tejun Heo111c2252013-01-17 17:16:24 -08002371 struct worker *rescuer = __rescuer;
2372 struct workqueue_struct *wq = rescuer->rescue_wq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002373 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heoe22bee72010-06-29 10:07:14 +02002374
2375 set_user_nice(current, RESCUER_NICE_LEVEL);
Tejun Heo111c2252013-01-17 17:16:24 -08002376
2377 /*
2378 * Mark rescuer as worker too. As WORKER_PREP is never cleared, it
2379 * doesn't participate in concurrency management.
2380 */
2381 rescuer->task->flags |= PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002382repeat:
2383 set_current_state(TASK_INTERRUPTIBLE);
2384
Mike Galbraith412d32e2012-11-28 07:17:18 +01002385 if (kthread_should_stop()) {
2386 __set_current_state(TASK_RUNNING);
Tejun Heo111c2252013-01-17 17:16:24 -08002387 rescuer->task->flags &= ~PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002388 return 0;
Mike Galbraith412d32e2012-11-28 07:17:18 +01002389 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002390
Tejun Heo493a1722013-03-12 11:29:59 -07002391 /* see whether any pwq is asking for help */
2392 spin_lock_irq(&workqueue_lock);
2393
2394 while (!list_empty(&wq->maydays)) {
2395 struct pool_workqueue *pwq = list_first_entry(&wq->maydays,
2396 struct pool_workqueue, mayday_node);
Tejun Heo112202d2013-02-13 19:29:12 -08002397 struct worker_pool *pool = pwq->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002398 struct work_struct *work, *n;
2399
2400 __set_current_state(TASK_RUNNING);
Tejun Heo493a1722013-03-12 11:29:59 -07002401 list_del_init(&pwq->mayday_node);
2402
2403 spin_unlock_irq(&workqueue_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002404
2405 /* migrate to the target cpu if possible */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08002406 worker_maybe_bind_and_lock(pool);
Lai Jiangshanb3104102013-02-19 12:17:02 -08002407 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002408
2409 /*
2410 * Slurp in all works issued via this workqueue and
2411 * process'em.
2412 */
Tejun Heo6183c002013-03-12 11:29:57 -07002413 WARN_ON_ONCE(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002414 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heo112202d2013-02-13 19:29:12 -08002415 if (get_work_pwq(work) == pwq)
Tejun Heoe22bee72010-06-29 10:07:14 +02002416 move_linked_works(work, scheduled, &n);
2417
2418 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002419
2420 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002421 * Leave this pool. If keep_working() is %true, notify a
Tejun Heo75769582011-02-14 14:04:46 +01002422 * regular worker; otherwise, we end up with 0 concurrency
2423 * and stalling the execution.
2424 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002425 if (keep_working(pool))
2426 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002427
Lai Jiangshanb3104102013-02-19 12:17:02 -08002428 rescuer->pool = NULL;
Tejun Heo493a1722013-03-12 11:29:59 -07002429 spin_unlock(&pool->lock);
2430 spin_lock(&workqueue_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002431 }
2432
Tejun Heo493a1722013-03-12 11:29:59 -07002433 spin_unlock_irq(&workqueue_lock);
2434
Tejun Heo111c2252013-01-17 17:16:24 -08002435 /* rescuers should never participate in concurrency management */
2436 WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
Tejun Heoe22bee72010-06-29 10:07:14 +02002437 schedule();
2438 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439}
2440
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002441struct wq_barrier {
2442 struct work_struct work;
2443 struct completion done;
2444};
2445
2446static void wq_barrier_func(struct work_struct *work)
2447{
2448 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2449 complete(&barr->done);
2450}
2451
Tejun Heo4690c4a2010-06-29 10:07:10 +02002452/**
2453 * insert_wq_barrier - insert a barrier work
Tejun Heo112202d2013-02-13 19:29:12 -08002454 * @pwq: pwq to insert barrier into
Tejun Heo4690c4a2010-06-29 10:07:10 +02002455 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002456 * @target: target work to attach @barr to
2457 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002458 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002459 * @barr is linked to @target such that @barr is completed only after
2460 * @target finishes execution. Please note that the ordering
2461 * guarantee is observed only with respect to @target and on the local
2462 * cpu.
2463 *
2464 * Currently, a queued barrier can't be canceled. This is because
2465 * try_to_grab_pending() can't determine whether the work to be
2466 * grabbed is at the head of the queue and thus can't clear LINKED
2467 * flag of the previous work while there must be a valid next work
2468 * after a work with LINKED flag set.
2469 *
2470 * Note that when @worker is non-NULL, @target may be modified
Tejun Heo112202d2013-02-13 19:29:12 -08002471 * underneath us, so we can't reliably determine pwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002472 *
2473 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002474 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002475 */
Tejun Heo112202d2013-02-13 19:29:12 -08002476static void insert_wq_barrier(struct pool_workqueue *pwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002477 struct wq_barrier *barr,
2478 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002479{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002480 struct list_head *head;
2481 unsigned int linked = 0;
2482
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002483 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002484 * debugobject calls are safe here even with pool->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002485 * as we know for sure that this will not trigger any of the
2486 * checks and call back into the fixup functions where we
2487 * might deadlock.
2488 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002489 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002490 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002491 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002492
Tejun Heoaffee4b2010-06-29 10:07:12 +02002493 /*
2494 * If @target is currently being executed, schedule the
2495 * barrier to the worker; otherwise, put it after @target.
2496 */
2497 if (worker)
2498 head = worker->scheduled.next;
2499 else {
2500 unsigned long *bits = work_data_bits(target);
2501
2502 head = target->entry.next;
2503 /* there can already be other linked works, inherit and set */
2504 linked = *bits & WORK_STRUCT_LINKED;
2505 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2506 }
2507
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002508 debug_work_activate(&barr->work);
Tejun Heo112202d2013-02-13 19:29:12 -08002509 insert_work(pwq, &barr->work, head,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002510 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002511}
2512
Tejun Heo73f53c42010-06-29 10:07:11 +02002513/**
Tejun Heo112202d2013-02-13 19:29:12 -08002514 * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing
Tejun Heo73f53c42010-06-29 10:07:11 +02002515 * @wq: workqueue being flushed
2516 * @flush_color: new flush color, < 0 for no-op
2517 * @work_color: new work color, < 0 for no-op
2518 *
Tejun Heo112202d2013-02-13 19:29:12 -08002519 * Prepare pwqs for workqueue flushing.
Tejun Heo73f53c42010-06-29 10:07:11 +02002520 *
Tejun Heo112202d2013-02-13 19:29:12 -08002521 * If @flush_color is non-negative, flush_color on all pwqs should be
2522 * -1. If no pwq has in-flight commands at the specified color, all
2523 * pwq->flush_color's stay at -1 and %false is returned. If any pwq
2524 * has in flight commands, its pwq->flush_color is set to
2525 * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq
Tejun Heo73f53c42010-06-29 10:07:11 +02002526 * wakeup logic is armed and %true is returned.
2527 *
2528 * The caller should have initialized @wq->first_flusher prior to
2529 * calling this function with non-negative @flush_color. If
2530 * @flush_color is negative, no flush color update is done and %false
2531 * is returned.
2532 *
Tejun Heo112202d2013-02-13 19:29:12 -08002533 * If @work_color is non-negative, all pwqs should have the same
Tejun Heo73f53c42010-06-29 10:07:11 +02002534 * work_color which is previous to @work_color and all will be
2535 * advanced to @work_color.
2536 *
2537 * CONTEXT:
2538 * mutex_lock(wq->flush_mutex).
2539 *
2540 * RETURNS:
2541 * %true if @flush_color >= 0 and there's something to flush. %false
2542 * otherwise.
2543 */
Tejun Heo112202d2013-02-13 19:29:12 -08002544static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
Tejun Heo73f53c42010-06-29 10:07:11 +02002545 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546{
Tejun Heo73f53c42010-06-29 10:07:11 +02002547 bool wait = false;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002548 struct pool_workqueue *pwq;
Oleg Nesterov14441962007-05-23 13:57:57 -07002549
Tejun Heo73f53c42010-06-29 10:07:11 +02002550 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002551 WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush));
Tejun Heo112202d2013-02-13 19:29:12 -08002552 atomic_set(&wq->nr_pwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002553 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002554
Tejun Heo76af4d92013-03-12 11:30:00 -07002555 local_irq_disable();
2556
Tejun Heo49e3cf42013-03-12 11:29:58 -07002557 for_each_pwq(pwq, wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08002558 struct worker_pool *pool = pwq->pool;
Tejun Heo73f53c42010-06-29 10:07:11 +02002559
Tejun Heo76af4d92013-03-12 11:30:00 -07002560 spin_lock(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002561
2562 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002563 WARN_ON_ONCE(pwq->flush_color != -1);
Tejun Heo73f53c42010-06-29 10:07:11 +02002564
Tejun Heo112202d2013-02-13 19:29:12 -08002565 if (pwq->nr_in_flight[flush_color]) {
2566 pwq->flush_color = flush_color;
2567 atomic_inc(&wq->nr_pwqs_to_flush);
Tejun Heo73f53c42010-06-29 10:07:11 +02002568 wait = true;
2569 }
2570 }
2571
2572 if (work_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002573 WARN_ON_ONCE(work_color != work_next_color(pwq->work_color));
Tejun Heo112202d2013-02-13 19:29:12 -08002574 pwq->work_color = work_color;
Tejun Heo73f53c42010-06-29 10:07:11 +02002575 }
2576
Tejun Heo76af4d92013-03-12 11:30:00 -07002577 spin_unlock(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002578 }
2579
Tejun Heo76af4d92013-03-12 11:30:00 -07002580 local_irq_enable();
2581
Tejun Heo112202d2013-02-13 19:29:12 -08002582 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush))
Tejun Heo73f53c42010-06-29 10:07:11 +02002583 complete(&wq->first_flusher->done);
2584
2585 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586}
2587
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002588/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002590 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002592 * This function sleeps until all work items which were queued on entry
2593 * have finished execution, but it is not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002595void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596{
Tejun Heo73f53c42010-06-29 10:07:11 +02002597 struct wq_flusher this_flusher = {
2598 .list = LIST_HEAD_INIT(this_flusher.list),
2599 .flush_color = -1,
2600 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2601 };
2602 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002603
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002604 lock_map_acquire(&wq->lockdep_map);
2605 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002606
2607 mutex_lock(&wq->flush_mutex);
2608
2609 /*
2610 * Start-to-wait phase
2611 */
2612 next_color = work_next_color(wq->work_color);
2613
2614 if (next_color != wq->flush_color) {
2615 /*
2616 * Color space is not full. The current work_color
2617 * becomes our flush_color and work_color is advanced
2618 * by one.
2619 */
Tejun Heo6183c002013-03-12 11:29:57 -07002620 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow));
Tejun Heo73f53c42010-06-29 10:07:11 +02002621 this_flusher.flush_color = wq->work_color;
2622 wq->work_color = next_color;
2623
2624 if (!wq->first_flusher) {
2625 /* no flush in progress, become the first flusher */
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
2628 wq->first_flusher = &this_flusher;
2629
Tejun Heo112202d2013-02-13 19:29:12 -08002630 if (!flush_workqueue_prep_pwqs(wq, wq->flush_color,
Tejun Heo73f53c42010-06-29 10:07:11 +02002631 wq->work_color)) {
2632 /* nothing to flush, done */
2633 wq->flush_color = next_color;
2634 wq->first_flusher = NULL;
2635 goto out_unlock;
2636 }
2637 } else {
2638 /* wait in queue */
Tejun Heo6183c002013-03-12 11:29:57 -07002639 WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002640 list_add_tail(&this_flusher.list, &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002641 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002642 }
2643 } else {
2644 /*
2645 * Oops, color space is full, wait on overflow queue.
2646 * The next flush completion will assign us
2647 * flush_color and transfer to flusher_queue.
2648 */
2649 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2650 }
2651
2652 mutex_unlock(&wq->flush_mutex);
2653
2654 wait_for_completion(&this_flusher.done);
2655
2656 /*
2657 * Wake-up-and-cascade phase
2658 *
2659 * First flushers are responsible for cascading flushes and
2660 * handling overflow. Non-first flushers can simply return.
2661 */
2662 if (wq->first_flusher != &this_flusher)
2663 return;
2664
2665 mutex_lock(&wq->flush_mutex);
2666
Tejun Heo4ce48b32010-07-02 10:03:51 +02002667 /* we might have raced, check again with mutex held */
2668 if (wq->first_flusher != &this_flusher)
2669 goto out_unlock;
2670
Tejun Heo73f53c42010-06-29 10:07:11 +02002671 wq->first_flusher = NULL;
2672
Tejun Heo6183c002013-03-12 11:29:57 -07002673 WARN_ON_ONCE(!list_empty(&this_flusher.list));
2674 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002675
2676 while (true) {
2677 struct wq_flusher *next, *tmp;
2678
2679 /* complete all the flushers sharing the current flush color */
2680 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2681 if (next->flush_color != wq->flush_color)
2682 break;
2683 list_del_init(&next->list);
2684 complete(&next->done);
2685 }
2686
Tejun Heo6183c002013-03-12 11:29:57 -07002687 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) &&
2688 wq->flush_color != work_next_color(wq->work_color));
Tejun Heo73f53c42010-06-29 10:07:11 +02002689
2690 /* this flush_color is finished, advance by one */
2691 wq->flush_color = work_next_color(wq->flush_color);
2692
2693 /* one color has been freed, handle overflow queue */
2694 if (!list_empty(&wq->flusher_overflow)) {
2695 /*
2696 * Assign the same color to all overflowed
2697 * flushers, advance work_color and append to
2698 * flusher_queue. This is the start-to-wait
2699 * phase for these overflowed flushers.
2700 */
2701 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2702 tmp->flush_color = wq->work_color;
2703
2704 wq->work_color = work_next_color(wq->work_color);
2705
2706 list_splice_tail_init(&wq->flusher_overflow,
2707 &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002708 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002709 }
2710
2711 if (list_empty(&wq->flusher_queue)) {
Tejun Heo6183c002013-03-12 11:29:57 -07002712 WARN_ON_ONCE(wq->flush_color != wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002713 break;
2714 }
2715
2716 /*
2717 * Need to flush more colors. Make the next flusher
Tejun Heo112202d2013-02-13 19:29:12 -08002718 * the new first flusher and arm pwqs.
Tejun Heo73f53c42010-06-29 10:07:11 +02002719 */
Tejun Heo6183c002013-03-12 11:29:57 -07002720 WARN_ON_ONCE(wq->flush_color == wq->work_color);
2721 WARN_ON_ONCE(wq->flush_color != next->flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002722
2723 list_del_init(&next->list);
2724 wq->first_flusher = next;
2725
Tejun Heo112202d2013-02-13 19:29:12 -08002726 if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1))
Tejun Heo73f53c42010-06-29 10:07:11 +02002727 break;
2728
2729 /*
2730 * Meh... this color is already done, clear first
2731 * flusher and repeat cascading.
2732 */
2733 wq->first_flusher = NULL;
2734 }
2735
2736out_unlock:
2737 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738}
Dave Jonesae90dd52006-06-30 01:40:45 -04002739EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002741/**
2742 * drain_workqueue - drain a workqueue
2743 * @wq: workqueue to drain
2744 *
2745 * Wait until the workqueue becomes empty. While draining is in progress,
2746 * only chain queueing is allowed. IOW, only currently pending or running
2747 * work items on @wq can queue further work items on it. @wq is flushed
2748 * repeatedly until it becomes empty. The number of flushing is detemined
2749 * by the depth of chaining and should be relatively short. Whine if it
2750 * takes too long.
2751 */
2752void drain_workqueue(struct workqueue_struct *wq)
2753{
2754 unsigned int flush_cnt = 0;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002755 struct pool_workqueue *pwq;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002756
2757 /*
2758 * __queue_work() needs to test whether there are drainers, is much
2759 * hotter than drain_workqueue() and already looks at @wq->flags.
Tejun Heo618b01e2013-03-12 11:30:04 -07002760 * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers.
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002761 */
Tejun Heoe98d5b12013-03-12 11:29:57 -07002762 spin_lock_irq(&workqueue_lock);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002763 if (!wq->nr_drainers++)
Tejun Heo618b01e2013-03-12 11:30:04 -07002764 wq->flags |= __WQ_DRAINING;
Tejun Heoe98d5b12013-03-12 11:29:57 -07002765 spin_unlock_irq(&workqueue_lock);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002766reflush:
2767 flush_workqueue(wq);
2768
Tejun Heo76af4d92013-03-12 11:30:00 -07002769 local_irq_disable();
2770
Tejun Heo49e3cf42013-03-12 11:29:58 -07002771 for_each_pwq(pwq, wq) {
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002772 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002773
Tejun Heo76af4d92013-03-12 11:30:00 -07002774 spin_lock(&pwq->pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08002775 drained = !pwq->nr_active && list_empty(&pwq->delayed_works);
Tejun Heo76af4d92013-03-12 11:30:00 -07002776 spin_unlock(&pwq->pool->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002777
2778 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002779 continue;
2780
2781 if (++flush_cnt == 10 ||
2782 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002783 pr_warn("workqueue %s: drain_workqueue() isn't complete after %u tries\n",
Valentin Ilie044c7822012-08-19 00:52:42 +03002784 wq->name, flush_cnt);
Tejun Heo76af4d92013-03-12 11:30:00 -07002785
2786 local_irq_enable();
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002787 goto reflush;
2788 }
2789
Tejun Heo76af4d92013-03-12 11:30:00 -07002790 spin_lock(&workqueue_lock);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002791 if (!--wq->nr_drainers)
Tejun Heo618b01e2013-03-12 11:30:04 -07002792 wq->flags &= ~__WQ_DRAINING;
Tejun Heo76af4d92013-03-12 11:30:00 -07002793 spin_unlock(&workqueue_lock);
2794
2795 local_irq_enable();
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002796}
2797EXPORT_SYMBOL_GPL(drain_workqueue);
2798
Tejun Heo606a5022012-08-20 14:51:23 -07002799static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
Tejun Heobaf59022010-09-16 10:42:16 +02002800{
2801 struct worker *worker = NULL;
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002802 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002803 struct pool_workqueue *pwq;
Tejun Heobaf59022010-09-16 10:42:16 +02002804
2805 might_sleep();
Tejun Heobaf59022010-09-16 10:42:16 +02002806
Tejun Heofa1b54e2013-03-12 11:30:00 -07002807 local_irq_disable();
2808 pool = get_work_pool(work);
2809 if (!pool) {
2810 local_irq_enable();
2811 return false;
2812 }
2813
2814 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08002815 /* see the comment in try_to_grab_pending() with the same code */
Tejun Heo112202d2013-02-13 19:29:12 -08002816 pwq = get_work_pwq(work);
2817 if (pwq) {
2818 if (unlikely(pwq->pool != pool))
Tejun Heobaf59022010-09-16 10:42:16 +02002819 goto already_gone;
Tejun Heo606a5022012-08-20 14:51:23 -07002820 } else {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002821 worker = find_worker_executing_work(pool, work);
Tejun Heobaf59022010-09-16 10:42:16 +02002822 if (!worker)
2823 goto already_gone;
Tejun Heo112202d2013-02-13 19:29:12 -08002824 pwq = worker->current_pwq;
Tejun Heo606a5022012-08-20 14:51:23 -07002825 }
Tejun Heobaf59022010-09-16 10:42:16 +02002826
Tejun Heo112202d2013-02-13 19:29:12 -08002827 insert_wq_barrier(pwq, barr, work, worker);
Tejun Heod565ed62013-01-24 11:01:33 -08002828 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002829
Tejun Heoe1594892011-01-09 23:32:15 +01002830 /*
2831 * If @max_active is 1 or rescuer is in use, flushing another work
2832 * item on the same workqueue may lead to deadlock. Make sure the
2833 * flusher is not running on the same workqueue by verifying write
2834 * access.
2835 */
Tejun Heo493008a2013-03-12 11:30:03 -07002836 if (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)
Tejun Heo112202d2013-02-13 19:29:12 -08002837 lock_map_acquire(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002838 else
Tejun Heo112202d2013-02-13 19:29:12 -08002839 lock_map_acquire_read(&pwq->wq->lockdep_map);
2840 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002841
Tejun Heobaf59022010-09-16 10:42:16 +02002842 return true;
2843already_gone:
Tejun Heod565ed62013-01-24 11:01:33 -08002844 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002845 return false;
2846}
2847
Oleg Nesterovdb700892008-07-25 01:47:49 -07002848/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002849 * flush_work - wait for a work to finish executing the last queueing instance
2850 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002851 *
Tejun Heo606a5022012-08-20 14:51:23 -07002852 * Wait until @work has finished execution. @work is guaranteed to be idle
2853 * on return if it hasn't been requeued since flush started.
Tejun Heo401a8d02010-09-16 10:36:00 +02002854 *
2855 * RETURNS:
2856 * %true if flush_work() waited for the work to finish execution,
2857 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002858 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002859bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002860{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002861 struct wq_barrier barr;
2862
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002863 lock_map_acquire(&work->lockdep_map);
2864 lock_map_release(&work->lockdep_map);
2865
Tejun Heo606a5022012-08-20 14:51:23 -07002866 if (start_flush_work(work, &barr)) {
Tejun Heobaf59022010-09-16 10:42:16 +02002867 wait_for_completion(&barr.done);
2868 destroy_work_on_stack(&barr.work);
2869 return true;
Tejun Heo606a5022012-08-20 14:51:23 -07002870 } else {
Tejun Heobaf59022010-09-16 10:42:16 +02002871 return false;
Tejun Heo606a5022012-08-20 14:51:23 -07002872 }
Oleg Nesterovdb700892008-07-25 01:47:49 -07002873}
2874EXPORT_SYMBOL_GPL(flush_work);
2875
Tejun Heo36e227d2012-08-03 10:30:46 -07002876static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
Tejun Heo401a8d02010-09-16 10:36:00 +02002877{
Tejun Heobbb68df2012-08-03 10:30:46 -07002878 unsigned long flags;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002879 int ret;
2880
2881 do {
Tejun Heobbb68df2012-08-03 10:30:46 -07002882 ret = try_to_grab_pending(work, is_dwork, &flags);
2883 /*
2884 * If someone else is canceling, wait for the same event it
2885 * would be waiting for before retrying.
2886 */
2887 if (unlikely(ret == -ENOENT))
Tejun Heo606a5022012-08-20 14:51:23 -07002888 flush_work(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002889 } while (unlikely(ret < 0));
2890
Tejun Heobbb68df2012-08-03 10:30:46 -07002891 /* tell other tasks trying to grab @work to back off */
2892 mark_work_canceling(work);
2893 local_irq_restore(flags);
2894
Tejun Heo606a5022012-08-20 14:51:23 -07002895 flush_work(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002896 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002897 return ret;
2898}
2899
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002900/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002901 * cancel_work_sync - cancel a work and wait for it to finish
2902 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002903 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002904 * Cancel @work and wait for its execution to finish. This function
2905 * can be used even if the work re-queues itself or migrates to
2906 * another workqueue. On return from this function, @work is
2907 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002908 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002909 * cancel_work_sync(&delayed_work->work) must not be used for
2910 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002911 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002912 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002913 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002914 *
2915 * RETURNS:
2916 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002917 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002918bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002919{
Tejun Heo36e227d2012-08-03 10:30:46 -07002920 return __cancel_work_timer(work, false);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002921}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002922EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002923
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002924/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002925 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2926 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002927 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002928 * Delayed timer is cancelled and the pending work is queued for
2929 * immediate execution. Like flush_work(), this function only
2930 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002931 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002932 * RETURNS:
2933 * %true if flush_work() waited for the work to finish execution,
2934 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002935 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002936bool flush_delayed_work(struct delayed_work *dwork)
2937{
Tejun Heo8930cab2012-08-03 10:30:45 -07002938 local_irq_disable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002939 if (del_timer_sync(&dwork->timer))
Lai Jiangshan60c057b2013-02-06 18:04:53 -08002940 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Tejun Heo8930cab2012-08-03 10:30:45 -07002941 local_irq_enable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002942 return flush_work(&dwork->work);
2943}
2944EXPORT_SYMBOL(flush_delayed_work);
2945
2946/**
Tejun Heo57b30ae2012-08-21 13:18:24 -07002947 * cancel_delayed_work - cancel a delayed work
2948 * @dwork: delayed_work to cancel
Tejun Heo09383492010-09-16 10:48:29 +02002949 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002950 * Kill off a pending delayed_work. Returns %true if @dwork was pending
2951 * and canceled; %false if wasn't pending. Note that the work callback
2952 * function may still be running on return, unless it returns %true and the
2953 * work doesn't re-arm itself. Explicitly flush or use
2954 * cancel_delayed_work_sync() to wait on it.
Tejun Heo09383492010-09-16 10:48:29 +02002955 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002956 * This function is safe to call from any context including IRQ handler.
Tejun Heo09383492010-09-16 10:48:29 +02002957 */
Tejun Heo57b30ae2012-08-21 13:18:24 -07002958bool cancel_delayed_work(struct delayed_work *dwork)
Tejun Heo09383492010-09-16 10:48:29 +02002959{
Tejun Heo57b30ae2012-08-21 13:18:24 -07002960 unsigned long flags;
2961 int ret;
2962
2963 do {
2964 ret = try_to_grab_pending(&dwork->work, true, &flags);
2965 } while (unlikely(ret == -EAGAIN));
2966
2967 if (unlikely(ret < 0))
2968 return false;
2969
Tejun Heo7c3eed52013-01-24 11:01:33 -08002970 set_work_pool_and_clear_pending(&dwork->work,
2971 get_work_pool_id(&dwork->work));
Tejun Heo57b30ae2012-08-21 13:18:24 -07002972 local_irq_restore(flags);
Dan Magenheimerc0158ca2012-10-18 16:31:37 -07002973 return ret;
Tejun Heo09383492010-09-16 10:48:29 +02002974}
Tejun Heo57b30ae2012-08-21 13:18:24 -07002975EXPORT_SYMBOL(cancel_delayed_work);
Tejun Heo09383492010-09-16 10:48:29 +02002976
2977/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002978 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2979 * @dwork: the delayed work cancel
2980 *
2981 * This is cancel_work_sync() for delayed works.
2982 *
2983 * RETURNS:
2984 * %true if @dwork was pending, %false otherwise.
2985 */
2986bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002987{
Tejun Heo36e227d2012-08-03 10:30:46 -07002988 return __cancel_work_timer(&dwork->work, true);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002989}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002990EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002992/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002993 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002994 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002995 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002996 * schedule_on_each_cpu() executes @func on each online CPU using the
2997 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002998 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002999 *
3000 * RETURNS:
3001 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07003002 */
David Howells65f27f32006-11-22 14:55:48 +00003003int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08003004{
3005 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02003006 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08003007
Andrew Mortonb6136772006-06-25 05:47:49 -07003008 works = alloc_percpu(struct work_struct);
3009 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08003010 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07003011
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003012 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08003013
Christoph Lameter15316ba2006-01-08 01:00:43 -08003014 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01003015 struct work_struct *work = per_cpu_ptr(works, cpu);
3016
3017 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003018 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02003019 }
Tejun Heo93981802009-11-17 14:06:20 -08003020
3021 for_each_online_cpu(cpu)
3022 flush_work(per_cpu_ptr(works, cpu));
3023
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003024 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07003025 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08003026 return 0;
3027}
3028
Alan Sterneef6a7d2010-02-12 17:39:21 +09003029/**
3030 * flush_scheduled_work - ensure that any scheduled work has run to completion.
3031 *
3032 * Forces execution of the kernel-global workqueue and blocks until its
3033 * completion.
3034 *
3035 * Think twice before calling this function! It's very easy to get into
3036 * trouble if you don't take great care. Either of the following situations
3037 * will lead to deadlock:
3038 *
3039 * One of the work items currently on the workqueue needs to acquire
3040 * a lock held by your code or its caller.
3041 *
3042 * Your code is running in the context of a work routine.
3043 *
3044 * They will be detected by lockdep when they occur, but the first might not
3045 * occur very often. It depends on what work items are on the workqueue and
3046 * what locks they need, which you have no control over.
3047 *
3048 * In most situations flushing the entire workqueue is overkill; you merely
3049 * need to know that a particular work item isn't queued and isn't running.
3050 * In such cases you should use cancel_delayed_work_sync() or
3051 * cancel_work_sync() instead.
3052 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053void flush_scheduled_work(void)
3054{
Tejun Heod320c032010-06-29 10:07:14 +02003055 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056}
Dave Jonesae90dd52006-06-30 01:40:45 -04003057EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058
3059/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06003060 * execute_in_process_context - reliably execute the routine with user context
3061 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06003062 * @ew: guaranteed storage for the execute work structure (must
3063 * be available when the work executes)
3064 *
3065 * Executes the function immediately if process context is available,
3066 * otherwise schedules the function for delayed execution.
3067 *
3068 * Returns: 0 - function was executed
3069 * 1 - function was scheduled for execution
3070 */
David Howells65f27f32006-11-22 14:55:48 +00003071int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06003072{
3073 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00003074 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003075 return 0;
3076 }
3077
David Howells65f27f32006-11-22 14:55:48 +00003078 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003079 schedule_work(&ew->work);
3080
3081 return 1;
3082}
3083EXPORT_SYMBOL_GPL(execute_in_process_context);
3084
Tejun Heo226223a2013-03-12 11:30:05 -07003085#ifdef CONFIG_SYSFS
3086/*
3087 * Workqueues with WQ_SYSFS flag set is visible to userland via
3088 * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the
3089 * following attributes.
3090 *
3091 * per_cpu RO bool : whether the workqueue is per-cpu or unbound
3092 * max_active RW int : maximum number of in-flight work items
3093 *
3094 * Unbound workqueues have the following extra attributes.
3095 *
3096 * id RO int : the associated pool ID
3097 * nice RW int : nice value of the workers
3098 * cpumask RW mask : bitmask of allowed CPUs for the workers
3099 */
3100struct wq_device {
3101 struct workqueue_struct *wq;
3102 struct device dev;
3103};
3104
3105static struct workqueue_struct *dev_to_wq(struct device *dev)
3106{
3107 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
3108
3109 return wq_dev->wq;
3110}
3111
3112static ssize_t wq_per_cpu_show(struct device *dev,
3113 struct device_attribute *attr, char *buf)
3114{
3115 struct workqueue_struct *wq = dev_to_wq(dev);
3116
3117 return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND));
3118}
3119
3120static ssize_t wq_max_active_show(struct device *dev,
3121 struct device_attribute *attr, char *buf)
3122{
3123 struct workqueue_struct *wq = dev_to_wq(dev);
3124
3125 return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active);
3126}
3127
3128static ssize_t wq_max_active_store(struct device *dev,
3129 struct device_attribute *attr,
3130 const char *buf, size_t count)
3131{
3132 struct workqueue_struct *wq = dev_to_wq(dev);
3133 int val;
3134
3135 if (sscanf(buf, "%d", &val) != 1 || val <= 0)
3136 return -EINVAL;
3137
3138 workqueue_set_max_active(wq, val);
3139 return count;
3140}
3141
3142static struct device_attribute wq_sysfs_attrs[] = {
3143 __ATTR(per_cpu, 0444, wq_per_cpu_show, NULL),
3144 __ATTR(max_active, 0644, wq_max_active_show, wq_max_active_store),
3145 __ATTR_NULL,
3146};
3147
3148static ssize_t wq_pool_id_show(struct device *dev,
3149 struct device_attribute *attr, char *buf)
3150{
3151 struct workqueue_struct *wq = dev_to_wq(dev);
3152 struct worker_pool *pool;
3153 int written;
3154
3155 rcu_read_lock_sched();
3156 pool = first_pwq(wq)->pool;
3157 written = scnprintf(buf, PAGE_SIZE, "%d\n", pool->id);
3158 rcu_read_unlock_sched();
3159
3160 return written;
3161}
3162
3163static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr,
3164 char *buf)
3165{
3166 struct workqueue_struct *wq = dev_to_wq(dev);
3167 int written;
3168
3169 rcu_read_lock_sched();
3170 written = scnprintf(buf, PAGE_SIZE, "%d\n",
3171 first_pwq(wq)->pool->attrs->nice);
3172 rcu_read_unlock_sched();
3173
3174 return written;
3175}
3176
3177/* prepare workqueue_attrs for sysfs store operations */
3178static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq)
3179{
3180 struct workqueue_attrs *attrs;
3181
3182 attrs = alloc_workqueue_attrs(GFP_KERNEL);
3183 if (!attrs)
3184 return NULL;
3185
3186 rcu_read_lock_sched();
3187 copy_workqueue_attrs(attrs, first_pwq(wq)->pool->attrs);
3188 rcu_read_unlock_sched();
3189 return attrs;
3190}
3191
3192static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr,
3193 const char *buf, size_t count)
3194{
3195 struct workqueue_struct *wq = dev_to_wq(dev);
3196 struct workqueue_attrs *attrs;
3197 int ret;
3198
3199 attrs = wq_sysfs_prep_attrs(wq);
3200 if (!attrs)
3201 return -ENOMEM;
3202
3203 if (sscanf(buf, "%d", &attrs->nice) == 1 &&
3204 attrs->nice >= -20 && attrs->nice <= 19)
3205 ret = apply_workqueue_attrs(wq, attrs);
3206 else
3207 ret = -EINVAL;
3208
3209 free_workqueue_attrs(attrs);
3210 return ret ?: count;
3211}
3212
3213static ssize_t wq_cpumask_show(struct device *dev,
3214 struct device_attribute *attr, char *buf)
3215{
3216 struct workqueue_struct *wq = dev_to_wq(dev);
3217 int written;
3218
3219 rcu_read_lock_sched();
3220 written = cpumask_scnprintf(buf, PAGE_SIZE,
3221 first_pwq(wq)->pool->attrs->cpumask);
3222 rcu_read_unlock_sched();
3223
3224 written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
3225 return written;
3226}
3227
3228static ssize_t wq_cpumask_store(struct device *dev,
3229 struct device_attribute *attr,
3230 const char *buf, size_t count)
3231{
3232 struct workqueue_struct *wq = dev_to_wq(dev);
3233 struct workqueue_attrs *attrs;
3234 int ret;
3235
3236 attrs = wq_sysfs_prep_attrs(wq);
3237 if (!attrs)
3238 return -ENOMEM;
3239
3240 ret = cpumask_parse(buf, attrs->cpumask);
3241 if (!ret)
3242 ret = apply_workqueue_attrs(wq, attrs);
3243
3244 free_workqueue_attrs(attrs);
3245 return ret ?: count;
3246}
3247
3248static struct device_attribute wq_sysfs_unbound_attrs[] = {
3249 __ATTR(pool_id, 0444, wq_pool_id_show, NULL),
3250 __ATTR(nice, 0644, wq_nice_show, wq_nice_store),
3251 __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store),
3252 __ATTR_NULL,
3253};
3254
3255static struct bus_type wq_subsys = {
3256 .name = "workqueue",
3257 .dev_attrs = wq_sysfs_attrs,
3258};
3259
3260static int __init wq_sysfs_init(void)
3261{
3262 return subsys_virtual_register(&wq_subsys, NULL);
3263}
3264core_initcall(wq_sysfs_init);
3265
3266static void wq_device_release(struct device *dev)
3267{
3268 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
3269
3270 kfree(wq_dev);
3271}
3272
3273/**
3274 * workqueue_sysfs_register - make a workqueue visible in sysfs
3275 * @wq: the workqueue to register
3276 *
3277 * Expose @wq in sysfs under /sys/bus/workqueue/devices.
3278 * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set
3279 * which is the preferred method.
3280 *
3281 * Workqueue user should use this function directly iff it wants to apply
3282 * workqueue_attrs before making the workqueue visible in sysfs; otherwise,
3283 * apply_workqueue_attrs() may race against userland updating the
3284 * attributes.
3285 *
3286 * Returns 0 on success, -errno on failure.
3287 */
3288int workqueue_sysfs_register(struct workqueue_struct *wq)
3289{
3290 struct wq_device *wq_dev;
3291 int ret;
3292
3293 /*
3294 * Adjusting max_active or creating new pwqs by applyting
3295 * attributes breaks ordering guarantee. Disallow exposing ordered
3296 * workqueues.
3297 */
3298 if (WARN_ON(wq->flags & __WQ_ORDERED))
3299 return -EINVAL;
3300
3301 wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL);
3302 if (!wq_dev)
3303 return -ENOMEM;
3304
3305 wq_dev->wq = wq;
3306 wq_dev->dev.bus = &wq_subsys;
3307 wq_dev->dev.init_name = wq->name;
3308 wq_dev->dev.release = wq_device_release;
3309
3310 /*
3311 * unbound_attrs are created separately. Suppress uevent until
3312 * everything is ready.
3313 */
3314 dev_set_uevent_suppress(&wq_dev->dev, true);
3315
3316 ret = device_register(&wq_dev->dev);
3317 if (ret) {
3318 kfree(wq_dev);
3319 wq->wq_dev = NULL;
3320 return ret;
3321 }
3322
3323 if (wq->flags & WQ_UNBOUND) {
3324 struct device_attribute *attr;
3325
3326 for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) {
3327 ret = device_create_file(&wq_dev->dev, attr);
3328 if (ret) {
3329 device_unregister(&wq_dev->dev);
3330 wq->wq_dev = NULL;
3331 return ret;
3332 }
3333 }
3334 }
3335
3336 kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD);
3337 return 0;
3338}
3339
3340/**
3341 * workqueue_sysfs_unregister - undo workqueue_sysfs_register()
3342 * @wq: the workqueue to unregister
3343 *
3344 * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister.
3345 */
3346static void workqueue_sysfs_unregister(struct workqueue_struct *wq)
3347{
3348 struct wq_device *wq_dev = wq->wq_dev;
3349
3350 if (!wq->wq_dev)
3351 return;
3352
3353 wq->wq_dev = NULL;
3354 device_unregister(&wq_dev->dev);
3355}
3356#else /* CONFIG_SYSFS */
3357static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { }
3358#endif /* CONFIG_SYSFS */
3359
Tejun Heo7a4e3442013-03-12 11:30:00 -07003360/**
3361 * free_workqueue_attrs - free a workqueue_attrs
3362 * @attrs: workqueue_attrs to free
3363 *
3364 * Undo alloc_workqueue_attrs().
3365 */
3366void free_workqueue_attrs(struct workqueue_attrs *attrs)
3367{
3368 if (attrs) {
3369 free_cpumask_var(attrs->cpumask);
3370 kfree(attrs);
3371 }
3372}
3373
3374/**
3375 * alloc_workqueue_attrs - allocate a workqueue_attrs
3376 * @gfp_mask: allocation mask to use
3377 *
3378 * Allocate a new workqueue_attrs, initialize with default settings and
3379 * return it. Returns NULL on failure.
3380 */
3381struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask)
3382{
3383 struct workqueue_attrs *attrs;
3384
3385 attrs = kzalloc(sizeof(*attrs), gfp_mask);
3386 if (!attrs)
3387 goto fail;
3388 if (!alloc_cpumask_var(&attrs->cpumask, gfp_mask))
3389 goto fail;
3390
3391 cpumask_setall(attrs->cpumask);
3392 return attrs;
3393fail:
3394 free_workqueue_attrs(attrs);
3395 return NULL;
3396}
3397
Tejun Heo29c91e92013-03-12 11:30:03 -07003398static void copy_workqueue_attrs(struct workqueue_attrs *to,
3399 const struct workqueue_attrs *from)
3400{
3401 to->nice = from->nice;
3402 cpumask_copy(to->cpumask, from->cpumask);
3403}
3404
3405/*
3406 * Hacky implementation of jhash of bitmaps which only considers the
3407 * specified number of bits. We probably want a proper implementation in
3408 * include/linux/jhash.h.
3409 */
3410static u32 jhash_bitmap(const unsigned long *bitmap, int bits, u32 hash)
3411{
3412 int nr_longs = bits / BITS_PER_LONG;
3413 int nr_leftover = bits % BITS_PER_LONG;
3414 unsigned long leftover = 0;
3415
3416 if (nr_longs)
3417 hash = jhash(bitmap, nr_longs * sizeof(long), hash);
3418 if (nr_leftover) {
3419 bitmap_copy(&leftover, bitmap + nr_longs, nr_leftover);
3420 hash = jhash(&leftover, sizeof(long), hash);
3421 }
3422 return hash;
3423}
3424
3425/* hash value of the content of @attr */
3426static u32 wqattrs_hash(const struct workqueue_attrs *attrs)
3427{
3428 u32 hash = 0;
3429
3430 hash = jhash_1word(attrs->nice, hash);
3431 hash = jhash_bitmap(cpumask_bits(attrs->cpumask), nr_cpu_ids, hash);
3432 return hash;
3433}
3434
3435/* content equality test */
3436static bool wqattrs_equal(const struct workqueue_attrs *a,
3437 const struct workqueue_attrs *b)
3438{
3439 if (a->nice != b->nice)
3440 return false;
3441 if (!cpumask_equal(a->cpumask, b->cpumask))
3442 return false;
3443 return true;
3444}
3445
Tejun Heo7a4e3442013-03-12 11:30:00 -07003446/**
3447 * init_worker_pool - initialize a newly zalloc'd worker_pool
3448 * @pool: worker_pool to initialize
3449 *
3450 * Initiailize a newly zalloc'd @pool. It also allocates @pool->attrs.
Tejun Heo29c91e92013-03-12 11:30:03 -07003451 * Returns 0 on success, -errno on failure. Even on failure, all fields
3452 * inside @pool proper are initialized and put_unbound_pool() can be called
3453 * on @pool safely to release it.
Tejun Heo7a4e3442013-03-12 11:30:00 -07003454 */
3455static int init_worker_pool(struct worker_pool *pool)
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003456{
3457 spin_lock_init(&pool->lock);
Tejun Heo29c91e92013-03-12 11:30:03 -07003458 pool->id = -1;
3459 pool->cpu = -1;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003460 pool->flags |= POOL_DISASSOCIATED;
3461 INIT_LIST_HEAD(&pool->worklist);
3462 INIT_LIST_HEAD(&pool->idle_list);
3463 hash_init(pool->busy_hash);
3464
3465 init_timer_deferrable(&pool->idle_timer);
3466 pool->idle_timer.function = idle_worker_timeout;
3467 pool->idle_timer.data = (unsigned long)pool;
3468
3469 setup_timer(&pool->mayday_timer, pool_mayday_timeout,
3470 (unsigned long)pool);
3471
3472 mutex_init(&pool->manager_arb);
Tejun Heobc3a1af2013-03-13 19:47:39 -07003473 mutex_init(&pool->manager_mutex);
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003474 ida_init(&pool->worker_ida);
Tejun Heo7a4e3442013-03-12 11:30:00 -07003475
Tejun Heo29c91e92013-03-12 11:30:03 -07003476 INIT_HLIST_NODE(&pool->hash_node);
3477 pool->refcnt = 1;
3478
3479 /* shouldn't fail above this point */
Tejun Heo7a4e3442013-03-12 11:30:00 -07003480 pool->attrs = alloc_workqueue_attrs(GFP_KERNEL);
3481 if (!pool->attrs)
3482 return -ENOMEM;
3483 return 0;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003484}
3485
Tejun Heo29c91e92013-03-12 11:30:03 -07003486static void rcu_free_pool(struct rcu_head *rcu)
3487{
3488 struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu);
3489
3490 ida_destroy(&pool->worker_ida);
3491 free_workqueue_attrs(pool->attrs);
3492 kfree(pool);
3493}
3494
3495/**
3496 * put_unbound_pool - put a worker_pool
3497 * @pool: worker_pool to put
3498 *
3499 * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003500 * safe manner. get_unbound_pool() calls this function on its failure path
3501 * and this function should be able to release pools which went through,
3502 * successfully or not, init_worker_pool().
Tejun Heo29c91e92013-03-12 11:30:03 -07003503 */
3504static void put_unbound_pool(struct worker_pool *pool)
3505{
3506 struct worker *worker;
3507
3508 spin_lock_irq(&workqueue_lock);
3509 if (--pool->refcnt) {
3510 spin_unlock_irq(&workqueue_lock);
3511 return;
3512 }
3513
3514 /* sanity checks */
3515 if (WARN_ON(!(pool->flags & POOL_DISASSOCIATED)) ||
3516 WARN_ON(!list_empty(&pool->worklist))) {
3517 spin_unlock_irq(&workqueue_lock);
3518 return;
3519 }
3520
3521 /* release id and unhash */
3522 if (pool->id >= 0)
3523 idr_remove(&worker_pool_idr, pool->id);
3524 hash_del(&pool->hash_node);
3525
3526 spin_unlock_irq(&workqueue_lock);
3527
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003528 /*
3529 * Become the manager and destroy all workers. Grabbing
3530 * manager_arb prevents @pool's workers from blocking on
3531 * manager_mutex.
3532 */
Tejun Heo29c91e92013-03-12 11:30:03 -07003533 mutex_lock(&pool->manager_arb);
3534 spin_lock_irq(&pool->lock);
3535
3536 while ((worker = first_worker(pool)))
3537 destroy_worker(worker);
3538 WARN_ON(pool->nr_workers || pool->nr_idle);
3539
3540 spin_unlock_irq(&pool->lock);
3541 mutex_unlock(&pool->manager_arb);
3542
3543 /* shut down the timers */
3544 del_timer_sync(&pool->idle_timer);
3545 del_timer_sync(&pool->mayday_timer);
3546
3547 /* sched-RCU protected to allow dereferences from get_work_pool() */
3548 call_rcu_sched(&pool->rcu, rcu_free_pool);
3549}
3550
3551/**
3552 * get_unbound_pool - get a worker_pool with the specified attributes
3553 * @attrs: the attributes of the worker_pool to get
3554 *
3555 * Obtain a worker_pool which has the same attributes as @attrs, bump the
3556 * reference count and return it. If there already is a matching
3557 * worker_pool, it will be used; otherwise, this function attempts to
3558 * create a new one. On failure, returns NULL.
3559 */
3560static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs)
3561{
3562 static DEFINE_MUTEX(create_mutex);
3563 u32 hash = wqattrs_hash(attrs);
3564 struct worker_pool *pool;
Tejun Heo29c91e92013-03-12 11:30:03 -07003565
3566 mutex_lock(&create_mutex);
3567
3568 /* do we already have a matching pool? */
3569 spin_lock_irq(&workqueue_lock);
3570 hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) {
3571 if (wqattrs_equal(pool->attrs, attrs)) {
3572 pool->refcnt++;
3573 goto out_unlock;
3574 }
3575 }
3576 spin_unlock_irq(&workqueue_lock);
3577
3578 /* nope, create a new one */
3579 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
3580 if (!pool || init_worker_pool(pool) < 0)
3581 goto fail;
3582
Tejun Heo8864b4e2013-03-12 11:30:04 -07003583 lockdep_set_subclass(&pool->lock, 1); /* see put_pwq() */
Tejun Heo29c91e92013-03-12 11:30:03 -07003584 copy_workqueue_attrs(pool->attrs, attrs);
3585
3586 if (worker_pool_assign_id(pool) < 0)
3587 goto fail;
3588
3589 /* create and start the initial worker */
Tejun Heoebf44d12013-03-13 19:47:39 -07003590 if (create_and_start_worker(pool) < 0)
Tejun Heo29c91e92013-03-12 11:30:03 -07003591 goto fail;
3592
Tejun Heo29c91e92013-03-12 11:30:03 -07003593 /* install */
3594 spin_lock_irq(&workqueue_lock);
3595 hash_add(unbound_pool_hash, &pool->hash_node, hash);
3596out_unlock:
3597 spin_unlock_irq(&workqueue_lock);
3598 mutex_unlock(&create_mutex);
3599 return pool;
3600fail:
3601 mutex_unlock(&create_mutex);
3602 if (pool)
3603 put_unbound_pool(pool);
3604 return NULL;
3605}
3606
Tejun Heo8864b4e2013-03-12 11:30:04 -07003607static void rcu_free_pwq(struct rcu_head *rcu)
3608{
3609 kmem_cache_free(pwq_cache,
3610 container_of(rcu, struct pool_workqueue, rcu));
3611}
3612
3613/*
3614 * Scheduled on system_wq by put_pwq() when an unbound pwq hits zero refcnt
3615 * and needs to be destroyed.
3616 */
3617static void pwq_unbound_release_workfn(struct work_struct *work)
3618{
3619 struct pool_workqueue *pwq = container_of(work, struct pool_workqueue,
3620 unbound_release_work);
3621 struct workqueue_struct *wq = pwq->wq;
3622 struct worker_pool *pool = pwq->pool;
3623
3624 if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)))
3625 return;
3626
Tejun Heo75ccf592013-03-12 11:30:04 -07003627 /*
3628 * Unlink @pwq. Synchronization against flush_mutex isn't strictly
3629 * necessary on release but do it anyway. It's easier to verify
3630 * and consistent with the linking path.
3631 */
3632 mutex_lock(&wq->flush_mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003633 spin_lock_irq(&workqueue_lock);
3634 list_del_rcu(&pwq->pwqs_node);
3635 spin_unlock_irq(&workqueue_lock);
Tejun Heo75ccf592013-03-12 11:30:04 -07003636 mutex_unlock(&wq->flush_mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003637
3638 put_unbound_pool(pool);
3639 call_rcu_sched(&pwq->rcu, rcu_free_pwq);
3640
3641 /*
3642 * If we're the last pwq going away, @wq is already dead and no one
3643 * is gonna access it anymore. Free it.
3644 */
3645 if (list_empty(&wq->pwqs))
3646 kfree(wq);
3647}
3648
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003649/**
Tejun Heo699ce092013-03-13 16:51:35 -07003650 * pwq_adjust_max_active - update a pwq's max_active to the current setting
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003651 * @pwq: target pool_workqueue
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003652 *
Tejun Heo699ce092013-03-13 16:51:35 -07003653 * If @pwq isn't freezing, set @pwq->max_active to the associated
3654 * workqueue's saved_max_active and activate delayed work items
3655 * accordingly. If @pwq is freezing, clear @pwq->max_active to zero.
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003656 */
Tejun Heo699ce092013-03-13 16:51:35 -07003657static void pwq_adjust_max_active(struct pool_workqueue *pwq)
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003658{
Tejun Heo699ce092013-03-13 16:51:35 -07003659 struct workqueue_struct *wq = pwq->wq;
3660 bool freezable = wq->flags & WQ_FREEZABLE;
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003661
Tejun Heo699ce092013-03-13 16:51:35 -07003662 /* for @wq->saved_max_active */
3663 lockdep_assert_held(&workqueue_lock);
3664
3665 /* fast exit for non-freezable wqs */
3666 if (!freezable && pwq->max_active == wq->saved_max_active)
3667 return;
3668
3669 spin_lock(&pwq->pool->lock);
3670
3671 if (!freezable || !(pwq->pool->flags & POOL_FREEZING)) {
3672 pwq->max_active = wq->saved_max_active;
3673
3674 while (!list_empty(&pwq->delayed_works) &&
3675 pwq->nr_active < pwq->max_active)
3676 pwq_activate_first_delayed(pwq);
3677 } else {
3678 pwq->max_active = 0;
3679 }
3680
3681 spin_unlock(&pwq->pool->lock);
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003682}
3683
Tejun Heod2c1d402013-03-12 11:30:04 -07003684static void init_and_link_pwq(struct pool_workqueue *pwq,
3685 struct workqueue_struct *wq,
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003686 struct worker_pool *pool,
3687 struct pool_workqueue **p_last_pwq)
Tejun Heod2c1d402013-03-12 11:30:04 -07003688{
3689 BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK);
3690
3691 pwq->pool = pool;
3692 pwq->wq = wq;
3693 pwq->flush_color = -1;
Tejun Heo8864b4e2013-03-12 11:30:04 -07003694 pwq->refcnt = 1;
Tejun Heod2c1d402013-03-12 11:30:04 -07003695 INIT_LIST_HEAD(&pwq->delayed_works);
3696 INIT_LIST_HEAD(&pwq->mayday_node);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003697 INIT_WORK(&pwq->unbound_release_work, pwq_unbound_release_workfn);
Tejun Heod2c1d402013-03-12 11:30:04 -07003698
Tejun Heo75ccf592013-03-12 11:30:04 -07003699 mutex_lock(&wq->flush_mutex);
3700 spin_lock_irq(&workqueue_lock);
3701
Tejun Heo983ca252013-03-13 16:51:35 -07003702 /*
3703 * Set the matching work_color. This is synchronized with
3704 * flush_mutex to avoid confusing flush_workqueue().
3705 */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003706 if (p_last_pwq)
3707 *p_last_pwq = first_pwq(wq);
Tejun Heo75ccf592013-03-12 11:30:04 -07003708 pwq->work_color = wq->work_color;
Tejun Heo983ca252013-03-13 16:51:35 -07003709
3710 /* sync max_active to the current setting */
3711 pwq_adjust_max_active(pwq);
3712
3713 /* link in @pwq */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003714 list_add_rcu(&pwq->pwqs_node, &wq->pwqs);
Tejun Heo75ccf592013-03-12 11:30:04 -07003715
3716 spin_unlock_irq(&workqueue_lock);
3717 mutex_unlock(&wq->flush_mutex);
Tejun Heod2c1d402013-03-12 11:30:04 -07003718}
3719
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003720/**
3721 * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue
3722 * @wq: the target workqueue
3723 * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs()
3724 *
3725 * Apply @attrs to an unbound workqueue @wq. If @attrs doesn't match the
3726 * current attributes, a new pwq is created and made the first pwq which
3727 * will serve all new work items. Older pwqs are released as in-flight
3728 * work items finish. Note that a work item which repeatedly requeues
3729 * itself back-to-back will stay on its current pwq.
3730 *
3731 * Performs GFP_KERNEL allocations. Returns 0 on success and -errno on
3732 * failure.
3733 */
3734int apply_workqueue_attrs(struct workqueue_struct *wq,
3735 const struct workqueue_attrs *attrs)
3736{
3737 struct pool_workqueue *pwq, *last_pwq;
3738 struct worker_pool *pool;
3739
Tejun Heo8719dce2013-03-12 11:30:04 -07003740 /* only unbound workqueues can change attributes */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003741 if (WARN_ON(!(wq->flags & WQ_UNBOUND)))
3742 return -EINVAL;
3743
Tejun Heo8719dce2013-03-12 11:30:04 -07003744 /* creating multiple pwqs breaks ordering guarantee */
3745 if (WARN_ON((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs)))
3746 return -EINVAL;
3747
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003748 pwq = kmem_cache_zalloc(pwq_cache, GFP_KERNEL);
3749 if (!pwq)
3750 return -ENOMEM;
3751
3752 pool = get_unbound_pool(attrs);
3753 if (!pool) {
3754 kmem_cache_free(pwq_cache, pwq);
3755 return -ENOMEM;
3756 }
3757
3758 init_and_link_pwq(pwq, wq, pool, &last_pwq);
3759 if (last_pwq) {
3760 spin_lock_irq(&last_pwq->pool->lock);
3761 put_pwq(last_pwq);
3762 spin_unlock_irq(&last_pwq->pool->lock);
3763 }
3764
3765 return 0;
3766}
3767
Tejun Heo30cdf242013-03-12 11:29:57 -07003768static int alloc_and_link_pwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003770 bool highpri = wq->flags & WQ_HIGHPRI;
Tejun Heo30cdf242013-03-12 11:29:57 -07003771 int cpu;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01003772
Tejun Heo30cdf242013-03-12 11:29:57 -07003773 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo420c0dd2013-03-12 11:29:59 -07003774 wq->cpu_pwqs = alloc_percpu(struct pool_workqueue);
3775 if (!wq->cpu_pwqs)
Tejun Heo30cdf242013-03-12 11:29:57 -07003776 return -ENOMEM;
3777
3778 for_each_possible_cpu(cpu) {
Tejun Heo7fb98ea2013-03-12 11:30:00 -07003779 struct pool_workqueue *pwq =
3780 per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heo7a62c2c2013-03-12 11:30:03 -07003781 struct worker_pool *cpu_pools =
Tejun Heof02ae732013-03-12 11:30:03 -07003782 per_cpu(cpu_worker_pools, cpu);
Tejun Heo30cdf242013-03-12 11:29:57 -07003783
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003784 init_and_link_pwq(pwq, wq, &cpu_pools[highpri], NULL);
Tejun Heo30cdf242013-03-12 11:29:57 -07003785 }
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003786 return 0;
Tejun Heo30cdf242013-03-12 11:29:57 -07003787 } else {
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003788 return apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]);
Tejun Heo30cdf242013-03-12 11:29:57 -07003789 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003790}
3791
Tejun Heof3421792010-07-02 10:03:51 +02003792static int wq_clamp_max_active(int max_active, unsigned int flags,
3793 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003794{
Tejun Heof3421792010-07-02 10:03:51 +02003795 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3796
3797 if (max_active < 1 || max_active > lim)
Valentin Ilie044c7822012-08-19 00:52:42 +03003798 pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
3799 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003800
Tejun Heof3421792010-07-02 10:03:51 +02003801 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003802}
3803
Tejun Heob196be82012-01-10 15:11:35 -08003804struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02003805 unsigned int flags,
3806 int max_active,
3807 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08003808 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003809{
Tejun Heob196be82012-01-10 15:11:35 -08003810 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003811 struct workqueue_struct *wq;
Tejun Heo49e3cf42013-03-12 11:29:58 -07003812 struct pool_workqueue *pwq;
Tejun Heob196be82012-01-10 15:11:35 -08003813 size_t namelen;
3814
3815 /* determine namelen, allocate wq and format name */
3816 va_start(args, lock_name);
3817 va_copy(args1, args);
3818 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3819
3820 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3821 if (!wq)
Tejun Heod2c1d402013-03-12 11:30:04 -07003822 return NULL;
Tejun Heob196be82012-01-10 15:11:35 -08003823
3824 vsnprintf(wq->name, namelen, fmt, args1);
3825 va_end(args);
3826 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003827
Tejun Heod320c032010-06-29 10:07:14 +02003828 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003829 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003830
Tejun Heob196be82012-01-10 15:11:35 -08003831 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003832 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003833 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003834 mutex_init(&wq->flush_mutex);
Tejun Heo112202d2013-02-13 19:29:12 -08003835 atomic_set(&wq->nr_pwqs_to_flush, 0);
Tejun Heo30cdf242013-03-12 11:29:57 -07003836 INIT_LIST_HEAD(&wq->pwqs);
Tejun Heo73f53c42010-06-29 10:07:11 +02003837 INIT_LIST_HEAD(&wq->flusher_queue);
3838 INIT_LIST_HEAD(&wq->flusher_overflow);
Tejun Heo493a1722013-03-12 11:29:59 -07003839 INIT_LIST_HEAD(&wq->maydays);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003840
Johannes Bergeb13ba82008-01-16 09:51:58 +01003841 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003842 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003843
Tejun Heo30cdf242013-03-12 11:29:57 -07003844 if (alloc_and_link_pwqs(wq) < 0)
Tejun Heod2c1d402013-03-12 11:30:04 -07003845 goto err_free_wq;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003846
Tejun Heo493008a2013-03-12 11:30:03 -07003847 /*
3848 * Workqueues which may be used during memory reclaim should
3849 * have a rescuer to guarantee forward progress.
3850 */
3851 if (flags & WQ_MEM_RECLAIM) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003852 struct worker *rescuer;
3853
Tejun Heod2c1d402013-03-12 11:30:04 -07003854 rescuer = alloc_worker();
Tejun Heoe22bee72010-06-29 10:07:14 +02003855 if (!rescuer)
Tejun Heod2c1d402013-03-12 11:30:04 -07003856 goto err_destroy;
Tejun Heoe22bee72010-06-29 10:07:14 +02003857
Tejun Heo111c2252013-01-17 17:16:24 -08003858 rescuer->rescue_wq = wq;
3859 rescuer->task = kthread_create(rescuer_thread, rescuer, "%s",
Tejun Heob196be82012-01-10 15:11:35 -08003860 wq->name);
Tejun Heod2c1d402013-03-12 11:30:04 -07003861 if (IS_ERR(rescuer->task)) {
3862 kfree(rescuer);
3863 goto err_destroy;
3864 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003865
Tejun Heod2c1d402013-03-12 11:30:04 -07003866 wq->rescuer = rescuer;
Tejun Heoe22bee72010-06-29 10:07:14 +02003867 rescuer->task->flags |= PF_THREAD_BOUND;
3868 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003869 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003870
Tejun Heo226223a2013-03-12 11:30:05 -07003871 if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq))
3872 goto err_destroy;
3873
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003874 /*
Tejun Heo699ce092013-03-13 16:51:35 -07003875 * workqueue_lock protects global freeze state and workqueues list.
3876 * Grab it, adjust max_active and add the new workqueue to
3877 * workqueues list.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003878 */
Tejun Heoe98d5b12013-03-12 11:29:57 -07003879 spin_lock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003880
Tejun Heo699ce092013-03-13 16:51:35 -07003881 for_each_pwq(pwq, wq)
3882 pwq_adjust_max_active(pwq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003883
Tejun Heo15376632010-06-29 10:07:11 +02003884 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003885
Tejun Heoe98d5b12013-03-12 11:29:57 -07003886 spin_unlock_irq(&workqueue_lock);
Tejun Heo15376632010-06-29 10:07:11 +02003887
Oleg Nesterov3af244332007-05-09 02:34:09 -07003888 return wq;
Tejun Heod2c1d402013-03-12 11:30:04 -07003889
3890err_free_wq:
3891 kfree(wq);
3892 return NULL;
3893err_destroy:
3894 destroy_workqueue(wq);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003895 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003896}
Tejun Heod320c032010-06-29 10:07:14 +02003897EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003898
3899/**
3900 * destroy_workqueue - safely terminate a workqueue
3901 * @wq: target workqueue
3902 *
3903 * Safely destroy a workqueue. All work currently pending will be done first.
3904 */
3905void destroy_workqueue(struct workqueue_struct *wq)
3906{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003907 struct pool_workqueue *pwq;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003908
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003909 /* drain it before proceeding with destruction */
3910 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003911
Tejun Heo76af4d92013-03-12 11:30:00 -07003912 spin_lock_irq(&workqueue_lock);
3913
Tejun Heo6183c002013-03-12 11:29:57 -07003914 /* sanity checks */
Tejun Heo49e3cf42013-03-12 11:29:58 -07003915 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07003916 int i;
3917
Tejun Heo76af4d92013-03-12 11:30:00 -07003918 for (i = 0; i < WORK_NR_COLORS; i++) {
3919 if (WARN_ON(pwq->nr_in_flight[i])) {
3920 spin_unlock_irq(&workqueue_lock);
Tejun Heo6183c002013-03-12 11:29:57 -07003921 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07003922 }
3923 }
3924
Tejun Heo8864b4e2013-03-12 11:30:04 -07003925 if (WARN_ON(pwq->refcnt > 1) ||
3926 WARN_ON(pwq->nr_active) ||
Tejun Heo76af4d92013-03-12 11:30:00 -07003927 WARN_ON(!list_empty(&pwq->delayed_works))) {
3928 spin_unlock_irq(&workqueue_lock);
Tejun Heo6183c002013-03-12 11:29:57 -07003929 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07003930 }
Tejun Heo6183c002013-03-12 11:29:57 -07003931 }
3932
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003933 /*
3934 * wq list is used to freeze wq, remove from list after
3935 * flushing is complete in case freeze races us.
3936 */
Tejun Heod2c1d402013-03-12 11:30:04 -07003937 list_del_init(&wq->list);
Tejun Heo76af4d92013-03-12 11:30:00 -07003938
Tejun Heoe98d5b12013-03-12 11:29:57 -07003939 spin_unlock_irq(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003940
Tejun Heo226223a2013-03-12 11:30:05 -07003941 workqueue_sysfs_unregister(wq);
3942
Tejun Heo493008a2013-03-12 11:30:03 -07003943 if (wq->rescuer) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003944 kthread_stop(wq->rescuer->task);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003945 kfree(wq->rescuer);
Tejun Heo493008a2013-03-12 11:30:03 -07003946 wq->rescuer = NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +02003947 }
3948
Tejun Heo8864b4e2013-03-12 11:30:04 -07003949 if (!(wq->flags & WQ_UNBOUND)) {
3950 /*
3951 * The base ref is never dropped on per-cpu pwqs. Directly
3952 * free the pwqs and wq.
3953 */
3954 free_percpu(wq->cpu_pwqs);
3955 kfree(wq);
3956 } else {
3957 /*
3958 * We're the sole accessor of @wq at this point. Directly
3959 * access the first pwq and put the base ref. As both pwqs
3960 * and pools are sched-RCU protected, the lock operations
3961 * are safe. @wq will be freed when the last pwq is
3962 * released.
3963 */
Tejun Heo29c91e92013-03-12 11:30:03 -07003964 pwq = list_first_entry(&wq->pwqs, struct pool_workqueue,
3965 pwqs_node);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003966 spin_lock_irq(&pwq->pool->lock);
3967 put_pwq(pwq);
3968 spin_unlock_irq(&pwq->pool->lock);
Tejun Heo29c91e92013-03-12 11:30:03 -07003969 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003970}
3971EXPORT_SYMBOL_GPL(destroy_workqueue);
3972
Tejun Heodcd989c2010-06-29 10:07:14 +02003973/**
3974 * workqueue_set_max_active - adjust max_active of a workqueue
3975 * @wq: target workqueue
3976 * @max_active: new max_active value.
3977 *
3978 * Set max_active of @wq to @max_active.
3979 *
3980 * CONTEXT:
3981 * Don't call from IRQ context.
3982 */
3983void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3984{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003985 struct pool_workqueue *pwq;
Tejun Heodcd989c2010-06-29 10:07:14 +02003986
Tejun Heo8719dce2013-03-12 11:30:04 -07003987 /* disallow meddling with max_active for ordered workqueues */
3988 if (WARN_ON(wq->flags & __WQ_ORDERED))
3989 return;
3990
Tejun Heof3421792010-07-02 10:03:51 +02003991 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003992
Tejun Heoe98d5b12013-03-12 11:29:57 -07003993 spin_lock_irq(&workqueue_lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02003994
3995 wq->saved_max_active = max_active;
3996
Tejun Heo699ce092013-03-13 16:51:35 -07003997 for_each_pwq(pwq, wq)
3998 pwq_adjust_max_active(pwq);
Tejun Heodcd989c2010-06-29 10:07:14 +02003999
Tejun Heoe98d5b12013-03-12 11:29:57 -07004000 spin_unlock_irq(&workqueue_lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02004001}
4002EXPORT_SYMBOL_GPL(workqueue_set_max_active);
4003
4004/**
Tejun Heoe6267612013-03-12 17:41:37 -07004005 * current_is_workqueue_rescuer - is %current workqueue rescuer?
4006 *
4007 * Determine whether %current is a workqueue rescuer. Can be used from
4008 * work functions to determine whether it's being run off the rescuer task.
4009 */
4010bool current_is_workqueue_rescuer(void)
4011{
4012 struct worker *worker = current_wq_worker();
4013
4014 return worker && worker == worker->current_pwq->wq->rescuer;
4015}
4016
4017/**
Tejun Heodcd989c2010-06-29 10:07:14 +02004018 * workqueue_congested - test whether a workqueue is congested
4019 * @cpu: CPU in question
4020 * @wq: target workqueue
4021 *
4022 * Test whether @wq's cpu workqueue for @cpu is congested. There is
4023 * no synchronization around this function and the test result is
4024 * unreliable and only useful as advisory hints or for debugging.
4025 *
4026 * RETURNS:
4027 * %true if congested, %false otherwise.
4028 */
Tejun Heod84ff052013-03-12 11:29:59 -07004029bool workqueue_congested(int cpu, struct workqueue_struct *wq)
Tejun Heodcd989c2010-06-29 10:07:14 +02004030{
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004031 struct pool_workqueue *pwq;
Tejun Heo76af4d92013-03-12 11:30:00 -07004032 bool ret;
4033
4034 preempt_disable();
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004035
4036 if (!(wq->flags & WQ_UNBOUND))
4037 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
4038 else
4039 pwq = first_pwq(wq);
Tejun Heodcd989c2010-06-29 10:07:14 +02004040
Tejun Heo76af4d92013-03-12 11:30:00 -07004041 ret = !list_empty(&pwq->delayed_works);
4042 preempt_enable();
4043
4044 return ret;
Tejun Heodcd989c2010-06-29 10:07:14 +02004045}
4046EXPORT_SYMBOL_GPL(workqueue_congested);
4047
4048/**
Tejun Heodcd989c2010-06-29 10:07:14 +02004049 * work_busy - test whether a work is currently pending or running
4050 * @work: the work to be tested
4051 *
4052 * Test whether @work is currently pending or running. There is no
4053 * synchronization around this function and the test result is
4054 * unreliable and only useful as advisory hints or for debugging.
Tejun Heodcd989c2010-06-29 10:07:14 +02004055 *
4056 * RETURNS:
4057 * OR'd bitmask of WORK_BUSY_* bits.
4058 */
4059unsigned int work_busy(struct work_struct *work)
4060{
Tejun Heofa1b54e2013-03-12 11:30:00 -07004061 struct worker_pool *pool;
Tejun Heodcd989c2010-06-29 10:07:14 +02004062 unsigned long flags;
4063 unsigned int ret = 0;
4064
Tejun Heodcd989c2010-06-29 10:07:14 +02004065 if (work_pending(work))
4066 ret |= WORK_BUSY_PENDING;
Tejun Heodcd989c2010-06-29 10:07:14 +02004067
Tejun Heofa1b54e2013-03-12 11:30:00 -07004068 local_irq_save(flags);
4069 pool = get_work_pool(work);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004070 if (pool) {
Tejun Heofa1b54e2013-03-12 11:30:00 -07004071 spin_lock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004072 if (find_worker_executing_work(pool, work))
4073 ret |= WORK_BUSY_RUNNING;
Tejun Heofa1b54e2013-03-12 11:30:00 -07004074 spin_unlock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004075 }
Tejun Heofa1b54e2013-03-12 11:30:00 -07004076 local_irq_restore(flags);
Tejun Heodcd989c2010-06-29 10:07:14 +02004077
4078 return ret;
4079}
4080EXPORT_SYMBOL_GPL(work_busy);
4081
Tejun Heodb7bccf2010-06-29 10:07:12 +02004082/*
4083 * CPU hotplug.
4084 *
Tejun Heoe22bee72010-06-29 10:07:14 +02004085 * There are two challenges in supporting CPU hotplug. Firstly, there
Tejun Heo112202d2013-02-13 19:29:12 -08004086 * are a lot of assumptions on strong associations among work, pwq and
Tejun Heo706026c2013-01-24 11:01:34 -08004087 * pool which make migrating pending and scheduled works very
Tejun Heoe22bee72010-06-29 10:07:14 +02004088 * difficult to implement without impacting hot paths. Secondly,
Tejun Heo94cf58b2013-01-24 11:01:33 -08004089 * worker pools serve mix of short, long and very long running works making
Tejun Heoe22bee72010-06-29 10:07:14 +02004090 * blocked draining impractical.
4091 *
Tejun Heo24647572013-01-24 11:01:33 -08004092 * This is solved by allowing the pools to be disassociated from the CPU
Tejun Heo628c78e2012-07-17 12:39:27 -07004093 * running as an unbound one and allowing it to be reattached later if the
4094 * cpu comes back online.
Tejun Heodb7bccf2010-06-29 10:07:12 +02004095 */
4096
Tejun Heo706026c2013-01-24 11:01:34 -08004097static void wq_unbind_fn(struct work_struct *work)
Tejun Heodb7bccf2010-06-29 10:07:12 +02004098{
Tejun Heo38db41d2013-01-24 11:01:34 -08004099 int cpu = smp_processor_id();
Tejun Heo4ce62e92012-07-13 22:16:44 -07004100 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004101 struct worker *worker;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004102 int i;
4103
Tejun Heof02ae732013-03-12 11:30:03 -07004104 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo6183c002013-03-12 11:29:57 -07004105 WARN_ON_ONCE(cpu != smp_processor_id());
Tejun Heo94cf58b2013-01-24 11:01:33 -08004106
Tejun Heobc3a1af2013-03-13 19:47:39 -07004107 mutex_lock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004108 spin_lock_irq(&pool->lock);
4109
4110 /*
Tejun Heobc3a1af2013-03-13 19:47:39 -07004111 * We've blocked all manager operations. Make all workers
Tejun Heo94cf58b2013-01-24 11:01:33 -08004112 * unbound and set DISASSOCIATED. Before this, all workers
4113 * except for the ones which are still executing works from
4114 * before the last CPU down must be on the cpu. After
4115 * this, they may become diasporas.
4116 */
Tejun Heo4ce62e92012-07-13 22:16:44 -07004117 list_for_each_entry(worker, &pool->idle_list, entry)
Tejun Heo403c8212012-07-17 12:39:27 -07004118 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004119
Sasha Levinb67bfe02013-02-27 17:06:00 -08004120 for_each_busy_worker(worker, i, pool)
Tejun Heoc9e7cf22013-01-24 11:01:33 -08004121 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004122
Tejun Heo24647572013-01-24 11:01:33 -08004123 pool->flags |= POOL_DISASSOCIATED;
Tejun Heof2d5a0e2012-07-17 12:39:26 -07004124
Tejun Heo94cf58b2013-01-24 11:01:33 -08004125 spin_unlock_irq(&pool->lock);
Tejun Heobc3a1af2013-03-13 19:47:39 -07004126 mutex_unlock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004127 }
Tejun Heoe22bee72010-06-29 10:07:14 +02004128
4129 /*
Tejun Heo628c78e2012-07-17 12:39:27 -07004130 * Call schedule() so that we cross rq->lock and thus can guarantee
4131 * sched callbacks see the %WORKER_UNBOUND flag. This is necessary
4132 * as scheduler callbacks may be invoked from other cpus.
4133 */
4134 schedule();
4135
4136 /*
4137 * Sched callbacks are disabled now. Zap nr_running. After this,
4138 * nr_running stays zero and need_more_worker() and keep_working()
Tejun Heo38db41d2013-01-24 11:01:34 -08004139 * are always true as long as the worklist is not empty. Pools on
4140 * @cpu now behave as unbound (in terms of concurrency management)
4141 * pools which are served by workers tied to the CPU.
Tejun Heo628c78e2012-07-17 12:39:27 -07004142 *
4143 * On return from this function, the current worker would trigger
4144 * unbound chain execution of pending work items if other workers
4145 * didn't already.
Tejun Heoe22bee72010-06-29 10:07:14 +02004146 */
Tejun Heof02ae732013-03-12 11:30:03 -07004147 for_each_cpu_worker_pool(pool, cpu)
Tejun Heoe19e3972013-01-24 11:39:44 -08004148 atomic_set(&pool->nr_running, 0);
Tejun Heodb7bccf2010-06-29 10:07:12 +02004149}
4150
Tejun Heo8db25e72012-07-17 12:39:28 -07004151/*
4152 * Workqueues should be brought up before normal priority CPU notifiers.
4153 * This will be registered high priority CPU notifier.
4154 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07004155static int __cpuinit workqueue_cpu_up_callback(struct notifier_block *nfb,
Tejun Heo8db25e72012-07-17 12:39:28 -07004156 unsigned long action,
4157 void *hcpu)
Oleg Nesterov3af244332007-05-09 02:34:09 -07004158{
Tejun Heod84ff052013-03-12 11:29:59 -07004159 int cpu = (unsigned long)hcpu;
Tejun Heo4ce62e92012-07-13 22:16:44 -07004160 struct worker_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161
Tejun Heo8db25e72012-07-17 12:39:28 -07004162 switch (action & ~CPU_TASKS_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 case CPU_UP_PREPARE:
Tejun Heof02ae732013-03-12 11:30:03 -07004164 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo3ce63372012-07-17 12:39:27 -07004165 if (pool->nr_workers)
4166 continue;
Tejun Heoebf44d12013-03-13 19:47:39 -07004167 if (create_and_start_worker(pool) < 0)
Tejun Heo3ce63372012-07-17 12:39:27 -07004168 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02004170 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07004171
Tejun Heo65758202012-07-17 12:39:26 -07004172 case CPU_DOWN_FAILED:
4173 case CPU_ONLINE:
Tejun Heof02ae732013-03-12 11:30:03 -07004174 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heobc3a1af2013-03-13 19:47:39 -07004175 mutex_lock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004176 spin_lock_irq(&pool->lock);
4177
Tejun Heo24647572013-01-24 11:01:33 -08004178 pool->flags &= ~POOL_DISASSOCIATED;
Tejun Heo94cf58b2013-01-24 11:01:33 -08004179 rebind_workers(pool);
4180
4181 spin_unlock_irq(&pool->lock);
Tejun Heobc3a1af2013-03-13 19:47:39 -07004182 mutex_unlock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004183 }
Tejun Heo8db25e72012-07-17 12:39:28 -07004184 break;
Tejun Heo65758202012-07-17 12:39:26 -07004185 }
4186 return NOTIFY_OK;
4187}
4188
4189/*
4190 * Workqueues should be brought down after normal priority CPU notifiers.
4191 * This will be registered as low priority CPU notifier.
4192 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07004193static int __cpuinit workqueue_cpu_down_callback(struct notifier_block *nfb,
Tejun Heo65758202012-07-17 12:39:26 -07004194 unsigned long action,
4195 void *hcpu)
4196{
Tejun Heod84ff052013-03-12 11:29:59 -07004197 int cpu = (unsigned long)hcpu;
Tejun Heo8db25e72012-07-17 12:39:28 -07004198 struct work_struct unbind_work;
4199
Tejun Heo65758202012-07-17 12:39:26 -07004200 switch (action & ~CPU_TASKS_FROZEN) {
4201 case CPU_DOWN_PREPARE:
Tejun Heo8db25e72012-07-17 12:39:28 -07004202 /* unbinding should happen on the local CPU */
Tejun Heo706026c2013-01-24 11:01:34 -08004203 INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn);
Joonsoo Kim7635d2f2012-08-15 23:25:41 +09004204 queue_work_on(cpu, system_highpri_wq, &unbind_work);
Tejun Heo8db25e72012-07-17 12:39:28 -07004205 flush_work(&unbind_work);
4206 break;
Tejun Heo65758202012-07-17 12:39:26 -07004207 }
4208 return NOTIFY_OK;
4209}
4210
Rusty Russell2d3854a2008-11-05 13:39:10 +11004211#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08004212
Rusty Russell2d3854a2008-11-05 13:39:10 +11004213struct work_for_cpu {
Tejun Heoed48ece2012-09-18 12:48:43 -07004214 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11004215 long (*fn)(void *);
4216 void *arg;
4217 long ret;
4218};
4219
Tejun Heoed48ece2012-09-18 12:48:43 -07004220static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11004221{
Tejun Heoed48ece2012-09-18 12:48:43 -07004222 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
4223
Rusty Russell2d3854a2008-11-05 13:39:10 +11004224 wfc->ret = wfc->fn(wfc->arg);
4225}
4226
4227/**
4228 * work_on_cpu - run a function in user context on a particular cpu
4229 * @cpu: the cpu to run on
4230 * @fn: the function to run
4231 * @arg: the function arg
4232 *
Rusty Russell31ad9082009-01-16 15:31:15 -08004233 * This will return the value @fn returns.
4234 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06004235 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11004236 */
Tejun Heod84ff052013-03-12 11:29:59 -07004237long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
Rusty Russell2d3854a2008-11-05 13:39:10 +11004238{
Tejun Heoed48ece2012-09-18 12:48:43 -07004239 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11004240
Tejun Heoed48ece2012-09-18 12:48:43 -07004241 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
4242 schedule_work_on(cpu, &wfc.work);
4243 flush_work(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11004244 return wfc.ret;
4245}
4246EXPORT_SYMBOL_GPL(work_on_cpu);
4247#endif /* CONFIG_SMP */
4248
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004249#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10304250
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004251/**
4252 * freeze_workqueues_begin - begin freezing workqueues
4253 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01004254 * Start freezing workqueues. After this function returns, all freezable
Tejun Heoc5aa87b2013-03-13 16:51:36 -07004255 * workqueues will queue new works to their delayed_works list instead of
Tejun Heo706026c2013-01-24 11:01:34 -08004256 * pool->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004257 *
4258 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08004259 * Grabs and releases workqueue_lock and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004260 */
4261void freeze_workqueues_begin(void)
4262{
Tejun Heo17116962013-03-12 11:29:58 -07004263 struct worker_pool *pool;
Tejun Heo24b8a842013-03-12 11:29:58 -07004264 struct workqueue_struct *wq;
4265 struct pool_workqueue *pwq;
Tejun Heo611c92a2013-03-13 16:51:36 -07004266 int pi;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004267
Tejun Heoe98d5b12013-03-12 11:29:57 -07004268 spin_lock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004269
Tejun Heo6183c002013-03-12 11:29:57 -07004270 WARN_ON_ONCE(workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004271 workqueue_freezing = true;
4272
Tejun Heo24b8a842013-03-12 11:29:58 -07004273 /* set FREEZING */
Tejun Heo611c92a2013-03-13 16:51:36 -07004274 for_each_pool(pool, pi) {
Tejun Heo17116962013-03-12 11:29:58 -07004275 spin_lock(&pool->lock);
Tejun Heo17116962013-03-12 11:29:58 -07004276 WARN_ON_ONCE(pool->flags & POOL_FREEZING);
4277 pool->flags |= POOL_FREEZING;
Tejun Heo17116962013-03-12 11:29:58 -07004278 spin_unlock(&pool->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004279 }
4280
Tejun Heo24b8a842013-03-12 11:29:58 -07004281 /* suppress further executions by setting max_active to zero */
4282 list_for_each_entry(wq, &workqueues, list) {
Tejun Heo699ce092013-03-13 16:51:35 -07004283 for_each_pwq(pwq, wq)
4284 pwq_adjust_max_active(pwq);
Tejun Heo24b8a842013-03-12 11:29:58 -07004285 }
4286
Tejun Heoe98d5b12013-03-12 11:29:57 -07004287 spin_unlock_irq(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004289
4290/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01004291 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004292 *
4293 * Check whether freezing is complete. This function must be called
4294 * between freeze_workqueues_begin() and thaw_workqueues().
4295 *
4296 * CONTEXT:
4297 * Grabs and releases workqueue_lock.
4298 *
4299 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01004300 * %true if some freezable workqueues are still busy. %false if freezing
4301 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004302 */
4303bool freeze_workqueues_busy(void)
4304{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004305 bool busy = false;
Tejun Heo24b8a842013-03-12 11:29:58 -07004306 struct workqueue_struct *wq;
4307 struct pool_workqueue *pwq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004308
Tejun Heoe98d5b12013-03-12 11:29:57 -07004309 spin_lock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004310
Tejun Heo6183c002013-03-12 11:29:57 -07004311 WARN_ON_ONCE(!workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004312
Tejun Heo24b8a842013-03-12 11:29:58 -07004313 list_for_each_entry(wq, &workqueues, list) {
4314 if (!(wq->flags & WQ_FREEZABLE))
4315 continue;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004316 /*
4317 * nr_active is monotonically decreasing. It's safe
4318 * to peek without lock.
4319 */
Tejun Heo24b8a842013-03-12 11:29:58 -07004320 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07004321 WARN_ON_ONCE(pwq->nr_active < 0);
Tejun Heo112202d2013-02-13 19:29:12 -08004322 if (pwq->nr_active) {
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004323 busy = true;
4324 goto out_unlock;
4325 }
4326 }
4327 }
4328out_unlock:
Tejun Heoe98d5b12013-03-12 11:29:57 -07004329 spin_unlock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004330 return busy;
4331}
4332
4333/**
4334 * thaw_workqueues - thaw workqueues
4335 *
4336 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo706026c2013-01-24 11:01:34 -08004337 * frozen works are transferred to their respective pool worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004338 *
4339 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08004340 * Grabs and releases workqueue_lock and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004341 */
4342void thaw_workqueues(void)
4343{
Tejun Heo24b8a842013-03-12 11:29:58 -07004344 struct workqueue_struct *wq;
4345 struct pool_workqueue *pwq;
4346 struct worker_pool *pool;
Tejun Heo611c92a2013-03-13 16:51:36 -07004347 int pi;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004348
Tejun Heoe98d5b12013-03-12 11:29:57 -07004349 spin_lock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004350
4351 if (!workqueue_freezing)
4352 goto out_unlock;
4353
Tejun Heo24b8a842013-03-12 11:29:58 -07004354 /* clear FREEZING */
Tejun Heo611c92a2013-03-13 16:51:36 -07004355 for_each_pool(pool, pi) {
Tejun Heo24b8a842013-03-12 11:29:58 -07004356 spin_lock(&pool->lock);
4357 WARN_ON_ONCE(!(pool->flags & POOL_FREEZING));
4358 pool->flags &= ~POOL_FREEZING;
4359 spin_unlock(&pool->lock);
4360 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02004361
Tejun Heo24b8a842013-03-12 11:29:58 -07004362 /* restore max_active and repopulate worklist */
4363 list_for_each_entry(wq, &workqueues, list) {
Tejun Heo699ce092013-03-13 16:51:35 -07004364 for_each_pwq(pwq, wq)
4365 pwq_adjust_max_active(pwq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004366 }
4367
Tejun Heo24b8a842013-03-12 11:29:58 -07004368 /* kick workers */
Tejun Heo611c92a2013-03-13 16:51:36 -07004369 for_each_pool(pool, pi) {
Tejun Heo24b8a842013-03-12 11:29:58 -07004370 spin_lock(&pool->lock);
4371 wake_up_worker(pool);
4372 spin_unlock(&pool->lock);
4373 }
4374
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004375 workqueue_freezing = false;
4376out_unlock:
Tejun Heoe98d5b12013-03-12 11:29:57 -07004377 spin_unlock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004378}
4379#endif /* CONFIG_FREEZER */
4380
Suresh Siddha6ee05782010-07-30 14:57:37 -07004381static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382{
Tejun Heo7a4e3442013-03-12 11:30:00 -07004383 int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL };
4384 int i, cpu;
Tejun Heoc34056a2010-06-29 10:07:11 +02004385
Tejun Heo7c3eed52013-01-24 11:01:33 -08004386 /* make sure we have enough bits for OFFQ pool ID */
4387 BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) <
Lai Jiangshan6be19582013-02-06 18:04:53 -08004388 WORK_CPU_END * NR_STD_WORKER_POOLS);
Tejun Heob5490072012-08-03 10:30:46 -07004389
Tejun Heoe904e6c2013-03-12 11:29:57 -07004390 WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long));
4391
4392 pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC);
4393
Tejun Heo65758202012-07-17 12:39:26 -07004394 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
Lai Jiangshana5b4e572012-09-18 09:59:23 -07004395 hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02004396
Tejun Heo706026c2013-01-24 11:01:34 -08004397 /* initialize CPU pools */
Tejun Heo29c91e92013-03-12 11:30:03 -07004398 for_each_possible_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07004399 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02004400
Tejun Heo7a4e3442013-03-12 11:30:00 -07004401 i = 0;
Tejun Heof02ae732013-03-12 11:30:03 -07004402 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo7a4e3442013-03-12 11:30:00 -07004403 BUG_ON(init_worker_pool(pool));
Tejun Heoec22ca52013-01-24 11:01:33 -08004404 pool->cpu = cpu;
Tejun Heo29c91e92013-03-12 11:30:03 -07004405 cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu));
Tejun Heo7a4e3442013-03-12 11:30:00 -07004406 pool->attrs->nice = std_nice[i++];
4407
Tejun Heo9daf9e62013-01-24 11:01:33 -08004408 /* alloc pool ID */
4409 BUG_ON(worker_pool_assign_id(pool));
Tejun Heo4ce62e92012-07-13 22:16:44 -07004410 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02004411 }
4412
Tejun Heoe22bee72010-06-29 10:07:14 +02004413 /* create the initial worker */
Tejun Heo29c91e92013-03-12 11:30:03 -07004414 for_each_online_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07004415 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02004416
Tejun Heof02ae732013-03-12 11:30:03 -07004417 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo29c91e92013-03-12 11:30:03 -07004418 pool->flags &= ~POOL_DISASSOCIATED;
Tejun Heoebf44d12013-03-13 19:47:39 -07004419 BUG_ON(create_and_start_worker(pool) < 0);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004420 }
Tejun Heoe22bee72010-06-29 10:07:14 +02004421 }
4422
Tejun Heo29c91e92013-03-12 11:30:03 -07004423 /* create default unbound wq attrs */
4424 for (i = 0; i < NR_STD_WORKER_POOLS; i++) {
4425 struct workqueue_attrs *attrs;
4426
4427 BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL)));
4428
4429 attrs->nice = std_nice[i];
4430 cpumask_setall(attrs->cpumask);
4431
4432 unbound_std_wq_attrs[i] = attrs;
4433 }
4434
Tejun Heod320c032010-06-29 10:07:14 +02004435 system_wq = alloc_workqueue("events", 0, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09004436 system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
Tejun Heod320c032010-06-29 10:07:14 +02004437 system_long_wq = alloc_workqueue("events_long", 0, 0);
Tejun Heof3421792010-07-02 10:03:51 +02004438 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
4439 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01004440 system_freezable_wq = alloc_workqueue("events_freezable",
4441 WQ_FREEZABLE, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09004442 BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
Tejun Heoae930e02012-08-20 14:51:23 -07004443 !system_unbound_wq || !system_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07004444 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445}
Suresh Siddha6ee05782010-07-30 14:57:37 -07004446early_initcall(init_workqueues);