blob: 3e297c574be86bc32c0bd34fd6526c51a426db1c [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 Heo822d8402013-03-19 13:45:21 -0700122 * MG: pool->manager_mutex and pool->lock protected. Writes require both
123 * locks. Reads can happen under either lock.
124 *
Tejun Heo5bcab332013-03-13 19:47:40 -0700125 * WQ: wq_mutex protected.
Tejun Heo76af4d92013-03-12 11:30:00 -0700126 *
Tejun Heo5bcab332013-03-13 19:47:40 -0700127 * WR: wq_mutex protected for writes. Sched-RCU protected for reads.
128 *
Tejun Heo794b18b2013-03-13 19:47:40 -0700129 * PW: pwq_lock protected.
130 *
Tejun Heo794b18b2013-03-13 19:47:40 -0700131 * FR: wq->flush_mutex and pwq_lock protected for writes. Sched-RCU
Tejun Heo75ccf592013-03-12 11:30:04 -0700132 * protected for reads.
Tejun Heo2e109a22013-03-13 19:47:40 -0700133 *
134 * MD: wq_mayday_lock protected.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200135 */
136
Tejun Heo2eaebdb2013-01-18 14:05:55 -0800137/* struct worker is defined in workqueue_internal.h */
Tejun Heoc34056a2010-06-29 10:07:11 +0200138
Tejun Heobd7bdd42012-07-12 14:46:37 -0700139struct worker_pool {
Tejun Heod565ed62013-01-24 11:01:33 -0800140 spinlock_t lock; /* the pool lock */
Tejun Heod84ff052013-03-12 11:29:59 -0700141 int cpu; /* I: the associated cpu */
Tejun Heo9daf9e62013-01-24 11:01:33 -0800142 int id; /* I: pool ID */
Tejun Heo11ebea52012-07-12 14:46:37 -0700143 unsigned int flags; /* X: flags */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700144
145 struct list_head worklist; /* L: list of pending works */
146 int nr_workers; /* L: total number of workers */
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700147
148 /* nr_idle includes the ones off idle_list for rebinding */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700149 int nr_idle; /* L: currently idle ones */
150
151 struct list_head idle_list; /* X: list of idle workers */
152 struct timer_list idle_timer; /* L: worker idle timeout */
153 struct timer_list mayday_timer; /* L: SOS timer for workers */
154
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700155 /* a workers is either on busy_hash or idle_list, or the manager */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800156 DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER);
157 /* L: hash of busy workers */
158
Tejun Heobc3a1af2013-03-13 19:47:39 -0700159 /* see manage_workers() for details on the two manager mutexes */
Tejun Heo34a06bd2013-03-12 11:30:00 -0700160 struct mutex manager_arb; /* manager arbitration */
Tejun Heobc3a1af2013-03-13 19:47:39 -0700161 struct mutex manager_mutex; /* manager exclusion */
Tejun Heo822d8402013-03-19 13:45:21 -0700162 struct idr worker_idr; /* MG: worker IDs and iteration */
Tejun Heoe19e3972013-01-24 11:39:44 -0800163
Tejun Heo7a4e3442013-03-12 11:30:00 -0700164 struct workqueue_attrs *attrs; /* I: worker attributes */
Tejun Heo5bcab332013-03-13 19:47:40 -0700165 struct hlist_node hash_node; /* WQ: unbound_pool_hash node */
166 int refcnt; /* WQ: refcnt for unbound pools */
Tejun Heo7a4e3442013-03-12 11:30:00 -0700167
Tejun Heoe19e3972013-01-24 11:39:44 -0800168 /*
169 * The current concurrency level. As it's likely to be accessed
170 * from other CPUs during try_to_wake_up(), put it in a separate
171 * cacheline.
172 */
173 atomic_t nr_running ____cacheline_aligned_in_smp;
Tejun Heo29c91e92013-03-12 11:30:03 -0700174
175 /*
176 * Destruction of pool is sched-RCU protected to allow dereferences
177 * from get_work_pool().
178 */
179 struct rcu_head rcu;
Tejun Heo8b03ae32010-06-29 10:07:12 +0200180} ____cacheline_aligned_in_smp;
181
182/*
Tejun Heo112202d2013-02-13 19:29:12 -0800183 * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS
184 * of work_struct->data are used for flags and the remaining high bits
185 * point to the pwq; thus, pwqs need to be aligned at two's power of the
186 * number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 */
Tejun Heo112202d2013-02-13 19:29:12 -0800188struct pool_workqueue {
Tejun Heobd7bdd42012-07-12 14:46:37 -0700189 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200190 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200191 int work_color; /* L: current color */
192 int flush_color; /* L: flushing color */
Tejun Heo8864b4e2013-03-12 11:30:04 -0700193 int refcnt; /* L: reference count */
Tejun Heo73f53c42010-06-29 10:07:11 +0200194 int nr_in_flight[WORK_NR_COLORS];
195 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200196 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200197 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200198 struct list_head delayed_works; /* L: delayed works */
Tejun Heo75ccf592013-03-12 11:30:04 -0700199 struct list_head pwqs_node; /* FR: node on wq->pwqs */
Tejun Heo2e109a22013-03-13 19:47:40 -0700200 struct list_head mayday_node; /* MD: node on wq->maydays */
Tejun Heo8864b4e2013-03-12 11:30:04 -0700201
202 /*
203 * Release of unbound pwq is punted to system_wq. See put_pwq()
204 * and pwq_unbound_release_workfn() for details. pool_workqueue
205 * itself is also sched-RCU protected so that the first pwq can be
Tejun Heo794b18b2013-03-13 19:47:40 -0700206 * determined without grabbing pwq_lock.
Tejun Heo8864b4e2013-03-12 11:30:04 -0700207 */
208 struct work_struct unbound_release_work;
209 struct rcu_head rcu;
Tejun Heoe904e6c2013-03-12 11:29:57 -0700210} __aligned(1 << WORK_STRUCT_FLAG_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200213 * Structure used to wait for workqueue flush.
214 */
215struct wq_flusher {
216 struct list_head list; /* F: list of flushers */
217 int flush_color; /* F: flush color waiting for */
218 struct completion done; /* flush completion */
219};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Tejun Heo226223a2013-03-12 11:30:05 -0700221struct wq_device;
222
Tejun Heo73f53c42010-06-29 10:07:11 +0200223/*
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700224 * The externally visible workqueue. It relays the issued work items to
225 * the appropriate worker_pool through its pool_workqueues.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 */
227struct workqueue_struct {
Tejun Heo5bcab332013-03-13 19:47:40 -0700228 unsigned int flags; /* WQ: WQ_* flags */
Tejun Heo420c0dd2013-03-12 11:29:59 -0700229 struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwq's */
Tejun Heo75ccf592013-03-12 11:30:04 -0700230 struct list_head pwqs; /* FR: all pwqs of this wq */
Tejun Heo5bcab332013-03-13 19:47:40 -0700231 struct list_head list; /* WQ: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200232
233 struct mutex flush_mutex; /* protects wq flushing */
234 int work_color; /* F: current work color */
235 int flush_color; /* F: current flush color */
Tejun Heo112202d2013-02-13 19:29:12 -0800236 atomic_t nr_pwqs_to_flush; /* flush in progress */
Tejun Heo73f53c42010-06-29 10:07:11 +0200237 struct wq_flusher *first_flusher; /* F: first flusher */
238 struct list_head flusher_queue; /* F: flush waiters */
239 struct list_head flusher_overflow; /* F: flush overflow list */
240
Tejun Heo2e109a22013-03-13 19:47:40 -0700241 struct list_head maydays; /* MD: pwqs requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200242 struct worker *rescuer; /* I: rescue worker */
243
Tejun Heo5bcab332013-03-13 19:47:40 -0700244 int nr_drainers; /* WQ: drain in progress */
Tejun Heo794b18b2013-03-13 19:47:40 -0700245 int saved_max_active; /* PW: saved pwq max_active */
Tejun Heo226223a2013-03-12 11:30:05 -0700246
247#ifdef CONFIG_SYSFS
248 struct wq_device *wq_dev; /* I: for sysfs interface */
249#endif
Johannes Berg4e6045f2007-10-18 23:39:55 -0700250#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200251 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700252#endif
Tejun Heob196be82012-01-10 15:11:35 -0800253 char name[]; /* I: workqueue name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254};
255
Tejun Heoe904e6c2013-03-12 11:29:57 -0700256static struct kmem_cache *pwq_cache;
257
Tejun Heo5bcab332013-03-13 19:47:40 -0700258static DEFINE_MUTEX(wq_mutex); /* protects workqueues and pools */
Tejun Heo794b18b2013-03-13 19:47:40 -0700259static DEFINE_SPINLOCK(pwq_lock); /* protects pool_workqueues */
Tejun Heo2e109a22013-03-13 19:47:40 -0700260static DEFINE_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */
Tejun Heo5bcab332013-03-13 19:47:40 -0700261
262static LIST_HEAD(workqueues); /* WQ: list of all workqueues */
263static bool workqueue_freezing; /* WQ: have wqs started freezing? */
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700264
265/* the per-cpu worker pools */
266static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS],
267 cpu_worker_pools);
268
Tejun Heo5bcab332013-03-13 19:47:40 -0700269static DEFINE_IDR(worker_pool_idr); /* WR: idr of all pools */
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700270
Tejun Heo5bcab332013-03-13 19:47:40 -0700271/* WQ: hash of all unbound pools keyed by pool->attrs */
Tejun Heo29c91e92013-03-12 11:30:03 -0700272static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER);
273
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700274/* I: attributes used when instantiating standard unbound pools on demand */
Tejun Heo29c91e92013-03-12 11:30:03 -0700275static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS];
276
Tejun Heod320c032010-06-29 10:07:14 +0200277struct workqueue_struct *system_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200278EXPORT_SYMBOL_GPL(system_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300279struct workqueue_struct *system_highpri_wq __read_mostly;
Joonsoo Kim1aabe902012-08-15 23:25:39 +0900280EXPORT_SYMBOL_GPL(system_highpri_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300281struct workqueue_struct *system_long_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200282EXPORT_SYMBOL_GPL(system_long_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300283struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200284EXPORT_SYMBOL_GPL(system_unbound_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300285struct workqueue_struct *system_freezable_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100286EXPORT_SYMBOL_GPL(system_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200287
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700288static int worker_thread(void *__worker);
289static void copy_workqueue_attrs(struct workqueue_attrs *to,
290 const struct workqueue_attrs *from);
291
Tejun Heo97bd2342010-10-05 10:41:14 +0200292#define CREATE_TRACE_POINTS
293#include <trace/events/workqueue.h>
294
Tejun Heo5bcab332013-03-13 19:47:40 -0700295#define assert_rcu_or_wq_mutex() \
296 rcu_lockdep_assert(rcu_read_lock_sched_held() || \
297 lockdep_is_held(&wq_mutex), \
298 "sched RCU or wq_mutex should be held")
299
Tejun Heo794b18b2013-03-13 19:47:40 -0700300#define assert_rcu_or_pwq_lock() \
Tejun Heo76af4d92013-03-12 11:30:00 -0700301 rcu_lockdep_assert(rcu_read_lock_sched_held() || \
Tejun Heo794b18b2013-03-13 19:47:40 -0700302 lockdep_is_held(&pwq_lock), \
303 "sched RCU or pwq_lock should be held")
Tejun Heo76af4d92013-03-12 11:30:00 -0700304
Tejun Heo822d8402013-03-19 13:45:21 -0700305#ifdef CONFIG_LOCKDEP
306#define assert_manager_or_pool_lock(pool) \
307 WARN_ONCE(!lockdep_is_held(&(pool)->manager_mutex) && \
308 !lockdep_is_held(&(pool)->lock), \
309 "pool->manager_mutex or ->lock should be held")
310#else
311#define assert_manager_or_pool_lock(pool) do { } while (0)
312#endif
313
Tejun Heof02ae732013-03-12 11:30:03 -0700314#define for_each_cpu_worker_pool(pool, cpu) \
315 for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \
316 (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \
Tejun Heo7a62c2c2013-03-12 11:30:03 -0700317 (pool)++)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700318
Sasha Levinb67bfe02013-02-27 17:06:00 -0800319#define for_each_busy_worker(worker, i, pool) \
320 hash_for_each(pool->busy_hash, i, worker, hentry)
Tejun Heodb7bccf2010-06-29 10:07:12 +0200321
Tejun Heo49e3cf42013-03-12 11:29:58 -0700322/**
Tejun Heo17116962013-03-12 11:29:58 -0700323 * for_each_pool - iterate through all worker_pools in the system
324 * @pool: iteration cursor
Tejun Heo611c92a2013-03-13 16:51:36 -0700325 * @pi: integer used for iteration
Tejun Heofa1b54e2013-03-12 11:30:00 -0700326 *
Tejun Heo5bcab332013-03-13 19:47:40 -0700327 * This must be called either with wq_mutex held or sched RCU read locked.
328 * If the pool needs to be used beyond the locking in effect, the caller is
329 * responsible for guaranteeing that the pool stays online.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700330 *
331 * The if/else clause exists only for the lockdep assertion and can be
332 * ignored.
Tejun Heo17116962013-03-12 11:29:58 -0700333 */
Tejun Heo611c92a2013-03-13 16:51:36 -0700334#define for_each_pool(pool, pi) \
335 idr_for_each_entry(&worker_pool_idr, pool, pi) \
Tejun Heo5bcab332013-03-13 19:47:40 -0700336 if (({ assert_rcu_or_wq_mutex(); false; })) { } \
Tejun Heofa1b54e2013-03-12 11:30:00 -0700337 else
Tejun Heo17116962013-03-12 11:29:58 -0700338
339/**
Tejun Heo822d8402013-03-19 13:45:21 -0700340 * for_each_pool_worker - iterate through all workers of a worker_pool
341 * @worker: iteration cursor
342 * @wi: integer used for iteration
343 * @pool: worker_pool to iterate workers of
344 *
345 * This must be called with either @pool->manager_mutex or ->lock held.
346 *
347 * The if/else clause exists only for the lockdep assertion and can be
348 * ignored.
349 */
350#define for_each_pool_worker(worker, wi, pool) \
351 idr_for_each_entry(&(pool)->worker_idr, (worker), (wi)) \
352 if (({ assert_manager_or_pool_lock((pool)); false; })) { } \
353 else
354
355/**
Tejun Heo49e3cf42013-03-12 11:29:58 -0700356 * for_each_pwq - iterate through all pool_workqueues of the specified workqueue
357 * @pwq: iteration cursor
358 * @wq: the target workqueue
Tejun Heo76af4d92013-03-12 11:30:00 -0700359 *
Tejun Heo794b18b2013-03-13 19:47:40 -0700360 * This must be called either with pwq_lock held or sched RCU read locked.
361 * If the pwq needs to be used beyond the locking in effect, the caller is
362 * responsible for guaranteeing that the pwq stays online.
Tejun Heo76af4d92013-03-12 11:30:00 -0700363 *
364 * The if/else clause exists only for the lockdep assertion and can be
365 * ignored.
Tejun Heo49e3cf42013-03-12 11:29:58 -0700366 */
367#define for_each_pwq(pwq, wq) \
Tejun Heo76af4d92013-03-12 11:30:00 -0700368 list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node) \
Tejun Heo794b18b2013-03-13 19:47:40 -0700369 if (({ assert_rcu_or_pwq_lock(); false; })) { } \
Tejun Heo76af4d92013-03-12 11:30:00 -0700370 else
Tejun Heof3421792010-07-02 10:03:51 +0200371
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900372#ifdef CONFIG_DEBUG_OBJECTS_WORK
373
374static struct debug_obj_descr work_debug_descr;
375
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100376static void *work_debug_hint(void *addr)
377{
378 return ((struct work_struct *) addr)->func;
379}
380
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900381/*
382 * fixup_init is called when:
383 * - an active object is initialized
384 */
385static int work_fixup_init(void *addr, enum debug_obj_state state)
386{
387 struct work_struct *work = addr;
388
389 switch (state) {
390 case ODEBUG_STATE_ACTIVE:
391 cancel_work_sync(work);
392 debug_object_init(work, &work_debug_descr);
393 return 1;
394 default:
395 return 0;
396 }
397}
398
399/*
400 * fixup_activate is called when:
401 * - an active object is activated
402 * - an unknown object is activated (might be a statically initialized object)
403 */
404static int work_fixup_activate(void *addr, enum debug_obj_state state)
405{
406 struct work_struct *work = addr;
407
408 switch (state) {
409
410 case ODEBUG_STATE_NOTAVAILABLE:
411 /*
412 * This is not really a fixup. The work struct was
413 * statically initialized. We just make sure that it
414 * is tracked in the object tracker.
415 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200416 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900417 debug_object_init(work, &work_debug_descr);
418 debug_object_activate(work, &work_debug_descr);
419 return 0;
420 }
421 WARN_ON_ONCE(1);
422 return 0;
423
424 case ODEBUG_STATE_ACTIVE:
425 WARN_ON(1);
426
427 default:
428 return 0;
429 }
430}
431
432/*
433 * fixup_free is called when:
434 * - an active object is freed
435 */
436static int work_fixup_free(void *addr, enum debug_obj_state state)
437{
438 struct work_struct *work = addr;
439
440 switch (state) {
441 case ODEBUG_STATE_ACTIVE:
442 cancel_work_sync(work);
443 debug_object_free(work, &work_debug_descr);
444 return 1;
445 default:
446 return 0;
447 }
448}
449
450static struct debug_obj_descr work_debug_descr = {
451 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100452 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900453 .fixup_init = work_fixup_init,
454 .fixup_activate = work_fixup_activate,
455 .fixup_free = work_fixup_free,
456};
457
458static inline void debug_work_activate(struct work_struct *work)
459{
460 debug_object_activate(work, &work_debug_descr);
461}
462
463static inline void debug_work_deactivate(struct work_struct *work)
464{
465 debug_object_deactivate(work, &work_debug_descr);
466}
467
468void __init_work(struct work_struct *work, int onstack)
469{
470 if (onstack)
471 debug_object_init_on_stack(work, &work_debug_descr);
472 else
473 debug_object_init(work, &work_debug_descr);
474}
475EXPORT_SYMBOL_GPL(__init_work);
476
477void destroy_work_on_stack(struct work_struct *work)
478{
479 debug_object_free(work, &work_debug_descr);
480}
481EXPORT_SYMBOL_GPL(destroy_work_on_stack);
482
483#else
484static inline void debug_work_activate(struct work_struct *work) { }
485static inline void debug_work_deactivate(struct work_struct *work) { }
486#endif
487
Tejun Heo9daf9e62013-01-24 11:01:33 -0800488/* allocate ID and assign it to @pool */
489static int worker_pool_assign_id(struct worker_pool *pool)
490{
491 int ret;
492
Tejun Heo5bcab332013-03-13 19:47:40 -0700493 lockdep_assert_held(&wq_mutex);
494
Tejun Heofa1b54e2013-03-12 11:30:00 -0700495 do {
496 if (!idr_pre_get(&worker_pool_idr, GFP_KERNEL))
497 return -ENOMEM;
Tejun Heofa1b54e2013-03-12 11:30:00 -0700498 ret = idr_get_new(&worker_pool_idr, pool, &pool->id);
Tejun Heofa1b54e2013-03-12 11:30:00 -0700499 } while (ret == -EAGAIN);
Tejun Heo9daf9e62013-01-24 11:01:33 -0800500
501 return ret;
502}
503
Tejun Heo76af4d92013-03-12 11:30:00 -0700504/**
505 * first_pwq - return the first pool_workqueue of the specified workqueue
506 * @wq: the target workqueue
507 *
Tejun Heo794b18b2013-03-13 19:47:40 -0700508 * This must be called either with pwq_lock held or sched RCU read locked.
509 * If the pwq needs to be used beyond the locking in effect, the caller is
510 * responsible for guaranteeing that the pwq stays online.
Tejun Heo76af4d92013-03-12 11:30:00 -0700511 */
Tejun Heo7fb98ea2013-03-12 11:30:00 -0700512static struct pool_workqueue *first_pwq(struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700513{
Tejun Heo794b18b2013-03-13 19:47:40 -0700514 assert_rcu_or_pwq_lock();
Tejun Heo76af4d92013-03-12 11:30:00 -0700515 return list_first_or_null_rcu(&wq->pwqs, struct pool_workqueue,
516 pwqs_node);
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700517}
518
Tejun Heo73f53c42010-06-29 10:07:11 +0200519static unsigned int work_color_to_flags(int color)
520{
521 return color << WORK_STRUCT_COLOR_SHIFT;
522}
523
524static int get_work_color(struct work_struct *work)
525{
526 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
527 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
528}
529
530static int work_next_color(int color)
531{
532 return (color + 1) % WORK_NR_COLORS;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700533}
534
David Howells4594bf12006-12-07 11:33:26 +0000535/*
Tejun Heo112202d2013-02-13 19:29:12 -0800536 * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data
537 * contain the pointer to the queued pwq. Once execution starts, the flag
Tejun Heo7c3eed52013-01-24 11:01:33 -0800538 * is cleared and the high bits contain OFFQ flags and pool ID.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200539 *
Tejun Heo112202d2013-02-13 19:29:12 -0800540 * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling()
541 * and clear_work_data() can be used to set the pwq, pool or clear
Tejun Heobbb68df2012-08-03 10:30:46 -0700542 * work->data. These functions should only be called while the work is
543 * owned - ie. while the PENDING bit is set.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200544 *
Tejun Heo112202d2013-02-13 19:29:12 -0800545 * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq
Tejun Heo7c3eed52013-01-24 11:01:33 -0800546 * corresponding to a work. Pool is available once the work has been
Tejun Heo112202d2013-02-13 19:29:12 -0800547 * queued anywhere after initialization until it is sync canceled. pwq is
Tejun Heo7c3eed52013-01-24 11:01:33 -0800548 * available only while the work item is queued.
Tejun Heobbb68df2012-08-03 10:30:46 -0700549 *
550 * %WORK_OFFQ_CANCELING is used to mark a work item which is being
551 * canceled. While being canceled, a work item may have its PENDING set
552 * but stay off timer and worklist for arbitrarily long and nobody should
553 * try to steal the PENDING bit.
David Howells4594bf12006-12-07 11:33:26 +0000554 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200555static inline void set_work_data(struct work_struct *work, unsigned long data,
556 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000557{
Tejun Heo6183c002013-03-12 11:29:57 -0700558 WARN_ON_ONCE(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200559 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000560}
David Howells365970a2006-11-22 14:54:49 +0000561
Tejun Heo112202d2013-02-13 19:29:12 -0800562static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq,
Tejun Heo7a22ad72010-06-29 10:07:13 +0200563 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200564{
Tejun Heo112202d2013-02-13 19:29:12 -0800565 set_work_data(work, (unsigned long)pwq,
566 WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200567}
568
Lai Jiangshan4468a002013-02-06 18:04:53 -0800569static void set_work_pool_and_keep_pending(struct work_struct *work,
570 int pool_id)
571{
572 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT,
573 WORK_STRUCT_PENDING);
574}
575
Tejun Heo7c3eed52013-01-24 11:01:33 -0800576static void set_work_pool_and_clear_pending(struct work_struct *work,
577 int pool_id)
David Howells365970a2006-11-22 14:54:49 +0000578{
Tejun Heo23657bb2012-08-13 17:08:19 -0700579 /*
580 * The following wmb is paired with the implied mb in
581 * test_and_set_bit(PENDING) and ensures all updates to @work made
582 * here are visible to and precede any updates by the next PENDING
583 * owner.
584 */
585 smp_wmb();
Tejun Heo7c3eed52013-01-24 11:01:33 -0800586 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200587}
588
589static void clear_work_data(struct work_struct *work)
590{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800591 smp_wmb(); /* see set_work_pool_and_clear_pending() */
592 set_work_data(work, WORK_STRUCT_NO_POOL, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200593}
594
Tejun Heo112202d2013-02-13 19:29:12 -0800595static struct pool_workqueue *get_work_pwq(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200596{
Tejun Heoe1201532010-07-22 14:14:25 +0200597 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200598
Tejun Heo112202d2013-02-13 19:29:12 -0800599 if (data & WORK_STRUCT_PWQ)
Tejun Heoe1201532010-07-22 14:14:25 +0200600 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
601 else
602 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200603}
604
Tejun Heo7c3eed52013-01-24 11:01:33 -0800605/**
606 * get_work_pool - return the worker_pool a given work was associated with
607 * @work: the work item of interest
608 *
609 * Return the worker_pool @work was last associated with. %NULL if none.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700610 *
Tejun Heo5bcab332013-03-13 19:47:40 -0700611 * Pools are created and destroyed under wq_mutex, and allows read access
612 * under sched-RCU read lock. As such, this function should be called
613 * under wq_mutex or with preemption disabled.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700614 *
615 * All fields of the returned pool are accessible as long as the above
616 * mentioned locking is in effect. If the returned pool needs to be used
617 * beyond the critical section, the caller is responsible for ensuring the
618 * returned pool is and stays online.
Tejun Heo7c3eed52013-01-24 11:01:33 -0800619 */
620static struct worker_pool *get_work_pool(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200621{
Tejun Heoe1201532010-07-22 14:14:25 +0200622 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800623 int pool_id;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200624
Tejun Heo5bcab332013-03-13 19:47:40 -0700625 assert_rcu_or_wq_mutex();
Tejun Heofa1b54e2013-03-12 11:30:00 -0700626
Tejun Heo112202d2013-02-13 19:29:12 -0800627 if (data & WORK_STRUCT_PWQ)
628 return ((struct pool_workqueue *)
Tejun Heo7c3eed52013-01-24 11:01:33 -0800629 (data & WORK_STRUCT_WQ_DATA_MASK))->pool;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200630
Tejun Heo7c3eed52013-01-24 11:01:33 -0800631 pool_id = data >> WORK_OFFQ_POOL_SHIFT;
632 if (pool_id == WORK_OFFQ_POOL_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200633 return NULL;
634
Tejun Heofa1b54e2013-03-12 11:30:00 -0700635 return idr_find(&worker_pool_idr, pool_id);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800636}
637
638/**
639 * get_work_pool_id - return the worker pool ID a given work is associated with
640 * @work: the work item of interest
641 *
642 * Return the worker_pool ID @work was last associated with.
643 * %WORK_OFFQ_POOL_NONE if none.
644 */
645static int get_work_pool_id(struct work_struct *work)
646{
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800647 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800648
Tejun Heo112202d2013-02-13 19:29:12 -0800649 if (data & WORK_STRUCT_PWQ)
650 return ((struct pool_workqueue *)
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800651 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id;
652
653 return data >> WORK_OFFQ_POOL_SHIFT;
Tejun Heo7c3eed52013-01-24 11:01:33 -0800654}
655
Tejun Heobbb68df2012-08-03 10:30:46 -0700656static void mark_work_canceling(struct work_struct *work)
657{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800658 unsigned long pool_id = get_work_pool_id(work);
Tejun Heobbb68df2012-08-03 10:30:46 -0700659
Tejun Heo7c3eed52013-01-24 11:01:33 -0800660 pool_id <<= WORK_OFFQ_POOL_SHIFT;
661 set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700662}
663
664static bool work_is_canceling(struct work_struct *work)
665{
666 unsigned long data = atomic_long_read(&work->data);
667
Tejun Heo112202d2013-02-13 19:29:12 -0800668 return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700669}
670
David Howells365970a2006-11-22 14:54:49 +0000671/*
Tejun Heo32704762012-07-13 22:16:45 -0700672 * Policy functions. These define the policies on how the global worker
673 * pools are managed. Unless noted otherwise, these functions assume that
Tejun Heod565ed62013-01-24 11:01:33 -0800674 * they're being called with pool->lock held.
David Howells365970a2006-11-22 14:54:49 +0000675 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200676
Tejun Heo63d95a92012-07-12 14:46:37 -0700677static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000678{
Tejun Heoe19e3972013-01-24 11:39:44 -0800679 return !atomic_read(&pool->nr_running);
David Howells365970a2006-11-22 14:54:49 +0000680}
681
Tejun Heoe22bee72010-06-29 10:07:14 +0200682/*
683 * Need to wake up a worker? Called from anything but currently
684 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700685 *
686 * Note that, because unbound workers never contribute to nr_running, this
Tejun Heo706026c2013-01-24 11:01:34 -0800687 * function will always return %true for unbound pools as long as the
Tejun Heo974271c2012-07-12 14:46:37 -0700688 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200689 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700690static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000691{
Tejun Heo63d95a92012-07-12 14:46:37 -0700692 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000693}
694
Tejun Heoe22bee72010-06-29 10:07:14 +0200695/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700696static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200697{
Tejun Heo63d95a92012-07-12 14:46:37 -0700698 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200699}
700
701/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700702static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200703{
Tejun Heoe19e3972013-01-24 11:39:44 -0800704 return !list_empty(&pool->worklist) &&
705 atomic_read(&pool->nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200706}
707
708/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700709static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200710{
Tejun Heo63d95a92012-07-12 14:46:37 -0700711 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200712}
713
714/* Do I need to be the manager? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700715static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200716{
Tejun Heo63d95a92012-07-12 14:46:37 -0700717 return need_to_create_worker(pool) ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700718 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200719}
720
721/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700722static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200723{
Tejun Heo34a06bd2013-03-12 11:30:00 -0700724 bool managing = mutex_is_locked(&pool->manager_arb);
Tejun Heo63d95a92012-07-12 14:46:37 -0700725 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
726 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200727
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700728 /*
729 * nr_idle and idle_list may disagree if idle rebinding is in
730 * progress. Never return %true if idle_list is empty.
731 */
732 if (list_empty(&pool->idle_list))
733 return false;
734
Tejun Heoe22bee72010-06-29 10:07:14 +0200735 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
736}
737
738/*
739 * Wake up functions.
740 */
741
Tejun Heo7e116292010-06-29 10:07:13 +0200742/* Return the first worker. Safe with preemption disabled */
Tejun Heo63d95a92012-07-12 14:46:37 -0700743static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200744{
Tejun Heo63d95a92012-07-12 14:46:37 -0700745 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200746 return NULL;
747
Tejun Heo63d95a92012-07-12 14:46:37 -0700748 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200749}
750
751/**
752 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700753 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200754 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700755 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200756 *
757 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800758 * spin_lock_irq(pool->lock).
Tejun Heo7e116292010-06-29 10:07:13 +0200759 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700760static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200761{
Tejun Heo63d95a92012-07-12 14:46:37 -0700762 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200763
764 if (likely(worker))
765 wake_up_process(worker->task);
766}
767
Tejun Heo4690c4a2010-06-29 10:07:10 +0200768/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200769 * wq_worker_waking_up - a worker is waking up
770 * @task: task waking up
771 * @cpu: CPU @task is waking up to
772 *
773 * This function is called during try_to_wake_up() when a worker is
774 * being awoken.
775 *
776 * CONTEXT:
777 * spin_lock_irq(rq->lock)
778 */
Tejun Heod84ff052013-03-12 11:29:59 -0700779void wq_worker_waking_up(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200780{
781 struct worker *worker = kthread_data(task);
782
Joonsoo Kim36576002012-10-26 23:03:49 +0900783 if (!(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoec22ca52013-01-24 11:01:33 -0800784 WARN_ON_ONCE(worker->pool->cpu != cpu);
Tejun Heoe19e3972013-01-24 11:39:44 -0800785 atomic_inc(&worker->pool->nr_running);
Joonsoo Kim36576002012-10-26 23:03:49 +0900786 }
Tejun Heoe22bee72010-06-29 10:07:14 +0200787}
788
789/**
790 * wq_worker_sleeping - a worker is going to sleep
791 * @task: task going to sleep
792 * @cpu: CPU in question, must be the current CPU number
793 *
794 * This function is called during schedule() when a busy worker is
795 * going to sleep. Worker on the same cpu can be woken up by
796 * returning pointer to its task.
797 *
798 * CONTEXT:
799 * spin_lock_irq(rq->lock)
800 *
801 * RETURNS:
802 * Worker task on @cpu to wake up, %NULL if none.
803 */
Tejun Heod84ff052013-03-12 11:29:59 -0700804struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200805{
806 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heo111c2252013-01-17 17:16:24 -0800807 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200808
Tejun Heo111c2252013-01-17 17:16:24 -0800809 /*
810 * Rescuers, which may not have all the fields set up like normal
811 * workers, also reach here, let's not access anything before
812 * checking NOT_RUNNING.
813 */
Steven Rostedt2d646722010-12-03 23:12:33 -0500814 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200815 return NULL;
816
Tejun Heo111c2252013-01-17 17:16:24 -0800817 pool = worker->pool;
Tejun Heo111c2252013-01-17 17:16:24 -0800818
Tejun Heoe22bee72010-06-29 10:07:14 +0200819 /* this can only happen on the local cpu */
Tejun Heo6183c002013-03-12 11:29:57 -0700820 if (WARN_ON_ONCE(cpu != raw_smp_processor_id()))
821 return NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +0200822
823 /*
824 * The counterpart of the following dec_and_test, implied mb,
825 * worklist not empty test sequence is in insert_work().
826 * Please read comment there.
827 *
Tejun Heo628c78e2012-07-17 12:39:27 -0700828 * NOT_RUNNING is clear. This means that we're bound to and
829 * running on the local cpu w/ rq lock held and preemption
830 * disabled, which in turn means that none else could be
Tejun Heod565ed62013-01-24 11:01:33 -0800831 * manipulating idle_list, so dereferencing idle_list without pool
Tejun Heo628c78e2012-07-17 12:39:27 -0700832 * lock is safe.
Tejun Heoe22bee72010-06-29 10:07:14 +0200833 */
Tejun Heoe19e3972013-01-24 11:39:44 -0800834 if (atomic_dec_and_test(&pool->nr_running) &&
835 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700836 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200837 return to_wakeup ? to_wakeup->task : NULL;
838}
839
840/**
841 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200842 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200843 * @flags: flags to set
844 * @wakeup: wakeup an idle worker if necessary
845 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200846 * Set @flags in @worker->flags and adjust nr_running accordingly. If
847 * nr_running becomes zero and @wakeup is %true, an idle worker is
848 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200849 *
Tejun Heocb444762010-07-02 10:03:50 +0200850 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800851 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200852 */
853static inline void worker_set_flags(struct worker *worker, unsigned int flags,
854 bool wakeup)
855{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700856 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200857
Tejun Heocb444762010-07-02 10:03:50 +0200858 WARN_ON_ONCE(worker->task != current);
859
Tejun Heoe22bee72010-06-29 10:07:14 +0200860 /*
861 * If transitioning into NOT_RUNNING, adjust nr_running and
862 * wake up an idle worker as necessary if requested by
863 * @wakeup.
864 */
865 if ((flags & WORKER_NOT_RUNNING) &&
866 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoe22bee72010-06-29 10:07:14 +0200867 if (wakeup) {
Tejun Heoe19e3972013-01-24 11:39:44 -0800868 if (atomic_dec_and_test(&pool->nr_running) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -0700869 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700870 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200871 } else
Tejun Heoe19e3972013-01-24 11:39:44 -0800872 atomic_dec(&pool->nr_running);
Tejun Heoe22bee72010-06-29 10:07:14 +0200873 }
874
Tejun Heod302f012010-06-29 10:07:13 +0200875 worker->flags |= flags;
876}
877
878/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200879 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200880 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200881 * @flags: flags to clear
882 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200883 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200884 *
Tejun Heocb444762010-07-02 10:03:50 +0200885 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800886 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200887 */
888static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
889{
Tejun Heo63d95a92012-07-12 14:46:37 -0700890 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200891 unsigned int oflags = worker->flags;
892
Tejun Heocb444762010-07-02 10:03:50 +0200893 WARN_ON_ONCE(worker->task != current);
894
Tejun Heod302f012010-06-29 10:07:13 +0200895 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200896
Tejun Heo42c025f2011-01-11 15:58:49 +0100897 /*
898 * If transitioning out of NOT_RUNNING, increment nr_running. Note
899 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
900 * of multiple flags, not a single flag.
901 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200902 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
903 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heoe19e3972013-01-24 11:39:44 -0800904 atomic_inc(&pool->nr_running);
Tejun Heod302f012010-06-29 10:07:13 +0200905}
906
907/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200908 * find_worker_executing_work - find worker which is executing a work
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800909 * @pool: pool of interest
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200910 * @work: work to find worker for
911 *
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800912 * Find a worker which is executing @work on @pool by searching
913 * @pool->busy_hash which is keyed by the address of @work. For a worker
Tejun Heoa2c1c572012-12-18 10:35:02 -0800914 * to match, its current execution should match the address of @work and
915 * its work function. This is to avoid unwanted dependency between
916 * unrelated work executions through a work item being recycled while still
917 * being executed.
918 *
919 * This is a bit tricky. A work item may be freed once its execution
920 * starts and nothing prevents the freed area from being recycled for
921 * another work item. If the same work item address ends up being reused
922 * before the original execution finishes, workqueue will identify the
923 * recycled work item as currently executing and make it wait until the
924 * current execution finishes, introducing an unwanted dependency.
925 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700926 * This function checks the work item address and work function to avoid
927 * false positives. Note that this isn't complete as one may construct a
928 * work function which can introduce dependency onto itself through a
929 * recycled work item. Well, if somebody wants to shoot oneself in the
930 * foot that badly, there's only so much we can do, and if such deadlock
931 * actually occurs, it should be easy to locate the culprit work function.
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200932 *
933 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800934 * spin_lock_irq(pool->lock).
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200935 *
936 * RETURNS:
937 * Pointer to worker which is executing @work if found, NULL
938 * otherwise.
939 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800940static struct worker *find_worker_executing_work(struct worker_pool *pool,
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200941 struct work_struct *work)
942{
Sasha Levin42f85702012-12-17 10:01:23 -0500943 struct worker *worker;
Sasha Levin42f85702012-12-17 10:01:23 -0500944
Sasha Levinb67bfe02013-02-27 17:06:00 -0800945 hash_for_each_possible(pool->busy_hash, worker, hentry,
Tejun Heoa2c1c572012-12-18 10:35:02 -0800946 (unsigned long)work)
947 if (worker->current_work == work &&
948 worker->current_func == work->func)
Sasha Levin42f85702012-12-17 10:01:23 -0500949 return worker;
950
951 return NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200952}
953
954/**
Tejun Heobf4ede02012-08-03 10:30:46 -0700955 * move_linked_works - move linked works to a list
956 * @work: start of series of works to be scheduled
957 * @head: target list to append @work to
958 * @nextp: out paramter for nested worklist walking
959 *
960 * Schedule linked works starting from @work to @head. Work series to
961 * be scheduled starts at @work and includes any consecutive work with
962 * WORK_STRUCT_LINKED set in its predecessor.
963 *
964 * If @nextp is not NULL, it's updated to point to the next work of
965 * the last scheduled work. This allows move_linked_works() to be
966 * nested inside outer list_for_each_entry_safe().
967 *
968 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800969 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -0700970 */
971static void move_linked_works(struct work_struct *work, struct list_head *head,
972 struct work_struct **nextp)
973{
974 struct work_struct *n;
975
976 /*
977 * Linked worklist will always end before the end of the list,
978 * use NULL for list head.
979 */
980 list_for_each_entry_safe_from(work, n, NULL, entry) {
981 list_move_tail(&work->entry, head);
982 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
983 break;
984 }
985
986 /*
987 * If we're already inside safe list traversal and have moved
988 * multiple works to the scheduled queue, the next position
989 * needs to be updated.
990 */
991 if (nextp)
992 *nextp = n;
993}
994
Tejun Heo8864b4e2013-03-12 11:30:04 -0700995/**
996 * get_pwq - get an extra reference on the specified pool_workqueue
997 * @pwq: pool_workqueue to get
998 *
999 * Obtain an extra reference on @pwq. The caller should guarantee that
1000 * @pwq has positive refcnt and be holding the matching pool->lock.
1001 */
1002static void get_pwq(struct pool_workqueue *pwq)
1003{
1004 lockdep_assert_held(&pwq->pool->lock);
1005 WARN_ON_ONCE(pwq->refcnt <= 0);
1006 pwq->refcnt++;
1007}
1008
1009/**
1010 * put_pwq - put a pool_workqueue reference
1011 * @pwq: pool_workqueue to put
1012 *
1013 * Drop a reference of @pwq. If its refcnt reaches zero, schedule its
1014 * destruction. The caller should be holding the matching pool->lock.
1015 */
1016static void put_pwq(struct pool_workqueue *pwq)
1017{
1018 lockdep_assert_held(&pwq->pool->lock);
1019 if (likely(--pwq->refcnt))
1020 return;
1021 if (WARN_ON_ONCE(!(pwq->wq->flags & WQ_UNBOUND)))
1022 return;
1023 /*
1024 * @pwq can't be released under pool->lock, bounce to
1025 * pwq_unbound_release_workfn(). This never recurses on the same
1026 * pool->lock as this path is taken only for unbound workqueues and
1027 * the release work item is scheduled on a per-cpu workqueue. To
1028 * avoid lockdep warning, unbound pool->locks are given lockdep
1029 * subclass of 1 in get_unbound_pool().
1030 */
1031 schedule_work(&pwq->unbound_release_work);
1032}
1033
Tejun Heo112202d2013-02-13 19:29:12 -08001034static void pwq_activate_delayed_work(struct work_struct *work)
Tejun Heobf4ede02012-08-03 10:30:46 -07001035{
Tejun Heo112202d2013-02-13 19:29:12 -08001036 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobf4ede02012-08-03 10:30:46 -07001037
1038 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001039 move_linked_works(work, &pwq->pool->worklist, NULL);
Tejun Heobf4ede02012-08-03 10:30:46 -07001040 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo112202d2013-02-13 19:29:12 -08001041 pwq->nr_active++;
Tejun Heobf4ede02012-08-03 10:30:46 -07001042}
1043
Tejun Heo112202d2013-02-13 19:29:12 -08001044static void pwq_activate_first_delayed(struct pool_workqueue *pwq)
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001045{
Tejun Heo112202d2013-02-13 19:29:12 -08001046 struct work_struct *work = list_first_entry(&pwq->delayed_works,
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001047 struct work_struct, entry);
1048
Tejun Heo112202d2013-02-13 19:29:12 -08001049 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001050}
1051
Tejun Heobf4ede02012-08-03 10:30:46 -07001052/**
Tejun Heo112202d2013-02-13 19:29:12 -08001053 * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight
1054 * @pwq: pwq of interest
Tejun Heobf4ede02012-08-03 10:30:46 -07001055 * @color: color of work which left the queue
Tejun Heobf4ede02012-08-03 10:30:46 -07001056 *
1057 * A work either has completed or is removed from pending queue,
Tejun Heo112202d2013-02-13 19:29:12 -08001058 * decrement nr_in_flight of its pwq and handle workqueue flushing.
Tejun Heobf4ede02012-08-03 10:30:46 -07001059 *
1060 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001061 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -07001062 */
Tejun Heo112202d2013-02-13 19:29:12 -08001063static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color)
Tejun Heobf4ede02012-08-03 10:30:46 -07001064{
Tejun Heo8864b4e2013-03-12 11:30:04 -07001065 /* uncolored work items don't participate in flushing or nr_active */
Tejun Heobf4ede02012-08-03 10:30:46 -07001066 if (color == WORK_NO_COLOR)
Tejun Heo8864b4e2013-03-12 11:30:04 -07001067 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001068
Tejun Heo112202d2013-02-13 19:29:12 -08001069 pwq->nr_in_flight[color]--;
Tejun Heobf4ede02012-08-03 10:30:46 -07001070
Tejun Heo112202d2013-02-13 19:29:12 -08001071 pwq->nr_active--;
1072 if (!list_empty(&pwq->delayed_works)) {
Lai Jiangshanb3f9f402012-09-18 10:40:00 -07001073 /* one down, submit a delayed one */
Tejun Heo112202d2013-02-13 19:29:12 -08001074 if (pwq->nr_active < pwq->max_active)
1075 pwq_activate_first_delayed(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001076 }
1077
1078 /* is flush in progress and are we at the flushing tip? */
Tejun Heo112202d2013-02-13 19:29:12 -08001079 if (likely(pwq->flush_color != color))
Tejun Heo8864b4e2013-03-12 11:30:04 -07001080 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001081
1082 /* are there still in-flight works? */
Tejun Heo112202d2013-02-13 19:29:12 -08001083 if (pwq->nr_in_flight[color])
Tejun Heo8864b4e2013-03-12 11:30:04 -07001084 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001085
Tejun Heo112202d2013-02-13 19:29:12 -08001086 /* this pwq is done, clear flush_color */
1087 pwq->flush_color = -1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001088
1089 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001090 * If this was the last pwq, wake up the first flusher. It
Tejun Heobf4ede02012-08-03 10:30:46 -07001091 * will handle the rest.
1092 */
Tejun Heo112202d2013-02-13 19:29:12 -08001093 if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush))
1094 complete(&pwq->wq->first_flusher->done);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001095out_put:
1096 put_pwq(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001097}
1098
Tejun Heo36e227d2012-08-03 10:30:46 -07001099/**
Tejun Heobbb68df2012-08-03 10:30:46 -07001100 * try_to_grab_pending - steal work item from worklist and disable irq
Tejun Heo36e227d2012-08-03 10:30:46 -07001101 * @work: work item to steal
1102 * @is_dwork: @work is a delayed_work
Tejun Heobbb68df2012-08-03 10:30:46 -07001103 * @flags: place to store irq state
Tejun Heo36e227d2012-08-03 10:30:46 -07001104 *
1105 * Try to grab PENDING bit of @work. This function can handle @work in any
1106 * stable state - idle, on timer or on worklist. Return values are
1107 *
1108 * 1 if @work was pending and we successfully stole PENDING
1109 * 0 if @work was idle and we claimed PENDING
1110 * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry
Tejun Heobbb68df2012-08-03 10:30:46 -07001111 * -ENOENT if someone else is canceling @work, this state may persist
1112 * for arbitrarily long
Tejun Heo36e227d2012-08-03 10:30:46 -07001113 *
Tejun Heobbb68df2012-08-03 10:30:46 -07001114 * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001115 * interrupted while holding PENDING and @work off queue, irq must be
1116 * disabled on entry. This, combined with delayed_work->timer being
1117 * irqsafe, ensures that we return -EAGAIN for finite short period of time.
Tejun Heobbb68df2012-08-03 10:30:46 -07001118 *
1119 * On successful return, >= 0, irq is disabled and the caller is
1120 * responsible for releasing it using local_irq_restore(*@flags).
1121 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001122 * This function is safe to call from any context including IRQ handler.
Tejun Heobf4ede02012-08-03 10:30:46 -07001123 */
Tejun Heobbb68df2012-08-03 10:30:46 -07001124static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
1125 unsigned long *flags)
Tejun Heobf4ede02012-08-03 10:30:46 -07001126{
Tejun Heod565ed62013-01-24 11:01:33 -08001127 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08001128 struct pool_workqueue *pwq;
Tejun Heobf4ede02012-08-03 10:30:46 -07001129
Tejun Heobbb68df2012-08-03 10:30:46 -07001130 local_irq_save(*flags);
1131
Tejun Heo36e227d2012-08-03 10:30:46 -07001132 /* try to steal the timer if it exists */
1133 if (is_dwork) {
1134 struct delayed_work *dwork = to_delayed_work(work);
1135
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001136 /*
1137 * dwork->timer is irqsafe. If del_timer() fails, it's
1138 * guaranteed that the timer is not queued anywhere and not
1139 * running on the local CPU.
1140 */
Tejun Heo36e227d2012-08-03 10:30:46 -07001141 if (likely(del_timer(&dwork->timer)))
1142 return 1;
1143 }
1144
1145 /* try to claim PENDING the normal way */
Tejun Heobf4ede02012-08-03 10:30:46 -07001146 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1147 return 0;
1148
1149 /*
1150 * The queueing is in progress, or it is already queued. Try to
1151 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1152 */
Tejun Heod565ed62013-01-24 11:01:33 -08001153 pool = get_work_pool(work);
1154 if (!pool)
Tejun Heobbb68df2012-08-03 10:30:46 -07001155 goto fail;
Tejun Heobf4ede02012-08-03 10:30:46 -07001156
Tejun Heod565ed62013-01-24 11:01:33 -08001157 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001158 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001159 * work->data is guaranteed to point to pwq only while the work
1160 * item is queued on pwq->wq, and both updating work->data to point
1161 * to pwq on queueing and to pool on dequeueing are done under
1162 * pwq->pool->lock. This in turn guarantees that, if work->data
1163 * points to pwq which is associated with a locked pool, the work
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001164 * item is currently queued on that pool.
1165 */
Tejun Heo112202d2013-02-13 19:29:12 -08001166 pwq = get_work_pwq(work);
1167 if (pwq && pwq->pool == pool) {
Tejun Heo16062832013-02-06 18:04:53 -08001168 debug_work_deactivate(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001169
Tejun Heo16062832013-02-06 18:04:53 -08001170 /*
1171 * A delayed work item cannot be grabbed directly because
1172 * it might have linked NO_COLOR work items which, if left
Tejun Heo112202d2013-02-13 19:29:12 -08001173 * on the delayed_list, will confuse pwq->nr_active
Tejun Heo16062832013-02-06 18:04:53 -08001174 * management later on and cause stall. Make sure the work
1175 * item is activated before grabbing.
1176 */
1177 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
Tejun Heo112202d2013-02-13 19:29:12 -08001178 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001179
Tejun Heo16062832013-02-06 18:04:53 -08001180 list_del_init(&work->entry);
Tejun Heo112202d2013-02-13 19:29:12 -08001181 pwq_dec_nr_in_flight(get_work_pwq(work), get_work_color(work));
Tejun Heo36e227d2012-08-03 10:30:46 -07001182
Tejun Heo112202d2013-02-13 19:29:12 -08001183 /* work->data points to pwq iff queued, point to pool */
Tejun Heo16062832013-02-06 18:04:53 -08001184 set_work_pool_and_keep_pending(work, pool->id);
Lai Jiangshan4468a002013-02-06 18:04:53 -08001185
Tejun Heo16062832013-02-06 18:04:53 -08001186 spin_unlock(&pool->lock);
1187 return 1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001188 }
Tejun Heod565ed62013-01-24 11:01:33 -08001189 spin_unlock(&pool->lock);
Tejun Heobbb68df2012-08-03 10:30:46 -07001190fail:
1191 local_irq_restore(*flags);
1192 if (work_is_canceling(work))
1193 return -ENOENT;
1194 cpu_relax();
Tejun Heo36e227d2012-08-03 10:30:46 -07001195 return -EAGAIN;
Tejun Heobf4ede02012-08-03 10:30:46 -07001196}
1197
1198/**
Tejun Heo706026c2013-01-24 11:01:34 -08001199 * insert_work - insert a work into a pool
Tejun Heo112202d2013-02-13 19:29:12 -08001200 * @pwq: pwq @work belongs to
Tejun Heo4690c4a2010-06-29 10:07:10 +02001201 * @work: work to insert
1202 * @head: insertion point
1203 * @extra_flags: extra WORK_STRUCT_* flags to set
1204 *
Tejun Heo112202d2013-02-13 19:29:12 -08001205 * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to
Tejun Heo706026c2013-01-24 11:01:34 -08001206 * work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +02001207 *
1208 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001209 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02001210 */
Tejun Heo112202d2013-02-13 19:29:12 -08001211static void insert_work(struct pool_workqueue *pwq, struct work_struct *work,
1212 struct list_head *head, unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001213{
Tejun Heo112202d2013-02-13 19:29:12 -08001214 struct worker_pool *pool = pwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01001215
Tejun Heo4690c4a2010-06-29 10:07:10 +02001216 /* we own @work, set data and link */
Tejun Heo112202d2013-02-13 19:29:12 -08001217 set_work_pwq(work, pwq, extra_flags);
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -07001218 list_add_tail(&work->entry, head);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001219 get_pwq(pwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02001220
1221 /*
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001222 * Ensure either wq_worker_sleeping() sees the above
1223 * list_add_tail() or we see zero nr_running to avoid workers lying
1224 * around lazily while there are works to be processed.
Tejun Heoe22bee72010-06-29 10:07:14 +02001225 */
1226 smp_mb();
1227
Tejun Heo63d95a92012-07-12 14:46:37 -07001228 if (__need_more_worker(pool))
1229 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001230}
1231
Tejun Heoc8efcc22010-12-20 19:32:04 +01001232/*
1233 * Test whether @work is being queued from another work executing on the
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001234 * same workqueue.
Tejun Heoc8efcc22010-12-20 19:32:04 +01001235 */
1236static bool is_chained_work(struct workqueue_struct *wq)
1237{
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001238 struct worker *worker;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001239
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001240 worker = current_wq_worker();
1241 /*
1242 * Return %true iff I'm a worker execuing a work item on @wq. If
1243 * I'm @worker, it's safe to dereference it without locking.
1244 */
Tejun Heo112202d2013-02-13 19:29:12 -08001245 return worker && worker->current_pwq->wq == wq;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001246}
1247
Tejun Heod84ff052013-03-12 11:29:59 -07001248static void __queue_work(int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 struct work_struct *work)
1250{
Tejun Heo112202d2013-02-13 19:29:12 -08001251 struct pool_workqueue *pwq;
Tejun Heoc9178082013-03-12 11:30:04 -07001252 struct worker_pool *last_pool;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001253 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001254 unsigned int work_flags;
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001255 unsigned int req_cpu = cpu;
Tejun Heo8930cab2012-08-03 10:30:45 -07001256
1257 /*
1258 * While a work item is PENDING && off queue, a task trying to
1259 * steal the PENDING will busy-loop waiting for it to either get
1260 * queued or lose PENDING. Grabbing PENDING and queueing should
1261 * happen with IRQ disabled.
1262 */
1263 WARN_ON_ONCE(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001265 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001266
Tejun Heoc8efcc22010-12-20 19:32:04 +01001267 /* if dying, only works from the same workqueue are allowed */
Tejun Heo618b01e2013-03-12 11:30:04 -07001268 if (unlikely(wq->flags & __WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001269 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001270 return;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001271retry:
Tejun Heoc9178082013-03-12 11:30:04 -07001272 /* pwq which will be used unless @work is executing elsewhere */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001273 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo57469822012-08-03 10:30:45 -07001274 if (cpu == WORK_CPU_UNBOUND)
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001275 cpu = raw_smp_processor_id();
Tejun Heo7fb98ea2013-03-12 11:30:00 -07001276 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heoc9178082013-03-12 11:30:04 -07001277 } else {
1278 pwq = first_pwq(wq);
1279 }
Tejun Heodbf25762012-08-20 14:51:23 -07001280
Tejun Heoc9178082013-03-12 11:30:04 -07001281 /*
1282 * If @work was previously on a different pool, it might still be
1283 * running there, in which case the work needs to be queued on that
1284 * pool to guarantee non-reentrancy.
1285 */
1286 last_pool = get_work_pool(work);
1287 if (last_pool && last_pool != pwq->pool) {
1288 struct worker *worker;
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001289
Tejun Heoc9178082013-03-12 11:30:04 -07001290 spin_lock(&last_pool->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001291
Tejun Heoc9178082013-03-12 11:30:04 -07001292 worker = find_worker_executing_work(last_pool, work);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001293
Tejun Heoc9178082013-03-12 11:30:04 -07001294 if (worker && worker->current_pwq->wq == wq) {
1295 pwq = worker->current_pwq;
Tejun Heo8930cab2012-08-03 10:30:45 -07001296 } else {
Tejun Heoc9178082013-03-12 11:30:04 -07001297 /* meh... not running there, queue here */
1298 spin_unlock(&last_pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08001299 spin_lock(&pwq->pool->lock);
Tejun Heo8930cab2012-08-03 10:30:45 -07001300 }
Tejun Heof3421792010-07-02 10:03:51 +02001301 } else {
Tejun Heo112202d2013-02-13 19:29:12 -08001302 spin_lock(&pwq->pool->lock);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001303 }
1304
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001305 /*
1306 * pwq is determined and locked. For unbound pools, we could have
1307 * raced with pwq release and it could already be dead. If its
1308 * refcnt is zero, repeat pwq selection. Note that pwqs never die
1309 * without another pwq replacing it as the first pwq or while a
1310 * work item is executing on it, so the retying is guaranteed to
1311 * make forward-progress.
1312 */
1313 if (unlikely(!pwq->refcnt)) {
1314 if (wq->flags & WQ_UNBOUND) {
1315 spin_unlock(&pwq->pool->lock);
1316 cpu_relax();
1317 goto retry;
1318 }
1319 /* oops */
1320 WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt",
1321 wq->name, cpu);
1322 }
1323
Tejun Heo112202d2013-02-13 19:29:12 -08001324 /* pwq determined, queue */
1325 trace_workqueue_queue_work(req_cpu, pwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001326
Dan Carpenterf5b25522012-04-13 22:06:58 +03001327 if (WARN_ON(!list_empty(&work->entry))) {
Tejun Heo112202d2013-02-13 19:29:12 -08001328 spin_unlock(&pwq->pool->lock);
Dan Carpenterf5b25522012-04-13 22:06:58 +03001329 return;
1330 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001331
Tejun Heo112202d2013-02-13 19:29:12 -08001332 pwq->nr_in_flight[pwq->work_color]++;
1333 work_flags = work_color_to_flags(pwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001334
Tejun Heo112202d2013-02-13 19:29:12 -08001335 if (likely(pwq->nr_active < pwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001336 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001337 pwq->nr_active++;
1338 worklist = &pwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001339 } else {
1340 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo112202d2013-02-13 19:29:12 -08001341 worklist = &pwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001342 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001343
Tejun Heo112202d2013-02-13 19:29:12 -08001344 insert_work(pwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001345
Tejun Heo112202d2013-02-13 19:29:12 -08001346 spin_unlock(&pwq->pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347}
1348
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001349/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07001350 * queue_work_on - queue work on specific cpu
1351 * @cpu: CPU number to execute work on
1352 * @wq: workqueue to use
1353 * @work: work to queue
1354 *
Tejun Heod4283e92012-08-03 10:30:44 -07001355 * Returns %false if @work was already on a queue, %true otherwise.
Zhang Ruic1a220e2008-07-23 21:28:39 -07001356 *
1357 * We queue the work to a specific CPU, the caller must ensure it
1358 * can't go away.
1359 */
Tejun Heod4283e92012-08-03 10:30:44 -07001360bool queue_work_on(int cpu, struct workqueue_struct *wq,
1361 struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07001362{
Tejun Heod4283e92012-08-03 10:30:44 -07001363 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001364 unsigned long flags;
1365
1366 local_irq_save(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001367
Tejun Heo22df02b2010-06-29 10:07:10 +02001368 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001369 __queue_work(cpu, wq, work);
Tejun Heod4283e92012-08-03 10:30:44 -07001370 ret = true;
Zhang Ruic1a220e2008-07-23 21:28:39 -07001371 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001372
1373 local_irq_restore(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001374 return ret;
1375}
1376EXPORT_SYMBOL_GPL(queue_work_on);
1377
Tejun Heod8e794d2012-08-03 10:30:45 -07001378void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
David Howells52bad642006-11-22 14:54:01 +00001380 struct delayed_work *dwork = (struct delayed_work *)__data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001382 /* should have been called from irqsafe timer with irq already off */
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001383 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384}
Konstantin Khlebnikov1438ade52013-01-24 16:36:31 +04001385EXPORT_SYMBOL(delayed_work_timer_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001387static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
1388 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001390 struct timer_list *timer = &dwork->timer;
1391 struct work_struct *work = &dwork->work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001393 WARN_ON_ONCE(timer->function != delayed_work_timer_fn ||
1394 timer->data != (unsigned long)dwork);
Tejun Heofc4b5142012-12-04 07:40:39 -08001395 WARN_ON_ONCE(timer_pending(timer));
1396 WARN_ON_ONCE(!list_empty(&work->entry));
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001397
Tejun Heo8852aac2012-12-01 16:23:42 -08001398 /*
1399 * If @delay is 0, queue @dwork->work immediately. This is for
1400 * both optimization and correctness. The earliest @timer can
1401 * expire is on the closest next tick and delayed_work users depend
1402 * on that there's no such delay when @delay is 0.
1403 */
1404 if (!delay) {
1405 __queue_work(cpu, wq, &dwork->work);
1406 return;
1407 }
1408
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001409 timer_stats_timer_set_start_info(&dwork->timer);
1410
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001411 dwork->wq = wq;
Tejun Heo12650572012-08-08 09:38:42 -07001412 dwork->cpu = cpu;
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001413 timer->expires = jiffies + delay;
1414
1415 if (unlikely(cpu != WORK_CPU_UNBOUND))
1416 add_timer_on(timer, cpu);
1417 else
1418 add_timer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419}
1420
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001421/**
1422 * queue_delayed_work_on - queue work on specific CPU after delay
1423 * @cpu: CPU number to execute work on
1424 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001425 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001426 * @delay: number of jiffies to wait before queueing
1427 *
Tejun Heo715f1302012-08-03 10:30:46 -07001428 * Returns %false if @work was already on a queue, %true otherwise. If
1429 * @delay is zero and @dwork is idle, it will be scheduled for immediate
1430 * execution.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001431 */
Tejun Heod4283e92012-08-03 10:30:44 -07001432bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
1433 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001434{
David Howells52bad642006-11-22 14:54:01 +00001435 struct work_struct *work = &dwork->work;
Tejun Heod4283e92012-08-03 10:30:44 -07001436 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001437 unsigned long flags;
1438
1439 /* read the comment in __queue_work() */
1440 local_irq_save(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001441
Tejun Heo22df02b2010-06-29 10:07:10 +02001442 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001443 __queue_delayed_work(cpu, wq, dwork, delay);
Tejun Heod4283e92012-08-03 10:30:44 -07001444 ret = true;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001445 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001446
1447 local_irq_restore(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001448 return ret;
1449}
Dave Jonesae90dd52006-06-30 01:40:45 -04001450EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Tejun Heoc8e55f32010-06-29 10:07:12 +02001452/**
Tejun Heo8376fe22012-08-03 10:30:47 -07001453 * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
1454 * @cpu: CPU number to execute work on
1455 * @wq: workqueue to use
1456 * @dwork: work to queue
1457 * @delay: number of jiffies to wait before queueing
1458 *
1459 * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
1460 * modify @dwork's timer so that it expires after @delay. If @delay is
1461 * zero, @work is guaranteed to be scheduled immediately regardless of its
1462 * current state.
1463 *
1464 * Returns %false if @dwork was idle and queued, %true if @dwork was
1465 * pending and its timer was modified.
1466 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001467 * This function is safe to call from any context including IRQ handler.
Tejun Heo8376fe22012-08-03 10:30:47 -07001468 * See try_to_grab_pending() for details.
1469 */
1470bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
1471 struct delayed_work *dwork, unsigned long delay)
1472{
1473 unsigned long flags;
1474 int ret;
1475
1476 do {
1477 ret = try_to_grab_pending(&dwork->work, true, &flags);
1478 } while (unlikely(ret == -EAGAIN));
1479
1480 if (likely(ret >= 0)) {
1481 __queue_delayed_work(cpu, wq, dwork, delay);
1482 local_irq_restore(flags);
1483 }
1484
1485 /* -ENOENT from try_to_grab_pending() becomes %true */
1486 return ret;
1487}
1488EXPORT_SYMBOL_GPL(mod_delayed_work_on);
1489
1490/**
Tejun Heoc8e55f32010-06-29 10:07:12 +02001491 * worker_enter_idle - enter idle state
1492 * @worker: worker which is entering idle state
1493 *
1494 * @worker is entering idle state. Update stats and idle timer if
1495 * necessary.
1496 *
1497 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001498 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001499 */
1500static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001502 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Tejun Heo6183c002013-03-12 11:29:57 -07001504 if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) ||
1505 WARN_ON_ONCE(!list_empty(&worker->entry) &&
1506 (worker->hentry.next || worker->hentry.pprev)))
1507 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Tejun Heocb444762010-07-02 10:03:50 +02001509 /* can't use worker_set_flags(), also called from start_worker() */
1510 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001511 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001512 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001513
Tejun Heoc8e55f32010-06-29 10:07:12 +02001514 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001515 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001516
Tejun Heo628c78e2012-07-17 12:39:27 -07001517 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1518 mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
Tejun Heocb444762010-07-02 10:03:50 +02001519
Tejun Heo544ecf32012-05-14 15:04:50 -07001520 /*
Tejun Heo706026c2013-01-24 11:01:34 -08001521 * Sanity check nr_running. Because wq_unbind_fn() releases
Tejun Heod565ed62013-01-24 11:01:33 -08001522 * pool->lock between setting %WORKER_UNBOUND and zapping
Tejun Heo628c78e2012-07-17 12:39:27 -07001523 * nr_running, the warning may trigger spuriously. Check iff
1524 * unbind is not in progress.
Tejun Heo544ecf32012-05-14 15:04:50 -07001525 */
Tejun Heo24647572013-01-24 11:01:33 -08001526 WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001527 pool->nr_workers == pool->nr_idle &&
Tejun Heoe19e3972013-01-24 11:39:44 -08001528 atomic_read(&pool->nr_running));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529}
1530
Tejun Heoc8e55f32010-06-29 10:07:12 +02001531/**
1532 * worker_leave_idle - leave idle state
1533 * @worker: worker which is leaving idle state
1534 *
1535 * @worker is leaving idle state. Update stats.
1536 *
1537 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001538 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001539 */
1540static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001542 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Tejun Heo6183c002013-03-12 11:29:57 -07001544 if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE)))
1545 return;
Tejun Heod302f012010-06-29 10:07:13 +02001546 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001547 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001548 list_del_init(&worker->entry);
1549}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Tejun Heoe22bee72010-06-29 10:07:14 +02001551/**
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001552 * worker_maybe_bind_and_lock - try to bind %current to worker_pool and lock it
1553 * @pool: target worker_pool
1554 *
1555 * Bind %current to the cpu of @pool if it is associated and lock @pool.
Tejun Heoe22bee72010-06-29 10:07:14 +02001556 *
1557 * Works which are scheduled while the cpu is online must at least be
1558 * scheduled to a worker which is bound to the cpu so that if they are
1559 * flushed from cpu callbacks while cpu is going down, they are
1560 * guaranteed to execute on the cpu.
1561 *
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001562 * This function is to be used by unbound workers and rescuers to bind
Tejun Heoe22bee72010-06-29 10:07:14 +02001563 * themselves to the target cpu and may race with cpu going down or
1564 * coming online. kthread_bind() can't be used because it may put the
1565 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
Tejun Heo706026c2013-01-24 11:01:34 -08001566 * verbatim as it's best effort and blocking and pool may be
Tejun Heoe22bee72010-06-29 10:07:14 +02001567 * [dis]associated in the meantime.
1568 *
Tejun Heo706026c2013-01-24 11:01:34 -08001569 * This function tries set_cpus_allowed() and locks pool and verifies the
Tejun Heo24647572013-01-24 11:01:33 -08001570 * binding against %POOL_DISASSOCIATED which is set during
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001571 * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1572 * enters idle state or fetches works without dropping lock, it can
1573 * guarantee the scheduling requirement described in the first paragraph.
Tejun Heoe22bee72010-06-29 10:07:14 +02001574 *
1575 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001576 * Might sleep. Called without any lock but returns with pool->lock
Tejun Heoe22bee72010-06-29 10:07:14 +02001577 * held.
1578 *
1579 * RETURNS:
Tejun Heo706026c2013-01-24 11:01:34 -08001580 * %true if the associated pool is online (@worker is successfully
Tejun Heoe22bee72010-06-29 10:07:14 +02001581 * bound), %false if offline.
1582 */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001583static bool worker_maybe_bind_and_lock(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001584__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001585{
Tejun Heoe22bee72010-06-29 10:07:14 +02001586 while (true) {
1587 /*
1588 * The following call may fail, succeed or succeed
1589 * without actually migrating the task to the cpu if
1590 * it races with cpu hotunplug operation. Verify
Tejun Heo24647572013-01-24 11:01:33 -08001591 * against POOL_DISASSOCIATED.
Tejun Heoe22bee72010-06-29 10:07:14 +02001592 */
Tejun Heo24647572013-01-24 11:01:33 -08001593 if (!(pool->flags & POOL_DISASSOCIATED))
Tejun Heo7a4e3442013-03-12 11:30:00 -07001594 set_cpus_allowed_ptr(current, pool->attrs->cpumask);
Oleg Nesterov85f41862007-05-09 02:34:20 -07001595
Tejun Heod565ed62013-01-24 11:01:33 -08001596 spin_lock_irq(&pool->lock);
Tejun Heo24647572013-01-24 11:01:33 -08001597 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heoe22bee72010-06-29 10:07:14 +02001598 return false;
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001599 if (task_cpu(current) == pool->cpu &&
Tejun Heo7a4e3442013-03-12 11:30:00 -07001600 cpumask_equal(&current->cpus_allowed, pool->attrs->cpumask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001601 return true;
Tejun Heod565ed62013-01-24 11:01:33 -08001602 spin_unlock_irq(&pool->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001603
Tejun Heo5035b202011-04-29 18:08:37 +02001604 /*
1605 * We've raced with CPU hot[un]plug. Give it a breather
1606 * and retry migration. cond_resched() is required here;
1607 * otherwise, we might deadlock against cpu_stop trying to
1608 * bring down the CPU on non-preemptive kernel.
1609 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001610 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001611 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001612 }
1613}
1614
1615/*
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001616 * Rebind an idle @worker to its CPU. worker_thread() will test
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001617 * list_empty(@worker->entry) before leaving idle and call this function.
Tejun Heo25511a42012-07-17 12:39:27 -07001618 */
1619static void idle_worker_rebind(struct worker *worker)
1620{
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001621 /* CPU may go down again inbetween, clear UNBOUND only on success */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001622 if (worker_maybe_bind_and_lock(worker->pool))
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001623 worker_clr_flags(worker, WORKER_UNBOUND);
Tejun Heo25511a42012-07-17 12:39:27 -07001624
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001625 /* rebind complete, become available again */
1626 list_add(&worker->entry, &worker->pool->idle_list);
Tejun Heod565ed62013-01-24 11:01:33 -08001627 spin_unlock_irq(&worker->pool->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07001628}
1629
1630/*
1631 * Function for @worker->rebind.work used to rebind unbound busy workers to
Tejun Heo403c8212012-07-17 12:39:27 -07001632 * the associated cpu which is coming back online. This is scheduled by
1633 * cpu up but can race with other cpu hotplug operations and may be
1634 * executed twice without intervening cpu down.
Tejun Heoe22bee72010-06-29 10:07:14 +02001635 */
Tejun Heo25511a42012-07-17 12:39:27 -07001636static void busy_worker_rebind_fn(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001637{
1638 struct worker *worker = container_of(work, struct worker, rebind_work);
Tejun Heoe22bee72010-06-29 10:07:14 +02001639
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001640 if (worker_maybe_bind_and_lock(worker->pool))
Lai Jiangshaneab6d822012-09-18 09:59:22 -07001641 worker_clr_flags(worker, WORKER_UNBOUND);
Tejun Heoe22bee72010-06-29 10:07:14 +02001642
Tejun Heod565ed62013-01-24 11:01:33 -08001643 spin_unlock_irq(&worker->pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001644}
1645
Tejun Heoc34056a2010-06-29 10:07:11 +02001646static struct worker *alloc_worker(void)
1647{
1648 struct worker *worker;
1649
1650 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001651 if (worker) {
1652 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001653 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heo25511a42012-07-17 12:39:27 -07001654 INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn);
Tejun Heoe22bee72010-06-29 10:07:14 +02001655 /* on creation a worker is in !idle && prep state */
1656 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001657 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001658 return worker;
1659}
1660
1661/**
1662 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001663 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001664 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001665 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001666 * can be started by calling start_worker() or destroyed using
1667 * destroy_worker().
1668 *
1669 * CONTEXT:
1670 * Might sleep. Does GFP_KERNEL allocations.
1671 *
1672 * RETURNS:
1673 * Pointer to the newly created worker.
1674 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001675static struct worker *create_worker(struct worker_pool *pool)
Tejun Heoc34056a2010-06-29 10:07:11 +02001676{
Tejun Heo7a4e3442013-03-12 11:30:00 -07001677 const char *pri = pool->attrs->nice < 0 ? "H" : "";
Tejun Heoc34056a2010-06-29 10:07:11 +02001678 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001679 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001680
Tejun Heocd549682013-03-13 19:47:39 -07001681 lockdep_assert_held(&pool->manager_mutex);
1682
Tejun Heo822d8402013-03-19 13:45:21 -07001683 /*
1684 * ID is needed to determine kthread name. Allocate ID first
1685 * without installing the pointer.
1686 */
1687 idr_preload(GFP_KERNEL);
Tejun Heod565ed62013-01-24 11:01:33 -08001688 spin_lock_irq(&pool->lock);
Tejun Heo822d8402013-03-19 13:45:21 -07001689
1690 id = idr_alloc(&pool->worker_idr, NULL, 0, 0, GFP_NOWAIT);
1691
Tejun Heod565ed62013-01-24 11:01:33 -08001692 spin_unlock_irq(&pool->lock);
Tejun Heo822d8402013-03-19 13:45:21 -07001693 idr_preload_end();
1694 if (id < 0)
1695 goto fail;
Tejun Heoc34056a2010-06-29 10:07:11 +02001696
1697 worker = alloc_worker();
1698 if (!worker)
1699 goto fail;
1700
Tejun Heobd7bdd42012-07-12 14:46:37 -07001701 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001702 worker->id = id;
1703
Tejun Heo29c91e92013-03-12 11:30:03 -07001704 if (pool->cpu >= 0)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001705 worker->task = kthread_create_on_node(worker_thread,
Tejun Heoec22ca52013-01-24 11:01:33 -08001706 worker, cpu_to_node(pool->cpu),
Tejun Heod84ff052013-03-12 11:29:59 -07001707 "kworker/%d:%d%s", pool->cpu, id, pri);
Tejun Heof3421792010-07-02 10:03:51 +02001708 else
1709 worker->task = kthread_create(worker_thread, worker,
Tejun Heoac6104c2013-03-12 11:30:03 -07001710 "kworker/u%d:%d%s",
1711 pool->id, id, pri);
Tejun Heoc34056a2010-06-29 10:07:11 +02001712 if (IS_ERR(worker->task))
1713 goto fail;
1714
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001715 /*
1716 * set_cpus_allowed_ptr() will fail if the cpumask doesn't have any
1717 * online CPUs. It'll be re-applied when any of the CPUs come up.
1718 */
Tejun Heo7a4e3442013-03-12 11:30:00 -07001719 set_user_nice(worker->task, pool->attrs->nice);
1720 set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask);
Tejun Heo32704762012-07-13 22:16:45 -07001721
Tejun Heo14a40ff2013-03-19 13:45:20 -07001722 /* prevent userland from meddling with cpumask of workqueue workers */
1723 worker->task->flags |= PF_NO_SETAFFINITY;
Tejun Heo7a4e3442013-03-12 11:30:00 -07001724
1725 /*
1726 * The caller is responsible for ensuring %POOL_DISASSOCIATED
1727 * remains stable across this function. See the comments above the
1728 * flag definition for details.
1729 */
1730 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001731 worker->flags |= WORKER_UNBOUND;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001732
Tejun Heo822d8402013-03-19 13:45:21 -07001733 /* successful, commit the pointer to idr */
1734 spin_lock_irq(&pool->lock);
1735 idr_replace(&pool->worker_idr, worker, worker->id);
1736 spin_unlock_irq(&pool->lock);
1737
Tejun Heoc34056a2010-06-29 10:07:11 +02001738 return worker;
Tejun Heo822d8402013-03-19 13:45:21 -07001739
Tejun Heoc34056a2010-06-29 10:07:11 +02001740fail:
1741 if (id >= 0) {
Tejun Heod565ed62013-01-24 11:01:33 -08001742 spin_lock_irq(&pool->lock);
Tejun Heo822d8402013-03-19 13:45:21 -07001743 idr_remove(&pool->worker_idr, id);
Tejun Heod565ed62013-01-24 11:01:33 -08001744 spin_unlock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001745 }
1746 kfree(worker);
1747 return NULL;
1748}
1749
1750/**
1751 * start_worker - start a newly created worker
1752 * @worker: worker to start
1753 *
Tejun Heo706026c2013-01-24 11:01:34 -08001754 * Make the pool aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001755 *
1756 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001757 * spin_lock_irq(pool->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001758 */
1759static void start_worker(struct worker *worker)
1760{
Tejun Heocb444762010-07-02 10:03:50 +02001761 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001762 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001763 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001764 wake_up_process(worker->task);
1765}
1766
1767/**
Tejun Heoebf44d12013-03-13 19:47:39 -07001768 * create_and_start_worker - create and start a worker for a pool
1769 * @pool: the target pool
1770 *
Tejun Heocd549682013-03-13 19:47:39 -07001771 * Grab the managership of @pool and create and start a new worker for it.
Tejun Heoebf44d12013-03-13 19:47:39 -07001772 */
1773static int create_and_start_worker(struct worker_pool *pool)
1774{
1775 struct worker *worker;
1776
Tejun Heocd549682013-03-13 19:47:39 -07001777 mutex_lock(&pool->manager_mutex);
1778
Tejun Heoebf44d12013-03-13 19:47:39 -07001779 worker = create_worker(pool);
1780 if (worker) {
1781 spin_lock_irq(&pool->lock);
1782 start_worker(worker);
1783 spin_unlock_irq(&pool->lock);
1784 }
1785
Tejun Heocd549682013-03-13 19:47:39 -07001786 mutex_unlock(&pool->manager_mutex);
1787
Tejun Heoebf44d12013-03-13 19:47:39 -07001788 return worker ? 0 : -ENOMEM;
1789}
1790
1791/**
Tejun Heoc34056a2010-06-29 10:07:11 +02001792 * destroy_worker - destroy a workqueue worker
1793 * @worker: worker to be destroyed
1794 *
Tejun Heo706026c2013-01-24 11:01:34 -08001795 * Destroy @worker and adjust @pool stats accordingly.
Tejun Heoc8e55f32010-06-29 10:07:12 +02001796 *
1797 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001798 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001799 */
1800static void destroy_worker(struct worker *worker)
1801{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001802 struct worker_pool *pool = worker->pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001803
Tejun Heocd549682013-03-13 19:47:39 -07001804 lockdep_assert_held(&pool->manager_mutex);
1805 lockdep_assert_held(&pool->lock);
1806
Tejun Heoc34056a2010-06-29 10:07:11 +02001807 /* sanity check frenzy */
Tejun Heo6183c002013-03-12 11:29:57 -07001808 if (WARN_ON(worker->current_work) ||
1809 WARN_ON(!list_empty(&worker->scheduled)))
1810 return;
Tejun Heoc34056a2010-06-29 10:07:11 +02001811
Tejun Heoc8e55f32010-06-29 10:07:12 +02001812 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001813 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001814 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001815 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001816
1817 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001818 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001819
Tejun Heo822d8402013-03-19 13:45:21 -07001820 idr_remove(&pool->worker_idr, worker->id);
1821
Tejun Heod565ed62013-01-24 11:01:33 -08001822 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001823
Tejun Heoc34056a2010-06-29 10:07:11 +02001824 kthread_stop(worker->task);
1825 kfree(worker);
1826
Tejun Heod565ed62013-01-24 11:01:33 -08001827 spin_lock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001828}
1829
Tejun Heo63d95a92012-07-12 14:46:37 -07001830static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001831{
Tejun Heo63d95a92012-07-12 14:46:37 -07001832 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001833
Tejun Heod565ed62013-01-24 11:01:33 -08001834 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001835
Tejun Heo63d95a92012-07-12 14:46:37 -07001836 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001837 struct worker *worker;
1838 unsigned long expires;
1839
1840 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001841 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001842 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1843
1844 if (time_before(jiffies, expires))
Tejun Heo63d95a92012-07-12 14:46:37 -07001845 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001846 else {
1847 /* it's been idle for too long, wake up manager */
Tejun Heo11ebea52012-07-12 14:46:37 -07001848 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -07001849 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001850 }
1851 }
1852
Tejun Heod565ed62013-01-24 11:01:33 -08001853 spin_unlock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001854}
1855
Tejun Heo493a1722013-03-12 11:29:59 -07001856static void send_mayday(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001857{
Tejun Heo112202d2013-02-13 19:29:12 -08001858 struct pool_workqueue *pwq = get_work_pwq(work);
1859 struct workqueue_struct *wq = pwq->wq;
Tejun Heo493a1722013-03-12 11:29:59 -07001860
Tejun Heo2e109a22013-03-13 19:47:40 -07001861 lockdep_assert_held(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001862
Tejun Heo493008a2013-03-12 11:30:03 -07001863 if (!wq->rescuer)
Tejun Heo493a1722013-03-12 11:29:59 -07001864 return;
Tejun Heoe22bee72010-06-29 10:07:14 +02001865
1866 /* mayday mayday mayday */
Tejun Heo493a1722013-03-12 11:29:59 -07001867 if (list_empty(&pwq->mayday_node)) {
1868 list_add_tail(&pwq->mayday_node, &wq->maydays);
Tejun Heoe22bee72010-06-29 10:07:14 +02001869 wake_up_process(wq->rescuer->task);
Tejun Heo493a1722013-03-12 11:29:59 -07001870 }
Tejun Heoe22bee72010-06-29 10:07:14 +02001871}
1872
Tejun Heo706026c2013-01-24 11:01:34 -08001873static void pool_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001874{
Tejun Heo63d95a92012-07-12 14:46:37 -07001875 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001876 struct work_struct *work;
1877
Tejun Heo2e109a22013-03-13 19:47:40 -07001878 spin_lock_irq(&wq_mayday_lock); /* for wq->maydays */
Tejun Heo493a1722013-03-12 11:29:59 -07001879 spin_lock(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001880
Tejun Heo63d95a92012-07-12 14:46:37 -07001881 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001882 /*
1883 * We've been trying to create a new worker but
1884 * haven't been successful. We might be hitting an
1885 * allocation deadlock. Send distress signals to
1886 * rescuers.
1887 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001888 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001889 send_mayday(work);
1890 }
1891
Tejun Heo493a1722013-03-12 11:29:59 -07001892 spin_unlock(&pool->lock);
Tejun Heo2e109a22013-03-13 19:47:40 -07001893 spin_unlock_irq(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001894
Tejun Heo63d95a92012-07-12 14:46:37 -07001895 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001896}
1897
1898/**
1899 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001900 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001901 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001902 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001903 * have at least one idle worker on return from this function. If
1904 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001905 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001906 * possible allocation deadlock.
1907 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001908 * On return, need_to_create_worker() is guaranteed to be %false and
1909 * may_start_working() %true.
Tejun Heoe22bee72010-06-29 10:07:14 +02001910 *
1911 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001912 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001913 * multiple times. Does GFP_KERNEL allocations. Called only from
1914 * manager.
1915 *
1916 * RETURNS:
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001917 * %false if no action was taken and pool->lock stayed locked, %true
Tejun Heoe22bee72010-06-29 10:07:14 +02001918 * otherwise.
1919 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001920static bool maybe_create_worker(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001921__releases(&pool->lock)
1922__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001923{
Tejun Heo63d95a92012-07-12 14:46:37 -07001924 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001925 return false;
1926restart:
Tejun Heod565ed62013-01-24 11:01:33 -08001927 spin_unlock_irq(&pool->lock);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001928
Tejun Heoe22bee72010-06-29 10:07:14 +02001929 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07001930 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001931
1932 while (true) {
1933 struct worker *worker;
1934
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001935 worker = create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001936 if (worker) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001937 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001938 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001939 start_worker(worker);
Tejun Heo6183c002013-03-12 11:29:57 -07001940 if (WARN_ON_ONCE(need_to_create_worker(pool)))
1941 goto restart;
Tejun Heoe22bee72010-06-29 10:07:14 +02001942 return true;
1943 }
1944
Tejun Heo63d95a92012-07-12 14:46:37 -07001945 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001946 break;
1947
Tejun Heoe22bee72010-06-29 10:07:14 +02001948 __set_current_state(TASK_INTERRUPTIBLE);
1949 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001950
Tejun Heo63d95a92012-07-12 14:46:37 -07001951 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001952 break;
1953 }
1954
Tejun Heo63d95a92012-07-12 14:46:37 -07001955 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001956 spin_lock_irq(&pool->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07001957 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001958 goto restart;
1959 return true;
1960}
1961
1962/**
1963 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo63d95a92012-07-12 14:46:37 -07001964 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001965 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001966 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02001967 * IDLE_WORKER_TIMEOUT.
1968 *
1969 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001970 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001971 * multiple times. Called only from manager.
1972 *
1973 * RETURNS:
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001974 * %false if no action was taken and pool->lock stayed locked, %true
Tejun Heoe22bee72010-06-29 10:07:14 +02001975 * otherwise.
1976 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001977static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001978{
1979 bool ret = false;
1980
Tejun Heo63d95a92012-07-12 14:46:37 -07001981 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001982 struct worker *worker;
1983 unsigned long expires;
1984
Tejun Heo63d95a92012-07-12 14:46:37 -07001985 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001986 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1987
1988 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001989 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001990 break;
1991 }
1992
1993 destroy_worker(worker);
1994 ret = true;
1995 }
1996
1997 return ret;
1998}
1999
2000/**
2001 * manage_workers - manage worker pool
2002 * @worker: self
2003 *
Tejun Heo706026c2013-01-24 11:01:34 -08002004 * Assume the manager role and manage the worker pool @worker belongs
Tejun Heoe22bee72010-06-29 10:07:14 +02002005 * to. At any given time, there can be only zero or one manager per
Tejun Heo706026c2013-01-24 11:01:34 -08002006 * pool. The exclusion is handled automatically by this function.
Tejun Heoe22bee72010-06-29 10:07:14 +02002007 *
2008 * The caller can safely start processing works on false return. On
2009 * true return, it's guaranteed that need_to_create_worker() is false
2010 * and may_start_working() is true.
2011 *
2012 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002013 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02002014 * multiple times. Does GFP_KERNEL allocations.
2015 *
2016 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08002017 * spin_lock_irq(pool->lock) which may be released and regrabbed
2018 * multiple times. Does GFP_KERNEL allocations.
Tejun Heoe22bee72010-06-29 10:07:14 +02002019 */
2020static bool manage_workers(struct worker *worker)
2021{
Tejun Heo63d95a92012-07-12 14:46:37 -07002022 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002023 bool ret = false;
2024
Tejun Heobc3a1af2013-03-13 19:47:39 -07002025 /*
2026 * Managership is governed by two mutexes - manager_arb and
2027 * manager_mutex. manager_arb handles arbitration of manager role.
2028 * Anyone who successfully grabs manager_arb wins the arbitration
2029 * and becomes the manager. mutex_trylock() on pool->manager_arb
2030 * failure while holding pool->lock reliably indicates that someone
2031 * else is managing the pool and the worker which failed trylock
2032 * can proceed to executing work items. This means that anyone
2033 * grabbing manager_arb is responsible for actually performing
2034 * manager duties. If manager_arb is grabbed and released without
2035 * actual management, the pool may stall indefinitely.
2036 *
2037 * manager_mutex is used for exclusion of actual management
2038 * operations. The holder of manager_mutex can be sure that none
2039 * of management operations, including creation and destruction of
2040 * workers, won't take place until the mutex is released. Because
2041 * manager_mutex doesn't interfere with manager role arbitration,
2042 * it is guaranteed that the pool's management, while may be
2043 * delayed, won't be disturbed by someone else grabbing
2044 * manager_mutex.
2045 */
Tejun Heo34a06bd2013-03-12 11:30:00 -07002046 if (!mutex_trylock(&pool->manager_arb))
Tejun Heoe22bee72010-06-29 10:07:14 +02002047 return ret;
2048
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002049 /*
Tejun Heobc3a1af2013-03-13 19:47:39 -07002050 * With manager arbitration won, manager_mutex would be free in
2051 * most cases. trylock first without dropping @pool->lock.
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002052 */
Tejun Heobc3a1af2013-03-13 19:47:39 -07002053 if (unlikely(!mutex_trylock(&pool->manager_mutex))) {
Tejun Heod565ed62013-01-24 11:01:33 -08002054 spin_unlock_irq(&pool->lock);
Tejun Heobc3a1af2013-03-13 19:47:39 -07002055 mutex_lock(&pool->manager_mutex);
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002056 /*
2057 * CPU hotplug could have happened while we were waiting
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002058 * for assoc_mutex. Hotplug itself can't handle us
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002059 * because manager isn't either on idle or busy list, and
Tejun Heo706026c2013-01-24 11:01:34 -08002060 * @pool's state and ours could have deviated.
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002061 *
Tejun Heobc3a1af2013-03-13 19:47:39 -07002062 * As hotplug is now excluded via manager_mutex, we can
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002063 * simply try to bind. It will succeed or fail depending
Tejun Heo706026c2013-01-24 11:01:34 -08002064 * on @pool's current state. Try it and adjust
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002065 * %WORKER_UNBOUND accordingly.
2066 */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08002067 if (worker_maybe_bind_and_lock(pool))
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002068 worker->flags &= ~WORKER_UNBOUND;
2069 else
2070 worker->flags |= WORKER_UNBOUND;
2071
2072 ret = true;
2073 }
2074
Tejun Heo11ebea52012-07-12 14:46:37 -07002075 pool->flags &= ~POOL_MANAGE_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02002076
2077 /*
2078 * Destroy and then create so that may_start_working() is true
2079 * on return.
2080 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002081 ret |= maybe_destroy_workers(pool);
2082 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002083
Tejun Heobc3a1af2013-03-13 19:47:39 -07002084 mutex_unlock(&pool->manager_mutex);
Tejun Heo34a06bd2013-03-12 11:30:00 -07002085 mutex_unlock(&pool->manager_arb);
Tejun Heoe22bee72010-06-29 10:07:14 +02002086 return ret;
2087}
2088
Tejun Heoa62428c2010-06-29 10:07:10 +02002089/**
2090 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02002091 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02002092 * @work: work to process
2093 *
2094 * Process @work. This function contains all the logics necessary to
2095 * process a single work including synchronization against and
2096 * interaction with other workers on the same cpu, queueing and
2097 * flushing. As long as context requirement is met, any worker can
2098 * call this function to process a work.
2099 *
2100 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002101 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02002102 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002103static void process_one_work(struct worker *worker, struct work_struct *work)
Tejun Heod565ed62013-01-24 11:01:33 -08002104__releases(&pool->lock)
2105__acquires(&pool->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02002106{
Tejun Heo112202d2013-02-13 19:29:12 -08002107 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002108 struct worker_pool *pool = worker->pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002109 bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02002110 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02002111 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02002112#ifdef CONFIG_LOCKDEP
2113 /*
2114 * It is permissible to free the struct work_struct from
2115 * inside the function that is called from it, this we need to
2116 * take into account for lockdep too. To avoid bogus "held
2117 * lock freed" warnings as well as problems when looking into
2118 * work->lockdep_map, make a copy and use that here.
2119 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07002120 struct lockdep_map lockdep_map;
2121
2122 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002123#endif
Tejun Heo6fec10a2012-07-22 10:16:34 -07002124 /*
2125 * Ensure we're on the correct CPU. DISASSOCIATED test is
2126 * necessary to avoid spurious warnings from rescuers servicing the
Tejun Heo24647572013-01-24 11:01:33 -08002127 * unbound or a disassociated pool.
Tejun Heo6fec10a2012-07-22 10:16:34 -07002128 */
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002129 WARN_ON_ONCE(!(worker->flags & WORKER_UNBOUND) &&
Tejun Heo24647572013-01-24 11:01:33 -08002130 !(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heoec22ca52013-01-24 11:01:33 -08002131 raw_smp_processor_id() != pool->cpu);
Tejun Heo25511a42012-07-17 12:39:27 -07002132
Tejun Heo7e116292010-06-29 10:07:13 +02002133 /*
2134 * A single work shouldn't be executed concurrently by
2135 * multiple workers on a single cpu. Check whether anyone is
2136 * already processing the work. If so, defer the work to the
2137 * currently executing one.
2138 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002139 collision = find_worker_executing_work(pool, work);
Tejun Heo7e116292010-06-29 10:07:13 +02002140 if (unlikely(collision)) {
2141 move_linked_works(work, &collision->scheduled, NULL);
2142 return;
2143 }
2144
Tejun Heo8930cab2012-08-03 10:30:45 -07002145 /* claim and dequeue */
Tejun Heoa62428c2010-06-29 10:07:10 +02002146 debug_work_deactivate(work);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002147 hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work);
Tejun Heoc34056a2010-06-29 10:07:11 +02002148 worker->current_work = work;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002149 worker->current_func = work->func;
Tejun Heo112202d2013-02-13 19:29:12 -08002150 worker->current_pwq = pwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002151 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002152
Tejun Heoa62428c2010-06-29 10:07:10 +02002153 list_del_init(&work->entry);
2154
Tejun Heo649027d2010-06-29 10:07:14 +02002155 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02002156 * CPU intensive works don't participate in concurrency
2157 * management. They're the scheduler's responsibility.
2158 */
2159 if (unlikely(cpu_intensive))
2160 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
2161
Tejun Heo974271c2012-07-12 14:46:37 -07002162 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002163 * Unbound pool isn't concurrency managed and work items should be
Tejun Heo974271c2012-07-12 14:46:37 -07002164 * executed ASAP. Wake up another worker if necessary.
2165 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002166 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
2167 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07002168
Tejun Heo8930cab2012-08-03 10:30:45 -07002169 /*
Tejun Heo7c3eed52013-01-24 11:01:33 -08002170 * Record the last pool and clear PENDING which should be the last
Tejun Heod565ed62013-01-24 11:01:33 -08002171 * update to @work. Also, do this inside @pool->lock so that
Tejun Heo23657bb2012-08-13 17:08:19 -07002172 * PENDING and queued state changes happen together while IRQ is
2173 * disabled.
Tejun Heo8930cab2012-08-03 10:30:45 -07002174 */
Tejun Heo7c3eed52013-01-24 11:01:33 -08002175 set_work_pool_and_clear_pending(work, pool->id);
Tejun Heoa62428c2010-06-29 10:07:10 +02002176
Tejun Heod565ed62013-01-24 11:01:33 -08002177 spin_unlock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002178
Tejun Heo112202d2013-02-13 19:29:12 -08002179 lock_map_acquire_read(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002180 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002181 trace_workqueue_execute_start(work);
Tejun Heoa2c1c572012-12-18 10:35:02 -08002182 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002183 /*
2184 * While we must be careful to not use "work" after this, the trace
2185 * point will only record its address.
2186 */
2187 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002188 lock_map_release(&lockdep_map);
Tejun Heo112202d2013-02-13 19:29:12 -08002189 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002190
2191 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Valentin Ilie044c7822012-08-19 00:52:42 +03002192 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
2193 " last function: %pf\n",
Tejun Heoa2c1c572012-12-18 10:35:02 -08002194 current->comm, preempt_count(), task_pid_nr(current),
2195 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02002196 debug_show_held_locks(current);
2197 dump_stack();
2198 }
2199
Tejun Heod565ed62013-01-24 11:01:33 -08002200 spin_lock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002201
Tejun Heofb0e7be2010-06-29 10:07:15 +02002202 /* clear cpu intensive status */
2203 if (unlikely(cpu_intensive))
2204 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2205
Tejun Heoa62428c2010-06-29 10:07:10 +02002206 /* we're done with it, release */
Sasha Levin42f85702012-12-17 10:01:23 -05002207 hash_del(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002208 worker->current_work = NULL;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002209 worker->current_func = NULL;
Tejun Heo112202d2013-02-13 19:29:12 -08002210 worker->current_pwq = NULL;
2211 pwq_dec_nr_in_flight(pwq, work_color);
Tejun Heoa62428c2010-06-29 10:07:10 +02002212}
2213
Tejun Heoaffee4b2010-06-29 10:07:12 +02002214/**
2215 * process_scheduled_works - process scheduled works
2216 * @worker: self
2217 *
2218 * Process all scheduled works. Please note that the scheduled list
2219 * may change while processing a work, so this function repeatedly
2220 * fetches a work from the top and executes it.
2221 *
2222 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002223 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02002224 * multiple times.
2225 */
2226static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002228 while (!list_empty(&worker->scheduled)) {
2229 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002231 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233}
2234
Tejun Heo4690c4a2010-06-29 10:07:10 +02002235/**
2236 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02002237 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02002238 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002239 * The worker thread function. All workers belong to a worker_pool -
2240 * either a per-cpu one or dynamic unbound one. These workers process all
2241 * work items regardless of their specific target workqueue. The only
2242 * exception is work items which belong to workqueues with a rescuer which
2243 * will be explained in rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02002244 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002245static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246{
Tejun Heoc34056a2010-06-29 10:07:11 +02002247 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07002248 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
Tejun Heoe22bee72010-06-29 10:07:14 +02002250 /* tell the scheduler that this is a workqueue worker */
2251 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002252woke_up:
Tejun Heod565ed62013-01-24 11:01:33 -08002253 spin_lock_irq(&pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002255 /* we are off idle list if destruction or rebind is requested */
2256 if (unlikely(list_empty(&worker->entry))) {
Tejun Heod565ed62013-01-24 11:01:33 -08002257 spin_unlock_irq(&pool->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07002258
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002259 /* if DIE is set, destruction is requested */
Tejun Heo25511a42012-07-17 12:39:27 -07002260 if (worker->flags & WORKER_DIE) {
2261 worker->task->flags &= ~PF_WQ_WORKER;
2262 return 0;
2263 }
2264
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002265 /* otherwise, rebind */
Tejun Heo25511a42012-07-17 12:39:27 -07002266 idle_worker_rebind(worker);
2267 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 }
2269
Tejun Heoc8e55f32010-06-29 10:07:12 +02002270 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002271recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002272 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002273 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002274 goto sleep;
2275
2276 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002277 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002278 goto recheck;
2279
Tejun Heoc8e55f32010-06-29 10:07:12 +02002280 /*
2281 * ->scheduled list can only be filled while a worker is
2282 * preparing to process a work or actually processing it.
2283 * Make sure nobody diddled with it while I was sleeping.
2284 */
Tejun Heo6183c002013-03-12 11:29:57 -07002285 WARN_ON_ONCE(!list_empty(&worker->scheduled));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002286
Tejun Heoe22bee72010-06-29 10:07:14 +02002287 /*
2288 * When control reaches this point, we're guaranteed to have
2289 * at least one idle worker or that someone else has already
2290 * assumed the manager role.
2291 */
2292 worker_clr_flags(worker, WORKER_PREP);
2293
2294 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002295 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002296 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002297 struct work_struct, entry);
2298
2299 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2300 /* optimization path, not strictly necessary */
2301 process_one_work(worker, work);
2302 if (unlikely(!list_empty(&worker->scheduled)))
2303 process_scheduled_works(worker);
2304 } else {
2305 move_linked_works(work, &worker->scheduled, NULL);
2306 process_scheduled_works(worker);
2307 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002308 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002309
Tejun Heoe22bee72010-06-29 10:07:14 +02002310 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002311sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07002312 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002313 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002314
Tejun Heoc8e55f32010-06-29 10:07:12 +02002315 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002316 * pool->lock is held and there's no work to process and no need to
2317 * manage, sleep. Workers are woken up only while holding
2318 * pool->lock or from local cpu, so setting the current state
2319 * before releasing pool->lock is enough to prevent losing any
2320 * event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002321 */
2322 worker_enter_idle(worker);
2323 __set_current_state(TASK_INTERRUPTIBLE);
Tejun Heod565ed62013-01-24 11:01:33 -08002324 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02002325 schedule();
2326 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327}
2328
Tejun Heoe22bee72010-06-29 10:07:14 +02002329/**
2330 * rescuer_thread - the rescuer thread function
Tejun Heo111c2252013-01-17 17:16:24 -08002331 * @__rescuer: self
Tejun Heoe22bee72010-06-29 10:07:14 +02002332 *
2333 * Workqueue rescuer thread function. There's one rescuer for each
Tejun Heo493008a2013-03-12 11:30:03 -07002334 * workqueue which has WQ_MEM_RECLAIM set.
Tejun Heoe22bee72010-06-29 10:07:14 +02002335 *
Tejun Heo706026c2013-01-24 11:01:34 -08002336 * Regular work processing on a pool may block trying to create a new
Tejun Heoe22bee72010-06-29 10:07:14 +02002337 * worker which uses GFP_KERNEL allocation which has slight chance of
2338 * developing into deadlock if some works currently on the same queue
2339 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2340 * the problem rescuer solves.
2341 *
Tejun Heo706026c2013-01-24 11:01:34 -08002342 * When such condition is possible, the pool summons rescuers of all
2343 * workqueues which have works queued on the pool and let them process
Tejun Heoe22bee72010-06-29 10:07:14 +02002344 * those works so that forward progress can be guaranteed.
2345 *
2346 * This should happen rarely.
2347 */
Tejun Heo111c2252013-01-17 17:16:24 -08002348static int rescuer_thread(void *__rescuer)
Tejun Heoe22bee72010-06-29 10:07:14 +02002349{
Tejun Heo111c2252013-01-17 17:16:24 -08002350 struct worker *rescuer = __rescuer;
2351 struct workqueue_struct *wq = rescuer->rescue_wq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002352 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heoe22bee72010-06-29 10:07:14 +02002353
2354 set_user_nice(current, RESCUER_NICE_LEVEL);
Tejun Heo111c2252013-01-17 17:16:24 -08002355
2356 /*
2357 * Mark rescuer as worker too. As WORKER_PREP is never cleared, it
2358 * doesn't participate in concurrency management.
2359 */
2360 rescuer->task->flags |= PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002361repeat:
2362 set_current_state(TASK_INTERRUPTIBLE);
2363
Mike Galbraith412d32e2012-11-28 07:17:18 +01002364 if (kthread_should_stop()) {
2365 __set_current_state(TASK_RUNNING);
Tejun Heo111c2252013-01-17 17:16:24 -08002366 rescuer->task->flags &= ~PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002367 return 0;
Mike Galbraith412d32e2012-11-28 07:17:18 +01002368 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002369
Tejun Heo493a1722013-03-12 11:29:59 -07002370 /* see whether any pwq is asking for help */
Tejun Heo2e109a22013-03-13 19:47:40 -07002371 spin_lock_irq(&wq_mayday_lock);
Tejun Heo493a1722013-03-12 11:29:59 -07002372
2373 while (!list_empty(&wq->maydays)) {
2374 struct pool_workqueue *pwq = list_first_entry(&wq->maydays,
2375 struct pool_workqueue, mayday_node);
Tejun Heo112202d2013-02-13 19:29:12 -08002376 struct worker_pool *pool = pwq->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002377 struct work_struct *work, *n;
2378
2379 __set_current_state(TASK_RUNNING);
Tejun Heo493a1722013-03-12 11:29:59 -07002380 list_del_init(&pwq->mayday_node);
2381
Tejun Heo2e109a22013-03-13 19:47:40 -07002382 spin_unlock_irq(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002383
2384 /* migrate to the target cpu if possible */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08002385 worker_maybe_bind_and_lock(pool);
Lai Jiangshanb3104102013-02-19 12:17:02 -08002386 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002387
2388 /*
2389 * Slurp in all works issued via this workqueue and
2390 * process'em.
2391 */
Tejun Heo6183c002013-03-12 11:29:57 -07002392 WARN_ON_ONCE(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002393 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heo112202d2013-02-13 19:29:12 -08002394 if (get_work_pwq(work) == pwq)
Tejun Heoe22bee72010-06-29 10:07:14 +02002395 move_linked_works(work, scheduled, &n);
2396
2397 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002398
2399 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002400 * Leave this pool. If keep_working() is %true, notify a
Tejun Heo75769582011-02-14 14:04:46 +01002401 * regular worker; otherwise, we end up with 0 concurrency
2402 * and stalling the execution.
2403 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002404 if (keep_working(pool))
2405 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002406
Lai Jiangshanb3104102013-02-19 12:17:02 -08002407 rescuer->pool = NULL;
Tejun Heo493a1722013-03-12 11:29:59 -07002408 spin_unlock(&pool->lock);
Tejun Heo2e109a22013-03-13 19:47:40 -07002409 spin_lock(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002410 }
2411
Tejun Heo2e109a22013-03-13 19:47:40 -07002412 spin_unlock_irq(&wq_mayday_lock);
Tejun Heo493a1722013-03-12 11:29:59 -07002413
Tejun Heo111c2252013-01-17 17:16:24 -08002414 /* rescuers should never participate in concurrency management */
2415 WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
Tejun Heoe22bee72010-06-29 10:07:14 +02002416 schedule();
2417 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418}
2419
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002420struct wq_barrier {
2421 struct work_struct work;
2422 struct completion done;
2423};
2424
2425static void wq_barrier_func(struct work_struct *work)
2426{
2427 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2428 complete(&barr->done);
2429}
2430
Tejun Heo4690c4a2010-06-29 10:07:10 +02002431/**
2432 * insert_wq_barrier - insert a barrier work
Tejun Heo112202d2013-02-13 19:29:12 -08002433 * @pwq: pwq to insert barrier into
Tejun Heo4690c4a2010-06-29 10:07:10 +02002434 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002435 * @target: target work to attach @barr to
2436 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002437 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002438 * @barr is linked to @target such that @barr is completed only after
2439 * @target finishes execution. Please note that the ordering
2440 * guarantee is observed only with respect to @target and on the local
2441 * cpu.
2442 *
2443 * Currently, a queued barrier can't be canceled. This is because
2444 * try_to_grab_pending() can't determine whether the work to be
2445 * grabbed is at the head of the queue and thus can't clear LINKED
2446 * flag of the previous work while there must be a valid next work
2447 * after a work with LINKED flag set.
2448 *
2449 * Note that when @worker is non-NULL, @target may be modified
Tejun Heo112202d2013-02-13 19:29:12 -08002450 * underneath us, so we can't reliably determine pwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002451 *
2452 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002453 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002454 */
Tejun Heo112202d2013-02-13 19:29:12 -08002455static void insert_wq_barrier(struct pool_workqueue *pwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002456 struct wq_barrier *barr,
2457 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002458{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002459 struct list_head *head;
2460 unsigned int linked = 0;
2461
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002462 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002463 * debugobject calls are safe here even with pool->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002464 * as we know for sure that this will not trigger any of the
2465 * checks and call back into the fixup functions where we
2466 * might deadlock.
2467 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002468 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002469 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002470 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002471
Tejun Heoaffee4b2010-06-29 10:07:12 +02002472 /*
2473 * If @target is currently being executed, schedule the
2474 * barrier to the worker; otherwise, put it after @target.
2475 */
2476 if (worker)
2477 head = worker->scheduled.next;
2478 else {
2479 unsigned long *bits = work_data_bits(target);
2480
2481 head = target->entry.next;
2482 /* there can already be other linked works, inherit and set */
2483 linked = *bits & WORK_STRUCT_LINKED;
2484 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2485 }
2486
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002487 debug_work_activate(&barr->work);
Tejun Heo112202d2013-02-13 19:29:12 -08002488 insert_work(pwq, &barr->work, head,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002489 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002490}
2491
Tejun Heo73f53c42010-06-29 10:07:11 +02002492/**
Tejun Heo112202d2013-02-13 19:29:12 -08002493 * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing
Tejun Heo73f53c42010-06-29 10:07:11 +02002494 * @wq: workqueue being flushed
2495 * @flush_color: new flush color, < 0 for no-op
2496 * @work_color: new work color, < 0 for no-op
2497 *
Tejun Heo112202d2013-02-13 19:29:12 -08002498 * Prepare pwqs for workqueue flushing.
Tejun Heo73f53c42010-06-29 10:07:11 +02002499 *
Tejun Heo112202d2013-02-13 19:29:12 -08002500 * If @flush_color is non-negative, flush_color on all pwqs should be
2501 * -1. If no pwq has in-flight commands at the specified color, all
2502 * pwq->flush_color's stay at -1 and %false is returned. If any pwq
2503 * has in flight commands, its pwq->flush_color is set to
2504 * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq
Tejun Heo73f53c42010-06-29 10:07:11 +02002505 * wakeup logic is armed and %true is returned.
2506 *
2507 * The caller should have initialized @wq->first_flusher prior to
2508 * calling this function with non-negative @flush_color. If
2509 * @flush_color is negative, no flush color update is done and %false
2510 * is returned.
2511 *
Tejun Heo112202d2013-02-13 19:29:12 -08002512 * If @work_color is non-negative, all pwqs should have the same
Tejun Heo73f53c42010-06-29 10:07:11 +02002513 * work_color which is previous to @work_color and all will be
2514 * advanced to @work_color.
2515 *
2516 * CONTEXT:
2517 * mutex_lock(wq->flush_mutex).
2518 *
2519 * RETURNS:
2520 * %true if @flush_color >= 0 and there's something to flush. %false
2521 * otherwise.
2522 */
Tejun Heo112202d2013-02-13 19:29:12 -08002523static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
Tejun Heo73f53c42010-06-29 10:07:11 +02002524 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525{
Tejun Heo73f53c42010-06-29 10:07:11 +02002526 bool wait = false;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002527 struct pool_workqueue *pwq;
Oleg Nesterov14441962007-05-23 13:57:57 -07002528
Tejun Heo73f53c42010-06-29 10:07:11 +02002529 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002530 WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush));
Tejun Heo112202d2013-02-13 19:29:12 -08002531 atomic_set(&wq->nr_pwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002532 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002533
Tejun Heo76af4d92013-03-12 11:30:00 -07002534 local_irq_disable();
2535
Tejun Heo49e3cf42013-03-12 11:29:58 -07002536 for_each_pwq(pwq, wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08002537 struct worker_pool *pool = pwq->pool;
Tejun Heo73f53c42010-06-29 10:07:11 +02002538
Tejun Heo76af4d92013-03-12 11:30:00 -07002539 spin_lock(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002540
2541 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002542 WARN_ON_ONCE(pwq->flush_color != -1);
Tejun Heo73f53c42010-06-29 10:07:11 +02002543
Tejun Heo112202d2013-02-13 19:29:12 -08002544 if (pwq->nr_in_flight[flush_color]) {
2545 pwq->flush_color = flush_color;
2546 atomic_inc(&wq->nr_pwqs_to_flush);
Tejun Heo73f53c42010-06-29 10:07:11 +02002547 wait = true;
2548 }
2549 }
2550
2551 if (work_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002552 WARN_ON_ONCE(work_color != work_next_color(pwq->work_color));
Tejun Heo112202d2013-02-13 19:29:12 -08002553 pwq->work_color = work_color;
Tejun Heo73f53c42010-06-29 10:07:11 +02002554 }
2555
Tejun Heo76af4d92013-03-12 11:30:00 -07002556 spin_unlock(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002557 }
2558
Tejun Heo76af4d92013-03-12 11:30:00 -07002559 local_irq_enable();
2560
Tejun Heo112202d2013-02-13 19:29:12 -08002561 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush))
Tejun Heo73f53c42010-06-29 10:07:11 +02002562 complete(&wq->first_flusher->done);
2563
2564 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565}
2566
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002567/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002569 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002571 * This function sleeps until all work items which were queued on entry
2572 * have finished execution, but it is not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002574void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575{
Tejun Heo73f53c42010-06-29 10:07:11 +02002576 struct wq_flusher this_flusher = {
2577 .list = LIST_HEAD_INIT(this_flusher.list),
2578 .flush_color = -1,
2579 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2580 };
2581 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002582
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002583 lock_map_acquire(&wq->lockdep_map);
2584 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002585
2586 mutex_lock(&wq->flush_mutex);
2587
2588 /*
2589 * Start-to-wait phase
2590 */
2591 next_color = work_next_color(wq->work_color);
2592
2593 if (next_color != wq->flush_color) {
2594 /*
2595 * Color space is not full. The current work_color
2596 * becomes our flush_color and work_color is advanced
2597 * by one.
2598 */
Tejun Heo6183c002013-03-12 11:29:57 -07002599 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow));
Tejun Heo73f53c42010-06-29 10:07:11 +02002600 this_flusher.flush_color = wq->work_color;
2601 wq->work_color = next_color;
2602
2603 if (!wq->first_flusher) {
2604 /* no flush in progress, become the first flusher */
Tejun Heo6183c002013-03-12 11:29:57 -07002605 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002606
2607 wq->first_flusher = &this_flusher;
2608
Tejun Heo112202d2013-02-13 19:29:12 -08002609 if (!flush_workqueue_prep_pwqs(wq, wq->flush_color,
Tejun Heo73f53c42010-06-29 10:07:11 +02002610 wq->work_color)) {
2611 /* nothing to flush, done */
2612 wq->flush_color = next_color;
2613 wq->first_flusher = NULL;
2614 goto out_unlock;
2615 }
2616 } else {
2617 /* wait in queue */
Tejun Heo6183c002013-03-12 11:29:57 -07002618 WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002619 list_add_tail(&this_flusher.list, &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002620 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002621 }
2622 } else {
2623 /*
2624 * Oops, color space is full, wait on overflow queue.
2625 * The next flush completion will assign us
2626 * flush_color and transfer to flusher_queue.
2627 */
2628 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2629 }
2630
2631 mutex_unlock(&wq->flush_mutex);
2632
2633 wait_for_completion(&this_flusher.done);
2634
2635 /*
2636 * Wake-up-and-cascade phase
2637 *
2638 * First flushers are responsible for cascading flushes and
2639 * handling overflow. Non-first flushers can simply return.
2640 */
2641 if (wq->first_flusher != &this_flusher)
2642 return;
2643
2644 mutex_lock(&wq->flush_mutex);
2645
Tejun Heo4ce48b32010-07-02 10:03:51 +02002646 /* we might have raced, check again with mutex held */
2647 if (wq->first_flusher != &this_flusher)
2648 goto out_unlock;
2649
Tejun Heo73f53c42010-06-29 10:07:11 +02002650 wq->first_flusher = NULL;
2651
Tejun Heo6183c002013-03-12 11:29:57 -07002652 WARN_ON_ONCE(!list_empty(&this_flusher.list));
2653 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002654
2655 while (true) {
2656 struct wq_flusher *next, *tmp;
2657
2658 /* complete all the flushers sharing the current flush color */
2659 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2660 if (next->flush_color != wq->flush_color)
2661 break;
2662 list_del_init(&next->list);
2663 complete(&next->done);
2664 }
2665
Tejun Heo6183c002013-03-12 11:29:57 -07002666 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) &&
2667 wq->flush_color != work_next_color(wq->work_color));
Tejun Heo73f53c42010-06-29 10:07:11 +02002668
2669 /* this flush_color is finished, advance by one */
2670 wq->flush_color = work_next_color(wq->flush_color);
2671
2672 /* one color has been freed, handle overflow queue */
2673 if (!list_empty(&wq->flusher_overflow)) {
2674 /*
2675 * Assign the same color to all overflowed
2676 * flushers, advance work_color and append to
2677 * flusher_queue. This is the start-to-wait
2678 * phase for these overflowed flushers.
2679 */
2680 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2681 tmp->flush_color = wq->work_color;
2682
2683 wq->work_color = work_next_color(wq->work_color);
2684
2685 list_splice_tail_init(&wq->flusher_overflow,
2686 &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002687 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002688 }
2689
2690 if (list_empty(&wq->flusher_queue)) {
Tejun Heo6183c002013-03-12 11:29:57 -07002691 WARN_ON_ONCE(wq->flush_color != wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002692 break;
2693 }
2694
2695 /*
2696 * Need to flush more colors. Make the next flusher
Tejun Heo112202d2013-02-13 19:29:12 -08002697 * the new first flusher and arm pwqs.
Tejun Heo73f53c42010-06-29 10:07:11 +02002698 */
Tejun Heo6183c002013-03-12 11:29:57 -07002699 WARN_ON_ONCE(wq->flush_color == wq->work_color);
2700 WARN_ON_ONCE(wq->flush_color != next->flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002701
2702 list_del_init(&next->list);
2703 wq->first_flusher = next;
2704
Tejun Heo112202d2013-02-13 19:29:12 -08002705 if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1))
Tejun Heo73f53c42010-06-29 10:07:11 +02002706 break;
2707
2708 /*
2709 * Meh... this color is already done, clear first
2710 * flusher and repeat cascading.
2711 */
2712 wq->first_flusher = NULL;
2713 }
2714
2715out_unlock:
2716 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717}
Dave Jonesae90dd52006-06-30 01:40:45 -04002718EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002720/**
2721 * drain_workqueue - drain a workqueue
2722 * @wq: workqueue to drain
2723 *
2724 * Wait until the workqueue becomes empty. While draining is in progress,
2725 * only chain queueing is allowed. IOW, only currently pending or running
2726 * work items on @wq can queue further work items on it. @wq is flushed
2727 * repeatedly until it becomes empty. The number of flushing is detemined
2728 * by the depth of chaining and should be relatively short. Whine if it
2729 * takes too long.
2730 */
2731void drain_workqueue(struct workqueue_struct *wq)
2732{
2733 unsigned int flush_cnt = 0;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002734 struct pool_workqueue *pwq;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002735
2736 /*
2737 * __queue_work() needs to test whether there are drainers, is much
2738 * hotter than drain_workqueue() and already looks at @wq->flags.
Tejun Heo618b01e2013-03-12 11:30:04 -07002739 * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers.
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002740 */
Tejun Heo5bcab332013-03-13 19:47:40 -07002741 mutex_lock(&wq_mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002742 if (!wq->nr_drainers++)
Tejun Heo618b01e2013-03-12 11:30:04 -07002743 wq->flags |= __WQ_DRAINING;
Tejun Heo5bcab332013-03-13 19:47:40 -07002744 mutex_unlock(&wq_mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002745reflush:
2746 flush_workqueue(wq);
2747
Tejun Heo76af4d92013-03-12 11:30:00 -07002748 local_irq_disable();
2749
Tejun Heo49e3cf42013-03-12 11:29:58 -07002750 for_each_pwq(pwq, wq) {
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002751 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002752
Tejun Heo76af4d92013-03-12 11:30:00 -07002753 spin_lock(&pwq->pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08002754 drained = !pwq->nr_active && list_empty(&pwq->delayed_works);
Tejun Heo76af4d92013-03-12 11:30:00 -07002755 spin_unlock(&pwq->pool->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002756
2757 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002758 continue;
2759
2760 if (++flush_cnt == 10 ||
2761 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002762 pr_warn("workqueue %s: drain_workqueue() isn't complete after %u tries\n",
Valentin Ilie044c7822012-08-19 00:52:42 +03002763 wq->name, flush_cnt);
Tejun Heo76af4d92013-03-12 11:30:00 -07002764
2765 local_irq_enable();
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002766 goto reflush;
2767 }
2768
Tejun Heo5bcab332013-03-13 19:47:40 -07002769 local_irq_enable();
2770
2771 mutex_lock(&wq_mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002772 if (!--wq->nr_drainers)
Tejun Heo618b01e2013-03-12 11:30:04 -07002773 wq->flags &= ~__WQ_DRAINING;
Tejun Heo5bcab332013-03-13 19:47:40 -07002774 mutex_unlock(&wq_mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002775}
2776EXPORT_SYMBOL_GPL(drain_workqueue);
2777
Tejun Heo606a5022012-08-20 14:51:23 -07002778static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
Tejun Heobaf59022010-09-16 10:42:16 +02002779{
2780 struct worker *worker = NULL;
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002781 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002782 struct pool_workqueue *pwq;
Tejun Heobaf59022010-09-16 10:42:16 +02002783
2784 might_sleep();
Tejun Heobaf59022010-09-16 10:42:16 +02002785
Tejun Heofa1b54e2013-03-12 11:30:00 -07002786 local_irq_disable();
2787 pool = get_work_pool(work);
2788 if (!pool) {
2789 local_irq_enable();
2790 return false;
2791 }
2792
2793 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08002794 /* see the comment in try_to_grab_pending() with the same code */
Tejun Heo112202d2013-02-13 19:29:12 -08002795 pwq = get_work_pwq(work);
2796 if (pwq) {
2797 if (unlikely(pwq->pool != pool))
Tejun Heobaf59022010-09-16 10:42:16 +02002798 goto already_gone;
Tejun Heo606a5022012-08-20 14:51:23 -07002799 } else {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002800 worker = find_worker_executing_work(pool, work);
Tejun Heobaf59022010-09-16 10:42:16 +02002801 if (!worker)
2802 goto already_gone;
Tejun Heo112202d2013-02-13 19:29:12 -08002803 pwq = worker->current_pwq;
Tejun Heo606a5022012-08-20 14:51:23 -07002804 }
Tejun Heobaf59022010-09-16 10:42:16 +02002805
Tejun Heo112202d2013-02-13 19:29:12 -08002806 insert_wq_barrier(pwq, barr, work, worker);
Tejun Heod565ed62013-01-24 11:01:33 -08002807 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002808
Tejun Heoe1594892011-01-09 23:32:15 +01002809 /*
2810 * If @max_active is 1 or rescuer is in use, flushing another work
2811 * item on the same workqueue may lead to deadlock. Make sure the
2812 * flusher is not running on the same workqueue by verifying write
2813 * access.
2814 */
Tejun Heo493008a2013-03-12 11:30:03 -07002815 if (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)
Tejun Heo112202d2013-02-13 19:29:12 -08002816 lock_map_acquire(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002817 else
Tejun Heo112202d2013-02-13 19:29:12 -08002818 lock_map_acquire_read(&pwq->wq->lockdep_map);
2819 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002820
Tejun Heobaf59022010-09-16 10:42:16 +02002821 return true;
2822already_gone:
Tejun Heod565ed62013-01-24 11:01:33 -08002823 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002824 return false;
2825}
2826
Oleg Nesterovdb700892008-07-25 01:47:49 -07002827/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002828 * flush_work - wait for a work to finish executing the last queueing instance
2829 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002830 *
Tejun Heo606a5022012-08-20 14:51:23 -07002831 * Wait until @work has finished execution. @work is guaranteed to be idle
2832 * on return if it hasn't been requeued since flush started.
Tejun Heo401a8d02010-09-16 10:36:00 +02002833 *
2834 * RETURNS:
2835 * %true if flush_work() waited for the work to finish execution,
2836 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002837 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002838bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002839{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002840 struct wq_barrier barr;
2841
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002842 lock_map_acquire(&work->lockdep_map);
2843 lock_map_release(&work->lockdep_map);
2844
Tejun Heo606a5022012-08-20 14:51:23 -07002845 if (start_flush_work(work, &barr)) {
Tejun Heobaf59022010-09-16 10:42:16 +02002846 wait_for_completion(&barr.done);
2847 destroy_work_on_stack(&barr.work);
2848 return true;
Tejun Heo606a5022012-08-20 14:51:23 -07002849 } else {
Tejun Heobaf59022010-09-16 10:42:16 +02002850 return false;
Tejun Heo606a5022012-08-20 14:51:23 -07002851 }
Oleg Nesterovdb700892008-07-25 01:47:49 -07002852}
2853EXPORT_SYMBOL_GPL(flush_work);
2854
Tejun Heo36e227d2012-08-03 10:30:46 -07002855static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
Tejun Heo401a8d02010-09-16 10:36:00 +02002856{
Tejun Heobbb68df2012-08-03 10:30:46 -07002857 unsigned long flags;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002858 int ret;
2859
2860 do {
Tejun Heobbb68df2012-08-03 10:30:46 -07002861 ret = try_to_grab_pending(work, is_dwork, &flags);
2862 /*
2863 * If someone else is canceling, wait for the same event it
2864 * would be waiting for before retrying.
2865 */
2866 if (unlikely(ret == -ENOENT))
Tejun Heo606a5022012-08-20 14:51:23 -07002867 flush_work(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002868 } while (unlikely(ret < 0));
2869
Tejun Heobbb68df2012-08-03 10:30:46 -07002870 /* tell other tasks trying to grab @work to back off */
2871 mark_work_canceling(work);
2872 local_irq_restore(flags);
2873
Tejun Heo606a5022012-08-20 14:51:23 -07002874 flush_work(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002875 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002876 return ret;
2877}
2878
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002879/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002880 * cancel_work_sync - cancel a work and wait for it to finish
2881 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002882 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002883 * Cancel @work and wait for its execution to finish. This function
2884 * can be used even if the work re-queues itself or migrates to
2885 * another workqueue. On return from this function, @work is
2886 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002887 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002888 * cancel_work_sync(&delayed_work->work) must not be used for
2889 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002890 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002891 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002892 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002893 *
2894 * RETURNS:
2895 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002896 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002897bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002898{
Tejun Heo36e227d2012-08-03 10:30:46 -07002899 return __cancel_work_timer(work, false);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002900}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002901EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002902
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002903/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002904 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2905 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002906 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002907 * Delayed timer is cancelled and the pending work is queued for
2908 * immediate execution. Like flush_work(), this function only
2909 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002910 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002911 * RETURNS:
2912 * %true if flush_work() waited for the work to finish execution,
2913 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002914 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002915bool flush_delayed_work(struct delayed_work *dwork)
2916{
Tejun Heo8930cab2012-08-03 10:30:45 -07002917 local_irq_disable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002918 if (del_timer_sync(&dwork->timer))
Lai Jiangshan60c057b2013-02-06 18:04:53 -08002919 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Tejun Heo8930cab2012-08-03 10:30:45 -07002920 local_irq_enable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002921 return flush_work(&dwork->work);
2922}
2923EXPORT_SYMBOL(flush_delayed_work);
2924
2925/**
Tejun Heo57b30ae2012-08-21 13:18:24 -07002926 * cancel_delayed_work - cancel a delayed work
2927 * @dwork: delayed_work to cancel
Tejun Heo09383492010-09-16 10:48:29 +02002928 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002929 * Kill off a pending delayed_work. Returns %true if @dwork was pending
2930 * and canceled; %false if wasn't pending. Note that the work callback
2931 * function may still be running on return, unless it returns %true and the
2932 * work doesn't re-arm itself. Explicitly flush or use
2933 * cancel_delayed_work_sync() to wait on it.
Tejun Heo09383492010-09-16 10:48:29 +02002934 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002935 * This function is safe to call from any context including IRQ handler.
Tejun Heo09383492010-09-16 10:48:29 +02002936 */
Tejun Heo57b30ae2012-08-21 13:18:24 -07002937bool cancel_delayed_work(struct delayed_work *dwork)
Tejun Heo09383492010-09-16 10:48:29 +02002938{
Tejun Heo57b30ae2012-08-21 13:18:24 -07002939 unsigned long flags;
2940 int ret;
2941
2942 do {
2943 ret = try_to_grab_pending(&dwork->work, true, &flags);
2944 } while (unlikely(ret == -EAGAIN));
2945
2946 if (unlikely(ret < 0))
2947 return false;
2948
Tejun Heo7c3eed52013-01-24 11:01:33 -08002949 set_work_pool_and_clear_pending(&dwork->work,
2950 get_work_pool_id(&dwork->work));
Tejun Heo57b30ae2012-08-21 13:18:24 -07002951 local_irq_restore(flags);
Dan Magenheimerc0158ca2012-10-18 16:31:37 -07002952 return ret;
Tejun Heo09383492010-09-16 10:48:29 +02002953}
Tejun Heo57b30ae2012-08-21 13:18:24 -07002954EXPORT_SYMBOL(cancel_delayed_work);
Tejun Heo09383492010-09-16 10:48:29 +02002955
2956/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002957 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2958 * @dwork: the delayed work cancel
2959 *
2960 * This is cancel_work_sync() for delayed works.
2961 *
2962 * RETURNS:
2963 * %true if @dwork was pending, %false otherwise.
2964 */
2965bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002966{
Tejun Heo36e227d2012-08-03 10:30:46 -07002967 return __cancel_work_timer(&dwork->work, true);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002968}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002969EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002971/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002972 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002973 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002974 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002975 * schedule_on_each_cpu() executes @func on each online CPU using the
2976 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002977 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002978 *
2979 * RETURNS:
2980 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002981 */
David Howells65f27f32006-11-22 14:55:48 +00002982int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002983{
2984 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002985 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002986
Andrew Mortonb6136772006-06-25 05:47:49 -07002987 works = alloc_percpu(struct work_struct);
2988 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002989 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002990
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002991 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002992
Christoph Lameter15316ba2006-01-08 01:00:43 -08002993 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002994 struct work_struct *work = per_cpu_ptr(works, cpu);
2995
2996 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002997 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002998 }
Tejun Heo93981802009-11-17 14:06:20 -08002999
3000 for_each_online_cpu(cpu)
3001 flush_work(per_cpu_ptr(works, cpu));
3002
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003003 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07003004 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08003005 return 0;
3006}
3007
Alan Sterneef6a7d2010-02-12 17:39:21 +09003008/**
3009 * flush_scheduled_work - ensure that any scheduled work has run to completion.
3010 *
3011 * Forces execution of the kernel-global workqueue and blocks until its
3012 * completion.
3013 *
3014 * Think twice before calling this function! It's very easy to get into
3015 * trouble if you don't take great care. Either of the following situations
3016 * will lead to deadlock:
3017 *
3018 * One of the work items currently on the workqueue needs to acquire
3019 * a lock held by your code or its caller.
3020 *
3021 * Your code is running in the context of a work routine.
3022 *
3023 * They will be detected by lockdep when they occur, but the first might not
3024 * occur very often. It depends on what work items are on the workqueue and
3025 * what locks they need, which you have no control over.
3026 *
3027 * In most situations flushing the entire workqueue is overkill; you merely
3028 * need to know that a particular work item isn't queued and isn't running.
3029 * In such cases you should use cancel_delayed_work_sync() or
3030 * cancel_work_sync() instead.
3031 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032void flush_scheduled_work(void)
3033{
Tejun Heod320c032010-06-29 10:07:14 +02003034 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035}
Dave Jonesae90dd52006-06-30 01:40:45 -04003036EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
3038/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06003039 * execute_in_process_context - reliably execute the routine with user context
3040 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06003041 * @ew: guaranteed storage for the execute work structure (must
3042 * be available when the work executes)
3043 *
3044 * Executes the function immediately if process context is available,
3045 * otherwise schedules the function for delayed execution.
3046 *
3047 * Returns: 0 - function was executed
3048 * 1 - function was scheduled for execution
3049 */
David Howells65f27f32006-11-22 14:55:48 +00003050int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06003051{
3052 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00003053 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003054 return 0;
3055 }
3056
David Howells65f27f32006-11-22 14:55:48 +00003057 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003058 schedule_work(&ew->work);
3059
3060 return 1;
3061}
3062EXPORT_SYMBOL_GPL(execute_in_process_context);
3063
Tejun Heo226223a2013-03-12 11:30:05 -07003064#ifdef CONFIG_SYSFS
3065/*
3066 * Workqueues with WQ_SYSFS flag set is visible to userland via
3067 * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the
3068 * following attributes.
3069 *
3070 * per_cpu RO bool : whether the workqueue is per-cpu or unbound
3071 * max_active RW int : maximum number of in-flight work items
3072 *
3073 * Unbound workqueues have the following extra attributes.
3074 *
3075 * id RO int : the associated pool ID
3076 * nice RW int : nice value of the workers
3077 * cpumask RW mask : bitmask of allowed CPUs for the workers
3078 */
3079struct wq_device {
3080 struct workqueue_struct *wq;
3081 struct device dev;
3082};
3083
3084static struct workqueue_struct *dev_to_wq(struct device *dev)
3085{
3086 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
3087
3088 return wq_dev->wq;
3089}
3090
3091static ssize_t wq_per_cpu_show(struct device *dev,
3092 struct device_attribute *attr, char *buf)
3093{
3094 struct workqueue_struct *wq = dev_to_wq(dev);
3095
3096 return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND));
3097}
3098
3099static ssize_t wq_max_active_show(struct device *dev,
3100 struct device_attribute *attr, char *buf)
3101{
3102 struct workqueue_struct *wq = dev_to_wq(dev);
3103
3104 return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active);
3105}
3106
3107static ssize_t wq_max_active_store(struct device *dev,
3108 struct device_attribute *attr,
3109 const char *buf, size_t count)
3110{
3111 struct workqueue_struct *wq = dev_to_wq(dev);
3112 int val;
3113
3114 if (sscanf(buf, "%d", &val) != 1 || val <= 0)
3115 return -EINVAL;
3116
3117 workqueue_set_max_active(wq, val);
3118 return count;
3119}
3120
3121static struct device_attribute wq_sysfs_attrs[] = {
3122 __ATTR(per_cpu, 0444, wq_per_cpu_show, NULL),
3123 __ATTR(max_active, 0644, wq_max_active_show, wq_max_active_store),
3124 __ATTR_NULL,
3125};
3126
3127static ssize_t wq_pool_id_show(struct device *dev,
3128 struct device_attribute *attr, char *buf)
3129{
3130 struct workqueue_struct *wq = dev_to_wq(dev);
3131 struct worker_pool *pool;
3132 int written;
3133
3134 rcu_read_lock_sched();
3135 pool = first_pwq(wq)->pool;
3136 written = scnprintf(buf, PAGE_SIZE, "%d\n", pool->id);
3137 rcu_read_unlock_sched();
3138
3139 return written;
3140}
3141
3142static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr,
3143 char *buf)
3144{
3145 struct workqueue_struct *wq = dev_to_wq(dev);
3146 int written;
3147
3148 rcu_read_lock_sched();
3149 written = scnprintf(buf, PAGE_SIZE, "%d\n",
3150 first_pwq(wq)->pool->attrs->nice);
3151 rcu_read_unlock_sched();
3152
3153 return written;
3154}
3155
3156/* prepare workqueue_attrs for sysfs store operations */
3157static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq)
3158{
3159 struct workqueue_attrs *attrs;
3160
3161 attrs = alloc_workqueue_attrs(GFP_KERNEL);
3162 if (!attrs)
3163 return NULL;
3164
3165 rcu_read_lock_sched();
3166 copy_workqueue_attrs(attrs, first_pwq(wq)->pool->attrs);
3167 rcu_read_unlock_sched();
3168 return attrs;
3169}
3170
3171static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr,
3172 const char *buf, size_t count)
3173{
3174 struct workqueue_struct *wq = dev_to_wq(dev);
3175 struct workqueue_attrs *attrs;
3176 int ret;
3177
3178 attrs = wq_sysfs_prep_attrs(wq);
3179 if (!attrs)
3180 return -ENOMEM;
3181
3182 if (sscanf(buf, "%d", &attrs->nice) == 1 &&
3183 attrs->nice >= -20 && attrs->nice <= 19)
3184 ret = apply_workqueue_attrs(wq, attrs);
3185 else
3186 ret = -EINVAL;
3187
3188 free_workqueue_attrs(attrs);
3189 return ret ?: count;
3190}
3191
3192static ssize_t wq_cpumask_show(struct device *dev,
3193 struct device_attribute *attr, char *buf)
3194{
3195 struct workqueue_struct *wq = dev_to_wq(dev);
3196 int written;
3197
3198 rcu_read_lock_sched();
3199 written = cpumask_scnprintf(buf, PAGE_SIZE,
3200 first_pwq(wq)->pool->attrs->cpumask);
3201 rcu_read_unlock_sched();
3202
3203 written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
3204 return written;
3205}
3206
3207static ssize_t wq_cpumask_store(struct device *dev,
3208 struct device_attribute *attr,
3209 const char *buf, size_t count)
3210{
3211 struct workqueue_struct *wq = dev_to_wq(dev);
3212 struct workqueue_attrs *attrs;
3213 int ret;
3214
3215 attrs = wq_sysfs_prep_attrs(wq);
3216 if (!attrs)
3217 return -ENOMEM;
3218
3219 ret = cpumask_parse(buf, attrs->cpumask);
3220 if (!ret)
3221 ret = apply_workqueue_attrs(wq, attrs);
3222
3223 free_workqueue_attrs(attrs);
3224 return ret ?: count;
3225}
3226
3227static struct device_attribute wq_sysfs_unbound_attrs[] = {
3228 __ATTR(pool_id, 0444, wq_pool_id_show, NULL),
3229 __ATTR(nice, 0644, wq_nice_show, wq_nice_store),
3230 __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store),
3231 __ATTR_NULL,
3232};
3233
3234static struct bus_type wq_subsys = {
3235 .name = "workqueue",
3236 .dev_attrs = wq_sysfs_attrs,
3237};
3238
3239static int __init wq_sysfs_init(void)
3240{
3241 return subsys_virtual_register(&wq_subsys, NULL);
3242}
3243core_initcall(wq_sysfs_init);
3244
3245static void wq_device_release(struct device *dev)
3246{
3247 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
3248
3249 kfree(wq_dev);
3250}
3251
3252/**
3253 * workqueue_sysfs_register - make a workqueue visible in sysfs
3254 * @wq: the workqueue to register
3255 *
3256 * Expose @wq in sysfs under /sys/bus/workqueue/devices.
3257 * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set
3258 * which is the preferred method.
3259 *
3260 * Workqueue user should use this function directly iff it wants to apply
3261 * workqueue_attrs before making the workqueue visible in sysfs; otherwise,
3262 * apply_workqueue_attrs() may race against userland updating the
3263 * attributes.
3264 *
3265 * Returns 0 on success, -errno on failure.
3266 */
3267int workqueue_sysfs_register(struct workqueue_struct *wq)
3268{
3269 struct wq_device *wq_dev;
3270 int ret;
3271
3272 /*
3273 * Adjusting max_active or creating new pwqs by applyting
3274 * attributes breaks ordering guarantee. Disallow exposing ordered
3275 * workqueues.
3276 */
3277 if (WARN_ON(wq->flags & __WQ_ORDERED))
3278 return -EINVAL;
3279
3280 wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL);
3281 if (!wq_dev)
3282 return -ENOMEM;
3283
3284 wq_dev->wq = wq;
3285 wq_dev->dev.bus = &wq_subsys;
3286 wq_dev->dev.init_name = wq->name;
3287 wq_dev->dev.release = wq_device_release;
3288
3289 /*
3290 * unbound_attrs are created separately. Suppress uevent until
3291 * everything is ready.
3292 */
3293 dev_set_uevent_suppress(&wq_dev->dev, true);
3294
3295 ret = device_register(&wq_dev->dev);
3296 if (ret) {
3297 kfree(wq_dev);
3298 wq->wq_dev = NULL;
3299 return ret;
3300 }
3301
3302 if (wq->flags & WQ_UNBOUND) {
3303 struct device_attribute *attr;
3304
3305 for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) {
3306 ret = device_create_file(&wq_dev->dev, attr);
3307 if (ret) {
3308 device_unregister(&wq_dev->dev);
3309 wq->wq_dev = NULL;
3310 return ret;
3311 }
3312 }
3313 }
3314
3315 kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD);
3316 return 0;
3317}
3318
3319/**
3320 * workqueue_sysfs_unregister - undo workqueue_sysfs_register()
3321 * @wq: the workqueue to unregister
3322 *
3323 * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister.
3324 */
3325static void workqueue_sysfs_unregister(struct workqueue_struct *wq)
3326{
3327 struct wq_device *wq_dev = wq->wq_dev;
3328
3329 if (!wq->wq_dev)
3330 return;
3331
3332 wq->wq_dev = NULL;
3333 device_unregister(&wq_dev->dev);
3334}
3335#else /* CONFIG_SYSFS */
3336static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { }
3337#endif /* CONFIG_SYSFS */
3338
Tejun Heo7a4e3442013-03-12 11:30:00 -07003339/**
3340 * free_workqueue_attrs - free a workqueue_attrs
3341 * @attrs: workqueue_attrs to free
3342 *
3343 * Undo alloc_workqueue_attrs().
3344 */
3345void free_workqueue_attrs(struct workqueue_attrs *attrs)
3346{
3347 if (attrs) {
3348 free_cpumask_var(attrs->cpumask);
3349 kfree(attrs);
3350 }
3351}
3352
3353/**
3354 * alloc_workqueue_attrs - allocate a workqueue_attrs
3355 * @gfp_mask: allocation mask to use
3356 *
3357 * Allocate a new workqueue_attrs, initialize with default settings and
3358 * return it. Returns NULL on failure.
3359 */
3360struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask)
3361{
3362 struct workqueue_attrs *attrs;
3363
3364 attrs = kzalloc(sizeof(*attrs), gfp_mask);
3365 if (!attrs)
3366 goto fail;
3367 if (!alloc_cpumask_var(&attrs->cpumask, gfp_mask))
3368 goto fail;
3369
3370 cpumask_setall(attrs->cpumask);
3371 return attrs;
3372fail:
3373 free_workqueue_attrs(attrs);
3374 return NULL;
3375}
3376
Tejun Heo29c91e92013-03-12 11:30:03 -07003377static void copy_workqueue_attrs(struct workqueue_attrs *to,
3378 const struct workqueue_attrs *from)
3379{
3380 to->nice = from->nice;
3381 cpumask_copy(to->cpumask, from->cpumask);
3382}
3383
3384/*
3385 * Hacky implementation of jhash of bitmaps which only considers the
3386 * specified number of bits. We probably want a proper implementation in
3387 * include/linux/jhash.h.
3388 */
3389static u32 jhash_bitmap(const unsigned long *bitmap, int bits, u32 hash)
3390{
3391 int nr_longs = bits / BITS_PER_LONG;
3392 int nr_leftover = bits % BITS_PER_LONG;
3393 unsigned long leftover = 0;
3394
3395 if (nr_longs)
3396 hash = jhash(bitmap, nr_longs * sizeof(long), hash);
3397 if (nr_leftover) {
3398 bitmap_copy(&leftover, bitmap + nr_longs, nr_leftover);
3399 hash = jhash(&leftover, sizeof(long), hash);
3400 }
3401 return hash;
3402}
3403
3404/* hash value of the content of @attr */
3405static u32 wqattrs_hash(const struct workqueue_attrs *attrs)
3406{
3407 u32 hash = 0;
3408
3409 hash = jhash_1word(attrs->nice, hash);
3410 hash = jhash_bitmap(cpumask_bits(attrs->cpumask), nr_cpu_ids, hash);
3411 return hash;
3412}
3413
3414/* content equality test */
3415static bool wqattrs_equal(const struct workqueue_attrs *a,
3416 const struct workqueue_attrs *b)
3417{
3418 if (a->nice != b->nice)
3419 return false;
3420 if (!cpumask_equal(a->cpumask, b->cpumask))
3421 return false;
3422 return true;
3423}
3424
Tejun Heo7a4e3442013-03-12 11:30:00 -07003425/**
3426 * init_worker_pool - initialize a newly zalloc'd worker_pool
3427 * @pool: worker_pool to initialize
3428 *
3429 * Initiailize a newly zalloc'd @pool. It also allocates @pool->attrs.
Tejun Heo29c91e92013-03-12 11:30:03 -07003430 * Returns 0 on success, -errno on failure. Even on failure, all fields
3431 * inside @pool proper are initialized and put_unbound_pool() can be called
3432 * on @pool safely to release it.
Tejun Heo7a4e3442013-03-12 11:30:00 -07003433 */
3434static int init_worker_pool(struct worker_pool *pool)
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003435{
3436 spin_lock_init(&pool->lock);
Tejun Heo29c91e92013-03-12 11:30:03 -07003437 pool->id = -1;
3438 pool->cpu = -1;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003439 pool->flags |= POOL_DISASSOCIATED;
3440 INIT_LIST_HEAD(&pool->worklist);
3441 INIT_LIST_HEAD(&pool->idle_list);
3442 hash_init(pool->busy_hash);
3443
3444 init_timer_deferrable(&pool->idle_timer);
3445 pool->idle_timer.function = idle_worker_timeout;
3446 pool->idle_timer.data = (unsigned long)pool;
3447
3448 setup_timer(&pool->mayday_timer, pool_mayday_timeout,
3449 (unsigned long)pool);
3450
3451 mutex_init(&pool->manager_arb);
Tejun Heobc3a1af2013-03-13 19:47:39 -07003452 mutex_init(&pool->manager_mutex);
Tejun Heo822d8402013-03-19 13:45:21 -07003453 idr_init(&pool->worker_idr);
Tejun Heo7a4e3442013-03-12 11:30:00 -07003454
Tejun Heo29c91e92013-03-12 11:30:03 -07003455 INIT_HLIST_NODE(&pool->hash_node);
3456 pool->refcnt = 1;
3457
3458 /* shouldn't fail above this point */
Tejun Heo7a4e3442013-03-12 11:30:00 -07003459 pool->attrs = alloc_workqueue_attrs(GFP_KERNEL);
3460 if (!pool->attrs)
3461 return -ENOMEM;
3462 return 0;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003463}
3464
Tejun Heo29c91e92013-03-12 11:30:03 -07003465static void rcu_free_pool(struct rcu_head *rcu)
3466{
3467 struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu);
3468
Tejun Heo822d8402013-03-19 13:45:21 -07003469 idr_destroy(&pool->worker_idr);
Tejun Heo29c91e92013-03-12 11:30:03 -07003470 free_workqueue_attrs(pool->attrs);
3471 kfree(pool);
3472}
3473
3474/**
3475 * put_unbound_pool - put a worker_pool
3476 * @pool: worker_pool to put
3477 *
3478 * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003479 * safe manner. get_unbound_pool() calls this function on its failure path
3480 * and this function should be able to release pools which went through,
3481 * successfully or not, init_worker_pool().
Tejun Heo29c91e92013-03-12 11:30:03 -07003482 */
3483static void put_unbound_pool(struct worker_pool *pool)
3484{
3485 struct worker *worker;
3486
Tejun Heo5bcab332013-03-13 19:47:40 -07003487 mutex_lock(&wq_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003488 if (--pool->refcnt) {
Tejun Heo5bcab332013-03-13 19:47:40 -07003489 mutex_unlock(&wq_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003490 return;
3491 }
3492
3493 /* sanity checks */
3494 if (WARN_ON(!(pool->flags & POOL_DISASSOCIATED)) ||
3495 WARN_ON(!list_empty(&pool->worklist))) {
Tejun Heo5bcab332013-03-13 19:47:40 -07003496 mutex_unlock(&wq_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003497 return;
3498 }
3499
3500 /* release id and unhash */
3501 if (pool->id >= 0)
3502 idr_remove(&worker_pool_idr, pool->id);
3503 hash_del(&pool->hash_node);
3504
Tejun Heo5bcab332013-03-13 19:47:40 -07003505 mutex_unlock(&wq_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003506
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003507 /*
3508 * Become the manager and destroy all workers. Grabbing
3509 * manager_arb prevents @pool's workers from blocking on
3510 * manager_mutex.
3511 */
Tejun Heo29c91e92013-03-12 11:30:03 -07003512 mutex_lock(&pool->manager_arb);
Tejun Heocd549682013-03-13 19:47:39 -07003513 mutex_lock(&pool->manager_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003514 spin_lock_irq(&pool->lock);
3515
3516 while ((worker = first_worker(pool)))
3517 destroy_worker(worker);
3518 WARN_ON(pool->nr_workers || pool->nr_idle);
3519
3520 spin_unlock_irq(&pool->lock);
Tejun Heocd549682013-03-13 19:47:39 -07003521 mutex_unlock(&pool->manager_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003522 mutex_unlock(&pool->manager_arb);
3523
3524 /* shut down the timers */
3525 del_timer_sync(&pool->idle_timer);
3526 del_timer_sync(&pool->mayday_timer);
3527
3528 /* sched-RCU protected to allow dereferences from get_work_pool() */
3529 call_rcu_sched(&pool->rcu, rcu_free_pool);
3530}
3531
3532/**
3533 * get_unbound_pool - get a worker_pool with the specified attributes
3534 * @attrs: the attributes of the worker_pool to get
3535 *
3536 * Obtain a worker_pool which has the same attributes as @attrs, bump the
3537 * reference count and return it. If there already is a matching
3538 * worker_pool, it will be used; otherwise, this function attempts to
3539 * create a new one. On failure, returns NULL.
3540 */
3541static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs)
3542{
Tejun Heo29c91e92013-03-12 11:30:03 -07003543 u32 hash = wqattrs_hash(attrs);
3544 struct worker_pool *pool;
Tejun Heo29c91e92013-03-12 11:30:03 -07003545
Tejun Heo5bcab332013-03-13 19:47:40 -07003546 mutex_lock(&wq_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003547
3548 /* do we already have a matching pool? */
Tejun Heo29c91e92013-03-12 11:30:03 -07003549 hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) {
3550 if (wqattrs_equal(pool->attrs, attrs)) {
3551 pool->refcnt++;
3552 goto out_unlock;
3553 }
3554 }
Tejun Heo29c91e92013-03-12 11:30:03 -07003555
3556 /* nope, create a new one */
3557 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
3558 if (!pool || init_worker_pool(pool) < 0)
3559 goto fail;
3560
Tejun Heo8864b4e2013-03-12 11:30:04 -07003561 lockdep_set_subclass(&pool->lock, 1); /* see put_pwq() */
Tejun Heo29c91e92013-03-12 11:30:03 -07003562 copy_workqueue_attrs(pool->attrs, attrs);
3563
3564 if (worker_pool_assign_id(pool) < 0)
3565 goto fail;
3566
3567 /* create and start the initial worker */
Tejun Heoebf44d12013-03-13 19:47:39 -07003568 if (create_and_start_worker(pool) < 0)
Tejun Heo29c91e92013-03-12 11:30:03 -07003569 goto fail;
3570
Tejun Heo29c91e92013-03-12 11:30:03 -07003571 /* install */
Tejun Heo29c91e92013-03-12 11:30:03 -07003572 hash_add(unbound_pool_hash, &pool->hash_node, hash);
3573out_unlock:
Tejun Heo5bcab332013-03-13 19:47:40 -07003574 mutex_unlock(&wq_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003575 return pool;
3576fail:
Tejun Heo5bcab332013-03-13 19:47:40 -07003577 mutex_unlock(&wq_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003578 if (pool)
3579 put_unbound_pool(pool);
3580 return NULL;
3581}
3582
Tejun Heo8864b4e2013-03-12 11:30:04 -07003583static void rcu_free_pwq(struct rcu_head *rcu)
3584{
3585 kmem_cache_free(pwq_cache,
3586 container_of(rcu, struct pool_workqueue, rcu));
3587}
3588
3589/*
3590 * Scheduled on system_wq by put_pwq() when an unbound pwq hits zero refcnt
3591 * and needs to be destroyed.
3592 */
3593static void pwq_unbound_release_workfn(struct work_struct *work)
3594{
3595 struct pool_workqueue *pwq = container_of(work, struct pool_workqueue,
3596 unbound_release_work);
3597 struct workqueue_struct *wq = pwq->wq;
3598 struct worker_pool *pool = pwq->pool;
3599
3600 if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)))
3601 return;
3602
Tejun Heo75ccf592013-03-12 11:30:04 -07003603 /*
3604 * Unlink @pwq. Synchronization against flush_mutex isn't strictly
3605 * necessary on release but do it anyway. It's easier to verify
3606 * and consistent with the linking path.
3607 */
3608 mutex_lock(&wq->flush_mutex);
Tejun Heo794b18b2013-03-13 19:47:40 -07003609 spin_lock_irq(&pwq_lock);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003610 list_del_rcu(&pwq->pwqs_node);
Tejun Heo794b18b2013-03-13 19:47:40 -07003611 spin_unlock_irq(&pwq_lock);
Tejun Heo75ccf592013-03-12 11:30:04 -07003612 mutex_unlock(&wq->flush_mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003613
3614 put_unbound_pool(pool);
3615 call_rcu_sched(&pwq->rcu, rcu_free_pwq);
3616
3617 /*
3618 * If we're the last pwq going away, @wq is already dead and no one
3619 * is gonna access it anymore. Free it.
3620 */
3621 if (list_empty(&wq->pwqs))
3622 kfree(wq);
3623}
3624
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003625/**
Tejun Heo699ce092013-03-13 16:51:35 -07003626 * pwq_adjust_max_active - update a pwq's max_active to the current setting
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003627 * @pwq: target pool_workqueue
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003628 *
Tejun Heo699ce092013-03-13 16:51:35 -07003629 * If @pwq isn't freezing, set @pwq->max_active to the associated
3630 * workqueue's saved_max_active and activate delayed work items
3631 * accordingly. If @pwq is freezing, clear @pwq->max_active to zero.
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003632 */
Tejun Heo699ce092013-03-13 16:51:35 -07003633static void pwq_adjust_max_active(struct pool_workqueue *pwq)
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003634{
Tejun Heo699ce092013-03-13 16:51:35 -07003635 struct workqueue_struct *wq = pwq->wq;
3636 bool freezable = wq->flags & WQ_FREEZABLE;
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003637
Tejun Heo699ce092013-03-13 16:51:35 -07003638 /* for @wq->saved_max_active */
Tejun Heo794b18b2013-03-13 19:47:40 -07003639 lockdep_assert_held(&pwq_lock);
Tejun Heo699ce092013-03-13 16:51:35 -07003640
3641 /* fast exit for non-freezable wqs */
3642 if (!freezable && pwq->max_active == wq->saved_max_active)
3643 return;
3644
3645 spin_lock(&pwq->pool->lock);
3646
3647 if (!freezable || !(pwq->pool->flags & POOL_FREEZING)) {
3648 pwq->max_active = wq->saved_max_active;
3649
3650 while (!list_empty(&pwq->delayed_works) &&
3651 pwq->nr_active < pwq->max_active)
3652 pwq_activate_first_delayed(pwq);
3653 } else {
3654 pwq->max_active = 0;
3655 }
3656
3657 spin_unlock(&pwq->pool->lock);
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003658}
3659
Tejun Heod2c1d402013-03-12 11:30:04 -07003660static void init_and_link_pwq(struct pool_workqueue *pwq,
3661 struct workqueue_struct *wq,
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003662 struct worker_pool *pool,
3663 struct pool_workqueue **p_last_pwq)
Tejun Heod2c1d402013-03-12 11:30:04 -07003664{
3665 BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK);
3666
3667 pwq->pool = pool;
3668 pwq->wq = wq;
3669 pwq->flush_color = -1;
Tejun Heo8864b4e2013-03-12 11:30:04 -07003670 pwq->refcnt = 1;
Tejun Heod2c1d402013-03-12 11:30:04 -07003671 INIT_LIST_HEAD(&pwq->delayed_works);
3672 INIT_LIST_HEAD(&pwq->mayday_node);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003673 INIT_WORK(&pwq->unbound_release_work, pwq_unbound_release_workfn);
Tejun Heod2c1d402013-03-12 11:30:04 -07003674
Tejun Heo75ccf592013-03-12 11:30:04 -07003675 mutex_lock(&wq->flush_mutex);
Tejun Heo794b18b2013-03-13 19:47:40 -07003676 spin_lock_irq(&pwq_lock);
Tejun Heo75ccf592013-03-12 11:30:04 -07003677
Tejun Heo983ca252013-03-13 16:51:35 -07003678 /*
3679 * Set the matching work_color. This is synchronized with
3680 * flush_mutex to avoid confusing flush_workqueue().
3681 */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003682 if (p_last_pwq)
3683 *p_last_pwq = first_pwq(wq);
Tejun Heo75ccf592013-03-12 11:30:04 -07003684 pwq->work_color = wq->work_color;
Tejun Heo983ca252013-03-13 16:51:35 -07003685
3686 /* sync max_active to the current setting */
3687 pwq_adjust_max_active(pwq);
3688
3689 /* link in @pwq */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003690 list_add_rcu(&pwq->pwqs_node, &wq->pwqs);
Tejun Heo75ccf592013-03-12 11:30:04 -07003691
Tejun Heo794b18b2013-03-13 19:47:40 -07003692 spin_unlock_irq(&pwq_lock);
Tejun Heo75ccf592013-03-12 11:30:04 -07003693 mutex_unlock(&wq->flush_mutex);
Tejun Heod2c1d402013-03-12 11:30:04 -07003694}
3695
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003696/**
3697 * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue
3698 * @wq: the target workqueue
3699 * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs()
3700 *
3701 * Apply @attrs to an unbound workqueue @wq. If @attrs doesn't match the
3702 * current attributes, a new pwq is created and made the first pwq which
3703 * will serve all new work items. Older pwqs are released as in-flight
3704 * work items finish. Note that a work item which repeatedly requeues
3705 * itself back-to-back will stay on its current pwq.
3706 *
3707 * Performs GFP_KERNEL allocations. Returns 0 on success and -errno on
3708 * failure.
3709 */
3710int apply_workqueue_attrs(struct workqueue_struct *wq,
3711 const struct workqueue_attrs *attrs)
3712{
3713 struct pool_workqueue *pwq, *last_pwq;
3714 struct worker_pool *pool;
3715
Tejun Heo8719dce2013-03-12 11:30:04 -07003716 /* only unbound workqueues can change attributes */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003717 if (WARN_ON(!(wq->flags & WQ_UNBOUND)))
3718 return -EINVAL;
3719
Tejun Heo8719dce2013-03-12 11:30:04 -07003720 /* creating multiple pwqs breaks ordering guarantee */
3721 if (WARN_ON((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs)))
3722 return -EINVAL;
3723
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003724 pwq = kmem_cache_zalloc(pwq_cache, GFP_KERNEL);
3725 if (!pwq)
3726 return -ENOMEM;
3727
3728 pool = get_unbound_pool(attrs);
3729 if (!pool) {
3730 kmem_cache_free(pwq_cache, pwq);
3731 return -ENOMEM;
3732 }
3733
3734 init_and_link_pwq(pwq, wq, pool, &last_pwq);
3735 if (last_pwq) {
3736 spin_lock_irq(&last_pwq->pool->lock);
3737 put_pwq(last_pwq);
3738 spin_unlock_irq(&last_pwq->pool->lock);
3739 }
3740
3741 return 0;
3742}
3743
Tejun Heo30cdf242013-03-12 11:29:57 -07003744static int alloc_and_link_pwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003746 bool highpri = wq->flags & WQ_HIGHPRI;
Tejun Heo30cdf242013-03-12 11:29:57 -07003747 int cpu;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01003748
Tejun Heo30cdf242013-03-12 11:29:57 -07003749 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo420c0dd2013-03-12 11:29:59 -07003750 wq->cpu_pwqs = alloc_percpu(struct pool_workqueue);
3751 if (!wq->cpu_pwqs)
Tejun Heo30cdf242013-03-12 11:29:57 -07003752 return -ENOMEM;
3753
3754 for_each_possible_cpu(cpu) {
Tejun Heo7fb98ea2013-03-12 11:30:00 -07003755 struct pool_workqueue *pwq =
3756 per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heo7a62c2c2013-03-12 11:30:03 -07003757 struct worker_pool *cpu_pools =
Tejun Heof02ae732013-03-12 11:30:03 -07003758 per_cpu(cpu_worker_pools, cpu);
Tejun Heo30cdf242013-03-12 11:29:57 -07003759
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003760 init_and_link_pwq(pwq, wq, &cpu_pools[highpri], NULL);
Tejun Heo30cdf242013-03-12 11:29:57 -07003761 }
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003762 return 0;
Tejun Heo30cdf242013-03-12 11:29:57 -07003763 } else {
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003764 return apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]);
Tejun Heo30cdf242013-03-12 11:29:57 -07003765 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003766}
3767
Tejun Heof3421792010-07-02 10:03:51 +02003768static int wq_clamp_max_active(int max_active, unsigned int flags,
3769 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003770{
Tejun Heof3421792010-07-02 10:03:51 +02003771 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3772
3773 if (max_active < 1 || max_active > lim)
Valentin Ilie044c7822012-08-19 00:52:42 +03003774 pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
3775 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003776
Tejun Heof3421792010-07-02 10:03:51 +02003777 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003778}
3779
Tejun Heob196be82012-01-10 15:11:35 -08003780struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02003781 unsigned int flags,
3782 int max_active,
3783 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08003784 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003785{
Tejun Heob196be82012-01-10 15:11:35 -08003786 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003787 struct workqueue_struct *wq;
Tejun Heo49e3cf42013-03-12 11:29:58 -07003788 struct pool_workqueue *pwq;
Tejun Heob196be82012-01-10 15:11:35 -08003789 size_t namelen;
3790
3791 /* determine namelen, allocate wq and format name */
3792 va_start(args, lock_name);
3793 va_copy(args1, args);
3794 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3795
3796 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3797 if (!wq)
Tejun Heod2c1d402013-03-12 11:30:04 -07003798 return NULL;
Tejun Heob196be82012-01-10 15:11:35 -08003799
3800 vsnprintf(wq->name, namelen, fmt, args1);
3801 va_end(args);
3802 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003803
Tejun Heod320c032010-06-29 10:07:14 +02003804 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003805 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003806
Tejun Heob196be82012-01-10 15:11:35 -08003807 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003808 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003809 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003810 mutex_init(&wq->flush_mutex);
Tejun Heo112202d2013-02-13 19:29:12 -08003811 atomic_set(&wq->nr_pwqs_to_flush, 0);
Tejun Heo30cdf242013-03-12 11:29:57 -07003812 INIT_LIST_HEAD(&wq->pwqs);
Tejun Heo73f53c42010-06-29 10:07:11 +02003813 INIT_LIST_HEAD(&wq->flusher_queue);
3814 INIT_LIST_HEAD(&wq->flusher_overflow);
Tejun Heo493a1722013-03-12 11:29:59 -07003815 INIT_LIST_HEAD(&wq->maydays);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003816
Johannes Bergeb13ba82008-01-16 09:51:58 +01003817 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003818 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003819
Tejun Heo30cdf242013-03-12 11:29:57 -07003820 if (alloc_and_link_pwqs(wq) < 0)
Tejun Heod2c1d402013-03-12 11:30:04 -07003821 goto err_free_wq;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003822
Tejun Heo493008a2013-03-12 11:30:03 -07003823 /*
3824 * Workqueues which may be used during memory reclaim should
3825 * have a rescuer to guarantee forward progress.
3826 */
3827 if (flags & WQ_MEM_RECLAIM) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003828 struct worker *rescuer;
3829
Tejun Heod2c1d402013-03-12 11:30:04 -07003830 rescuer = alloc_worker();
Tejun Heoe22bee72010-06-29 10:07:14 +02003831 if (!rescuer)
Tejun Heod2c1d402013-03-12 11:30:04 -07003832 goto err_destroy;
Tejun Heoe22bee72010-06-29 10:07:14 +02003833
Tejun Heo111c2252013-01-17 17:16:24 -08003834 rescuer->rescue_wq = wq;
3835 rescuer->task = kthread_create(rescuer_thread, rescuer, "%s",
Tejun Heob196be82012-01-10 15:11:35 -08003836 wq->name);
Tejun Heod2c1d402013-03-12 11:30:04 -07003837 if (IS_ERR(rescuer->task)) {
3838 kfree(rescuer);
3839 goto err_destroy;
3840 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003841
Tejun Heod2c1d402013-03-12 11:30:04 -07003842 wq->rescuer = rescuer;
Tejun Heo14a40ff2013-03-19 13:45:20 -07003843 rescuer->task->flags |= PF_NO_SETAFFINITY;
Tejun Heoe22bee72010-06-29 10:07:14 +02003844 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003845 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003846
Tejun Heo226223a2013-03-12 11:30:05 -07003847 if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq))
3848 goto err_destroy;
3849
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003850 /*
Tejun Heo5bcab332013-03-13 19:47:40 -07003851 * wq_mutex protects global freeze state and workqueues list. Grab
3852 * it, adjust max_active and add the new @wq to workqueues list.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003853 */
Tejun Heo5bcab332013-03-13 19:47:40 -07003854 mutex_lock(&wq_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003855
Tejun Heo794b18b2013-03-13 19:47:40 -07003856 spin_lock_irq(&pwq_lock);
Tejun Heo699ce092013-03-13 16:51:35 -07003857 for_each_pwq(pwq, wq)
3858 pwq_adjust_max_active(pwq);
Tejun Heo794b18b2013-03-13 19:47:40 -07003859 spin_unlock_irq(&pwq_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003860
Tejun Heo15376632010-06-29 10:07:11 +02003861 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003862
Tejun Heo5bcab332013-03-13 19:47:40 -07003863 mutex_unlock(&wq_mutex);
Tejun Heo15376632010-06-29 10:07:11 +02003864
Oleg Nesterov3af244332007-05-09 02:34:09 -07003865 return wq;
Tejun Heod2c1d402013-03-12 11:30:04 -07003866
3867err_free_wq:
3868 kfree(wq);
3869 return NULL;
3870err_destroy:
3871 destroy_workqueue(wq);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003872 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003873}
Tejun Heod320c032010-06-29 10:07:14 +02003874EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003875
3876/**
3877 * destroy_workqueue - safely terminate a workqueue
3878 * @wq: target workqueue
3879 *
3880 * Safely destroy a workqueue. All work currently pending will be done first.
3881 */
3882void destroy_workqueue(struct workqueue_struct *wq)
3883{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003884 struct pool_workqueue *pwq;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003885
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003886 /* drain it before proceeding with destruction */
3887 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003888
Tejun Heo6183c002013-03-12 11:29:57 -07003889 /* sanity checks */
Tejun Heo794b18b2013-03-13 19:47:40 -07003890 spin_lock_irq(&pwq_lock);
Tejun Heo49e3cf42013-03-12 11:29:58 -07003891 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07003892 int i;
3893
Tejun Heo76af4d92013-03-12 11:30:00 -07003894 for (i = 0; i < WORK_NR_COLORS; i++) {
3895 if (WARN_ON(pwq->nr_in_flight[i])) {
Tejun Heo794b18b2013-03-13 19:47:40 -07003896 spin_unlock_irq(&pwq_lock);
Tejun Heo6183c002013-03-12 11:29:57 -07003897 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07003898 }
3899 }
3900
Tejun Heo8864b4e2013-03-12 11:30:04 -07003901 if (WARN_ON(pwq->refcnt > 1) ||
3902 WARN_ON(pwq->nr_active) ||
Tejun Heo76af4d92013-03-12 11:30:00 -07003903 WARN_ON(!list_empty(&pwq->delayed_works))) {
Tejun Heo794b18b2013-03-13 19:47:40 -07003904 spin_unlock_irq(&pwq_lock);
Tejun Heo6183c002013-03-12 11:29:57 -07003905 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07003906 }
Tejun Heo6183c002013-03-12 11:29:57 -07003907 }
Tejun Heo794b18b2013-03-13 19:47:40 -07003908 spin_unlock_irq(&pwq_lock);
Tejun Heo6183c002013-03-12 11:29:57 -07003909
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003910 /*
3911 * wq list is used to freeze wq, remove from list after
3912 * flushing is complete in case freeze races us.
3913 */
Tejun Heo5bcab332013-03-13 19:47:40 -07003914 mutex_lock(&wq_mutex);
Tejun Heod2c1d402013-03-12 11:30:04 -07003915 list_del_init(&wq->list);
Tejun Heo5bcab332013-03-13 19:47:40 -07003916 mutex_unlock(&wq_mutex);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003917
Tejun Heo226223a2013-03-12 11:30:05 -07003918 workqueue_sysfs_unregister(wq);
3919
Tejun Heo493008a2013-03-12 11:30:03 -07003920 if (wq->rescuer) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003921 kthread_stop(wq->rescuer->task);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003922 kfree(wq->rescuer);
Tejun Heo493008a2013-03-12 11:30:03 -07003923 wq->rescuer = NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +02003924 }
3925
Tejun Heo8864b4e2013-03-12 11:30:04 -07003926 if (!(wq->flags & WQ_UNBOUND)) {
3927 /*
3928 * The base ref is never dropped on per-cpu pwqs. Directly
3929 * free the pwqs and wq.
3930 */
3931 free_percpu(wq->cpu_pwqs);
3932 kfree(wq);
3933 } else {
3934 /*
3935 * We're the sole accessor of @wq at this point. Directly
3936 * access the first pwq and put the base ref. As both pwqs
3937 * and pools are sched-RCU protected, the lock operations
3938 * are safe. @wq will be freed when the last pwq is
3939 * released.
3940 */
Tejun Heo29c91e92013-03-12 11:30:03 -07003941 pwq = list_first_entry(&wq->pwqs, struct pool_workqueue,
3942 pwqs_node);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003943 spin_lock_irq(&pwq->pool->lock);
3944 put_pwq(pwq);
3945 spin_unlock_irq(&pwq->pool->lock);
Tejun Heo29c91e92013-03-12 11:30:03 -07003946 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003947}
3948EXPORT_SYMBOL_GPL(destroy_workqueue);
3949
Tejun Heodcd989c2010-06-29 10:07:14 +02003950/**
3951 * workqueue_set_max_active - adjust max_active of a workqueue
3952 * @wq: target workqueue
3953 * @max_active: new max_active value.
3954 *
3955 * Set max_active of @wq to @max_active.
3956 *
3957 * CONTEXT:
3958 * Don't call from IRQ context.
3959 */
3960void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3961{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003962 struct pool_workqueue *pwq;
Tejun Heodcd989c2010-06-29 10:07:14 +02003963
Tejun Heo8719dce2013-03-12 11:30:04 -07003964 /* disallow meddling with max_active for ordered workqueues */
3965 if (WARN_ON(wq->flags & __WQ_ORDERED))
3966 return;
3967
Tejun Heof3421792010-07-02 10:03:51 +02003968 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003969
Tejun Heo794b18b2013-03-13 19:47:40 -07003970 spin_lock_irq(&pwq_lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02003971
3972 wq->saved_max_active = max_active;
3973
Tejun Heo699ce092013-03-13 16:51:35 -07003974 for_each_pwq(pwq, wq)
3975 pwq_adjust_max_active(pwq);
Tejun Heodcd989c2010-06-29 10:07:14 +02003976
Tejun Heo794b18b2013-03-13 19:47:40 -07003977 spin_unlock_irq(&pwq_lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02003978}
3979EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3980
3981/**
Tejun Heoe6267612013-03-12 17:41:37 -07003982 * current_is_workqueue_rescuer - is %current workqueue rescuer?
3983 *
3984 * Determine whether %current is a workqueue rescuer. Can be used from
3985 * work functions to determine whether it's being run off the rescuer task.
3986 */
3987bool current_is_workqueue_rescuer(void)
3988{
3989 struct worker *worker = current_wq_worker();
3990
3991 return worker && worker == worker->current_pwq->wq->rescuer;
3992}
3993
3994/**
Tejun Heodcd989c2010-06-29 10:07:14 +02003995 * workqueue_congested - test whether a workqueue is congested
3996 * @cpu: CPU in question
3997 * @wq: target workqueue
3998 *
3999 * Test whether @wq's cpu workqueue for @cpu is congested. There is
4000 * no synchronization around this function and the test result is
4001 * unreliable and only useful as advisory hints or for debugging.
4002 *
4003 * RETURNS:
4004 * %true if congested, %false otherwise.
4005 */
Tejun Heod84ff052013-03-12 11:29:59 -07004006bool workqueue_congested(int cpu, struct workqueue_struct *wq)
Tejun Heodcd989c2010-06-29 10:07:14 +02004007{
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004008 struct pool_workqueue *pwq;
Tejun Heo76af4d92013-03-12 11:30:00 -07004009 bool ret;
4010
4011 preempt_disable();
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004012
4013 if (!(wq->flags & WQ_UNBOUND))
4014 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
4015 else
4016 pwq = first_pwq(wq);
Tejun Heodcd989c2010-06-29 10:07:14 +02004017
Tejun Heo76af4d92013-03-12 11:30:00 -07004018 ret = !list_empty(&pwq->delayed_works);
4019 preempt_enable();
4020
4021 return ret;
Tejun Heodcd989c2010-06-29 10:07:14 +02004022}
4023EXPORT_SYMBOL_GPL(workqueue_congested);
4024
4025/**
Tejun Heodcd989c2010-06-29 10:07:14 +02004026 * work_busy - test whether a work is currently pending or running
4027 * @work: the work to be tested
4028 *
4029 * Test whether @work is currently pending or running. There is no
4030 * synchronization around this function and the test result is
4031 * unreliable and only useful as advisory hints or for debugging.
Tejun Heodcd989c2010-06-29 10:07:14 +02004032 *
4033 * RETURNS:
4034 * OR'd bitmask of WORK_BUSY_* bits.
4035 */
4036unsigned int work_busy(struct work_struct *work)
4037{
Tejun Heofa1b54e2013-03-12 11:30:00 -07004038 struct worker_pool *pool;
Tejun Heodcd989c2010-06-29 10:07:14 +02004039 unsigned long flags;
4040 unsigned int ret = 0;
4041
Tejun Heodcd989c2010-06-29 10:07:14 +02004042 if (work_pending(work))
4043 ret |= WORK_BUSY_PENDING;
Tejun Heodcd989c2010-06-29 10:07:14 +02004044
Tejun Heofa1b54e2013-03-12 11:30:00 -07004045 local_irq_save(flags);
4046 pool = get_work_pool(work);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004047 if (pool) {
Tejun Heofa1b54e2013-03-12 11:30:00 -07004048 spin_lock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004049 if (find_worker_executing_work(pool, work))
4050 ret |= WORK_BUSY_RUNNING;
Tejun Heofa1b54e2013-03-12 11:30:00 -07004051 spin_unlock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004052 }
Tejun Heofa1b54e2013-03-12 11:30:00 -07004053 local_irq_restore(flags);
Tejun Heodcd989c2010-06-29 10:07:14 +02004054
4055 return ret;
4056}
4057EXPORT_SYMBOL_GPL(work_busy);
4058
Tejun Heodb7bccf2010-06-29 10:07:12 +02004059/*
4060 * CPU hotplug.
4061 *
Tejun Heoe22bee72010-06-29 10:07:14 +02004062 * There are two challenges in supporting CPU hotplug. Firstly, there
Tejun Heo112202d2013-02-13 19:29:12 -08004063 * are a lot of assumptions on strong associations among work, pwq and
Tejun Heo706026c2013-01-24 11:01:34 -08004064 * pool which make migrating pending and scheduled works very
Tejun Heoe22bee72010-06-29 10:07:14 +02004065 * difficult to implement without impacting hot paths. Secondly,
Tejun Heo94cf58b2013-01-24 11:01:33 -08004066 * worker pools serve mix of short, long and very long running works making
Tejun Heoe22bee72010-06-29 10:07:14 +02004067 * blocked draining impractical.
4068 *
Tejun Heo24647572013-01-24 11:01:33 -08004069 * This is solved by allowing the pools to be disassociated from the CPU
Tejun Heo628c78e2012-07-17 12:39:27 -07004070 * running as an unbound one and allowing it to be reattached later if the
4071 * cpu comes back online.
Tejun Heodb7bccf2010-06-29 10:07:12 +02004072 */
4073
Tejun Heo706026c2013-01-24 11:01:34 -08004074static void wq_unbind_fn(struct work_struct *work)
Tejun Heodb7bccf2010-06-29 10:07:12 +02004075{
Tejun Heo38db41d2013-01-24 11:01:34 -08004076 int cpu = smp_processor_id();
Tejun Heo4ce62e92012-07-13 22:16:44 -07004077 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004078 struct worker *worker;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004079 int i;
4080
Tejun Heof02ae732013-03-12 11:30:03 -07004081 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo6183c002013-03-12 11:29:57 -07004082 WARN_ON_ONCE(cpu != smp_processor_id());
Tejun Heo94cf58b2013-01-24 11:01:33 -08004083
Tejun Heobc3a1af2013-03-13 19:47:39 -07004084 mutex_lock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004085 spin_lock_irq(&pool->lock);
4086
4087 /*
Tejun Heobc3a1af2013-03-13 19:47:39 -07004088 * We've blocked all manager operations. Make all workers
Tejun Heo94cf58b2013-01-24 11:01:33 -08004089 * unbound and set DISASSOCIATED. Before this, all workers
4090 * except for the ones which are still executing works from
4091 * before the last CPU down must be on the cpu. After
4092 * this, they may become diasporas.
4093 */
Tejun Heo4ce62e92012-07-13 22:16:44 -07004094 list_for_each_entry(worker, &pool->idle_list, entry)
Tejun Heo403c8212012-07-17 12:39:27 -07004095 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004096
Sasha Levinb67bfe02013-02-27 17:06:00 -08004097 for_each_busy_worker(worker, i, pool)
Tejun Heoc9e7cf22013-01-24 11:01:33 -08004098 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004099
Tejun Heo24647572013-01-24 11:01:33 -08004100 pool->flags |= POOL_DISASSOCIATED;
Tejun Heof2d5a0e2012-07-17 12:39:26 -07004101
Tejun Heo94cf58b2013-01-24 11:01:33 -08004102 spin_unlock_irq(&pool->lock);
Tejun Heobc3a1af2013-03-13 19:47:39 -07004103 mutex_unlock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004104 }
Tejun Heoe22bee72010-06-29 10:07:14 +02004105
4106 /*
Tejun Heo628c78e2012-07-17 12:39:27 -07004107 * Call schedule() so that we cross rq->lock and thus can guarantee
4108 * sched callbacks see the %WORKER_UNBOUND flag. This is necessary
4109 * as scheduler callbacks may be invoked from other cpus.
4110 */
4111 schedule();
4112
4113 /*
4114 * Sched callbacks are disabled now. Zap nr_running. After this,
4115 * nr_running stays zero and need_more_worker() and keep_working()
Tejun Heo38db41d2013-01-24 11:01:34 -08004116 * are always true as long as the worklist is not empty. Pools on
4117 * @cpu now behave as unbound (in terms of concurrency management)
4118 * pools which are served by workers tied to the CPU.
Tejun Heo628c78e2012-07-17 12:39:27 -07004119 *
4120 * On return from this function, the current worker would trigger
4121 * unbound chain execution of pending work items if other workers
4122 * didn't already.
Tejun Heoe22bee72010-06-29 10:07:14 +02004123 */
Tejun Heof02ae732013-03-12 11:30:03 -07004124 for_each_cpu_worker_pool(pool, cpu)
Tejun Heoe19e3972013-01-24 11:39:44 -08004125 atomic_set(&pool->nr_running, 0);
Tejun Heodb7bccf2010-06-29 10:07:12 +02004126}
4127
Tejun Heobd7c0892013-03-19 13:45:21 -07004128/**
4129 * rebind_workers - rebind all workers of a pool to the associated CPU
4130 * @pool: pool of interest
4131 *
4132 * @pool->cpu is coming online. Rebind all workers to the CPU. Rebinding
4133 * is different for idle and busy ones.
4134 *
4135 * Idle ones will be removed from the idle_list and woken up. They will
4136 * add themselves back after completing rebind. This ensures that the
4137 * idle_list doesn't contain any unbound workers when re-bound busy workers
4138 * try to perform local wake-ups for concurrency management.
4139 *
4140 * Busy workers can rebind after they finish their current work items.
4141 * Queueing the rebind work item at the head of the scheduled list is
4142 * enough. Note that nr_running will be properly bumped as busy workers
4143 * rebind.
4144 *
4145 * On return, all non-manager workers are scheduled for rebind - see
4146 * manage_workers() for the manager special case. Any idle worker
4147 * including the manager will not appear on @idle_list until rebind is
4148 * complete, making local wake-ups safe.
4149 */
4150static void rebind_workers(struct worker_pool *pool)
4151{
4152 struct worker *worker, *n;
4153 int i;
4154
4155 lockdep_assert_held(&pool->manager_mutex);
4156 lockdep_assert_held(&pool->lock);
4157
4158 /* dequeue and kick idle ones */
4159 list_for_each_entry_safe(worker, n, &pool->idle_list, entry) {
4160 /*
4161 * idle workers should be off @pool->idle_list until rebind
4162 * is complete to avoid receiving premature local wake-ups.
4163 */
4164 list_del_init(&worker->entry);
4165
4166 /*
4167 * worker_thread() will see the above dequeuing and call
4168 * idle_worker_rebind().
4169 */
4170 wake_up_process(worker->task);
4171 }
4172
4173 /* rebind busy workers */
4174 for_each_busy_worker(worker, i, pool) {
4175 struct work_struct *rebind_work = &worker->rebind_work;
4176 struct workqueue_struct *wq;
4177
4178 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
4179 work_data_bits(rebind_work)))
4180 continue;
4181
4182 debug_work_activate(rebind_work);
4183
4184 /*
4185 * wq doesn't really matter but let's keep @worker->pool
4186 * and @pwq->pool consistent for sanity.
4187 */
4188 if (worker->pool->attrs->nice < 0)
4189 wq = system_highpri_wq;
4190 else
4191 wq = system_wq;
4192
4193 insert_work(per_cpu_ptr(wq->cpu_pwqs, pool->cpu), rebind_work,
4194 worker->scheduled.next,
4195 work_color_to_flags(WORK_NO_COLOR));
4196 }
4197}
4198
Tejun Heo8db25e72012-07-17 12:39:28 -07004199/*
4200 * Workqueues should be brought up before normal priority CPU notifiers.
4201 * This will be registered high priority CPU notifier.
4202 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07004203static int __cpuinit workqueue_cpu_up_callback(struct notifier_block *nfb,
Tejun Heo8db25e72012-07-17 12:39:28 -07004204 unsigned long action,
4205 void *hcpu)
Oleg Nesterov3af244332007-05-09 02:34:09 -07004206{
Tejun Heod84ff052013-03-12 11:29:59 -07004207 int cpu = (unsigned long)hcpu;
Tejun Heo4ce62e92012-07-13 22:16:44 -07004208 struct worker_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209
Tejun Heo8db25e72012-07-17 12:39:28 -07004210 switch (action & ~CPU_TASKS_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 case CPU_UP_PREPARE:
Tejun Heof02ae732013-03-12 11:30:03 -07004212 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo3ce63372012-07-17 12:39:27 -07004213 if (pool->nr_workers)
4214 continue;
Tejun Heoebf44d12013-03-13 19:47:39 -07004215 if (create_and_start_worker(pool) < 0)
Tejun Heo3ce63372012-07-17 12:39:27 -07004216 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02004218 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07004219
Tejun Heo65758202012-07-17 12:39:26 -07004220 case CPU_DOWN_FAILED:
4221 case CPU_ONLINE:
Tejun Heof02ae732013-03-12 11:30:03 -07004222 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heobc3a1af2013-03-13 19:47:39 -07004223 mutex_lock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004224 spin_lock_irq(&pool->lock);
4225
Tejun Heo24647572013-01-24 11:01:33 -08004226 pool->flags &= ~POOL_DISASSOCIATED;
Tejun Heo94cf58b2013-01-24 11:01:33 -08004227 rebind_workers(pool);
4228
4229 spin_unlock_irq(&pool->lock);
Tejun Heobc3a1af2013-03-13 19:47:39 -07004230 mutex_unlock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004231 }
Tejun Heo8db25e72012-07-17 12:39:28 -07004232 break;
Tejun Heo65758202012-07-17 12:39:26 -07004233 }
4234 return NOTIFY_OK;
4235}
4236
4237/*
4238 * Workqueues should be brought down after normal priority CPU notifiers.
4239 * This will be registered as low priority CPU notifier.
4240 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07004241static int __cpuinit workqueue_cpu_down_callback(struct notifier_block *nfb,
Tejun Heo65758202012-07-17 12:39:26 -07004242 unsigned long action,
4243 void *hcpu)
4244{
Tejun Heod84ff052013-03-12 11:29:59 -07004245 int cpu = (unsigned long)hcpu;
Tejun Heo8db25e72012-07-17 12:39:28 -07004246 struct work_struct unbind_work;
4247
Tejun Heo65758202012-07-17 12:39:26 -07004248 switch (action & ~CPU_TASKS_FROZEN) {
4249 case CPU_DOWN_PREPARE:
Tejun Heo8db25e72012-07-17 12:39:28 -07004250 /* unbinding should happen on the local CPU */
Tejun Heo706026c2013-01-24 11:01:34 -08004251 INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn);
Joonsoo Kim7635d2f2012-08-15 23:25:41 +09004252 queue_work_on(cpu, system_highpri_wq, &unbind_work);
Tejun Heo8db25e72012-07-17 12:39:28 -07004253 flush_work(&unbind_work);
4254 break;
Tejun Heo65758202012-07-17 12:39:26 -07004255 }
4256 return NOTIFY_OK;
4257}
4258
Rusty Russell2d3854a2008-11-05 13:39:10 +11004259#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08004260
Rusty Russell2d3854a2008-11-05 13:39:10 +11004261struct work_for_cpu {
Tejun Heoed48ece2012-09-18 12:48:43 -07004262 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11004263 long (*fn)(void *);
4264 void *arg;
4265 long ret;
4266};
4267
Tejun Heoed48ece2012-09-18 12:48:43 -07004268static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11004269{
Tejun Heoed48ece2012-09-18 12:48:43 -07004270 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
4271
Rusty Russell2d3854a2008-11-05 13:39:10 +11004272 wfc->ret = wfc->fn(wfc->arg);
4273}
4274
4275/**
4276 * work_on_cpu - run a function in user context on a particular cpu
4277 * @cpu: the cpu to run on
4278 * @fn: the function to run
4279 * @arg: the function arg
4280 *
Rusty Russell31ad9082009-01-16 15:31:15 -08004281 * This will return the value @fn returns.
4282 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06004283 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11004284 */
Tejun Heod84ff052013-03-12 11:29:59 -07004285long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
Rusty Russell2d3854a2008-11-05 13:39:10 +11004286{
Tejun Heoed48ece2012-09-18 12:48:43 -07004287 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11004288
Tejun Heoed48ece2012-09-18 12:48:43 -07004289 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
4290 schedule_work_on(cpu, &wfc.work);
4291 flush_work(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11004292 return wfc.ret;
4293}
4294EXPORT_SYMBOL_GPL(work_on_cpu);
4295#endif /* CONFIG_SMP */
4296
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004297#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10304298
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004299/**
4300 * freeze_workqueues_begin - begin freezing workqueues
4301 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01004302 * Start freezing workqueues. After this function returns, all freezable
Tejun Heoc5aa87b2013-03-13 16:51:36 -07004303 * workqueues will queue new works to their delayed_works list instead of
Tejun Heo706026c2013-01-24 11:01:34 -08004304 * pool->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004305 *
4306 * CONTEXT:
Tejun Heo794b18b2013-03-13 19:47:40 -07004307 * Grabs and releases wq_mutex, pwq_lock and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004308 */
4309void freeze_workqueues_begin(void)
4310{
Tejun Heo17116962013-03-12 11:29:58 -07004311 struct worker_pool *pool;
Tejun Heo24b8a842013-03-12 11:29:58 -07004312 struct workqueue_struct *wq;
4313 struct pool_workqueue *pwq;
Tejun Heo611c92a2013-03-13 16:51:36 -07004314 int pi;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004315
Tejun Heo5bcab332013-03-13 19:47:40 -07004316 mutex_lock(&wq_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004317
Tejun Heo6183c002013-03-12 11:29:57 -07004318 WARN_ON_ONCE(workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004319 workqueue_freezing = true;
4320
Tejun Heo24b8a842013-03-12 11:29:58 -07004321 /* set FREEZING */
Tejun Heo611c92a2013-03-13 16:51:36 -07004322 for_each_pool(pool, pi) {
Tejun Heo5bcab332013-03-13 19:47:40 -07004323 spin_lock_irq(&pool->lock);
Tejun Heo17116962013-03-12 11:29:58 -07004324 WARN_ON_ONCE(pool->flags & POOL_FREEZING);
4325 pool->flags |= POOL_FREEZING;
Tejun Heo5bcab332013-03-13 19:47:40 -07004326 spin_unlock_irq(&pool->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004327 }
4328
Tejun Heo24b8a842013-03-12 11:29:58 -07004329 /* suppress further executions by setting max_active to zero */
Tejun Heo794b18b2013-03-13 19:47:40 -07004330 spin_lock_irq(&pwq_lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004331 list_for_each_entry(wq, &workqueues, list) {
Tejun Heo699ce092013-03-13 16:51:35 -07004332 for_each_pwq(pwq, wq)
4333 pwq_adjust_max_active(pwq);
Tejun Heo24b8a842013-03-12 11:29:58 -07004334 }
Tejun Heo794b18b2013-03-13 19:47:40 -07004335 spin_unlock_irq(&pwq_lock);
Tejun Heo5bcab332013-03-13 19:47:40 -07004336
4337 mutex_unlock(&wq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004339
4340/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01004341 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004342 *
4343 * Check whether freezing is complete. This function must be called
4344 * between freeze_workqueues_begin() and thaw_workqueues().
4345 *
4346 * CONTEXT:
Tejun Heo5bcab332013-03-13 19:47:40 -07004347 * Grabs and releases wq_mutex.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004348 *
4349 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01004350 * %true if some freezable workqueues are still busy. %false if freezing
4351 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004352 */
4353bool freeze_workqueues_busy(void)
4354{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004355 bool busy = false;
Tejun Heo24b8a842013-03-12 11:29:58 -07004356 struct workqueue_struct *wq;
4357 struct pool_workqueue *pwq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004358
Tejun Heo5bcab332013-03-13 19:47:40 -07004359 mutex_lock(&wq_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004360
Tejun Heo6183c002013-03-12 11:29:57 -07004361 WARN_ON_ONCE(!workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004362
Tejun Heo24b8a842013-03-12 11:29:58 -07004363 list_for_each_entry(wq, &workqueues, list) {
4364 if (!(wq->flags & WQ_FREEZABLE))
4365 continue;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004366 /*
4367 * nr_active is monotonically decreasing. It's safe
4368 * to peek without lock.
4369 */
Tejun Heo5bcab332013-03-13 19:47:40 -07004370 preempt_disable();
Tejun Heo24b8a842013-03-12 11:29:58 -07004371 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07004372 WARN_ON_ONCE(pwq->nr_active < 0);
Tejun Heo112202d2013-02-13 19:29:12 -08004373 if (pwq->nr_active) {
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004374 busy = true;
Tejun Heo5bcab332013-03-13 19:47:40 -07004375 preempt_enable();
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004376 goto out_unlock;
4377 }
4378 }
Tejun Heo5bcab332013-03-13 19:47:40 -07004379 preempt_enable();
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004380 }
4381out_unlock:
Tejun Heo5bcab332013-03-13 19:47:40 -07004382 mutex_unlock(&wq_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004383 return busy;
4384}
4385
4386/**
4387 * thaw_workqueues - thaw workqueues
4388 *
4389 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo706026c2013-01-24 11:01:34 -08004390 * frozen works are transferred to their respective pool worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004391 *
4392 * CONTEXT:
Tejun Heo794b18b2013-03-13 19:47:40 -07004393 * Grabs and releases wq_mutex, pwq_lock and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004394 */
4395void thaw_workqueues(void)
4396{
Tejun Heo24b8a842013-03-12 11:29:58 -07004397 struct workqueue_struct *wq;
4398 struct pool_workqueue *pwq;
4399 struct worker_pool *pool;
Tejun Heo611c92a2013-03-13 16:51:36 -07004400 int pi;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004401
Tejun Heo5bcab332013-03-13 19:47:40 -07004402 mutex_lock(&wq_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004403
4404 if (!workqueue_freezing)
4405 goto out_unlock;
4406
Tejun Heo24b8a842013-03-12 11:29:58 -07004407 /* clear FREEZING */
Tejun Heo611c92a2013-03-13 16:51:36 -07004408 for_each_pool(pool, pi) {
Tejun Heo5bcab332013-03-13 19:47:40 -07004409 spin_lock_irq(&pool->lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004410 WARN_ON_ONCE(!(pool->flags & POOL_FREEZING));
4411 pool->flags &= ~POOL_FREEZING;
Tejun Heo5bcab332013-03-13 19:47:40 -07004412 spin_unlock_irq(&pool->lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004413 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02004414
Tejun Heo24b8a842013-03-12 11:29:58 -07004415 /* restore max_active and repopulate worklist */
Tejun Heo794b18b2013-03-13 19:47:40 -07004416 spin_lock_irq(&pwq_lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004417 list_for_each_entry(wq, &workqueues, list) {
Tejun Heo699ce092013-03-13 16:51:35 -07004418 for_each_pwq(pwq, wq)
4419 pwq_adjust_max_active(pwq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004420 }
Tejun Heo794b18b2013-03-13 19:47:40 -07004421 spin_unlock_irq(&pwq_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004422
Tejun Heo24b8a842013-03-12 11:29:58 -07004423 /* kick workers */
Tejun Heo611c92a2013-03-13 16:51:36 -07004424 for_each_pool(pool, pi) {
Tejun Heo5bcab332013-03-13 19:47:40 -07004425 spin_lock_irq(&pool->lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004426 wake_up_worker(pool);
Tejun Heo5bcab332013-03-13 19:47:40 -07004427 spin_unlock_irq(&pool->lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004428 }
4429
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004430 workqueue_freezing = false;
4431out_unlock:
Tejun Heo5bcab332013-03-13 19:47:40 -07004432 mutex_unlock(&wq_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004433}
4434#endif /* CONFIG_FREEZER */
4435
Suresh Siddha6ee05782010-07-30 14:57:37 -07004436static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437{
Tejun Heo7a4e3442013-03-12 11:30:00 -07004438 int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL };
4439 int i, cpu;
Tejun Heoc34056a2010-06-29 10:07:11 +02004440
Tejun Heo7c3eed52013-01-24 11:01:33 -08004441 /* make sure we have enough bits for OFFQ pool ID */
4442 BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) <
Lai Jiangshan6be19582013-02-06 18:04:53 -08004443 WORK_CPU_END * NR_STD_WORKER_POOLS);
Tejun Heob5490072012-08-03 10:30:46 -07004444
Tejun Heoe904e6c2013-03-12 11:29:57 -07004445 WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long));
4446
4447 pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC);
4448
Tejun Heo65758202012-07-17 12:39:26 -07004449 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
Lai Jiangshana5b4e572012-09-18 09:59:23 -07004450 hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02004451
Tejun Heo706026c2013-01-24 11:01:34 -08004452 /* initialize CPU pools */
Tejun Heo29c91e92013-03-12 11:30:03 -07004453 for_each_possible_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07004454 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02004455
Tejun Heo7a4e3442013-03-12 11:30:00 -07004456 i = 0;
Tejun Heof02ae732013-03-12 11:30:03 -07004457 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo7a4e3442013-03-12 11:30:00 -07004458 BUG_ON(init_worker_pool(pool));
Tejun Heoec22ca52013-01-24 11:01:33 -08004459 pool->cpu = cpu;
Tejun Heo29c91e92013-03-12 11:30:03 -07004460 cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu));
Tejun Heo7a4e3442013-03-12 11:30:00 -07004461 pool->attrs->nice = std_nice[i++];
4462
Tejun Heo9daf9e62013-01-24 11:01:33 -08004463 /* alloc pool ID */
Tejun Heo5bcab332013-03-13 19:47:40 -07004464 mutex_lock(&wq_mutex);
Tejun Heo9daf9e62013-01-24 11:01:33 -08004465 BUG_ON(worker_pool_assign_id(pool));
Tejun Heo5bcab332013-03-13 19:47:40 -07004466 mutex_unlock(&wq_mutex);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004467 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02004468 }
4469
Tejun Heoe22bee72010-06-29 10:07:14 +02004470 /* create the initial worker */
Tejun Heo29c91e92013-03-12 11:30:03 -07004471 for_each_online_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07004472 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02004473
Tejun Heof02ae732013-03-12 11:30:03 -07004474 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo29c91e92013-03-12 11:30:03 -07004475 pool->flags &= ~POOL_DISASSOCIATED;
Tejun Heoebf44d12013-03-13 19:47:39 -07004476 BUG_ON(create_and_start_worker(pool) < 0);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004477 }
Tejun Heoe22bee72010-06-29 10:07:14 +02004478 }
4479
Tejun Heo29c91e92013-03-12 11:30:03 -07004480 /* create default unbound wq attrs */
4481 for (i = 0; i < NR_STD_WORKER_POOLS; i++) {
4482 struct workqueue_attrs *attrs;
4483
4484 BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL)));
4485
4486 attrs->nice = std_nice[i];
4487 cpumask_setall(attrs->cpumask);
4488
4489 unbound_std_wq_attrs[i] = attrs;
4490 }
4491
Tejun Heod320c032010-06-29 10:07:14 +02004492 system_wq = alloc_workqueue("events", 0, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09004493 system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
Tejun Heod320c032010-06-29 10:07:14 +02004494 system_long_wq = alloc_workqueue("events_long", 0, 0);
Tejun Heof3421792010-07-02 10:03:51 +02004495 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
4496 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01004497 system_freezable_wq = alloc_workqueue("events_freezable",
4498 WQ_FREEZABLE, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09004499 BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
Tejun Heoae930e02012-08-20 14:51:23 -07004500 !system_unbound_wq || !system_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07004501 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502}
Suresh Siddha6ee05782010-07-30 14:57:37 -07004503early_initcall(init_workqueues);