blob: c3b59ff220075c8776d536dab203a6190935de1f [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 Heo5bcab332013-03-13 19:47:40 -0700122 * WQ: wq_mutex protected.
Tejun Heo76af4d92013-03-12 11:30:00 -0700123 *
Tejun Heo5bcab332013-03-13 19:47:40 -0700124 * WR: wq_mutex protected for writes. Sched-RCU protected for reads.
125 *
126 * W: workqueue_lock protected.
Tejun Heo75ccf592013-03-12 11:30:04 -0700127 *
128 * FR: wq->flush_mutex and workqueue_lock protected for writes. Sched-RCU
129 * protected for reads.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200130 */
131
Tejun Heo2eaebdb2013-01-18 14:05:55 -0800132/* struct worker is defined in workqueue_internal.h */
Tejun Heoc34056a2010-06-29 10:07:11 +0200133
Tejun Heobd7bdd42012-07-12 14:46:37 -0700134struct worker_pool {
Tejun Heod565ed62013-01-24 11:01:33 -0800135 spinlock_t lock; /* the pool lock */
Tejun Heod84ff052013-03-12 11:29:59 -0700136 int cpu; /* I: the associated cpu */
Tejun Heo9daf9e62013-01-24 11:01:33 -0800137 int id; /* I: pool ID */
Tejun Heo11ebea52012-07-12 14:46:37 -0700138 unsigned int flags; /* X: flags */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700139
140 struct list_head worklist; /* L: list of pending works */
141 int nr_workers; /* L: total number of workers */
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700142
143 /* nr_idle includes the ones off idle_list for rebinding */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700144 int nr_idle; /* L: currently idle ones */
145
146 struct list_head idle_list; /* X: list of idle workers */
147 struct timer_list idle_timer; /* L: worker idle timeout */
148 struct timer_list mayday_timer; /* L: SOS timer for workers */
149
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700150 /* a workers is either on busy_hash or idle_list, or the manager */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800151 DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER);
152 /* L: hash of busy workers */
153
Tejun Heobc3a1af2013-03-13 19:47:39 -0700154 /* see manage_workers() for details on the two manager mutexes */
Tejun Heo34a06bd2013-03-12 11:30:00 -0700155 struct mutex manager_arb; /* manager arbitration */
Tejun Heobc3a1af2013-03-13 19:47:39 -0700156 struct mutex manager_mutex; /* manager exclusion */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700157 struct ida worker_ida; /* L: for worker IDs */
Tejun Heoe19e3972013-01-24 11:39:44 -0800158
Tejun Heo7a4e3442013-03-12 11:30:00 -0700159 struct workqueue_attrs *attrs; /* I: worker attributes */
Tejun Heo5bcab332013-03-13 19:47:40 -0700160 struct hlist_node hash_node; /* WQ: unbound_pool_hash node */
161 int refcnt; /* WQ: refcnt for unbound pools */
Tejun Heo7a4e3442013-03-12 11:30:00 -0700162
Tejun Heoe19e3972013-01-24 11:39:44 -0800163 /*
164 * The current concurrency level. As it's likely to be accessed
165 * from other CPUs during try_to_wake_up(), put it in a separate
166 * cacheline.
167 */
168 atomic_t nr_running ____cacheline_aligned_in_smp;
Tejun Heo29c91e92013-03-12 11:30:03 -0700169
170 /*
171 * Destruction of pool is sched-RCU protected to allow dereferences
172 * from get_work_pool().
173 */
174 struct rcu_head rcu;
Tejun Heo8b03ae32010-06-29 10:07:12 +0200175} ____cacheline_aligned_in_smp;
176
177/*
Tejun Heo112202d2013-02-13 19:29:12 -0800178 * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS
179 * of work_struct->data are used for flags and the remaining high bits
180 * point to the pwq; thus, pwqs need to be aligned at two's power of the
181 * number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 */
Tejun Heo112202d2013-02-13 19:29:12 -0800183struct pool_workqueue {
Tejun Heobd7bdd42012-07-12 14:46:37 -0700184 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200185 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200186 int work_color; /* L: current color */
187 int flush_color; /* L: flushing color */
Tejun Heo8864b4e2013-03-12 11:30:04 -0700188 int refcnt; /* L: reference count */
Tejun Heo73f53c42010-06-29 10:07:11 +0200189 int nr_in_flight[WORK_NR_COLORS];
190 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200191 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200192 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200193 struct list_head delayed_works; /* L: delayed works */
Tejun Heo75ccf592013-03-12 11:30:04 -0700194 struct list_head pwqs_node; /* FR: node on wq->pwqs */
Tejun Heo493a1722013-03-12 11:29:59 -0700195 struct list_head mayday_node; /* W: node on wq->maydays */
Tejun Heo8864b4e2013-03-12 11:30:04 -0700196
197 /*
198 * Release of unbound pwq is punted to system_wq. See put_pwq()
199 * and pwq_unbound_release_workfn() for details. pool_workqueue
200 * itself is also sched-RCU protected so that the first pwq can be
201 * determined without grabbing workqueue_lock.
202 */
203 struct work_struct unbound_release_work;
204 struct rcu_head rcu;
Tejun Heoe904e6c2013-03-12 11:29:57 -0700205} __aligned(1 << WORK_STRUCT_FLAG_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200208 * Structure used to wait for workqueue flush.
209 */
210struct wq_flusher {
211 struct list_head list; /* F: list of flushers */
212 int flush_color; /* F: flush color waiting for */
213 struct completion done; /* flush completion */
214};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Tejun Heo226223a2013-03-12 11:30:05 -0700216struct wq_device;
217
Tejun Heo73f53c42010-06-29 10:07:11 +0200218/*
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700219 * The externally visible workqueue. It relays the issued work items to
220 * the appropriate worker_pool through its pool_workqueues.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 */
222struct workqueue_struct {
Tejun Heo5bcab332013-03-13 19:47:40 -0700223 unsigned int flags; /* WQ: WQ_* flags */
Tejun Heo420c0dd2013-03-12 11:29:59 -0700224 struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwq's */
Tejun Heo75ccf592013-03-12 11:30:04 -0700225 struct list_head pwqs; /* FR: all pwqs of this wq */
Tejun Heo5bcab332013-03-13 19:47:40 -0700226 struct list_head list; /* WQ: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200227
228 struct mutex flush_mutex; /* protects wq flushing */
229 int work_color; /* F: current work color */
230 int flush_color; /* F: current flush color */
Tejun Heo112202d2013-02-13 19:29:12 -0800231 atomic_t nr_pwqs_to_flush; /* flush in progress */
Tejun Heo73f53c42010-06-29 10:07:11 +0200232 struct wq_flusher *first_flusher; /* F: first flusher */
233 struct list_head flusher_queue; /* F: flush waiters */
234 struct list_head flusher_overflow; /* F: flush overflow list */
235
Tejun Heo493a1722013-03-12 11:29:59 -0700236 struct list_head maydays; /* W: pwqs requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200237 struct worker *rescuer; /* I: rescue worker */
238
Tejun Heo5bcab332013-03-13 19:47:40 -0700239 int nr_drainers; /* WQ: drain in progress */
Tejun Heo112202d2013-02-13 19:29:12 -0800240 int saved_max_active; /* W: saved pwq max_active */
Tejun Heo226223a2013-03-12 11:30:05 -0700241
242#ifdef CONFIG_SYSFS
243 struct wq_device *wq_dev; /* I: for sysfs interface */
244#endif
Johannes Berg4e6045f2007-10-18 23:39:55 -0700245#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200246 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700247#endif
Tejun Heob196be82012-01-10 15:11:35 -0800248 char name[]; /* I: workqueue name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249};
250
Tejun Heoe904e6c2013-03-12 11:29:57 -0700251static struct kmem_cache *pwq_cache;
252
Tejun Heo5bcab332013-03-13 19:47:40 -0700253static DEFINE_MUTEX(wq_mutex); /* protects workqueues and pools */
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700254static DEFINE_SPINLOCK(workqueue_lock);
Tejun Heo5bcab332013-03-13 19:47:40 -0700255
256static LIST_HEAD(workqueues); /* WQ: list of all workqueues */
257static bool workqueue_freezing; /* WQ: have wqs started freezing? */
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700258
259/* the per-cpu worker pools */
260static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS],
261 cpu_worker_pools);
262
Tejun Heo5bcab332013-03-13 19:47:40 -0700263static DEFINE_IDR(worker_pool_idr); /* WR: idr of all pools */
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700264
Tejun Heo5bcab332013-03-13 19:47:40 -0700265/* WQ: hash of all unbound pools keyed by pool->attrs */
Tejun Heo29c91e92013-03-12 11:30:03 -0700266static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER);
267
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700268/* I: attributes used when instantiating standard unbound pools on demand */
Tejun Heo29c91e92013-03-12 11:30:03 -0700269static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS];
270
Tejun Heod320c032010-06-29 10:07:14 +0200271struct workqueue_struct *system_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200272EXPORT_SYMBOL_GPL(system_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300273struct workqueue_struct *system_highpri_wq __read_mostly;
Joonsoo Kim1aabe902012-08-15 23:25:39 +0900274EXPORT_SYMBOL_GPL(system_highpri_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300275struct workqueue_struct *system_long_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200276EXPORT_SYMBOL_GPL(system_long_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300277struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200278EXPORT_SYMBOL_GPL(system_unbound_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300279struct workqueue_struct *system_freezable_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100280EXPORT_SYMBOL_GPL(system_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200281
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700282static int worker_thread(void *__worker);
283static void copy_workqueue_attrs(struct workqueue_attrs *to,
284 const struct workqueue_attrs *from);
285
Tejun Heo97bd2342010-10-05 10:41:14 +0200286#define CREATE_TRACE_POINTS
287#include <trace/events/workqueue.h>
288
Tejun Heo5bcab332013-03-13 19:47:40 -0700289#define assert_rcu_or_wq_mutex() \
290 rcu_lockdep_assert(rcu_read_lock_sched_held() || \
291 lockdep_is_held(&wq_mutex), \
292 "sched RCU or wq_mutex should be held")
293
Tejun Heo76af4d92013-03-12 11:30:00 -0700294#define assert_rcu_or_wq_lock() \
295 rcu_lockdep_assert(rcu_read_lock_sched_held() || \
296 lockdep_is_held(&workqueue_lock), \
297 "sched RCU or workqueue lock should be held")
298
Tejun Heof02ae732013-03-12 11:30:03 -0700299#define for_each_cpu_worker_pool(pool, cpu) \
300 for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \
301 (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \
Tejun Heo7a62c2c2013-03-12 11:30:03 -0700302 (pool)++)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700303
Sasha Levinb67bfe02013-02-27 17:06:00 -0800304#define for_each_busy_worker(worker, i, pool) \
305 hash_for_each(pool->busy_hash, i, worker, hentry)
Tejun Heodb7bccf2010-06-29 10:07:12 +0200306
Tejun Heo49e3cf42013-03-12 11:29:58 -0700307/**
Tejun Heo17116962013-03-12 11:29:58 -0700308 * for_each_pool - iterate through all worker_pools in the system
309 * @pool: iteration cursor
Tejun Heo611c92a2013-03-13 16:51:36 -0700310 * @pi: integer used for iteration
Tejun Heofa1b54e2013-03-12 11:30:00 -0700311 *
Tejun Heo5bcab332013-03-13 19:47:40 -0700312 * This must be called either with wq_mutex held or sched RCU read locked.
313 * If the pool needs to be used beyond the locking in effect, the caller is
314 * responsible for guaranteeing that the pool stays online.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700315 *
316 * The if/else clause exists only for the lockdep assertion and can be
317 * ignored.
Tejun Heo17116962013-03-12 11:29:58 -0700318 */
Tejun Heo611c92a2013-03-13 16:51:36 -0700319#define for_each_pool(pool, pi) \
320 idr_for_each_entry(&worker_pool_idr, pool, pi) \
Tejun Heo5bcab332013-03-13 19:47:40 -0700321 if (({ assert_rcu_or_wq_mutex(); false; })) { } \
Tejun Heofa1b54e2013-03-12 11:30:00 -0700322 else
Tejun Heo17116962013-03-12 11:29:58 -0700323
324/**
Tejun Heo49e3cf42013-03-12 11:29:58 -0700325 * for_each_pwq - iterate through all pool_workqueues of the specified workqueue
326 * @pwq: iteration cursor
327 * @wq: the target workqueue
Tejun Heo76af4d92013-03-12 11:30:00 -0700328 *
329 * This must be called either with workqueue_lock held or sched RCU read
330 * locked. If the pwq needs to be used beyond the locking in effect, the
331 * caller is responsible for guaranteeing that the pwq stays online.
332 *
333 * The if/else clause exists only for the lockdep assertion and can be
334 * ignored.
Tejun Heo49e3cf42013-03-12 11:29:58 -0700335 */
336#define for_each_pwq(pwq, wq) \
Tejun Heo76af4d92013-03-12 11:30:00 -0700337 list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node) \
338 if (({ assert_rcu_or_wq_lock(); false; })) { } \
339 else
Tejun Heof3421792010-07-02 10:03:51 +0200340
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900341#ifdef CONFIG_DEBUG_OBJECTS_WORK
342
343static struct debug_obj_descr work_debug_descr;
344
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100345static void *work_debug_hint(void *addr)
346{
347 return ((struct work_struct *) addr)->func;
348}
349
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900350/*
351 * fixup_init is called when:
352 * - an active object is initialized
353 */
354static int work_fixup_init(void *addr, enum debug_obj_state state)
355{
356 struct work_struct *work = addr;
357
358 switch (state) {
359 case ODEBUG_STATE_ACTIVE:
360 cancel_work_sync(work);
361 debug_object_init(work, &work_debug_descr);
362 return 1;
363 default:
364 return 0;
365 }
366}
367
368/*
369 * fixup_activate is called when:
370 * - an active object is activated
371 * - an unknown object is activated (might be a statically initialized object)
372 */
373static int work_fixup_activate(void *addr, enum debug_obj_state state)
374{
375 struct work_struct *work = addr;
376
377 switch (state) {
378
379 case ODEBUG_STATE_NOTAVAILABLE:
380 /*
381 * This is not really a fixup. The work struct was
382 * statically initialized. We just make sure that it
383 * is tracked in the object tracker.
384 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200385 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900386 debug_object_init(work, &work_debug_descr);
387 debug_object_activate(work, &work_debug_descr);
388 return 0;
389 }
390 WARN_ON_ONCE(1);
391 return 0;
392
393 case ODEBUG_STATE_ACTIVE:
394 WARN_ON(1);
395
396 default:
397 return 0;
398 }
399}
400
401/*
402 * fixup_free is called when:
403 * - an active object is freed
404 */
405static int work_fixup_free(void *addr, enum debug_obj_state state)
406{
407 struct work_struct *work = addr;
408
409 switch (state) {
410 case ODEBUG_STATE_ACTIVE:
411 cancel_work_sync(work);
412 debug_object_free(work, &work_debug_descr);
413 return 1;
414 default:
415 return 0;
416 }
417}
418
419static struct debug_obj_descr work_debug_descr = {
420 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100421 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900422 .fixup_init = work_fixup_init,
423 .fixup_activate = work_fixup_activate,
424 .fixup_free = work_fixup_free,
425};
426
427static inline void debug_work_activate(struct work_struct *work)
428{
429 debug_object_activate(work, &work_debug_descr);
430}
431
432static inline void debug_work_deactivate(struct work_struct *work)
433{
434 debug_object_deactivate(work, &work_debug_descr);
435}
436
437void __init_work(struct work_struct *work, int onstack)
438{
439 if (onstack)
440 debug_object_init_on_stack(work, &work_debug_descr);
441 else
442 debug_object_init(work, &work_debug_descr);
443}
444EXPORT_SYMBOL_GPL(__init_work);
445
446void destroy_work_on_stack(struct work_struct *work)
447{
448 debug_object_free(work, &work_debug_descr);
449}
450EXPORT_SYMBOL_GPL(destroy_work_on_stack);
451
452#else
453static inline void debug_work_activate(struct work_struct *work) { }
454static inline void debug_work_deactivate(struct work_struct *work) { }
455#endif
456
Tejun Heo9daf9e62013-01-24 11:01:33 -0800457/* allocate ID and assign it to @pool */
458static int worker_pool_assign_id(struct worker_pool *pool)
459{
460 int ret;
461
Tejun Heo5bcab332013-03-13 19:47:40 -0700462 lockdep_assert_held(&wq_mutex);
463
Tejun Heofa1b54e2013-03-12 11:30:00 -0700464 do {
465 if (!idr_pre_get(&worker_pool_idr, GFP_KERNEL))
466 return -ENOMEM;
Tejun Heofa1b54e2013-03-12 11:30:00 -0700467 ret = idr_get_new(&worker_pool_idr, pool, &pool->id);
Tejun Heofa1b54e2013-03-12 11:30:00 -0700468 } while (ret == -EAGAIN);
Tejun Heo9daf9e62013-01-24 11:01:33 -0800469
470 return ret;
471}
472
Tejun Heo76af4d92013-03-12 11:30:00 -0700473/**
474 * first_pwq - return the first pool_workqueue of the specified workqueue
475 * @wq: the target workqueue
476 *
477 * This must be called either with workqueue_lock held or sched RCU read
478 * locked. If the pwq needs to be used beyond the locking in effect, the
479 * caller is responsible for guaranteeing that the pwq stays online.
480 */
Tejun Heo7fb98ea2013-03-12 11:30:00 -0700481static struct pool_workqueue *first_pwq(struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700482{
Tejun Heo76af4d92013-03-12 11:30:00 -0700483 assert_rcu_or_wq_lock();
484 return list_first_or_null_rcu(&wq->pwqs, struct pool_workqueue,
485 pwqs_node);
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700486}
487
Tejun Heo73f53c42010-06-29 10:07:11 +0200488static unsigned int work_color_to_flags(int color)
489{
490 return color << WORK_STRUCT_COLOR_SHIFT;
491}
492
493static int get_work_color(struct work_struct *work)
494{
495 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
496 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
497}
498
499static int work_next_color(int color)
500{
501 return (color + 1) % WORK_NR_COLORS;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700502}
503
David Howells4594bf12006-12-07 11:33:26 +0000504/*
Tejun Heo112202d2013-02-13 19:29:12 -0800505 * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data
506 * contain the pointer to the queued pwq. Once execution starts, the flag
Tejun Heo7c3eed52013-01-24 11:01:33 -0800507 * is cleared and the high bits contain OFFQ flags and pool ID.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200508 *
Tejun Heo112202d2013-02-13 19:29:12 -0800509 * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling()
510 * and clear_work_data() can be used to set the pwq, pool or clear
Tejun Heobbb68df2012-08-03 10:30:46 -0700511 * work->data. These functions should only be called while the work is
512 * owned - ie. while the PENDING bit is set.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200513 *
Tejun Heo112202d2013-02-13 19:29:12 -0800514 * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq
Tejun Heo7c3eed52013-01-24 11:01:33 -0800515 * corresponding to a work. Pool is available once the work has been
Tejun Heo112202d2013-02-13 19:29:12 -0800516 * queued anywhere after initialization until it is sync canceled. pwq is
Tejun Heo7c3eed52013-01-24 11:01:33 -0800517 * available only while the work item is queued.
Tejun Heobbb68df2012-08-03 10:30:46 -0700518 *
519 * %WORK_OFFQ_CANCELING is used to mark a work item which is being
520 * canceled. While being canceled, a work item may have its PENDING set
521 * but stay off timer and worklist for arbitrarily long and nobody should
522 * try to steal the PENDING bit.
David Howells4594bf12006-12-07 11:33:26 +0000523 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200524static inline void set_work_data(struct work_struct *work, unsigned long data,
525 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000526{
Tejun Heo6183c002013-03-12 11:29:57 -0700527 WARN_ON_ONCE(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200528 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000529}
David Howells365970a2006-11-22 14:54:49 +0000530
Tejun Heo112202d2013-02-13 19:29:12 -0800531static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq,
Tejun Heo7a22ad72010-06-29 10:07:13 +0200532 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200533{
Tejun Heo112202d2013-02-13 19:29:12 -0800534 set_work_data(work, (unsigned long)pwq,
535 WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200536}
537
Lai Jiangshan4468a002013-02-06 18:04:53 -0800538static void set_work_pool_and_keep_pending(struct work_struct *work,
539 int pool_id)
540{
541 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT,
542 WORK_STRUCT_PENDING);
543}
544
Tejun Heo7c3eed52013-01-24 11:01:33 -0800545static void set_work_pool_and_clear_pending(struct work_struct *work,
546 int pool_id)
David Howells365970a2006-11-22 14:54:49 +0000547{
Tejun Heo23657bb2012-08-13 17:08:19 -0700548 /*
549 * The following wmb is paired with the implied mb in
550 * test_and_set_bit(PENDING) and ensures all updates to @work made
551 * here are visible to and precede any updates by the next PENDING
552 * owner.
553 */
554 smp_wmb();
Tejun Heo7c3eed52013-01-24 11:01:33 -0800555 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200556}
557
558static void clear_work_data(struct work_struct *work)
559{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800560 smp_wmb(); /* see set_work_pool_and_clear_pending() */
561 set_work_data(work, WORK_STRUCT_NO_POOL, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200562}
563
Tejun Heo112202d2013-02-13 19:29:12 -0800564static struct pool_workqueue *get_work_pwq(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200565{
Tejun Heoe1201532010-07-22 14:14:25 +0200566 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200567
Tejun Heo112202d2013-02-13 19:29:12 -0800568 if (data & WORK_STRUCT_PWQ)
Tejun Heoe1201532010-07-22 14:14:25 +0200569 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
570 else
571 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200572}
573
Tejun Heo7c3eed52013-01-24 11:01:33 -0800574/**
575 * get_work_pool - return the worker_pool a given work was associated with
576 * @work: the work item of interest
577 *
578 * Return the worker_pool @work was last associated with. %NULL if none.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700579 *
Tejun Heo5bcab332013-03-13 19:47:40 -0700580 * Pools are created and destroyed under wq_mutex, and allows read access
581 * under sched-RCU read lock. As such, this function should be called
582 * under wq_mutex or with preemption disabled.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700583 *
584 * All fields of the returned pool are accessible as long as the above
585 * mentioned locking is in effect. If the returned pool needs to be used
586 * beyond the critical section, the caller is responsible for ensuring the
587 * returned pool is and stays online.
Tejun Heo7c3eed52013-01-24 11:01:33 -0800588 */
589static struct worker_pool *get_work_pool(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200590{
Tejun Heoe1201532010-07-22 14:14:25 +0200591 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800592 int pool_id;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200593
Tejun Heo5bcab332013-03-13 19:47:40 -0700594 assert_rcu_or_wq_mutex();
Tejun Heofa1b54e2013-03-12 11:30:00 -0700595
Tejun Heo112202d2013-02-13 19:29:12 -0800596 if (data & WORK_STRUCT_PWQ)
597 return ((struct pool_workqueue *)
Tejun Heo7c3eed52013-01-24 11:01:33 -0800598 (data & WORK_STRUCT_WQ_DATA_MASK))->pool;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200599
Tejun Heo7c3eed52013-01-24 11:01:33 -0800600 pool_id = data >> WORK_OFFQ_POOL_SHIFT;
601 if (pool_id == WORK_OFFQ_POOL_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200602 return NULL;
603
Tejun Heofa1b54e2013-03-12 11:30:00 -0700604 return idr_find(&worker_pool_idr, pool_id);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800605}
606
607/**
608 * get_work_pool_id - return the worker pool ID a given work is associated with
609 * @work: the work item of interest
610 *
611 * Return the worker_pool ID @work was last associated with.
612 * %WORK_OFFQ_POOL_NONE if none.
613 */
614static int get_work_pool_id(struct work_struct *work)
615{
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800616 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800617
Tejun Heo112202d2013-02-13 19:29:12 -0800618 if (data & WORK_STRUCT_PWQ)
619 return ((struct pool_workqueue *)
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800620 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id;
621
622 return data >> WORK_OFFQ_POOL_SHIFT;
Tejun Heo7c3eed52013-01-24 11:01:33 -0800623}
624
Tejun Heobbb68df2012-08-03 10:30:46 -0700625static void mark_work_canceling(struct work_struct *work)
626{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800627 unsigned long pool_id = get_work_pool_id(work);
Tejun Heobbb68df2012-08-03 10:30:46 -0700628
Tejun Heo7c3eed52013-01-24 11:01:33 -0800629 pool_id <<= WORK_OFFQ_POOL_SHIFT;
630 set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700631}
632
633static bool work_is_canceling(struct work_struct *work)
634{
635 unsigned long data = atomic_long_read(&work->data);
636
Tejun Heo112202d2013-02-13 19:29:12 -0800637 return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700638}
639
David Howells365970a2006-11-22 14:54:49 +0000640/*
Tejun Heo32704762012-07-13 22:16:45 -0700641 * Policy functions. These define the policies on how the global worker
642 * pools are managed. Unless noted otherwise, these functions assume that
Tejun Heod565ed62013-01-24 11:01:33 -0800643 * they're being called with pool->lock held.
David Howells365970a2006-11-22 14:54:49 +0000644 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200645
Tejun Heo63d95a92012-07-12 14:46:37 -0700646static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000647{
Tejun Heoe19e3972013-01-24 11:39:44 -0800648 return !atomic_read(&pool->nr_running);
David Howells365970a2006-11-22 14:54:49 +0000649}
650
Tejun Heoe22bee72010-06-29 10:07:14 +0200651/*
652 * Need to wake up a worker? Called from anything but currently
653 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700654 *
655 * Note that, because unbound workers never contribute to nr_running, this
Tejun Heo706026c2013-01-24 11:01:34 -0800656 * function will always return %true for unbound pools as long as the
Tejun Heo974271c2012-07-12 14:46:37 -0700657 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200658 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700659static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000660{
Tejun Heo63d95a92012-07-12 14:46:37 -0700661 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000662}
663
Tejun Heoe22bee72010-06-29 10:07:14 +0200664/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700665static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200666{
Tejun Heo63d95a92012-07-12 14:46:37 -0700667 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200668}
669
670/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700671static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200672{
Tejun Heoe19e3972013-01-24 11:39:44 -0800673 return !list_empty(&pool->worklist) &&
674 atomic_read(&pool->nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200675}
676
677/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700678static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200679{
Tejun Heo63d95a92012-07-12 14:46:37 -0700680 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200681}
682
683/* Do I need to be the manager? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700684static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200685{
Tejun Heo63d95a92012-07-12 14:46:37 -0700686 return need_to_create_worker(pool) ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700687 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200688}
689
690/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700691static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200692{
Tejun Heo34a06bd2013-03-12 11:30:00 -0700693 bool managing = mutex_is_locked(&pool->manager_arb);
Tejun Heo63d95a92012-07-12 14:46:37 -0700694 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
695 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200696
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700697 /*
698 * nr_idle and idle_list may disagree if idle rebinding is in
699 * progress. Never return %true if idle_list is empty.
700 */
701 if (list_empty(&pool->idle_list))
702 return false;
703
Tejun Heoe22bee72010-06-29 10:07:14 +0200704 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
705}
706
707/*
708 * Wake up functions.
709 */
710
Tejun Heo7e116292010-06-29 10:07:13 +0200711/* Return the first worker. Safe with preemption disabled */
Tejun Heo63d95a92012-07-12 14:46:37 -0700712static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200713{
Tejun Heo63d95a92012-07-12 14:46:37 -0700714 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200715 return NULL;
716
Tejun Heo63d95a92012-07-12 14:46:37 -0700717 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200718}
719
720/**
721 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700722 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200723 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700724 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200725 *
726 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800727 * spin_lock_irq(pool->lock).
Tejun Heo7e116292010-06-29 10:07:13 +0200728 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700729static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200730{
Tejun Heo63d95a92012-07-12 14:46:37 -0700731 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200732
733 if (likely(worker))
734 wake_up_process(worker->task);
735}
736
Tejun Heo4690c4a2010-06-29 10:07:10 +0200737/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200738 * wq_worker_waking_up - a worker is waking up
739 * @task: task waking up
740 * @cpu: CPU @task is waking up to
741 *
742 * This function is called during try_to_wake_up() when a worker is
743 * being awoken.
744 *
745 * CONTEXT:
746 * spin_lock_irq(rq->lock)
747 */
Tejun Heod84ff052013-03-12 11:29:59 -0700748void wq_worker_waking_up(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200749{
750 struct worker *worker = kthread_data(task);
751
Joonsoo Kim36576002012-10-26 23:03:49 +0900752 if (!(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoec22ca52013-01-24 11:01:33 -0800753 WARN_ON_ONCE(worker->pool->cpu != cpu);
Tejun Heoe19e3972013-01-24 11:39:44 -0800754 atomic_inc(&worker->pool->nr_running);
Joonsoo Kim36576002012-10-26 23:03:49 +0900755 }
Tejun Heoe22bee72010-06-29 10:07:14 +0200756}
757
758/**
759 * wq_worker_sleeping - a worker is going to sleep
760 * @task: task going to sleep
761 * @cpu: CPU in question, must be the current CPU number
762 *
763 * This function is called during schedule() when a busy worker is
764 * going to sleep. Worker on the same cpu can be woken up by
765 * returning pointer to its task.
766 *
767 * CONTEXT:
768 * spin_lock_irq(rq->lock)
769 *
770 * RETURNS:
771 * Worker task on @cpu to wake up, %NULL if none.
772 */
Tejun Heod84ff052013-03-12 11:29:59 -0700773struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200774{
775 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heo111c2252013-01-17 17:16:24 -0800776 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200777
Tejun Heo111c2252013-01-17 17:16:24 -0800778 /*
779 * Rescuers, which may not have all the fields set up like normal
780 * workers, also reach here, let's not access anything before
781 * checking NOT_RUNNING.
782 */
Steven Rostedt2d646722010-12-03 23:12:33 -0500783 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200784 return NULL;
785
Tejun Heo111c2252013-01-17 17:16:24 -0800786 pool = worker->pool;
Tejun Heo111c2252013-01-17 17:16:24 -0800787
Tejun Heoe22bee72010-06-29 10:07:14 +0200788 /* this can only happen on the local cpu */
Tejun Heo6183c002013-03-12 11:29:57 -0700789 if (WARN_ON_ONCE(cpu != raw_smp_processor_id()))
790 return NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +0200791
792 /*
793 * The counterpart of the following dec_and_test, implied mb,
794 * worklist not empty test sequence is in insert_work().
795 * Please read comment there.
796 *
Tejun Heo628c78e2012-07-17 12:39:27 -0700797 * NOT_RUNNING is clear. This means that we're bound to and
798 * running on the local cpu w/ rq lock held and preemption
799 * disabled, which in turn means that none else could be
Tejun Heod565ed62013-01-24 11:01:33 -0800800 * manipulating idle_list, so dereferencing idle_list without pool
Tejun Heo628c78e2012-07-17 12:39:27 -0700801 * lock is safe.
Tejun Heoe22bee72010-06-29 10:07:14 +0200802 */
Tejun Heoe19e3972013-01-24 11:39:44 -0800803 if (atomic_dec_and_test(&pool->nr_running) &&
804 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700805 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200806 return to_wakeup ? to_wakeup->task : NULL;
807}
808
809/**
810 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200811 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200812 * @flags: flags to set
813 * @wakeup: wakeup an idle worker if necessary
814 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200815 * Set @flags in @worker->flags and adjust nr_running accordingly. If
816 * nr_running becomes zero and @wakeup is %true, an idle worker is
817 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200818 *
Tejun Heocb444762010-07-02 10:03:50 +0200819 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800820 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200821 */
822static inline void worker_set_flags(struct worker *worker, unsigned int flags,
823 bool wakeup)
824{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700825 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200826
Tejun Heocb444762010-07-02 10:03:50 +0200827 WARN_ON_ONCE(worker->task != current);
828
Tejun Heoe22bee72010-06-29 10:07:14 +0200829 /*
830 * If transitioning into NOT_RUNNING, adjust nr_running and
831 * wake up an idle worker as necessary if requested by
832 * @wakeup.
833 */
834 if ((flags & WORKER_NOT_RUNNING) &&
835 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoe22bee72010-06-29 10:07:14 +0200836 if (wakeup) {
Tejun Heoe19e3972013-01-24 11:39:44 -0800837 if (atomic_dec_and_test(&pool->nr_running) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -0700838 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700839 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200840 } else
Tejun Heoe19e3972013-01-24 11:39:44 -0800841 atomic_dec(&pool->nr_running);
Tejun Heoe22bee72010-06-29 10:07:14 +0200842 }
843
Tejun Heod302f012010-06-29 10:07:13 +0200844 worker->flags |= flags;
845}
846
847/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200848 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200849 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200850 * @flags: flags to clear
851 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200852 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200853 *
Tejun Heocb444762010-07-02 10:03:50 +0200854 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800855 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200856 */
857static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
858{
Tejun Heo63d95a92012-07-12 14:46:37 -0700859 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200860 unsigned int oflags = worker->flags;
861
Tejun Heocb444762010-07-02 10:03:50 +0200862 WARN_ON_ONCE(worker->task != current);
863
Tejun Heod302f012010-06-29 10:07:13 +0200864 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200865
Tejun Heo42c025f2011-01-11 15:58:49 +0100866 /*
867 * If transitioning out of NOT_RUNNING, increment nr_running. Note
868 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
869 * of multiple flags, not a single flag.
870 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200871 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
872 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heoe19e3972013-01-24 11:39:44 -0800873 atomic_inc(&pool->nr_running);
Tejun Heod302f012010-06-29 10:07:13 +0200874}
875
876/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200877 * find_worker_executing_work - find worker which is executing a work
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800878 * @pool: pool of interest
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200879 * @work: work to find worker for
880 *
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800881 * Find a worker which is executing @work on @pool by searching
882 * @pool->busy_hash which is keyed by the address of @work. For a worker
Tejun Heoa2c1c572012-12-18 10:35:02 -0800883 * to match, its current execution should match the address of @work and
884 * its work function. This is to avoid unwanted dependency between
885 * unrelated work executions through a work item being recycled while still
886 * being executed.
887 *
888 * This is a bit tricky. A work item may be freed once its execution
889 * starts and nothing prevents the freed area from being recycled for
890 * another work item. If the same work item address ends up being reused
891 * before the original execution finishes, workqueue will identify the
892 * recycled work item as currently executing and make it wait until the
893 * current execution finishes, introducing an unwanted dependency.
894 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700895 * This function checks the work item address and work function to avoid
896 * false positives. Note that this isn't complete as one may construct a
897 * work function which can introduce dependency onto itself through a
898 * recycled work item. Well, if somebody wants to shoot oneself in the
899 * foot that badly, there's only so much we can do, and if such deadlock
900 * actually occurs, it should be easy to locate the culprit work function.
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200901 *
902 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800903 * spin_lock_irq(pool->lock).
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200904 *
905 * RETURNS:
906 * Pointer to worker which is executing @work if found, NULL
907 * otherwise.
908 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800909static struct worker *find_worker_executing_work(struct worker_pool *pool,
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200910 struct work_struct *work)
911{
Sasha Levin42f85702012-12-17 10:01:23 -0500912 struct worker *worker;
Sasha Levin42f85702012-12-17 10:01:23 -0500913
Sasha Levinb67bfe02013-02-27 17:06:00 -0800914 hash_for_each_possible(pool->busy_hash, worker, hentry,
Tejun Heoa2c1c572012-12-18 10:35:02 -0800915 (unsigned long)work)
916 if (worker->current_work == work &&
917 worker->current_func == work->func)
Sasha Levin42f85702012-12-17 10:01:23 -0500918 return worker;
919
920 return NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200921}
922
923/**
Tejun Heobf4ede02012-08-03 10:30:46 -0700924 * move_linked_works - move linked works to a list
925 * @work: start of series of works to be scheduled
926 * @head: target list to append @work to
927 * @nextp: out paramter for nested worklist walking
928 *
929 * Schedule linked works starting from @work to @head. Work series to
930 * be scheduled starts at @work and includes any consecutive work with
931 * WORK_STRUCT_LINKED set in its predecessor.
932 *
933 * If @nextp is not NULL, it's updated to point to the next work of
934 * the last scheduled work. This allows move_linked_works() to be
935 * nested inside outer list_for_each_entry_safe().
936 *
937 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800938 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -0700939 */
940static void move_linked_works(struct work_struct *work, struct list_head *head,
941 struct work_struct **nextp)
942{
943 struct work_struct *n;
944
945 /*
946 * Linked worklist will always end before the end of the list,
947 * use NULL for list head.
948 */
949 list_for_each_entry_safe_from(work, n, NULL, entry) {
950 list_move_tail(&work->entry, head);
951 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
952 break;
953 }
954
955 /*
956 * If we're already inside safe list traversal and have moved
957 * multiple works to the scheduled queue, the next position
958 * needs to be updated.
959 */
960 if (nextp)
961 *nextp = n;
962}
963
Tejun Heo8864b4e2013-03-12 11:30:04 -0700964/**
965 * get_pwq - get an extra reference on the specified pool_workqueue
966 * @pwq: pool_workqueue to get
967 *
968 * Obtain an extra reference on @pwq. The caller should guarantee that
969 * @pwq has positive refcnt and be holding the matching pool->lock.
970 */
971static void get_pwq(struct pool_workqueue *pwq)
972{
973 lockdep_assert_held(&pwq->pool->lock);
974 WARN_ON_ONCE(pwq->refcnt <= 0);
975 pwq->refcnt++;
976}
977
978/**
979 * put_pwq - put a pool_workqueue reference
980 * @pwq: pool_workqueue to put
981 *
982 * Drop a reference of @pwq. If its refcnt reaches zero, schedule its
983 * destruction. The caller should be holding the matching pool->lock.
984 */
985static void put_pwq(struct pool_workqueue *pwq)
986{
987 lockdep_assert_held(&pwq->pool->lock);
988 if (likely(--pwq->refcnt))
989 return;
990 if (WARN_ON_ONCE(!(pwq->wq->flags & WQ_UNBOUND)))
991 return;
992 /*
993 * @pwq can't be released under pool->lock, bounce to
994 * pwq_unbound_release_workfn(). This never recurses on the same
995 * pool->lock as this path is taken only for unbound workqueues and
996 * the release work item is scheduled on a per-cpu workqueue. To
997 * avoid lockdep warning, unbound pool->locks are given lockdep
998 * subclass of 1 in get_unbound_pool().
999 */
1000 schedule_work(&pwq->unbound_release_work);
1001}
1002
Tejun Heo112202d2013-02-13 19:29:12 -08001003static void pwq_activate_delayed_work(struct work_struct *work)
Tejun Heobf4ede02012-08-03 10:30:46 -07001004{
Tejun Heo112202d2013-02-13 19:29:12 -08001005 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobf4ede02012-08-03 10:30:46 -07001006
1007 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001008 move_linked_works(work, &pwq->pool->worklist, NULL);
Tejun Heobf4ede02012-08-03 10:30:46 -07001009 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo112202d2013-02-13 19:29:12 -08001010 pwq->nr_active++;
Tejun Heobf4ede02012-08-03 10:30:46 -07001011}
1012
Tejun Heo112202d2013-02-13 19:29:12 -08001013static void pwq_activate_first_delayed(struct pool_workqueue *pwq)
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001014{
Tejun Heo112202d2013-02-13 19:29:12 -08001015 struct work_struct *work = list_first_entry(&pwq->delayed_works,
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001016 struct work_struct, entry);
1017
Tejun Heo112202d2013-02-13 19:29:12 -08001018 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001019}
1020
Tejun Heobf4ede02012-08-03 10:30:46 -07001021/**
Tejun Heo112202d2013-02-13 19:29:12 -08001022 * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight
1023 * @pwq: pwq of interest
Tejun Heobf4ede02012-08-03 10:30:46 -07001024 * @color: color of work which left the queue
Tejun Heobf4ede02012-08-03 10:30:46 -07001025 *
1026 * A work either has completed or is removed from pending queue,
Tejun Heo112202d2013-02-13 19:29:12 -08001027 * decrement nr_in_flight of its pwq and handle workqueue flushing.
Tejun Heobf4ede02012-08-03 10:30:46 -07001028 *
1029 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001030 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -07001031 */
Tejun Heo112202d2013-02-13 19:29:12 -08001032static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color)
Tejun Heobf4ede02012-08-03 10:30:46 -07001033{
Tejun Heo8864b4e2013-03-12 11:30:04 -07001034 /* uncolored work items don't participate in flushing or nr_active */
Tejun Heobf4ede02012-08-03 10:30:46 -07001035 if (color == WORK_NO_COLOR)
Tejun Heo8864b4e2013-03-12 11:30:04 -07001036 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001037
Tejun Heo112202d2013-02-13 19:29:12 -08001038 pwq->nr_in_flight[color]--;
Tejun Heobf4ede02012-08-03 10:30:46 -07001039
Tejun Heo112202d2013-02-13 19:29:12 -08001040 pwq->nr_active--;
1041 if (!list_empty(&pwq->delayed_works)) {
Lai Jiangshanb3f9f402012-09-18 10:40:00 -07001042 /* one down, submit a delayed one */
Tejun Heo112202d2013-02-13 19:29:12 -08001043 if (pwq->nr_active < pwq->max_active)
1044 pwq_activate_first_delayed(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001045 }
1046
1047 /* is flush in progress and are we at the flushing tip? */
Tejun Heo112202d2013-02-13 19:29:12 -08001048 if (likely(pwq->flush_color != color))
Tejun Heo8864b4e2013-03-12 11:30:04 -07001049 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001050
1051 /* are there still in-flight works? */
Tejun Heo112202d2013-02-13 19:29:12 -08001052 if (pwq->nr_in_flight[color])
Tejun Heo8864b4e2013-03-12 11:30:04 -07001053 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001054
Tejun Heo112202d2013-02-13 19:29:12 -08001055 /* this pwq is done, clear flush_color */
1056 pwq->flush_color = -1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001057
1058 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001059 * If this was the last pwq, wake up the first flusher. It
Tejun Heobf4ede02012-08-03 10:30:46 -07001060 * will handle the rest.
1061 */
Tejun Heo112202d2013-02-13 19:29:12 -08001062 if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush))
1063 complete(&pwq->wq->first_flusher->done);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001064out_put:
1065 put_pwq(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001066}
1067
Tejun Heo36e227d2012-08-03 10:30:46 -07001068/**
Tejun Heobbb68df2012-08-03 10:30:46 -07001069 * try_to_grab_pending - steal work item from worklist and disable irq
Tejun Heo36e227d2012-08-03 10:30:46 -07001070 * @work: work item to steal
1071 * @is_dwork: @work is a delayed_work
Tejun Heobbb68df2012-08-03 10:30:46 -07001072 * @flags: place to store irq state
Tejun Heo36e227d2012-08-03 10:30:46 -07001073 *
1074 * Try to grab PENDING bit of @work. This function can handle @work in any
1075 * stable state - idle, on timer or on worklist. Return values are
1076 *
1077 * 1 if @work was pending and we successfully stole PENDING
1078 * 0 if @work was idle and we claimed PENDING
1079 * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry
Tejun Heobbb68df2012-08-03 10:30:46 -07001080 * -ENOENT if someone else is canceling @work, this state may persist
1081 * for arbitrarily long
Tejun Heo36e227d2012-08-03 10:30:46 -07001082 *
Tejun Heobbb68df2012-08-03 10:30:46 -07001083 * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001084 * interrupted while holding PENDING and @work off queue, irq must be
1085 * disabled on entry. This, combined with delayed_work->timer being
1086 * irqsafe, ensures that we return -EAGAIN for finite short period of time.
Tejun Heobbb68df2012-08-03 10:30:46 -07001087 *
1088 * On successful return, >= 0, irq is disabled and the caller is
1089 * responsible for releasing it using local_irq_restore(*@flags).
1090 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001091 * This function is safe to call from any context including IRQ handler.
Tejun Heobf4ede02012-08-03 10:30:46 -07001092 */
Tejun Heobbb68df2012-08-03 10:30:46 -07001093static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
1094 unsigned long *flags)
Tejun Heobf4ede02012-08-03 10:30:46 -07001095{
Tejun Heod565ed62013-01-24 11:01:33 -08001096 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08001097 struct pool_workqueue *pwq;
Tejun Heobf4ede02012-08-03 10:30:46 -07001098
Tejun Heobbb68df2012-08-03 10:30:46 -07001099 local_irq_save(*flags);
1100
Tejun Heo36e227d2012-08-03 10:30:46 -07001101 /* try to steal the timer if it exists */
1102 if (is_dwork) {
1103 struct delayed_work *dwork = to_delayed_work(work);
1104
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001105 /*
1106 * dwork->timer is irqsafe. If del_timer() fails, it's
1107 * guaranteed that the timer is not queued anywhere and not
1108 * running on the local CPU.
1109 */
Tejun Heo36e227d2012-08-03 10:30:46 -07001110 if (likely(del_timer(&dwork->timer)))
1111 return 1;
1112 }
1113
1114 /* try to claim PENDING the normal way */
Tejun Heobf4ede02012-08-03 10:30:46 -07001115 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1116 return 0;
1117
1118 /*
1119 * The queueing is in progress, or it is already queued. Try to
1120 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1121 */
Tejun Heod565ed62013-01-24 11:01:33 -08001122 pool = get_work_pool(work);
1123 if (!pool)
Tejun Heobbb68df2012-08-03 10:30:46 -07001124 goto fail;
Tejun Heobf4ede02012-08-03 10:30:46 -07001125
Tejun Heod565ed62013-01-24 11:01:33 -08001126 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001127 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001128 * work->data is guaranteed to point to pwq only while the work
1129 * item is queued on pwq->wq, and both updating work->data to point
1130 * to pwq on queueing and to pool on dequeueing are done under
1131 * pwq->pool->lock. This in turn guarantees that, if work->data
1132 * points to pwq which is associated with a locked pool, the work
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001133 * item is currently queued on that pool.
1134 */
Tejun Heo112202d2013-02-13 19:29:12 -08001135 pwq = get_work_pwq(work);
1136 if (pwq && pwq->pool == pool) {
Tejun Heo16062832013-02-06 18:04:53 -08001137 debug_work_deactivate(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001138
Tejun Heo16062832013-02-06 18:04:53 -08001139 /*
1140 * A delayed work item cannot be grabbed directly because
1141 * it might have linked NO_COLOR work items which, if left
Tejun Heo112202d2013-02-13 19:29:12 -08001142 * on the delayed_list, will confuse pwq->nr_active
Tejun Heo16062832013-02-06 18:04:53 -08001143 * management later on and cause stall. Make sure the work
1144 * item is activated before grabbing.
1145 */
1146 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
Tejun Heo112202d2013-02-13 19:29:12 -08001147 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001148
Tejun Heo16062832013-02-06 18:04:53 -08001149 list_del_init(&work->entry);
Tejun Heo112202d2013-02-13 19:29:12 -08001150 pwq_dec_nr_in_flight(get_work_pwq(work), get_work_color(work));
Tejun Heo36e227d2012-08-03 10:30:46 -07001151
Tejun Heo112202d2013-02-13 19:29:12 -08001152 /* work->data points to pwq iff queued, point to pool */
Tejun Heo16062832013-02-06 18:04:53 -08001153 set_work_pool_and_keep_pending(work, pool->id);
Lai Jiangshan4468a002013-02-06 18:04:53 -08001154
Tejun Heo16062832013-02-06 18:04:53 -08001155 spin_unlock(&pool->lock);
1156 return 1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001157 }
Tejun Heod565ed62013-01-24 11:01:33 -08001158 spin_unlock(&pool->lock);
Tejun Heobbb68df2012-08-03 10:30:46 -07001159fail:
1160 local_irq_restore(*flags);
1161 if (work_is_canceling(work))
1162 return -ENOENT;
1163 cpu_relax();
Tejun Heo36e227d2012-08-03 10:30:46 -07001164 return -EAGAIN;
Tejun Heobf4ede02012-08-03 10:30:46 -07001165}
1166
1167/**
Tejun Heo706026c2013-01-24 11:01:34 -08001168 * insert_work - insert a work into a pool
Tejun Heo112202d2013-02-13 19:29:12 -08001169 * @pwq: pwq @work belongs to
Tejun Heo4690c4a2010-06-29 10:07:10 +02001170 * @work: work to insert
1171 * @head: insertion point
1172 * @extra_flags: extra WORK_STRUCT_* flags to set
1173 *
Tejun Heo112202d2013-02-13 19:29:12 -08001174 * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to
Tejun Heo706026c2013-01-24 11:01:34 -08001175 * work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +02001176 *
1177 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001178 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02001179 */
Tejun Heo112202d2013-02-13 19:29:12 -08001180static void insert_work(struct pool_workqueue *pwq, struct work_struct *work,
1181 struct list_head *head, unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001182{
Tejun Heo112202d2013-02-13 19:29:12 -08001183 struct worker_pool *pool = pwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01001184
Tejun Heo4690c4a2010-06-29 10:07:10 +02001185 /* we own @work, set data and link */
Tejun Heo112202d2013-02-13 19:29:12 -08001186 set_work_pwq(work, pwq, extra_flags);
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -07001187 list_add_tail(&work->entry, head);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001188 get_pwq(pwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02001189
1190 /*
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001191 * Ensure either wq_worker_sleeping() sees the above
1192 * list_add_tail() or we see zero nr_running to avoid workers lying
1193 * around lazily while there are works to be processed.
Tejun Heoe22bee72010-06-29 10:07:14 +02001194 */
1195 smp_mb();
1196
Tejun Heo63d95a92012-07-12 14:46:37 -07001197 if (__need_more_worker(pool))
1198 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001199}
1200
Tejun Heoc8efcc22010-12-20 19:32:04 +01001201/*
1202 * Test whether @work is being queued from another work executing on the
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001203 * same workqueue.
Tejun Heoc8efcc22010-12-20 19:32:04 +01001204 */
1205static bool is_chained_work(struct workqueue_struct *wq)
1206{
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001207 struct worker *worker;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001208
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001209 worker = current_wq_worker();
1210 /*
1211 * Return %true iff I'm a worker execuing a work item on @wq. If
1212 * I'm @worker, it's safe to dereference it without locking.
1213 */
Tejun Heo112202d2013-02-13 19:29:12 -08001214 return worker && worker->current_pwq->wq == wq;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001215}
1216
Tejun Heod84ff052013-03-12 11:29:59 -07001217static void __queue_work(int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 struct work_struct *work)
1219{
Tejun Heo112202d2013-02-13 19:29:12 -08001220 struct pool_workqueue *pwq;
Tejun Heoc9178082013-03-12 11:30:04 -07001221 struct worker_pool *last_pool;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001222 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001223 unsigned int work_flags;
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001224 unsigned int req_cpu = cpu;
Tejun Heo8930cab2012-08-03 10:30:45 -07001225
1226 /*
1227 * While a work item is PENDING && off queue, a task trying to
1228 * steal the PENDING will busy-loop waiting for it to either get
1229 * queued or lose PENDING. Grabbing PENDING and queueing should
1230 * happen with IRQ disabled.
1231 */
1232 WARN_ON_ONCE(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001234 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001235
Tejun Heoc8efcc22010-12-20 19:32:04 +01001236 /* if dying, only works from the same workqueue are allowed */
Tejun Heo618b01e2013-03-12 11:30:04 -07001237 if (unlikely(wq->flags & __WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001238 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001239 return;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001240retry:
Tejun Heoc9178082013-03-12 11:30:04 -07001241 /* pwq which will be used unless @work is executing elsewhere */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001242 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo57469822012-08-03 10:30:45 -07001243 if (cpu == WORK_CPU_UNBOUND)
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001244 cpu = raw_smp_processor_id();
Tejun Heo7fb98ea2013-03-12 11:30:00 -07001245 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heoc9178082013-03-12 11:30:04 -07001246 } else {
1247 pwq = first_pwq(wq);
1248 }
Tejun Heodbf25762012-08-20 14:51:23 -07001249
Tejun Heoc9178082013-03-12 11:30:04 -07001250 /*
1251 * If @work was previously on a different pool, it might still be
1252 * running there, in which case the work needs to be queued on that
1253 * pool to guarantee non-reentrancy.
1254 */
1255 last_pool = get_work_pool(work);
1256 if (last_pool && last_pool != pwq->pool) {
1257 struct worker *worker;
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001258
Tejun Heoc9178082013-03-12 11:30:04 -07001259 spin_lock(&last_pool->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001260
Tejun Heoc9178082013-03-12 11:30:04 -07001261 worker = find_worker_executing_work(last_pool, work);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001262
Tejun Heoc9178082013-03-12 11:30:04 -07001263 if (worker && worker->current_pwq->wq == wq) {
1264 pwq = worker->current_pwq;
Tejun Heo8930cab2012-08-03 10:30:45 -07001265 } else {
Tejun Heoc9178082013-03-12 11:30:04 -07001266 /* meh... not running there, queue here */
1267 spin_unlock(&last_pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08001268 spin_lock(&pwq->pool->lock);
Tejun Heo8930cab2012-08-03 10:30:45 -07001269 }
Tejun Heof3421792010-07-02 10:03:51 +02001270 } else {
Tejun Heo112202d2013-02-13 19:29:12 -08001271 spin_lock(&pwq->pool->lock);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001272 }
1273
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001274 /*
1275 * pwq is determined and locked. For unbound pools, we could have
1276 * raced with pwq release and it could already be dead. If its
1277 * refcnt is zero, repeat pwq selection. Note that pwqs never die
1278 * without another pwq replacing it as the first pwq or while a
1279 * work item is executing on it, so the retying is guaranteed to
1280 * make forward-progress.
1281 */
1282 if (unlikely(!pwq->refcnt)) {
1283 if (wq->flags & WQ_UNBOUND) {
1284 spin_unlock(&pwq->pool->lock);
1285 cpu_relax();
1286 goto retry;
1287 }
1288 /* oops */
1289 WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt",
1290 wq->name, cpu);
1291 }
1292
Tejun Heo112202d2013-02-13 19:29:12 -08001293 /* pwq determined, queue */
1294 trace_workqueue_queue_work(req_cpu, pwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001295
Dan Carpenterf5b25522012-04-13 22:06:58 +03001296 if (WARN_ON(!list_empty(&work->entry))) {
Tejun Heo112202d2013-02-13 19:29:12 -08001297 spin_unlock(&pwq->pool->lock);
Dan Carpenterf5b25522012-04-13 22:06:58 +03001298 return;
1299 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001300
Tejun Heo112202d2013-02-13 19:29:12 -08001301 pwq->nr_in_flight[pwq->work_color]++;
1302 work_flags = work_color_to_flags(pwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001303
Tejun Heo112202d2013-02-13 19:29:12 -08001304 if (likely(pwq->nr_active < pwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001305 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001306 pwq->nr_active++;
1307 worklist = &pwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001308 } else {
1309 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo112202d2013-02-13 19:29:12 -08001310 worklist = &pwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001311 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001312
Tejun Heo112202d2013-02-13 19:29:12 -08001313 insert_work(pwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001314
Tejun Heo112202d2013-02-13 19:29:12 -08001315 spin_unlock(&pwq->pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316}
1317
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001318/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07001319 * queue_work_on - queue work on specific cpu
1320 * @cpu: CPU number to execute work on
1321 * @wq: workqueue to use
1322 * @work: work to queue
1323 *
Tejun Heod4283e92012-08-03 10:30:44 -07001324 * Returns %false if @work was already on a queue, %true otherwise.
Zhang Ruic1a220e2008-07-23 21:28:39 -07001325 *
1326 * We queue the work to a specific CPU, the caller must ensure it
1327 * can't go away.
1328 */
Tejun Heod4283e92012-08-03 10:30:44 -07001329bool queue_work_on(int cpu, struct workqueue_struct *wq,
1330 struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07001331{
Tejun Heod4283e92012-08-03 10:30:44 -07001332 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001333 unsigned long flags;
1334
1335 local_irq_save(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001336
Tejun Heo22df02b2010-06-29 10:07:10 +02001337 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001338 __queue_work(cpu, wq, work);
Tejun Heod4283e92012-08-03 10:30:44 -07001339 ret = true;
Zhang Ruic1a220e2008-07-23 21:28:39 -07001340 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001341
1342 local_irq_restore(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001343 return ret;
1344}
1345EXPORT_SYMBOL_GPL(queue_work_on);
1346
Tejun Heod8e794d2012-08-03 10:30:45 -07001347void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
David Howells52bad642006-11-22 14:54:01 +00001349 struct delayed_work *dwork = (struct delayed_work *)__data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001351 /* should have been called from irqsafe timer with irq already off */
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001352 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353}
Konstantin Khlebnikov1438ade52013-01-24 16:36:31 +04001354EXPORT_SYMBOL(delayed_work_timer_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001356static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
1357 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358{
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001359 struct timer_list *timer = &dwork->timer;
1360 struct work_struct *work = &dwork->work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001362 WARN_ON_ONCE(timer->function != delayed_work_timer_fn ||
1363 timer->data != (unsigned long)dwork);
Tejun Heofc4b5142012-12-04 07:40:39 -08001364 WARN_ON_ONCE(timer_pending(timer));
1365 WARN_ON_ONCE(!list_empty(&work->entry));
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001366
Tejun Heo8852aac2012-12-01 16:23:42 -08001367 /*
1368 * If @delay is 0, queue @dwork->work immediately. This is for
1369 * both optimization and correctness. The earliest @timer can
1370 * expire is on the closest next tick and delayed_work users depend
1371 * on that there's no such delay when @delay is 0.
1372 */
1373 if (!delay) {
1374 __queue_work(cpu, wq, &dwork->work);
1375 return;
1376 }
1377
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001378 timer_stats_timer_set_start_info(&dwork->timer);
1379
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001380 dwork->wq = wq;
Tejun Heo12650572012-08-08 09:38:42 -07001381 dwork->cpu = cpu;
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001382 timer->expires = jiffies + delay;
1383
1384 if (unlikely(cpu != WORK_CPU_UNBOUND))
1385 add_timer_on(timer, cpu);
1386 else
1387 add_timer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388}
1389
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001390/**
1391 * queue_delayed_work_on - queue work on specific CPU after delay
1392 * @cpu: CPU number to execute work on
1393 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001394 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001395 * @delay: number of jiffies to wait before queueing
1396 *
Tejun Heo715f1302012-08-03 10:30:46 -07001397 * Returns %false if @work was already on a queue, %true otherwise. If
1398 * @delay is zero and @dwork is idle, it will be scheduled for immediate
1399 * execution.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001400 */
Tejun Heod4283e92012-08-03 10:30:44 -07001401bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
1402 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001403{
David Howells52bad642006-11-22 14:54:01 +00001404 struct work_struct *work = &dwork->work;
Tejun Heod4283e92012-08-03 10:30:44 -07001405 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001406 unsigned long flags;
1407
1408 /* read the comment in __queue_work() */
1409 local_irq_save(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001410
Tejun Heo22df02b2010-06-29 10:07:10 +02001411 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001412 __queue_delayed_work(cpu, wq, dwork, delay);
Tejun Heod4283e92012-08-03 10:30:44 -07001413 ret = true;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001414 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001415
1416 local_irq_restore(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001417 return ret;
1418}
Dave Jonesae90dd52006-06-30 01:40:45 -04001419EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Tejun Heoc8e55f32010-06-29 10:07:12 +02001421/**
Tejun Heo8376fe22012-08-03 10:30:47 -07001422 * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
1423 * @cpu: CPU number to execute work on
1424 * @wq: workqueue to use
1425 * @dwork: work to queue
1426 * @delay: number of jiffies to wait before queueing
1427 *
1428 * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
1429 * modify @dwork's timer so that it expires after @delay. If @delay is
1430 * zero, @work is guaranteed to be scheduled immediately regardless of its
1431 * current state.
1432 *
1433 * Returns %false if @dwork was idle and queued, %true if @dwork was
1434 * pending and its timer was modified.
1435 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001436 * This function is safe to call from any context including IRQ handler.
Tejun Heo8376fe22012-08-03 10:30:47 -07001437 * See try_to_grab_pending() for details.
1438 */
1439bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
1440 struct delayed_work *dwork, unsigned long delay)
1441{
1442 unsigned long flags;
1443 int ret;
1444
1445 do {
1446 ret = try_to_grab_pending(&dwork->work, true, &flags);
1447 } while (unlikely(ret == -EAGAIN));
1448
1449 if (likely(ret >= 0)) {
1450 __queue_delayed_work(cpu, wq, dwork, delay);
1451 local_irq_restore(flags);
1452 }
1453
1454 /* -ENOENT from try_to_grab_pending() becomes %true */
1455 return ret;
1456}
1457EXPORT_SYMBOL_GPL(mod_delayed_work_on);
1458
1459/**
Tejun Heoc8e55f32010-06-29 10:07:12 +02001460 * worker_enter_idle - enter idle state
1461 * @worker: worker which is entering idle state
1462 *
1463 * @worker is entering idle state. Update stats and idle timer if
1464 * necessary.
1465 *
1466 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001467 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001468 */
1469static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001471 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Tejun Heo6183c002013-03-12 11:29:57 -07001473 if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) ||
1474 WARN_ON_ONCE(!list_empty(&worker->entry) &&
1475 (worker->hentry.next || worker->hentry.pprev)))
1476 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Tejun Heocb444762010-07-02 10:03:50 +02001478 /* can't use worker_set_flags(), also called from start_worker() */
1479 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001480 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001481 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001482
Tejun Heoc8e55f32010-06-29 10:07:12 +02001483 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001484 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001485
Tejun Heo628c78e2012-07-17 12:39:27 -07001486 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1487 mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
Tejun Heocb444762010-07-02 10:03:50 +02001488
Tejun Heo544ecf32012-05-14 15:04:50 -07001489 /*
Tejun Heo706026c2013-01-24 11:01:34 -08001490 * Sanity check nr_running. Because wq_unbind_fn() releases
Tejun Heod565ed62013-01-24 11:01:33 -08001491 * pool->lock between setting %WORKER_UNBOUND and zapping
Tejun Heo628c78e2012-07-17 12:39:27 -07001492 * nr_running, the warning may trigger spuriously. Check iff
1493 * unbind is not in progress.
Tejun Heo544ecf32012-05-14 15:04:50 -07001494 */
Tejun Heo24647572013-01-24 11:01:33 -08001495 WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001496 pool->nr_workers == pool->nr_idle &&
Tejun Heoe19e3972013-01-24 11:39:44 -08001497 atomic_read(&pool->nr_running));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498}
1499
Tejun Heoc8e55f32010-06-29 10:07:12 +02001500/**
1501 * worker_leave_idle - leave idle state
1502 * @worker: worker which is leaving idle state
1503 *
1504 * @worker is leaving idle state. Update stats.
1505 *
1506 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001507 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001508 */
1509static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001511 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Tejun Heo6183c002013-03-12 11:29:57 -07001513 if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE)))
1514 return;
Tejun Heod302f012010-06-29 10:07:13 +02001515 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001516 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001517 list_del_init(&worker->entry);
1518}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Tejun Heoe22bee72010-06-29 10:07:14 +02001520/**
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001521 * worker_maybe_bind_and_lock - try to bind %current to worker_pool and lock it
1522 * @pool: target worker_pool
1523 *
1524 * Bind %current to the cpu of @pool if it is associated and lock @pool.
Tejun Heoe22bee72010-06-29 10:07:14 +02001525 *
1526 * Works which are scheduled while the cpu is online must at least be
1527 * scheduled to a worker which is bound to the cpu so that if they are
1528 * flushed from cpu callbacks while cpu is going down, they are
1529 * guaranteed to execute on the cpu.
1530 *
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001531 * This function is to be used by unbound workers and rescuers to bind
Tejun Heoe22bee72010-06-29 10:07:14 +02001532 * themselves to the target cpu and may race with cpu going down or
1533 * coming online. kthread_bind() can't be used because it may put the
1534 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
Tejun Heo706026c2013-01-24 11:01:34 -08001535 * verbatim as it's best effort and blocking and pool may be
Tejun Heoe22bee72010-06-29 10:07:14 +02001536 * [dis]associated in the meantime.
1537 *
Tejun Heo706026c2013-01-24 11:01:34 -08001538 * This function tries set_cpus_allowed() and locks pool and verifies the
Tejun Heo24647572013-01-24 11:01:33 -08001539 * binding against %POOL_DISASSOCIATED which is set during
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001540 * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1541 * enters idle state or fetches works without dropping lock, it can
1542 * guarantee the scheduling requirement described in the first paragraph.
Tejun Heoe22bee72010-06-29 10:07:14 +02001543 *
1544 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001545 * Might sleep. Called without any lock but returns with pool->lock
Tejun Heoe22bee72010-06-29 10:07:14 +02001546 * held.
1547 *
1548 * RETURNS:
Tejun Heo706026c2013-01-24 11:01:34 -08001549 * %true if the associated pool is online (@worker is successfully
Tejun Heoe22bee72010-06-29 10:07:14 +02001550 * bound), %false if offline.
1551 */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001552static bool worker_maybe_bind_and_lock(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001553__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001554{
Tejun Heoe22bee72010-06-29 10:07:14 +02001555 while (true) {
1556 /*
1557 * The following call may fail, succeed or succeed
1558 * without actually migrating the task to the cpu if
1559 * it races with cpu hotunplug operation. Verify
Tejun Heo24647572013-01-24 11:01:33 -08001560 * against POOL_DISASSOCIATED.
Tejun Heoe22bee72010-06-29 10:07:14 +02001561 */
Tejun Heo24647572013-01-24 11:01:33 -08001562 if (!(pool->flags & POOL_DISASSOCIATED))
Tejun Heo7a4e3442013-03-12 11:30:00 -07001563 set_cpus_allowed_ptr(current, pool->attrs->cpumask);
Oleg Nesterov85f41862007-05-09 02:34:20 -07001564
Tejun Heod565ed62013-01-24 11:01:33 -08001565 spin_lock_irq(&pool->lock);
Tejun Heo24647572013-01-24 11:01:33 -08001566 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heoe22bee72010-06-29 10:07:14 +02001567 return false;
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001568 if (task_cpu(current) == pool->cpu &&
Tejun Heo7a4e3442013-03-12 11:30:00 -07001569 cpumask_equal(&current->cpus_allowed, pool->attrs->cpumask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001570 return true;
Tejun Heod565ed62013-01-24 11:01:33 -08001571 spin_unlock_irq(&pool->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001572
Tejun Heo5035b202011-04-29 18:08:37 +02001573 /*
1574 * We've raced with CPU hot[un]plug. Give it a breather
1575 * and retry migration. cond_resched() is required here;
1576 * otherwise, we might deadlock against cpu_stop trying to
1577 * bring down the CPU on non-preemptive kernel.
1578 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001579 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001580 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001581 }
1582}
1583
1584/*
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001585 * Rebind an idle @worker to its CPU. worker_thread() will test
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001586 * list_empty(@worker->entry) before leaving idle and call this function.
Tejun Heo25511a42012-07-17 12:39:27 -07001587 */
1588static void idle_worker_rebind(struct worker *worker)
1589{
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001590 /* CPU may go down again inbetween, clear UNBOUND only on success */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001591 if (worker_maybe_bind_and_lock(worker->pool))
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001592 worker_clr_flags(worker, WORKER_UNBOUND);
Tejun Heo25511a42012-07-17 12:39:27 -07001593
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001594 /* rebind complete, become available again */
1595 list_add(&worker->entry, &worker->pool->idle_list);
Tejun Heod565ed62013-01-24 11:01:33 -08001596 spin_unlock_irq(&worker->pool->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07001597}
1598
1599/*
1600 * Function for @worker->rebind.work used to rebind unbound busy workers to
Tejun Heo403c8212012-07-17 12:39:27 -07001601 * the associated cpu which is coming back online. This is scheduled by
1602 * cpu up but can race with other cpu hotplug operations and may be
1603 * executed twice without intervening cpu down.
Tejun Heoe22bee72010-06-29 10:07:14 +02001604 */
Tejun Heo25511a42012-07-17 12:39:27 -07001605static void busy_worker_rebind_fn(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001606{
1607 struct worker *worker = container_of(work, struct worker, rebind_work);
Tejun Heoe22bee72010-06-29 10:07:14 +02001608
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001609 if (worker_maybe_bind_and_lock(worker->pool))
Lai Jiangshaneab6d822012-09-18 09:59:22 -07001610 worker_clr_flags(worker, WORKER_UNBOUND);
Tejun Heoe22bee72010-06-29 10:07:14 +02001611
Tejun Heod565ed62013-01-24 11:01:33 -08001612 spin_unlock_irq(&worker->pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001613}
1614
Tejun Heo25511a42012-07-17 12:39:27 -07001615/**
Tejun Heo94cf58b2013-01-24 11:01:33 -08001616 * rebind_workers - rebind all workers of a pool to the associated CPU
1617 * @pool: pool of interest
Tejun Heo25511a42012-07-17 12:39:27 -07001618 *
Tejun Heo94cf58b2013-01-24 11:01:33 -08001619 * @pool->cpu is coming online. Rebind all workers to the CPU. Rebinding
Tejun Heo25511a42012-07-17 12:39:27 -07001620 * is different for idle and busy ones.
1621 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001622 * Idle ones will be removed from the idle_list and woken up. They will
1623 * add themselves back after completing rebind. This ensures that the
1624 * idle_list doesn't contain any unbound workers when re-bound busy workers
1625 * try to perform local wake-ups for concurrency management.
Tejun Heo25511a42012-07-17 12:39:27 -07001626 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001627 * Busy workers can rebind after they finish their current work items.
1628 * Queueing the rebind work item at the head of the scheduled list is
1629 * enough. Note that nr_running will be properly bumped as busy workers
1630 * rebind.
Tejun Heo25511a42012-07-17 12:39:27 -07001631 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001632 * On return, all non-manager workers are scheduled for rebind - see
1633 * manage_workers() for the manager special case. Any idle worker
1634 * including the manager will not appear on @idle_list until rebind is
1635 * complete, making local wake-ups safe.
Tejun Heo25511a42012-07-17 12:39:27 -07001636 */
Tejun Heo94cf58b2013-01-24 11:01:33 -08001637static void rebind_workers(struct worker_pool *pool)
Tejun Heo25511a42012-07-17 12:39:27 -07001638{
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001639 struct worker *worker, *n;
Tejun Heo25511a42012-07-17 12:39:27 -07001640 int i;
1641
Tejun Heobc3a1af2013-03-13 19:47:39 -07001642 lockdep_assert_held(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08001643 lockdep_assert_held(&pool->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07001644
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001645 /* dequeue and kick idle ones */
Tejun Heo94cf58b2013-01-24 11:01:33 -08001646 list_for_each_entry_safe(worker, n, &pool->idle_list, entry) {
1647 /*
1648 * idle workers should be off @pool->idle_list until rebind
1649 * is complete to avoid receiving premature local wake-ups.
1650 */
1651 list_del_init(&worker->entry);
Lai Jiangshan96e65302012-09-02 00:28:19 +08001652
Tejun Heo94cf58b2013-01-24 11:01:33 -08001653 /*
1654 * worker_thread() will see the above dequeuing and call
1655 * idle_worker_rebind().
1656 */
1657 wake_up_process(worker->task);
1658 }
Tejun Heo25511a42012-07-17 12:39:27 -07001659
Tejun Heo94cf58b2013-01-24 11:01:33 -08001660 /* rebind busy workers */
Sasha Levinb67bfe02013-02-27 17:06:00 -08001661 for_each_busy_worker(worker, i, pool) {
Tejun Heo94cf58b2013-01-24 11:01:33 -08001662 struct work_struct *rebind_work = &worker->rebind_work;
1663 struct workqueue_struct *wq;
Tejun Heo25511a42012-07-17 12:39:27 -07001664
Tejun Heo94cf58b2013-01-24 11:01:33 -08001665 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
1666 work_data_bits(rebind_work)))
1667 continue;
Tejun Heo25511a42012-07-17 12:39:27 -07001668
Tejun Heo94cf58b2013-01-24 11:01:33 -08001669 debug_work_activate(rebind_work);
Tejun Heo90beca52012-09-04 23:12:33 -07001670
Tejun Heo94cf58b2013-01-24 11:01:33 -08001671 /*
1672 * wq doesn't really matter but let's keep @worker->pool
Tejun Heo112202d2013-02-13 19:29:12 -08001673 * and @pwq->pool consistent for sanity.
Tejun Heo94cf58b2013-01-24 11:01:33 -08001674 */
Tejun Heo7a4e3442013-03-12 11:30:00 -07001675 if (worker->pool->attrs->nice < 0)
Tejun Heo94cf58b2013-01-24 11:01:33 -08001676 wq = system_highpri_wq;
1677 else
1678 wq = system_wq;
Tejun Heoec588152012-09-04 23:16:32 -07001679
Tejun Heo7fb98ea2013-03-12 11:30:00 -07001680 insert_work(per_cpu_ptr(wq->cpu_pwqs, pool->cpu), rebind_work,
Tejun Heo94cf58b2013-01-24 11:01:33 -08001681 worker->scheduled.next,
1682 work_color_to_flags(WORK_NO_COLOR));
Tejun Heoec588152012-09-04 23:16:32 -07001683 }
Tejun Heo25511a42012-07-17 12:39:27 -07001684}
1685
Tejun Heoc34056a2010-06-29 10:07:11 +02001686static struct worker *alloc_worker(void)
1687{
1688 struct worker *worker;
1689
1690 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001691 if (worker) {
1692 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001693 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heo25511a42012-07-17 12:39:27 -07001694 INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn);
Tejun Heoe22bee72010-06-29 10:07:14 +02001695 /* on creation a worker is in !idle && prep state */
1696 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001697 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001698 return worker;
1699}
1700
1701/**
1702 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001703 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001704 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001705 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001706 * can be started by calling start_worker() or destroyed using
1707 * destroy_worker().
1708 *
1709 * CONTEXT:
1710 * Might sleep. Does GFP_KERNEL allocations.
1711 *
1712 * RETURNS:
1713 * Pointer to the newly created worker.
1714 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001715static struct worker *create_worker(struct worker_pool *pool)
Tejun Heoc34056a2010-06-29 10:07:11 +02001716{
Tejun Heo7a4e3442013-03-12 11:30:00 -07001717 const char *pri = pool->attrs->nice < 0 ? "H" : "";
Tejun Heoc34056a2010-06-29 10:07:11 +02001718 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001719 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001720
Tejun Heocd549682013-03-13 19:47:39 -07001721 lockdep_assert_held(&pool->manager_mutex);
1722
Tejun Heod565ed62013-01-24 11:01:33 -08001723 spin_lock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001724 while (ida_get_new(&pool->worker_ida, &id)) {
Tejun Heod565ed62013-01-24 11:01:33 -08001725 spin_unlock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001726 if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001727 goto fail;
Tejun Heod565ed62013-01-24 11:01:33 -08001728 spin_lock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001729 }
Tejun Heod565ed62013-01-24 11:01:33 -08001730 spin_unlock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001731
1732 worker = alloc_worker();
1733 if (!worker)
1734 goto fail;
1735
Tejun Heobd7bdd42012-07-12 14:46:37 -07001736 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001737 worker->id = id;
1738
Tejun Heo29c91e92013-03-12 11:30:03 -07001739 if (pool->cpu >= 0)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001740 worker->task = kthread_create_on_node(worker_thread,
Tejun Heoec22ca52013-01-24 11:01:33 -08001741 worker, cpu_to_node(pool->cpu),
Tejun Heod84ff052013-03-12 11:29:59 -07001742 "kworker/%d:%d%s", pool->cpu, id, pri);
Tejun Heof3421792010-07-02 10:03:51 +02001743 else
1744 worker->task = kthread_create(worker_thread, worker,
Tejun Heoac6104c2013-03-12 11:30:03 -07001745 "kworker/u%d:%d%s",
1746 pool->id, id, pri);
Tejun Heoc34056a2010-06-29 10:07:11 +02001747 if (IS_ERR(worker->task))
1748 goto fail;
1749
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001750 /*
1751 * set_cpus_allowed_ptr() will fail if the cpumask doesn't have any
1752 * online CPUs. It'll be re-applied when any of the CPUs come up.
1753 */
Tejun Heo7a4e3442013-03-12 11:30:00 -07001754 set_user_nice(worker->task, pool->attrs->nice);
1755 set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask);
Tejun Heo32704762012-07-13 22:16:45 -07001756
Tejun Heodb7bccf2010-06-29 10:07:12 +02001757 /*
Tejun Heo7a4e3442013-03-12 11:30:00 -07001758 * %PF_THREAD_BOUND is used to prevent userland from meddling with
1759 * cpumask of workqueue workers. This is an abuse. We need
1760 * %PF_NO_SETAFFINITY.
Tejun Heodb7bccf2010-06-29 10:07:12 +02001761 */
Tejun Heo7a4e3442013-03-12 11:30:00 -07001762 worker->task->flags |= PF_THREAD_BOUND;
1763
1764 /*
1765 * The caller is responsible for ensuring %POOL_DISASSOCIATED
1766 * remains stable across this function. See the comments above the
1767 * flag definition for details.
1768 */
1769 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001770 worker->flags |= WORKER_UNBOUND;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001771
Tejun Heoc34056a2010-06-29 10:07:11 +02001772 return worker;
1773fail:
1774 if (id >= 0) {
Tejun Heod565ed62013-01-24 11:01:33 -08001775 spin_lock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001776 ida_remove(&pool->worker_ida, id);
Tejun Heod565ed62013-01-24 11:01:33 -08001777 spin_unlock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001778 }
1779 kfree(worker);
1780 return NULL;
1781}
1782
1783/**
1784 * start_worker - start a newly created worker
1785 * @worker: worker to start
1786 *
Tejun Heo706026c2013-01-24 11:01:34 -08001787 * Make the pool aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001788 *
1789 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001790 * spin_lock_irq(pool->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001791 */
1792static void start_worker(struct worker *worker)
1793{
Tejun Heocb444762010-07-02 10:03:50 +02001794 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001795 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001796 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001797 wake_up_process(worker->task);
1798}
1799
1800/**
Tejun Heoebf44d12013-03-13 19:47:39 -07001801 * create_and_start_worker - create and start a worker for a pool
1802 * @pool: the target pool
1803 *
Tejun Heocd549682013-03-13 19:47:39 -07001804 * Grab the managership of @pool and create and start a new worker for it.
Tejun Heoebf44d12013-03-13 19:47:39 -07001805 */
1806static int create_and_start_worker(struct worker_pool *pool)
1807{
1808 struct worker *worker;
1809
Tejun Heocd549682013-03-13 19:47:39 -07001810 mutex_lock(&pool->manager_mutex);
1811
Tejun Heoebf44d12013-03-13 19:47:39 -07001812 worker = create_worker(pool);
1813 if (worker) {
1814 spin_lock_irq(&pool->lock);
1815 start_worker(worker);
1816 spin_unlock_irq(&pool->lock);
1817 }
1818
Tejun Heocd549682013-03-13 19:47:39 -07001819 mutex_unlock(&pool->manager_mutex);
1820
Tejun Heoebf44d12013-03-13 19:47:39 -07001821 return worker ? 0 : -ENOMEM;
1822}
1823
1824/**
Tejun Heoc34056a2010-06-29 10:07:11 +02001825 * destroy_worker - destroy a workqueue worker
1826 * @worker: worker to be destroyed
1827 *
Tejun Heo706026c2013-01-24 11:01:34 -08001828 * Destroy @worker and adjust @pool stats accordingly.
Tejun Heoc8e55f32010-06-29 10:07:12 +02001829 *
1830 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001831 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001832 */
1833static void destroy_worker(struct worker *worker)
1834{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001835 struct worker_pool *pool = worker->pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001836 int id = worker->id;
1837
Tejun Heocd549682013-03-13 19:47:39 -07001838 lockdep_assert_held(&pool->manager_mutex);
1839 lockdep_assert_held(&pool->lock);
1840
Tejun Heoc34056a2010-06-29 10:07:11 +02001841 /* sanity check frenzy */
Tejun Heo6183c002013-03-12 11:29:57 -07001842 if (WARN_ON(worker->current_work) ||
1843 WARN_ON(!list_empty(&worker->scheduled)))
1844 return;
Tejun Heoc34056a2010-06-29 10:07:11 +02001845
Tejun Heoc8e55f32010-06-29 10:07:12 +02001846 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001847 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001848 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001849 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001850
1851 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001852 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001853
Tejun Heod565ed62013-01-24 11:01:33 -08001854 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001855
Tejun Heoc34056a2010-06-29 10:07:11 +02001856 kthread_stop(worker->task);
1857 kfree(worker);
1858
Tejun Heod565ed62013-01-24 11:01:33 -08001859 spin_lock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001860 ida_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001861}
1862
Tejun Heo63d95a92012-07-12 14:46:37 -07001863static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001864{
Tejun Heo63d95a92012-07-12 14:46:37 -07001865 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001866
Tejun Heod565ed62013-01-24 11:01:33 -08001867 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001868
Tejun Heo63d95a92012-07-12 14:46:37 -07001869 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001870 struct worker *worker;
1871 unsigned long expires;
1872
1873 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001874 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001875 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1876
1877 if (time_before(jiffies, expires))
Tejun Heo63d95a92012-07-12 14:46:37 -07001878 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001879 else {
1880 /* it's been idle for too long, wake up manager */
Tejun Heo11ebea52012-07-12 14:46:37 -07001881 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -07001882 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001883 }
1884 }
1885
Tejun Heod565ed62013-01-24 11:01:33 -08001886 spin_unlock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001887}
1888
Tejun Heo493a1722013-03-12 11:29:59 -07001889static void send_mayday(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001890{
Tejun Heo112202d2013-02-13 19:29:12 -08001891 struct pool_workqueue *pwq = get_work_pwq(work);
1892 struct workqueue_struct *wq = pwq->wq;
Tejun Heo493a1722013-03-12 11:29:59 -07001893
1894 lockdep_assert_held(&workqueue_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001895
Tejun Heo493008a2013-03-12 11:30:03 -07001896 if (!wq->rescuer)
Tejun Heo493a1722013-03-12 11:29:59 -07001897 return;
Tejun Heoe22bee72010-06-29 10:07:14 +02001898
1899 /* mayday mayday mayday */
Tejun Heo493a1722013-03-12 11:29:59 -07001900 if (list_empty(&pwq->mayday_node)) {
1901 list_add_tail(&pwq->mayday_node, &wq->maydays);
Tejun Heoe22bee72010-06-29 10:07:14 +02001902 wake_up_process(wq->rescuer->task);
Tejun Heo493a1722013-03-12 11:29:59 -07001903 }
Tejun Heoe22bee72010-06-29 10:07:14 +02001904}
1905
Tejun Heo706026c2013-01-24 11:01:34 -08001906static void pool_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001907{
Tejun Heo63d95a92012-07-12 14:46:37 -07001908 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001909 struct work_struct *work;
1910
Tejun Heo493a1722013-03-12 11:29:59 -07001911 spin_lock_irq(&workqueue_lock); /* for wq->maydays */
1912 spin_lock(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001913
Tejun Heo63d95a92012-07-12 14:46:37 -07001914 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001915 /*
1916 * We've been trying to create a new worker but
1917 * haven't been successful. We might be hitting an
1918 * allocation deadlock. Send distress signals to
1919 * rescuers.
1920 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001921 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001922 send_mayday(work);
1923 }
1924
Tejun Heo493a1722013-03-12 11:29:59 -07001925 spin_unlock(&pool->lock);
1926 spin_unlock_irq(&workqueue_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001927
Tejun Heo63d95a92012-07-12 14:46:37 -07001928 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001929}
1930
1931/**
1932 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001933 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001934 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001935 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001936 * have at least one idle worker on return from this function. If
1937 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001938 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001939 * possible allocation deadlock.
1940 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001941 * On return, need_to_create_worker() is guaranteed to be %false and
1942 * may_start_working() %true.
Tejun Heoe22bee72010-06-29 10:07:14 +02001943 *
1944 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001945 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001946 * multiple times. Does GFP_KERNEL allocations. Called only from
1947 * manager.
1948 *
1949 * RETURNS:
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001950 * %false if no action was taken and pool->lock stayed locked, %true
Tejun Heoe22bee72010-06-29 10:07:14 +02001951 * otherwise.
1952 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001953static bool maybe_create_worker(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001954__releases(&pool->lock)
1955__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001956{
Tejun Heo63d95a92012-07-12 14:46:37 -07001957 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001958 return false;
1959restart:
Tejun Heod565ed62013-01-24 11:01:33 -08001960 spin_unlock_irq(&pool->lock);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001961
Tejun Heoe22bee72010-06-29 10:07:14 +02001962 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07001963 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001964
1965 while (true) {
1966 struct worker *worker;
1967
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001968 worker = create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001969 if (worker) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001970 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001971 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001972 start_worker(worker);
Tejun Heo6183c002013-03-12 11:29:57 -07001973 if (WARN_ON_ONCE(need_to_create_worker(pool)))
1974 goto restart;
Tejun Heoe22bee72010-06-29 10:07:14 +02001975 return true;
1976 }
1977
Tejun Heo63d95a92012-07-12 14:46:37 -07001978 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001979 break;
1980
Tejun Heoe22bee72010-06-29 10:07:14 +02001981 __set_current_state(TASK_INTERRUPTIBLE);
1982 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001983
Tejun Heo63d95a92012-07-12 14:46:37 -07001984 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001985 break;
1986 }
1987
Tejun Heo63d95a92012-07-12 14:46:37 -07001988 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001989 spin_lock_irq(&pool->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07001990 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001991 goto restart;
1992 return true;
1993}
1994
1995/**
1996 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo63d95a92012-07-12 14:46:37 -07001997 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001998 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001999 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02002000 * IDLE_WORKER_TIMEOUT.
2001 *
2002 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08002003 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02002004 * multiple times. Called only from manager.
2005 *
2006 * RETURNS:
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002007 * %false if no action was taken and pool->lock stayed locked, %true
Tejun Heoe22bee72010-06-29 10:07:14 +02002008 * otherwise.
2009 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002010static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02002011{
2012 bool ret = false;
2013
Tejun Heo63d95a92012-07-12 14:46:37 -07002014 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02002015 struct worker *worker;
2016 unsigned long expires;
2017
Tejun Heo63d95a92012-07-12 14:46:37 -07002018 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02002019 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
2020
2021 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07002022 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02002023 break;
2024 }
2025
2026 destroy_worker(worker);
2027 ret = true;
2028 }
2029
2030 return ret;
2031}
2032
2033/**
2034 * manage_workers - manage worker pool
2035 * @worker: self
2036 *
Tejun Heo706026c2013-01-24 11:01:34 -08002037 * Assume the manager role and manage the worker pool @worker belongs
Tejun Heoe22bee72010-06-29 10:07:14 +02002038 * to. At any given time, there can be only zero or one manager per
Tejun Heo706026c2013-01-24 11:01:34 -08002039 * pool. The exclusion is handled automatically by this function.
Tejun Heoe22bee72010-06-29 10:07:14 +02002040 *
2041 * The caller can safely start processing works on false return. On
2042 * true return, it's guaranteed that need_to_create_worker() is false
2043 * and may_start_working() is true.
2044 *
2045 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002046 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02002047 * multiple times. Does GFP_KERNEL allocations.
2048 *
2049 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08002050 * spin_lock_irq(pool->lock) which may be released and regrabbed
2051 * multiple times. Does GFP_KERNEL allocations.
Tejun Heoe22bee72010-06-29 10:07:14 +02002052 */
2053static bool manage_workers(struct worker *worker)
2054{
Tejun Heo63d95a92012-07-12 14:46:37 -07002055 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002056 bool ret = false;
2057
Tejun Heobc3a1af2013-03-13 19:47:39 -07002058 /*
2059 * Managership is governed by two mutexes - manager_arb and
2060 * manager_mutex. manager_arb handles arbitration of manager role.
2061 * Anyone who successfully grabs manager_arb wins the arbitration
2062 * and becomes the manager. mutex_trylock() on pool->manager_arb
2063 * failure while holding pool->lock reliably indicates that someone
2064 * else is managing the pool and the worker which failed trylock
2065 * can proceed to executing work items. This means that anyone
2066 * grabbing manager_arb is responsible for actually performing
2067 * manager duties. If manager_arb is grabbed and released without
2068 * actual management, the pool may stall indefinitely.
2069 *
2070 * manager_mutex is used for exclusion of actual management
2071 * operations. The holder of manager_mutex can be sure that none
2072 * of management operations, including creation and destruction of
2073 * workers, won't take place until the mutex is released. Because
2074 * manager_mutex doesn't interfere with manager role arbitration,
2075 * it is guaranteed that the pool's management, while may be
2076 * delayed, won't be disturbed by someone else grabbing
2077 * manager_mutex.
2078 */
Tejun Heo34a06bd2013-03-12 11:30:00 -07002079 if (!mutex_trylock(&pool->manager_arb))
Tejun Heoe22bee72010-06-29 10:07:14 +02002080 return ret;
2081
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002082 /*
Tejun Heobc3a1af2013-03-13 19:47:39 -07002083 * With manager arbitration won, manager_mutex would be free in
2084 * most cases. trylock first without dropping @pool->lock.
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002085 */
Tejun Heobc3a1af2013-03-13 19:47:39 -07002086 if (unlikely(!mutex_trylock(&pool->manager_mutex))) {
Tejun Heod565ed62013-01-24 11:01:33 -08002087 spin_unlock_irq(&pool->lock);
Tejun Heobc3a1af2013-03-13 19:47:39 -07002088 mutex_lock(&pool->manager_mutex);
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002089 /*
2090 * CPU hotplug could have happened while we were waiting
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002091 * for assoc_mutex. Hotplug itself can't handle us
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002092 * because manager isn't either on idle or busy list, and
Tejun Heo706026c2013-01-24 11:01:34 -08002093 * @pool's state and ours could have deviated.
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002094 *
Tejun Heobc3a1af2013-03-13 19:47:39 -07002095 * As hotplug is now excluded via manager_mutex, we can
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002096 * simply try to bind. It will succeed or fail depending
Tejun Heo706026c2013-01-24 11:01:34 -08002097 * on @pool's current state. Try it and adjust
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002098 * %WORKER_UNBOUND accordingly.
2099 */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08002100 if (worker_maybe_bind_and_lock(pool))
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002101 worker->flags &= ~WORKER_UNBOUND;
2102 else
2103 worker->flags |= WORKER_UNBOUND;
2104
2105 ret = true;
2106 }
2107
Tejun Heo11ebea52012-07-12 14:46:37 -07002108 pool->flags &= ~POOL_MANAGE_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02002109
2110 /*
2111 * Destroy and then create so that may_start_working() is true
2112 * on return.
2113 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002114 ret |= maybe_destroy_workers(pool);
2115 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002116
Tejun Heobc3a1af2013-03-13 19:47:39 -07002117 mutex_unlock(&pool->manager_mutex);
Tejun Heo34a06bd2013-03-12 11:30:00 -07002118 mutex_unlock(&pool->manager_arb);
Tejun Heoe22bee72010-06-29 10:07:14 +02002119 return ret;
2120}
2121
Tejun Heoa62428c2010-06-29 10:07:10 +02002122/**
2123 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02002124 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02002125 * @work: work to process
2126 *
2127 * Process @work. This function contains all the logics necessary to
2128 * process a single work including synchronization against and
2129 * interaction with other workers on the same cpu, queueing and
2130 * flushing. As long as context requirement is met, any worker can
2131 * call this function to process a work.
2132 *
2133 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002134 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02002135 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002136static void process_one_work(struct worker *worker, struct work_struct *work)
Tejun Heod565ed62013-01-24 11:01:33 -08002137__releases(&pool->lock)
2138__acquires(&pool->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02002139{
Tejun Heo112202d2013-02-13 19:29:12 -08002140 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002141 struct worker_pool *pool = worker->pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002142 bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02002143 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02002144 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02002145#ifdef CONFIG_LOCKDEP
2146 /*
2147 * It is permissible to free the struct work_struct from
2148 * inside the function that is called from it, this we need to
2149 * take into account for lockdep too. To avoid bogus "held
2150 * lock freed" warnings as well as problems when looking into
2151 * work->lockdep_map, make a copy and use that here.
2152 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07002153 struct lockdep_map lockdep_map;
2154
2155 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002156#endif
Tejun Heo6fec10a2012-07-22 10:16:34 -07002157 /*
2158 * Ensure we're on the correct CPU. DISASSOCIATED test is
2159 * necessary to avoid spurious warnings from rescuers servicing the
Tejun Heo24647572013-01-24 11:01:33 -08002160 * unbound or a disassociated pool.
Tejun Heo6fec10a2012-07-22 10:16:34 -07002161 */
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002162 WARN_ON_ONCE(!(worker->flags & WORKER_UNBOUND) &&
Tejun Heo24647572013-01-24 11:01:33 -08002163 !(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heoec22ca52013-01-24 11:01:33 -08002164 raw_smp_processor_id() != pool->cpu);
Tejun Heo25511a42012-07-17 12:39:27 -07002165
Tejun Heo7e116292010-06-29 10:07:13 +02002166 /*
2167 * A single work shouldn't be executed concurrently by
2168 * multiple workers on a single cpu. Check whether anyone is
2169 * already processing the work. If so, defer the work to the
2170 * currently executing one.
2171 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002172 collision = find_worker_executing_work(pool, work);
Tejun Heo7e116292010-06-29 10:07:13 +02002173 if (unlikely(collision)) {
2174 move_linked_works(work, &collision->scheduled, NULL);
2175 return;
2176 }
2177
Tejun Heo8930cab2012-08-03 10:30:45 -07002178 /* claim and dequeue */
Tejun Heoa62428c2010-06-29 10:07:10 +02002179 debug_work_deactivate(work);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002180 hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work);
Tejun Heoc34056a2010-06-29 10:07:11 +02002181 worker->current_work = work;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002182 worker->current_func = work->func;
Tejun Heo112202d2013-02-13 19:29:12 -08002183 worker->current_pwq = pwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002184 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002185
Tejun Heoa62428c2010-06-29 10:07:10 +02002186 list_del_init(&work->entry);
2187
Tejun Heo649027d2010-06-29 10:07:14 +02002188 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02002189 * CPU intensive works don't participate in concurrency
2190 * management. They're the scheduler's responsibility.
2191 */
2192 if (unlikely(cpu_intensive))
2193 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
2194
Tejun Heo974271c2012-07-12 14:46:37 -07002195 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002196 * Unbound pool isn't concurrency managed and work items should be
Tejun Heo974271c2012-07-12 14:46:37 -07002197 * executed ASAP. Wake up another worker if necessary.
2198 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002199 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
2200 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07002201
Tejun Heo8930cab2012-08-03 10:30:45 -07002202 /*
Tejun Heo7c3eed52013-01-24 11:01:33 -08002203 * Record the last pool and clear PENDING which should be the last
Tejun Heod565ed62013-01-24 11:01:33 -08002204 * update to @work. Also, do this inside @pool->lock so that
Tejun Heo23657bb2012-08-13 17:08:19 -07002205 * PENDING and queued state changes happen together while IRQ is
2206 * disabled.
Tejun Heo8930cab2012-08-03 10:30:45 -07002207 */
Tejun Heo7c3eed52013-01-24 11:01:33 -08002208 set_work_pool_and_clear_pending(work, pool->id);
Tejun Heoa62428c2010-06-29 10:07:10 +02002209
Tejun Heod565ed62013-01-24 11:01:33 -08002210 spin_unlock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002211
Tejun Heo112202d2013-02-13 19:29:12 -08002212 lock_map_acquire_read(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002213 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002214 trace_workqueue_execute_start(work);
Tejun Heoa2c1c572012-12-18 10:35:02 -08002215 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002216 /*
2217 * While we must be careful to not use "work" after this, the trace
2218 * point will only record its address.
2219 */
2220 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002221 lock_map_release(&lockdep_map);
Tejun Heo112202d2013-02-13 19:29:12 -08002222 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002223
2224 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Valentin Ilie044c7822012-08-19 00:52:42 +03002225 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
2226 " last function: %pf\n",
Tejun Heoa2c1c572012-12-18 10:35:02 -08002227 current->comm, preempt_count(), task_pid_nr(current),
2228 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02002229 debug_show_held_locks(current);
2230 dump_stack();
2231 }
2232
Tejun Heod565ed62013-01-24 11:01:33 -08002233 spin_lock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002234
Tejun Heofb0e7be2010-06-29 10:07:15 +02002235 /* clear cpu intensive status */
2236 if (unlikely(cpu_intensive))
2237 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2238
Tejun Heoa62428c2010-06-29 10:07:10 +02002239 /* we're done with it, release */
Sasha Levin42f85702012-12-17 10:01:23 -05002240 hash_del(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002241 worker->current_work = NULL;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002242 worker->current_func = NULL;
Tejun Heo112202d2013-02-13 19:29:12 -08002243 worker->current_pwq = NULL;
2244 pwq_dec_nr_in_flight(pwq, work_color);
Tejun Heoa62428c2010-06-29 10:07:10 +02002245}
2246
Tejun Heoaffee4b2010-06-29 10:07:12 +02002247/**
2248 * process_scheduled_works - process scheduled works
2249 * @worker: self
2250 *
2251 * Process all scheduled works. Please note that the scheduled list
2252 * may change while processing a work, so this function repeatedly
2253 * fetches a work from the top and executes it.
2254 *
2255 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002256 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02002257 * multiple times.
2258 */
2259static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002261 while (!list_empty(&worker->scheduled)) {
2262 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002264 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266}
2267
Tejun Heo4690c4a2010-06-29 10:07:10 +02002268/**
2269 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02002270 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02002271 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002272 * The worker thread function. All workers belong to a worker_pool -
2273 * either a per-cpu one or dynamic unbound one. These workers process all
2274 * work items regardless of their specific target workqueue. The only
2275 * exception is work items which belong to workqueues with a rescuer which
2276 * will be explained in rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02002277 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002278static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279{
Tejun Heoc34056a2010-06-29 10:07:11 +02002280 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07002281 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
Tejun Heoe22bee72010-06-29 10:07:14 +02002283 /* tell the scheduler that this is a workqueue worker */
2284 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002285woke_up:
Tejun Heod565ed62013-01-24 11:01:33 -08002286 spin_lock_irq(&pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002288 /* we are off idle list if destruction or rebind is requested */
2289 if (unlikely(list_empty(&worker->entry))) {
Tejun Heod565ed62013-01-24 11:01:33 -08002290 spin_unlock_irq(&pool->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07002291
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002292 /* if DIE is set, destruction is requested */
Tejun Heo25511a42012-07-17 12:39:27 -07002293 if (worker->flags & WORKER_DIE) {
2294 worker->task->flags &= ~PF_WQ_WORKER;
2295 return 0;
2296 }
2297
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002298 /* otherwise, rebind */
Tejun Heo25511a42012-07-17 12:39:27 -07002299 idle_worker_rebind(worker);
2300 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 }
2302
Tejun Heoc8e55f32010-06-29 10:07:12 +02002303 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002304recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002305 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002306 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002307 goto sleep;
2308
2309 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002310 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002311 goto recheck;
2312
Tejun Heoc8e55f32010-06-29 10:07:12 +02002313 /*
2314 * ->scheduled list can only be filled while a worker is
2315 * preparing to process a work or actually processing it.
2316 * Make sure nobody diddled with it while I was sleeping.
2317 */
Tejun Heo6183c002013-03-12 11:29:57 -07002318 WARN_ON_ONCE(!list_empty(&worker->scheduled));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002319
Tejun Heoe22bee72010-06-29 10:07:14 +02002320 /*
2321 * When control reaches this point, we're guaranteed to have
2322 * at least one idle worker or that someone else has already
2323 * assumed the manager role.
2324 */
2325 worker_clr_flags(worker, WORKER_PREP);
2326
2327 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002328 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002329 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002330 struct work_struct, entry);
2331
2332 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2333 /* optimization path, not strictly necessary */
2334 process_one_work(worker, work);
2335 if (unlikely(!list_empty(&worker->scheduled)))
2336 process_scheduled_works(worker);
2337 } else {
2338 move_linked_works(work, &worker->scheduled, NULL);
2339 process_scheduled_works(worker);
2340 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002341 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002342
Tejun Heoe22bee72010-06-29 10:07:14 +02002343 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002344sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07002345 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002346 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002347
Tejun Heoc8e55f32010-06-29 10:07:12 +02002348 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002349 * pool->lock is held and there's no work to process and no need to
2350 * manage, sleep. Workers are woken up only while holding
2351 * pool->lock or from local cpu, so setting the current state
2352 * before releasing pool->lock is enough to prevent losing any
2353 * event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002354 */
2355 worker_enter_idle(worker);
2356 __set_current_state(TASK_INTERRUPTIBLE);
Tejun Heod565ed62013-01-24 11:01:33 -08002357 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02002358 schedule();
2359 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360}
2361
Tejun Heoe22bee72010-06-29 10:07:14 +02002362/**
2363 * rescuer_thread - the rescuer thread function
Tejun Heo111c2252013-01-17 17:16:24 -08002364 * @__rescuer: self
Tejun Heoe22bee72010-06-29 10:07:14 +02002365 *
2366 * Workqueue rescuer thread function. There's one rescuer for each
Tejun Heo493008a2013-03-12 11:30:03 -07002367 * workqueue which has WQ_MEM_RECLAIM set.
Tejun Heoe22bee72010-06-29 10:07:14 +02002368 *
Tejun Heo706026c2013-01-24 11:01:34 -08002369 * Regular work processing on a pool may block trying to create a new
Tejun Heoe22bee72010-06-29 10:07:14 +02002370 * worker which uses GFP_KERNEL allocation which has slight chance of
2371 * developing into deadlock if some works currently on the same queue
2372 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2373 * the problem rescuer solves.
2374 *
Tejun Heo706026c2013-01-24 11:01:34 -08002375 * When such condition is possible, the pool summons rescuers of all
2376 * workqueues which have works queued on the pool and let them process
Tejun Heoe22bee72010-06-29 10:07:14 +02002377 * those works so that forward progress can be guaranteed.
2378 *
2379 * This should happen rarely.
2380 */
Tejun Heo111c2252013-01-17 17:16:24 -08002381static int rescuer_thread(void *__rescuer)
Tejun Heoe22bee72010-06-29 10:07:14 +02002382{
Tejun Heo111c2252013-01-17 17:16:24 -08002383 struct worker *rescuer = __rescuer;
2384 struct workqueue_struct *wq = rescuer->rescue_wq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002385 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heoe22bee72010-06-29 10:07:14 +02002386
2387 set_user_nice(current, RESCUER_NICE_LEVEL);
Tejun Heo111c2252013-01-17 17:16:24 -08002388
2389 /*
2390 * Mark rescuer as worker too. As WORKER_PREP is never cleared, it
2391 * doesn't participate in concurrency management.
2392 */
2393 rescuer->task->flags |= PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002394repeat:
2395 set_current_state(TASK_INTERRUPTIBLE);
2396
Mike Galbraith412d32e2012-11-28 07:17:18 +01002397 if (kthread_should_stop()) {
2398 __set_current_state(TASK_RUNNING);
Tejun Heo111c2252013-01-17 17:16:24 -08002399 rescuer->task->flags &= ~PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002400 return 0;
Mike Galbraith412d32e2012-11-28 07:17:18 +01002401 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002402
Tejun Heo493a1722013-03-12 11:29:59 -07002403 /* see whether any pwq is asking for help */
2404 spin_lock_irq(&workqueue_lock);
2405
2406 while (!list_empty(&wq->maydays)) {
2407 struct pool_workqueue *pwq = list_first_entry(&wq->maydays,
2408 struct pool_workqueue, mayday_node);
Tejun Heo112202d2013-02-13 19:29:12 -08002409 struct worker_pool *pool = pwq->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002410 struct work_struct *work, *n;
2411
2412 __set_current_state(TASK_RUNNING);
Tejun Heo493a1722013-03-12 11:29:59 -07002413 list_del_init(&pwq->mayday_node);
2414
2415 spin_unlock_irq(&workqueue_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002416
2417 /* migrate to the target cpu if possible */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08002418 worker_maybe_bind_and_lock(pool);
Lai Jiangshanb3104102013-02-19 12:17:02 -08002419 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002420
2421 /*
2422 * Slurp in all works issued via this workqueue and
2423 * process'em.
2424 */
Tejun Heo6183c002013-03-12 11:29:57 -07002425 WARN_ON_ONCE(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002426 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heo112202d2013-02-13 19:29:12 -08002427 if (get_work_pwq(work) == pwq)
Tejun Heoe22bee72010-06-29 10:07:14 +02002428 move_linked_works(work, scheduled, &n);
2429
2430 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002431
2432 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002433 * Leave this pool. If keep_working() is %true, notify a
Tejun Heo75769582011-02-14 14:04:46 +01002434 * regular worker; otherwise, we end up with 0 concurrency
2435 * and stalling the execution.
2436 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002437 if (keep_working(pool))
2438 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002439
Lai Jiangshanb3104102013-02-19 12:17:02 -08002440 rescuer->pool = NULL;
Tejun Heo493a1722013-03-12 11:29:59 -07002441 spin_unlock(&pool->lock);
2442 spin_lock(&workqueue_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002443 }
2444
Tejun Heo493a1722013-03-12 11:29:59 -07002445 spin_unlock_irq(&workqueue_lock);
2446
Tejun Heo111c2252013-01-17 17:16:24 -08002447 /* rescuers should never participate in concurrency management */
2448 WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
Tejun Heoe22bee72010-06-29 10:07:14 +02002449 schedule();
2450 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451}
2452
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002453struct wq_barrier {
2454 struct work_struct work;
2455 struct completion done;
2456};
2457
2458static void wq_barrier_func(struct work_struct *work)
2459{
2460 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2461 complete(&barr->done);
2462}
2463
Tejun Heo4690c4a2010-06-29 10:07:10 +02002464/**
2465 * insert_wq_barrier - insert a barrier work
Tejun Heo112202d2013-02-13 19:29:12 -08002466 * @pwq: pwq to insert barrier into
Tejun Heo4690c4a2010-06-29 10:07:10 +02002467 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002468 * @target: target work to attach @barr to
2469 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002470 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002471 * @barr is linked to @target such that @barr is completed only after
2472 * @target finishes execution. Please note that the ordering
2473 * guarantee is observed only with respect to @target and on the local
2474 * cpu.
2475 *
2476 * Currently, a queued barrier can't be canceled. This is because
2477 * try_to_grab_pending() can't determine whether the work to be
2478 * grabbed is at the head of the queue and thus can't clear LINKED
2479 * flag of the previous work while there must be a valid next work
2480 * after a work with LINKED flag set.
2481 *
2482 * Note that when @worker is non-NULL, @target may be modified
Tejun Heo112202d2013-02-13 19:29:12 -08002483 * underneath us, so we can't reliably determine pwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002484 *
2485 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002486 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002487 */
Tejun Heo112202d2013-02-13 19:29:12 -08002488static void insert_wq_barrier(struct pool_workqueue *pwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002489 struct wq_barrier *barr,
2490 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002491{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002492 struct list_head *head;
2493 unsigned int linked = 0;
2494
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002495 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002496 * debugobject calls are safe here even with pool->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002497 * as we know for sure that this will not trigger any of the
2498 * checks and call back into the fixup functions where we
2499 * might deadlock.
2500 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002501 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002502 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002503 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002504
Tejun Heoaffee4b2010-06-29 10:07:12 +02002505 /*
2506 * If @target is currently being executed, schedule the
2507 * barrier to the worker; otherwise, put it after @target.
2508 */
2509 if (worker)
2510 head = worker->scheduled.next;
2511 else {
2512 unsigned long *bits = work_data_bits(target);
2513
2514 head = target->entry.next;
2515 /* there can already be other linked works, inherit and set */
2516 linked = *bits & WORK_STRUCT_LINKED;
2517 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2518 }
2519
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002520 debug_work_activate(&barr->work);
Tejun Heo112202d2013-02-13 19:29:12 -08002521 insert_work(pwq, &barr->work, head,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002522 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002523}
2524
Tejun Heo73f53c42010-06-29 10:07:11 +02002525/**
Tejun Heo112202d2013-02-13 19:29:12 -08002526 * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing
Tejun Heo73f53c42010-06-29 10:07:11 +02002527 * @wq: workqueue being flushed
2528 * @flush_color: new flush color, < 0 for no-op
2529 * @work_color: new work color, < 0 for no-op
2530 *
Tejun Heo112202d2013-02-13 19:29:12 -08002531 * Prepare pwqs for workqueue flushing.
Tejun Heo73f53c42010-06-29 10:07:11 +02002532 *
Tejun Heo112202d2013-02-13 19:29:12 -08002533 * If @flush_color is non-negative, flush_color on all pwqs should be
2534 * -1. If no pwq has in-flight commands at the specified color, all
2535 * pwq->flush_color's stay at -1 and %false is returned. If any pwq
2536 * has in flight commands, its pwq->flush_color is set to
2537 * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq
Tejun Heo73f53c42010-06-29 10:07:11 +02002538 * wakeup logic is armed and %true is returned.
2539 *
2540 * The caller should have initialized @wq->first_flusher prior to
2541 * calling this function with non-negative @flush_color. If
2542 * @flush_color is negative, no flush color update is done and %false
2543 * is returned.
2544 *
Tejun Heo112202d2013-02-13 19:29:12 -08002545 * If @work_color is non-negative, all pwqs should have the same
Tejun Heo73f53c42010-06-29 10:07:11 +02002546 * work_color which is previous to @work_color and all will be
2547 * advanced to @work_color.
2548 *
2549 * CONTEXT:
2550 * mutex_lock(wq->flush_mutex).
2551 *
2552 * RETURNS:
2553 * %true if @flush_color >= 0 and there's something to flush. %false
2554 * otherwise.
2555 */
Tejun Heo112202d2013-02-13 19:29:12 -08002556static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
Tejun Heo73f53c42010-06-29 10:07:11 +02002557 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558{
Tejun Heo73f53c42010-06-29 10:07:11 +02002559 bool wait = false;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002560 struct pool_workqueue *pwq;
Oleg Nesterov14441962007-05-23 13:57:57 -07002561
Tejun Heo73f53c42010-06-29 10:07:11 +02002562 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002563 WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush));
Tejun Heo112202d2013-02-13 19:29:12 -08002564 atomic_set(&wq->nr_pwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002565 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002566
Tejun Heo76af4d92013-03-12 11:30:00 -07002567 local_irq_disable();
2568
Tejun Heo49e3cf42013-03-12 11:29:58 -07002569 for_each_pwq(pwq, wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08002570 struct worker_pool *pool = pwq->pool;
Tejun Heo73f53c42010-06-29 10:07:11 +02002571
Tejun Heo76af4d92013-03-12 11:30:00 -07002572 spin_lock(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002573
2574 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002575 WARN_ON_ONCE(pwq->flush_color != -1);
Tejun Heo73f53c42010-06-29 10:07:11 +02002576
Tejun Heo112202d2013-02-13 19:29:12 -08002577 if (pwq->nr_in_flight[flush_color]) {
2578 pwq->flush_color = flush_color;
2579 atomic_inc(&wq->nr_pwqs_to_flush);
Tejun Heo73f53c42010-06-29 10:07:11 +02002580 wait = true;
2581 }
2582 }
2583
2584 if (work_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002585 WARN_ON_ONCE(work_color != work_next_color(pwq->work_color));
Tejun Heo112202d2013-02-13 19:29:12 -08002586 pwq->work_color = work_color;
Tejun Heo73f53c42010-06-29 10:07:11 +02002587 }
2588
Tejun Heo76af4d92013-03-12 11:30:00 -07002589 spin_unlock(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002590 }
2591
Tejun Heo76af4d92013-03-12 11:30:00 -07002592 local_irq_enable();
2593
Tejun Heo112202d2013-02-13 19:29:12 -08002594 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush))
Tejun Heo73f53c42010-06-29 10:07:11 +02002595 complete(&wq->first_flusher->done);
2596
2597 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598}
2599
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002600/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002602 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002604 * This function sleeps until all work items which were queued on entry
2605 * have finished execution, but it is not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002607void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608{
Tejun Heo73f53c42010-06-29 10:07:11 +02002609 struct wq_flusher this_flusher = {
2610 .list = LIST_HEAD_INIT(this_flusher.list),
2611 .flush_color = -1,
2612 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2613 };
2614 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002615
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002616 lock_map_acquire(&wq->lockdep_map);
2617 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002618
2619 mutex_lock(&wq->flush_mutex);
2620
2621 /*
2622 * Start-to-wait phase
2623 */
2624 next_color = work_next_color(wq->work_color);
2625
2626 if (next_color != wq->flush_color) {
2627 /*
2628 * Color space is not full. The current work_color
2629 * becomes our flush_color and work_color is advanced
2630 * by one.
2631 */
Tejun Heo6183c002013-03-12 11:29:57 -07002632 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow));
Tejun Heo73f53c42010-06-29 10:07:11 +02002633 this_flusher.flush_color = wq->work_color;
2634 wq->work_color = next_color;
2635
2636 if (!wq->first_flusher) {
2637 /* no flush in progress, become the first flusher */
Tejun Heo6183c002013-03-12 11:29:57 -07002638 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002639
2640 wq->first_flusher = &this_flusher;
2641
Tejun Heo112202d2013-02-13 19:29:12 -08002642 if (!flush_workqueue_prep_pwqs(wq, wq->flush_color,
Tejun Heo73f53c42010-06-29 10:07:11 +02002643 wq->work_color)) {
2644 /* nothing to flush, done */
2645 wq->flush_color = next_color;
2646 wq->first_flusher = NULL;
2647 goto out_unlock;
2648 }
2649 } else {
2650 /* wait in queue */
Tejun Heo6183c002013-03-12 11:29:57 -07002651 WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002652 list_add_tail(&this_flusher.list, &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002653 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002654 }
2655 } else {
2656 /*
2657 * Oops, color space is full, wait on overflow queue.
2658 * The next flush completion will assign us
2659 * flush_color and transfer to flusher_queue.
2660 */
2661 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2662 }
2663
2664 mutex_unlock(&wq->flush_mutex);
2665
2666 wait_for_completion(&this_flusher.done);
2667
2668 /*
2669 * Wake-up-and-cascade phase
2670 *
2671 * First flushers are responsible for cascading flushes and
2672 * handling overflow. Non-first flushers can simply return.
2673 */
2674 if (wq->first_flusher != &this_flusher)
2675 return;
2676
2677 mutex_lock(&wq->flush_mutex);
2678
Tejun Heo4ce48b32010-07-02 10:03:51 +02002679 /* we might have raced, check again with mutex held */
2680 if (wq->first_flusher != &this_flusher)
2681 goto out_unlock;
2682
Tejun Heo73f53c42010-06-29 10:07:11 +02002683 wq->first_flusher = NULL;
2684
Tejun Heo6183c002013-03-12 11:29:57 -07002685 WARN_ON_ONCE(!list_empty(&this_flusher.list));
2686 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002687
2688 while (true) {
2689 struct wq_flusher *next, *tmp;
2690
2691 /* complete all the flushers sharing the current flush color */
2692 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2693 if (next->flush_color != wq->flush_color)
2694 break;
2695 list_del_init(&next->list);
2696 complete(&next->done);
2697 }
2698
Tejun Heo6183c002013-03-12 11:29:57 -07002699 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) &&
2700 wq->flush_color != work_next_color(wq->work_color));
Tejun Heo73f53c42010-06-29 10:07:11 +02002701
2702 /* this flush_color is finished, advance by one */
2703 wq->flush_color = work_next_color(wq->flush_color);
2704
2705 /* one color has been freed, handle overflow queue */
2706 if (!list_empty(&wq->flusher_overflow)) {
2707 /*
2708 * Assign the same color to all overflowed
2709 * flushers, advance work_color and append to
2710 * flusher_queue. This is the start-to-wait
2711 * phase for these overflowed flushers.
2712 */
2713 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2714 tmp->flush_color = wq->work_color;
2715
2716 wq->work_color = work_next_color(wq->work_color);
2717
2718 list_splice_tail_init(&wq->flusher_overflow,
2719 &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002720 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002721 }
2722
2723 if (list_empty(&wq->flusher_queue)) {
Tejun Heo6183c002013-03-12 11:29:57 -07002724 WARN_ON_ONCE(wq->flush_color != wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002725 break;
2726 }
2727
2728 /*
2729 * Need to flush more colors. Make the next flusher
Tejun Heo112202d2013-02-13 19:29:12 -08002730 * the new first flusher and arm pwqs.
Tejun Heo73f53c42010-06-29 10:07:11 +02002731 */
Tejun Heo6183c002013-03-12 11:29:57 -07002732 WARN_ON_ONCE(wq->flush_color == wq->work_color);
2733 WARN_ON_ONCE(wq->flush_color != next->flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002734
2735 list_del_init(&next->list);
2736 wq->first_flusher = next;
2737
Tejun Heo112202d2013-02-13 19:29:12 -08002738 if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1))
Tejun Heo73f53c42010-06-29 10:07:11 +02002739 break;
2740
2741 /*
2742 * Meh... this color is already done, clear first
2743 * flusher and repeat cascading.
2744 */
2745 wq->first_flusher = NULL;
2746 }
2747
2748out_unlock:
2749 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750}
Dave Jonesae90dd52006-06-30 01:40:45 -04002751EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002753/**
2754 * drain_workqueue - drain a workqueue
2755 * @wq: workqueue to drain
2756 *
2757 * Wait until the workqueue becomes empty. While draining is in progress,
2758 * only chain queueing is allowed. IOW, only currently pending or running
2759 * work items on @wq can queue further work items on it. @wq is flushed
2760 * repeatedly until it becomes empty. The number of flushing is detemined
2761 * by the depth of chaining and should be relatively short. Whine if it
2762 * takes too long.
2763 */
2764void drain_workqueue(struct workqueue_struct *wq)
2765{
2766 unsigned int flush_cnt = 0;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002767 struct pool_workqueue *pwq;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002768
2769 /*
2770 * __queue_work() needs to test whether there are drainers, is much
2771 * hotter than drain_workqueue() and already looks at @wq->flags.
Tejun Heo618b01e2013-03-12 11:30:04 -07002772 * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers.
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002773 */
Tejun Heo5bcab332013-03-13 19:47:40 -07002774 mutex_lock(&wq_mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002775 if (!wq->nr_drainers++)
Tejun Heo618b01e2013-03-12 11:30:04 -07002776 wq->flags |= __WQ_DRAINING;
Tejun Heo5bcab332013-03-13 19:47:40 -07002777 mutex_unlock(&wq_mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002778reflush:
2779 flush_workqueue(wq);
2780
Tejun Heo76af4d92013-03-12 11:30:00 -07002781 local_irq_disable();
2782
Tejun Heo49e3cf42013-03-12 11:29:58 -07002783 for_each_pwq(pwq, wq) {
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002784 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002785
Tejun Heo76af4d92013-03-12 11:30:00 -07002786 spin_lock(&pwq->pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08002787 drained = !pwq->nr_active && list_empty(&pwq->delayed_works);
Tejun Heo76af4d92013-03-12 11:30:00 -07002788 spin_unlock(&pwq->pool->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002789
2790 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002791 continue;
2792
2793 if (++flush_cnt == 10 ||
2794 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002795 pr_warn("workqueue %s: drain_workqueue() isn't complete after %u tries\n",
Valentin Ilie044c7822012-08-19 00:52:42 +03002796 wq->name, flush_cnt);
Tejun Heo76af4d92013-03-12 11:30:00 -07002797
2798 local_irq_enable();
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002799 goto reflush;
2800 }
2801
Tejun Heo5bcab332013-03-13 19:47:40 -07002802 local_irq_enable();
2803
2804 mutex_lock(&wq_mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002805 if (!--wq->nr_drainers)
Tejun Heo618b01e2013-03-12 11:30:04 -07002806 wq->flags &= ~__WQ_DRAINING;
Tejun Heo5bcab332013-03-13 19:47:40 -07002807 mutex_unlock(&wq_mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002808}
2809EXPORT_SYMBOL_GPL(drain_workqueue);
2810
Tejun Heo606a5022012-08-20 14:51:23 -07002811static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
Tejun Heobaf59022010-09-16 10:42:16 +02002812{
2813 struct worker *worker = NULL;
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002814 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002815 struct pool_workqueue *pwq;
Tejun Heobaf59022010-09-16 10:42:16 +02002816
2817 might_sleep();
Tejun Heobaf59022010-09-16 10:42:16 +02002818
Tejun Heofa1b54e2013-03-12 11:30:00 -07002819 local_irq_disable();
2820 pool = get_work_pool(work);
2821 if (!pool) {
2822 local_irq_enable();
2823 return false;
2824 }
2825
2826 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08002827 /* see the comment in try_to_grab_pending() with the same code */
Tejun Heo112202d2013-02-13 19:29:12 -08002828 pwq = get_work_pwq(work);
2829 if (pwq) {
2830 if (unlikely(pwq->pool != pool))
Tejun Heobaf59022010-09-16 10:42:16 +02002831 goto already_gone;
Tejun Heo606a5022012-08-20 14:51:23 -07002832 } else {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002833 worker = find_worker_executing_work(pool, work);
Tejun Heobaf59022010-09-16 10:42:16 +02002834 if (!worker)
2835 goto already_gone;
Tejun Heo112202d2013-02-13 19:29:12 -08002836 pwq = worker->current_pwq;
Tejun Heo606a5022012-08-20 14:51:23 -07002837 }
Tejun Heobaf59022010-09-16 10:42:16 +02002838
Tejun Heo112202d2013-02-13 19:29:12 -08002839 insert_wq_barrier(pwq, barr, work, worker);
Tejun Heod565ed62013-01-24 11:01:33 -08002840 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002841
Tejun Heoe1594892011-01-09 23:32:15 +01002842 /*
2843 * If @max_active is 1 or rescuer is in use, flushing another work
2844 * item on the same workqueue may lead to deadlock. Make sure the
2845 * flusher is not running on the same workqueue by verifying write
2846 * access.
2847 */
Tejun Heo493008a2013-03-12 11:30:03 -07002848 if (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)
Tejun Heo112202d2013-02-13 19:29:12 -08002849 lock_map_acquire(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002850 else
Tejun Heo112202d2013-02-13 19:29:12 -08002851 lock_map_acquire_read(&pwq->wq->lockdep_map);
2852 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002853
Tejun Heobaf59022010-09-16 10:42:16 +02002854 return true;
2855already_gone:
Tejun Heod565ed62013-01-24 11:01:33 -08002856 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002857 return false;
2858}
2859
Oleg Nesterovdb700892008-07-25 01:47:49 -07002860/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002861 * flush_work - wait for a work to finish executing the last queueing instance
2862 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002863 *
Tejun Heo606a5022012-08-20 14:51:23 -07002864 * Wait until @work has finished execution. @work is guaranteed to be idle
2865 * on return if it hasn't been requeued since flush started.
Tejun Heo401a8d02010-09-16 10:36:00 +02002866 *
2867 * RETURNS:
2868 * %true if flush_work() waited for the work to finish execution,
2869 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002870 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002871bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002872{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002873 struct wq_barrier barr;
2874
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002875 lock_map_acquire(&work->lockdep_map);
2876 lock_map_release(&work->lockdep_map);
2877
Tejun Heo606a5022012-08-20 14:51:23 -07002878 if (start_flush_work(work, &barr)) {
Tejun Heobaf59022010-09-16 10:42:16 +02002879 wait_for_completion(&barr.done);
2880 destroy_work_on_stack(&barr.work);
2881 return true;
Tejun Heo606a5022012-08-20 14:51:23 -07002882 } else {
Tejun Heobaf59022010-09-16 10:42:16 +02002883 return false;
Tejun Heo606a5022012-08-20 14:51:23 -07002884 }
Oleg Nesterovdb700892008-07-25 01:47:49 -07002885}
2886EXPORT_SYMBOL_GPL(flush_work);
2887
Tejun Heo36e227d2012-08-03 10:30:46 -07002888static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
Tejun Heo401a8d02010-09-16 10:36:00 +02002889{
Tejun Heobbb68df2012-08-03 10:30:46 -07002890 unsigned long flags;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002891 int ret;
2892
2893 do {
Tejun Heobbb68df2012-08-03 10:30:46 -07002894 ret = try_to_grab_pending(work, is_dwork, &flags);
2895 /*
2896 * If someone else is canceling, wait for the same event it
2897 * would be waiting for before retrying.
2898 */
2899 if (unlikely(ret == -ENOENT))
Tejun Heo606a5022012-08-20 14:51:23 -07002900 flush_work(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002901 } while (unlikely(ret < 0));
2902
Tejun Heobbb68df2012-08-03 10:30:46 -07002903 /* tell other tasks trying to grab @work to back off */
2904 mark_work_canceling(work);
2905 local_irq_restore(flags);
2906
Tejun Heo606a5022012-08-20 14:51:23 -07002907 flush_work(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002908 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002909 return ret;
2910}
2911
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002912/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002913 * cancel_work_sync - cancel a work and wait for it to finish
2914 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002915 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002916 * Cancel @work and wait for its execution to finish. This function
2917 * can be used even if the work re-queues itself or migrates to
2918 * another workqueue. On return from this function, @work is
2919 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002920 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002921 * cancel_work_sync(&delayed_work->work) must not be used for
2922 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002923 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002924 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002925 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002926 *
2927 * RETURNS:
2928 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002929 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002930bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002931{
Tejun Heo36e227d2012-08-03 10:30:46 -07002932 return __cancel_work_timer(work, false);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002933}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002934EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002935
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002936/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002937 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2938 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002939 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002940 * Delayed timer is cancelled and the pending work is queued for
2941 * immediate execution. Like flush_work(), this function only
2942 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002943 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002944 * RETURNS:
2945 * %true if flush_work() waited for the work to finish execution,
2946 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002947 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002948bool flush_delayed_work(struct delayed_work *dwork)
2949{
Tejun Heo8930cab2012-08-03 10:30:45 -07002950 local_irq_disable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002951 if (del_timer_sync(&dwork->timer))
Lai Jiangshan60c057b2013-02-06 18:04:53 -08002952 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Tejun Heo8930cab2012-08-03 10:30:45 -07002953 local_irq_enable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002954 return flush_work(&dwork->work);
2955}
2956EXPORT_SYMBOL(flush_delayed_work);
2957
2958/**
Tejun Heo57b30ae2012-08-21 13:18:24 -07002959 * cancel_delayed_work - cancel a delayed work
2960 * @dwork: delayed_work to cancel
Tejun Heo09383492010-09-16 10:48:29 +02002961 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002962 * Kill off a pending delayed_work. Returns %true if @dwork was pending
2963 * and canceled; %false if wasn't pending. Note that the work callback
2964 * function may still be running on return, unless it returns %true and the
2965 * work doesn't re-arm itself. Explicitly flush or use
2966 * cancel_delayed_work_sync() to wait on it.
Tejun Heo09383492010-09-16 10:48:29 +02002967 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002968 * This function is safe to call from any context including IRQ handler.
Tejun Heo09383492010-09-16 10:48:29 +02002969 */
Tejun Heo57b30ae2012-08-21 13:18:24 -07002970bool cancel_delayed_work(struct delayed_work *dwork)
Tejun Heo09383492010-09-16 10:48:29 +02002971{
Tejun Heo57b30ae2012-08-21 13:18:24 -07002972 unsigned long flags;
2973 int ret;
2974
2975 do {
2976 ret = try_to_grab_pending(&dwork->work, true, &flags);
2977 } while (unlikely(ret == -EAGAIN));
2978
2979 if (unlikely(ret < 0))
2980 return false;
2981
Tejun Heo7c3eed52013-01-24 11:01:33 -08002982 set_work_pool_and_clear_pending(&dwork->work,
2983 get_work_pool_id(&dwork->work));
Tejun Heo57b30ae2012-08-21 13:18:24 -07002984 local_irq_restore(flags);
Dan Magenheimerc0158ca2012-10-18 16:31:37 -07002985 return ret;
Tejun Heo09383492010-09-16 10:48:29 +02002986}
Tejun Heo57b30ae2012-08-21 13:18:24 -07002987EXPORT_SYMBOL(cancel_delayed_work);
Tejun Heo09383492010-09-16 10:48:29 +02002988
2989/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002990 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2991 * @dwork: the delayed work cancel
2992 *
2993 * This is cancel_work_sync() for delayed works.
2994 *
2995 * RETURNS:
2996 * %true if @dwork was pending, %false otherwise.
2997 */
2998bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002999{
Tejun Heo36e227d2012-08-03 10:30:46 -07003000 return __cancel_work_timer(&dwork->work, true);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07003001}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07003002EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003004/**
Tejun Heo31ddd872010-10-19 11:14:49 +02003005 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07003006 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07003007 *
Tejun Heo31ddd872010-10-19 11:14:49 +02003008 * schedule_on_each_cpu() executes @func on each online CPU using the
3009 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07003010 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02003011 *
3012 * RETURNS:
3013 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07003014 */
David Howells65f27f32006-11-22 14:55:48 +00003015int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08003016{
3017 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02003018 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08003019
Andrew Mortonb6136772006-06-25 05:47:49 -07003020 works = alloc_percpu(struct work_struct);
3021 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08003022 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07003023
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003024 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08003025
Christoph Lameter15316ba2006-01-08 01:00:43 -08003026 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01003027 struct work_struct *work = per_cpu_ptr(works, cpu);
3028
3029 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003030 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02003031 }
Tejun Heo93981802009-11-17 14:06:20 -08003032
3033 for_each_online_cpu(cpu)
3034 flush_work(per_cpu_ptr(works, cpu));
3035
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003036 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07003037 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08003038 return 0;
3039}
3040
Alan Sterneef6a7d2010-02-12 17:39:21 +09003041/**
3042 * flush_scheduled_work - ensure that any scheduled work has run to completion.
3043 *
3044 * Forces execution of the kernel-global workqueue and blocks until its
3045 * completion.
3046 *
3047 * Think twice before calling this function! It's very easy to get into
3048 * trouble if you don't take great care. Either of the following situations
3049 * will lead to deadlock:
3050 *
3051 * One of the work items currently on the workqueue needs to acquire
3052 * a lock held by your code or its caller.
3053 *
3054 * Your code is running in the context of a work routine.
3055 *
3056 * They will be detected by lockdep when they occur, but the first might not
3057 * occur very often. It depends on what work items are on the workqueue and
3058 * what locks they need, which you have no control over.
3059 *
3060 * In most situations flushing the entire workqueue is overkill; you merely
3061 * need to know that a particular work item isn't queued and isn't running.
3062 * In such cases you should use cancel_delayed_work_sync() or
3063 * cancel_work_sync() instead.
3064 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065void flush_scheduled_work(void)
3066{
Tejun Heod320c032010-06-29 10:07:14 +02003067 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068}
Dave Jonesae90dd52006-06-30 01:40:45 -04003069EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
3071/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06003072 * execute_in_process_context - reliably execute the routine with user context
3073 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06003074 * @ew: guaranteed storage for the execute work structure (must
3075 * be available when the work executes)
3076 *
3077 * Executes the function immediately if process context is available,
3078 * otherwise schedules the function for delayed execution.
3079 *
3080 * Returns: 0 - function was executed
3081 * 1 - function was scheduled for execution
3082 */
David Howells65f27f32006-11-22 14:55:48 +00003083int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06003084{
3085 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00003086 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003087 return 0;
3088 }
3089
David Howells65f27f32006-11-22 14:55:48 +00003090 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003091 schedule_work(&ew->work);
3092
3093 return 1;
3094}
3095EXPORT_SYMBOL_GPL(execute_in_process_context);
3096
Tejun Heo226223a2013-03-12 11:30:05 -07003097#ifdef CONFIG_SYSFS
3098/*
3099 * Workqueues with WQ_SYSFS flag set is visible to userland via
3100 * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the
3101 * following attributes.
3102 *
3103 * per_cpu RO bool : whether the workqueue is per-cpu or unbound
3104 * max_active RW int : maximum number of in-flight work items
3105 *
3106 * Unbound workqueues have the following extra attributes.
3107 *
3108 * id RO int : the associated pool ID
3109 * nice RW int : nice value of the workers
3110 * cpumask RW mask : bitmask of allowed CPUs for the workers
3111 */
3112struct wq_device {
3113 struct workqueue_struct *wq;
3114 struct device dev;
3115};
3116
3117static struct workqueue_struct *dev_to_wq(struct device *dev)
3118{
3119 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
3120
3121 return wq_dev->wq;
3122}
3123
3124static ssize_t wq_per_cpu_show(struct device *dev,
3125 struct device_attribute *attr, char *buf)
3126{
3127 struct workqueue_struct *wq = dev_to_wq(dev);
3128
3129 return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND));
3130}
3131
3132static ssize_t wq_max_active_show(struct device *dev,
3133 struct device_attribute *attr, char *buf)
3134{
3135 struct workqueue_struct *wq = dev_to_wq(dev);
3136
3137 return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active);
3138}
3139
3140static ssize_t wq_max_active_store(struct device *dev,
3141 struct device_attribute *attr,
3142 const char *buf, size_t count)
3143{
3144 struct workqueue_struct *wq = dev_to_wq(dev);
3145 int val;
3146
3147 if (sscanf(buf, "%d", &val) != 1 || val <= 0)
3148 return -EINVAL;
3149
3150 workqueue_set_max_active(wq, val);
3151 return count;
3152}
3153
3154static struct device_attribute wq_sysfs_attrs[] = {
3155 __ATTR(per_cpu, 0444, wq_per_cpu_show, NULL),
3156 __ATTR(max_active, 0644, wq_max_active_show, wq_max_active_store),
3157 __ATTR_NULL,
3158};
3159
3160static ssize_t wq_pool_id_show(struct device *dev,
3161 struct device_attribute *attr, char *buf)
3162{
3163 struct workqueue_struct *wq = dev_to_wq(dev);
3164 struct worker_pool *pool;
3165 int written;
3166
3167 rcu_read_lock_sched();
3168 pool = first_pwq(wq)->pool;
3169 written = scnprintf(buf, PAGE_SIZE, "%d\n", pool->id);
3170 rcu_read_unlock_sched();
3171
3172 return written;
3173}
3174
3175static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr,
3176 char *buf)
3177{
3178 struct workqueue_struct *wq = dev_to_wq(dev);
3179 int written;
3180
3181 rcu_read_lock_sched();
3182 written = scnprintf(buf, PAGE_SIZE, "%d\n",
3183 first_pwq(wq)->pool->attrs->nice);
3184 rcu_read_unlock_sched();
3185
3186 return written;
3187}
3188
3189/* prepare workqueue_attrs for sysfs store operations */
3190static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq)
3191{
3192 struct workqueue_attrs *attrs;
3193
3194 attrs = alloc_workqueue_attrs(GFP_KERNEL);
3195 if (!attrs)
3196 return NULL;
3197
3198 rcu_read_lock_sched();
3199 copy_workqueue_attrs(attrs, first_pwq(wq)->pool->attrs);
3200 rcu_read_unlock_sched();
3201 return attrs;
3202}
3203
3204static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr,
3205 const char *buf, size_t count)
3206{
3207 struct workqueue_struct *wq = dev_to_wq(dev);
3208 struct workqueue_attrs *attrs;
3209 int ret;
3210
3211 attrs = wq_sysfs_prep_attrs(wq);
3212 if (!attrs)
3213 return -ENOMEM;
3214
3215 if (sscanf(buf, "%d", &attrs->nice) == 1 &&
3216 attrs->nice >= -20 && attrs->nice <= 19)
3217 ret = apply_workqueue_attrs(wq, attrs);
3218 else
3219 ret = -EINVAL;
3220
3221 free_workqueue_attrs(attrs);
3222 return ret ?: count;
3223}
3224
3225static ssize_t wq_cpumask_show(struct device *dev,
3226 struct device_attribute *attr, char *buf)
3227{
3228 struct workqueue_struct *wq = dev_to_wq(dev);
3229 int written;
3230
3231 rcu_read_lock_sched();
3232 written = cpumask_scnprintf(buf, PAGE_SIZE,
3233 first_pwq(wq)->pool->attrs->cpumask);
3234 rcu_read_unlock_sched();
3235
3236 written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
3237 return written;
3238}
3239
3240static ssize_t wq_cpumask_store(struct device *dev,
3241 struct device_attribute *attr,
3242 const char *buf, size_t count)
3243{
3244 struct workqueue_struct *wq = dev_to_wq(dev);
3245 struct workqueue_attrs *attrs;
3246 int ret;
3247
3248 attrs = wq_sysfs_prep_attrs(wq);
3249 if (!attrs)
3250 return -ENOMEM;
3251
3252 ret = cpumask_parse(buf, attrs->cpumask);
3253 if (!ret)
3254 ret = apply_workqueue_attrs(wq, attrs);
3255
3256 free_workqueue_attrs(attrs);
3257 return ret ?: count;
3258}
3259
3260static struct device_attribute wq_sysfs_unbound_attrs[] = {
3261 __ATTR(pool_id, 0444, wq_pool_id_show, NULL),
3262 __ATTR(nice, 0644, wq_nice_show, wq_nice_store),
3263 __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store),
3264 __ATTR_NULL,
3265};
3266
3267static struct bus_type wq_subsys = {
3268 .name = "workqueue",
3269 .dev_attrs = wq_sysfs_attrs,
3270};
3271
3272static int __init wq_sysfs_init(void)
3273{
3274 return subsys_virtual_register(&wq_subsys, NULL);
3275}
3276core_initcall(wq_sysfs_init);
3277
3278static void wq_device_release(struct device *dev)
3279{
3280 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
3281
3282 kfree(wq_dev);
3283}
3284
3285/**
3286 * workqueue_sysfs_register - make a workqueue visible in sysfs
3287 * @wq: the workqueue to register
3288 *
3289 * Expose @wq in sysfs under /sys/bus/workqueue/devices.
3290 * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set
3291 * which is the preferred method.
3292 *
3293 * Workqueue user should use this function directly iff it wants to apply
3294 * workqueue_attrs before making the workqueue visible in sysfs; otherwise,
3295 * apply_workqueue_attrs() may race against userland updating the
3296 * attributes.
3297 *
3298 * Returns 0 on success, -errno on failure.
3299 */
3300int workqueue_sysfs_register(struct workqueue_struct *wq)
3301{
3302 struct wq_device *wq_dev;
3303 int ret;
3304
3305 /*
3306 * Adjusting max_active or creating new pwqs by applyting
3307 * attributes breaks ordering guarantee. Disallow exposing ordered
3308 * workqueues.
3309 */
3310 if (WARN_ON(wq->flags & __WQ_ORDERED))
3311 return -EINVAL;
3312
3313 wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL);
3314 if (!wq_dev)
3315 return -ENOMEM;
3316
3317 wq_dev->wq = wq;
3318 wq_dev->dev.bus = &wq_subsys;
3319 wq_dev->dev.init_name = wq->name;
3320 wq_dev->dev.release = wq_device_release;
3321
3322 /*
3323 * unbound_attrs are created separately. Suppress uevent until
3324 * everything is ready.
3325 */
3326 dev_set_uevent_suppress(&wq_dev->dev, true);
3327
3328 ret = device_register(&wq_dev->dev);
3329 if (ret) {
3330 kfree(wq_dev);
3331 wq->wq_dev = NULL;
3332 return ret;
3333 }
3334
3335 if (wq->flags & WQ_UNBOUND) {
3336 struct device_attribute *attr;
3337
3338 for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) {
3339 ret = device_create_file(&wq_dev->dev, attr);
3340 if (ret) {
3341 device_unregister(&wq_dev->dev);
3342 wq->wq_dev = NULL;
3343 return ret;
3344 }
3345 }
3346 }
3347
3348 kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD);
3349 return 0;
3350}
3351
3352/**
3353 * workqueue_sysfs_unregister - undo workqueue_sysfs_register()
3354 * @wq: the workqueue to unregister
3355 *
3356 * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister.
3357 */
3358static void workqueue_sysfs_unregister(struct workqueue_struct *wq)
3359{
3360 struct wq_device *wq_dev = wq->wq_dev;
3361
3362 if (!wq->wq_dev)
3363 return;
3364
3365 wq->wq_dev = NULL;
3366 device_unregister(&wq_dev->dev);
3367}
3368#else /* CONFIG_SYSFS */
3369static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { }
3370#endif /* CONFIG_SYSFS */
3371
Tejun Heo7a4e3442013-03-12 11:30:00 -07003372/**
3373 * free_workqueue_attrs - free a workqueue_attrs
3374 * @attrs: workqueue_attrs to free
3375 *
3376 * Undo alloc_workqueue_attrs().
3377 */
3378void free_workqueue_attrs(struct workqueue_attrs *attrs)
3379{
3380 if (attrs) {
3381 free_cpumask_var(attrs->cpumask);
3382 kfree(attrs);
3383 }
3384}
3385
3386/**
3387 * alloc_workqueue_attrs - allocate a workqueue_attrs
3388 * @gfp_mask: allocation mask to use
3389 *
3390 * Allocate a new workqueue_attrs, initialize with default settings and
3391 * return it. Returns NULL on failure.
3392 */
3393struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask)
3394{
3395 struct workqueue_attrs *attrs;
3396
3397 attrs = kzalloc(sizeof(*attrs), gfp_mask);
3398 if (!attrs)
3399 goto fail;
3400 if (!alloc_cpumask_var(&attrs->cpumask, gfp_mask))
3401 goto fail;
3402
3403 cpumask_setall(attrs->cpumask);
3404 return attrs;
3405fail:
3406 free_workqueue_attrs(attrs);
3407 return NULL;
3408}
3409
Tejun Heo29c91e92013-03-12 11:30:03 -07003410static void copy_workqueue_attrs(struct workqueue_attrs *to,
3411 const struct workqueue_attrs *from)
3412{
3413 to->nice = from->nice;
3414 cpumask_copy(to->cpumask, from->cpumask);
3415}
3416
3417/*
3418 * Hacky implementation of jhash of bitmaps which only considers the
3419 * specified number of bits. We probably want a proper implementation in
3420 * include/linux/jhash.h.
3421 */
3422static u32 jhash_bitmap(const unsigned long *bitmap, int bits, u32 hash)
3423{
3424 int nr_longs = bits / BITS_PER_LONG;
3425 int nr_leftover = bits % BITS_PER_LONG;
3426 unsigned long leftover = 0;
3427
3428 if (nr_longs)
3429 hash = jhash(bitmap, nr_longs * sizeof(long), hash);
3430 if (nr_leftover) {
3431 bitmap_copy(&leftover, bitmap + nr_longs, nr_leftover);
3432 hash = jhash(&leftover, sizeof(long), hash);
3433 }
3434 return hash;
3435}
3436
3437/* hash value of the content of @attr */
3438static u32 wqattrs_hash(const struct workqueue_attrs *attrs)
3439{
3440 u32 hash = 0;
3441
3442 hash = jhash_1word(attrs->nice, hash);
3443 hash = jhash_bitmap(cpumask_bits(attrs->cpumask), nr_cpu_ids, hash);
3444 return hash;
3445}
3446
3447/* content equality test */
3448static bool wqattrs_equal(const struct workqueue_attrs *a,
3449 const struct workqueue_attrs *b)
3450{
3451 if (a->nice != b->nice)
3452 return false;
3453 if (!cpumask_equal(a->cpumask, b->cpumask))
3454 return false;
3455 return true;
3456}
3457
Tejun Heo7a4e3442013-03-12 11:30:00 -07003458/**
3459 * init_worker_pool - initialize a newly zalloc'd worker_pool
3460 * @pool: worker_pool to initialize
3461 *
3462 * Initiailize a newly zalloc'd @pool. It also allocates @pool->attrs.
Tejun Heo29c91e92013-03-12 11:30:03 -07003463 * Returns 0 on success, -errno on failure. Even on failure, all fields
3464 * inside @pool proper are initialized and put_unbound_pool() can be called
3465 * on @pool safely to release it.
Tejun Heo7a4e3442013-03-12 11:30:00 -07003466 */
3467static int init_worker_pool(struct worker_pool *pool)
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003468{
3469 spin_lock_init(&pool->lock);
Tejun Heo29c91e92013-03-12 11:30:03 -07003470 pool->id = -1;
3471 pool->cpu = -1;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003472 pool->flags |= POOL_DISASSOCIATED;
3473 INIT_LIST_HEAD(&pool->worklist);
3474 INIT_LIST_HEAD(&pool->idle_list);
3475 hash_init(pool->busy_hash);
3476
3477 init_timer_deferrable(&pool->idle_timer);
3478 pool->idle_timer.function = idle_worker_timeout;
3479 pool->idle_timer.data = (unsigned long)pool;
3480
3481 setup_timer(&pool->mayday_timer, pool_mayday_timeout,
3482 (unsigned long)pool);
3483
3484 mutex_init(&pool->manager_arb);
Tejun Heobc3a1af2013-03-13 19:47:39 -07003485 mutex_init(&pool->manager_mutex);
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003486 ida_init(&pool->worker_ida);
Tejun Heo7a4e3442013-03-12 11:30:00 -07003487
Tejun Heo29c91e92013-03-12 11:30:03 -07003488 INIT_HLIST_NODE(&pool->hash_node);
3489 pool->refcnt = 1;
3490
3491 /* shouldn't fail above this point */
Tejun Heo7a4e3442013-03-12 11:30:00 -07003492 pool->attrs = alloc_workqueue_attrs(GFP_KERNEL);
3493 if (!pool->attrs)
3494 return -ENOMEM;
3495 return 0;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003496}
3497
Tejun Heo29c91e92013-03-12 11:30:03 -07003498static void rcu_free_pool(struct rcu_head *rcu)
3499{
3500 struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu);
3501
3502 ida_destroy(&pool->worker_ida);
3503 free_workqueue_attrs(pool->attrs);
3504 kfree(pool);
3505}
3506
3507/**
3508 * put_unbound_pool - put a worker_pool
3509 * @pool: worker_pool to put
3510 *
3511 * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003512 * safe manner. get_unbound_pool() calls this function on its failure path
3513 * and this function should be able to release pools which went through,
3514 * successfully or not, init_worker_pool().
Tejun Heo29c91e92013-03-12 11:30:03 -07003515 */
3516static void put_unbound_pool(struct worker_pool *pool)
3517{
3518 struct worker *worker;
3519
Tejun Heo5bcab332013-03-13 19:47:40 -07003520 mutex_lock(&wq_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003521 if (--pool->refcnt) {
Tejun Heo5bcab332013-03-13 19:47:40 -07003522 mutex_unlock(&wq_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003523 return;
3524 }
3525
3526 /* sanity checks */
3527 if (WARN_ON(!(pool->flags & POOL_DISASSOCIATED)) ||
3528 WARN_ON(!list_empty(&pool->worklist))) {
Tejun Heo5bcab332013-03-13 19:47:40 -07003529 mutex_unlock(&wq_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003530 return;
3531 }
3532
3533 /* release id and unhash */
3534 if (pool->id >= 0)
3535 idr_remove(&worker_pool_idr, pool->id);
3536 hash_del(&pool->hash_node);
3537
Tejun Heo5bcab332013-03-13 19:47:40 -07003538 mutex_unlock(&wq_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003539
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003540 /*
3541 * Become the manager and destroy all workers. Grabbing
3542 * manager_arb prevents @pool's workers from blocking on
3543 * manager_mutex.
3544 */
Tejun Heo29c91e92013-03-12 11:30:03 -07003545 mutex_lock(&pool->manager_arb);
Tejun Heocd549682013-03-13 19:47:39 -07003546 mutex_lock(&pool->manager_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003547 spin_lock_irq(&pool->lock);
3548
3549 while ((worker = first_worker(pool)))
3550 destroy_worker(worker);
3551 WARN_ON(pool->nr_workers || pool->nr_idle);
3552
3553 spin_unlock_irq(&pool->lock);
Tejun Heocd549682013-03-13 19:47:39 -07003554 mutex_unlock(&pool->manager_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003555 mutex_unlock(&pool->manager_arb);
3556
3557 /* shut down the timers */
3558 del_timer_sync(&pool->idle_timer);
3559 del_timer_sync(&pool->mayday_timer);
3560
3561 /* sched-RCU protected to allow dereferences from get_work_pool() */
3562 call_rcu_sched(&pool->rcu, rcu_free_pool);
3563}
3564
3565/**
3566 * get_unbound_pool - get a worker_pool with the specified attributes
3567 * @attrs: the attributes of the worker_pool to get
3568 *
3569 * Obtain a worker_pool which has the same attributes as @attrs, bump the
3570 * reference count and return it. If there already is a matching
3571 * worker_pool, it will be used; otherwise, this function attempts to
3572 * create a new one. On failure, returns NULL.
3573 */
3574static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs)
3575{
Tejun Heo29c91e92013-03-12 11:30:03 -07003576 u32 hash = wqattrs_hash(attrs);
3577 struct worker_pool *pool;
Tejun Heo29c91e92013-03-12 11:30:03 -07003578
Tejun Heo5bcab332013-03-13 19:47:40 -07003579 mutex_lock(&wq_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003580
3581 /* do we already have a matching pool? */
Tejun Heo29c91e92013-03-12 11:30:03 -07003582 hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) {
3583 if (wqattrs_equal(pool->attrs, attrs)) {
3584 pool->refcnt++;
3585 goto out_unlock;
3586 }
3587 }
Tejun Heo29c91e92013-03-12 11:30:03 -07003588
3589 /* nope, create a new one */
3590 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
3591 if (!pool || init_worker_pool(pool) < 0)
3592 goto fail;
3593
Tejun Heo8864b4e2013-03-12 11:30:04 -07003594 lockdep_set_subclass(&pool->lock, 1); /* see put_pwq() */
Tejun Heo29c91e92013-03-12 11:30:03 -07003595 copy_workqueue_attrs(pool->attrs, attrs);
3596
3597 if (worker_pool_assign_id(pool) < 0)
3598 goto fail;
3599
3600 /* create and start the initial worker */
Tejun Heoebf44d12013-03-13 19:47:39 -07003601 if (create_and_start_worker(pool) < 0)
Tejun Heo29c91e92013-03-12 11:30:03 -07003602 goto fail;
3603
Tejun Heo29c91e92013-03-12 11:30:03 -07003604 /* install */
Tejun Heo29c91e92013-03-12 11:30:03 -07003605 hash_add(unbound_pool_hash, &pool->hash_node, hash);
3606out_unlock:
Tejun Heo5bcab332013-03-13 19:47:40 -07003607 mutex_unlock(&wq_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003608 return pool;
3609fail:
Tejun Heo5bcab332013-03-13 19:47:40 -07003610 mutex_unlock(&wq_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003611 if (pool)
3612 put_unbound_pool(pool);
3613 return NULL;
3614}
3615
Tejun Heo8864b4e2013-03-12 11:30:04 -07003616static void rcu_free_pwq(struct rcu_head *rcu)
3617{
3618 kmem_cache_free(pwq_cache,
3619 container_of(rcu, struct pool_workqueue, rcu));
3620}
3621
3622/*
3623 * Scheduled on system_wq by put_pwq() when an unbound pwq hits zero refcnt
3624 * and needs to be destroyed.
3625 */
3626static void pwq_unbound_release_workfn(struct work_struct *work)
3627{
3628 struct pool_workqueue *pwq = container_of(work, struct pool_workqueue,
3629 unbound_release_work);
3630 struct workqueue_struct *wq = pwq->wq;
3631 struct worker_pool *pool = pwq->pool;
3632
3633 if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)))
3634 return;
3635
Tejun Heo75ccf592013-03-12 11:30:04 -07003636 /*
3637 * Unlink @pwq. Synchronization against flush_mutex isn't strictly
3638 * necessary on release but do it anyway. It's easier to verify
3639 * and consistent with the linking path.
3640 */
3641 mutex_lock(&wq->flush_mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003642 spin_lock_irq(&workqueue_lock);
3643 list_del_rcu(&pwq->pwqs_node);
3644 spin_unlock_irq(&workqueue_lock);
Tejun Heo75ccf592013-03-12 11:30:04 -07003645 mutex_unlock(&wq->flush_mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003646
3647 put_unbound_pool(pool);
3648 call_rcu_sched(&pwq->rcu, rcu_free_pwq);
3649
3650 /*
3651 * If we're the last pwq going away, @wq is already dead and no one
3652 * is gonna access it anymore. Free it.
3653 */
3654 if (list_empty(&wq->pwqs))
3655 kfree(wq);
3656}
3657
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003658/**
Tejun Heo699ce092013-03-13 16:51:35 -07003659 * pwq_adjust_max_active - update a pwq's max_active to the current setting
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003660 * @pwq: target pool_workqueue
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003661 *
Tejun Heo699ce092013-03-13 16:51:35 -07003662 * If @pwq isn't freezing, set @pwq->max_active to the associated
3663 * workqueue's saved_max_active and activate delayed work items
3664 * accordingly. If @pwq is freezing, clear @pwq->max_active to zero.
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003665 */
Tejun Heo699ce092013-03-13 16:51:35 -07003666static void pwq_adjust_max_active(struct pool_workqueue *pwq)
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003667{
Tejun Heo699ce092013-03-13 16:51:35 -07003668 struct workqueue_struct *wq = pwq->wq;
3669 bool freezable = wq->flags & WQ_FREEZABLE;
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003670
Tejun Heo699ce092013-03-13 16:51:35 -07003671 /* for @wq->saved_max_active */
3672 lockdep_assert_held(&workqueue_lock);
3673
3674 /* fast exit for non-freezable wqs */
3675 if (!freezable && pwq->max_active == wq->saved_max_active)
3676 return;
3677
3678 spin_lock(&pwq->pool->lock);
3679
3680 if (!freezable || !(pwq->pool->flags & POOL_FREEZING)) {
3681 pwq->max_active = wq->saved_max_active;
3682
3683 while (!list_empty(&pwq->delayed_works) &&
3684 pwq->nr_active < pwq->max_active)
3685 pwq_activate_first_delayed(pwq);
3686 } else {
3687 pwq->max_active = 0;
3688 }
3689
3690 spin_unlock(&pwq->pool->lock);
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003691}
3692
Tejun Heod2c1d402013-03-12 11:30:04 -07003693static void init_and_link_pwq(struct pool_workqueue *pwq,
3694 struct workqueue_struct *wq,
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003695 struct worker_pool *pool,
3696 struct pool_workqueue **p_last_pwq)
Tejun Heod2c1d402013-03-12 11:30:04 -07003697{
3698 BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK);
3699
3700 pwq->pool = pool;
3701 pwq->wq = wq;
3702 pwq->flush_color = -1;
Tejun Heo8864b4e2013-03-12 11:30:04 -07003703 pwq->refcnt = 1;
Tejun Heod2c1d402013-03-12 11:30:04 -07003704 INIT_LIST_HEAD(&pwq->delayed_works);
3705 INIT_LIST_HEAD(&pwq->mayday_node);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003706 INIT_WORK(&pwq->unbound_release_work, pwq_unbound_release_workfn);
Tejun Heod2c1d402013-03-12 11:30:04 -07003707
Tejun Heo75ccf592013-03-12 11:30:04 -07003708 mutex_lock(&wq->flush_mutex);
3709 spin_lock_irq(&workqueue_lock);
3710
Tejun Heo983ca252013-03-13 16:51:35 -07003711 /*
3712 * Set the matching work_color. This is synchronized with
3713 * flush_mutex to avoid confusing flush_workqueue().
3714 */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003715 if (p_last_pwq)
3716 *p_last_pwq = first_pwq(wq);
Tejun Heo75ccf592013-03-12 11:30:04 -07003717 pwq->work_color = wq->work_color;
Tejun Heo983ca252013-03-13 16:51:35 -07003718
3719 /* sync max_active to the current setting */
3720 pwq_adjust_max_active(pwq);
3721
3722 /* link in @pwq */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003723 list_add_rcu(&pwq->pwqs_node, &wq->pwqs);
Tejun Heo75ccf592013-03-12 11:30:04 -07003724
3725 spin_unlock_irq(&workqueue_lock);
3726 mutex_unlock(&wq->flush_mutex);
Tejun Heod2c1d402013-03-12 11:30:04 -07003727}
3728
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003729/**
3730 * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue
3731 * @wq: the target workqueue
3732 * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs()
3733 *
3734 * Apply @attrs to an unbound workqueue @wq. If @attrs doesn't match the
3735 * current attributes, a new pwq is created and made the first pwq which
3736 * will serve all new work items. Older pwqs are released as in-flight
3737 * work items finish. Note that a work item which repeatedly requeues
3738 * itself back-to-back will stay on its current pwq.
3739 *
3740 * Performs GFP_KERNEL allocations. Returns 0 on success and -errno on
3741 * failure.
3742 */
3743int apply_workqueue_attrs(struct workqueue_struct *wq,
3744 const struct workqueue_attrs *attrs)
3745{
3746 struct pool_workqueue *pwq, *last_pwq;
3747 struct worker_pool *pool;
3748
Tejun Heo8719dce2013-03-12 11:30:04 -07003749 /* only unbound workqueues can change attributes */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003750 if (WARN_ON(!(wq->flags & WQ_UNBOUND)))
3751 return -EINVAL;
3752
Tejun Heo8719dce2013-03-12 11:30:04 -07003753 /* creating multiple pwqs breaks ordering guarantee */
3754 if (WARN_ON((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs)))
3755 return -EINVAL;
3756
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003757 pwq = kmem_cache_zalloc(pwq_cache, GFP_KERNEL);
3758 if (!pwq)
3759 return -ENOMEM;
3760
3761 pool = get_unbound_pool(attrs);
3762 if (!pool) {
3763 kmem_cache_free(pwq_cache, pwq);
3764 return -ENOMEM;
3765 }
3766
3767 init_and_link_pwq(pwq, wq, pool, &last_pwq);
3768 if (last_pwq) {
3769 spin_lock_irq(&last_pwq->pool->lock);
3770 put_pwq(last_pwq);
3771 spin_unlock_irq(&last_pwq->pool->lock);
3772 }
3773
3774 return 0;
3775}
3776
Tejun Heo30cdf242013-03-12 11:29:57 -07003777static int alloc_and_link_pwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003779 bool highpri = wq->flags & WQ_HIGHPRI;
Tejun Heo30cdf242013-03-12 11:29:57 -07003780 int cpu;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01003781
Tejun Heo30cdf242013-03-12 11:29:57 -07003782 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo420c0dd2013-03-12 11:29:59 -07003783 wq->cpu_pwqs = alloc_percpu(struct pool_workqueue);
3784 if (!wq->cpu_pwqs)
Tejun Heo30cdf242013-03-12 11:29:57 -07003785 return -ENOMEM;
3786
3787 for_each_possible_cpu(cpu) {
Tejun Heo7fb98ea2013-03-12 11:30:00 -07003788 struct pool_workqueue *pwq =
3789 per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heo7a62c2c2013-03-12 11:30:03 -07003790 struct worker_pool *cpu_pools =
Tejun Heof02ae732013-03-12 11:30:03 -07003791 per_cpu(cpu_worker_pools, cpu);
Tejun Heo30cdf242013-03-12 11:29:57 -07003792
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003793 init_and_link_pwq(pwq, wq, &cpu_pools[highpri], NULL);
Tejun Heo30cdf242013-03-12 11:29:57 -07003794 }
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003795 return 0;
Tejun Heo30cdf242013-03-12 11:29:57 -07003796 } else {
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003797 return apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]);
Tejun Heo30cdf242013-03-12 11:29:57 -07003798 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003799}
3800
Tejun Heof3421792010-07-02 10:03:51 +02003801static int wq_clamp_max_active(int max_active, unsigned int flags,
3802 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003803{
Tejun Heof3421792010-07-02 10:03:51 +02003804 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3805
3806 if (max_active < 1 || max_active > lim)
Valentin Ilie044c7822012-08-19 00:52:42 +03003807 pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
3808 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003809
Tejun Heof3421792010-07-02 10:03:51 +02003810 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003811}
3812
Tejun Heob196be82012-01-10 15:11:35 -08003813struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02003814 unsigned int flags,
3815 int max_active,
3816 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08003817 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003818{
Tejun Heob196be82012-01-10 15:11:35 -08003819 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003820 struct workqueue_struct *wq;
Tejun Heo49e3cf42013-03-12 11:29:58 -07003821 struct pool_workqueue *pwq;
Tejun Heob196be82012-01-10 15:11:35 -08003822 size_t namelen;
3823
3824 /* determine namelen, allocate wq and format name */
3825 va_start(args, lock_name);
3826 va_copy(args1, args);
3827 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3828
3829 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3830 if (!wq)
Tejun Heod2c1d402013-03-12 11:30:04 -07003831 return NULL;
Tejun Heob196be82012-01-10 15:11:35 -08003832
3833 vsnprintf(wq->name, namelen, fmt, args1);
3834 va_end(args);
3835 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003836
Tejun Heod320c032010-06-29 10:07:14 +02003837 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003838 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003839
Tejun Heob196be82012-01-10 15:11:35 -08003840 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003841 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003842 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003843 mutex_init(&wq->flush_mutex);
Tejun Heo112202d2013-02-13 19:29:12 -08003844 atomic_set(&wq->nr_pwqs_to_flush, 0);
Tejun Heo30cdf242013-03-12 11:29:57 -07003845 INIT_LIST_HEAD(&wq->pwqs);
Tejun Heo73f53c42010-06-29 10:07:11 +02003846 INIT_LIST_HEAD(&wq->flusher_queue);
3847 INIT_LIST_HEAD(&wq->flusher_overflow);
Tejun Heo493a1722013-03-12 11:29:59 -07003848 INIT_LIST_HEAD(&wq->maydays);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003849
Johannes Bergeb13ba82008-01-16 09:51:58 +01003850 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003851 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003852
Tejun Heo30cdf242013-03-12 11:29:57 -07003853 if (alloc_and_link_pwqs(wq) < 0)
Tejun Heod2c1d402013-03-12 11:30:04 -07003854 goto err_free_wq;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003855
Tejun Heo493008a2013-03-12 11:30:03 -07003856 /*
3857 * Workqueues which may be used during memory reclaim should
3858 * have a rescuer to guarantee forward progress.
3859 */
3860 if (flags & WQ_MEM_RECLAIM) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003861 struct worker *rescuer;
3862
Tejun Heod2c1d402013-03-12 11:30:04 -07003863 rescuer = alloc_worker();
Tejun Heoe22bee72010-06-29 10:07:14 +02003864 if (!rescuer)
Tejun Heod2c1d402013-03-12 11:30:04 -07003865 goto err_destroy;
Tejun Heoe22bee72010-06-29 10:07:14 +02003866
Tejun Heo111c2252013-01-17 17:16:24 -08003867 rescuer->rescue_wq = wq;
3868 rescuer->task = kthread_create(rescuer_thread, rescuer, "%s",
Tejun Heob196be82012-01-10 15:11:35 -08003869 wq->name);
Tejun Heod2c1d402013-03-12 11:30:04 -07003870 if (IS_ERR(rescuer->task)) {
3871 kfree(rescuer);
3872 goto err_destroy;
3873 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003874
Tejun Heod2c1d402013-03-12 11:30:04 -07003875 wq->rescuer = rescuer;
Tejun Heoe22bee72010-06-29 10:07:14 +02003876 rescuer->task->flags |= PF_THREAD_BOUND;
3877 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003878 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003879
Tejun Heo226223a2013-03-12 11:30:05 -07003880 if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq))
3881 goto err_destroy;
3882
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003883 /*
Tejun Heo5bcab332013-03-13 19:47:40 -07003884 * wq_mutex protects global freeze state and workqueues list. Grab
3885 * it, adjust max_active and add the new @wq to workqueues list.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003886 */
Tejun Heo5bcab332013-03-13 19:47:40 -07003887 mutex_lock(&wq_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003888
Tejun Heo5bcab332013-03-13 19:47:40 -07003889 spin_lock_irq(&workqueue_lock);
Tejun Heo699ce092013-03-13 16:51:35 -07003890 for_each_pwq(pwq, wq)
3891 pwq_adjust_max_active(pwq);
Tejun Heo5bcab332013-03-13 19:47:40 -07003892 spin_unlock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003893
Tejun Heo15376632010-06-29 10:07:11 +02003894 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003895
Tejun Heo5bcab332013-03-13 19:47:40 -07003896 mutex_unlock(&wq_mutex);
Tejun Heo15376632010-06-29 10:07:11 +02003897
Oleg Nesterov3af244332007-05-09 02:34:09 -07003898 return wq;
Tejun Heod2c1d402013-03-12 11:30:04 -07003899
3900err_free_wq:
3901 kfree(wq);
3902 return NULL;
3903err_destroy:
3904 destroy_workqueue(wq);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003905 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003906}
Tejun Heod320c032010-06-29 10:07:14 +02003907EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003908
3909/**
3910 * destroy_workqueue - safely terminate a workqueue
3911 * @wq: target workqueue
3912 *
3913 * Safely destroy a workqueue. All work currently pending will be done first.
3914 */
3915void destroy_workqueue(struct workqueue_struct *wq)
3916{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003917 struct pool_workqueue *pwq;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003918
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003919 /* drain it before proceeding with destruction */
3920 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003921
Tejun Heo6183c002013-03-12 11:29:57 -07003922 /* sanity checks */
Tejun Heo5bcab332013-03-13 19:47:40 -07003923 spin_lock_irq(&workqueue_lock);
Tejun Heo49e3cf42013-03-12 11:29:58 -07003924 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07003925 int i;
3926
Tejun Heo76af4d92013-03-12 11:30:00 -07003927 for (i = 0; i < WORK_NR_COLORS; i++) {
3928 if (WARN_ON(pwq->nr_in_flight[i])) {
3929 spin_unlock_irq(&workqueue_lock);
Tejun Heo6183c002013-03-12 11:29:57 -07003930 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07003931 }
3932 }
3933
Tejun Heo8864b4e2013-03-12 11:30:04 -07003934 if (WARN_ON(pwq->refcnt > 1) ||
3935 WARN_ON(pwq->nr_active) ||
Tejun Heo76af4d92013-03-12 11:30:00 -07003936 WARN_ON(!list_empty(&pwq->delayed_works))) {
3937 spin_unlock_irq(&workqueue_lock);
Tejun Heo6183c002013-03-12 11:29:57 -07003938 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07003939 }
Tejun Heo6183c002013-03-12 11:29:57 -07003940 }
Tejun Heo5bcab332013-03-13 19:47:40 -07003941 spin_unlock_irq(&workqueue_lock);
Tejun Heo6183c002013-03-12 11:29:57 -07003942
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003943 /*
3944 * wq list is used to freeze wq, remove from list after
3945 * flushing is complete in case freeze races us.
3946 */
Tejun Heo5bcab332013-03-13 19:47:40 -07003947 mutex_lock(&wq_mutex);
Tejun Heod2c1d402013-03-12 11:30:04 -07003948 list_del_init(&wq->list);
Tejun Heo5bcab332013-03-13 19:47:40 -07003949 mutex_unlock(&wq_mutex);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003950
Tejun Heo226223a2013-03-12 11:30:05 -07003951 workqueue_sysfs_unregister(wq);
3952
Tejun Heo493008a2013-03-12 11:30:03 -07003953 if (wq->rescuer) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003954 kthread_stop(wq->rescuer->task);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003955 kfree(wq->rescuer);
Tejun Heo493008a2013-03-12 11:30:03 -07003956 wq->rescuer = NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +02003957 }
3958
Tejun Heo8864b4e2013-03-12 11:30:04 -07003959 if (!(wq->flags & WQ_UNBOUND)) {
3960 /*
3961 * The base ref is never dropped on per-cpu pwqs. Directly
3962 * free the pwqs and wq.
3963 */
3964 free_percpu(wq->cpu_pwqs);
3965 kfree(wq);
3966 } else {
3967 /*
3968 * We're the sole accessor of @wq at this point. Directly
3969 * access the first pwq and put the base ref. As both pwqs
3970 * and pools are sched-RCU protected, the lock operations
3971 * are safe. @wq will be freed when the last pwq is
3972 * released.
3973 */
Tejun Heo29c91e92013-03-12 11:30:03 -07003974 pwq = list_first_entry(&wq->pwqs, struct pool_workqueue,
3975 pwqs_node);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003976 spin_lock_irq(&pwq->pool->lock);
3977 put_pwq(pwq);
3978 spin_unlock_irq(&pwq->pool->lock);
Tejun Heo29c91e92013-03-12 11:30:03 -07003979 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003980}
3981EXPORT_SYMBOL_GPL(destroy_workqueue);
3982
Tejun Heodcd989c2010-06-29 10:07:14 +02003983/**
3984 * workqueue_set_max_active - adjust max_active of a workqueue
3985 * @wq: target workqueue
3986 * @max_active: new max_active value.
3987 *
3988 * Set max_active of @wq to @max_active.
3989 *
3990 * CONTEXT:
3991 * Don't call from IRQ context.
3992 */
3993void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3994{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003995 struct pool_workqueue *pwq;
Tejun Heodcd989c2010-06-29 10:07:14 +02003996
Tejun Heo8719dce2013-03-12 11:30:04 -07003997 /* disallow meddling with max_active for ordered workqueues */
3998 if (WARN_ON(wq->flags & __WQ_ORDERED))
3999 return;
4000
Tejun Heof3421792010-07-02 10:03:51 +02004001 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02004002
Tejun Heoe98d5b12013-03-12 11:29:57 -07004003 spin_lock_irq(&workqueue_lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02004004
4005 wq->saved_max_active = max_active;
4006
Tejun Heo699ce092013-03-13 16:51:35 -07004007 for_each_pwq(pwq, wq)
4008 pwq_adjust_max_active(pwq);
Tejun Heodcd989c2010-06-29 10:07:14 +02004009
Tejun Heoe98d5b12013-03-12 11:29:57 -07004010 spin_unlock_irq(&workqueue_lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02004011}
4012EXPORT_SYMBOL_GPL(workqueue_set_max_active);
4013
4014/**
Tejun Heoe6267612013-03-12 17:41:37 -07004015 * current_is_workqueue_rescuer - is %current workqueue rescuer?
4016 *
4017 * Determine whether %current is a workqueue rescuer. Can be used from
4018 * work functions to determine whether it's being run off the rescuer task.
4019 */
4020bool current_is_workqueue_rescuer(void)
4021{
4022 struct worker *worker = current_wq_worker();
4023
4024 return worker && worker == worker->current_pwq->wq->rescuer;
4025}
4026
4027/**
Tejun Heodcd989c2010-06-29 10:07:14 +02004028 * workqueue_congested - test whether a workqueue is congested
4029 * @cpu: CPU in question
4030 * @wq: target workqueue
4031 *
4032 * Test whether @wq's cpu workqueue for @cpu is congested. There is
4033 * no synchronization around this function and the test result is
4034 * unreliable and only useful as advisory hints or for debugging.
4035 *
4036 * RETURNS:
4037 * %true if congested, %false otherwise.
4038 */
Tejun Heod84ff052013-03-12 11:29:59 -07004039bool workqueue_congested(int cpu, struct workqueue_struct *wq)
Tejun Heodcd989c2010-06-29 10:07:14 +02004040{
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004041 struct pool_workqueue *pwq;
Tejun Heo76af4d92013-03-12 11:30:00 -07004042 bool ret;
4043
4044 preempt_disable();
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004045
4046 if (!(wq->flags & WQ_UNBOUND))
4047 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
4048 else
4049 pwq = first_pwq(wq);
Tejun Heodcd989c2010-06-29 10:07:14 +02004050
Tejun Heo76af4d92013-03-12 11:30:00 -07004051 ret = !list_empty(&pwq->delayed_works);
4052 preempt_enable();
4053
4054 return ret;
Tejun Heodcd989c2010-06-29 10:07:14 +02004055}
4056EXPORT_SYMBOL_GPL(workqueue_congested);
4057
4058/**
Tejun Heodcd989c2010-06-29 10:07:14 +02004059 * work_busy - test whether a work is currently pending or running
4060 * @work: the work to be tested
4061 *
4062 * Test whether @work is currently pending or running. There is no
4063 * synchronization around this function and the test result is
4064 * unreliable and only useful as advisory hints or for debugging.
Tejun Heodcd989c2010-06-29 10:07:14 +02004065 *
4066 * RETURNS:
4067 * OR'd bitmask of WORK_BUSY_* bits.
4068 */
4069unsigned int work_busy(struct work_struct *work)
4070{
Tejun Heofa1b54e2013-03-12 11:30:00 -07004071 struct worker_pool *pool;
Tejun Heodcd989c2010-06-29 10:07:14 +02004072 unsigned long flags;
4073 unsigned int ret = 0;
4074
Tejun Heodcd989c2010-06-29 10:07:14 +02004075 if (work_pending(work))
4076 ret |= WORK_BUSY_PENDING;
Tejun Heodcd989c2010-06-29 10:07:14 +02004077
Tejun Heofa1b54e2013-03-12 11:30:00 -07004078 local_irq_save(flags);
4079 pool = get_work_pool(work);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004080 if (pool) {
Tejun Heofa1b54e2013-03-12 11:30:00 -07004081 spin_lock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004082 if (find_worker_executing_work(pool, work))
4083 ret |= WORK_BUSY_RUNNING;
Tejun Heofa1b54e2013-03-12 11:30:00 -07004084 spin_unlock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004085 }
Tejun Heofa1b54e2013-03-12 11:30:00 -07004086 local_irq_restore(flags);
Tejun Heodcd989c2010-06-29 10:07:14 +02004087
4088 return ret;
4089}
4090EXPORT_SYMBOL_GPL(work_busy);
4091
Tejun Heodb7bccf2010-06-29 10:07:12 +02004092/*
4093 * CPU hotplug.
4094 *
Tejun Heoe22bee72010-06-29 10:07:14 +02004095 * There are two challenges in supporting CPU hotplug. Firstly, there
Tejun Heo112202d2013-02-13 19:29:12 -08004096 * are a lot of assumptions on strong associations among work, pwq and
Tejun Heo706026c2013-01-24 11:01:34 -08004097 * pool which make migrating pending and scheduled works very
Tejun Heoe22bee72010-06-29 10:07:14 +02004098 * difficult to implement without impacting hot paths. Secondly,
Tejun Heo94cf58b2013-01-24 11:01:33 -08004099 * worker pools serve mix of short, long and very long running works making
Tejun Heoe22bee72010-06-29 10:07:14 +02004100 * blocked draining impractical.
4101 *
Tejun Heo24647572013-01-24 11:01:33 -08004102 * This is solved by allowing the pools to be disassociated from the CPU
Tejun Heo628c78e2012-07-17 12:39:27 -07004103 * running as an unbound one and allowing it to be reattached later if the
4104 * cpu comes back online.
Tejun Heodb7bccf2010-06-29 10:07:12 +02004105 */
4106
Tejun Heo706026c2013-01-24 11:01:34 -08004107static void wq_unbind_fn(struct work_struct *work)
Tejun Heodb7bccf2010-06-29 10:07:12 +02004108{
Tejun Heo38db41d2013-01-24 11:01:34 -08004109 int cpu = smp_processor_id();
Tejun Heo4ce62e92012-07-13 22:16:44 -07004110 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004111 struct worker *worker;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004112 int i;
4113
Tejun Heof02ae732013-03-12 11:30:03 -07004114 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo6183c002013-03-12 11:29:57 -07004115 WARN_ON_ONCE(cpu != smp_processor_id());
Tejun Heo94cf58b2013-01-24 11:01:33 -08004116
Tejun Heobc3a1af2013-03-13 19:47:39 -07004117 mutex_lock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004118 spin_lock_irq(&pool->lock);
4119
4120 /*
Tejun Heobc3a1af2013-03-13 19:47:39 -07004121 * We've blocked all manager operations. Make all workers
Tejun Heo94cf58b2013-01-24 11:01:33 -08004122 * unbound and set DISASSOCIATED. Before this, all workers
4123 * except for the ones which are still executing works from
4124 * before the last CPU down must be on the cpu. After
4125 * this, they may become diasporas.
4126 */
Tejun Heo4ce62e92012-07-13 22:16:44 -07004127 list_for_each_entry(worker, &pool->idle_list, entry)
Tejun Heo403c8212012-07-17 12:39:27 -07004128 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004129
Sasha Levinb67bfe02013-02-27 17:06:00 -08004130 for_each_busy_worker(worker, i, pool)
Tejun Heoc9e7cf22013-01-24 11:01:33 -08004131 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004132
Tejun Heo24647572013-01-24 11:01:33 -08004133 pool->flags |= POOL_DISASSOCIATED;
Tejun Heof2d5a0e2012-07-17 12:39:26 -07004134
Tejun Heo94cf58b2013-01-24 11:01:33 -08004135 spin_unlock_irq(&pool->lock);
Tejun Heobc3a1af2013-03-13 19:47:39 -07004136 mutex_unlock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004137 }
Tejun Heoe22bee72010-06-29 10:07:14 +02004138
4139 /*
Tejun Heo628c78e2012-07-17 12:39:27 -07004140 * Call schedule() so that we cross rq->lock and thus can guarantee
4141 * sched callbacks see the %WORKER_UNBOUND flag. This is necessary
4142 * as scheduler callbacks may be invoked from other cpus.
4143 */
4144 schedule();
4145
4146 /*
4147 * Sched callbacks are disabled now. Zap nr_running. After this,
4148 * nr_running stays zero and need_more_worker() and keep_working()
Tejun Heo38db41d2013-01-24 11:01:34 -08004149 * are always true as long as the worklist is not empty. Pools on
4150 * @cpu now behave as unbound (in terms of concurrency management)
4151 * pools which are served by workers tied to the CPU.
Tejun Heo628c78e2012-07-17 12:39:27 -07004152 *
4153 * On return from this function, the current worker would trigger
4154 * unbound chain execution of pending work items if other workers
4155 * didn't already.
Tejun Heoe22bee72010-06-29 10:07:14 +02004156 */
Tejun Heof02ae732013-03-12 11:30:03 -07004157 for_each_cpu_worker_pool(pool, cpu)
Tejun Heoe19e3972013-01-24 11:39:44 -08004158 atomic_set(&pool->nr_running, 0);
Tejun Heodb7bccf2010-06-29 10:07:12 +02004159}
4160
Tejun Heo8db25e72012-07-17 12:39:28 -07004161/*
4162 * Workqueues should be brought up before normal priority CPU notifiers.
4163 * This will be registered high priority CPU notifier.
4164 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07004165static int __cpuinit workqueue_cpu_up_callback(struct notifier_block *nfb,
Tejun Heo8db25e72012-07-17 12:39:28 -07004166 unsigned long action,
4167 void *hcpu)
Oleg Nesterov3af244332007-05-09 02:34:09 -07004168{
Tejun Heod84ff052013-03-12 11:29:59 -07004169 int cpu = (unsigned long)hcpu;
Tejun Heo4ce62e92012-07-13 22:16:44 -07004170 struct worker_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171
Tejun Heo8db25e72012-07-17 12:39:28 -07004172 switch (action & ~CPU_TASKS_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 case CPU_UP_PREPARE:
Tejun Heof02ae732013-03-12 11:30:03 -07004174 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo3ce63372012-07-17 12:39:27 -07004175 if (pool->nr_workers)
4176 continue;
Tejun Heoebf44d12013-03-13 19:47:39 -07004177 if (create_and_start_worker(pool) < 0)
Tejun Heo3ce63372012-07-17 12:39:27 -07004178 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02004180 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07004181
Tejun Heo65758202012-07-17 12:39:26 -07004182 case CPU_DOWN_FAILED:
4183 case CPU_ONLINE:
Tejun Heof02ae732013-03-12 11:30:03 -07004184 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heobc3a1af2013-03-13 19:47:39 -07004185 mutex_lock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004186 spin_lock_irq(&pool->lock);
4187
Tejun Heo24647572013-01-24 11:01:33 -08004188 pool->flags &= ~POOL_DISASSOCIATED;
Tejun Heo94cf58b2013-01-24 11:01:33 -08004189 rebind_workers(pool);
4190
4191 spin_unlock_irq(&pool->lock);
Tejun Heobc3a1af2013-03-13 19:47:39 -07004192 mutex_unlock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004193 }
Tejun Heo8db25e72012-07-17 12:39:28 -07004194 break;
Tejun Heo65758202012-07-17 12:39:26 -07004195 }
4196 return NOTIFY_OK;
4197}
4198
4199/*
4200 * Workqueues should be brought down after normal priority CPU notifiers.
4201 * This will be registered as low priority CPU notifier.
4202 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07004203static int __cpuinit workqueue_cpu_down_callback(struct notifier_block *nfb,
Tejun Heo65758202012-07-17 12:39:26 -07004204 unsigned long action,
4205 void *hcpu)
4206{
Tejun Heod84ff052013-03-12 11:29:59 -07004207 int cpu = (unsigned long)hcpu;
Tejun Heo8db25e72012-07-17 12:39:28 -07004208 struct work_struct unbind_work;
4209
Tejun Heo65758202012-07-17 12:39:26 -07004210 switch (action & ~CPU_TASKS_FROZEN) {
4211 case CPU_DOWN_PREPARE:
Tejun Heo8db25e72012-07-17 12:39:28 -07004212 /* unbinding should happen on the local CPU */
Tejun Heo706026c2013-01-24 11:01:34 -08004213 INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn);
Joonsoo Kim7635d2f2012-08-15 23:25:41 +09004214 queue_work_on(cpu, system_highpri_wq, &unbind_work);
Tejun Heo8db25e72012-07-17 12:39:28 -07004215 flush_work(&unbind_work);
4216 break;
Tejun Heo65758202012-07-17 12:39:26 -07004217 }
4218 return NOTIFY_OK;
4219}
4220
Rusty Russell2d3854a2008-11-05 13:39:10 +11004221#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08004222
Rusty Russell2d3854a2008-11-05 13:39:10 +11004223struct work_for_cpu {
Tejun Heoed48ece2012-09-18 12:48:43 -07004224 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11004225 long (*fn)(void *);
4226 void *arg;
4227 long ret;
4228};
4229
Tejun Heoed48ece2012-09-18 12:48:43 -07004230static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11004231{
Tejun Heoed48ece2012-09-18 12:48:43 -07004232 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
4233
Rusty Russell2d3854a2008-11-05 13:39:10 +11004234 wfc->ret = wfc->fn(wfc->arg);
4235}
4236
4237/**
4238 * work_on_cpu - run a function in user context on a particular cpu
4239 * @cpu: the cpu to run on
4240 * @fn: the function to run
4241 * @arg: the function arg
4242 *
Rusty Russell31ad9082009-01-16 15:31:15 -08004243 * This will return the value @fn returns.
4244 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06004245 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11004246 */
Tejun Heod84ff052013-03-12 11:29:59 -07004247long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
Rusty Russell2d3854a2008-11-05 13:39:10 +11004248{
Tejun Heoed48ece2012-09-18 12:48:43 -07004249 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11004250
Tejun Heoed48ece2012-09-18 12:48:43 -07004251 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
4252 schedule_work_on(cpu, &wfc.work);
4253 flush_work(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11004254 return wfc.ret;
4255}
4256EXPORT_SYMBOL_GPL(work_on_cpu);
4257#endif /* CONFIG_SMP */
4258
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004259#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10304260
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004261/**
4262 * freeze_workqueues_begin - begin freezing workqueues
4263 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01004264 * Start freezing workqueues. After this function returns, all freezable
Tejun Heoc5aa87b2013-03-13 16:51:36 -07004265 * workqueues will queue new works to their delayed_works list instead of
Tejun Heo706026c2013-01-24 11:01:34 -08004266 * pool->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004267 *
4268 * CONTEXT:
Tejun Heo5bcab332013-03-13 19:47:40 -07004269 * Grabs and releases wq_mutex, workqueue_lock and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004270 */
4271void freeze_workqueues_begin(void)
4272{
Tejun Heo17116962013-03-12 11:29:58 -07004273 struct worker_pool *pool;
Tejun Heo24b8a842013-03-12 11:29:58 -07004274 struct workqueue_struct *wq;
4275 struct pool_workqueue *pwq;
Tejun Heo611c92a2013-03-13 16:51:36 -07004276 int pi;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004277
Tejun Heo5bcab332013-03-13 19:47:40 -07004278 mutex_lock(&wq_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004279
Tejun Heo6183c002013-03-12 11:29:57 -07004280 WARN_ON_ONCE(workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004281 workqueue_freezing = true;
4282
Tejun Heo24b8a842013-03-12 11:29:58 -07004283 /* set FREEZING */
Tejun Heo611c92a2013-03-13 16:51:36 -07004284 for_each_pool(pool, pi) {
Tejun Heo5bcab332013-03-13 19:47:40 -07004285 spin_lock_irq(&pool->lock);
Tejun Heo17116962013-03-12 11:29:58 -07004286 WARN_ON_ONCE(pool->flags & POOL_FREEZING);
4287 pool->flags |= POOL_FREEZING;
Tejun Heo5bcab332013-03-13 19:47:40 -07004288 spin_unlock_irq(&pool->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004289 }
4290
Tejun Heo24b8a842013-03-12 11:29:58 -07004291 /* suppress further executions by setting max_active to zero */
Tejun Heo5bcab332013-03-13 19:47:40 -07004292 spin_lock_irq(&workqueue_lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004293 list_for_each_entry(wq, &workqueues, list) {
Tejun Heo699ce092013-03-13 16:51:35 -07004294 for_each_pwq(pwq, wq)
4295 pwq_adjust_max_active(pwq);
Tejun Heo24b8a842013-03-12 11:29:58 -07004296 }
Tejun Heoe98d5b12013-03-12 11:29:57 -07004297 spin_unlock_irq(&workqueue_lock);
Tejun Heo5bcab332013-03-13 19:47:40 -07004298
4299 mutex_unlock(&wq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004301
4302/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01004303 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004304 *
4305 * Check whether freezing is complete. This function must be called
4306 * between freeze_workqueues_begin() and thaw_workqueues().
4307 *
4308 * CONTEXT:
Tejun Heo5bcab332013-03-13 19:47:40 -07004309 * Grabs and releases wq_mutex.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004310 *
4311 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01004312 * %true if some freezable workqueues are still busy. %false if freezing
4313 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004314 */
4315bool freeze_workqueues_busy(void)
4316{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004317 bool busy = false;
Tejun Heo24b8a842013-03-12 11:29:58 -07004318 struct workqueue_struct *wq;
4319 struct pool_workqueue *pwq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004320
Tejun Heo5bcab332013-03-13 19:47:40 -07004321 mutex_lock(&wq_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004322
Tejun Heo6183c002013-03-12 11:29:57 -07004323 WARN_ON_ONCE(!workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004324
Tejun Heo24b8a842013-03-12 11:29:58 -07004325 list_for_each_entry(wq, &workqueues, list) {
4326 if (!(wq->flags & WQ_FREEZABLE))
4327 continue;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004328 /*
4329 * nr_active is monotonically decreasing. It's safe
4330 * to peek without lock.
4331 */
Tejun Heo5bcab332013-03-13 19:47:40 -07004332 preempt_disable();
Tejun Heo24b8a842013-03-12 11:29:58 -07004333 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07004334 WARN_ON_ONCE(pwq->nr_active < 0);
Tejun Heo112202d2013-02-13 19:29:12 -08004335 if (pwq->nr_active) {
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004336 busy = true;
Tejun Heo5bcab332013-03-13 19:47:40 -07004337 preempt_enable();
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004338 goto out_unlock;
4339 }
4340 }
Tejun Heo5bcab332013-03-13 19:47:40 -07004341 preempt_enable();
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004342 }
4343out_unlock:
Tejun Heo5bcab332013-03-13 19:47:40 -07004344 mutex_unlock(&wq_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004345 return busy;
4346}
4347
4348/**
4349 * thaw_workqueues - thaw workqueues
4350 *
4351 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo706026c2013-01-24 11:01:34 -08004352 * frozen works are transferred to their respective pool worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004353 *
4354 * CONTEXT:
Tejun Heo5bcab332013-03-13 19:47:40 -07004355 * Grabs and releases wq_mutex, workqueue_lock and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004356 */
4357void thaw_workqueues(void)
4358{
Tejun Heo24b8a842013-03-12 11:29:58 -07004359 struct workqueue_struct *wq;
4360 struct pool_workqueue *pwq;
4361 struct worker_pool *pool;
Tejun Heo611c92a2013-03-13 16:51:36 -07004362 int pi;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004363
Tejun Heo5bcab332013-03-13 19:47:40 -07004364 mutex_lock(&wq_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004365
4366 if (!workqueue_freezing)
4367 goto out_unlock;
4368
Tejun Heo24b8a842013-03-12 11:29:58 -07004369 /* clear FREEZING */
Tejun Heo611c92a2013-03-13 16:51:36 -07004370 for_each_pool(pool, pi) {
Tejun Heo5bcab332013-03-13 19:47:40 -07004371 spin_lock_irq(&pool->lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004372 WARN_ON_ONCE(!(pool->flags & POOL_FREEZING));
4373 pool->flags &= ~POOL_FREEZING;
Tejun Heo5bcab332013-03-13 19:47:40 -07004374 spin_unlock_irq(&pool->lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004375 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02004376
Tejun Heo24b8a842013-03-12 11:29:58 -07004377 /* restore max_active and repopulate worklist */
Tejun Heo5bcab332013-03-13 19:47:40 -07004378 spin_lock_irq(&workqueue_lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004379 list_for_each_entry(wq, &workqueues, list) {
Tejun Heo699ce092013-03-13 16:51:35 -07004380 for_each_pwq(pwq, wq)
4381 pwq_adjust_max_active(pwq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004382 }
Tejun Heo5bcab332013-03-13 19:47:40 -07004383 spin_unlock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004384
Tejun Heo24b8a842013-03-12 11:29:58 -07004385 /* kick workers */
Tejun Heo611c92a2013-03-13 16:51:36 -07004386 for_each_pool(pool, pi) {
Tejun Heo5bcab332013-03-13 19:47:40 -07004387 spin_lock_irq(&pool->lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004388 wake_up_worker(pool);
Tejun Heo5bcab332013-03-13 19:47:40 -07004389 spin_unlock_irq(&pool->lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004390 }
4391
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004392 workqueue_freezing = false;
4393out_unlock:
Tejun Heo5bcab332013-03-13 19:47:40 -07004394 mutex_unlock(&wq_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004395}
4396#endif /* CONFIG_FREEZER */
4397
Suresh Siddha6ee05782010-07-30 14:57:37 -07004398static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399{
Tejun Heo7a4e3442013-03-12 11:30:00 -07004400 int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL };
4401 int i, cpu;
Tejun Heoc34056a2010-06-29 10:07:11 +02004402
Tejun Heo7c3eed52013-01-24 11:01:33 -08004403 /* make sure we have enough bits for OFFQ pool ID */
4404 BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) <
Lai Jiangshan6be19582013-02-06 18:04:53 -08004405 WORK_CPU_END * NR_STD_WORKER_POOLS);
Tejun Heob5490072012-08-03 10:30:46 -07004406
Tejun Heoe904e6c2013-03-12 11:29:57 -07004407 WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long));
4408
4409 pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC);
4410
Tejun Heo65758202012-07-17 12:39:26 -07004411 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
Lai Jiangshana5b4e572012-09-18 09:59:23 -07004412 hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02004413
Tejun Heo706026c2013-01-24 11:01:34 -08004414 /* initialize CPU pools */
Tejun Heo29c91e92013-03-12 11:30:03 -07004415 for_each_possible_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07004416 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02004417
Tejun Heo7a4e3442013-03-12 11:30:00 -07004418 i = 0;
Tejun Heof02ae732013-03-12 11:30:03 -07004419 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo7a4e3442013-03-12 11:30:00 -07004420 BUG_ON(init_worker_pool(pool));
Tejun Heoec22ca52013-01-24 11:01:33 -08004421 pool->cpu = cpu;
Tejun Heo29c91e92013-03-12 11:30:03 -07004422 cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu));
Tejun Heo7a4e3442013-03-12 11:30:00 -07004423 pool->attrs->nice = std_nice[i++];
4424
Tejun Heo9daf9e62013-01-24 11:01:33 -08004425 /* alloc pool ID */
Tejun Heo5bcab332013-03-13 19:47:40 -07004426 mutex_lock(&wq_mutex);
Tejun Heo9daf9e62013-01-24 11:01:33 -08004427 BUG_ON(worker_pool_assign_id(pool));
Tejun Heo5bcab332013-03-13 19:47:40 -07004428 mutex_unlock(&wq_mutex);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004429 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02004430 }
4431
Tejun Heoe22bee72010-06-29 10:07:14 +02004432 /* create the initial worker */
Tejun Heo29c91e92013-03-12 11:30:03 -07004433 for_each_online_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07004434 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02004435
Tejun Heof02ae732013-03-12 11:30:03 -07004436 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo29c91e92013-03-12 11:30:03 -07004437 pool->flags &= ~POOL_DISASSOCIATED;
Tejun Heoebf44d12013-03-13 19:47:39 -07004438 BUG_ON(create_and_start_worker(pool) < 0);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004439 }
Tejun Heoe22bee72010-06-29 10:07:14 +02004440 }
4441
Tejun Heo29c91e92013-03-12 11:30:03 -07004442 /* create default unbound wq attrs */
4443 for (i = 0; i < NR_STD_WORKER_POOLS; i++) {
4444 struct workqueue_attrs *attrs;
4445
4446 BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL)));
4447
4448 attrs->nice = std_nice[i];
4449 cpumask_setall(attrs->cpumask);
4450
4451 unbound_std_wq_attrs[i] = attrs;
4452 }
4453
Tejun Heod320c032010-06-29 10:07:14 +02004454 system_wq = alloc_workqueue("events", 0, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09004455 system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
Tejun Heod320c032010-06-29 10:07:14 +02004456 system_long_wq = alloc_workqueue("events_long", 0, 0);
Tejun Heof3421792010-07-02 10:03:51 +02004457 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
4458 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01004459 system_freezable_wq = alloc_workqueue("events_freezable",
4460 WQ_FREEZABLE, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09004461 BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
Tejun Heoae930e02012-08-20 14:51:23 -07004462 !system_unbound_wq || !system_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07004463 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464}
Suresh Siddha6ee05782010-07-30 14:57:37 -07004465early_initcall(init_workqueues);