blob: 3fe2c79bf166ccaf2fa2bbae401c4d75b1c63459 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tejun Heoc54fce62010-09-10 16:51:36 +02002 * kernel/workqueue.c - generic async execution with shared worker pool
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Tejun Heoc54fce62010-09-10 16:51:36 +02004 * Copyright (C) 2002 Ingo Molnar
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Tejun Heoc54fce62010-09-10 16:51:36 +02006 * Derived from the taskqueue/keventd code by:
7 * David Woodhouse <dwmw2@infradead.org>
8 * Andrew Morton
9 * Kai Petzke <wpp@marie.physik.tu-berlin.de>
10 * Theodore Ts'o <tytso@mit.edu>
Christoph Lameter89ada672005-10-30 15:01:59 -080011 *
Christoph Lametercde53532008-07-04 09:59:22 -070012 * Made to use alloc_percpu by Christoph Lameter.
Tejun Heoc54fce62010-09-10 16:51:36 +020013 *
14 * Copyright (C) 2010 SUSE Linux Products GmbH
15 * Copyright (C) 2010 Tejun Heo <tj@kernel.org>
16 *
17 * This is the generic async execution mechanism. Work items as are
18 * executed in process context. The worker pool is shared and
19 * automatically managed. There is one worker pool for each CPU and
20 * one extra for works which are better served by workers which are
21 * not bound to any specific CPU.
22 *
23 * Please read Documentation/workqueue.txt for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 */
25
Paul Gortmaker9984de12011-05-23 14:51:41 -040026#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/kernel.h>
28#include <linux/sched.h>
29#include <linux/init.h>
30#include <linux/signal.h>
31#include <linux/completion.h>
32#include <linux/workqueue.h>
33#include <linux/slab.h>
34#include <linux/cpu.h>
35#include <linux/notifier.h>
36#include <linux/kthread.h>
James Bottomley1fa44ec2006-02-23 12:43:43 -060037#include <linux/hardirq.h>
Christoph Lameter46934022006-10-11 01:21:26 -070038#include <linux/mempolicy.h>
Rafael J. Wysocki341a5952006-12-06 20:34:49 -080039#include <linux/freezer.h>
Peter Zijlstrad5abe662006-12-06 20:37:26 -080040#include <linux/kallsyms.h>
41#include <linux/debug_locks.h>
Johannes Berg4e6045f2007-10-18 23:39:55 -070042#include <linux/lockdep.h>
Tejun Heoc34056a2010-06-29 10:07:11 +020043#include <linux/idr.h>
Tejun Heo29c91e92013-03-12 11:30:03 -070044#include <linux/jhash.h>
Sasha Levin42f85702012-12-17 10:01:23 -050045#include <linux/hashtable.h>
Tejun Heo76af4d92013-03-12 11:30:00 -070046#include <linux/rculist.h>
Tejun Heoe22bee72010-06-29 10:07:14 +020047
Tejun Heoea138442013-01-18 14:05:55 -080048#include "workqueue_internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Tejun Heoc8e55f32010-06-29 10:07:12 +020050enum {
Tejun Heobc2ae0f2012-07-17 12:39:27 -070051 /*
Tejun Heo24647572013-01-24 11:01:33 -080052 * worker_pool flags
Tejun Heobc2ae0f2012-07-17 12:39:27 -070053 *
Tejun Heo24647572013-01-24 11:01:33 -080054 * A bound pool is either associated or disassociated with its CPU.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070055 * While associated (!DISASSOCIATED), all workers are bound to the
56 * CPU and none has %WORKER_UNBOUND set and concurrency management
57 * is in effect.
58 *
59 * While DISASSOCIATED, the cpu may be offline and all workers have
60 * %WORKER_UNBOUND set and concurrency management disabled, and may
Tejun Heo24647572013-01-24 11:01:33 -080061 * be executing on any CPU. The pool behaves as an unbound one.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070062 *
63 * Note that DISASSOCIATED can be flipped only while holding
Tejun Heo24647572013-01-24 11:01:33 -080064 * assoc_mutex to avoid changing binding state while
65 * create_worker() is in progress.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070066 */
Tejun Heo11ebea52012-07-12 14:46:37 -070067 POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
Tejun Heo24647572013-01-24 11:01:33 -080068 POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */
Tejun Heo35b6bb62013-01-24 11:01:33 -080069 POOL_FREEZING = 1 << 3, /* freeze in progress */
Tejun Heodb7bccf2010-06-29 10:07:12 +020070
Tejun Heoc8e55f32010-06-29 10:07:12 +020071 /* worker flags */
72 WORKER_STARTED = 1 << 0, /* started */
73 WORKER_DIE = 1 << 1, /* die die die */
74 WORKER_IDLE = 1 << 2, /* is idle */
Tejun Heoe22bee72010-06-29 10:07:14 +020075 WORKER_PREP = 1 << 3, /* preparing to run works */
Tejun Heofb0e7be2010-06-29 10:07:15 +020076 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
Tejun Heof3421792010-07-02 10:03:51 +020077 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
Tejun Heoe22bee72010-06-29 10:07:14 +020078
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -070079 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_UNBOUND |
Tejun Heo403c8212012-07-17 12:39:27 -070080 WORKER_CPU_INTENSIVE,
Tejun Heodb7bccf2010-06-29 10:07:12 +020081
Tejun Heoe34cdddb2013-01-24 11:01:33 -080082 NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */
Tejun Heo4ce62e92012-07-13 22:16:44 -070083
Tejun Heo29c91e92013-03-12 11:30:03 -070084 UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */
Tejun Heoc8e55f32010-06-29 10:07:12 +020085 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
Tejun Heodb7bccf2010-06-29 10:07:12 +020086
Tejun Heoe22bee72010-06-29 10:07:14 +020087 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
88 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
89
Tejun Heo3233cdb2011-02-16 18:10:19 +010090 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
91 /* call for help after 10ms
92 (min two ticks) */
Tejun Heoe22bee72010-06-29 10:07:14 +020093 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
94 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heoe22bee72010-06-29 10:07:14 +020095
96 /*
97 * Rescue workers are used only on emergencies and shared by
98 * all cpus. Give -20.
99 */
100 RESCUER_NICE_LEVEL = -20,
Tejun Heo32704762012-07-13 22:16:45 -0700101 HIGHPRI_NICE_LEVEL = -20,
Tejun Heoc8e55f32010-06-29 10:07:12 +0200102};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104/*
Tejun Heo4690c4a2010-06-29 10:07:10 +0200105 * Structure fields follow one of the following exclusion rules.
106 *
Tejun Heoe41e7042010-08-24 14:22:47 +0200107 * I: Modifiable by initialization/destruction paths and read-only for
108 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200109 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200110 * P: Preemption protected. Disabling preemption is enough and should
111 * only be modified and accessed from the local cpu.
112 *
Tejun Heod565ed62013-01-24 11:01:33 -0800113 * L: pool->lock protected. Access with pool->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200114 *
Tejun Heod565ed62013-01-24 11:01:33 -0800115 * X: During normal operation, modification requires pool->lock and should
116 * be done only from local cpu. Either disabling preemption on local
117 * cpu or grabbing pool->lock is enough for read access. If
118 * POOL_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200119 *
Tejun Heo73f53c42010-06-29 10:07:11 +0200120 * F: wq->flush_mutex protected.
121 *
Tejun Heo4690c4a2010-06-29 10:07:10 +0200122 * W: workqueue_lock protected.
Tejun Heo76af4d92013-03-12 11:30:00 -0700123 *
124 * R: workqueue_lock protected for writes. Sched-RCU protected for reads.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200125 */
126
Tejun Heo2eaebdb2013-01-18 14:05:55 -0800127/* struct worker is defined in workqueue_internal.h */
Tejun Heoc34056a2010-06-29 10:07:11 +0200128
Tejun Heobd7bdd42012-07-12 14:46:37 -0700129struct worker_pool {
Tejun Heod565ed62013-01-24 11:01:33 -0800130 spinlock_t lock; /* the pool lock */
Tejun Heod84ff052013-03-12 11:29:59 -0700131 int cpu; /* I: the associated cpu */
Tejun Heo9daf9e62013-01-24 11:01:33 -0800132 int id; /* I: pool ID */
Tejun Heo11ebea52012-07-12 14:46:37 -0700133 unsigned int flags; /* X: flags */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700134
135 struct list_head worklist; /* L: list of pending works */
136 int nr_workers; /* L: total number of workers */
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700137
138 /* nr_idle includes the ones off idle_list for rebinding */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700139 int nr_idle; /* L: currently idle ones */
140
141 struct list_head idle_list; /* X: list of idle workers */
142 struct timer_list idle_timer; /* L: worker idle timeout */
143 struct timer_list mayday_timer; /* L: SOS timer for workers */
144
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800145 /* workers are chained either in busy_hash or idle_list */
146 DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER);
147 /* L: hash of busy workers */
148
Tejun Heo34a06bd2013-03-12 11:30:00 -0700149 struct mutex manager_arb; /* manager arbitration */
Tejun Heo24647572013-01-24 11:01:33 -0800150 struct mutex assoc_mutex; /* protect POOL_DISASSOCIATED */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700151 struct ida worker_ida; /* L: for worker IDs */
Tejun Heoe19e3972013-01-24 11:39:44 -0800152
Tejun Heo7a4e3442013-03-12 11:30:00 -0700153 struct workqueue_attrs *attrs; /* I: worker attributes */
Tejun Heo29c91e92013-03-12 11:30:03 -0700154 struct hlist_node hash_node; /* R: unbound_pool_hash node */
155 int refcnt; /* refcnt for unbound pools */
Tejun Heo7a4e3442013-03-12 11:30:00 -0700156
Tejun Heoe19e3972013-01-24 11:39:44 -0800157 /*
158 * The current concurrency level. As it's likely to be accessed
159 * from other CPUs during try_to_wake_up(), put it in a separate
160 * cacheline.
161 */
162 atomic_t nr_running ____cacheline_aligned_in_smp;
Tejun Heo29c91e92013-03-12 11:30:03 -0700163
164 /*
165 * Destruction of pool is sched-RCU protected to allow dereferences
166 * from get_work_pool().
167 */
168 struct rcu_head rcu;
Tejun Heo8b03ae32010-06-29 10:07:12 +0200169} ____cacheline_aligned_in_smp;
170
171/*
Tejun Heo112202d2013-02-13 19:29:12 -0800172 * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS
173 * of work_struct->data are used for flags and the remaining high bits
174 * point to the pwq; thus, pwqs need to be aligned at two's power of the
175 * number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 */
Tejun Heo112202d2013-02-13 19:29:12 -0800177struct pool_workqueue {
Tejun Heobd7bdd42012-07-12 14:46:37 -0700178 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200179 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200180 int work_color; /* L: current color */
181 int flush_color; /* L: flushing color */
182 int nr_in_flight[WORK_NR_COLORS];
183 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200184 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200185 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200186 struct list_head delayed_works; /* L: delayed works */
Tejun Heo76af4d92013-03-12 11:30:00 -0700187 struct list_head pwqs_node; /* R: node on wq->pwqs */
Tejun Heo493a1722013-03-12 11:29:59 -0700188 struct list_head mayday_node; /* W: node on wq->maydays */
Tejun Heoe904e6c2013-03-12 11:29:57 -0700189} __aligned(1 << WORK_STRUCT_FLAG_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200192 * Structure used to wait for workqueue flush.
193 */
194struct wq_flusher {
195 struct list_head list; /* F: list of flushers */
196 int flush_color; /* F: flush color waiting for */
197 struct completion done; /* flush completion */
198};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Tejun Heo73f53c42010-06-29 10:07:11 +0200200/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 * The externally visible workqueue abstraction is an array of
202 * per-CPU workqueues:
203 */
204struct workqueue_struct {
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200205 unsigned int flags; /* W: WQ_* flags */
Tejun Heo420c0dd2013-03-12 11:29:59 -0700206 struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwq's */
Tejun Heo76af4d92013-03-12 11:30:00 -0700207 struct list_head pwqs; /* R: all pwqs of this wq */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200208 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200209
210 struct mutex flush_mutex; /* protects wq flushing */
211 int work_color; /* F: current work color */
212 int flush_color; /* F: current flush color */
Tejun Heo112202d2013-02-13 19:29:12 -0800213 atomic_t nr_pwqs_to_flush; /* flush in progress */
Tejun Heo73f53c42010-06-29 10:07:11 +0200214 struct wq_flusher *first_flusher; /* F: first flusher */
215 struct list_head flusher_queue; /* F: flush waiters */
216 struct list_head flusher_overflow; /* F: flush overflow list */
217
Tejun Heo493a1722013-03-12 11:29:59 -0700218 struct list_head maydays; /* W: pwqs requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200219 struct worker *rescuer; /* I: rescue worker */
220
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200221 int nr_drainers; /* W: drain in progress */
Tejun Heo112202d2013-02-13 19:29:12 -0800222 int saved_max_active; /* W: saved pwq max_active */
Johannes Berg4e6045f2007-10-18 23:39:55 -0700223#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200224 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700225#endif
Tejun Heob196be82012-01-10 15:11:35 -0800226 char name[]; /* I: workqueue name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227};
228
Tejun Heoe904e6c2013-03-12 11:29:57 -0700229static struct kmem_cache *pwq_cache;
230
Tejun Heo29c91e92013-03-12 11:30:03 -0700231/* hash of all unbound pools keyed by pool->attrs */
232static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER);
233
234static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS];
235
Tejun Heod320c032010-06-29 10:07:14 +0200236struct workqueue_struct *system_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200237EXPORT_SYMBOL_GPL(system_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300238struct workqueue_struct *system_highpri_wq __read_mostly;
Joonsoo Kim1aabe902012-08-15 23:25:39 +0900239EXPORT_SYMBOL_GPL(system_highpri_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300240struct workqueue_struct *system_long_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200241EXPORT_SYMBOL_GPL(system_long_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300242struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200243EXPORT_SYMBOL_GPL(system_unbound_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300244struct workqueue_struct *system_freezable_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100245EXPORT_SYMBOL_GPL(system_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200246
Tejun Heo97bd2342010-10-05 10:41:14 +0200247#define CREATE_TRACE_POINTS
248#include <trace/events/workqueue.h>
249
Tejun Heo76af4d92013-03-12 11:30:00 -0700250#define assert_rcu_or_wq_lock() \
251 rcu_lockdep_assert(rcu_read_lock_sched_held() || \
252 lockdep_is_held(&workqueue_lock), \
253 "sched RCU or workqueue lock should be held")
254
Tejun Heo38db41d2013-01-24 11:01:34 -0800255#define for_each_std_worker_pool(pool, cpu) \
Tejun Heoa60dc392013-01-24 11:01:34 -0800256 for ((pool) = &std_worker_pools(cpu)[0]; \
257 (pool) < &std_worker_pools(cpu)[NR_STD_WORKER_POOLS]; (pool)++)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700258
Sasha Levinb67bfe02013-02-27 17:06:00 -0800259#define for_each_busy_worker(worker, i, pool) \
260 hash_for_each(pool->busy_hash, i, worker, hentry)
Tejun Heodb7bccf2010-06-29 10:07:12 +0200261
Tejun Heo706026c2013-01-24 11:01:34 -0800262static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
263 unsigned int sw)
Tejun Heof3421792010-07-02 10:03:51 +0200264{
265 if (cpu < nr_cpu_ids) {
266 if (sw & 1) {
267 cpu = cpumask_next(cpu, mask);
268 if (cpu < nr_cpu_ids)
269 return cpu;
270 }
271 if (sw & 2)
272 return WORK_CPU_UNBOUND;
273 }
Lai Jiangshan6be19582013-02-06 18:04:53 -0800274 return WORK_CPU_END;
Tejun Heof3421792010-07-02 10:03:51 +0200275}
276
Tejun Heo09884952010-08-01 11:50:12 +0200277/*
278 * CPU iterators
279 *
Tejun Heo706026c2013-01-24 11:01:34 -0800280 * An extra cpu number is defined using an invalid cpu number
Tejun Heo09884952010-08-01 11:50:12 +0200281 * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any
Tejun Heo706026c2013-01-24 11:01:34 -0800282 * specific CPU. The following iterators are similar to for_each_*_cpu()
283 * iterators but also considers the unbound CPU.
Tejun Heo09884952010-08-01 11:50:12 +0200284 *
Tejun Heo706026c2013-01-24 11:01:34 -0800285 * for_each_wq_cpu() : possible CPUs + WORK_CPU_UNBOUND
286 * for_each_online_wq_cpu() : online CPUs + WORK_CPU_UNBOUND
Tejun Heo09884952010-08-01 11:50:12 +0200287 */
Tejun Heo706026c2013-01-24 11:01:34 -0800288#define for_each_wq_cpu(cpu) \
289 for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, 3); \
Lai Jiangshan6be19582013-02-06 18:04:53 -0800290 (cpu) < WORK_CPU_END; \
Tejun Heo706026c2013-01-24 11:01:34 -0800291 (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, 3))
Tejun Heof3421792010-07-02 10:03:51 +0200292
Tejun Heo706026c2013-01-24 11:01:34 -0800293#define for_each_online_wq_cpu(cpu) \
294 for ((cpu) = __next_wq_cpu(-1, cpu_online_mask, 3); \
Lai Jiangshan6be19582013-02-06 18:04:53 -0800295 (cpu) < WORK_CPU_END; \
Tejun Heo706026c2013-01-24 11:01:34 -0800296 (cpu) = __next_wq_cpu((cpu), cpu_online_mask, 3))
Tejun Heof3421792010-07-02 10:03:51 +0200297
Tejun Heo49e3cf42013-03-12 11:29:58 -0700298/**
Tejun Heo17116962013-03-12 11:29:58 -0700299 * for_each_pool - iterate through all worker_pools in the system
300 * @pool: iteration cursor
301 * @id: integer used for iteration
Tejun Heofa1b54e2013-03-12 11:30:00 -0700302 *
303 * This must be called either with workqueue_lock held or sched RCU read
304 * locked. If the pool needs to be used beyond the locking in effect, the
305 * caller is responsible for guaranteeing that the pool stays online.
306 *
307 * The if/else clause exists only for the lockdep assertion and can be
308 * ignored.
Tejun Heo17116962013-03-12 11:29:58 -0700309 */
310#define for_each_pool(pool, id) \
Tejun Heofa1b54e2013-03-12 11:30:00 -0700311 idr_for_each_entry(&worker_pool_idr, pool, id) \
312 if (({ assert_rcu_or_wq_lock(); false; })) { } \
313 else
Tejun Heo17116962013-03-12 11:29:58 -0700314
315/**
Tejun Heo49e3cf42013-03-12 11:29:58 -0700316 * for_each_pwq - iterate through all pool_workqueues of the specified workqueue
317 * @pwq: iteration cursor
318 * @wq: the target workqueue
Tejun Heo76af4d92013-03-12 11:30:00 -0700319 *
320 * This must be called either with workqueue_lock held or sched RCU read
321 * locked. If the pwq needs to be used beyond the locking in effect, the
322 * caller is responsible for guaranteeing that the pwq stays online.
323 *
324 * The if/else clause exists only for the lockdep assertion and can be
325 * ignored.
Tejun Heo49e3cf42013-03-12 11:29:58 -0700326 */
327#define for_each_pwq(pwq, wq) \
Tejun Heo76af4d92013-03-12 11:30:00 -0700328 list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node) \
329 if (({ assert_rcu_or_wq_lock(); false; })) { } \
330 else
Tejun Heof3421792010-07-02 10:03:51 +0200331
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900332#ifdef CONFIG_DEBUG_OBJECTS_WORK
333
334static struct debug_obj_descr work_debug_descr;
335
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100336static void *work_debug_hint(void *addr)
337{
338 return ((struct work_struct *) addr)->func;
339}
340
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900341/*
342 * fixup_init is called when:
343 * - an active object is initialized
344 */
345static int work_fixup_init(void *addr, enum debug_obj_state state)
346{
347 struct work_struct *work = addr;
348
349 switch (state) {
350 case ODEBUG_STATE_ACTIVE:
351 cancel_work_sync(work);
352 debug_object_init(work, &work_debug_descr);
353 return 1;
354 default:
355 return 0;
356 }
357}
358
359/*
360 * fixup_activate is called when:
361 * - an active object is activated
362 * - an unknown object is activated (might be a statically initialized object)
363 */
364static int work_fixup_activate(void *addr, enum debug_obj_state state)
365{
366 struct work_struct *work = addr;
367
368 switch (state) {
369
370 case ODEBUG_STATE_NOTAVAILABLE:
371 /*
372 * This is not really a fixup. The work struct was
373 * statically initialized. We just make sure that it
374 * is tracked in the object tracker.
375 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200376 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900377 debug_object_init(work, &work_debug_descr);
378 debug_object_activate(work, &work_debug_descr);
379 return 0;
380 }
381 WARN_ON_ONCE(1);
382 return 0;
383
384 case ODEBUG_STATE_ACTIVE:
385 WARN_ON(1);
386
387 default:
388 return 0;
389 }
390}
391
392/*
393 * fixup_free is called when:
394 * - an active object is freed
395 */
396static int work_fixup_free(void *addr, enum debug_obj_state state)
397{
398 struct work_struct *work = addr;
399
400 switch (state) {
401 case ODEBUG_STATE_ACTIVE:
402 cancel_work_sync(work);
403 debug_object_free(work, &work_debug_descr);
404 return 1;
405 default:
406 return 0;
407 }
408}
409
410static struct debug_obj_descr work_debug_descr = {
411 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100412 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900413 .fixup_init = work_fixup_init,
414 .fixup_activate = work_fixup_activate,
415 .fixup_free = work_fixup_free,
416};
417
418static inline void debug_work_activate(struct work_struct *work)
419{
420 debug_object_activate(work, &work_debug_descr);
421}
422
423static inline void debug_work_deactivate(struct work_struct *work)
424{
425 debug_object_deactivate(work, &work_debug_descr);
426}
427
428void __init_work(struct work_struct *work, int onstack)
429{
430 if (onstack)
431 debug_object_init_on_stack(work, &work_debug_descr);
432 else
433 debug_object_init(work, &work_debug_descr);
434}
435EXPORT_SYMBOL_GPL(__init_work);
436
437void destroy_work_on_stack(struct work_struct *work)
438{
439 debug_object_free(work, &work_debug_descr);
440}
441EXPORT_SYMBOL_GPL(destroy_work_on_stack);
442
443#else
444static inline void debug_work_activate(struct work_struct *work) { }
445static inline void debug_work_deactivate(struct work_struct *work) { }
446#endif
447
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100448/* Serializes the accesses to the list of workqueues. */
449static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450static LIST_HEAD(workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200451static bool workqueue_freezing; /* W: have wqs started freezing? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Oleg Nesterov14441962007-05-23 13:57:57 -0700453/*
Tejun Heoe19e3972013-01-24 11:39:44 -0800454 * The CPU and unbound standard worker pools. The unbound ones have
455 * POOL_DISASSOCIATED set, and their workers have WORKER_UNBOUND set.
Oleg Nesterov14441962007-05-23 13:57:57 -0700456 */
Tejun Heoe19e3972013-01-24 11:39:44 -0800457static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS],
458 cpu_std_worker_pools);
Tejun Heoa60dc392013-01-24 11:01:34 -0800459static struct worker_pool unbound_std_worker_pools[NR_STD_WORKER_POOLS];
Tejun Heof3421792010-07-02 10:03:51 +0200460
Tejun Heofa1b54e2013-03-12 11:30:00 -0700461/*
462 * idr of all pools. Modifications are protected by workqueue_lock. Read
463 * accesses are protected by sched-RCU protected.
464 */
Tejun Heo9daf9e62013-01-24 11:01:33 -0800465static DEFINE_IDR(worker_pool_idr);
466
Tejun Heoc34056a2010-06-29 10:07:11 +0200467static int worker_thread(void *__worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Tejun Heoa60dc392013-01-24 11:01:34 -0800469static struct worker_pool *std_worker_pools(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Tejun Heof3421792010-07-02 10:03:51 +0200471 if (cpu != WORK_CPU_UNBOUND)
Tejun Heoa60dc392013-01-24 11:01:34 -0800472 return per_cpu(cpu_std_worker_pools, cpu);
Tejun Heof3421792010-07-02 10:03:51 +0200473 else
Tejun Heoa60dc392013-01-24 11:01:34 -0800474 return unbound_std_worker_pools;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
Tejun Heo4e8f0a62013-01-24 11:01:34 -0800477static int std_worker_pool_pri(struct worker_pool *pool)
478{
Tejun Heoa60dc392013-01-24 11:01:34 -0800479 return pool - std_worker_pools(pool->cpu);
Tejun Heo4e8f0a62013-01-24 11:01:34 -0800480}
481
Tejun Heo9daf9e62013-01-24 11:01:33 -0800482/* allocate ID and assign it to @pool */
483static int worker_pool_assign_id(struct worker_pool *pool)
484{
485 int ret;
486
Tejun Heofa1b54e2013-03-12 11:30:00 -0700487 do {
488 if (!idr_pre_get(&worker_pool_idr, GFP_KERNEL))
489 return -ENOMEM;
490
491 spin_lock_irq(&workqueue_lock);
492 ret = idr_get_new(&worker_pool_idr, pool, &pool->id);
493 spin_unlock_irq(&workqueue_lock);
494 } while (ret == -EAGAIN);
Tejun Heo9daf9e62013-01-24 11:01:33 -0800495
496 return ret;
497}
498
Tejun Heod565ed62013-01-24 11:01:33 -0800499static struct worker_pool *get_std_worker_pool(int cpu, bool highpri)
500{
Tejun Heoa60dc392013-01-24 11:01:34 -0800501 struct worker_pool *pools = std_worker_pools(cpu);
Tejun Heod565ed62013-01-24 11:01:33 -0800502
Tejun Heoa60dc392013-01-24 11:01:34 -0800503 return &pools[highpri];
Tejun Heod565ed62013-01-24 11:01:33 -0800504}
505
Tejun Heo76af4d92013-03-12 11:30:00 -0700506/**
507 * first_pwq - return the first pool_workqueue of the specified workqueue
508 * @wq: the target workqueue
509 *
510 * This must be called either with workqueue_lock held or sched RCU read
511 * locked. If the pwq needs to be used beyond the locking in effect, the
512 * caller is responsible for guaranteeing that the pwq stays online.
513 */
Tejun Heo7fb98ea2013-03-12 11:30:00 -0700514static struct pool_workqueue *first_pwq(struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700515{
Tejun Heo76af4d92013-03-12 11:30:00 -0700516 assert_rcu_or_wq_lock();
517 return list_first_or_null_rcu(&wq->pwqs, struct pool_workqueue,
518 pwqs_node);
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700519}
520
Tejun Heo73f53c42010-06-29 10:07:11 +0200521static unsigned int work_color_to_flags(int color)
522{
523 return color << WORK_STRUCT_COLOR_SHIFT;
524}
525
526static int get_work_color(struct work_struct *work)
527{
528 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
529 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
530}
531
532static int work_next_color(int color)
533{
534 return (color + 1) % WORK_NR_COLORS;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700535}
536
David Howells4594bf12006-12-07 11:33:26 +0000537/*
Tejun Heo112202d2013-02-13 19:29:12 -0800538 * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data
539 * contain the pointer to the queued pwq. Once execution starts, the flag
Tejun Heo7c3eed52013-01-24 11:01:33 -0800540 * is cleared and the high bits contain OFFQ flags and pool ID.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200541 *
Tejun Heo112202d2013-02-13 19:29:12 -0800542 * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling()
543 * and clear_work_data() can be used to set the pwq, pool or clear
Tejun Heobbb68df2012-08-03 10:30:46 -0700544 * work->data. These functions should only be called while the work is
545 * owned - ie. while the PENDING bit is set.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200546 *
Tejun Heo112202d2013-02-13 19:29:12 -0800547 * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq
Tejun Heo7c3eed52013-01-24 11:01:33 -0800548 * corresponding to a work. Pool is available once the work has been
Tejun Heo112202d2013-02-13 19:29:12 -0800549 * queued anywhere after initialization until it is sync canceled. pwq is
Tejun Heo7c3eed52013-01-24 11:01:33 -0800550 * available only while the work item is queued.
Tejun Heobbb68df2012-08-03 10:30:46 -0700551 *
552 * %WORK_OFFQ_CANCELING is used to mark a work item which is being
553 * canceled. While being canceled, a work item may have its PENDING set
554 * but stay off timer and worklist for arbitrarily long and nobody should
555 * try to steal the PENDING bit.
David Howells4594bf12006-12-07 11:33:26 +0000556 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200557static inline void set_work_data(struct work_struct *work, unsigned long data,
558 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000559{
Tejun Heo6183c002013-03-12 11:29:57 -0700560 WARN_ON_ONCE(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200561 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000562}
David Howells365970a2006-11-22 14:54:49 +0000563
Tejun Heo112202d2013-02-13 19:29:12 -0800564static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq,
Tejun Heo7a22ad72010-06-29 10:07:13 +0200565 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200566{
Tejun Heo112202d2013-02-13 19:29:12 -0800567 set_work_data(work, (unsigned long)pwq,
568 WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200569}
570
Lai Jiangshan4468a002013-02-06 18:04:53 -0800571static void set_work_pool_and_keep_pending(struct work_struct *work,
572 int pool_id)
573{
574 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT,
575 WORK_STRUCT_PENDING);
576}
577
Tejun Heo7c3eed52013-01-24 11:01:33 -0800578static void set_work_pool_and_clear_pending(struct work_struct *work,
579 int pool_id)
David Howells365970a2006-11-22 14:54:49 +0000580{
Tejun Heo23657bb2012-08-13 17:08:19 -0700581 /*
582 * The following wmb is paired with the implied mb in
583 * test_and_set_bit(PENDING) and ensures all updates to @work made
584 * here are visible to and precede any updates by the next PENDING
585 * owner.
586 */
587 smp_wmb();
Tejun Heo7c3eed52013-01-24 11:01:33 -0800588 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200589}
590
591static void clear_work_data(struct work_struct *work)
592{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800593 smp_wmb(); /* see set_work_pool_and_clear_pending() */
594 set_work_data(work, WORK_STRUCT_NO_POOL, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200595}
596
Tejun Heo112202d2013-02-13 19:29:12 -0800597static struct pool_workqueue *get_work_pwq(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200598{
Tejun Heoe1201532010-07-22 14:14:25 +0200599 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200600
Tejun Heo112202d2013-02-13 19:29:12 -0800601 if (data & WORK_STRUCT_PWQ)
Tejun Heoe1201532010-07-22 14:14:25 +0200602 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
603 else
604 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200605}
606
Tejun Heo7c3eed52013-01-24 11:01:33 -0800607/**
608 * get_work_pool - return the worker_pool a given work was associated with
609 * @work: the work item of interest
610 *
611 * Return the worker_pool @work was last associated with. %NULL if none.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700612 *
613 * Pools are created and destroyed under workqueue_lock, and allows read
614 * access under sched-RCU read lock. As such, this function should be
615 * called under workqueue_lock or with preemption disabled.
616 *
617 * All fields of the returned pool are accessible as long as the above
618 * mentioned locking is in effect. If the returned pool needs to be used
619 * beyond the critical section, the caller is responsible for ensuring the
620 * returned pool is and stays online.
Tejun Heo7c3eed52013-01-24 11:01:33 -0800621 */
622static struct worker_pool *get_work_pool(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200623{
Tejun Heoe1201532010-07-22 14:14:25 +0200624 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800625 int pool_id;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200626
Tejun Heofa1b54e2013-03-12 11:30:00 -0700627 assert_rcu_or_wq_lock();
628
Tejun Heo112202d2013-02-13 19:29:12 -0800629 if (data & WORK_STRUCT_PWQ)
630 return ((struct pool_workqueue *)
Tejun Heo7c3eed52013-01-24 11:01:33 -0800631 (data & WORK_STRUCT_WQ_DATA_MASK))->pool;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200632
Tejun Heo7c3eed52013-01-24 11:01:33 -0800633 pool_id = data >> WORK_OFFQ_POOL_SHIFT;
634 if (pool_id == WORK_OFFQ_POOL_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200635 return NULL;
636
Tejun Heofa1b54e2013-03-12 11:30:00 -0700637 return idr_find(&worker_pool_idr, pool_id);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800638}
639
640/**
641 * get_work_pool_id - return the worker pool ID a given work is associated with
642 * @work: the work item of interest
643 *
644 * Return the worker_pool ID @work was last associated with.
645 * %WORK_OFFQ_POOL_NONE if none.
646 */
647static int get_work_pool_id(struct work_struct *work)
648{
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800649 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800650
Tejun Heo112202d2013-02-13 19:29:12 -0800651 if (data & WORK_STRUCT_PWQ)
652 return ((struct pool_workqueue *)
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800653 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id;
654
655 return data >> WORK_OFFQ_POOL_SHIFT;
Tejun Heo7c3eed52013-01-24 11:01:33 -0800656}
657
Tejun Heobbb68df2012-08-03 10:30:46 -0700658static void mark_work_canceling(struct work_struct *work)
659{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800660 unsigned long pool_id = get_work_pool_id(work);
Tejun Heobbb68df2012-08-03 10:30:46 -0700661
Tejun Heo7c3eed52013-01-24 11:01:33 -0800662 pool_id <<= WORK_OFFQ_POOL_SHIFT;
663 set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700664}
665
666static bool work_is_canceling(struct work_struct *work)
667{
668 unsigned long data = atomic_long_read(&work->data);
669
Tejun Heo112202d2013-02-13 19:29:12 -0800670 return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700671}
672
David Howells365970a2006-11-22 14:54:49 +0000673/*
Tejun Heo32704762012-07-13 22:16:45 -0700674 * Policy functions. These define the policies on how the global worker
675 * pools are managed. Unless noted otherwise, these functions assume that
Tejun Heod565ed62013-01-24 11:01:33 -0800676 * they're being called with pool->lock held.
David Howells365970a2006-11-22 14:54:49 +0000677 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200678
Tejun Heo63d95a92012-07-12 14:46:37 -0700679static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000680{
Tejun Heoe19e3972013-01-24 11:39:44 -0800681 return !atomic_read(&pool->nr_running);
David Howells365970a2006-11-22 14:54:49 +0000682}
683
Tejun Heoe22bee72010-06-29 10:07:14 +0200684/*
685 * Need to wake up a worker? Called from anything but currently
686 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700687 *
688 * Note that, because unbound workers never contribute to nr_running, this
Tejun Heo706026c2013-01-24 11:01:34 -0800689 * function will always return %true for unbound pools as long as the
Tejun Heo974271c2012-07-12 14:46:37 -0700690 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200691 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700692static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000693{
Tejun Heo63d95a92012-07-12 14:46:37 -0700694 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000695}
696
Tejun Heoe22bee72010-06-29 10:07:14 +0200697/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700698static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200699{
Tejun Heo63d95a92012-07-12 14:46:37 -0700700 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200701}
702
703/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700704static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200705{
Tejun Heoe19e3972013-01-24 11:39:44 -0800706 return !list_empty(&pool->worklist) &&
707 atomic_read(&pool->nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200708}
709
710/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700711static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200712{
Tejun Heo63d95a92012-07-12 14:46:37 -0700713 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200714}
715
716/* Do I need to be the manager? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700717static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200718{
Tejun Heo63d95a92012-07-12 14:46:37 -0700719 return need_to_create_worker(pool) ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700720 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200721}
722
723/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700724static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200725{
Tejun Heo34a06bd2013-03-12 11:30:00 -0700726 bool managing = mutex_is_locked(&pool->manager_arb);
Tejun Heo63d95a92012-07-12 14:46:37 -0700727 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
728 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200729
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700730 /*
731 * nr_idle and idle_list may disagree if idle rebinding is in
732 * progress. Never return %true if idle_list is empty.
733 */
734 if (list_empty(&pool->idle_list))
735 return false;
736
Tejun Heoe22bee72010-06-29 10:07:14 +0200737 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
738}
739
740/*
741 * Wake up functions.
742 */
743
Tejun Heo7e116292010-06-29 10:07:13 +0200744/* Return the first worker. Safe with preemption disabled */
Tejun Heo63d95a92012-07-12 14:46:37 -0700745static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200746{
Tejun Heo63d95a92012-07-12 14:46:37 -0700747 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200748 return NULL;
749
Tejun Heo63d95a92012-07-12 14:46:37 -0700750 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200751}
752
753/**
754 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700755 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200756 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700757 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200758 *
759 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800760 * spin_lock_irq(pool->lock).
Tejun Heo7e116292010-06-29 10:07:13 +0200761 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700762static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200763{
Tejun Heo63d95a92012-07-12 14:46:37 -0700764 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200765
766 if (likely(worker))
767 wake_up_process(worker->task);
768}
769
Tejun Heo4690c4a2010-06-29 10:07:10 +0200770/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200771 * wq_worker_waking_up - a worker is waking up
772 * @task: task waking up
773 * @cpu: CPU @task is waking up to
774 *
775 * This function is called during try_to_wake_up() when a worker is
776 * being awoken.
777 *
778 * CONTEXT:
779 * spin_lock_irq(rq->lock)
780 */
Tejun Heod84ff052013-03-12 11:29:59 -0700781void wq_worker_waking_up(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200782{
783 struct worker *worker = kthread_data(task);
784
Joonsoo Kim36576002012-10-26 23:03:49 +0900785 if (!(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoec22ca52013-01-24 11:01:33 -0800786 WARN_ON_ONCE(worker->pool->cpu != cpu);
Tejun Heoe19e3972013-01-24 11:39:44 -0800787 atomic_inc(&worker->pool->nr_running);
Joonsoo Kim36576002012-10-26 23:03:49 +0900788 }
Tejun Heoe22bee72010-06-29 10:07:14 +0200789}
790
791/**
792 * wq_worker_sleeping - a worker is going to sleep
793 * @task: task going to sleep
794 * @cpu: CPU in question, must be the current CPU number
795 *
796 * This function is called during schedule() when a busy worker is
797 * going to sleep. Worker on the same cpu can be woken up by
798 * returning pointer to its task.
799 *
800 * CONTEXT:
801 * spin_lock_irq(rq->lock)
802 *
803 * RETURNS:
804 * Worker task on @cpu to wake up, %NULL if none.
805 */
Tejun Heod84ff052013-03-12 11:29:59 -0700806struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200807{
808 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heo111c2252013-01-17 17:16:24 -0800809 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200810
Tejun Heo111c2252013-01-17 17:16:24 -0800811 /*
812 * Rescuers, which may not have all the fields set up like normal
813 * workers, also reach here, let's not access anything before
814 * checking NOT_RUNNING.
815 */
Steven Rostedt2d646722010-12-03 23:12:33 -0500816 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200817 return NULL;
818
Tejun Heo111c2252013-01-17 17:16:24 -0800819 pool = worker->pool;
Tejun Heo111c2252013-01-17 17:16:24 -0800820
Tejun Heoe22bee72010-06-29 10:07:14 +0200821 /* this can only happen on the local cpu */
Tejun Heo6183c002013-03-12 11:29:57 -0700822 if (WARN_ON_ONCE(cpu != raw_smp_processor_id()))
823 return NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +0200824
825 /*
826 * The counterpart of the following dec_and_test, implied mb,
827 * worklist not empty test sequence is in insert_work().
828 * Please read comment there.
829 *
Tejun Heo628c78e2012-07-17 12:39:27 -0700830 * NOT_RUNNING is clear. This means that we're bound to and
831 * running on the local cpu w/ rq lock held and preemption
832 * disabled, which in turn means that none else could be
Tejun Heod565ed62013-01-24 11:01:33 -0800833 * manipulating idle_list, so dereferencing idle_list without pool
Tejun Heo628c78e2012-07-17 12:39:27 -0700834 * lock is safe.
Tejun Heoe22bee72010-06-29 10:07:14 +0200835 */
Tejun Heoe19e3972013-01-24 11:39:44 -0800836 if (atomic_dec_and_test(&pool->nr_running) &&
837 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700838 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200839 return to_wakeup ? to_wakeup->task : NULL;
840}
841
842/**
843 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200844 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200845 * @flags: flags to set
846 * @wakeup: wakeup an idle worker if necessary
847 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200848 * Set @flags in @worker->flags and adjust nr_running accordingly. If
849 * nr_running becomes zero and @wakeup is %true, an idle worker is
850 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200851 *
Tejun Heocb444762010-07-02 10:03:50 +0200852 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800853 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200854 */
855static inline void worker_set_flags(struct worker *worker, unsigned int flags,
856 bool wakeup)
857{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700858 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200859
Tejun Heocb444762010-07-02 10:03:50 +0200860 WARN_ON_ONCE(worker->task != current);
861
Tejun Heoe22bee72010-06-29 10:07:14 +0200862 /*
863 * If transitioning into NOT_RUNNING, adjust nr_running and
864 * wake up an idle worker as necessary if requested by
865 * @wakeup.
866 */
867 if ((flags & WORKER_NOT_RUNNING) &&
868 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoe22bee72010-06-29 10:07:14 +0200869 if (wakeup) {
Tejun Heoe19e3972013-01-24 11:39:44 -0800870 if (atomic_dec_and_test(&pool->nr_running) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -0700871 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700872 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200873 } else
Tejun Heoe19e3972013-01-24 11:39:44 -0800874 atomic_dec(&pool->nr_running);
Tejun Heoe22bee72010-06-29 10:07:14 +0200875 }
876
Tejun Heod302f012010-06-29 10:07:13 +0200877 worker->flags |= flags;
878}
879
880/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200881 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200882 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200883 * @flags: flags to clear
884 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200885 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200886 *
Tejun Heocb444762010-07-02 10:03:50 +0200887 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800888 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200889 */
890static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
891{
Tejun Heo63d95a92012-07-12 14:46:37 -0700892 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200893 unsigned int oflags = worker->flags;
894
Tejun Heocb444762010-07-02 10:03:50 +0200895 WARN_ON_ONCE(worker->task != current);
896
Tejun Heod302f012010-06-29 10:07:13 +0200897 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200898
Tejun Heo42c025f2011-01-11 15:58:49 +0100899 /*
900 * If transitioning out of NOT_RUNNING, increment nr_running. Note
901 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
902 * of multiple flags, not a single flag.
903 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200904 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
905 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heoe19e3972013-01-24 11:39:44 -0800906 atomic_inc(&pool->nr_running);
Tejun Heod302f012010-06-29 10:07:13 +0200907}
908
909/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200910 * find_worker_executing_work - find worker which is executing a work
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800911 * @pool: pool of interest
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200912 * @work: work to find worker for
913 *
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800914 * Find a worker which is executing @work on @pool by searching
915 * @pool->busy_hash which is keyed by the address of @work. For a worker
Tejun Heoa2c1c572012-12-18 10:35:02 -0800916 * to match, its current execution should match the address of @work and
917 * its work function. This is to avoid unwanted dependency between
918 * unrelated work executions through a work item being recycled while still
919 * being executed.
920 *
921 * This is a bit tricky. A work item may be freed once its execution
922 * starts and nothing prevents the freed area from being recycled for
923 * another work item. If the same work item address ends up being reused
924 * before the original execution finishes, workqueue will identify the
925 * recycled work item as currently executing and make it wait until the
926 * current execution finishes, introducing an unwanted dependency.
927 *
928 * This function checks the work item address, work function and workqueue
929 * to avoid false positives. Note that this isn't complete as one may
930 * construct a work function which can introduce dependency onto itself
931 * through a recycled work item. Well, if somebody wants to shoot oneself
932 * in the foot that badly, there's only so much we can do, and if such
933 * deadlock actually occurs, it should be easy to locate the culprit work
934 * function.
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200935 *
936 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800937 * spin_lock_irq(pool->lock).
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200938 *
939 * RETURNS:
940 * Pointer to worker which is executing @work if found, NULL
941 * otherwise.
942 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800943static struct worker *find_worker_executing_work(struct worker_pool *pool,
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200944 struct work_struct *work)
945{
Sasha Levin42f85702012-12-17 10:01:23 -0500946 struct worker *worker;
Sasha Levin42f85702012-12-17 10:01:23 -0500947
Sasha Levinb67bfe02013-02-27 17:06:00 -0800948 hash_for_each_possible(pool->busy_hash, worker, hentry,
Tejun Heoa2c1c572012-12-18 10:35:02 -0800949 (unsigned long)work)
950 if (worker->current_work == work &&
951 worker->current_func == work->func)
Sasha Levin42f85702012-12-17 10:01:23 -0500952 return worker;
953
954 return NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200955}
956
957/**
Tejun Heobf4ede02012-08-03 10:30:46 -0700958 * move_linked_works - move linked works to a list
959 * @work: start of series of works to be scheduled
960 * @head: target list to append @work to
961 * @nextp: out paramter for nested worklist walking
962 *
963 * Schedule linked works starting from @work to @head. Work series to
964 * be scheduled starts at @work and includes any consecutive work with
965 * WORK_STRUCT_LINKED set in its predecessor.
966 *
967 * If @nextp is not NULL, it's updated to point to the next work of
968 * the last scheduled work. This allows move_linked_works() to be
969 * nested inside outer list_for_each_entry_safe().
970 *
971 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800972 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -0700973 */
974static void move_linked_works(struct work_struct *work, struct list_head *head,
975 struct work_struct **nextp)
976{
977 struct work_struct *n;
978
979 /*
980 * Linked worklist will always end before the end of the list,
981 * use NULL for list head.
982 */
983 list_for_each_entry_safe_from(work, n, NULL, entry) {
984 list_move_tail(&work->entry, head);
985 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
986 break;
987 }
988
989 /*
990 * If we're already inside safe list traversal and have moved
991 * multiple works to the scheduled queue, the next position
992 * needs to be updated.
993 */
994 if (nextp)
995 *nextp = n;
996}
997
Tejun Heo112202d2013-02-13 19:29:12 -0800998static void pwq_activate_delayed_work(struct work_struct *work)
Tejun Heobf4ede02012-08-03 10:30:46 -0700999{
Tejun Heo112202d2013-02-13 19:29:12 -08001000 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobf4ede02012-08-03 10:30:46 -07001001
1002 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001003 move_linked_works(work, &pwq->pool->worklist, NULL);
Tejun Heobf4ede02012-08-03 10:30:46 -07001004 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo112202d2013-02-13 19:29:12 -08001005 pwq->nr_active++;
Tejun Heobf4ede02012-08-03 10:30:46 -07001006}
1007
Tejun Heo112202d2013-02-13 19:29:12 -08001008static void pwq_activate_first_delayed(struct pool_workqueue *pwq)
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001009{
Tejun Heo112202d2013-02-13 19:29:12 -08001010 struct work_struct *work = list_first_entry(&pwq->delayed_works,
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001011 struct work_struct, entry);
1012
Tejun Heo112202d2013-02-13 19:29:12 -08001013 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001014}
1015
Tejun Heobf4ede02012-08-03 10:30:46 -07001016/**
Tejun Heo112202d2013-02-13 19:29:12 -08001017 * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight
1018 * @pwq: pwq of interest
Tejun Heobf4ede02012-08-03 10:30:46 -07001019 * @color: color of work which left the queue
Tejun Heobf4ede02012-08-03 10:30:46 -07001020 *
1021 * A work either has completed or is removed from pending queue,
Tejun Heo112202d2013-02-13 19:29:12 -08001022 * decrement nr_in_flight of its pwq and handle workqueue flushing.
Tejun Heobf4ede02012-08-03 10:30:46 -07001023 *
1024 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001025 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -07001026 */
Tejun Heo112202d2013-02-13 19:29:12 -08001027static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color)
Tejun Heobf4ede02012-08-03 10:30:46 -07001028{
1029 /* ignore uncolored works */
1030 if (color == WORK_NO_COLOR)
1031 return;
1032
Tejun Heo112202d2013-02-13 19:29:12 -08001033 pwq->nr_in_flight[color]--;
Tejun Heobf4ede02012-08-03 10:30:46 -07001034
Tejun Heo112202d2013-02-13 19:29:12 -08001035 pwq->nr_active--;
1036 if (!list_empty(&pwq->delayed_works)) {
Lai Jiangshanb3f9f402012-09-18 10:40:00 -07001037 /* one down, submit a delayed one */
Tejun Heo112202d2013-02-13 19:29:12 -08001038 if (pwq->nr_active < pwq->max_active)
1039 pwq_activate_first_delayed(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001040 }
1041
1042 /* is flush in progress and are we at the flushing tip? */
Tejun Heo112202d2013-02-13 19:29:12 -08001043 if (likely(pwq->flush_color != color))
Tejun Heobf4ede02012-08-03 10:30:46 -07001044 return;
1045
1046 /* are there still in-flight works? */
Tejun Heo112202d2013-02-13 19:29:12 -08001047 if (pwq->nr_in_flight[color])
Tejun Heobf4ede02012-08-03 10:30:46 -07001048 return;
1049
Tejun Heo112202d2013-02-13 19:29:12 -08001050 /* this pwq is done, clear flush_color */
1051 pwq->flush_color = -1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001052
1053 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001054 * If this was the last pwq, wake up the first flusher. It
Tejun Heobf4ede02012-08-03 10:30:46 -07001055 * will handle the rest.
1056 */
Tejun Heo112202d2013-02-13 19:29:12 -08001057 if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush))
1058 complete(&pwq->wq->first_flusher->done);
Tejun Heobf4ede02012-08-03 10:30:46 -07001059}
1060
Tejun Heo36e227d2012-08-03 10:30:46 -07001061/**
Tejun Heobbb68df2012-08-03 10:30:46 -07001062 * try_to_grab_pending - steal work item from worklist and disable irq
Tejun Heo36e227d2012-08-03 10:30:46 -07001063 * @work: work item to steal
1064 * @is_dwork: @work is a delayed_work
Tejun Heobbb68df2012-08-03 10:30:46 -07001065 * @flags: place to store irq state
Tejun Heo36e227d2012-08-03 10:30:46 -07001066 *
1067 * Try to grab PENDING bit of @work. This function can handle @work in any
1068 * stable state - idle, on timer or on worklist. Return values are
1069 *
1070 * 1 if @work was pending and we successfully stole PENDING
1071 * 0 if @work was idle and we claimed PENDING
1072 * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry
Tejun Heobbb68df2012-08-03 10:30:46 -07001073 * -ENOENT if someone else is canceling @work, this state may persist
1074 * for arbitrarily long
Tejun Heo36e227d2012-08-03 10:30:46 -07001075 *
Tejun Heobbb68df2012-08-03 10:30:46 -07001076 * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001077 * interrupted while holding PENDING and @work off queue, irq must be
1078 * disabled on entry. This, combined with delayed_work->timer being
1079 * irqsafe, ensures that we return -EAGAIN for finite short period of time.
Tejun Heobbb68df2012-08-03 10:30:46 -07001080 *
1081 * On successful return, >= 0, irq is disabled and the caller is
1082 * responsible for releasing it using local_irq_restore(*@flags).
1083 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001084 * This function is safe to call from any context including IRQ handler.
Tejun Heobf4ede02012-08-03 10:30:46 -07001085 */
Tejun Heobbb68df2012-08-03 10:30:46 -07001086static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
1087 unsigned long *flags)
Tejun Heobf4ede02012-08-03 10:30:46 -07001088{
Tejun Heod565ed62013-01-24 11:01:33 -08001089 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08001090 struct pool_workqueue *pwq;
Tejun Heobf4ede02012-08-03 10:30:46 -07001091
Tejun Heobbb68df2012-08-03 10:30:46 -07001092 local_irq_save(*flags);
1093
Tejun Heo36e227d2012-08-03 10:30:46 -07001094 /* try to steal the timer if it exists */
1095 if (is_dwork) {
1096 struct delayed_work *dwork = to_delayed_work(work);
1097
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001098 /*
1099 * dwork->timer is irqsafe. If del_timer() fails, it's
1100 * guaranteed that the timer is not queued anywhere and not
1101 * running on the local CPU.
1102 */
Tejun Heo36e227d2012-08-03 10:30:46 -07001103 if (likely(del_timer(&dwork->timer)))
1104 return 1;
1105 }
1106
1107 /* try to claim PENDING the normal way */
Tejun Heobf4ede02012-08-03 10:30:46 -07001108 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1109 return 0;
1110
1111 /*
1112 * The queueing is in progress, or it is already queued. Try to
1113 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1114 */
Tejun Heod565ed62013-01-24 11:01:33 -08001115 pool = get_work_pool(work);
1116 if (!pool)
Tejun Heobbb68df2012-08-03 10:30:46 -07001117 goto fail;
Tejun Heobf4ede02012-08-03 10:30:46 -07001118
Tejun Heod565ed62013-01-24 11:01:33 -08001119 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001120 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001121 * work->data is guaranteed to point to pwq only while the work
1122 * item is queued on pwq->wq, and both updating work->data to point
1123 * to pwq on queueing and to pool on dequeueing are done under
1124 * pwq->pool->lock. This in turn guarantees that, if work->data
1125 * points to pwq which is associated with a locked pool, the work
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001126 * item is currently queued on that pool.
1127 */
Tejun Heo112202d2013-02-13 19:29:12 -08001128 pwq = get_work_pwq(work);
1129 if (pwq && pwq->pool == pool) {
Tejun Heo16062832013-02-06 18:04:53 -08001130 debug_work_deactivate(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001131
Tejun Heo16062832013-02-06 18:04:53 -08001132 /*
1133 * A delayed work item cannot be grabbed directly because
1134 * it might have linked NO_COLOR work items which, if left
Tejun Heo112202d2013-02-13 19:29:12 -08001135 * on the delayed_list, will confuse pwq->nr_active
Tejun Heo16062832013-02-06 18:04:53 -08001136 * management later on and cause stall. Make sure the work
1137 * item is activated before grabbing.
1138 */
1139 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
Tejun Heo112202d2013-02-13 19:29:12 -08001140 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001141
Tejun Heo16062832013-02-06 18:04:53 -08001142 list_del_init(&work->entry);
Tejun Heo112202d2013-02-13 19:29:12 -08001143 pwq_dec_nr_in_flight(get_work_pwq(work), get_work_color(work));
Tejun Heo36e227d2012-08-03 10:30:46 -07001144
Tejun Heo112202d2013-02-13 19:29:12 -08001145 /* work->data points to pwq iff queued, point to pool */
Tejun Heo16062832013-02-06 18:04:53 -08001146 set_work_pool_and_keep_pending(work, pool->id);
Lai Jiangshan4468a002013-02-06 18:04:53 -08001147
Tejun Heo16062832013-02-06 18:04:53 -08001148 spin_unlock(&pool->lock);
1149 return 1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001150 }
Tejun Heod565ed62013-01-24 11:01:33 -08001151 spin_unlock(&pool->lock);
Tejun Heobbb68df2012-08-03 10:30:46 -07001152fail:
1153 local_irq_restore(*flags);
1154 if (work_is_canceling(work))
1155 return -ENOENT;
1156 cpu_relax();
Tejun Heo36e227d2012-08-03 10:30:46 -07001157 return -EAGAIN;
Tejun Heobf4ede02012-08-03 10:30:46 -07001158}
1159
1160/**
Tejun Heo706026c2013-01-24 11:01:34 -08001161 * insert_work - insert a work into a pool
Tejun Heo112202d2013-02-13 19:29:12 -08001162 * @pwq: pwq @work belongs to
Tejun Heo4690c4a2010-06-29 10:07:10 +02001163 * @work: work to insert
1164 * @head: insertion point
1165 * @extra_flags: extra WORK_STRUCT_* flags to set
1166 *
Tejun Heo112202d2013-02-13 19:29:12 -08001167 * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to
Tejun Heo706026c2013-01-24 11:01:34 -08001168 * work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +02001169 *
1170 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001171 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02001172 */
Tejun Heo112202d2013-02-13 19:29:12 -08001173static void insert_work(struct pool_workqueue *pwq, struct work_struct *work,
1174 struct list_head *head, unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001175{
Tejun Heo112202d2013-02-13 19:29:12 -08001176 struct worker_pool *pool = pwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01001177
Tejun Heo4690c4a2010-06-29 10:07:10 +02001178 /* we own @work, set data and link */
Tejun Heo112202d2013-02-13 19:29:12 -08001179 set_work_pwq(work, pwq, extra_flags);
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -07001180 list_add_tail(&work->entry, head);
Tejun Heoe22bee72010-06-29 10:07:14 +02001181
1182 /*
1183 * Ensure either worker_sched_deactivated() sees the above
1184 * list_add_tail() or we see zero nr_running to avoid workers
1185 * lying around lazily while there are works to be processed.
1186 */
1187 smp_mb();
1188
Tejun Heo63d95a92012-07-12 14:46:37 -07001189 if (__need_more_worker(pool))
1190 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001191}
1192
Tejun Heoc8efcc22010-12-20 19:32:04 +01001193/*
1194 * Test whether @work is being queued from another work executing on the
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001195 * same workqueue.
Tejun Heoc8efcc22010-12-20 19:32:04 +01001196 */
1197static bool is_chained_work(struct workqueue_struct *wq)
1198{
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001199 struct worker *worker;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001200
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001201 worker = current_wq_worker();
1202 /*
1203 * Return %true iff I'm a worker execuing a work item on @wq. If
1204 * I'm @worker, it's safe to dereference it without locking.
1205 */
Tejun Heo112202d2013-02-13 19:29:12 -08001206 return worker && worker->current_pwq->wq == wq;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001207}
1208
Tejun Heod84ff052013-03-12 11:29:59 -07001209static void __queue_work(int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 struct work_struct *work)
1211{
Tejun Heo112202d2013-02-13 19:29:12 -08001212 struct pool_workqueue *pwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001213 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001214 unsigned int work_flags;
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001215 unsigned int req_cpu = cpu;
Tejun Heo8930cab2012-08-03 10:30:45 -07001216
1217 /*
1218 * While a work item is PENDING && off queue, a task trying to
1219 * steal the PENDING will busy-loop waiting for it to either get
1220 * queued or lose PENDING. Grabbing PENDING and queueing should
1221 * happen with IRQ disabled.
1222 */
1223 WARN_ON_ONCE(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001225 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001226
Tejun Heoc8efcc22010-12-20 19:32:04 +01001227 /* if dying, only works from the same workqueue are allowed */
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02001228 if (unlikely(wq->flags & WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001229 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001230 return;
1231
Tejun Heo112202d2013-02-13 19:29:12 -08001232 /* determine the pwq to use */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001233 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001234 struct worker_pool *last_pool;
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001235
Tejun Heo57469822012-08-03 10:30:45 -07001236 if (cpu == WORK_CPU_UNBOUND)
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001237 cpu = raw_smp_processor_id();
1238
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001239 /*
Tejun Heodbf25762012-08-20 14:51:23 -07001240 * It's multi cpu. If @work was previously on a different
1241 * cpu, it might still be running there, in which case the
1242 * work needs to be queued on that cpu to guarantee
1243 * non-reentrancy.
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001244 */
Tejun Heo7fb98ea2013-03-12 11:30:00 -07001245 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001246 last_pool = get_work_pool(work);
Tejun Heodbf25762012-08-20 14:51:23 -07001247
Tejun Heo112202d2013-02-13 19:29:12 -08001248 if (last_pool && last_pool != pwq->pool) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001249 struct worker *worker;
1250
Tejun Heod565ed62013-01-24 11:01:33 -08001251 spin_lock(&last_pool->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001252
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001253 worker = find_worker_executing_work(last_pool, work);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001254
Tejun Heo112202d2013-02-13 19:29:12 -08001255 if (worker && worker->current_pwq->wq == wq) {
Tejun Heo7fb98ea2013-03-12 11:30:00 -07001256 pwq = per_cpu_ptr(wq->cpu_pwqs, last_pool->cpu);
Lai Jiangshan8594fad2013-02-07 13:14:20 -08001257 } else {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001258 /* meh... not running there, queue here */
Tejun Heod565ed62013-01-24 11:01:33 -08001259 spin_unlock(&last_pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08001260 spin_lock(&pwq->pool->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001261 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001262 } else {
Tejun Heo112202d2013-02-13 19:29:12 -08001263 spin_lock(&pwq->pool->lock);
Tejun Heo8930cab2012-08-03 10:30:45 -07001264 }
Tejun Heof3421792010-07-02 10:03:51 +02001265 } else {
Tejun Heo7fb98ea2013-03-12 11:30:00 -07001266 pwq = first_pwq(wq);
Tejun Heo112202d2013-02-13 19:29:12 -08001267 spin_lock(&pwq->pool->lock);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001268 }
1269
Tejun Heo112202d2013-02-13 19:29:12 -08001270 /* pwq determined, queue */
1271 trace_workqueue_queue_work(req_cpu, pwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001272
Dan Carpenterf5b25522012-04-13 22:06:58 +03001273 if (WARN_ON(!list_empty(&work->entry))) {
Tejun Heo112202d2013-02-13 19:29:12 -08001274 spin_unlock(&pwq->pool->lock);
Dan Carpenterf5b25522012-04-13 22:06:58 +03001275 return;
1276 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001277
Tejun Heo112202d2013-02-13 19:29:12 -08001278 pwq->nr_in_flight[pwq->work_color]++;
1279 work_flags = work_color_to_flags(pwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001280
Tejun Heo112202d2013-02-13 19:29:12 -08001281 if (likely(pwq->nr_active < pwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001282 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001283 pwq->nr_active++;
1284 worklist = &pwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001285 } else {
1286 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo112202d2013-02-13 19:29:12 -08001287 worklist = &pwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001288 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001289
Tejun Heo112202d2013-02-13 19:29:12 -08001290 insert_work(pwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001291
Tejun Heo112202d2013-02-13 19:29:12 -08001292 spin_unlock(&pwq->pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293}
1294
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001295/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07001296 * queue_work_on - queue work on specific cpu
1297 * @cpu: CPU number to execute work on
1298 * @wq: workqueue to use
1299 * @work: work to queue
1300 *
Tejun Heod4283e92012-08-03 10:30:44 -07001301 * Returns %false if @work was already on a queue, %true otherwise.
Zhang Ruic1a220e2008-07-23 21:28:39 -07001302 *
1303 * We queue the work to a specific CPU, the caller must ensure it
1304 * can't go away.
1305 */
Tejun Heod4283e92012-08-03 10:30:44 -07001306bool queue_work_on(int cpu, struct workqueue_struct *wq,
1307 struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07001308{
Tejun Heod4283e92012-08-03 10:30:44 -07001309 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001310 unsigned long flags;
1311
1312 local_irq_save(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001313
Tejun Heo22df02b2010-06-29 10:07:10 +02001314 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001315 __queue_work(cpu, wq, work);
Tejun Heod4283e92012-08-03 10:30:44 -07001316 ret = true;
Zhang Ruic1a220e2008-07-23 21:28:39 -07001317 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001318
1319 local_irq_restore(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001320 return ret;
1321}
1322EXPORT_SYMBOL_GPL(queue_work_on);
1323
Tejun Heo0a13c002012-08-03 10:30:44 -07001324/**
1325 * queue_work - queue work on a workqueue
1326 * @wq: workqueue to use
1327 * @work: work to queue
1328 *
Tejun Heod4283e92012-08-03 10:30:44 -07001329 * Returns %false if @work was already on a queue, %true otherwise.
Tejun Heo0a13c002012-08-03 10:30:44 -07001330 *
1331 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1332 * it can be processed by another CPU.
1333 */
Tejun Heod4283e92012-08-03 10:30:44 -07001334bool queue_work(struct workqueue_struct *wq, struct work_struct *work)
Tejun Heo0a13c002012-08-03 10:30:44 -07001335{
Tejun Heo57469822012-08-03 10:30:45 -07001336 return queue_work_on(WORK_CPU_UNBOUND, wq, work);
Tejun Heo0a13c002012-08-03 10:30:44 -07001337}
1338EXPORT_SYMBOL_GPL(queue_work);
1339
Tejun Heod8e794d2012-08-03 10:30:45 -07001340void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
David Howells52bad642006-11-22 14:54:01 +00001342 struct delayed_work *dwork = (struct delayed_work *)__data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001344 /* should have been called from irqsafe timer with irq already off */
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001345 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346}
Konstantin Khlebnikov1438ade52013-01-24 16:36:31 +04001347EXPORT_SYMBOL(delayed_work_timer_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001349static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
1350 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001352 struct timer_list *timer = &dwork->timer;
1353 struct work_struct *work = &dwork->work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001355 WARN_ON_ONCE(timer->function != delayed_work_timer_fn ||
1356 timer->data != (unsigned long)dwork);
Tejun Heofc4b5142012-12-04 07:40:39 -08001357 WARN_ON_ONCE(timer_pending(timer));
1358 WARN_ON_ONCE(!list_empty(&work->entry));
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001359
Tejun Heo8852aac2012-12-01 16:23:42 -08001360 /*
1361 * If @delay is 0, queue @dwork->work immediately. This is for
1362 * both optimization and correctness. The earliest @timer can
1363 * expire is on the closest next tick and delayed_work users depend
1364 * on that there's no such delay when @delay is 0.
1365 */
1366 if (!delay) {
1367 __queue_work(cpu, wq, &dwork->work);
1368 return;
1369 }
1370
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001371 timer_stats_timer_set_start_info(&dwork->timer);
1372
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001373 dwork->wq = wq;
Tejun Heo12650572012-08-08 09:38:42 -07001374 dwork->cpu = cpu;
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001375 timer->expires = jiffies + delay;
1376
1377 if (unlikely(cpu != WORK_CPU_UNBOUND))
1378 add_timer_on(timer, cpu);
1379 else
1380 add_timer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381}
1382
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001383/**
1384 * queue_delayed_work_on - queue work on specific CPU after delay
1385 * @cpu: CPU number to execute work on
1386 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001387 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001388 * @delay: number of jiffies to wait before queueing
1389 *
Tejun Heo715f1302012-08-03 10:30:46 -07001390 * Returns %false if @work was already on a queue, %true otherwise. If
1391 * @delay is zero and @dwork is idle, it will be scheduled for immediate
1392 * execution.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001393 */
Tejun Heod4283e92012-08-03 10:30:44 -07001394bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
1395 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001396{
David Howells52bad642006-11-22 14:54:01 +00001397 struct work_struct *work = &dwork->work;
Tejun Heod4283e92012-08-03 10:30:44 -07001398 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001399 unsigned long flags;
1400
1401 /* read the comment in __queue_work() */
1402 local_irq_save(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001403
Tejun Heo22df02b2010-06-29 10:07:10 +02001404 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001405 __queue_delayed_work(cpu, wq, dwork, delay);
Tejun Heod4283e92012-08-03 10:30:44 -07001406 ret = true;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001407 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001408
1409 local_irq_restore(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001410 return ret;
1411}
Dave Jonesae90dd52006-06-30 01:40:45 -04001412EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Tejun Heoc8e55f32010-06-29 10:07:12 +02001414/**
Tejun Heo0a13c002012-08-03 10:30:44 -07001415 * queue_delayed_work - queue work on a workqueue after delay
1416 * @wq: workqueue to use
1417 * @dwork: delayable work to queue
1418 * @delay: number of jiffies to wait before queueing
1419 *
Tejun Heo715f1302012-08-03 10:30:46 -07001420 * Equivalent to queue_delayed_work_on() but tries to use the local CPU.
Tejun Heo0a13c002012-08-03 10:30:44 -07001421 */
Tejun Heod4283e92012-08-03 10:30:44 -07001422bool queue_delayed_work(struct workqueue_struct *wq,
Tejun Heo0a13c002012-08-03 10:30:44 -07001423 struct delayed_work *dwork, unsigned long delay)
1424{
Tejun Heo57469822012-08-03 10:30:45 -07001425 return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
Tejun Heo0a13c002012-08-03 10:30:44 -07001426}
1427EXPORT_SYMBOL_GPL(queue_delayed_work);
1428
1429/**
Tejun Heo8376fe22012-08-03 10:30:47 -07001430 * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
1431 * @cpu: CPU number to execute work on
1432 * @wq: workqueue to use
1433 * @dwork: work to queue
1434 * @delay: number of jiffies to wait before queueing
1435 *
1436 * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
1437 * modify @dwork's timer so that it expires after @delay. If @delay is
1438 * zero, @work is guaranteed to be scheduled immediately regardless of its
1439 * current state.
1440 *
1441 * Returns %false if @dwork was idle and queued, %true if @dwork was
1442 * pending and its timer was modified.
1443 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001444 * This function is safe to call from any context including IRQ handler.
Tejun Heo8376fe22012-08-03 10:30:47 -07001445 * See try_to_grab_pending() for details.
1446 */
1447bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
1448 struct delayed_work *dwork, unsigned long delay)
1449{
1450 unsigned long flags;
1451 int ret;
1452
1453 do {
1454 ret = try_to_grab_pending(&dwork->work, true, &flags);
1455 } while (unlikely(ret == -EAGAIN));
1456
1457 if (likely(ret >= 0)) {
1458 __queue_delayed_work(cpu, wq, dwork, delay);
1459 local_irq_restore(flags);
1460 }
1461
1462 /* -ENOENT from try_to_grab_pending() becomes %true */
1463 return ret;
1464}
1465EXPORT_SYMBOL_GPL(mod_delayed_work_on);
1466
1467/**
1468 * mod_delayed_work - modify delay of or queue a delayed work
1469 * @wq: workqueue to use
1470 * @dwork: work to queue
1471 * @delay: number of jiffies to wait before queueing
1472 *
1473 * mod_delayed_work_on() on local CPU.
1474 */
1475bool mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dwork,
1476 unsigned long delay)
1477{
1478 return mod_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
1479}
1480EXPORT_SYMBOL_GPL(mod_delayed_work);
1481
1482/**
Tejun Heoc8e55f32010-06-29 10:07:12 +02001483 * worker_enter_idle - enter idle state
1484 * @worker: worker which is entering idle state
1485 *
1486 * @worker is entering idle state. Update stats and idle timer if
1487 * necessary.
1488 *
1489 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001490 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001491 */
1492static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001494 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Tejun Heo6183c002013-03-12 11:29:57 -07001496 if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) ||
1497 WARN_ON_ONCE(!list_empty(&worker->entry) &&
1498 (worker->hentry.next || worker->hentry.pprev)))
1499 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Tejun Heocb444762010-07-02 10:03:50 +02001501 /* can't use worker_set_flags(), also called from start_worker() */
1502 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001503 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001504 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001505
Tejun Heoc8e55f32010-06-29 10:07:12 +02001506 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001507 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001508
Tejun Heo628c78e2012-07-17 12:39:27 -07001509 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1510 mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
Tejun Heocb444762010-07-02 10:03:50 +02001511
Tejun Heo544ecf32012-05-14 15:04:50 -07001512 /*
Tejun Heo706026c2013-01-24 11:01:34 -08001513 * Sanity check nr_running. Because wq_unbind_fn() releases
Tejun Heod565ed62013-01-24 11:01:33 -08001514 * pool->lock between setting %WORKER_UNBOUND and zapping
Tejun Heo628c78e2012-07-17 12:39:27 -07001515 * nr_running, the warning may trigger spuriously. Check iff
1516 * unbind is not in progress.
Tejun Heo544ecf32012-05-14 15:04:50 -07001517 */
Tejun Heo24647572013-01-24 11:01:33 -08001518 WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001519 pool->nr_workers == pool->nr_idle &&
Tejun Heoe19e3972013-01-24 11:39:44 -08001520 atomic_read(&pool->nr_running));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521}
1522
Tejun Heoc8e55f32010-06-29 10:07:12 +02001523/**
1524 * worker_leave_idle - leave idle state
1525 * @worker: worker which is leaving idle state
1526 *
1527 * @worker is leaving idle state. Update stats.
1528 *
1529 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001530 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001531 */
1532static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001534 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Tejun Heo6183c002013-03-12 11:29:57 -07001536 if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE)))
1537 return;
Tejun Heod302f012010-06-29 10:07:13 +02001538 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001539 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001540 list_del_init(&worker->entry);
1541}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Tejun Heoe22bee72010-06-29 10:07:14 +02001543/**
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001544 * worker_maybe_bind_and_lock - try to bind %current to worker_pool and lock it
1545 * @pool: target worker_pool
1546 *
1547 * Bind %current to the cpu of @pool if it is associated and lock @pool.
Tejun Heoe22bee72010-06-29 10:07:14 +02001548 *
1549 * Works which are scheduled while the cpu is online must at least be
1550 * scheduled to a worker which is bound to the cpu so that if they are
1551 * flushed from cpu callbacks while cpu is going down, they are
1552 * guaranteed to execute on the cpu.
1553 *
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001554 * This function is to be used by unbound workers and rescuers to bind
Tejun Heoe22bee72010-06-29 10:07:14 +02001555 * themselves to the target cpu and may race with cpu going down or
1556 * coming online. kthread_bind() can't be used because it may put the
1557 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
Tejun Heo706026c2013-01-24 11:01:34 -08001558 * verbatim as it's best effort and blocking and pool may be
Tejun Heoe22bee72010-06-29 10:07:14 +02001559 * [dis]associated in the meantime.
1560 *
Tejun Heo706026c2013-01-24 11:01:34 -08001561 * This function tries set_cpus_allowed() and locks pool and verifies the
Tejun Heo24647572013-01-24 11:01:33 -08001562 * binding against %POOL_DISASSOCIATED which is set during
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001563 * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1564 * enters idle state or fetches works without dropping lock, it can
1565 * guarantee the scheduling requirement described in the first paragraph.
Tejun Heoe22bee72010-06-29 10:07:14 +02001566 *
1567 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001568 * Might sleep. Called without any lock but returns with pool->lock
Tejun Heoe22bee72010-06-29 10:07:14 +02001569 * held.
1570 *
1571 * RETURNS:
Tejun Heo706026c2013-01-24 11:01:34 -08001572 * %true if the associated pool is online (@worker is successfully
Tejun Heoe22bee72010-06-29 10:07:14 +02001573 * bound), %false if offline.
1574 */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001575static bool worker_maybe_bind_and_lock(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001576__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001577{
Tejun Heoe22bee72010-06-29 10:07:14 +02001578 while (true) {
1579 /*
1580 * The following call may fail, succeed or succeed
1581 * without actually migrating the task to the cpu if
1582 * it races with cpu hotunplug operation. Verify
Tejun Heo24647572013-01-24 11:01:33 -08001583 * against POOL_DISASSOCIATED.
Tejun Heoe22bee72010-06-29 10:07:14 +02001584 */
Tejun Heo24647572013-01-24 11:01:33 -08001585 if (!(pool->flags & POOL_DISASSOCIATED))
Tejun Heo7a4e3442013-03-12 11:30:00 -07001586 set_cpus_allowed_ptr(current, pool->attrs->cpumask);
Oleg Nesterov85f41862007-05-09 02:34:20 -07001587
Tejun Heod565ed62013-01-24 11:01:33 -08001588 spin_lock_irq(&pool->lock);
Tejun Heo24647572013-01-24 11:01:33 -08001589 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heoe22bee72010-06-29 10:07:14 +02001590 return false;
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001591 if (task_cpu(current) == pool->cpu &&
Tejun Heo7a4e3442013-03-12 11:30:00 -07001592 cpumask_equal(&current->cpus_allowed, pool->attrs->cpumask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001593 return true;
Tejun Heod565ed62013-01-24 11:01:33 -08001594 spin_unlock_irq(&pool->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001595
Tejun Heo5035b202011-04-29 18:08:37 +02001596 /*
1597 * We've raced with CPU hot[un]plug. Give it a breather
1598 * and retry migration. cond_resched() is required here;
1599 * otherwise, we might deadlock against cpu_stop trying to
1600 * bring down the CPU on non-preemptive kernel.
1601 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001602 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001603 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001604 }
1605}
1606
1607/*
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001608 * Rebind an idle @worker to its CPU. worker_thread() will test
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001609 * list_empty(@worker->entry) before leaving idle and call this function.
Tejun Heo25511a42012-07-17 12:39:27 -07001610 */
1611static void idle_worker_rebind(struct worker *worker)
1612{
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001613 /* CPU may go down again inbetween, clear UNBOUND only on success */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001614 if (worker_maybe_bind_and_lock(worker->pool))
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001615 worker_clr_flags(worker, WORKER_UNBOUND);
Tejun Heo25511a42012-07-17 12:39:27 -07001616
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001617 /* rebind complete, become available again */
1618 list_add(&worker->entry, &worker->pool->idle_list);
Tejun Heod565ed62013-01-24 11:01:33 -08001619 spin_unlock_irq(&worker->pool->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07001620}
1621
1622/*
1623 * Function for @worker->rebind.work used to rebind unbound busy workers to
Tejun Heo403c8212012-07-17 12:39:27 -07001624 * the associated cpu which is coming back online. This is scheduled by
1625 * cpu up but can race with other cpu hotplug operations and may be
1626 * executed twice without intervening cpu down.
Tejun Heoe22bee72010-06-29 10:07:14 +02001627 */
Tejun Heo25511a42012-07-17 12:39:27 -07001628static void busy_worker_rebind_fn(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001629{
1630 struct worker *worker = container_of(work, struct worker, rebind_work);
Tejun Heoe22bee72010-06-29 10:07:14 +02001631
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001632 if (worker_maybe_bind_and_lock(worker->pool))
Lai Jiangshaneab6d822012-09-18 09:59:22 -07001633 worker_clr_flags(worker, WORKER_UNBOUND);
Tejun Heoe22bee72010-06-29 10:07:14 +02001634
Tejun Heod565ed62013-01-24 11:01:33 -08001635 spin_unlock_irq(&worker->pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001636}
1637
Tejun Heo25511a42012-07-17 12:39:27 -07001638/**
Tejun Heo94cf58b2013-01-24 11:01:33 -08001639 * rebind_workers - rebind all workers of a pool to the associated CPU
1640 * @pool: pool of interest
Tejun Heo25511a42012-07-17 12:39:27 -07001641 *
Tejun Heo94cf58b2013-01-24 11:01:33 -08001642 * @pool->cpu is coming online. Rebind all workers to the CPU. Rebinding
Tejun Heo25511a42012-07-17 12:39:27 -07001643 * is different for idle and busy ones.
1644 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001645 * Idle ones will be removed from the idle_list and woken up. They will
1646 * add themselves back after completing rebind. This ensures that the
1647 * idle_list doesn't contain any unbound workers when re-bound busy workers
1648 * try to perform local wake-ups for concurrency management.
Tejun Heo25511a42012-07-17 12:39:27 -07001649 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001650 * Busy workers can rebind after they finish their current work items.
1651 * Queueing the rebind work item at the head of the scheduled list is
1652 * enough. Note that nr_running will be properly bumped as busy workers
1653 * rebind.
Tejun Heo25511a42012-07-17 12:39:27 -07001654 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001655 * On return, all non-manager workers are scheduled for rebind - see
1656 * manage_workers() for the manager special case. Any idle worker
1657 * including the manager will not appear on @idle_list until rebind is
1658 * complete, making local wake-ups safe.
Tejun Heo25511a42012-07-17 12:39:27 -07001659 */
Tejun Heo94cf58b2013-01-24 11:01:33 -08001660static void rebind_workers(struct worker_pool *pool)
Tejun Heo25511a42012-07-17 12:39:27 -07001661{
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001662 struct worker *worker, *n;
Tejun Heo25511a42012-07-17 12:39:27 -07001663 int i;
1664
Tejun Heo94cf58b2013-01-24 11:01:33 -08001665 lockdep_assert_held(&pool->assoc_mutex);
1666 lockdep_assert_held(&pool->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07001667
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001668 /* dequeue and kick idle ones */
Tejun Heo94cf58b2013-01-24 11:01:33 -08001669 list_for_each_entry_safe(worker, n, &pool->idle_list, entry) {
1670 /*
1671 * idle workers should be off @pool->idle_list until rebind
1672 * is complete to avoid receiving premature local wake-ups.
1673 */
1674 list_del_init(&worker->entry);
Lai Jiangshan96e65302012-09-02 00:28:19 +08001675
Tejun Heo94cf58b2013-01-24 11:01:33 -08001676 /*
1677 * worker_thread() will see the above dequeuing and call
1678 * idle_worker_rebind().
1679 */
1680 wake_up_process(worker->task);
1681 }
Tejun Heo25511a42012-07-17 12:39:27 -07001682
Tejun Heo94cf58b2013-01-24 11:01:33 -08001683 /* rebind busy workers */
Sasha Levinb67bfe02013-02-27 17:06:00 -08001684 for_each_busy_worker(worker, i, pool) {
Tejun Heo94cf58b2013-01-24 11:01:33 -08001685 struct work_struct *rebind_work = &worker->rebind_work;
1686 struct workqueue_struct *wq;
Tejun Heo25511a42012-07-17 12:39:27 -07001687
Tejun Heo94cf58b2013-01-24 11:01:33 -08001688 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
1689 work_data_bits(rebind_work)))
1690 continue;
Tejun Heo25511a42012-07-17 12:39:27 -07001691
Tejun Heo94cf58b2013-01-24 11:01:33 -08001692 debug_work_activate(rebind_work);
Tejun Heo90beca52012-09-04 23:12:33 -07001693
Tejun Heo94cf58b2013-01-24 11:01:33 -08001694 /*
1695 * wq doesn't really matter but let's keep @worker->pool
Tejun Heo112202d2013-02-13 19:29:12 -08001696 * and @pwq->pool consistent for sanity.
Tejun Heo94cf58b2013-01-24 11:01:33 -08001697 */
Tejun Heo7a4e3442013-03-12 11:30:00 -07001698 if (worker->pool->attrs->nice < 0)
Tejun Heo94cf58b2013-01-24 11:01:33 -08001699 wq = system_highpri_wq;
1700 else
1701 wq = system_wq;
Tejun Heoec588152012-09-04 23:16:32 -07001702
Tejun Heo7fb98ea2013-03-12 11:30:00 -07001703 insert_work(per_cpu_ptr(wq->cpu_pwqs, pool->cpu), rebind_work,
Tejun Heo94cf58b2013-01-24 11:01:33 -08001704 worker->scheduled.next,
1705 work_color_to_flags(WORK_NO_COLOR));
Tejun Heoec588152012-09-04 23:16:32 -07001706 }
Tejun Heo25511a42012-07-17 12:39:27 -07001707}
1708
Tejun Heoc34056a2010-06-29 10:07:11 +02001709static struct worker *alloc_worker(void)
1710{
1711 struct worker *worker;
1712
1713 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001714 if (worker) {
1715 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001716 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heo25511a42012-07-17 12:39:27 -07001717 INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn);
Tejun Heoe22bee72010-06-29 10:07:14 +02001718 /* on creation a worker is in !idle && prep state */
1719 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001720 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001721 return worker;
1722}
1723
1724/**
1725 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001726 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001727 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001728 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001729 * can be started by calling start_worker() or destroyed using
1730 * destroy_worker().
1731 *
1732 * CONTEXT:
1733 * Might sleep. Does GFP_KERNEL allocations.
1734 *
1735 * RETURNS:
1736 * Pointer to the newly created worker.
1737 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001738static struct worker *create_worker(struct worker_pool *pool)
Tejun Heoc34056a2010-06-29 10:07:11 +02001739{
Tejun Heo7a4e3442013-03-12 11:30:00 -07001740 const char *pri = pool->attrs->nice < 0 ? "H" : "";
Tejun Heoc34056a2010-06-29 10:07:11 +02001741 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001742 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001743
Tejun Heod565ed62013-01-24 11:01:33 -08001744 spin_lock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001745 while (ida_get_new(&pool->worker_ida, &id)) {
Tejun Heod565ed62013-01-24 11:01:33 -08001746 spin_unlock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001747 if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001748 goto fail;
Tejun Heod565ed62013-01-24 11:01:33 -08001749 spin_lock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001750 }
Tejun Heod565ed62013-01-24 11:01:33 -08001751 spin_unlock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001752
1753 worker = alloc_worker();
1754 if (!worker)
1755 goto fail;
1756
Tejun Heobd7bdd42012-07-12 14:46:37 -07001757 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001758 worker->id = id;
1759
Tejun Heo29c91e92013-03-12 11:30:03 -07001760 if (pool->cpu >= 0)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001761 worker->task = kthread_create_on_node(worker_thread,
Tejun Heoec22ca52013-01-24 11:01:33 -08001762 worker, cpu_to_node(pool->cpu),
Tejun Heod84ff052013-03-12 11:29:59 -07001763 "kworker/%d:%d%s", pool->cpu, id, pri);
Tejun Heof3421792010-07-02 10:03:51 +02001764 else
1765 worker->task = kthread_create(worker_thread, worker,
Tejun Heo32704762012-07-13 22:16:45 -07001766 "kworker/u:%d%s", id, pri);
Tejun Heoc34056a2010-06-29 10:07:11 +02001767 if (IS_ERR(worker->task))
1768 goto fail;
1769
Tejun Heo7a4e3442013-03-12 11:30:00 -07001770 set_user_nice(worker->task, pool->attrs->nice);
1771 set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask);
Tejun Heo32704762012-07-13 22:16:45 -07001772
Tejun Heodb7bccf2010-06-29 10:07:12 +02001773 /*
Tejun Heo7a4e3442013-03-12 11:30:00 -07001774 * %PF_THREAD_BOUND is used to prevent userland from meddling with
1775 * cpumask of workqueue workers. This is an abuse. We need
1776 * %PF_NO_SETAFFINITY.
Tejun Heodb7bccf2010-06-29 10:07:12 +02001777 */
Tejun Heo7a4e3442013-03-12 11:30:00 -07001778 worker->task->flags |= PF_THREAD_BOUND;
1779
1780 /*
1781 * The caller is responsible for ensuring %POOL_DISASSOCIATED
1782 * remains stable across this function. See the comments above the
1783 * flag definition for details.
1784 */
1785 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001786 worker->flags |= WORKER_UNBOUND;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001787
Tejun Heoc34056a2010-06-29 10:07:11 +02001788 return worker;
1789fail:
1790 if (id >= 0) {
Tejun Heod565ed62013-01-24 11:01:33 -08001791 spin_lock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001792 ida_remove(&pool->worker_ida, id);
Tejun Heod565ed62013-01-24 11:01:33 -08001793 spin_unlock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001794 }
1795 kfree(worker);
1796 return NULL;
1797}
1798
1799/**
1800 * start_worker - start a newly created worker
1801 * @worker: worker to start
1802 *
Tejun Heo706026c2013-01-24 11:01:34 -08001803 * Make the pool aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001804 *
1805 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001806 * spin_lock_irq(pool->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001807 */
1808static void start_worker(struct worker *worker)
1809{
Tejun Heocb444762010-07-02 10:03:50 +02001810 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001811 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001812 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001813 wake_up_process(worker->task);
1814}
1815
1816/**
1817 * destroy_worker - destroy a workqueue worker
1818 * @worker: worker to be destroyed
1819 *
Tejun Heo706026c2013-01-24 11:01:34 -08001820 * Destroy @worker and adjust @pool stats accordingly.
Tejun Heoc8e55f32010-06-29 10:07:12 +02001821 *
1822 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001823 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001824 */
1825static void destroy_worker(struct worker *worker)
1826{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001827 struct worker_pool *pool = worker->pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001828 int id = worker->id;
1829
1830 /* sanity check frenzy */
Tejun Heo6183c002013-03-12 11:29:57 -07001831 if (WARN_ON(worker->current_work) ||
1832 WARN_ON(!list_empty(&worker->scheduled)))
1833 return;
Tejun Heoc34056a2010-06-29 10:07:11 +02001834
Tejun Heoc8e55f32010-06-29 10:07:12 +02001835 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001836 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001837 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001838 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001839
1840 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001841 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001842
Tejun Heod565ed62013-01-24 11:01:33 -08001843 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001844
Tejun Heoc34056a2010-06-29 10:07:11 +02001845 kthread_stop(worker->task);
1846 kfree(worker);
1847
Tejun Heod565ed62013-01-24 11:01:33 -08001848 spin_lock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001849 ida_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001850}
1851
Tejun Heo63d95a92012-07-12 14:46:37 -07001852static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001853{
Tejun Heo63d95a92012-07-12 14:46:37 -07001854 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001855
Tejun Heod565ed62013-01-24 11:01:33 -08001856 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001857
Tejun Heo63d95a92012-07-12 14:46:37 -07001858 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001859 struct worker *worker;
1860 unsigned long expires;
1861
1862 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001863 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001864 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1865
1866 if (time_before(jiffies, expires))
Tejun Heo63d95a92012-07-12 14:46:37 -07001867 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001868 else {
1869 /* it's been idle for too long, wake up manager */
Tejun Heo11ebea52012-07-12 14:46:37 -07001870 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -07001871 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001872 }
1873 }
1874
Tejun Heod565ed62013-01-24 11:01:33 -08001875 spin_unlock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001876}
1877
Tejun Heo493a1722013-03-12 11:29:59 -07001878static void send_mayday(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001879{
Tejun Heo112202d2013-02-13 19:29:12 -08001880 struct pool_workqueue *pwq = get_work_pwq(work);
1881 struct workqueue_struct *wq = pwq->wq;
Tejun Heo493a1722013-03-12 11:29:59 -07001882
1883 lockdep_assert_held(&workqueue_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001884
1885 if (!(wq->flags & WQ_RESCUER))
Tejun Heo493a1722013-03-12 11:29:59 -07001886 return;
Tejun Heoe22bee72010-06-29 10:07:14 +02001887
1888 /* mayday mayday mayday */
Tejun Heo493a1722013-03-12 11:29:59 -07001889 if (list_empty(&pwq->mayday_node)) {
1890 list_add_tail(&pwq->mayday_node, &wq->maydays);
Tejun Heoe22bee72010-06-29 10:07:14 +02001891 wake_up_process(wq->rescuer->task);
Tejun Heo493a1722013-03-12 11:29:59 -07001892 }
Tejun Heoe22bee72010-06-29 10:07:14 +02001893}
1894
Tejun Heo706026c2013-01-24 11:01:34 -08001895static void pool_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001896{
Tejun Heo63d95a92012-07-12 14:46:37 -07001897 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001898 struct work_struct *work;
1899
Tejun Heo493a1722013-03-12 11:29:59 -07001900 spin_lock_irq(&workqueue_lock); /* for wq->maydays */
1901 spin_lock(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001902
Tejun Heo63d95a92012-07-12 14:46:37 -07001903 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001904 /*
1905 * We've been trying to create a new worker but
1906 * haven't been successful. We might be hitting an
1907 * allocation deadlock. Send distress signals to
1908 * rescuers.
1909 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001910 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001911 send_mayday(work);
1912 }
1913
Tejun Heo493a1722013-03-12 11:29:59 -07001914 spin_unlock(&pool->lock);
1915 spin_unlock_irq(&workqueue_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001916
Tejun Heo63d95a92012-07-12 14:46:37 -07001917 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001918}
1919
1920/**
1921 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001922 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001923 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001924 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001925 * have at least one idle worker on return from this function. If
1926 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001927 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001928 * possible allocation deadlock.
1929 *
1930 * On return, need_to_create_worker() is guaranteed to be false and
1931 * may_start_working() true.
1932 *
1933 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001934 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001935 * multiple times. Does GFP_KERNEL allocations. Called only from
1936 * manager.
1937 *
1938 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08001939 * false if no action was taken and pool->lock stayed locked, true
Tejun Heoe22bee72010-06-29 10:07:14 +02001940 * otherwise.
1941 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001942static bool maybe_create_worker(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001943__releases(&pool->lock)
1944__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001945{
Tejun Heo63d95a92012-07-12 14:46:37 -07001946 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001947 return false;
1948restart:
Tejun Heod565ed62013-01-24 11:01:33 -08001949 spin_unlock_irq(&pool->lock);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001950
Tejun Heoe22bee72010-06-29 10:07:14 +02001951 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07001952 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001953
1954 while (true) {
1955 struct worker *worker;
1956
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001957 worker = create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001958 if (worker) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001959 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001960 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001961 start_worker(worker);
Tejun Heo6183c002013-03-12 11:29:57 -07001962 if (WARN_ON_ONCE(need_to_create_worker(pool)))
1963 goto restart;
Tejun Heoe22bee72010-06-29 10:07:14 +02001964 return true;
1965 }
1966
Tejun Heo63d95a92012-07-12 14:46:37 -07001967 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001968 break;
1969
Tejun Heoe22bee72010-06-29 10:07:14 +02001970 __set_current_state(TASK_INTERRUPTIBLE);
1971 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001972
Tejun Heo63d95a92012-07-12 14:46:37 -07001973 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001974 break;
1975 }
1976
Tejun Heo63d95a92012-07-12 14:46:37 -07001977 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001978 spin_lock_irq(&pool->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07001979 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001980 goto restart;
1981 return true;
1982}
1983
1984/**
1985 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo63d95a92012-07-12 14:46:37 -07001986 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001987 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001988 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02001989 * IDLE_WORKER_TIMEOUT.
1990 *
1991 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001992 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001993 * multiple times. Called only from manager.
1994 *
1995 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08001996 * false if no action was taken and pool->lock stayed locked, true
Tejun Heoe22bee72010-06-29 10:07:14 +02001997 * otherwise.
1998 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001999static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02002000{
2001 bool ret = false;
2002
Tejun Heo63d95a92012-07-12 14:46:37 -07002003 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02002004 struct worker *worker;
2005 unsigned long expires;
2006
Tejun Heo63d95a92012-07-12 14:46:37 -07002007 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02002008 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
2009
2010 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07002011 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02002012 break;
2013 }
2014
2015 destroy_worker(worker);
2016 ret = true;
2017 }
2018
2019 return ret;
2020}
2021
2022/**
2023 * manage_workers - manage worker pool
2024 * @worker: self
2025 *
Tejun Heo706026c2013-01-24 11:01:34 -08002026 * Assume the manager role and manage the worker pool @worker belongs
Tejun Heoe22bee72010-06-29 10:07:14 +02002027 * to. At any given time, there can be only zero or one manager per
Tejun Heo706026c2013-01-24 11:01:34 -08002028 * pool. The exclusion is handled automatically by this function.
Tejun Heoe22bee72010-06-29 10:07:14 +02002029 *
2030 * The caller can safely start processing works on false return. On
2031 * true return, it's guaranteed that need_to_create_worker() is false
2032 * and may_start_working() is true.
2033 *
2034 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002035 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02002036 * multiple times. Does GFP_KERNEL allocations.
2037 *
2038 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08002039 * spin_lock_irq(pool->lock) which may be released and regrabbed
2040 * multiple times. Does GFP_KERNEL allocations.
Tejun Heoe22bee72010-06-29 10:07:14 +02002041 */
2042static bool manage_workers(struct worker *worker)
2043{
Tejun Heo63d95a92012-07-12 14:46:37 -07002044 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002045 bool ret = false;
2046
Tejun Heo34a06bd2013-03-12 11:30:00 -07002047 if (!mutex_trylock(&pool->manager_arb))
Tejun Heoe22bee72010-06-29 10:07:14 +02002048 return ret;
2049
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002050 /*
2051 * To simplify both worker management and CPU hotplug, hold off
2052 * management while hotplug is in progress. CPU hotplug path can't
Tejun Heo34a06bd2013-03-12 11:30:00 -07002053 * grab @pool->manager_arb to achieve this because that can lead to
2054 * idle worker depletion (all become busy thinking someone else is
2055 * managing) which in turn can result in deadlock under extreme
2056 * circumstances. Use @pool->assoc_mutex to synchronize manager
2057 * against CPU hotplug.
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002058 *
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002059 * assoc_mutex would always be free unless CPU hotplug is in
Tejun Heod565ed62013-01-24 11:01:33 -08002060 * progress. trylock first without dropping @pool->lock.
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002061 */
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002062 if (unlikely(!mutex_trylock(&pool->assoc_mutex))) {
Tejun Heod565ed62013-01-24 11:01:33 -08002063 spin_unlock_irq(&pool->lock);
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002064 mutex_lock(&pool->assoc_mutex);
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002065 /*
2066 * CPU hotplug could have happened while we were waiting
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002067 * for assoc_mutex. Hotplug itself can't handle us
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002068 * because manager isn't either on idle or busy list, and
Tejun Heo706026c2013-01-24 11:01:34 -08002069 * @pool's state and ours could have deviated.
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002070 *
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002071 * As hotplug is now excluded via assoc_mutex, we can
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002072 * simply try to bind. It will succeed or fail depending
Tejun Heo706026c2013-01-24 11:01:34 -08002073 * on @pool's current state. Try it and adjust
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002074 * %WORKER_UNBOUND accordingly.
2075 */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08002076 if (worker_maybe_bind_and_lock(pool))
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002077 worker->flags &= ~WORKER_UNBOUND;
2078 else
2079 worker->flags |= WORKER_UNBOUND;
2080
2081 ret = true;
2082 }
2083
Tejun Heo11ebea52012-07-12 14:46:37 -07002084 pool->flags &= ~POOL_MANAGE_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02002085
2086 /*
2087 * Destroy and then create so that may_start_working() is true
2088 * on return.
2089 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002090 ret |= maybe_destroy_workers(pool);
2091 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002092
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002093 mutex_unlock(&pool->assoc_mutex);
Tejun Heo34a06bd2013-03-12 11:30:00 -07002094 mutex_unlock(&pool->manager_arb);
Tejun Heoe22bee72010-06-29 10:07:14 +02002095 return ret;
2096}
2097
Tejun Heoa62428c2010-06-29 10:07:10 +02002098/**
2099 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02002100 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02002101 * @work: work to process
2102 *
2103 * Process @work. This function contains all the logics necessary to
2104 * process a single work including synchronization against and
2105 * interaction with other workers on the same cpu, queueing and
2106 * flushing. As long as context requirement is met, any worker can
2107 * call this function to process a work.
2108 *
2109 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002110 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02002111 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002112static void process_one_work(struct worker *worker, struct work_struct *work)
Tejun Heod565ed62013-01-24 11:01:33 -08002113__releases(&pool->lock)
2114__acquires(&pool->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02002115{
Tejun Heo112202d2013-02-13 19:29:12 -08002116 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002117 struct worker_pool *pool = worker->pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002118 bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02002119 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02002120 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02002121#ifdef CONFIG_LOCKDEP
2122 /*
2123 * It is permissible to free the struct work_struct from
2124 * inside the function that is called from it, this we need to
2125 * take into account for lockdep too. To avoid bogus "held
2126 * lock freed" warnings as well as problems when looking into
2127 * work->lockdep_map, make a copy and use that here.
2128 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07002129 struct lockdep_map lockdep_map;
2130
2131 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002132#endif
Tejun Heo6fec10a2012-07-22 10:16:34 -07002133 /*
2134 * Ensure we're on the correct CPU. DISASSOCIATED test is
2135 * necessary to avoid spurious warnings from rescuers servicing the
Tejun Heo24647572013-01-24 11:01:33 -08002136 * unbound or a disassociated pool.
Tejun Heo6fec10a2012-07-22 10:16:34 -07002137 */
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002138 WARN_ON_ONCE(!(worker->flags & WORKER_UNBOUND) &&
Tejun Heo24647572013-01-24 11:01:33 -08002139 !(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heoec22ca52013-01-24 11:01:33 -08002140 raw_smp_processor_id() != pool->cpu);
Tejun Heo25511a42012-07-17 12:39:27 -07002141
Tejun Heo7e116292010-06-29 10:07:13 +02002142 /*
2143 * A single work shouldn't be executed concurrently by
2144 * multiple workers on a single cpu. Check whether anyone is
2145 * already processing the work. If so, defer the work to the
2146 * currently executing one.
2147 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002148 collision = find_worker_executing_work(pool, work);
Tejun Heo7e116292010-06-29 10:07:13 +02002149 if (unlikely(collision)) {
2150 move_linked_works(work, &collision->scheduled, NULL);
2151 return;
2152 }
2153
Tejun Heo8930cab2012-08-03 10:30:45 -07002154 /* claim and dequeue */
Tejun Heoa62428c2010-06-29 10:07:10 +02002155 debug_work_deactivate(work);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002156 hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work);
Tejun Heoc34056a2010-06-29 10:07:11 +02002157 worker->current_work = work;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002158 worker->current_func = work->func;
Tejun Heo112202d2013-02-13 19:29:12 -08002159 worker->current_pwq = pwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002160 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002161
Tejun Heoa62428c2010-06-29 10:07:10 +02002162 list_del_init(&work->entry);
2163
Tejun Heo649027d2010-06-29 10:07:14 +02002164 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02002165 * CPU intensive works don't participate in concurrency
2166 * management. They're the scheduler's responsibility.
2167 */
2168 if (unlikely(cpu_intensive))
2169 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
2170
Tejun Heo974271c2012-07-12 14:46:37 -07002171 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002172 * Unbound pool isn't concurrency managed and work items should be
Tejun Heo974271c2012-07-12 14:46:37 -07002173 * executed ASAP. Wake up another worker if necessary.
2174 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002175 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
2176 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07002177
Tejun Heo8930cab2012-08-03 10:30:45 -07002178 /*
Tejun Heo7c3eed52013-01-24 11:01:33 -08002179 * Record the last pool and clear PENDING which should be the last
Tejun Heod565ed62013-01-24 11:01:33 -08002180 * update to @work. Also, do this inside @pool->lock so that
Tejun Heo23657bb2012-08-13 17:08:19 -07002181 * PENDING and queued state changes happen together while IRQ is
2182 * disabled.
Tejun Heo8930cab2012-08-03 10:30:45 -07002183 */
Tejun Heo7c3eed52013-01-24 11:01:33 -08002184 set_work_pool_and_clear_pending(work, pool->id);
Tejun Heoa62428c2010-06-29 10:07:10 +02002185
Tejun Heod565ed62013-01-24 11:01:33 -08002186 spin_unlock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002187
Tejun Heo112202d2013-02-13 19:29:12 -08002188 lock_map_acquire_read(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002189 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002190 trace_workqueue_execute_start(work);
Tejun Heoa2c1c572012-12-18 10:35:02 -08002191 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002192 /*
2193 * While we must be careful to not use "work" after this, the trace
2194 * point will only record its address.
2195 */
2196 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002197 lock_map_release(&lockdep_map);
Tejun Heo112202d2013-02-13 19:29:12 -08002198 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002199
2200 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Valentin Ilie044c7822012-08-19 00:52:42 +03002201 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
2202 " last function: %pf\n",
Tejun Heoa2c1c572012-12-18 10:35:02 -08002203 current->comm, preempt_count(), task_pid_nr(current),
2204 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02002205 debug_show_held_locks(current);
2206 dump_stack();
2207 }
2208
Tejun Heod565ed62013-01-24 11:01:33 -08002209 spin_lock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002210
Tejun Heofb0e7be2010-06-29 10:07:15 +02002211 /* clear cpu intensive status */
2212 if (unlikely(cpu_intensive))
2213 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2214
Tejun Heoa62428c2010-06-29 10:07:10 +02002215 /* we're done with it, release */
Sasha Levin42f85702012-12-17 10:01:23 -05002216 hash_del(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002217 worker->current_work = NULL;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002218 worker->current_func = NULL;
Tejun Heo112202d2013-02-13 19:29:12 -08002219 worker->current_pwq = NULL;
2220 pwq_dec_nr_in_flight(pwq, work_color);
Tejun Heoa62428c2010-06-29 10:07:10 +02002221}
2222
Tejun Heoaffee4b2010-06-29 10:07:12 +02002223/**
2224 * process_scheduled_works - process scheduled works
2225 * @worker: self
2226 *
2227 * Process all scheduled works. Please note that the scheduled list
2228 * may change while processing a work, so this function repeatedly
2229 * fetches a work from the top and executes it.
2230 *
2231 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002232 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02002233 * multiple times.
2234 */
2235static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002237 while (!list_empty(&worker->scheduled)) {
2238 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002240 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242}
2243
Tejun Heo4690c4a2010-06-29 10:07:10 +02002244/**
2245 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02002246 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02002247 *
Tejun Heo706026c2013-01-24 11:01:34 -08002248 * The worker thread function. There are NR_CPU_WORKER_POOLS dynamic pools
2249 * of these per each cpu. These workers process all works regardless of
Tejun Heoe22bee72010-06-29 10:07:14 +02002250 * their specific target workqueue. The only exception is works which
2251 * belong to workqueues with a rescuer which will be explained in
2252 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02002253 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002254static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255{
Tejun Heoc34056a2010-06-29 10:07:11 +02002256 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07002257 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Tejun Heoe22bee72010-06-29 10:07:14 +02002259 /* tell the scheduler that this is a workqueue worker */
2260 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002261woke_up:
Tejun Heod565ed62013-01-24 11:01:33 -08002262 spin_lock_irq(&pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002264 /* we are off idle list if destruction or rebind is requested */
2265 if (unlikely(list_empty(&worker->entry))) {
Tejun Heod565ed62013-01-24 11:01:33 -08002266 spin_unlock_irq(&pool->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07002267
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002268 /* if DIE is set, destruction is requested */
Tejun Heo25511a42012-07-17 12:39:27 -07002269 if (worker->flags & WORKER_DIE) {
2270 worker->task->flags &= ~PF_WQ_WORKER;
2271 return 0;
2272 }
2273
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002274 /* otherwise, rebind */
Tejun Heo25511a42012-07-17 12:39:27 -07002275 idle_worker_rebind(worker);
2276 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 }
2278
Tejun Heoc8e55f32010-06-29 10:07:12 +02002279 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002280recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002281 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002282 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002283 goto sleep;
2284
2285 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002286 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002287 goto recheck;
2288
Tejun Heoc8e55f32010-06-29 10:07:12 +02002289 /*
2290 * ->scheduled list can only be filled while a worker is
2291 * preparing to process a work or actually processing it.
2292 * Make sure nobody diddled with it while I was sleeping.
2293 */
Tejun Heo6183c002013-03-12 11:29:57 -07002294 WARN_ON_ONCE(!list_empty(&worker->scheduled));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002295
Tejun Heoe22bee72010-06-29 10:07:14 +02002296 /*
2297 * When control reaches this point, we're guaranteed to have
2298 * at least one idle worker or that someone else has already
2299 * assumed the manager role.
2300 */
2301 worker_clr_flags(worker, WORKER_PREP);
2302
2303 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002304 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002305 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002306 struct work_struct, entry);
2307
2308 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2309 /* optimization path, not strictly necessary */
2310 process_one_work(worker, work);
2311 if (unlikely(!list_empty(&worker->scheduled)))
2312 process_scheduled_works(worker);
2313 } else {
2314 move_linked_works(work, &worker->scheduled, NULL);
2315 process_scheduled_works(worker);
2316 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002317 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002318
Tejun Heoe22bee72010-06-29 10:07:14 +02002319 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002320sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07002321 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002322 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002323
Tejun Heoc8e55f32010-06-29 10:07:12 +02002324 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002325 * pool->lock is held and there's no work to process and no need to
2326 * manage, sleep. Workers are woken up only while holding
2327 * pool->lock or from local cpu, so setting the current state
2328 * before releasing pool->lock is enough to prevent losing any
2329 * event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002330 */
2331 worker_enter_idle(worker);
2332 __set_current_state(TASK_INTERRUPTIBLE);
Tejun Heod565ed62013-01-24 11:01:33 -08002333 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02002334 schedule();
2335 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336}
2337
Tejun Heoe22bee72010-06-29 10:07:14 +02002338/**
2339 * rescuer_thread - the rescuer thread function
Tejun Heo111c2252013-01-17 17:16:24 -08002340 * @__rescuer: self
Tejun Heoe22bee72010-06-29 10:07:14 +02002341 *
2342 * Workqueue rescuer thread function. There's one rescuer for each
2343 * workqueue which has WQ_RESCUER set.
2344 *
Tejun Heo706026c2013-01-24 11:01:34 -08002345 * Regular work processing on a pool may block trying to create a new
Tejun Heoe22bee72010-06-29 10:07:14 +02002346 * worker which uses GFP_KERNEL allocation which has slight chance of
2347 * developing into deadlock if some works currently on the same queue
2348 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2349 * the problem rescuer solves.
2350 *
Tejun Heo706026c2013-01-24 11:01:34 -08002351 * When such condition is possible, the pool summons rescuers of all
2352 * workqueues which have works queued on the pool and let them process
Tejun Heoe22bee72010-06-29 10:07:14 +02002353 * those works so that forward progress can be guaranteed.
2354 *
2355 * This should happen rarely.
2356 */
Tejun Heo111c2252013-01-17 17:16:24 -08002357static int rescuer_thread(void *__rescuer)
Tejun Heoe22bee72010-06-29 10:07:14 +02002358{
Tejun Heo111c2252013-01-17 17:16:24 -08002359 struct worker *rescuer = __rescuer;
2360 struct workqueue_struct *wq = rescuer->rescue_wq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002361 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heoe22bee72010-06-29 10:07:14 +02002362
2363 set_user_nice(current, RESCUER_NICE_LEVEL);
Tejun Heo111c2252013-01-17 17:16:24 -08002364
2365 /*
2366 * Mark rescuer as worker too. As WORKER_PREP is never cleared, it
2367 * doesn't participate in concurrency management.
2368 */
2369 rescuer->task->flags |= PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002370repeat:
2371 set_current_state(TASK_INTERRUPTIBLE);
2372
Mike Galbraith412d32e2012-11-28 07:17:18 +01002373 if (kthread_should_stop()) {
2374 __set_current_state(TASK_RUNNING);
Tejun Heo111c2252013-01-17 17:16:24 -08002375 rescuer->task->flags &= ~PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002376 return 0;
Mike Galbraith412d32e2012-11-28 07:17:18 +01002377 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002378
Tejun Heo493a1722013-03-12 11:29:59 -07002379 /* see whether any pwq is asking for help */
2380 spin_lock_irq(&workqueue_lock);
2381
2382 while (!list_empty(&wq->maydays)) {
2383 struct pool_workqueue *pwq = list_first_entry(&wq->maydays,
2384 struct pool_workqueue, mayday_node);
Tejun Heo112202d2013-02-13 19:29:12 -08002385 struct worker_pool *pool = pwq->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002386 struct work_struct *work, *n;
2387
2388 __set_current_state(TASK_RUNNING);
Tejun Heo493a1722013-03-12 11:29:59 -07002389 list_del_init(&pwq->mayday_node);
2390
2391 spin_unlock_irq(&workqueue_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002392
2393 /* migrate to the target cpu if possible */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08002394 worker_maybe_bind_and_lock(pool);
Lai Jiangshanb3104102013-02-19 12:17:02 -08002395 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002396
2397 /*
2398 * Slurp in all works issued via this workqueue and
2399 * process'em.
2400 */
Tejun Heo6183c002013-03-12 11:29:57 -07002401 WARN_ON_ONCE(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002402 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heo112202d2013-02-13 19:29:12 -08002403 if (get_work_pwq(work) == pwq)
Tejun Heoe22bee72010-06-29 10:07:14 +02002404 move_linked_works(work, scheduled, &n);
2405
2406 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002407
2408 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002409 * Leave this pool. If keep_working() is %true, notify a
Tejun Heo75769582011-02-14 14:04:46 +01002410 * regular worker; otherwise, we end up with 0 concurrency
2411 * and stalling the execution.
2412 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002413 if (keep_working(pool))
2414 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002415
Lai Jiangshanb3104102013-02-19 12:17:02 -08002416 rescuer->pool = NULL;
Tejun Heo493a1722013-03-12 11:29:59 -07002417 spin_unlock(&pool->lock);
2418 spin_lock(&workqueue_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002419 }
2420
Tejun Heo493a1722013-03-12 11:29:59 -07002421 spin_unlock_irq(&workqueue_lock);
2422
Tejun Heo111c2252013-01-17 17:16:24 -08002423 /* rescuers should never participate in concurrency management */
2424 WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
Tejun Heoe22bee72010-06-29 10:07:14 +02002425 schedule();
2426 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427}
2428
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002429struct wq_barrier {
2430 struct work_struct work;
2431 struct completion done;
2432};
2433
2434static void wq_barrier_func(struct work_struct *work)
2435{
2436 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2437 complete(&barr->done);
2438}
2439
Tejun Heo4690c4a2010-06-29 10:07:10 +02002440/**
2441 * insert_wq_barrier - insert a barrier work
Tejun Heo112202d2013-02-13 19:29:12 -08002442 * @pwq: pwq to insert barrier into
Tejun Heo4690c4a2010-06-29 10:07:10 +02002443 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002444 * @target: target work to attach @barr to
2445 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002446 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002447 * @barr is linked to @target such that @barr is completed only after
2448 * @target finishes execution. Please note that the ordering
2449 * guarantee is observed only with respect to @target and on the local
2450 * cpu.
2451 *
2452 * Currently, a queued barrier can't be canceled. This is because
2453 * try_to_grab_pending() can't determine whether the work to be
2454 * grabbed is at the head of the queue and thus can't clear LINKED
2455 * flag of the previous work while there must be a valid next work
2456 * after a work with LINKED flag set.
2457 *
2458 * Note that when @worker is non-NULL, @target may be modified
Tejun Heo112202d2013-02-13 19:29:12 -08002459 * underneath us, so we can't reliably determine pwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002460 *
2461 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002462 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002463 */
Tejun Heo112202d2013-02-13 19:29:12 -08002464static void insert_wq_barrier(struct pool_workqueue *pwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002465 struct wq_barrier *barr,
2466 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002467{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002468 struct list_head *head;
2469 unsigned int linked = 0;
2470
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002471 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002472 * debugobject calls are safe here even with pool->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002473 * as we know for sure that this will not trigger any of the
2474 * checks and call back into the fixup functions where we
2475 * might deadlock.
2476 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002477 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002478 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002479 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002480
Tejun Heoaffee4b2010-06-29 10:07:12 +02002481 /*
2482 * If @target is currently being executed, schedule the
2483 * barrier to the worker; otherwise, put it after @target.
2484 */
2485 if (worker)
2486 head = worker->scheduled.next;
2487 else {
2488 unsigned long *bits = work_data_bits(target);
2489
2490 head = target->entry.next;
2491 /* there can already be other linked works, inherit and set */
2492 linked = *bits & WORK_STRUCT_LINKED;
2493 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2494 }
2495
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002496 debug_work_activate(&barr->work);
Tejun Heo112202d2013-02-13 19:29:12 -08002497 insert_work(pwq, &barr->work, head,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002498 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002499}
2500
Tejun Heo73f53c42010-06-29 10:07:11 +02002501/**
Tejun Heo112202d2013-02-13 19:29:12 -08002502 * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing
Tejun Heo73f53c42010-06-29 10:07:11 +02002503 * @wq: workqueue being flushed
2504 * @flush_color: new flush color, < 0 for no-op
2505 * @work_color: new work color, < 0 for no-op
2506 *
Tejun Heo112202d2013-02-13 19:29:12 -08002507 * Prepare pwqs for workqueue flushing.
Tejun Heo73f53c42010-06-29 10:07:11 +02002508 *
Tejun Heo112202d2013-02-13 19:29:12 -08002509 * If @flush_color is non-negative, flush_color on all pwqs should be
2510 * -1. If no pwq has in-flight commands at the specified color, all
2511 * pwq->flush_color's stay at -1 and %false is returned. If any pwq
2512 * has in flight commands, its pwq->flush_color is set to
2513 * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq
Tejun Heo73f53c42010-06-29 10:07:11 +02002514 * wakeup logic is armed and %true is returned.
2515 *
2516 * The caller should have initialized @wq->first_flusher prior to
2517 * calling this function with non-negative @flush_color. If
2518 * @flush_color is negative, no flush color update is done and %false
2519 * is returned.
2520 *
Tejun Heo112202d2013-02-13 19:29:12 -08002521 * If @work_color is non-negative, all pwqs should have the same
Tejun Heo73f53c42010-06-29 10:07:11 +02002522 * work_color which is previous to @work_color and all will be
2523 * advanced to @work_color.
2524 *
2525 * CONTEXT:
2526 * mutex_lock(wq->flush_mutex).
2527 *
2528 * RETURNS:
2529 * %true if @flush_color >= 0 and there's something to flush. %false
2530 * otherwise.
2531 */
Tejun Heo112202d2013-02-13 19:29:12 -08002532static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
Tejun Heo73f53c42010-06-29 10:07:11 +02002533 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534{
Tejun Heo73f53c42010-06-29 10:07:11 +02002535 bool wait = false;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002536 struct pool_workqueue *pwq;
Oleg Nesterov14441962007-05-23 13:57:57 -07002537
Tejun Heo73f53c42010-06-29 10:07:11 +02002538 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002539 WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush));
Tejun Heo112202d2013-02-13 19:29:12 -08002540 atomic_set(&wq->nr_pwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002541 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002542
Tejun Heo76af4d92013-03-12 11:30:00 -07002543 local_irq_disable();
2544
Tejun Heo49e3cf42013-03-12 11:29:58 -07002545 for_each_pwq(pwq, wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08002546 struct worker_pool *pool = pwq->pool;
Tejun Heo73f53c42010-06-29 10:07:11 +02002547
Tejun Heo76af4d92013-03-12 11:30:00 -07002548 spin_lock(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002549
2550 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002551 WARN_ON_ONCE(pwq->flush_color != -1);
Tejun Heo73f53c42010-06-29 10:07:11 +02002552
Tejun Heo112202d2013-02-13 19:29:12 -08002553 if (pwq->nr_in_flight[flush_color]) {
2554 pwq->flush_color = flush_color;
2555 atomic_inc(&wq->nr_pwqs_to_flush);
Tejun Heo73f53c42010-06-29 10:07:11 +02002556 wait = true;
2557 }
2558 }
2559
2560 if (work_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002561 WARN_ON_ONCE(work_color != work_next_color(pwq->work_color));
Tejun Heo112202d2013-02-13 19:29:12 -08002562 pwq->work_color = work_color;
Tejun Heo73f53c42010-06-29 10:07:11 +02002563 }
2564
Tejun Heo76af4d92013-03-12 11:30:00 -07002565 spin_unlock(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002566 }
2567
Tejun Heo76af4d92013-03-12 11:30:00 -07002568 local_irq_enable();
2569
Tejun Heo112202d2013-02-13 19:29:12 -08002570 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush))
Tejun Heo73f53c42010-06-29 10:07:11 +02002571 complete(&wq->first_flusher->done);
2572
2573 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574}
2575
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002576/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002578 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 *
2580 * Forces execution of the workqueue and blocks until its completion.
2581 * This is typically used in driver shutdown handlers.
2582 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002583 * We sleep until all works which were queued on entry have been handled,
2584 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002586void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587{
Tejun Heo73f53c42010-06-29 10:07:11 +02002588 struct wq_flusher this_flusher = {
2589 .list = LIST_HEAD_INIT(this_flusher.list),
2590 .flush_color = -1,
2591 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2592 };
2593 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002594
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002595 lock_map_acquire(&wq->lockdep_map);
2596 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002597
2598 mutex_lock(&wq->flush_mutex);
2599
2600 /*
2601 * Start-to-wait phase
2602 */
2603 next_color = work_next_color(wq->work_color);
2604
2605 if (next_color != wq->flush_color) {
2606 /*
2607 * Color space is not full. The current work_color
2608 * becomes our flush_color and work_color is advanced
2609 * by one.
2610 */
Tejun Heo6183c002013-03-12 11:29:57 -07002611 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow));
Tejun Heo73f53c42010-06-29 10:07:11 +02002612 this_flusher.flush_color = wq->work_color;
2613 wq->work_color = next_color;
2614
2615 if (!wq->first_flusher) {
2616 /* no flush in progress, become the first flusher */
Tejun Heo6183c002013-03-12 11:29:57 -07002617 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002618
2619 wq->first_flusher = &this_flusher;
2620
Tejun Heo112202d2013-02-13 19:29:12 -08002621 if (!flush_workqueue_prep_pwqs(wq, wq->flush_color,
Tejun Heo73f53c42010-06-29 10:07:11 +02002622 wq->work_color)) {
2623 /* nothing to flush, done */
2624 wq->flush_color = next_color;
2625 wq->first_flusher = NULL;
2626 goto out_unlock;
2627 }
2628 } else {
2629 /* wait in queue */
Tejun Heo6183c002013-03-12 11:29:57 -07002630 WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002631 list_add_tail(&this_flusher.list, &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002632 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002633 }
2634 } else {
2635 /*
2636 * Oops, color space is full, wait on overflow queue.
2637 * The next flush completion will assign us
2638 * flush_color and transfer to flusher_queue.
2639 */
2640 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2641 }
2642
2643 mutex_unlock(&wq->flush_mutex);
2644
2645 wait_for_completion(&this_flusher.done);
2646
2647 /*
2648 * Wake-up-and-cascade phase
2649 *
2650 * First flushers are responsible for cascading flushes and
2651 * handling overflow. Non-first flushers can simply return.
2652 */
2653 if (wq->first_flusher != &this_flusher)
2654 return;
2655
2656 mutex_lock(&wq->flush_mutex);
2657
Tejun Heo4ce48b32010-07-02 10:03:51 +02002658 /* we might have raced, check again with mutex held */
2659 if (wq->first_flusher != &this_flusher)
2660 goto out_unlock;
2661
Tejun Heo73f53c42010-06-29 10:07:11 +02002662 wq->first_flusher = NULL;
2663
Tejun Heo6183c002013-03-12 11:29:57 -07002664 WARN_ON_ONCE(!list_empty(&this_flusher.list));
2665 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002666
2667 while (true) {
2668 struct wq_flusher *next, *tmp;
2669
2670 /* complete all the flushers sharing the current flush color */
2671 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2672 if (next->flush_color != wq->flush_color)
2673 break;
2674 list_del_init(&next->list);
2675 complete(&next->done);
2676 }
2677
Tejun Heo6183c002013-03-12 11:29:57 -07002678 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) &&
2679 wq->flush_color != work_next_color(wq->work_color));
Tejun Heo73f53c42010-06-29 10:07:11 +02002680
2681 /* this flush_color is finished, advance by one */
2682 wq->flush_color = work_next_color(wq->flush_color);
2683
2684 /* one color has been freed, handle overflow queue */
2685 if (!list_empty(&wq->flusher_overflow)) {
2686 /*
2687 * Assign the same color to all overflowed
2688 * flushers, advance work_color and append to
2689 * flusher_queue. This is the start-to-wait
2690 * phase for these overflowed flushers.
2691 */
2692 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2693 tmp->flush_color = wq->work_color;
2694
2695 wq->work_color = work_next_color(wq->work_color);
2696
2697 list_splice_tail_init(&wq->flusher_overflow,
2698 &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002699 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002700 }
2701
2702 if (list_empty(&wq->flusher_queue)) {
Tejun Heo6183c002013-03-12 11:29:57 -07002703 WARN_ON_ONCE(wq->flush_color != wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002704 break;
2705 }
2706
2707 /*
2708 * Need to flush more colors. Make the next flusher
Tejun Heo112202d2013-02-13 19:29:12 -08002709 * the new first flusher and arm pwqs.
Tejun Heo73f53c42010-06-29 10:07:11 +02002710 */
Tejun Heo6183c002013-03-12 11:29:57 -07002711 WARN_ON_ONCE(wq->flush_color == wq->work_color);
2712 WARN_ON_ONCE(wq->flush_color != next->flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002713
2714 list_del_init(&next->list);
2715 wq->first_flusher = next;
2716
Tejun Heo112202d2013-02-13 19:29:12 -08002717 if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1))
Tejun Heo73f53c42010-06-29 10:07:11 +02002718 break;
2719
2720 /*
2721 * Meh... this color is already done, clear first
2722 * flusher and repeat cascading.
2723 */
2724 wq->first_flusher = NULL;
2725 }
2726
2727out_unlock:
2728 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729}
Dave Jonesae90dd52006-06-30 01:40:45 -04002730EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002732/**
2733 * drain_workqueue - drain a workqueue
2734 * @wq: workqueue to drain
2735 *
2736 * Wait until the workqueue becomes empty. While draining is in progress,
2737 * only chain queueing is allowed. IOW, only currently pending or running
2738 * work items on @wq can queue further work items on it. @wq is flushed
2739 * repeatedly until it becomes empty. The number of flushing is detemined
2740 * by the depth of chaining and should be relatively short. Whine if it
2741 * takes too long.
2742 */
2743void drain_workqueue(struct workqueue_struct *wq)
2744{
2745 unsigned int flush_cnt = 0;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002746 struct pool_workqueue *pwq;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002747
2748 /*
2749 * __queue_work() needs to test whether there are drainers, is much
2750 * hotter than drain_workqueue() and already looks at @wq->flags.
2751 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2752 */
Tejun Heoe98d5b12013-03-12 11:29:57 -07002753 spin_lock_irq(&workqueue_lock);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002754 if (!wq->nr_drainers++)
2755 wq->flags |= WQ_DRAINING;
Tejun Heoe98d5b12013-03-12 11:29:57 -07002756 spin_unlock_irq(&workqueue_lock);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002757reflush:
2758 flush_workqueue(wq);
2759
Tejun Heo76af4d92013-03-12 11:30:00 -07002760 local_irq_disable();
2761
Tejun Heo49e3cf42013-03-12 11:29:58 -07002762 for_each_pwq(pwq, wq) {
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002763 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002764
Tejun Heo76af4d92013-03-12 11:30:00 -07002765 spin_lock(&pwq->pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08002766 drained = !pwq->nr_active && list_empty(&pwq->delayed_works);
Tejun Heo76af4d92013-03-12 11:30:00 -07002767 spin_unlock(&pwq->pool->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002768
2769 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002770 continue;
2771
2772 if (++flush_cnt == 10 ||
2773 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
Valentin Ilie044c7822012-08-19 00:52:42 +03002774 pr_warn("workqueue %s: flush on destruction isn't complete after %u tries\n",
2775 wq->name, flush_cnt);
Tejun Heo76af4d92013-03-12 11:30:00 -07002776
2777 local_irq_enable();
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002778 goto reflush;
2779 }
2780
Tejun Heo76af4d92013-03-12 11:30:00 -07002781 spin_lock(&workqueue_lock);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002782 if (!--wq->nr_drainers)
2783 wq->flags &= ~WQ_DRAINING;
Tejun Heo76af4d92013-03-12 11:30:00 -07002784 spin_unlock(&workqueue_lock);
2785
2786 local_irq_enable();
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002787}
2788EXPORT_SYMBOL_GPL(drain_workqueue);
2789
Tejun Heo606a5022012-08-20 14:51:23 -07002790static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
Tejun Heobaf59022010-09-16 10:42:16 +02002791{
2792 struct worker *worker = NULL;
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002793 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002794 struct pool_workqueue *pwq;
Tejun Heobaf59022010-09-16 10:42:16 +02002795
2796 might_sleep();
Tejun Heobaf59022010-09-16 10:42:16 +02002797
Tejun Heofa1b54e2013-03-12 11:30:00 -07002798 local_irq_disable();
2799 pool = get_work_pool(work);
2800 if (!pool) {
2801 local_irq_enable();
2802 return false;
2803 }
2804
2805 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08002806 /* see the comment in try_to_grab_pending() with the same code */
Tejun Heo112202d2013-02-13 19:29:12 -08002807 pwq = get_work_pwq(work);
2808 if (pwq) {
2809 if (unlikely(pwq->pool != pool))
Tejun Heobaf59022010-09-16 10:42:16 +02002810 goto already_gone;
Tejun Heo606a5022012-08-20 14:51:23 -07002811 } else {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002812 worker = find_worker_executing_work(pool, work);
Tejun Heobaf59022010-09-16 10:42:16 +02002813 if (!worker)
2814 goto already_gone;
Tejun Heo112202d2013-02-13 19:29:12 -08002815 pwq = worker->current_pwq;
Tejun Heo606a5022012-08-20 14:51:23 -07002816 }
Tejun Heobaf59022010-09-16 10:42:16 +02002817
Tejun Heo112202d2013-02-13 19:29:12 -08002818 insert_wq_barrier(pwq, barr, work, worker);
Tejun Heod565ed62013-01-24 11:01:33 -08002819 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002820
Tejun Heoe1594892011-01-09 23:32:15 +01002821 /*
2822 * If @max_active is 1 or rescuer is in use, flushing another work
2823 * item on the same workqueue may lead to deadlock. Make sure the
2824 * flusher is not running on the same workqueue by verifying write
2825 * access.
2826 */
Tejun Heo112202d2013-02-13 19:29:12 -08002827 if (pwq->wq->saved_max_active == 1 || pwq->wq->flags & WQ_RESCUER)
2828 lock_map_acquire(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002829 else
Tejun Heo112202d2013-02-13 19:29:12 -08002830 lock_map_acquire_read(&pwq->wq->lockdep_map);
2831 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002832
Tejun Heobaf59022010-09-16 10:42:16 +02002833 return true;
2834already_gone:
Tejun Heod565ed62013-01-24 11:01:33 -08002835 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002836 return false;
2837}
2838
Oleg Nesterovdb700892008-07-25 01:47:49 -07002839/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002840 * flush_work - wait for a work to finish executing the last queueing instance
2841 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002842 *
Tejun Heo606a5022012-08-20 14:51:23 -07002843 * Wait until @work has finished execution. @work is guaranteed to be idle
2844 * on return if it hasn't been requeued since flush started.
Tejun Heo401a8d02010-09-16 10:36:00 +02002845 *
2846 * RETURNS:
2847 * %true if flush_work() waited for the work to finish execution,
2848 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002849 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002850bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002851{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002852 struct wq_barrier barr;
2853
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002854 lock_map_acquire(&work->lockdep_map);
2855 lock_map_release(&work->lockdep_map);
2856
Tejun Heo606a5022012-08-20 14:51:23 -07002857 if (start_flush_work(work, &barr)) {
Tejun Heobaf59022010-09-16 10:42:16 +02002858 wait_for_completion(&barr.done);
2859 destroy_work_on_stack(&barr.work);
2860 return true;
Tejun Heo606a5022012-08-20 14:51:23 -07002861 } else {
Tejun Heobaf59022010-09-16 10:42:16 +02002862 return false;
Tejun Heo606a5022012-08-20 14:51:23 -07002863 }
Oleg Nesterovdb700892008-07-25 01:47:49 -07002864}
2865EXPORT_SYMBOL_GPL(flush_work);
2866
Tejun Heo36e227d2012-08-03 10:30:46 -07002867static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
Tejun Heo401a8d02010-09-16 10:36:00 +02002868{
Tejun Heobbb68df2012-08-03 10:30:46 -07002869 unsigned long flags;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002870 int ret;
2871
2872 do {
Tejun Heobbb68df2012-08-03 10:30:46 -07002873 ret = try_to_grab_pending(work, is_dwork, &flags);
2874 /*
2875 * If someone else is canceling, wait for the same event it
2876 * would be waiting for before retrying.
2877 */
2878 if (unlikely(ret == -ENOENT))
Tejun Heo606a5022012-08-20 14:51:23 -07002879 flush_work(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002880 } while (unlikely(ret < 0));
2881
Tejun Heobbb68df2012-08-03 10:30:46 -07002882 /* tell other tasks trying to grab @work to back off */
2883 mark_work_canceling(work);
2884 local_irq_restore(flags);
2885
Tejun Heo606a5022012-08-20 14:51:23 -07002886 flush_work(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002887 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002888 return ret;
2889}
2890
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002891/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002892 * cancel_work_sync - cancel a work and wait for it to finish
2893 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002894 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002895 * Cancel @work and wait for its execution to finish. This function
2896 * can be used even if the work re-queues itself or migrates to
2897 * another workqueue. On return from this function, @work is
2898 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002899 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002900 * cancel_work_sync(&delayed_work->work) must not be used for
2901 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002902 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002903 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002904 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002905 *
2906 * RETURNS:
2907 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002908 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002909bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002910{
Tejun Heo36e227d2012-08-03 10:30:46 -07002911 return __cancel_work_timer(work, false);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002912}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002913EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002914
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002915/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002916 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2917 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002918 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002919 * Delayed timer is cancelled and the pending work is queued for
2920 * immediate execution. Like flush_work(), this function only
2921 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002922 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002923 * RETURNS:
2924 * %true if flush_work() waited for the work to finish execution,
2925 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002926 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002927bool flush_delayed_work(struct delayed_work *dwork)
2928{
Tejun Heo8930cab2012-08-03 10:30:45 -07002929 local_irq_disable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002930 if (del_timer_sync(&dwork->timer))
Lai Jiangshan60c057b2013-02-06 18:04:53 -08002931 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Tejun Heo8930cab2012-08-03 10:30:45 -07002932 local_irq_enable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002933 return flush_work(&dwork->work);
2934}
2935EXPORT_SYMBOL(flush_delayed_work);
2936
2937/**
Tejun Heo57b30ae2012-08-21 13:18:24 -07002938 * cancel_delayed_work - cancel a delayed work
2939 * @dwork: delayed_work to cancel
Tejun Heo09383492010-09-16 10:48:29 +02002940 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002941 * Kill off a pending delayed_work. Returns %true if @dwork was pending
2942 * and canceled; %false if wasn't pending. Note that the work callback
2943 * function may still be running on return, unless it returns %true and the
2944 * work doesn't re-arm itself. Explicitly flush or use
2945 * cancel_delayed_work_sync() to wait on it.
Tejun Heo09383492010-09-16 10:48:29 +02002946 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002947 * This function is safe to call from any context including IRQ handler.
Tejun Heo09383492010-09-16 10:48:29 +02002948 */
Tejun Heo57b30ae2012-08-21 13:18:24 -07002949bool cancel_delayed_work(struct delayed_work *dwork)
Tejun Heo09383492010-09-16 10:48:29 +02002950{
Tejun Heo57b30ae2012-08-21 13:18:24 -07002951 unsigned long flags;
2952 int ret;
2953
2954 do {
2955 ret = try_to_grab_pending(&dwork->work, true, &flags);
2956 } while (unlikely(ret == -EAGAIN));
2957
2958 if (unlikely(ret < 0))
2959 return false;
2960
Tejun Heo7c3eed52013-01-24 11:01:33 -08002961 set_work_pool_and_clear_pending(&dwork->work,
2962 get_work_pool_id(&dwork->work));
Tejun Heo57b30ae2012-08-21 13:18:24 -07002963 local_irq_restore(flags);
Dan Magenheimerc0158ca2012-10-18 16:31:37 -07002964 return ret;
Tejun Heo09383492010-09-16 10:48:29 +02002965}
Tejun Heo57b30ae2012-08-21 13:18:24 -07002966EXPORT_SYMBOL(cancel_delayed_work);
Tejun Heo09383492010-09-16 10:48:29 +02002967
2968/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002969 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2970 * @dwork: the delayed work cancel
2971 *
2972 * This is cancel_work_sync() for delayed works.
2973 *
2974 * RETURNS:
2975 * %true if @dwork was pending, %false otherwise.
2976 */
2977bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002978{
Tejun Heo36e227d2012-08-03 10:30:46 -07002979 return __cancel_work_timer(&dwork->work, true);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002980}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002981EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002983/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07002984 * schedule_work_on - put work task on a specific cpu
2985 * @cpu: cpu to put the work task on
2986 * @work: job to be done
2987 *
2988 * This puts a job on a specific cpu
2989 */
Tejun Heod4283e92012-08-03 10:30:44 -07002990bool schedule_work_on(int cpu, struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07002991{
Tejun Heod320c032010-06-29 10:07:14 +02002992 return queue_work_on(cpu, system_wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07002993}
2994EXPORT_SYMBOL(schedule_work_on);
2995
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002996/**
Dave Jonesae90dd52006-06-30 01:40:45 -04002997 * schedule_work - put work task in global workqueue
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 * @work: job to be done
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 *
Tejun Heod4283e92012-08-03 10:30:44 -07003000 * Returns %false if @work was already on the kernel-global workqueue and
3001 * %true otherwise.
David Howells52bad642006-11-22 14:54:01 +00003002 *
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003003 * This puts a job in the kernel-global workqueue if it was not already
3004 * queued and leaves it in the same position on the kernel-global
3005 * workqueue otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 */
Tejun Heod4283e92012-08-03 10:30:44 -07003007bool schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008{
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003009 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010}
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003011EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07003013/**
3014 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
3015 * @cpu: cpu to use
3016 * @dwork: job to be done
3017 * @delay: number of jiffies to wait
3018 *
3019 * After waiting for a given time this puts a job in the kernel-global
3020 * workqueue on the specified CPU.
3021 */
Tejun Heod4283e92012-08-03 10:30:44 -07003022bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork,
3023 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024{
Tejun Heod320c032010-06-29 10:07:14 +02003025 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026}
Dave Jonesae90dd52006-06-30 01:40:45 -04003027EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028
Andrew Mortonb6136772006-06-25 05:47:49 -07003029/**
Tejun Heo0a13c002012-08-03 10:30:44 -07003030 * schedule_delayed_work - put work task in global workqueue after delay
3031 * @dwork: job to be done
3032 * @delay: number of jiffies to wait or 0 for immediate execution
3033 *
3034 * After waiting for a given time this puts a job in the kernel-global
3035 * workqueue.
3036 */
Tejun Heod4283e92012-08-03 10:30:44 -07003037bool schedule_delayed_work(struct delayed_work *dwork, unsigned long delay)
Tejun Heo0a13c002012-08-03 10:30:44 -07003038{
3039 return queue_delayed_work(system_wq, dwork, delay);
3040}
3041EXPORT_SYMBOL(schedule_delayed_work);
3042
3043/**
Tejun Heo31ddd872010-10-19 11:14:49 +02003044 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07003045 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07003046 *
Tejun Heo31ddd872010-10-19 11:14:49 +02003047 * schedule_on_each_cpu() executes @func on each online CPU using the
3048 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07003049 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02003050 *
3051 * RETURNS:
3052 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07003053 */
David Howells65f27f32006-11-22 14:55:48 +00003054int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08003055{
3056 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02003057 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08003058
Andrew Mortonb6136772006-06-25 05:47:49 -07003059 works = alloc_percpu(struct work_struct);
3060 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08003061 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07003062
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003063 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08003064
Christoph Lameter15316ba2006-01-08 01:00:43 -08003065 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01003066 struct work_struct *work = per_cpu_ptr(works, cpu);
3067
3068 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003069 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02003070 }
Tejun Heo93981802009-11-17 14:06:20 -08003071
3072 for_each_online_cpu(cpu)
3073 flush_work(per_cpu_ptr(works, cpu));
3074
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003075 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07003076 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08003077 return 0;
3078}
3079
Alan Sterneef6a7d2010-02-12 17:39:21 +09003080/**
3081 * flush_scheduled_work - ensure that any scheduled work has run to completion.
3082 *
3083 * Forces execution of the kernel-global workqueue and blocks until its
3084 * completion.
3085 *
3086 * Think twice before calling this function! It's very easy to get into
3087 * trouble if you don't take great care. Either of the following situations
3088 * will lead to deadlock:
3089 *
3090 * One of the work items currently on the workqueue needs to acquire
3091 * a lock held by your code or its caller.
3092 *
3093 * Your code is running in the context of a work routine.
3094 *
3095 * They will be detected by lockdep when they occur, but the first might not
3096 * occur very often. It depends on what work items are on the workqueue and
3097 * what locks they need, which you have no control over.
3098 *
3099 * In most situations flushing the entire workqueue is overkill; you merely
3100 * need to know that a particular work item isn't queued and isn't running.
3101 * In such cases you should use cancel_delayed_work_sync() or
3102 * cancel_work_sync() instead.
3103 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104void flush_scheduled_work(void)
3105{
Tejun Heod320c032010-06-29 10:07:14 +02003106 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107}
Dave Jonesae90dd52006-06-30 01:40:45 -04003108EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
3110/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06003111 * execute_in_process_context - reliably execute the routine with user context
3112 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06003113 * @ew: guaranteed storage for the execute work structure (must
3114 * be available when the work executes)
3115 *
3116 * Executes the function immediately if process context is available,
3117 * otherwise schedules the function for delayed execution.
3118 *
3119 * Returns: 0 - function was executed
3120 * 1 - function was scheduled for execution
3121 */
David Howells65f27f32006-11-22 14:55:48 +00003122int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06003123{
3124 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00003125 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003126 return 0;
3127 }
3128
David Howells65f27f32006-11-22 14:55:48 +00003129 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003130 schedule_work(&ew->work);
3131
3132 return 1;
3133}
3134EXPORT_SYMBOL_GPL(execute_in_process_context);
3135
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136int keventd_up(void)
3137{
Tejun Heod320c032010-06-29 10:07:14 +02003138 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139}
3140
Tejun Heo7a4e3442013-03-12 11:30:00 -07003141/**
3142 * free_workqueue_attrs - free a workqueue_attrs
3143 * @attrs: workqueue_attrs to free
3144 *
3145 * Undo alloc_workqueue_attrs().
3146 */
3147void free_workqueue_attrs(struct workqueue_attrs *attrs)
3148{
3149 if (attrs) {
3150 free_cpumask_var(attrs->cpumask);
3151 kfree(attrs);
3152 }
3153}
3154
3155/**
3156 * alloc_workqueue_attrs - allocate a workqueue_attrs
3157 * @gfp_mask: allocation mask to use
3158 *
3159 * Allocate a new workqueue_attrs, initialize with default settings and
3160 * return it. Returns NULL on failure.
3161 */
3162struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask)
3163{
3164 struct workqueue_attrs *attrs;
3165
3166 attrs = kzalloc(sizeof(*attrs), gfp_mask);
3167 if (!attrs)
3168 goto fail;
3169 if (!alloc_cpumask_var(&attrs->cpumask, gfp_mask))
3170 goto fail;
3171
3172 cpumask_setall(attrs->cpumask);
3173 return attrs;
3174fail:
3175 free_workqueue_attrs(attrs);
3176 return NULL;
3177}
3178
Tejun Heo29c91e92013-03-12 11:30:03 -07003179static void copy_workqueue_attrs(struct workqueue_attrs *to,
3180 const struct workqueue_attrs *from)
3181{
3182 to->nice = from->nice;
3183 cpumask_copy(to->cpumask, from->cpumask);
3184}
3185
3186/*
3187 * Hacky implementation of jhash of bitmaps which only considers the
3188 * specified number of bits. We probably want a proper implementation in
3189 * include/linux/jhash.h.
3190 */
3191static u32 jhash_bitmap(const unsigned long *bitmap, int bits, u32 hash)
3192{
3193 int nr_longs = bits / BITS_PER_LONG;
3194 int nr_leftover = bits % BITS_PER_LONG;
3195 unsigned long leftover = 0;
3196
3197 if (nr_longs)
3198 hash = jhash(bitmap, nr_longs * sizeof(long), hash);
3199 if (nr_leftover) {
3200 bitmap_copy(&leftover, bitmap + nr_longs, nr_leftover);
3201 hash = jhash(&leftover, sizeof(long), hash);
3202 }
3203 return hash;
3204}
3205
3206/* hash value of the content of @attr */
3207static u32 wqattrs_hash(const struct workqueue_attrs *attrs)
3208{
3209 u32 hash = 0;
3210
3211 hash = jhash_1word(attrs->nice, hash);
3212 hash = jhash_bitmap(cpumask_bits(attrs->cpumask), nr_cpu_ids, hash);
3213 return hash;
3214}
3215
3216/* content equality test */
3217static bool wqattrs_equal(const struct workqueue_attrs *a,
3218 const struct workqueue_attrs *b)
3219{
3220 if (a->nice != b->nice)
3221 return false;
3222 if (!cpumask_equal(a->cpumask, b->cpumask))
3223 return false;
3224 return true;
3225}
3226
Tejun Heo7a4e3442013-03-12 11:30:00 -07003227/**
3228 * init_worker_pool - initialize a newly zalloc'd worker_pool
3229 * @pool: worker_pool to initialize
3230 *
3231 * Initiailize a newly zalloc'd @pool. It also allocates @pool->attrs.
Tejun Heo29c91e92013-03-12 11:30:03 -07003232 * Returns 0 on success, -errno on failure. Even on failure, all fields
3233 * inside @pool proper are initialized and put_unbound_pool() can be called
3234 * on @pool safely to release it.
Tejun Heo7a4e3442013-03-12 11:30:00 -07003235 */
3236static int init_worker_pool(struct worker_pool *pool)
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003237{
3238 spin_lock_init(&pool->lock);
Tejun Heo29c91e92013-03-12 11:30:03 -07003239 pool->id = -1;
3240 pool->cpu = -1;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003241 pool->flags |= POOL_DISASSOCIATED;
3242 INIT_LIST_HEAD(&pool->worklist);
3243 INIT_LIST_HEAD(&pool->idle_list);
3244 hash_init(pool->busy_hash);
3245
3246 init_timer_deferrable(&pool->idle_timer);
3247 pool->idle_timer.function = idle_worker_timeout;
3248 pool->idle_timer.data = (unsigned long)pool;
3249
3250 setup_timer(&pool->mayday_timer, pool_mayday_timeout,
3251 (unsigned long)pool);
3252
3253 mutex_init(&pool->manager_arb);
3254 mutex_init(&pool->assoc_mutex);
3255 ida_init(&pool->worker_ida);
Tejun Heo7a4e3442013-03-12 11:30:00 -07003256
Tejun Heo29c91e92013-03-12 11:30:03 -07003257 INIT_HLIST_NODE(&pool->hash_node);
3258 pool->refcnt = 1;
3259
3260 /* shouldn't fail above this point */
Tejun Heo7a4e3442013-03-12 11:30:00 -07003261 pool->attrs = alloc_workqueue_attrs(GFP_KERNEL);
3262 if (!pool->attrs)
3263 return -ENOMEM;
3264 return 0;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003265}
3266
Tejun Heo29c91e92013-03-12 11:30:03 -07003267static void rcu_free_pool(struct rcu_head *rcu)
3268{
3269 struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu);
3270
3271 ida_destroy(&pool->worker_ida);
3272 free_workqueue_attrs(pool->attrs);
3273 kfree(pool);
3274}
3275
3276/**
3277 * put_unbound_pool - put a worker_pool
3278 * @pool: worker_pool to put
3279 *
3280 * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU
3281 * safe manner.
3282 */
3283static void put_unbound_pool(struct worker_pool *pool)
3284{
3285 struct worker *worker;
3286
3287 spin_lock_irq(&workqueue_lock);
3288 if (--pool->refcnt) {
3289 spin_unlock_irq(&workqueue_lock);
3290 return;
3291 }
3292
3293 /* sanity checks */
3294 if (WARN_ON(!(pool->flags & POOL_DISASSOCIATED)) ||
3295 WARN_ON(!list_empty(&pool->worklist))) {
3296 spin_unlock_irq(&workqueue_lock);
3297 return;
3298 }
3299
3300 /* release id and unhash */
3301 if (pool->id >= 0)
3302 idr_remove(&worker_pool_idr, pool->id);
3303 hash_del(&pool->hash_node);
3304
3305 spin_unlock_irq(&workqueue_lock);
3306
3307 /* lock out manager and destroy all workers */
3308 mutex_lock(&pool->manager_arb);
3309 spin_lock_irq(&pool->lock);
3310
3311 while ((worker = first_worker(pool)))
3312 destroy_worker(worker);
3313 WARN_ON(pool->nr_workers || pool->nr_idle);
3314
3315 spin_unlock_irq(&pool->lock);
3316 mutex_unlock(&pool->manager_arb);
3317
3318 /* shut down the timers */
3319 del_timer_sync(&pool->idle_timer);
3320 del_timer_sync(&pool->mayday_timer);
3321
3322 /* sched-RCU protected to allow dereferences from get_work_pool() */
3323 call_rcu_sched(&pool->rcu, rcu_free_pool);
3324}
3325
3326/**
3327 * get_unbound_pool - get a worker_pool with the specified attributes
3328 * @attrs: the attributes of the worker_pool to get
3329 *
3330 * Obtain a worker_pool which has the same attributes as @attrs, bump the
3331 * reference count and return it. If there already is a matching
3332 * worker_pool, it will be used; otherwise, this function attempts to
3333 * create a new one. On failure, returns NULL.
3334 */
3335static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs)
3336{
3337 static DEFINE_MUTEX(create_mutex);
3338 u32 hash = wqattrs_hash(attrs);
3339 struct worker_pool *pool;
3340 struct worker *worker;
3341
3342 mutex_lock(&create_mutex);
3343
3344 /* do we already have a matching pool? */
3345 spin_lock_irq(&workqueue_lock);
3346 hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) {
3347 if (wqattrs_equal(pool->attrs, attrs)) {
3348 pool->refcnt++;
3349 goto out_unlock;
3350 }
3351 }
3352 spin_unlock_irq(&workqueue_lock);
3353
3354 /* nope, create a new one */
3355 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
3356 if (!pool || init_worker_pool(pool) < 0)
3357 goto fail;
3358
3359 copy_workqueue_attrs(pool->attrs, attrs);
3360
3361 if (worker_pool_assign_id(pool) < 0)
3362 goto fail;
3363
3364 /* create and start the initial worker */
3365 worker = create_worker(pool);
3366 if (!worker)
3367 goto fail;
3368
3369 spin_lock_irq(&pool->lock);
3370 start_worker(worker);
3371 spin_unlock_irq(&pool->lock);
3372
3373 /* install */
3374 spin_lock_irq(&workqueue_lock);
3375 hash_add(unbound_pool_hash, &pool->hash_node, hash);
3376out_unlock:
3377 spin_unlock_irq(&workqueue_lock);
3378 mutex_unlock(&create_mutex);
3379 return pool;
3380fail:
3381 mutex_unlock(&create_mutex);
3382 if (pool)
3383 put_unbound_pool(pool);
3384 return NULL;
3385}
3386
Tejun Heo30cdf242013-03-12 11:29:57 -07003387static int alloc_and_link_pwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003389 bool highpri = wq->flags & WQ_HIGHPRI;
Tejun Heo30cdf242013-03-12 11:29:57 -07003390 int cpu;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01003391
Tejun Heo30cdf242013-03-12 11:29:57 -07003392 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo420c0dd2013-03-12 11:29:59 -07003393 wq->cpu_pwqs = alloc_percpu(struct pool_workqueue);
3394 if (!wq->cpu_pwqs)
Tejun Heo30cdf242013-03-12 11:29:57 -07003395 return -ENOMEM;
3396
3397 for_each_possible_cpu(cpu) {
Tejun Heo7fb98ea2013-03-12 11:30:00 -07003398 struct pool_workqueue *pwq =
3399 per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heo30cdf242013-03-12 11:29:57 -07003400
Tejun Heo49e3cf42013-03-12 11:29:58 -07003401 pwq->pool = get_std_worker_pool(cpu, highpri);
Tejun Heo76af4d92013-03-12 11:30:00 -07003402 list_add_tail_rcu(&pwq->pwqs_node, &wq->pwqs);
Tejun Heo30cdf242013-03-12 11:29:57 -07003403 }
3404 } else {
3405 struct pool_workqueue *pwq;
3406
3407 pwq = kmem_cache_zalloc(pwq_cache, GFP_KERNEL);
3408 if (!pwq)
3409 return -ENOMEM;
3410
Tejun Heo29c91e92013-03-12 11:30:03 -07003411 pwq->pool = get_unbound_pool(unbound_std_wq_attrs[highpri]);
3412 if (!pwq->pool) {
3413 kmem_cache_free(pwq_cache, pwq);
3414 return -ENOMEM;
3415 }
3416
Tejun Heo76af4d92013-03-12 11:30:00 -07003417 list_add_tail_rcu(&pwq->pwqs_node, &wq->pwqs);
Tejun Heo30cdf242013-03-12 11:29:57 -07003418 }
3419
3420 return 0;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003421}
3422
Tejun Heo112202d2013-02-13 19:29:12 -08003423static void free_pwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07003424{
Lai Jiangshane06ffa12012-03-09 18:03:20 +08003425 if (!(wq->flags & WQ_UNBOUND))
Tejun Heo420c0dd2013-03-12 11:29:59 -07003426 free_percpu(wq->cpu_pwqs);
3427 else if (!list_empty(&wq->pwqs))
3428 kmem_cache_free(pwq_cache, list_first_entry(&wq->pwqs,
3429 struct pool_workqueue, pwqs_node));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07003430}
3431
Tejun Heof3421792010-07-02 10:03:51 +02003432static int wq_clamp_max_active(int max_active, unsigned int flags,
3433 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003434{
Tejun Heof3421792010-07-02 10:03:51 +02003435 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3436
3437 if (max_active < 1 || max_active > lim)
Valentin Ilie044c7822012-08-19 00:52:42 +03003438 pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
3439 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003440
Tejun Heof3421792010-07-02 10:03:51 +02003441 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003442}
3443
Tejun Heob196be82012-01-10 15:11:35 -08003444struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02003445 unsigned int flags,
3446 int max_active,
3447 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08003448 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003449{
Tejun Heob196be82012-01-10 15:11:35 -08003450 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003451 struct workqueue_struct *wq;
Tejun Heo49e3cf42013-03-12 11:29:58 -07003452 struct pool_workqueue *pwq;
Tejun Heob196be82012-01-10 15:11:35 -08003453 size_t namelen;
3454
3455 /* determine namelen, allocate wq and format name */
3456 va_start(args, lock_name);
3457 va_copy(args1, args);
3458 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3459
3460 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3461 if (!wq)
3462 goto err;
3463
3464 vsnprintf(wq->name, namelen, fmt, args1);
3465 va_end(args);
3466 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003467
Tejun Heof3421792010-07-02 10:03:51 +02003468 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02003469 * Workqueues which may be used during memory reclaim should
3470 * have a rescuer to guarantee forward progress.
3471 */
3472 if (flags & WQ_MEM_RECLAIM)
3473 flags |= WQ_RESCUER;
3474
Tejun Heod320c032010-06-29 10:07:14 +02003475 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003476 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003477
Tejun Heob196be82012-01-10 15:11:35 -08003478 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003479 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003480 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003481 mutex_init(&wq->flush_mutex);
Tejun Heo112202d2013-02-13 19:29:12 -08003482 atomic_set(&wq->nr_pwqs_to_flush, 0);
Tejun Heo30cdf242013-03-12 11:29:57 -07003483 INIT_LIST_HEAD(&wq->pwqs);
Tejun Heo73f53c42010-06-29 10:07:11 +02003484 INIT_LIST_HEAD(&wq->flusher_queue);
3485 INIT_LIST_HEAD(&wq->flusher_overflow);
Tejun Heo493a1722013-03-12 11:29:59 -07003486 INIT_LIST_HEAD(&wq->maydays);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003487
Johannes Bergeb13ba82008-01-16 09:51:58 +01003488 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003489 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003490
Tejun Heo30cdf242013-03-12 11:29:57 -07003491 if (alloc_and_link_pwqs(wq) < 0)
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003492 goto err;
3493
Tejun Heo76af4d92013-03-12 11:30:00 -07003494 local_irq_disable();
Tejun Heo49e3cf42013-03-12 11:29:58 -07003495 for_each_pwq(pwq, wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08003496 BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK);
Tejun Heo112202d2013-02-13 19:29:12 -08003497 pwq->wq = wq;
3498 pwq->flush_color = -1;
3499 pwq->max_active = max_active;
3500 INIT_LIST_HEAD(&pwq->delayed_works);
Tejun Heo493a1722013-03-12 11:29:59 -07003501 INIT_LIST_HEAD(&pwq->mayday_node);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003502 }
Tejun Heo76af4d92013-03-12 11:30:00 -07003503 local_irq_enable();
Oleg Nesterov3af244332007-05-09 02:34:09 -07003504
Tejun Heoe22bee72010-06-29 10:07:14 +02003505 if (flags & WQ_RESCUER) {
3506 struct worker *rescuer;
3507
Tejun Heoe22bee72010-06-29 10:07:14 +02003508 wq->rescuer = rescuer = alloc_worker();
3509 if (!rescuer)
3510 goto err;
3511
Tejun Heo111c2252013-01-17 17:16:24 -08003512 rescuer->rescue_wq = wq;
3513 rescuer->task = kthread_create(rescuer_thread, rescuer, "%s",
Tejun Heob196be82012-01-10 15:11:35 -08003514 wq->name);
Tejun Heoe22bee72010-06-29 10:07:14 +02003515 if (IS_ERR(rescuer->task))
3516 goto err;
3517
Tejun Heoe22bee72010-06-29 10:07:14 +02003518 rescuer->task->flags |= PF_THREAD_BOUND;
3519 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003520 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003521
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003522 /*
3523 * workqueue_lock protects global freeze state and workqueues
3524 * list. Grab it, set max_active accordingly and add the new
3525 * workqueue to workqueues list.
3526 */
Tejun Heoe98d5b12013-03-12 11:29:57 -07003527 spin_lock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003528
Tejun Heo58a69cb2011-02-16 09:25:31 +01003529 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heo49e3cf42013-03-12 11:29:58 -07003530 for_each_pwq(pwq, wq)
3531 pwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003532
Tejun Heo15376632010-06-29 10:07:11 +02003533 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003534
Tejun Heoe98d5b12013-03-12 11:29:57 -07003535 spin_unlock_irq(&workqueue_lock);
Tejun Heo15376632010-06-29 10:07:11 +02003536
Oleg Nesterov3af244332007-05-09 02:34:09 -07003537 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02003538err:
3539 if (wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08003540 free_pwqs(wq);
Tejun Heoe22bee72010-06-29 10:07:14 +02003541 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003542 kfree(wq);
3543 }
3544 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003545}
Tejun Heod320c032010-06-29 10:07:14 +02003546EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003547
3548/**
3549 * destroy_workqueue - safely terminate a workqueue
3550 * @wq: target workqueue
3551 *
3552 * Safely destroy a workqueue. All work currently pending will be done first.
3553 */
3554void destroy_workqueue(struct workqueue_struct *wq)
3555{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003556 struct pool_workqueue *pwq;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003557
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003558 /* drain it before proceeding with destruction */
3559 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003560
Tejun Heo76af4d92013-03-12 11:30:00 -07003561 spin_lock_irq(&workqueue_lock);
3562
Tejun Heo6183c002013-03-12 11:29:57 -07003563 /* sanity checks */
Tejun Heo49e3cf42013-03-12 11:29:58 -07003564 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07003565 int i;
3566
Tejun Heo76af4d92013-03-12 11:30:00 -07003567 for (i = 0; i < WORK_NR_COLORS; i++) {
3568 if (WARN_ON(pwq->nr_in_flight[i])) {
3569 spin_unlock_irq(&workqueue_lock);
Tejun Heo6183c002013-03-12 11:29:57 -07003570 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07003571 }
3572 }
3573
Tejun Heo6183c002013-03-12 11:29:57 -07003574 if (WARN_ON(pwq->nr_active) ||
Tejun Heo76af4d92013-03-12 11:30:00 -07003575 WARN_ON(!list_empty(&pwq->delayed_works))) {
3576 spin_unlock_irq(&workqueue_lock);
Tejun Heo6183c002013-03-12 11:29:57 -07003577 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07003578 }
Tejun Heo6183c002013-03-12 11:29:57 -07003579 }
3580
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003581 /*
3582 * wq list is used to freeze wq, remove from list after
3583 * flushing is complete in case freeze races us.
3584 */
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07003585 list_del(&wq->list);
Tejun Heo76af4d92013-03-12 11:30:00 -07003586
Tejun Heoe98d5b12013-03-12 11:29:57 -07003587 spin_unlock_irq(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003588
Tejun Heoe22bee72010-06-29 10:07:14 +02003589 if (wq->flags & WQ_RESCUER) {
3590 kthread_stop(wq->rescuer->task);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003591 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003592 }
3593
Tejun Heo29c91e92013-03-12 11:30:03 -07003594 /*
3595 * We're the sole accessor of @wq at this point. Directly access
3596 * the first pwq and put its pool.
3597 */
3598 if (wq->flags & WQ_UNBOUND) {
3599 pwq = list_first_entry(&wq->pwqs, struct pool_workqueue,
3600 pwqs_node);
3601 put_unbound_pool(pwq->pool);
3602 }
Tejun Heo112202d2013-02-13 19:29:12 -08003603 free_pwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003604 kfree(wq);
3605}
3606EXPORT_SYMBOL_GPL(destroy_workqueue);
3607
Tejun Heodcd989c2010-06-29 10:07:14 +02003608/**
Tejun Heo112202d2013-02-13 19:29:12 -08003609 * pwq_set_max_active - adjust max_active of a pwq
3610 * @pwq: target pool_workqueue
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003611 * @max_active: new max_active value.
3612 *
Tejun Heo112202d2013-02-13 19:29:12 -08003613 * Set @pwq->max_active to @max_active and activate delayed works if
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003614 * increased.
3615 *
3616 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08003617 * spin_lock_irq(pool->lock).
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003618 */
Tejun Heo112202d2013-02-13 19:29:12 -08003619static void pwq_set_max_active(struct pool_workqueue *pwq, int max_active)
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003620{
Tejun Heo112202d2013-02-13 19:29:12 -08003621 pwq->max_active = max_active;
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003622
Tejun Heo112202d2013-02-13 19:29:12 -08003623 while (!list_empty(&pwq->delayed_works) &&
3624 pwq->nr_active < pwq->max_active)
3625 pwq_activate_first_delayed(pwq);
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003626}
3627
3628/**
Tejun Heodcd989c2010-06-29 10:07:14 +02003629 * workqueue_set_max_active - adjust max_active of a workqueue
3630 * @wq: target workqueue
3631 * @max_active: new max_active value.
3632 *
3633 * Set max_active of @wq to @max_active.
3634 *
3635 * CONTEXT:
3636 * Don't call from IRQ context.
3637 */
3638void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3639{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003640 struct pool_workqueue *pwq;
Tejun Heodcd989c2010-06-29 10:07:14 +02003641
Tejun Heof3421792010-07-02 10:03:51 +02003642 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003643
Tejun Heoe98d5b12013-03-12 11:29:57 -07003644 spin_lock_irq(&workqueue_lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02003645
3646 wq->saved_max_active = max_active;
3647
Tejun Heo49e3cf42013-03-12 11:29:58 -07003648 for_each_pwq(pwq, wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08003649 struct worker_pool *pool = pwq->pool;
Tejun Heodcd989c2010-06-29 10:07:14 +02003650
Tejun Heoe98d5b12013-03-12 11:29:57 -07003651 spin_lock(&pool->lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02003652
Tejun Heo58a69cb2011-02-16 09:25:31 +01003653 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heo35b6bb62013-01-24 11:01:33 -08003654 !(pool->flags & POOL_FREEZING))
Tejun Heo112202d2013-02-13 19:29:12 -08003655 pwq_set_max_active(pwq, max_active);
Tejun Heodcd989c2010-06-29 10:07:14 +02003656
Tejun Heoe98d5b12013-03-12 11:29:57 -07003657 spin_unlock(&pool->lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02003658 }
3659
Tejun Heoe98d5b12013-03-12 11:29:57 -07003660 spin_unlock_irq(&workqueue_lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02003661}
3662EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3663
3664/**
3665 * workqueue_congested - test whether a workqueue is congested
3666 * @cpu: CPU in question
3667 * @wq: target workqueue
3668 *
3669 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3670 * no synchronization around this function and the test result is
3671 * unreliable and only useful as advisory hints or for debugging.
3672 *
3673 * RETURNS:
3674 * %true if congested, %false otherwise.
3675 */
Tejun Heod84ff052013-03-12 11:29:59 -07003676bool workqueue_congested(int cpu, struct workqueue_struct *wq)
Tejun Heodcd989c2010-06-29 10:07:14 +02003677{
Tejun Heo7fb98ea2013-03-12 11:30:00 -07003678 struct pool_workqueue *pwq;
Tejun Heo76af4d92013-03-12 11:30:00 -07003679 bool ret;
3680
3681 preempt_disable();
Tejun Heo7fb98ea2013-03-12 11:30:00 -07003682
3683 if (!(wq->flags & WQ_UNBOUND))
3684 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
3685 else
3686 pwq = first_pwq(wq);
Tejun Heodcd989c2010-06-29 10:07:14 +02003687
Tejun Heo76af4d92013-03-12 11:30:00 -07003688 ret = !list_empty(&pwq->delayed_works);
3689 preempt_enable();
3690
3691 return ret;
Tejun Heodcd989c2010-06-29 10:07:14 +02003692}
3693EXPORT_SYMBOL_GPL(workqueue_congested);
3694
3695/**
Tejun Heodcd989c2010-06-29 10:07:14 +02003696 * work_busy - test whether a work is currently pending or running
3697 * @work: the work to be tested
3698 *
3699 * Test whether @work is currently pending or running. There is no
3700 * synchronization around this function and the test result is
3701 * unreliable and only useful as advisory hints or for debugging.
Tejun Heodcd989c2010-06-29 10:07:14 +02003702 *
3703 * RETURNS:
3704 * OR'd bitmask of WORK_BUSY_* bits.
3705 */
3706unsigned int work_busy(struct work_struct *work)
3707{
Tejun Heofa1b54e2013-03-12 11:30:00 -07003708 struct worker_pool *pool;
Tejun Heodcd989c2010-06-29 10:07:14 +02003709 unsigned long flags;
3710 unsigned int ret = 0;
3711
Tejun Heodcd989c2010-06-29 10:07:14 +02003712 if (work_pending(work))
3713 ret |= WORK_BUSY_PENDING;
Tejun Heodcd989c2010-06-29 10:07:14 +02003714
Tejun Heofa1b54e2013-03-12 11:30:00 -07003715 local_irq_save(flags);
3716 pool = get_work_pool(work);
Lai Jiangshan038366c2013-02-06 18:04:53 -08003717 if (pool) {
Tejun Heofa1b54e2013-03-12 11:30:00 -07003718 spin_lock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08003719 if (find_worker_executing_work(pool, work))
3720 ret |= WORK_BUSY_RUNNING;
Tejun Heofa1b54e2013-03-12 11:30:00 -07003721 spin_unlock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08003722 }
Tejun Heofa1b54e2013-03-12 11:30:00 -07003723 local_irq_restore(flags);
Tejun Heodcd989c2010-06-29 10:07:14 +02003724
3725 return ret;
3726}
3727EXPORT_SYMBOL_GPL(work_busy);
3728
Tejun Heodb7bccf2010-06-29 10:07:12 +02003729/*
3730 * CPU hotplug.
3731 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003732 * There are two challenges in supporting CPU hotplug. Firstly, there
Tejun Heo112202d2013-02-13 19:29:12 -08003733 * are a lot of assumptions on strong associations among work, pwq and
Tejun Heo706026c2013-01-24 11:01:34 -08003734 * pool which make migrating pending and scheduled works very
Tejun Heoe22bee72010-06-29 10:07:14 +02003735 * difficult to implement without impacting hot paths. Secondly,
Tejun Heo94cf58b2013-01-24 11:01:33 -08003736 * worker pools serve mix of short, long and very long running works making
Tejun Heoe22bee72010-06-29 10:07:14 +02003737 * blocked draining impractical.
3738 *
Tejun Heo24647572013-01-24 11:01:33 -08003739 * This is solved by allowing the pools to be disassociated from the CPU
Tejun Heo628c78e2012-07-17 12:39:27 -07003740 * running as an unbound one and allowing it to be reattached later if the
3741 * cpu comes back online.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003742 */
3743
Tejun Heo706026c2013-01-24 11:01:34 -08003744static void wq_unbind_fn(struct work_struct *work)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003745{
Tejun Heo38db41d2013-01-24 11:01:34 -08003746 int cpu = smp_processor_id();
Tejun Heo4ce62e92012-07-13 22:16:44 -07003747 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003748 struct worker *worker;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003749 int i;
3750
Tejun Heo38db41d2013-01-24 11:01:34 -08003751 for_each_std_worker_pool(pool, cpu) {
Tejun Heo6183c002013-03-12 11:29:57 -07003752 WARN_ON_ONCE(cpu != smp_processor_id());
Tejun Heo94cf58b2013-01-24 11:01:33 -08003753
3754 mutex_lock(&pool->assoc_mutex);
3755 spin_lock_irq(&pool->lock);
3756
3757 /*
3758 * We've claimed all manager positions. Make all workers
3759 * unbound and set DISASSOCIATED. Before this, all workers
3760 * except for the ones which are still executing works from
3761 * before the last CPU down must be on the cpu. After
3762 * this, they may become diasporas.
3763 */
Tejun Heo4ce62e92012-07-13 22:16:44 -07003764 list_for_each_entry(worker, &pool->idle_list, entry)
Tejun Heo403c8212012-07-17 12:39:27 -07003765 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003766
Sasha Levinb67bfe02013-02-27 17:06:00 -08003767 for_each_busy_worker(worker, i, pool)
Tejun Heoc9e7cf22013-01-24 11:01:33 -08003768 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003769
Tejun Heo24647572013-01-24 11:01:33 -08003770 pool->flags |= POOL_DISASSOCIATED;
Tejun Heof2d5a0e2012-07-17 12:39:26 -07003771
Tejun Heo94cf58b2013-01-24 11:01:33 -08003772 spin_unlock_irq(&pool->lock);
3773 mutex_unlock(&pool->assoc_mutex);
3774 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003775
3776 /*
Tejun Heo628c78e2012-07-17 12:39:27 -07003777 * Call schedule() so that we cross rq->lock and thus can guarantee
3778 * sched callbacks see the %WORKER_UNBOUND flag. This is necessary
3779 * as scheduler callbacks may be invoked from other cpus.
3780 */
3781 schedule();
3782
3783 /*
3784 * Sched callbacks are disabled now. Zap nr_running. After this,
3785 * nr_running stays zero and need_more_worker() and keep_working()
Tejun Heo38db41d2013-01-24 11:01:34 -08003786 * are always true as long as the worklist is not empty. Pools on
3787 * @cpu now behave as unbound (in terms of concurrency management)
3788 * pools which are served by workers tied to the CPU.
Tejun Heo628c78e2012-07-17 12:39:27 -07003789 *
3790 * On return from this function, the current worker would trigger
3791 * unbound chain execution of pending work items if other workers
3792 * didn't already.
Tejun Heoe22bee72010-06-29 10:07:14 +02003793 */
Tejun Heo38db41d2013-01-24 11:01:34 -08003794 for_each_std_worker_pool(pool, cpu)
Tejun Heoe19e3972013-01-24 11:39:44 -08003795 atomic_set(&pool->nr_running, 0);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003796}
3797
Tejun Heo8db25e72012-07-17 12:39:28 -07003798/*
3799 * Workqueues should be brought up before normal priority CPU notifiers.
3800 * This will be registered high priority CPU notifier.
3801 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07003802static int __cpuinit workqueue_cpu_up_callback(struct notifier_block *nfb,
Tejun Heo8db25e72012-07-17 12:39:28 -07003803 unsigned long action,
3804 void *hcpu)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003805{
Tejun Heod84ff052013-03-12 11:29:59 -07003806 int cpu = (unsigned long)hcpu;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003807 struct worker_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808
Tejun Heo8db25e72012-07-17 12:39:28 -07003809 switch (action & ~CPU_TASKS_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 case CPU_UP_PREPARE:
Tejun Heo38db41d2013-01-24 11:01:34 -08003811 for_each_std_worker_pool(pool, cpu) {
Tejun Heo3ce63372012-07-17 12:39:27 -07003812 struct worker *worker;
3813
3814 if (pool->nr_workers)
3815 continue;
3816
3817 worker = create_worker(pool);
3818 if (!worker)
3819 return NOTIFY_BAD;
3820
Tejun Heod565ed62013-01-24 11:01:33 -08003821 spin_lock_irq(&pool->lock);
Tejun Heo3ce63372012-07-17 12:39:27 -07003822 start_worker(worker);
Tejun Heod565ed62013-01-24 11:01:33 -08003823 spin_unlock_irq(&pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003825 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003826
Tejun Heo65758202012-07-17 12:39:26 -07003827 case CPU_DOWN_FAILED:
3828 case CPU_ONLINE:
Tejun Heo38db41d2013-01-24 11:01:34 -08003829 for_each_std_worker_pool(pool, cpu) {
Tejun Heo94cf58b2013-01-24 11:01:33 -08003830 mutex_lock(&pool->assoc_mutex);
3831 spin_lock_irq(&pool->lock);
3832
Tejun Heo24647572013-01-24 11:01:33 -08003833 pool->flags &= ~POOL_DISASSOCIATED;
Tejun Heo94cf58b2013-01-24 11:01:33 -08003834 rebind_workers(pool);
3835
3836 spin_unlock_irq(&pool->lock);
3837 mutex_unlock(&pool->assoc_mutex);
3838 }
Tejun Heo8db25e72012-07-17 12:39:28 -07003839 break;
Tejun Heo65758202012-07-17 12:39:26 -07003840 }
3841 return NOTIFY_OK;
3842}
3843
3844/*
3845 * Workqueues should be brought down after normal priority CPU notifiers.
3846 * This will be registered as low priority CPU notifier.
3847 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07003848static int __cpuinit workqueue_cpu_down_callback(struct notifier_block *nfb,
Tejun Heo65758202012-07-17 12:39:26 -07003849 unsigned long action,
3850 void *hcpu)
3851{
Tejun Heod84ff052013-03-12 11:29:59 -07003852 int cpu = (unsigned long)hcpu;
Tejun Heo8db25e72012-07-17 12:39:28 -07003853 struct work_struct unbind_work;
3854
Tejun Heo65758202012-07-17 12:39:26 -07003855 switch (action & ~CPU_TASKS_FROZEN) {
3856 case CPU_DOWN_PREPARE:
Tejun Heo8db25e72012-07-17 12:39:28 -07003857 /* unbinding should happen on the local CPU */
Tejun Heo706026c2013-01-24 11:01:34 -08003858 INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn);
Joonsoo Kim7635d2f2012-08-15 23:25:41 +09003859 queue_work_on(cpu, system_highpri_wq, &unbind_work);
Tejun Heo8db25e72012-07-17 12:39:28 -07003860 flush_work(&unbind_work);
3861 break;
Tejun Heo65758202012-07-17 12:39:26 -07003862 }
3863 return NOTIFY_OK;
3864}
3865
Rusty Russell2d3854a2008-11-05 13:39:10 +11003866#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003867
Rusty Russell2d3854a2008-11-05 13:39:10 +11003868struct work_for_cpu {
Tejun Heoed48ece2012-09-18 12:48:43 -07003869 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003870 long (*fn)(void *);
3871 void *arg;
3872 long ret;
3873};
3874
Tejun Heoed48ece2012-09-18 12:48:43 -07003875static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003876{
Tejun Heoed48ece2012-09-18 12:48:43 -07003877 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
3878
Rusty Russell2d3854a2008-11-05 13:39:10 +11003879 wfc->ret = wfc->fn(wfc->arg);
3880}
3881
3882/**
3883 * work_on_cpu - run a function in user context on a particular cpu
3884 * @cpu: the cpu to run on
3885 * @fn: the function to run
3886 * @arg: the function arg
3887 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003888 * This will return the value @fn returns.
3889 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06003890 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003891 */
Tejun Heod84ff052013-03-12 11:29:59 -07003892long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003893{
Tejun Heoed48ece2012-09-18 12:48:43 -07003894 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003895
Tejun Heoed48ece2012-09-18 12:48:43 -07003896 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
3897 schedule_work_on(cpu, &wfc.work);
3898 flush_work(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003899 return wfc.ret;
3900}
3901EXPORT_SYMBOL_GPL(work_on_cpu);
3902#endif /* CONFIG_SMP */
3903
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003904#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303905
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003906/**
3907 * freeze_workqueues_begin - begin freezing workqueues
3908 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003909 * Start freezing workqueues. After this function returns, all freezable
3910 * workqueues will queue new works to their frozen_works list instead of
Tejun Heo706026c2013-01-24 11:01:34 -08003911 * pool->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003912 *
3913 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08003914 * Grabs and releases workqueue_lock and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003915 */
3916void freeze_workqueues_begin(void)
3917{
Tejun Heo17116962013-03-12 11:29:58 -07003918 struct worker_pool *pool;
Tejun Heo24b8a842013-03-12 11:29:58 -07003919 struct workqueue_struct *wq;
3920 struct pool_workqueue *pwq;
Tejun Heo17116962013-03-12 11:29:58 -07003921 int id;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003922
Tejun Heoe98d5b12013-03-12 11:29:57 -07003923 spin_lock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003924
Tejun Heo6183c002013-03-12 11:29:57 -07003925 WARN_ON_ONCE(workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003926 workqueue_freezing = true;
3927
Tejun Heo24b8a842013-03-12 11:29:58 -07003928 /* set FREEZING */
Tejun Heo17116962013-03-12 11:29:58 -07003929 for_each_pool(pool, id) {
Tejun Heo17116962013-03-12 11:29:58 -07003930 spin_lock(&pool->lock);
Tejun Heo17116962013-03-12 11:29:58 -07003931 WARN_ON_ONCE(pool->flags & POOL_FREEZING);
3932 pool->flags |= POOL_FREEZING;
Tejun Heo17116962013-03-12 11:29:58 -07003933 spin_unlock(&pool->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003934 }
3935
Tejun Heo24b8a842013-03-12 11:29:58 -07003936 /* suppress further executions by setting max_active to zero */
3937 list_for_each_entry(wq, &workqueues, list) {
3938 if (!(wq->flags & WQ_FREEZABLE))
3939 continue;
3940
3941 for_each_pwq(pwq, wq) {
3942 spin_lock(&pwq->pool->lock);
3943 pwq->max_active = 0;
3944 spin_unlock(&pwq->pool->lock);
3945 }
3946 }
3947
Tejun Heoe98d5b12013-03-12 11:29:57 -07003948 spin_unlock_irq(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003950
3951/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003952 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003953 *
3954 * Check whether freezing is complete. This function must be called
3955 * between freeze_workqueues_begin() and thaw_workqueues().
3956 *
3957 * CONTEXT:
3958 * Grabs and releases workqueue_lock.
3959 *
3960 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01003961 * %true if some freezable workqueues are still busy. %false if freezing
3962 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003963 */
3964bool freeze_workqueues_busy(void)
3965{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003966 bool busy = false;
Tejun Heo24b8a842013-03-12 11:29:58 -07003967 struct workqueue_struct *wq;
3968 struct pool_workqueue *pwq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003969
Tejun Heoe98d5b12013-03-12 11:29:57 -07003970 spin_lock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003971
Tejun Heo6183c002013-03-12 11:29:57 -07003972 WARN_ON_ONCE(!workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003973
Tejun Heo24b8a842013-03-12 11:29:58 -07003974 list_for_each_entry(wq, &workqueues, list) {
3975 if (!(wq->flags & WQ_FREEZABLE))
3976 continue;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003977 /*
3978 * nr_active is monotonically decreasing. It's safe
3979 * to peek without lock.
3980 */
Tejun Heo24b8a842013-03-12 11:29:58 -07003981 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07003982 WARN_ON_ONCE(pwq->nr_active < 0);
Tejun Heo112202d2013-02-13 19:29:12 -08003983 if (pwq->nr_active) {
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003984 busy = true;
3985 goto out_unlock;
3986 }
3987 }
3988 }
3989out_unlock:
Tejun Heoe98d5b12013-03-12 11:29:57 -07003990 spin_unlock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003991 return busy;
3992}
3993
3994/**
3995 * thaw_workqueues - thaw workqueues
3996 *
3997 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo706026c2013-01-24 11:01:34 -08003998 * frozen works are transferred to their respective pool worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003999 *
4000 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08004001 * Grabs and releases workqueue_lock and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004002 */
4003void thaw_workqueues(void)
4004{
Tejun Heo24b8a842013-03-12 11:29:58 -07004005 struct workqueue_struct *wq;
4006 struct pool_workqueue *pwq;
4007 struct worker_pool *pool;
4008 int id;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004009
Tejun Heoe98d5b12013-03-12 11:29:57 -07004010 spin_lock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004011
4012 if (!workqueue_freezing)
4013 goto out_unlock;
4014
Tejun Heo24b8a842013-03-12 11:29:58 -07004015 /* clear FREEZING */
4016 for_each_pool(pool, id) {
4017 spin_lock(&pool->lock);
4018 WARN_ON_ONCE(!(pool->flags & POOL_FREEZING));
4019 pool->flags &= ~POOL_FREEZING;
4020 spin_unlock(&pool->lock);
4021 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02004022
Tejun Heo24b8a842013-03-12 11:29:58 -07004023 /* restore max_active and repopulate worklist */
4024 list_for_each_entry(wq, &workqueues, list) {
4025 if (!(wq->flags & WQ_FREEZABLE))
4026 continue;
Tejun Heod565ed62013-01-24 11:01:33 -08004027
Tejun Heo24b8a842013-03-12 11:29:58 -07004028 for_each_pwq(pwq, wq) {
4029 spin_lock(&pwq->pool->lock);
4030 pwq_set_max_active(pwq, wq->saved_max_active);
4031 spin_unlock(&pwq->pool->lock);
Tejun Heod565ed62013-01-24 11:01:33 -08004032 }
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004033 }
4034
Tejun Heo24b8a842013-03-12 11:29:58 -07004035 /* kick workers */
4036 for_each_pool(pool, id) {
4037 spin_lock(&pool->lock);
4038 wake_up_worker(pool);
4039 spin_unlock(&pool->lock);
4040 }
4041
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004042 workqueue_freezing = false;
4043out_unlock:
Tejun Heoe98d5b12013-03-12 11:29:57 -07004044 spin_unlock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004045}
4046#endif /* CONFIG_FREEZER */
4047
Suresh Siddha6ee05782010-07-30 14:57:37 -07004048static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049{
Tejun Heo7a4e3442013-03-12 11:30:00 -07004050 int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL };
4051 int i, cpu;
Tejun Heoc34056a2010-06-29 10:07:11 +02004052
Tejun Heo7c3eed52013-01-24 11:01:33 -08004053 /* make sure we have enough bits for OFFQ pool ID */
4054 BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) <
Lai Jiangshan6be19582013-02-06 18:04:53 -08004055 WORK_CPU_END * NR_STD_WORKER_POOLS);
Tejun Heob5490072012-08-03 10:30:46 -07004056
Tejun Heoe904e6c2013-03-12 11:29:57 -07004057 WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long));
4058
4059 pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC);
4060
Tejun Heo65758202012-07-17 12:39:26 -07004061 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
Lai Jiangshana5b4e572012-09-18 09:59:23 -07004062 hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02004063
Tejun Heo706026c2013-01-24 11:01:34 -08004064 /* initialize CPU pools */
Tejun Heo29c91e92013-03-12 11:30:03 -07004065 for_each_possible_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07004066 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02004067
Tejun Heo7a4e3442013-03-12 11:30:00 -07004068 i = 0;
Tejun Heo38db41d2013-01-24 11:01:34 -08004069 for_each_std_worker_pool(pool, cpu) {
Tejun Heo7a4e3442013-03-12 11:30:00 -07004070 BUG_ON(init_worker_pool(pool));
Tejun Heoec22ca52013-01-24 11:01:33 -08004071 pool->cpu = cpu;
Tejun Heo29c91e92013-03-12 11:30:03 -07004072 cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu));
Tejun Heo7a4e3442013-03-12 11:30:00 -07004073 pool->attrs->nice = std_nice[i++];
4074
Tejun Heo9daf9e62013-01-24 11:01:33 -08004075 /* alloc pool ID */
4076 BUG_ON(worker_pool_assign_id(pool));
Tejun Heo4ce62e92012-07-13 22:16:44 -07004077 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02004078 }
4079
Tejun Heoe22bee72010-06-29 10:07:14 +02004080 /* create the initial worker */
Tejun Heo29c91e92013-03-12 11:30:03 -07004081 for_each_online_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07004082 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02004083
Tejun Heo38db41d2013-01-24 11:01:34 -08004084 for_each_std_worker_pool(pool, cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07004085 struct worker *worker;
4086
Tejun Heo29c91e92013-03-12 11:30:03 -07004087 pool->flags &= ~POOL_DISASSOCIATED;
Tejun Heo24647572013-01-24 11:01:33 -08004088
Tejun Heobc2ae0f2012-07-17 12:39:27 -07004089 worker = create_worker(pool);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004090 BUG_ON(!worker);
Tejun Heod565ed62013-01-24 11:01:33 -08004091 spin_lock_irq(&pool->lock);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004092 start_worker(worker);
Tejun Heod565ed62013-01-24 11:01:33 -08004093 spin_unlock_irq(&pool->lock);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004094 }
Tejun Heoe22bee72010-06-29 10:07:14 +02004095 }
4096
Tejun Heo29c91e92013-03-12 11:30:03 -07004097 /* create default unbound wq attrs */
4098 for (i = 0; i < NR_STD_WORKER_POOLS; i++) {
4099 struct workqueue_attrs *attrs;
4100
4101 BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL)));
4102
4103 attrs->nice = std_nice[i];
4104 cpumask_setall(attrs->cpumask);
4105
4106 unbound_std_wq_attrs[i] = attrs;
4107 }
4108
Tejun Heod320c032010-06-29 10:07:14 +02004109 system_wq = alloc_workqueue("events", 0, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09004110 system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
Tejun Heod320c032010-06-29 10:07:14 +02004111 system_long_wq = alloc_workqueue("events_long", 0, 0);
Tejun Heof3421792010-07-02 10:03:51 +02004112 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
4113 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01004114 system_freezable_wq = alloc_workqueue("events_freezable",
4115 WQ_FREEZABLE, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09004116 BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
Tejun Heoae930e02012-08-20 14:51:23 -07004117 !system_unbound_wq || !system_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07004118 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119}
Suresh Siddha6ee05782010-07-30 14:57:37 -07004120early_initcall(init_workqueues);