blob: 73c5f68065b5726b729da9a7b22520f3cb3e8b45 [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>
Sasha Levin42f85702012-12-17 10:01:23 -050044#include <linux/hashtable.h>
Tejun Heoe22bee72010-06-29 10:07:14 +020045
Tejun Heoea138442013-01-18 14:05:55 -080046#include "workqueue_internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Tejun Heoc8e55f32010-06-29 10:07:12 +020048enum {
Tejun Heobc2ae0f2012-07-17 12:39:27 -070049 /*
Tejun Heo24647572013-01-24 11:01:33 -080050 * worker_pool flags
Tejun Heobc2ae0f2012-07-17 12:39:27 -070051 *
Tejun Heo24647572013-01-24 11:01:33 -080052 * A bound pool is either associated or disassociated with its CPU.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070053 * While associated (!DISASSOCIATED), all workers are bound to the
54 * CPU and none has %WORKER_UNBOUND set and concurrency management
55 * is in effect.
56 *
57 * While DISASSOCIATED, the cpu may be offline and all workers have
58 * %WORKER_UNBOUND set and concurrency management disabled, and may
Tejun Heo24647572013-01-24 11:01:33 -080059 * be executing on any CPU. The pool behaves as an unbound one.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070060 *
61 * Note that DISASSOCIATED can be flipped only while holding
Tejun Heo24647572013-01-24 11:01:33 -080062 * assoc_mutex to avoid changing binding state while
63 * create_worker() is in progress.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070064 */
Tejun Heo11ebea52012-07-12 14:46:37 -070065 POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
Lai Jiangshan552a37e2012-09-10 10:03:33 -070066 POOL_MANAGING_WORKERS = 1 << 1, /* managing workers */
Tejun Heo24647572013-01-24 11:01:33 -080067 POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */
Tejun Heo35b6bb62013-01-24 11:01:33 -080068 POOL_FREEZING = 1 << 3, /* freeze in progress */
Tejun Heodb7bccf2010-06-29 10:07:12 +020069
Tejun Heoc8e55f32010-06-29 10:07:12 +020070 /* worker flags */
71 WORKER_STARTED = 1 << 0, /* started */
72 WORKER_DIE = 1 << 1, /* die die die */
73 WORKER_IDLE = 1 << 2, /* is idle */
Tejun Heoe22bee72010-06-29 10:07:14 +020074 WORKER_PREP = 1 << 3, /* preparing to run works */
Tejun Heofb0e7be2010-06-29 10:07:15 +020075 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
Tejun Heof3421792010-07-02 10:03:51 +020076 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
Tejun Heoe22bee72010-06-29 10:07:14 +020077
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -070078 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_UNBOUND |
Tejun Heo403c8212012-07-17 12:39:27 -070079 WORKER_CPU_INTENSIVE,
Tejun Heodb7bccf2010-06-29 10:07:12 +020080
Tejun Heoe34cdddb2013-01-24 11:01:33 -080081 NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */
Tejun Heo4ce62e92012-07-13 22:16:44 -070082
Tejun Heoc8e55f32010-06-29 10:07:12 +020083 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
Tejun Heodb7bccf2010-06-29 10:07:12 +020084
Tejun Heoe22bee72010-06-29 10:07:14 +020085 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
86 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
87
Tejun Heo3233cdb2011-02-16 18:10:19 +010088 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
89 /* call for help after 10ms
90 (min two ticks) */
Tejun Heoe22bee72010-06-29 10:07:14 +020091 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
92 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heoe22bee72010-06-29 10:07:14 +020093
94 /*
95 * Rescue workers are used only on emergencies and shared by
96 * all cpus. Give -20.
97 */
98 RESCUER_NICE_LEVEL = -20,
Tejun Heo32704762012-07-13 22:16:45 -070099 HIGHPRI_NICE_LEVEL = -20,
Tejun Heoc8e55f32010-06-29 10:07:12 +0200100};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102/*
Tejun Heo4690c4a2010-06-29 10:07:10 +0200103 * Structure fields follow one of the following exclusion rules.
104 *
Tejun Heoe41e7042010-08-24 14:22:47 +0200105 * I: Modifiable by initialization/destruction paths and read-only for
106 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200107 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200108 * P: Preemption protected. Disabling preemption is enough and should
109 * only be modified and accessed from the local cpu.
110 *
Tejun Heod565ed62013-01-24 11:01:33 -0800111 * L: pool->lock protected. Access with pool->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200112 *
Tejun Heod565ed62013-01-24 11:01:33 -0800113 * X: During normal operation, modification requires pool->lock and should
114 * be done only from local cpu. Either disabling preemption on local
115 * cpu or grabbing pool->lock is enough for read access. If
116 * POOL_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200117 *
Tejun Heo73f53c42010-06-29 10:07:11 +0200118 * F: wq->flush_mutex protected.
119 *
Tejun Heo4690c4a2010-06-29 10:07:10 +0200120 * W: workqueue_lock protected.
121 */
122
Tejun Heo2eaebdb2013-01-18 14:05:55 -0800123/* struct worker is defined in workqueue_internal.h */
Tejun Heoc34056a2010-06-29 10:07:11 +0200124
Tejun Heobd7bdd42012-07-12 14:46:37 -0700125struct worker_pool {
Tejun Heod565ed62013-01-24 11:01:33 -0800126 spinlock_t lock; /* the pool lock */
Tejun Heod84ff052013-03-12 11:29:59 -0700127 int cpu; /* I: the associated cpu */
Tejun Heo9daf9e62013-01-24 11:01:33 -0800128 int id; /* I: pool ID */
Tejun Heo11ebea52012-07-12 14:46:37 -0700129 unsigned int flags; /* X: flags */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700130
131 struct list_head worklist; /* L: list of pending works */
132 int nr_workers; /* L: total number of workers */
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700133
134 /* nr_idle includes the ones off idle_list for rebinding */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700135 int nr_idle; /* L: currently idle ones */
136
137 struct list_head idle_list; /* X: list of idle workers */
138 struct timer_list idle_timer; /* L: worker idle timeout */
139 struct timer_list mayday_timer; /* L: SOS timer for workers */
140
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800141 /* workers are chained either in busy_hash or idle_list */
142 DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER);
143 /* L: hash of busy workers */
144
Tejun Heo24647572013-01-24 11:01:33 -0800145 struct mutex assoc_mutex; /* protect POOL_DISASSOCIATED */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700146 struct ida worker_ida; /* L: for worker IDs */
Tejun Heoe19e3972013-01-24 11:39:44 -0800147
148 /*
149 * The current concurrency level. As it's likely to be accessed
150 * from other CPUs during try_to_wake_up(), put it in a separate
151 * cacheline.
152 */
153 atomic_t nr_running ____cacheline_aligned_in_smp;
Tejun Heo8b03ae32010-06-29 10:07:12 +0200154} ____cacheline_aligned_in_smp;
155
156/*
Tejun Heo112202d2013-02-13 19:29:12 -0800157 * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS
158 * of work_struct->data are used for flags and the remaining high bits
159 * point to the pwq; thus, pwqs need to be aligned at two's power of the
160 * number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 */
Tejun Heo112202d2013-02-13 19:29:12 -0800162struct pool_workqueue {
Tejun Heobd7bdd42012-07-12 14:46:37 -0700163 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200164 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200165 int work_color; /* L: current color */
166 int flush_color; /* L: flushing color */
167 int nr_in_flight[WORK_NR_COLORS];
168 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200169 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200170 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200171 struct list_head delayed_works; /* L: delayed works */
Tejun Heo30cdf242013-03-12 11:29:57 -0700172 struct list_head pwqs_node; /* I: node on wq->pwqs */
Tejun Heo493a1722013-03-12 11:29:59 -0700173 struct list_head mayday_node; /* W: node on wq->maydays */
Tejun Heoe904e6c2013-03-12 11:29:57 -0700174} __aligned(1 << WORK_STRUCT_FLAG_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200177 * Structure used to wait for workqueue flush.
178 */
179struct wq_flusher {
180 struct list_head list; /* F: list of flushers */
181 int flush_color; /* F: flush color waiting for */
182 struct completion done; /* flush completion */
183};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Tejun Heo73f53c42010-06-29 10:07:11 +0200185/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 * The externally visible workqueue abstraction is an array of
187 * per-CPU workqueues:
188 */
189struct workqueue_struct {
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200190 unsigned int flags; /* W: WQ_* flags */
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200191 union {
Tejun Heo112202d2013-02-13 19:29:12 -0800192 struct pool_workqueue __percpu *pcpu;
193 struct pool_workqueue *single;
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200194 unsigned long v;
Tejun Heo112202d2013-02-13 19:29:12 -0800195 } pool_wq; /* I: pwq's */
Tejun Heo30cdf242013-03-12 11:29:57 -0700196 struct list_head pwqs; /* I: all pwqs of this wq */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200197 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200198
199 struct mutex flush_mutex; /* protects wq flushing */
200 int work_color; /* F: current work color */
201 int flush_color; /* F: current flush color */
Tejun Heo112202d2013-02-13 19:29:12 -0800202 atomic_t nr_pwqs_to_flush; /* flush in progress */
Tejun Heo73f53c42010-06-29 10:07:11 +0200203 struct wq_flusher *first_flusher; /* F: first flusher */
204 struct list_head flusher_queue; /* F: flush waiters */
205 struct list_head flusher_overflow; /* F: flush overflow list */
206
Tejun Heo493a1722013-03-12 11:29:59 -0700207 struct list_head maydays; /* W: pwqs requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200208 struct worker *rescuer; /* I: rescue worker */
209
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200210 int nr_drainers; /* W: drain in progress */
Tejun Heo112202d2013-02-13 19:29:12 -0800211 int saved_max_active; /* W: saved pwq max_active */
Johannes Berg4e6045f2007-10-18 23:39:55 -0700212#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200213 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700214#endif
Tejun Heob196be82012-01-10 15:11:35 -0800215 char name[]; /* I: workqueue name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216};
217
Tejun Heoe904e6c2013-03-12 11:29:57 -0700218static struct kmem_cache *pwq_cache;
219
Tejun Heod320c032010-06-29 10:07:14 +0200220struct workqueue_struct *system_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200221EXPORT_SYMBOL_GPL(system_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300222struct workqueue_struct *system_highpri_wq __read_mostly;
Joonsoo Kim1aabe902012-08-15 23:25:39 +0900223EXPORT_SYMBOL_GPL(system_highpri_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300224struct workqueue_struct *system_long_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200225EXPORT_SYMBOL_GPL(system_long_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300226struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200227EXPORT_SYMBOL_GPL(system_unbound_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300228struct workqueue_struct *system_freezable_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100229EXPORT_SYMBOL_GPL(system_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200230
Tejun Heo97bd2342010-10-05 10:41:14 +0200231#define CREATE_TRACE_POINTS
232#include <trace/events/workqueue.h>
233
Tejun Heo38db41d2013-01-24 11:01:34 -0800234#define for_each_std_worker_pool(pool, cpu) \
Tejun Heoa60dc392013-01-24 11:01:34 -0800235 for ((pool) = &std_worker_pools(cpu)[0]; \
236 (pool) < &std_worker_pools(cpu)[NR_STD_WORKER_POOLS]; (pool)++)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700237
Sasha Levinb67bfe02013-02-27 17:06:00 -0800238#define for_each_busy_worker(worker, i, pool) \
239 hash_for_each(pool->busy_hash, i, worker, hentry)
Tejun Heodb7bccf2010-06-29 10:07:12 +0200240
Tejun Heo706026c2013-01-24 11:01:34 -0800241static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
242 unsigned int sw)
Tejun Heof3421792010-07-02 10:03:51 +0200243{
244 if (cpu < nr_cpu_ids) {
245 if (sw & 1) {
246 cpu = cpumask_next(cpu, mask);
247 if (cpu < nr_cpu_ids)
248 return cpu;
249 }
250 if (sw & 2)
251 return WORK_CPU_UNBOUND;
252 }
Lai Jiangshan6be19582013-02-06 18:04:53 -0800253 return WORK_CPU_END;
Tejun Heof3421792010-07-02 10:03:51 +0200254}
255
Tejun Heo09884952010-08-01 11:50:12 +0200256/*
257 * CPU iterators
258 *
Tejun Heo706026c2013-01-24 11:01:34 -0800259 * An extra cpu number is defined using an invalid cpu number
Tejun Heo09884952010-08-01 11:50:12 +0200260 * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any
Tejun Heo706026c2013-01-24 11:01:34 -0800261 * specific CPU. The following iterators are similar to for_each_*_cpu()
262 * iterators but also considers the unbound CPU.
Tejun Heo09884952010-08-01 11:50:12 +0200263 *
Tejun Heo706026c2013-01-24 11:01:34 -0800264 * for_each_wq_cpu() : possible CPUs + WORK_CPU_UNBOUND
265 * for_each_online_wq_cpu() : online CPUs + WORK_CPU_UNBOUND
Tejun Heo09884952010-08-01 11:50:12 +0200266 */
Tejun Heo706026c2013-01-24 11:01:34 -0800267#define for_each_wq_cpu(cpu) \
268 for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, 3); \
Lai Jiangshan6be19582013-02-06 18:04:53 -0800269 (cpu) < WORK_CPU_END; \
Tejun Heo706026c2013-01-24 11:01:34 -0800270 (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, 3))
Tejun Heof3421792010-07-02 10:03:51 +0200271
Tejun Heo706026c2013-01-24 11:01:34 -0800272#define for_each_online_wq_cpu(cpu) \
273 for ((cpu) = __next_wq_cpu(-1, cpu_online_mask, 3); \
Lai Jiangshan6be19582013-02-06 18:04:53 -0800274 (cpu) < WORK_CPU_END; \
Tejun Heo706026c2013-01-24 11:01:34 -0800275 (cpu) = __next_wq_cpu((cpu), cpu_online_mask, 3))
Tejun Heof3421792010-07-02 10:03:51 +0200276
Tejun Heo49e3cf42013-03-12 11:29:58 -0700277/**
Tejun Heo17116962013-03-12 11:29:58 -0700278 * for_each_pool - iterate through all worker_pools in the system
279 * @pool: iteration cursor
280 * @id: integer used for iteration
281 */
282#define for_each_pool(pool, id) \
283 idr_for_each_entry(&worker_pool_idr, pool, id)
284
285/**
Tejun Heo49e3cf42013-03-12 11:29:58 -0700286 * for_each_pwq - iterate through all pool_workqueues of the specified workqueue
287 * @pwq: iteration cursor
288 * @wq: the target workqueue
289 */
290#define for_each_pwq(pwq, wq) \
291 list_for_each_entry((pwq), &(wq)->pwqs, pwqs_node)
Tejun Heof3421792010-07-02 10:03:51 +0200292
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900293#ifdef CONFIG_DEBUG_OBJECTS_WORK
294
295static struct debug_obj_descr work_debug_descr;
296
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100297static void *work_debug_hint(void *addr)
298{
299 return ((struct work_struct *) addr)->func;
300}
301
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900302/*
303 * fixup_init is called when:
304 * - an active object is initialized
305 */
306static int work_fixup_init(void *addr, enum debug_obj_state state)
307{
308 struct work_struct *work = addr;
309
310 switch (state) {
311 case ODEBUG_STATE_ACTIVE:
312 cancel_work_sync(work);
313 debug_object_init(work, &work_debug_descr);
314 return 1;
315 default:
316 return 0;
317 }
318}
319
320/*
321 * fixup_activate is called when:
322 * - an active object is activated
323 * - an unknown object is activated (might be a statically initialized object)
324 */
325static int work_fixup_activate(void *addr, enum debug_obj_state state)
326{
327 struct work_struct *work = addr;
328
329 switch (state) {
330
331 case ODEBUG_STATE_NOTAVAILABLE:
332 /*
333 * This is not really a fixup. The work struct was
334 * statically initialized. We just make sure that it
335 * is tracked in the object tracker.
336 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200337 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900338 debug_object_init(work, &work_debug_descr);
339 debug_object_activate(work, &work_debug_descr);
340 return 0;
341 }
342 WARN_ON_ONCE(1);
343 return 0;
344
345 case ODEBUG_STATE_ACTIVE:
346 WARN_ON(1);
347
348 default:
349 return 0;
350 }
351}
352
353/*
354 * fixup_free is called when:
355 * - an active object is freed
356 */
357static int work_fixup_free(void *addr, enum debug_obj_state state)
358{
359 struct work_struct *work = addr;
360
361 switch (state) {
362 case ODEBUG_STATE_ACTIVE:
363 cancel_work_sync(work);
364 debug_object_free(work, &work_debug_descr);
365 return 1;
366 default:
367 return 0;
368 }
369}
370
371static struct debug_obj_descr work_debug_descr = {
372 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100373 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900374 .fixup_init = work_fixup_init,
375 .fixup_activate = work_fixup_activate,
376 .fixup_free = work_fixup_free,
377};
378
379static inline void debug_work_activate(struct work_struct *work)
380{
381 debug_object_activate(work, &work_debug_descr);
382}
383
384static inline void debug_work_deactivate(struct work_struct *work)
385{
386 debug_object_deactivate(work, &work_debug_descr);
387}
388
389void __init_work(struct work_struct *work, int onstack)
390{
391 if (onstack)
392 debug_object_init_on_stack(work, &work_debug_descr);
393 else
394 debug_object_init(work, &work_debug_descr);
395}
396EXPORT_SYMBOL_GPL(__init_work);
397
398void destroy_work_on_stack(struct work_struct *work)
399{
400 debug_object_free(work, &work_debug_descr);
401}
402EXPORT_SYMBOL_GPL(destroy_work_on_stack);
403
404#else
405static inline void debug_work_activate(struct work_struct *work) { }
406static inline void debug_work_deactivate(struct work_struct *work) { }
407#endif
408
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100409/* Serializes the accesses to the list of workqueues. */
410static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411static LIST_HEAD(workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200412static bool workqueue_freezing; /* W: have wqs started freezing? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Oleg Nesterov14441962007-05-23 13:57:57 -0700414/*
Tejun Heoe19e3972013-01-24 11:39:44 -0800415 * The CPU and unbound standard worker pools. The unbound ones have
416 * POOL_DISASSOCIATED set, and their workers have WORKER_UNBOUND set.
Oleg Nesterov14441962007-05-23 13:57:57 -0700417 */
Tejun Heoe19e3972013-01-24 11:39:44 -0800418static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS],
419 cpu_std_worker_pools);
Tejun Heoa60dc392013-01-24 11:01:34 -0800420static struct worker_pool unbound_std_worker_pools[NR_STD_WORKER_POOLS];
Tejun Heof3421792010-07-02 10:03:51 +0200421
Tejun Heo9daf9e62013-01-24 11:01:33 -0800422/* idr of all pools */
423static DEFINE_MUTEX(worker_pool_idr_mutex);
424static DEFINE_IDR(worker_pool_idr);
425
Tejun Heoc34056a2010-06-29 10:07:11 +0200426static int worker_thread(void *__worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Tejun Heoa60dc392013-01-24 11:01:34 -0800428static struct worker_pool *std_worker_pools(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Tejun Heof3421792010-07-02 10:03:51 +0200430 if (cpu != WORK_CPU_UNBOUND)
Tejun Heoa60dc392013-01-24 11:01:34 -0800431 return per_cpu(cpu_std_worker_pools, cpu);
Tejun Heof3421792010-07-02 10:03:51 +0200432 else
Tejun Heoa60dc392013-01-24 11:01:34 -0800433 return unbound_std_worker_pools;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
Tejun Heo4e8f0a62013-01-24 11:01:34 -0800436static int std_worker_pool_pri(struct worker_pool *pool)
437{
Tejun Heoa60dc392013-01-24 11:01:34 -0800438 return pool - std_worker_pools(pool->cpu);
Tejun Heo4e8f0a62013-01-24 11:01:34 -0800439}
440
Tejun Heo9daf9e62013-01-24 11:01:33 -0800441/* allocate ID and assign it to @pool */
442static int worker_pool_assign_id(struct worker_pool *pool)
443{
444 int ret;
445
446 mutex_lock(&worker_pool_idr_mutex);
447 idr_pre_get(&worker_pool_idr, GFP_KERNEL);
448 ret = idr_get_new(&worker_pool_idr, pool, &pool->id);
449 mutex_unlock(&worker_pool_idr_mutex);
450
451 return ret;
452}
453
Tejun Heo7c3eed52013-01-24 11:01:33 -0800454/*
455 * Lookup worker_pool by id. The idr currently is built during boot and
456 * never modified. Don't worry about locking for now.
457 */
458static struct worker_pool *worker_pool_by_id(int pool_id)
459{
460 return idr_find(&worker_pool_idr, pool_id);
461}
462
Tejun Heod565ed62013-01-24 11:01:33 -0800463static struct worker_pool *get_std_worker_pool(int cpu, bool highpri)
464{
Tejun Heoa60dc392013-01-24 11:01:34 -0800465 struct worker_pool *pools = std_worker_pools(cpu);
Tejun Heod565ed62013-01-24 11:01:33 -0800466
Tejun Heoa60dc392013-01-24 11:01:34 -0800467 return &pools[highpri];
Tejun Heod565ed62013-01-24 11:01:33 -0800468}
469
Tejun Heod84ff052013-03-12 11:29:59 -0700470static struct pool_workqueue *get_pwq(int cpu, struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700471{
Tejun Heof3421792010-07-02 10:03:51 +0200472 if (!(wq->flags & WQ_UNBOUND)) {
Lai Jiangshane06ffa12012-03-09 18:03:20 +0800473 if (likely(cpu < nr_cpu_ids))
Tejun Heo112202d2013-02-13 19:29:12 -0800474 return per_cpu_ptr(wq->pool_wq.pcpu, cpu);
Tejun Heof3421792010-07-02 10:03:51 +0200475 } else if (likely(cpu == WORK_CPU_UNBOUND))
Tejun Heo112202d2013-02-13 19:29:12 -0800476 return wq->pool_wq.single;
Tejun Heof3421792010-07-02 10:03:51 +0200477 return NULL;
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700478}
479
Tejun Heo73f53c42010-06-29 10:07:11 +0200480static unsigned int work_color_to_flags(int color)
481{
482 return color << WORK_STRUCT_COLOR_SHIFT;
483}
484
485static int get_work_color(struct work_struct *work)
486{
487 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
488 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
489}
490
491static int work_next_color(int color)
492{
493 return (color + 1) % WORK_NR_COLORS;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700494}
495
David Howells4594bf12006-12-07 11:33:26 +0000496/*
Tejun Heo112202d2013-02-13 19:29:12 -0800497 * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data
498 * contain the pointer to the queued pwq. Once execution starts, the flag
Tejun Heo7c3eed52013-01-24 11:01:33 -0800499 * is cleared and the high bits contain OFFQ flags and pool ID.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200500 *
Tejun Heo112202d2013-02-13 19:29:12 -0800501 * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling()
502 * and clear_work_data() can be used to set the pwq, pool or clear
Tejun Heobbb68df2012-08-03 10:30:46 -0700503 * work->data. These functions should only be called while the work is
504 * owned - ie. while the PENDING bit is set.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200505 *
Tejun Heo112202d2013-02-13 19:29:12 -0800506 * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq
Tejun Heo7c3eed52013-01-24 11:01:33 -0800507 * corresponding to a work. Pool is available once the work has been
Tejun Heo112202d2013-02-13 19:29:12 -0800508 * queued anywhere after initialization until it is sync canceled. pwq is
Tejun Heo7c3eed52013-01-24 11:01:33 -0800509 * available only while the work item is queued.
Tejun Heobbb68df2012-08-03 10:30:46 -0700510 *
511 * %WORK_OFFQ_CANCELING is used to mark a work item which is being
512 * canceled. While being canceled, a work item may have its PENDING set
513 * but stay off timer and worklist for arbitrarily long and nobody should
514 * try to steal the PENDING bit.
David Howells4594bf12006-12-07 11:33:26 +0000515 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200516static inline void set_work_data(struct work_struct *work, unsigned long data,
517 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000518{
Tejun Heo6183c002013-03-12 11:29:57 -0700519 WARN_ON_ONCE(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200520 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000521}
David Howells365970a2006-11-22 14:54:49 +0000522
Tejun Heo112202d2013-02-13 19:29:12 -0800523static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq,
Tejun Heo7a22ad72010-06-29 10:07:13 +0200524 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200525{
Tejun Heo112202d2013-02-13 19:29:12 -0800526 set_work_data(work, (unsigned long)pwq,
527 WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200528}
529
Lai Jiangshan4468a002013-02-06 18:04:53 -0800530static void set_work_pool_and_keep_pending(struct work_struct *work,
531 int pool_id)
532{
533 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT,
534 WORK_STRUCT_PENDING);
535}
536
Tejun Heo7c3eed52013-01-24 11:01:33 -0800537static void set_work_pool_and_clear_pending(struct work_struct *work,
538 int pool_id)
David Howells365970a2006-11-22 14:54:49 +0000539{
Tejun Heo23657bb2012-08-13 17:08:19 -0700540 /*
541 * The following wmb is paired with the implied mb in
542 * test_and_set_bit(PENDING) and ensures all updates to @work made
543 * here are visible to and precede any updates by the next PENDING
544 * owner.
545 */
546 smp_wmb();
Tejun Heo7c3eed52013-01-24 11:01:33 -0800547 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200548}
549
550static void clear_work_data(struct work_struct *work)
551{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800552 smp_wmb(); /* see set_work_pool_and_clear_pending() */
553 set_work_data(work, WORK_STRUCT_NO_POOL, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200554}
555
Tejun Heo112202d2013-02-13 19:29:12 -0800556static struct pool_workqueue *get_work_pwq(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200557{
Tejun Heoe1201532010-07-22 14:14:25 +0200558 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200559
Tejun Heo112202d2013-02-13 19:29:12 -0800560 if (data & WORK_STRUCT_PWQ)
Tejun Heoe1201532010-07-22 14:14:25 +0200561 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
562 else
563 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200564}
565
Tejun Heo7c3eed52013-01-24 11:01:33 -0800566/**
567 * get_work_pool - return the worker_pool a given work was associated with
568 * @work: the work item of interest
569 *
570 * Return the worker_pool @work was last associated with. %NULL if none.
571 */
572static struct worker_pool *get_work_pool(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200573{
Tejun Heoe1201532010-07-22 14:14:25 +0200574 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800575 struct worker_pool *pool;
576 int pool_id;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200577
Tejun Heo112202d2013-02-13 19:29:12 -0800578 if (data & WORK_STRUCT_PWQ)
579 return ((struct pool_workqueue *)
Tejun Heo7c3eed52013-01-24 11:01:33 -0800580 (data & WORK_STRUCT_WQ_DATA_MASK))->pool;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200581
Tejun Heo7c3eed52013-01-24 11:01:33 -0800582 pool_id = data >> WORK_OFFQ_POOL_SHIFT;
583 if (pool_id == WORK_OFFQ_POOL_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200584 return NULL;
585
Tejun Heo7c3eed52013-01-24 11:01:33 -0800586 pool = worker_pool_by_id(pool_id);
587 WARN_ON_ONCE(!pool);
588 return pool;
589}
590
591/**
592 * get_work_pool_id - return the worker pool ID a given work is associated with
593 * @work: the work item of interest
594 *
595 * Return the worker_pool ID @work was last associated with.
596 * %WORK_OFFQ_POOL_NONE if none.
597 */
598static int get_work_pool_id(struct work_struct *work)
599{
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800600 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800601
Tejun Heo112202d2013-02-13 19:29:12 -0800602 if (data & WORK_STRUCT_PWQ)
603 return ((struct pool_workqueue *)
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800604 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id;
605
606 return data >> WORK_OFFQ_POOL_SHIFT;
Tejun Heo7c3eed52013-01-24 11:01:33 -0800607}
608
Tejun Heobbb68df2012-08-03 10:30:46 -0700609static void mark_work_canceling(struct work_struct *work)
610{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800611 unsigned long pool_id = get_work_pool_id(work);
Tejun Heobbb68df2012-08-03 10:30:46 -0700612
Tejun Heo7c3eed52013-01-24 11:01:33 -0800613 pool_id <<= WORK_OFFQ_POOL_SHIFT;
614 set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700615}
616
617static bool work_is_canceling(struct work_struct *work)
618{
619 unsigned long data = atomic_long_read(&work->data);
620
Tejun Heo112202d2013-02-13 19:29:12 -0800621 return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700622}
623
David Howells365970a2006-11-22 14:54:49 +0000624/*
Tejun Heo32704762012-07-13 22:16:45 -0700625 * Policy functions. These define the policies on how the global worker
626 * pools are managed. Unless noted otherwise, these functions assume that
Tejun Heod565ed62013-01-24 11:01:33 -0800627 * they're being called with pool->lock held.
David Howells365970a2006-11-22 14:54:49 +0000628 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200629
Tejun Heo63d95a92012-07-12 14:46:37 -0700630static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000631{
Tejun Heoe19e3972013-01-24 11:39:44 -0800632 return !atomic_read(&pool->nr_running);
David Howells365970a2006-11-22 14:54:49 +0000633}
634
Tejun Heoe22bee72010-06-29 10:07:14 +0200635/*
636 * Need to wake up a worker? Called from anything but currently
637 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700638 *
639 * Note that, because unbound workers never contribute to nr_running, this
Tejun Heo706026c2013-01-24 11:01:34 -0800640 * function will always return %true for unbound pools as long as the
Tejun Heo974271c2012-07-12 14:46:37 -0700641 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200642 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700643static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000644{
Tejun Heo63d95a92012-07-12 14:46:37 -0700645 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000646}
647
Tejun Heoe22bee72010-06-29 10:07:14 +0200648/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700649static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200650{
Tejun Heo63d95a92012-07-12 14:46:37 -0700651 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200652}
653
654/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700655static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200656{
Tejun Heoe19e3972013-01-24 11:39:44 -0800657 return !list_empty(&pool->worklist) &&
658 atomic_read(&pool->nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200659}
660
661/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700662static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200663{
Tejun Heo63d95a92012-07-12 14:46:37 -0700664 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200665}
666
667/* Do I need to be the manager? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700668static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200669{
Tejun Heo63d95a92012-07-12 14:46:37 -0700670 return need_to_create_worker(pool) ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700671 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200672}
673
674/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700675static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200676{
Lai Jiangshan552a37e2012-09-10 10:03:33 -0700677 bool managing = pool->flags & POOL_MANAGING_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -0700678 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
679 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200680
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700681 /*
682 * nr_idle and idle_list may disagree if idle rebinding is in
683 * progress. Never return %true if idle_list is empty.
684 */
685 if (list_empty(&pool->idle_list))
686 return false;
687
Tejun Heoe22bee72010-06-29 10:07:14 +0200688 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
689}
690
691/*
692 * Wake up functions.
693 */
694
Tejun Heo7e116292010-06-29 10:07:13 +0200695/* Return the first worker. Safe with preemption disabled */
Tejun Heo63d95a92012-07-12 14:46:37 -0700696static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200697{
Tejun Heo63d95a92012-07-12 14:46:37 -0700698 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200699 return NULL;
700
Tejun Heo63d95a92012-07-12 14:46:37 -0700701 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200702}
703
704/**
705 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700706 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200707 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700708 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200709 *
710 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800711 * spin_lock_irq(pool->lock).
Tejun Heo7e116292010-06-29 10:07:13 +0200712 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700713static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200714{
Tejun Heo63d95a92012-07-12 14:46:37 -0700715 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200716
717 if (likely(worker))
718 wake_up_process(worker->task);
719}
720
Tejun Heo4690c4a2010-06-29 10:07:10 +0200721/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200722 * wq_worker_waking_up - a worker is waking up
723 * @task: task waking up
724 * @cpu: CPU @task is waking up to
725 *
726 * This function is called during try_to_wake_up() when a worker is
727 * being awoken.
728 *
729 * CONTEXT:
730 * spin_lock_irq(rq->lock)
731 */
Tejun Heod84ff052013-03-12 11:29:59 -0700732void wq_worker_waking_up(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200733{
734 struct worker *worker = kthread_data(task);
735
Joonsoo Kim36576002012-10-26 23:03:49 +0900736 if (!(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoec22ca52013-01-24 11:01:33 -0800737 WARN_ON_ONCE(worker->pool->cpu != cpu);
Tejun Heoe19e3972013-01-24 11:39:44 -0800738 atomic_inc(&worker->pool->nr_running);
Joonsoo Kim36576002012-10-26 23:03:49 +0900739 }
Tejun Heoe22bee72010-06-29 10:07:14 +0200740}
741
742/**
743 * wq_worker_sleeping - a worker is going to sleep
744 * @task: task going to sleep
745 * @cpu: CPU in question, must be the current CPU number
746 *
747 * This function is called during schedule() when a busy worker is
748 * going to sleep. Worker on the same cpu can be woken up by
749 * returning pointer to its task.
750 *
751 * CONTEXT:
752 * spin_lock_irq(rq->lock)
753 *
754 * RETURNS:
755 * Worker task on @cpu to wake up, %NULL if none.
756 */
Tejun Heod84ff052013-03-12 11:29:59 -0700757struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200758{
759 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heo111c2252013-01-17 17:16:24 -0800760 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200761
Tejun Heo111c2252013-01-17 17:16:24 -0800762 /*
763 * Rescuers, which may not have all the fields set up like normal
764 * workers, also reach here, let's not access anything before
765 * checking NOT_RUNNING.
766 */
Steven Rostedt2d646722010-12-03 23:12:33 -0500767 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200768 return NULL;
769
Tejun Heo111c2252013-01-17 17:16:24 -0800770 pool = worker->pool;
Tejun Heo111c2252013-01-17 17:16:24 -0800771
Tejun Heoe22bee72010-06-29 10:07:14 +0200772 /* this can only happen on the local cpu */
Tejun Heo6183c002013-03-12 11:29:57 -0700773 if (WARN_ON_ONCE(cpu != raw_smp_processor_id()))
774 return NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +0200775
776 /*
777 * The counterpart of the following dec_and_test, implied mb,
778 * worklist not empty test sequence is in insert_work().
779 * Please read comment there.
780 *
Tejun Heo628c78e2012-07-17 12:39:27 -0700781 * NOT_RUNNING is clear. This means that we're bound to and
782 * running on the local cpu w/ rq lock held and preemption
783 * disabled, which in turn means that none else could be
Tejun Heod565ed62013-01-24 11:01:33 -0800784 * manipulating idle_list, so dereferencing idle_list without pool
Tejun Heo628c78e2012-07-17 12:39:27 -0700785 * lock is safe.
Tejun Heoe22bee72010-06-29 10:07:14 +0200786 */
Tejun Heoe19e3972013-01-24 11:39:44 -0800787 if (atomic_dec_and_test(&pool->nr_running) &&
788 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700789 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200790 return to_wakeup ? to_wakeup->task : NULL;
791}
792
793/**
794 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200795 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200796 * @flags: flags to set
797 * @wakeup: wakeup an idle worker if necessary
798 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200799 * Set @flags in @worker->flags and adjust nr_running accordingly. If
800 * nr_running becomes zero and @wakeup is %true, an idle worker is
801 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200802 *
Tejun Heocb444762010-07-02 10:03:50 +0200803 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800804 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200805 */
806static inline void worker_set_flags(struct worker *worker, unsigned int flags,
807 bool wakeup)
808{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700809 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200810
Tejun Heocb444762010-07-02 10:03:50 +0200811 WARN_ON_ONCE(worker->task != current);
812
Tejun Heoe22bee72010-06-29 10:07:14 +0200813 /*
814 * If transitioning into NOT_RUNNING, adjust nr_running and
815 * wake up an idle worker as necessary if requested by
816 * @wakeup.
817 */
818 if ((flags & WORKER_NOT_RUNNING) &&
819 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoe22bee72010-06-29 10:07:14 +0200820 if (wakeup) {
Tejun Heoe19e3972013-01-24 11:39:44 -0800821 if (atomic_dec_and_test(&pool->nr_running) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -0700822 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700823 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200824 } else
Tejun Heoe19e3972013-01-24 11:39:44 -0800825 atomic_dec(&pool->nr_running);
Tejun Heoe22bee72010-06-29 10:07:14 +0200826 }
827
Tejun Heod302f012010-06-29 10:07:13 +0200828 worker->flags |= flags;
829}
830
831/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200832 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200833 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200834 * @flags: flags to clear
835 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200836 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200837 *
Tejun Heocb444762010-07-02 10:03:50 +0200838 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800839 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200840 */
841static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
842{
Tejun Heo63d95a92012-07-12 14:46:37 -0700843 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200844 unsigned int oflags = worker->flags;
845
Tejun Heocb444762010-07-02 10:03:50 +0200846 WARN_ON_ONCE(worker->task != current);
847
Tejun Heod302f012010-06-29 10:07:13 +0200848 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200849
Tejun Heo42c025f2011-01-11 15:58:49 +0100850 /*
851 * If transitioning out of NOT_RUNNING, increment nr_running. Note
852 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
853 * of multiple flags, not a single flag.
854 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200855 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
856 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heoe19e3972013-01-24 11:39:44 -0800857 atomic_inc(&pool->nr_running);
Tejun Heod302f012010-06-29 10:07:13 +0200858}
859
860/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200861 * find_worker_executing_work - find worker which is executing a work
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800862 * @pool: pool of interest
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200863 * @work: work to find worker for
864 *
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800865 * Find a worker which is executing @work on @pool by searching
866 * @pool->busy_hash which is keyed by the address of @work. For a worker
Tejun Heoa2c1c572012-12-18 10:35:02 -0800867 * to match, its current execution should match the address of @work and
868 * its work function. This is to avoid unwanted dependency between
869 * unrelated work executions through a work item being recycled while still
870 * being executed.
871 *
872 * This is a bit tricky. A work item may be freed once its execution
873 * starts and nothing prevents the freed area from being recycled for
874 * another work item. If the same work item address ends up being reused
875 * before the original execution finishes, workqueue will identify the
876 * recycled work item as currently executing and make it wait until the
877 * current execution finishes, introducing an unwanted dependency.
878 *
879 * This function checks the work item address, work function and workqueue
880 * to avoid false positives. Note that this isn't complete as one may
881 * construct a work function which can introduce dependency onto itself
882 * through a recycled work item. Well, if somebody wants to shoot oneself
883 * in the foot that badly, there's only so much we can do, and if such
884 * deadlock actually occurs, it should be easy to locate the culprit work
885 * function.
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200886 *
887 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800888 * spin_lock_irq(pool->lock).
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200889 *
890 * RETURNS:
891 * Pointer to worker which is executing @work if found, NULL
892 * otherwise.
893 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800894static struct worker *find_worker_executing_work(struct worker_pool *pool,
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200895 struct work_struct *work)
896{
Sasha Levin42f85702012-12-17 10:01:23 -0500897 struct worker *worker;
Sasha Levin42f85702012-12-17 10:01:23 -0500898
Sasha Levinb67bfe02013-02-27 17:06:00 -0800899 hash_for_each_possible(pool->busy_hash, worker, hentry,
Tejun Heoa2c1c572012-12-18 10:35:02 -0800900 (unsigned long)work)
901 if (worker->current_work == work &&
902 worker->current_func == work->func)
Sasha Levin42f85702012-12-17 10:01:23 -0500903 return worker;
904
905 return NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200906}
907
908/**
Tejun Heobf4ede02012-08-03 10:30:46 -0700909 * move_linked_works - move linked works to a list
910 * @work: start of series of works to be scheduled
911 * @head: target list to append @work to
912 * @nextp: out paramter for nested worklist walking
913 *
914 * Schedule linked works starting from @work to @head. Work series to
915 * be scheduled starts at @work and includes any consecutive work with
916 * WORK_STRUCT_LINKED set in its predecessor.
917 *
918 * If @nextp is not NULL, it's updated to point to the next work of
919 * the last scheduled work. This allows move_linked_works() to be
920 * nested inside outer list_for_each_entry_safe().
921 *
922 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800923 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -0700924 */
925static void move_linked_works(struct work_struct *work, struct list_head *head,
926 struct work_struct **nextp)
927{
928 struct work_struct *n;
929
930 /*
931 * Linked worklist will always end before the end of the list,
932 * use NULL for list head.
933 */
934 list_for_each_entry_safe_from(work, n, NULL, entry) {
935 list_move_tail(&work->entry, head);
936 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
937 break;
938 }
939
940 /*
941 * If we're already inside safe list traversal and have moved
942 * multiple works to the scheduled queue, the next position
943 * needs to be updated.
944 */
945 if (nextp)
946 *nextp = n;
947}
948
Tejun Heo112202d2013-02-13 19:29:12 -0800949static void pwq_activate_delayed_work(struct work_struct *work)
Tejun Heobf4ede02012-08-03 10:30:46 -0700950{
Tejun Heo112202d2013-02-13 19:29:12 -0800951 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobf4ede02012-08-03 10:30:46 -0700952
953 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -0800954 move_linked_works(work, &pwq->pool->worklist, NULL);
Tejun Heobf4ede02012-08-03 10:30:46 -0700955 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo112202d2013-02-13 19:29:12 -0800956 pwq->nr_active++;
Tejun Heobf4ede02012-08-03 10:30:46 -0700957}
958
Tejun Heo112202d2013-02-13 19:29:12 -0800959static void pwq_activate_first_delayed(struct pool_workqueue *pwq)
Lai Jiangshan3aa62492012-09-18 10:40:00 -0700960{
Tejun Heo112202d2013-02-13 19:29:12 -0800961 struct work_struct *work = list_first_entry(&pwq->delayed_works,
Lai Jiangshan3aa62492012-09-18 10:40:00 -0700962 struct work_struct, entry);
963
Tejun Heo112202d2013-02-13 19:29:12 -0800964 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -0700965}
966
Tejun Heobf4ede02012-08-03 10:30:46 -0700967/**
Tejun Heo112202d2013-02-13 19:29:12 -0800968 * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight
969 * @pwq: pwq of interest
Tejun Heobf4ede02012-08-03 10:30:46 -0700970 * @color: color of work which left the queue
Tejun Heobf4ede02012-08-03 10:30:46 -0700971 *
972 * A work either has completed or is removed from pending queue,
Tejun Heo112202d2013-02-13 19:29:12 -0800973 * decrement nr_in_flight of its pwq and handle workqueue flushing.
Tejun Heobf4ede02012-08-03 10:30:46 -0700974 *
975 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800976 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -0700977 */
Tejun Heo112202d2013-02-13 19:29:12 -0800978static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color)
Tejun Heobf4ede02012-08-03 10:30:46 -0700979{
980 /* ignore uncolored works */
981 if (color == WORK_NO_COLOR)
982 return;
983
Tejun Heo112202d2013-02-13 19:29:12 -0800984 pwq->nr_in_flight[color]--;
Tejun Heobf4ede02012-08-03 10:30:46 -0700985
Tejun Heo112202d2013-02-13 19:29:12 -0800986 pwq->nr_active--;
987 if (!list_empty(&pwq->delayed_works)) {
Lai Jiangshanb3f9f402012-09-18 10:40:00 -0700988 /* one down, submit a delayed one */
Tejun Heo112202d2013-02-13 19:29:12 -0800989 if (pwq->nr_active < pwq->max_active)
990 pwq_activate_first_delayed(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -0700991 }
992
993 /* is flush in progress and are we at the flushing tip? */
Tejun Heo112202d2013-02-13 19:29:12 -0800994 if (likely(pwq->flush_color != color))
Tejun Heobf4ede02012-08-03 10:30:46 -0700995 return;
996
997 /* are there still in-flight works? */
Tejun Heo112202d2013-02-13 19:29:12 -0800998 if (pwq->nr_in_flight[color])
Tejun Heobf4ede02012-08-03 10:30:46 -0700999 return;
1000
Tejun Heo112202d2013-02-13 19:29:12 -08001001 /* this pwq is done, clear flush_color */
1002 pwq->flush_color = -1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001003
1004 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001005 * If this was the last pwq, wake up the first flusher. It
Tejun Heobf4ede02012-08-03 10:30:46 -07001006 * will handle the rest.
1007 */
Tejun Heo112202d2013-02-13 19:29:12 -08001008 if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush))
1009 complete(&pwq->wq->first_flusher->done);
Tejun Heobf4ede02012-08-03 10:30:46 -07001010}
1011
Tejun Heo36e227d2012-08-03 10:30:46 -07001012/**
Tejun Heobbb68df2012-08-03 10:30:46 -07001013 * try_to_grab_pending - steal work item from worklist and disable irq
Tejun Heo36e227d2012-08-03 10:30:46 -07001014 * @work: work item to steal
1015 * @is_dwork: @work is a delayed_work
Tejun Heobbb68df2012-08-03 10:30:46 -07001016 * @flags: place to store irq state
Tejun Heo36e227d2012-08-03 10:30:46 -07001017 *
1018 * Try to grab PENDING bit of @work. This function can handle @work in any
1019 * stable state - idle, on timer or on worklist. Return values are
1020 *
1021 * 1 if @work was pending and we successfully stole PENDING
1022 * 0 if @work was idle and we claimed PENDING
1023 * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry
Tejun Heobbb68df2012-08-03 10:30:46 -07001024 * -ENOENT if someone else is canceling @work, this state may persist
1025 * for arbitrarily long
Tejun Heo36e227d2012-08-03 10:30:46 -07001026 *
Tejun Heobbb68df2012-08-03 10:30:46 -07001027 * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001028 * interrupted while holding PENDING and @work off queue, irq must be
1029 * disabled on entry. This, combined with delayed_work->timer being
1030 * irqsafe, ensures that we return -EAGAIN for finite short period of time.
Tejun Heobbb68df2012-08-03 10:30:46 -07001031 *
1032 * On successful return, >= 0, irq is disabled and the caller is
1033 * responsible for releasing it using local_irq_restore(*@flags).
1034 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001035 * This function is safe to call from any context including IRQ handler.
Tejun Heobf4ede02012-08-03 10:30:46 -07001036 */
Tejun Heobbb68df2012-08-03 10:30:46 -07001037static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
1038 unsigned long *flags)
Tejun Heobf4ede02012-08-03 10:30:46 -07001039{
Tejun Heod565ed62013-01-24 11:01:33 -08001040 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08001041 struct pool_workqueue *pwq;
Tejun Heobf4ede02012-08-03 10:30:46 -07001042
Tejun Heobbb68df2012-08-03 10:30:46 -07001043 local_irq_save(*flags);
1044
Tejun Heo36e227d2012-08-03 10:30:46 -07001045 /* try to steal the timer if it exists */
1046 if (is_dwork) {
1047 struct delayed_work *dwork = to_delayed_work(work);
1048
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001049 /*
1050 * dwork->timer is irqsafe. If del_timer() fails, it's
1051 * guaranteed that the timer is not queued anywhere and not
1052 * running on the local CPU.
1053 */
Tejun Heo36e227d2012-08-03 10:30:46 -07001054 if (likely(del_timer(&dwork->timer)))
1055 return 1;
1056 }
1057
1058 /* try to claim PENDING the normal way */
Tejun Heobf4ede02012-08-03 10:30:46 -07001059 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1060 return 0;
1061
1062 /*
1063 * The queueing is in progress, or it is already queued. Try to
1064 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1065 */
Tejun Heod565ed62013-01-24 11:01:33 -08001066 pool = get_work_pool(work);
1067 if (!pool)
Tejun Heobbb68df2012-08-03 10:30:46 -07001068 goto fail;
Tejun Heobf4ede02012-08-03 10:30:46 -07001069
Tejun Heod565ed62013-01-24 11:01:33 -08001070 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001071 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001072 * work->data is guaranteed to point to pwq only while the work
1073 * item is queued on pwq->wq, and both updating work->data to point
1074 * to pwq on queueing and to pool on dequeueing are done under
1075 * pwq->pool->lock. This in turn guarantees that, if work->data
1076 * points to pwq which is associated with a locked pool, the work
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001077 * item is currently queued on that pool.
1078 */
Tejun Heo112202d2013-02-13 19:29:12 -08001079 pwq = get_work_pwq(work);
1080 if (pwq && pwq->pool == pool) {
Tejun Heo16062832013-02-06 18:04:53 -08001081 debug_work_deactivate(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001082
Tejun Heo16062832013-02-06 18:04:53 -08001083 /*
1084 * A delayed work item cannot be grabbed directly because
1085 * it might have linked NO_COLOR work items which, if left
Tejun Heo112202d2013-02-13 19:29:12 -08001086 * on the delayed_list, will confuse pwq->nr_active
Tejun Heo16062832013-02-06 18:04:53 -08001087 * management later on and cause stall. Make sure the work
1088 * item is activated before grabbing.
1089 */
1090 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
Tejun Heo112202d2013-02-13 19:29:12 -08001091 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001092
Tejun Heo16062832013-02-06 18:04:53 -08001093 list_del_init(&work->entry);
Tejun Heo112202d2013-02-13 19:29:12 -08001094 pwq_dec_nr_in_flight(get_work_pwq(work), get_work_color(work));
Tejun Heo36e227d2012-08-03 10:30:46 -07001095
Tejun Heo112202d2013-02-13 19:29:12 -08001096 /* work->data points to pwq iff queued, point to pool */
Tejun Heo16062832013-02-06 18:04:53 -08001097 set_work_pool_and_keep_pending(work, pool->id);
Lai Jiangshan4468a002013-02-06 18:04:53 -08001098
Tejun Heo16062832013-02-06 18:04:53 -08001099 spin_unlock(&pool->lock);
1100 return 1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001101 }
Tejun Heod565ed62013-01-24 11:01:33 -08001102 spin_unlock(&pool->lock);
Tejun Heobbb68df2012-08-03 10:30:46 -07001103fail:
1104 local_irq_restore(*flags);
1105 if (work_is_canceling(work))
1106 return -ENOENT;
1107 cpu_relax();
Tejun Heo36e227d2012-08-03 10:30:46 -07001108 return -EAGAIN;
Tejun Heobf4ede02012-08-03 10:30:46 -07001109}
1110
1111/**
Tejun Heo706026c2013-01-24 11:01:34 -08001112 * insert_work - insert a work into a pool
Tejun Heo112202d2013-02-13 19:29:12 -08001113 * @pwq: pwq @work belongs to
Tejun Heo4690c4a2010-06-29 10:07:10 +02001114 * @work: work to insert
1115 * @head: insertion point
1116 * @extra_flags: extra WORK_STRUCT_* flags to set
1117 *
Tejun Heo112202d2013-02-13 19:29:12 -08001118 * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to
Tejun Heo706026c2013-01-24 11:01:34 -08001119 * work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +02001120 *
1121 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001122 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02001123 */
Tejun Heo112202d2013-02-13 19:29:12 -08001124static void insert_work(struct pool_workqueue *pwq, struct work_struct *work,
1125 struct list_head *head, unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001126{
Tejun Heo112202d2013-02-13 19:29:12 -08001127 struct worker_pool *pool = pwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01001128
Tejun Heo4690c4a2010-06-29 10:07:10 +02001129 /* we own @work, set data and link */
Tejun Heo112202d2013-02-13 19:29:12 -08001130 set_work_pwq(work, pwq, extra_flags);
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -07001131 list_add_tail(&work->entry, head);
Tejun Heoe22bee72010-06-29 10:07:14 +02001132
1133 /*
1134 * Ensure either worker_sched_deactivated() sees the above
1135 * list_add_tail() or we see zero nr_running to avoid workers
1136 * lying around lazily while there are works to be processed.
1137 */
1138 smp_mb();
1139
Tejun Heo63d95a92012-07-12 14:46:37 -07001140 if (__need_more_worker(pool))
1141 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001142}
1143
Tejun Heoc8efcc22010-12-20 19:32:04 +01001144/*
1145 * Test whether @work is being queued from another work executing on the
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001146 * same workqueue.
Tejun Heoc8efcc22010-12-20 19:32:04 +01001147 */
1148static bool is_chained_work(struct workqueue_struct *wq)
1149{
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001150 struct worker *worker;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001151
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001152 worker = current_wq_worker();
1153 /*
1154 * Return %true iff I'm a worker execuing a work item on @wq. If
1155 * I'm @worker, it's safe to dereference it without locking.
1156 */
Tejun Heo112202d2013-02-13 19:29:12 -08001157 return worker && worker->current_pwq->wq == wq;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001158}
1159
Tejun Heod84ff052013-03-12 11:29:59 -07001160static void __queue_work(int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 struct work_struct *work)
1162{
Tejun Heo112202d2013-02-13 19:29:12 -08001163 struct pool_workqueue *pwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001164 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001165 unsigned int work_flags;
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001166 unsigned int req_cpu = cpu;
Tejun Heo8930cab2012-08-03 10:30:45 -07001167
1168 /*
1169 * While a work item is PENDING && off queue, a task trying to
1170 * steal the PENDING will busy-loop waiting for it to either get
1171 * queued or lose PENDING. Grabbing PENDING and queueing should
1172 * happen with IRQ disabled.
1173 */
1174 WARN_ON_ONCE(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001176 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001177
Tejun Heoc8efcc22010-12-20 19:32:04 +01001178 /* if dying, only works from the same workqueue are allowed */
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02001179 if (unlikely(wq->flags & WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001180 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001181 return;
1182
Tejun Heo112202d2013-02-13 19:29:12 -08001183 /* determine the pwq to use */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001184 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001185 struct worker_pool *last_pool;
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001186
Tejun Heo57469822012-08-03 10:30:45 -07001187 if (cpu == WORK_CPU_UNBOUND)
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001188 cpu = raw_smp_processor_id();
1189
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001190 /*
Tejun Heodbf25762012-08-20 14:51:23 -07001191 * It's multi cpu. If @work was previously on a different
1192 * cpu, it might still be running there, in which case the
1193 * work needs to be queued on that cpu to guarantee
1194 * non-reentrancy.
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001195 */
Tejun Heo112202d2013-02-13 19:29:12 -08001196 pwq = get_pwq(cpu, wq);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001197 last_pool = get_work_pool(work);
Tejun Heodbf25762012-08-20 14:51:23 -07001198
Tejun Heo112202d2013-02-13 19:29:12 -08001199 if (last_pool && last_pool != pwq->pool) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001200 struct worker *worker;
1201
Tejun Heod565ed62013-01-24 11:01:33 -08001202 spin_lock(&last_pool->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001203
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001204 worker = find_worker_executing_work(last_pool, work);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001205
Tejun Heo112202d2013-02-13 19:29:12 -08001206 if (worker && worker->current_pwq->wq == wq) {
1207 pwq = get_pwq(last_pool->cpu, wq);
Lai Jiangshan8594fad2013-02-07 13:14:20 -08001208 } else {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001209 /* meh... not running there, queue here */
Tejun Heod565ed62013-01-24 11:01:33 -08001210 spin_unlock(&last_pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08001211 spin_lock(&pwq->pool->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001212 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001213 } else {
Tejun Heo112202d2013-02-13 19:29:12 -08001214 spin_lock(&pwq->pool->lock);
Tejun Heo8930cab2012-08-03 10:30:45 -07001215 }
Tejun Heof3421792010-07-02 10:03:51 +02001216 } else {
Tejun Heo112202d2013-02-13 19:29:12 -08001217 pwq = get_pwq(WORK_CPU_UNBOUND, wq);
1218 spin_lock(&pwq->pool->lock);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001219 }
1220
Tejun Heo112202d2013-02-13 19:29:12 -08001221 /* pwq determined, queue */
1222 trace_workqueue_queue_work(req_cpu, pwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001223
Dan Carpenterf5b25522012-04-13 22:06:58 +03001224 if (WARN_ON(!list_empty(&work->entry))) {
Tejun Heo112202d2013-02-13 19:29:12 -08001225 spin_unlock(&pwq->pool->lock);
Dan Carpenterf5b25522012-04-13 22:06:58 +03001226 return;
1227 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001228
Tejun Heo112202d2013-02-13 19:29:12 -08001229 pwq->nr_in_flight[pwq->work_color]++;
1230 work_flags = work_color_to_flags(pwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001231
Tejun Heo112202d2013-02-13 19:29:12 -08001232 if (likely(pwq->nr_active < pwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001233 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001234 pwq->nr_active++;
1235 worklist = &pwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001236 } else {
1237 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo112202d2013-02-13 19:29:12 -08001238 worklist = &pwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001239 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001240
Tejun Heo112202d2013-02-13 19:29:12 -08001241 insert_work(pwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001242
Tejun Heo112202d2013-02-13 19:29:12 -08001243 spin_unlock(&pwq->pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001246/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07001247 * queue_work_on - queue work on specific cpu
1248 * @cpu: CPU number to execute work on
1249 * @wq: workqueue to use
1250 * @work: work to queue
1251 *
Tejun Heod4283e92012-08-03 10:30:44 -07001252 * Returns %false if @work was already on a queue, %true otherwise.
Zhang Ruic1a220e2008-07-23 21:28:39 -07001253 *
1254 * We queue the work to a specific CPU, the caller must ensure it
1255 * can't go away.
1256 */
Tejun Heod4283e92012-08-03 10:30:44 -07001257bool queue_work_on(int cpu, struct workqueue_struct *wq,
1258 struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07001259{
Tejun Heod4283e92012-08-03 10:30:44 -07001260 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001261 unsigned long flags;
1262
1263 local_irq_save(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001264
Tejun Heo22df02b2010-06-29 10:07:10 +02001265 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001266 __queue_work(cpu, wq, work);
Tejun Heod4283e92012-08-03 10:30:44 -07001267 ret = true;
Zhang Ruic1a220e2008-07-23 21:28:39 -07001268 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001269
1270 local_irq_restore(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001271 return ret;
1272}
1273EXPORT_SYMBOL_GPL(queue_work_on);
1274
Tejun Heo0a13c002012-08-03 10:30:44 -07001275/**
1276 * queue_work - queue work on a workqueue
1277 * @wq: workqueue to use
1278 * @work: work to queue
1279 *
Tejun Heod4283e92012-08-03 10:30:44 -07001280 * Returns %false if @work was already on a queue, %true otherwise.
Tejun Heo0a13c002012-08-03 10:30:44 -07001281 *
1282 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1283 * it can be processed by another CPU.
1284 */
Tejun Heod4283e92012-08-03 10:30:44 -07001285bool queue_work(struct workqueue_struct *wq, struct work_struct *work)
Tejun Heo0a13c002012-08-03 10:30:44 -07001286{
Tejun Heo57469822012-08-03 10:30:45 -07001287 return queue_work_on(WORK_CPU_UNBOUND, wq, work);
Tejun Heo0a13c002012-08-03 10:30:44 -07001288}
1289EXPORT_SYMBOL_GPL(queue_work);
1290
Tejun Heod8e794d2012-08-03 10:30:45 -07001291void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
David Howells52bad642006-11-22 14:54:01 +00001293 struct delayed_work *dwork = (struct delayed_work *)__data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001295 /* should have been called from irqsafe timer with irq already off */
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001296 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297}
Konstantin Khlebnikov1438ade52013-01-24 16:36:31 +04001298EXPORT_SYMBOL(delayed_work_timer_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001300static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
1301 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302{
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001303 struct timer_list *timer = &dwork->timer;
1304 struct work_struct *work = &dwork->work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001306 WARN_ON_ONCE(timer->function != delayed_work_timer_fn ||
1307 timer->data != (unsigned long)dwork);
Tejun Heofc4b5142012-12-04 07:40:39 -08001308 WARN_ON_ONCE(timer_pending(timer));
1309 WARN_ON_ONCE(!list_empty(&work->entry));
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001310
Tejun Heo8852aac2012-12-01 16:23:42 -08001311 /*
1312 * If @delay is 0, queue @dwork->work immediately. This is for
1313 * both optimization and correctness. The earliest @timer can
1314 * expire is on the closest next tick and delayed_work users depend
1315 * on that there's no such delay when @delay is 0.
1316 */
1317 if (!delay) {
1318 __queue_work(cpu, wq, &dwork->work);
1319 return;
1320 }
1321
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001322 timer_stats_timer_set_start_info(&dwork->timer);
1323
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001324 dwork->wq = wq;
Tejun Heo12650572012-08-08 09:38:42 -07001325 dwork->cpu = cpu;
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001326 timer->expires = jiffies + delay;
1327
1328 if (unlikely(cpu != WORK_CPU_UNBOUND))
1329 add_timer_on(timer, cpu);
1330 else
1331 add_timer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332}
1333
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001334/**
1335 * queue_delayed_work_on - queue work on specific CPU after delay
1336 * @cpu: CPU number to execute work on
1337 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001338 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001339 * @delay: number of jiffies to wait before queueing
1340 *
Tejun Heo715f1302012-08-03 10:30:46 -07001341 * Returns %false if @work was already on a queue, %true otherwise. If
1342 * @delay is zero and @dwork is idle, it will be scheduled for immediate
1343 * execution.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001344 */
Tejun Heod4283e92012-08-03 10:30:44 -07001345bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
1346 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001347{
David Howells52bad642006-11-22 14:54:01 +00001348 struct work_struct *work = &dwork->work;
Tejun Heod4283e92012-08-03 10:30:44 -07001349 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001350 unsigned long flags;
1351
1352 /* read the comment in __queue_work() */
1353 local_irq_save(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001354
Tejun Heo22df02b2010-06-29 10:07:10 +02001355 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001356 __queue_delayed_work(cpu, wq, dwork, delay);
Tejun Heod4283e92012-08-03 10:30:44 -07001357 ret = true;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001358 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001359
1360 local_irq_restore(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001361 return ret;
1362}
Dave Jonesae90dd52006-06-30 01:40:45 -04001363EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Tejun Heoc8e55f32010-06-29 10:07:12 +02001365/**
Tejun Heo0a13c002012-08-03 10:30:44 -07001366 * queue_delayed_work - queue work on a workqueue after delay
1367 * @wq: workqueue to use
1368 * @dwork: delayable work to queue
1369 * @delay: number of jiffies to wait before queueing
1370 *
Tejun Heo715f1302012-08-03 10:30:46 -07001371 * Equivalent to queue_delayed_work_on() but tries to use the local CPU.
Tejun Heo0a13c002012-08-03 10:30:44 -07001372 */
Tejun Heod4283e92012-08-03 10:30:44 -07001373bool queue_delayed_work(struct workqueue_struct *wq,
Tejun Heo0a13c002012-08-03 10:30:44 -07001374 struct delayed_work *dwork, unsigned long delay)
1375{
Tejun Heo57469822012-08-03 10:30:45 -07001376 return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
Tejun Heo0a13c002012-08-03 10:30:44 -07001377}
1378EXPORT_SYMBOL_GPL(queue_delayed_work);
1379
1380/**
Tejun Heo8376fe22012-08-03 10:30:47 -07001381 * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
1382 * @cpu: CPU number to execute work on
1383 * @wq: workqueue to use
1384 * @dwork: work to queue
1385 * @delay: number of jiffies to wait before queueing
1386 *
1387 * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
1388 * modify @dwork's timer so that it expires after @delay. If @delay is
1389 * zero, @work is guaranteed to be scheduled immediately regardless of its
1390 * current state.
1391 *
1392 * Returns %false if @dwork was idle and queued, %true if @dwork was
1393 * pending and its timer was modified.
1394 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001395 * This function is safe to call from any context including IRQ handler.
Tejun Heo8376fe22012-08-03 10:30:47 -07001396 * See try_to_grab_pending() for details.
1397 */
1398bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
1399 struct delayed_work *dwork, unsigned long delay)
1400{
1401 unsigned long flags;
1402 int ret;
1403
1404 do {
1405 ret = try_to_grab_pending(&dwork->work, true, &flags);
1406 } while (unlikely(ret == -EAGAIN));
1407
1408 if (likely(ret >= 0)) {
1409 __queue_delayed_work(cpu, wq, dwork, delay);
1410 local_irq_restore(flags);
1411 }
1412
1413 /* -ENOENT from try_to_grab_pending() becomes %true */
1414 return ret;
1415}
1416EXPORT_SYMBOL_GPL(mod_delayed_work_on);
1417
1418/**
1419 * mod_delayed_work - modify delay of or queue a delayed work
1420 * @wq: workqueue to use
1421 * @dwork: work to queue
1422 * @delay: number of jiffies to wait before queueing
1423 *
1424 * mod_delayed_work_on() on local CPU.
1425 */
1426bool mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dwork,
1427 unsigned long delay)
1428{
1429 return mod_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
1430}
1431EXPORT_SYMBOL_GPL(mod_delayed_work);
1432
1433/**
Tejun Heoc8e55f32010-06-29 10:07:12 +02001434 * worker_enter_idle - enter idle state
1435 * @worker: worker which is entering idle state
1436 *
1437 * @worker is entering idle state. Update stats and idle timer if
1438 * necessary.
1439 *
1440 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001441 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001442 */
1443static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001445 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Tejun Heo6183c002013-03-12 11:29:57 -07001447 if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) ||
1448 WARN_ON_ONCE(!list_empty(&worker->entry) &&
1449 (worker->hentry.next || worker->hentry.pprev)))
1450 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Tejun Heocb444762010-07-02 10:03:50 +02001452 /* can't use worker_set_flags(), also called from start_worker() */
1453 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001454 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001455 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001456
Tejun Heoc8e55f32010-06-29 10:07:12 +02001457 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001458 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001459
Tejun Heo628c78e2012-07-17 12:39:27 -07001460 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1461 mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
Tejun Heocb444762010-07-02 10:03:50 +02001462
Tejun Heo544ecf32012-05-14 15:04:50 -07001463 /*
Tejun Heo706026c2013-01-24 11:01:34 -08001464 * Sanity check nr_running. Because wq_unbind_fn() releases
Tejun Heod565ed62013-01-24 11:01:33 -08001465 * pool->lock between setting %WORKER_UNBOUND and zapping
Tejun Heo628c78e2012-07-17 12:39:27 -07001466 * nr_running, the warning may trigger spuriously. Check iff
1467 * unbind is not in progress.
Tejun Heo544ecf32012-05-14 15:04:50 -07001468 */
Tejun Heo24647572013-01-24 11:01:33 -08001469 WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001470 pool->nr_workers == pool->nr_idle &&
Tejun Heoe19e3972013-01-24 11:39:44 -08001471 atomic_read(&pool->nr_running));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472}
1473
Tejun Heoc8e55f32010-06-29 10:07:12 +02001474/**
1475 * worker_leave_idle - leave idle state
1476 * @worker: worker which is leaving idle state
1477 *
1478 * @worker is leaving idle state. Update stats.
1479 *
1480 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001481 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001482 */
1483static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001485 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Tejun Heo6183c002013-03-12 11:29:57 -07001487 if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE)))
1488 return;
Tejun Heod302f012010-06-29 10:07:13 +02001489 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001490 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001491 list_del_init(&worker->entry);
1492}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Tejun Heoe22bee72010-06-29 10:07:14 +02001494/**
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001495 * worker_maybe_bind_and_lock - try to bind %current to worker_pool and lock it
1496 * @pool: target worker_pool
1497 *
1498 * Bind %current to the cpu of @pool if it is associated and lock @pool.
Tejun Heoe22bee72010-06-29 10:07:14 +02001499 *
1500 * Works which are scheduled while the cpu is online must at least be
1501 * scheduled to a worker which is bound to the cpu so that if they are
1502 * flushed from cpu callbacks while cpu is going down, they are
1503 * guaranteed to execute on the cpu.
1504 *
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001505 * This function is to be used by unbound workers and rescuers to bind
Tejun Heoe22bee72010-06-29 10:07:14 +02001506 * themselves to the target cpu and may race with cpu going down or
1507 * coming online. kthread_bind() can't be used because it may put the
1508 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
Tejun Heo706026c2013-01-24 11:01:34 -08001509 * verbatim as it's best effort and blocking and pool may be
Tejun Heoe22bee72010-06-29 10:07:14 +02001510 * [dis]associated in the meantime.
1511 *
Tejun Heo706026c2013-01-24 11:01:34 -08001512 * This function tries set_cpus_allowed() and locks pool and verifies the
Tejun Heo24647572013-01-24 11:01:33 -08001513 * binding against %POOL_DISASSOCIATED which is set during
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001514 * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1515 * enters idle state or fetches works without dropping lock, it can
1516 * guarantee the scheduling requirement described in the first paragraph.
Tejun Heoe22bee72010-06-29 10:07:14 +02001517 *
1518 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001519 * Might sleep. Called without any lock but returns with pool->lock
Tejun Heoe22bee72010-06-29 10:07:14 +02001520 * held.
1521 *
1522 * RETURNS:
Tejun Heo706026c2013-01-24 11:01:34 -08001523 * %true if the associated pool is online (@worker is successfully
Tejun Heoe22bee72010-06-29 10:07:14 +02001524 * bound), %false if offline.
1525 */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001526static bool worker_maybe_bind_and_lock(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001527__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001528{
Tejun Heoe22bee72010-06-29 10:07:14 +02001529 while (true) {
1530 /*
1531 * The following call may fail, succeed or succeed
1532 * without actually migrating the task to the cpu if
1533 * it races with cpu hotunplug operation. Verify
Tejun Heo24647572013-01-24 11:01:33 -08001534 * against POOL_DISASSOCIATED.
Tejun Heoe22bee72010-06-29 10:07:14 +02001535 */
Tejun Heo24647572013-01-24 11:01:33 -08001536 if (!(pool->flags & POOL_DISASSOCIATED))
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001537 set_cpus_allowed_ptr(current, get_cpu_mask(pool->cpu));
Oleg Nesterov85f41862007-05-09 02:34:20 -07001538
Tejun Heod565ed62013-01-24 11:01:33 -08001539 spin_lock_irq(&pool->lock);
Tejun Heo24647572013-01-24 11:01:33 -08001540 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heoe22bee72010-06-29 10:07:14 +02001541 return false;
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001542 if (task_cpu(current) == pool->cpu &&
Tejun Heoe22bee72010-06-29 10:07:14 +02001543 cpumask_equal(&current->cpus_allowed,
Tejun Heoec22ca52013-01-24 11:01:33 -08001544 get_cpu_mask(pool->cpu)))
Tejun Heoe22bee72010-06-29 10:07:14 +02001545 return true;
Tejun Heod565ed62013-01-24 11:01:33 -08001546 spin_unlock_irq(&pool->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001547
Tejun Heo5035b202011-04-29 18:08:37 +02001548 /*
1549 * We've raced with CPU hot[un]plug. Give it a breather
1550 * and retry migration. cond_resched() is required here;
1551 * otherwise, we might deadlock against cpu_stop trying to
1552 * bring down the CPU on non-preemptive kernel.
1553 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001554 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001555 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001556 }
1557}
1558
1559/*
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001560 * Rebind an idle @worker to its CPU. worker_thread() will test
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001561 * list_empty(@worker->entry) before leaving idle and call this function.
Tejun Heo25511a42012-07-17 12:39:27 -07001562 */
1563static void idle_worker_rebind(struct worker *worker)
1564{
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001565 /* CPU may go down again inbetween, clear UNBOUND only on success */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001566 if (worker_maybe_bind_and_lock(worker->pool))
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001567 worker_clr_flags(worker, WORKER_UNBOUND);
Tejun Heo25511a42012-07-17 12:39:27 -07001568
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001569 /* rebind complete, become available again */
1570 list_add(&worker->entry, &worker->pool->idle_list);
Tejun Heod565ed62013-01-24 11:01:33 -08001571 spin_unlock_irq(&worker->pool->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07001572}
1573
1574/*
1575 * Function for @worker->rebind.work used to rebind unbound busy workers to
Tejun Heo403c8212012-07-17 12:39:27 -07001576 * the associated cpu which is coming back online. This is scheduled by
1577 * cpu up but can race with other cpu hotplug operations and may be
1578 * executed twice without intervening cpu down.
Tejun Heoe22bee72010-06-29 10:07:14 +02001579 */
Tejun Heo25511a42012-07-17 12:39:27 -07001580static void busy_worker_rebind_fn(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001581{
1582 struct worker *worker = container_of(work, struct worker, rebind_work);
Tejun Heoe22bee72010-06-29 10:07:14 +02001583
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001584 if (worker_maybe_bind_and_lock(worker->pool))
Lai Jiangshaneab6d822012-09-18 09:59:22 -07001585 worker_clr_flags(worker, WORKER_UNBOUND);
Tejun Heoe22bee72010-06-29 10:07:14 +02001586
Tejun Heod565ed62013-01-24 11:01:33 -08001587 spin_unlock_irq(&worker->pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001588}
1589
Tejun Heo25511a42012-07-17 12:39:27 -07001590/**
Tejun Heo94cf58b2013-01-24 11:01:33 -08001591 * rebind_workers - rebind all workers of a pool to the associated CPU
1592 * @pool: pool of interest
Tejun Heo25511a42012-07-17 12:39:27 -07001593 *
Tejun Heo94cf58b2013-01-24 11:01:33 -08001594 * @pool->cpu is coming online. Rebind all workers to the CPU. Rebinding
Tejun Heo25511a42012-07-17 12:39:27 -07001595 * is different for idle and busy ones.
1596 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001597 * Idle ones will be removed from the idle_list and woken up. They will
1598 * add themselves back after completing rebind. This ensures that the
1599 * idle_list doesn't contain any unbound workers when re-bound busy workers
1600 * try to perform local wake-ups for concurrency management.
Tejun Heo25511a42012-07-17 12:39:27 -07001601 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001602 * Busy workers can rebind after they finish their current work items.
1603 * Queueing the rebind work item at the head of the scheduled list is
1604 * enough. Note that nr_running will be properly bumped as busy workers
1605 * rebind.
Tejun Heo25511a42012-07-17 12:39:27 -07001606 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001607 * On return, all non-manager workers are scheduled for rebind - see
1608 * manage_workers() for the manager special case. Any idle worker
1609 * including the manager will not appear on @idle_list until rebind is
1610 * complete, making local wake-ups safe.
Tejun Heo25511a42012-07-17 12:39:27 -07001611 */
Tejun Heo94cf58b2013-01-24 11:01:33 -08001612static void rebind_workers(struct worker_pool *pool)
Tejun Heo25511a42012-07-17 12:39:27 -07001613{
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001614 struct worker *worker, *n;
Tejun Heo25511a42012-07-17 12:39:27 -07001615 int i;
1616
Tejun Heo94cf58b2013-01-24 11:01:33 -08001617 lockdep_assert_held(&pool->assoc_mutex);
1618 lockdep_assert_held(&pool->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07001619
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001620 /* dequeue and kick idle ones */
Tejun Heo94cf58b2013-01-24 11:01:33 -08001621 list_for_each_entry_safe(worker, n, &pool->idle_list, entry) {
1622 /*
1623 * idle workers should be off @pool->idle_list until rebind
1624 * is complete to avoid receiving premature local wake-ups.
1625 */
1626 list_del_init(&worker->entry);
Lai Jiangshan96e65302012-09-02 00:28:19 +08001627
Tejun Heo94cf58b2013-01-24 11:01:33 -08001628 /*
1629 * worker_thread() will see the above dequeuing and call
1630 * idle_worker_rebind().
1631 */
1632 wake_up_process(worker->task);
1633 }
Tejun Heo25511a42012-07-17 12:39:27 -07001634
Tejun Heo94cf58b2013-01-24 11:01:33 -08001635 /* rebind busy workers */
Sasha Levinb67bfe02013-02-27 17:06:00 -08001636 for_each_busy_worker(worker, i, pool) {
Tejun Heo94cf58b2013-01-24 11:01:33 -08001637 struct work_struct *rebind_work = &worker->rebind_work;
1638 struct workqueue_struct *wq;
Tejun Heo25511a42012-07-17 12:39:27 -07001639
Tejun Heo94cf58b2013-01-24 11:01:33 -08001640 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
1641 work_data_bits(rebind_work)))
1642 continue;
Tejun Heo25511a42012-07-17 12:39:27 -07001643
Tejun Heo94cf58b2013-01-24 11:01:33 -08001644 debug_work_activate(rebind_work);
Tejun Heo90beca52012-09-04 23:12:33 -07001645
Tejun Heo94cf58b2013-01-24 11:01:33 -08001646 /*
1647 * wq doesn't really matter but let's keep @worker->pool
Tejun Heo112202d2013-02-13 19:29:12 -08001648 * and @pwq->pool consistent for sanity.
Tejun Heo94cf58b2013-01-24 11:01:33 -08001649 */
1650 if (std_worker_pool_pri(worker->pool))
1651 wq = system_highpri_wq;
1652 else
1653 wq = system_wq;
Tejun Heoec588152012-09-04 23:16:32 -07001654
Tejun Heo112202d2013-02-13 19:29:12 -08001655 insert_work(get_pwq(pool->cpu, wq), rebind_work,
Tejun Heo94cf58b2013-01-24 11:01:33 -08001656 worker->scheduled.next,
1657 work_color_to_flags(WORK_NO_COLOR));
Tejun Heoec588152012-09-04 23:16:32 -07001658 }
Tejun Heo25511a42012-07-17 12:39:27 -07001659}
1660
Tejun Heoc34056a2010-06-29 10:07:11 +02001661static struct worker *alloc_worker(void)
1662{
1663 struct worker *worker;
1664
1665 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001666 if (worker) {
1667 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001668 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heo25511a42012-07-17 12:39:27 -07001669 INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn);
Tejun Heoe22bee72010-06-29 10:07:14 +02001670 /* on creation a worker is in !idle && prep state */
1671 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001672 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001673 return worker;
1674}
1675
1676/**
1677 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001678 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001679 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001680 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001681 * can be started by calling start_worker() or destroyed using
1682 * destroy_worker().
1683 *
1684 * CONTEXT:
1685 * Might sleep. Does GFP_KERNEL allocations.
1686 *
1687 * RETURNS:
1688 * Pointer to the newly created worker.
1689 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001690static struct worker *create_worker(struct worker_pool *pool)
Tejun Heoc34056a2010-06-29 10:07:11 +02001691{
Tejun Heoe34cdddb2013-01-24 11:01:33 -08001692 const char *pri = std_worker_pool_pri(pool) ? "H" : "";
Tejun Heoc34056a2010-06-29 10:07:11 +02001693 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001694 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001695
Tejun Heod565ed62013-01-24 11:01:33 -08001696 spin_lock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001697 while (ida_get_new(&pool->worker_ida, &id)) {
Tejun Heod565ed62013-01-24 11:01:33 -08001698 spin_unlock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001699 if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001700 goto fail;
Tejun Heod565ed62013-01-24 11:01:33 -08001701 spin_lock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001702 }
Tejun Heod565ed62013-01-24 11:01:33 -08001703 spin_unlock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001704
1705 worker = alloc_worker();
1706 if (!worker)
1707 goto fail;
1708
Tejun Heobd7bdd42012-07-12 14:46:37 -07001709 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001710 worker->id = id;
1711
Tejun Heoec22ca52013-01-24 11:01:33 -08001712 if (pool->cpu != WORK_CPU_UNBOUND)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001713 worker->task = kthread_create_on_node(worker_thread,
Tejun Heoec22ca52013-01-24 11:01:33 -08001714 worker, cpu_to_node(pool->cpu),
Tejun Heod84ff052013-03-12 11:29:59 -07001715 "kworker/%d:%d%s", pool->cpu, id, pri);
Tejun Heof3421792010-07-02 10:03:51 +02001716 else
1717 worker->task = kthread_create(worker_thread, worker,
Tejun Heo32704762012-07-13 22:16:45 -07001718 "kworker/u:%d%s", id, pri);
Tejun Heoc34056a2010-06-29 10:07:11 +02001719 if (IS_ERR(worker->task))
1720 goto fail;
1721
Tejun Heoe34cdddb2013-01-24 11:01:33 -08001722 if (std_worker_pool_pri(pool))
Tejun Heo32704762012-07-13 22:16:45 -07001723 set_user_nice(worker->task, HIGHPRI_NICE_LEVEL);
1724
Tejun Heodb7bccf2010-06-29 10:07:12 +02001725 /*
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001726 * Determine CPU binding of the new worker depending on
Tejun Heo24647572013-01-24 11:01:33 -08001727 * %POOL_DISASSOCIATED. The caller is responsible for ensuring the
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001728 * flag remains stable across this function. See the comments
1729 * above the flag definition for details.
1730 *
1731 * As an unbound worker may later become a regular one if CPU comes
1732 * online, make sure every worker has %PF_THREAD_BOUND set.
Tejun Heodb7bccf2010-06-29 10:07:12 +02001733 */
Tejun Heo24647572013-01-24 11:01:33 -08001734 if (!(pool->flags & POOL_DISASSOCIATED)) {
Tejun Heoec22ca52013-01-24 11:01:33 -08001735 kthread_bind(worker->task, pool->cpu);
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001736 } else {
Tejun Heodb7bccf2010-06-29 10:07:12 +02001737 worker->task->flags |= PF_THREAD_BOUND;
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001738 worker->flags |= WORKER_UNBOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001740
Tejun Heoc34056a2010-06-29 10:07:11 +02001741 return worker;
1742fail:
1743 if (id >= 0) {
Tejun Heod565ed62013-01-24 11:01:33 -08001744 spin_lock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001745 ida_remove(&pool->worker_ida, id);
Tejun Heod565ed62013-01-24 11:01:33 -08001746 spin_unlock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001747 }
1748 kfree(worker);
1749 return NULL;
1750}
1751
1752/**
1753 * start_worker - start a newly created worker
1754 * @worker: worker to start
1755 *
Tejun Heo706026c2013-01-24 11:01:34 -08001756 * Make the pool aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001757 *
1758 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001759 * spin_lock_irq(pool->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001760 */
1761static void start_worker(struct worker *worker)
1762{
Tejun Heocb444762010-07-02 10:03:50 +02001763 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001764 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001765 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001766 wake_up_process(worker->task);
1767}
1768
1769/**
1770 * destroy_worker - destroy a workqueue worker
1771 * @worker: worker to be destroyed
1772 *
Tejun Heo706026c2013-01-24 11:01:34 -08001773 * Destroy @worker and adjust @pool stats accordingly.
Tejun Heoc8e55f32010-06-29 10:07:12 +02001774 *
1775 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001776 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001777 */
1778static void destroy_worker(struct worker *worker)
1779{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001780 struct worker_pool *pool = worker->pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001781 int id = worker->id;
1782
1783 /* sanity check frenzy */
Tejun Heo6183c002013-03-12 11:29:57 -07001784 if (WARN_ON(worker->current_work) ||
1785 WARN_ON(!list_empty(&worker->scheduled)))
1786 return;
Tejun Heoc34056a2010-06-29 10:07:11 +02001787
Tejun Heoc8e55f32010-06-29 10:07:12 +02001788 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001789 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001790 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001791 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001792
1793 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001794 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001795
Tejun Heod565ed62013-01-24 11:01:33 -08001796 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001797
Tejun Heoc34056a2010-06-29 10:07:11 +02001798 kthread_stop(worker->task);
1799 kfree(worker);
1800
Tejun Heod565ed62013-01-24 11:01:33 -08001801 spin_lock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001802 ida_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001803}
1804
Tejun Heo63d95a92012-07-12 14:46:37 -07001805static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001806{
Tejun Heo63d95a92012-07-12 14:46:37 -07001807 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001808
Tejun Heod565ed62013-01-24 11:01:33 -08001809 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001810
Tejun Heo63d95a92012-07-12 14:46:37 -07001811 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001812 struct worker *worker;
1813 unsigned long expires;
1814
1815 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001816 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001817 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1818
1819 if (time_before(jiffies, expires))
Tejun Heo63d95a92012-07-12 14:46:37 -07001820 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001821 else {
1822 /* it's been idle for too long, wake up manager */
Tejun Heo11ebea52012-07-12 14:46:37 -07001823 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -07001824 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001825 }
1826 }
1827
Tejun Heod565ed62013-01-24 11:01:33 -08001828 spin_unlock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001829}
1830
Tejun Heo493a1722013-03-12 11:29:59 -07001831static void send_mayday(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001832{
Tejun Heo112202d2013-02-13 19:29:12 -08001833 struct pool_workqueue *pwq = get_work_pwq(work);
1834 struct workqueue_struct *wq = pwq->wq;
Tejun Heo493a1722013-03-12 11:29:59 -07001835
1836 lockdep_assert_held(&workqueue_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001837
1838 if (!(wq->flags & WQ_RESCUER))
Tejun Heo493a1722013-03-12 11:29:59 -07001839 return;
Tejun Heoe22bee72010-06-29 10:07:14 +02001840
1841 /* mayday mayday mayday */
Tejun Heo493a1722013-03-12 11:29:59 -07001842 if (list_empty(&pwq->mayday_node)) {
1843 list_add_tail(&pwq->mayday_node, &wq->maydays);
Tejun Heoe22bee72010-06-29 10:07:14 +02001844 wake_up_process(wq->rescuer->task);
Tejun Heo493a1722013-03-12 11:29:59 -07001845 }
Tejun Heoe22bee72010-06-29 10:07:14 +02001846}
1847
Tejun Heo706026c2013-01-24 11:01:34 -08001848static void pool_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001849{
Tejun Heo63d95a92012-07-12 14:46:37 -07001850 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001851 struct work_struct *work;
1852
Tejun Heo493a1722013-03-12 11:29:59 -07001853 spin_lock_irq(&workqueue_lock); /* for wq->maydays */
1854 spin_lock(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001855
Tejun Heo63d95a92012-07-12 14:46:37 -07001856 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001857 /*
1858 * We've been trying to create a new worker but
1859 * haven't been successful. We might be hitting an
1860 * allocation deadlock. Send distress signals to
1861 * rescuers.
1862 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001863 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001864 send_mayday(work);
1865 }
1866
Tejun Heo493a1722013-03-12 11:29:59 -07001867 spin_unlock(&pool->lock);
1868 spin_unlock_irq(&workqueue_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001869
Tejun Heo63d95a92012-07-12 14:46:37 -07001870 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001871}
1872
1873/**
1874 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001875 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001876 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001877 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001878 * have at least one idle worker on return from this function. If
1879 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001880 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001881 * possible allocation deadlock.
1882 *
1883 * On return, need_to_create_worker() is guaranteed to be false and
1884 * may_start_working() true.
1885 *
1886 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001887 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001888 * multiple times. Does GFP_KERNEL allocations. Called only from
1889 * manager.
1890 *
1891 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08001892 * false if no action was taken and pool->lock stayed locked, true
Tejun Heoe22bee72010-06-29 10:07:14 +02001893 * otherwise.
1894 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001895static bool maybe_create_worker(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001896__releases(&pool->lock)
1897__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001898{
Tejun Heo63d95a92012-07-12 14:46:37 -07001899 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001900 return false;
1901restart:
Tejun Heod565ed62013-01-24 11:01:33 -08001902 spin_unlock_irq(&pool->lock);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001903
Tejun Heoe22bee72010-06-29 10:07:14 +02001904 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07001905 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001906
1907 while (true) {
1908 struct worker *worker;
1909
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001910 worker = create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001911 if (worker) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001912 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001913 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001914 start_worker(worker);
Tejun Heo6183c002013-03-12 11:29:57 -07001915 if (WARN_ON_ONCE(need_to_create_worker(pool)))
1916 goto restart;
Tejun Heoe22bee72010-06-29 10:07:14 +02001917 return true;
1918 }
1919
Tejun Heo63d95a92012-07-12 14:46:37 -07001920 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001921 break;
1922
Tejun Heoe22bee72010-06-29 10:07:14 +02001923 __set_current_state(TASK_INTERRUPTIBLE);
1924 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001925
Tejun Heo63d95a92012-07-12 14:46:37 -07001926 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001927 break;
1928 }
1929
Tejun Heo63d95a92012-07-12 14:46:37 -07001930 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001931 spin_lock_irq(&pool->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07001932 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001933 goto restart;
1934 return true;
1935}
1936
1937/**
1938 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo63d95a92012-07-12 14:46:37 -07001939 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001940 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001941 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02001942 * IDLE_WORKER_TIMEOUT.
1943 *
1944 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001945 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001946 * multiple times. Called only from manager.
1947 *
1948 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08001949 * false if no action was taken and pool->lock stayed locked, true
Tejun Heoe22bee72010-06-29 10:07:14 +02001950 * otherwise.
1951 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001952static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001953{
1954 bool ret = false;
1955
Tejun Heo63d95a92012-07-12 14:46:37 -07001956 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001957 struct worker *worker;
1958 unsigned long expires;
1959
Tejun Heo63d95a92012-07-12 14:46:37 -07001960 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001961 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1962
1963 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001964 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001965 break;
1966 }
1967
1968 destroy_worker(worker);
1969 ret = true;
1970 }
1971
1972 return ret;
1973}
1974
1975/**
1976 * manage_workers - manage worker pool
1977 * @worker: self
1978 *
Tejun Heo706026c2013-01-24 11:01:34 -08001979 * Assume the manager role and manage the worker pool @worker belongs
Tejun Heoe22bee72010-06-29 10:07:14 +02001980 * to. At any given time, there can be only zero or one manager per
Tejun Heo706026c2013-01-24 11:01:34 -08001981 * pool. The exclusion is handled automatically by this function.
Tejun Heoe22bee72010-06-29 10:07:14 +02001982 *
1983 * The caller can safely start processing works on false return. On
1984 * true return, it's guaranteed that need_to_create_worker() is false
1985 * and may_start_working() is true.
1986 *
1987 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001988 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001989 * multiple times. Does GFP_KERNEL allocations.
1990 *
1991 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08001992 * spin_lock_irq(pool->lock) which may be released and regrabbed
1993 * multiple times. Does GFP_KERNEL allocations.
Tejun Heoe22bee72010-06-29 10:07:14 +02001994 */
1995static bool manage_workers(struct worker *worker)
1996{
Tejun Heo63d95a92012-07-12 14:46:37 -07001997 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001998 bool ret = false;
1999
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002000 if (pool->flags & POOL_MANAGING_WORKERS)
Tejun Heoe22bee72010-06-29 10:07:14 +02002001 return ret;
2002
Lai Jiangshan552a37e2012-09-10 10:03:33 -07002003 pool->flags |= POOL_MANAGING_WORKERS;
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002004
2005 /*
2006 * To simplify both worker management and CPU hotplug, hold off
2007 * management while hotplug is in progress. CPU hotplug path can't
2008 * grab %POOL_MANAGING_WORKERS to achieve this because that can
2009 * lead to idle worker depletion (all become busy thinking someone
2010 * else is managing) which in turn can result in deadlock under
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002011 * extreme circumstances. Use @pool->assoc_mutex to synchronize
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002012 * manager against CPU hotplug.
2013 *
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002014 * assoc_mutex would always be free unless CPU hotplug is in
Tejun Heod565ed62013-01-24 11:01:33 -08002015 * progress. trylock first without dropping @pool->lock.
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002016 */
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002017 if (unlikely(!mutex_trylock(&pool->assoc_mutex))) {
Tejun Heod565ed62013-01-24 11:01:33 -08002018 spin_unlock_irq(&pool->lock);
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002019 mutex_lock(&pool->assoc_mutex);
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002020 /*
2021 * CPU hotplug could have happened while we were waiting
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002022 * for assoc_mutex. Hotplug itself can't handle us
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002023 * because manager isn't either on idle or busy list, and
Tejun Heo706026c2013-01-24 11:01:34 -08002024 * @pool's state and ours could have deviated.
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002025 *
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002026 * As hotplug is now excluded via assoc_mutex, we can
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002027 * simply try to bind. It will succeed or fail depending
Tejun Heo706026c2013-01-24 11:01:34 -08002028 * on @pool's current state. Try it and adjust
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002029 * %WORKER_UNBOUND accordingly.
2030 */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08002031 if (worker_maybe_bind_and_lock(pool))
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002032 worker->flags &= ~WORKER_UNBOUND;
2033 else
2034 worker->flags |= WORKER_UNBOUND;
2035
2036 ret = true;
2037 }
2038
Tejun Heo11ebea52012-07-12 14:46:37 -07002039 pool->flags &= ~POOL_MANAGE_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02002040
2041 /*
2042 * Destroy and then create so that may_start_working() is true
2043 * on return.
2044 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002045 ret |= maybe_destroy_workers(pool);
2046 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002047
Lai Jiangshan552a37e2012-09-10 10:03:33 -07002048 pool->flags &= ~POOL_MANAGING_WORKERS;
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002049 mutex_unlock(&pool->assoc_mutex);
Tejun Heoe22bee72010-06-29 10:07:14 +02002050 return ret;
2051}
2052
Tejun Heoa62428c2010-06-29 10:07:10 +02002053/**
2054 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02002055 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02002056 * @work: work to process
2057 *
2058 * Process @work. This function contains all the logics necessary to
2059 * process a single work including synchronization against and
2060 * interaction with other workers on the same cpu, queueing and
2061 * flushing. As long as context requirement is met, any worker can
2062 * call this function to process a work.
2063 *
2064 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002065 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02002066 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002067static void process_one_work(struct worker *worker, struct work_struct *work)
Tejun Heod565ed62013-01-24 11:01:33 -08002068__releases(&pool->lock)
2069__acquires(&pool->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02002070{
Tejun Heo112202d2013-02-13 19:29:12 -08002071 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002072 struct worker_pool *pool = worker->pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002073 bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02002074 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02002075 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02002076#ifdef CONFIG_LOCKDEP
2077 /*
2078 * It is permissible to free the struct work_struct from
2079 * inside the function that is called from it, this we need to
2080 * take into account for lockdep too. To avoid bogus "held
2081 * lock freed" warnings as well as problems when looking into
2082 * work->lockdep_map, make a copy and use that here.
2083 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07002084 struct lockdep_map lockdep_map;
2085
2086 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002087#endif
Tejun Heo6fec10a2012-07-22 10:16:34 -07002088 /*
2089 * Ensure we're on the correct CPU. DISASSOCIATED test is
2090 * necessary to avoid spurious warnings from rescuers servicing the
Tejun Heo24647572013-01-24 11:01:33 -08002091 * unbound or a disassociated pool.
Tejun Heo6fec10a2012-07-22 10:16:34 -07002092 */
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002093 WARN_ON_ONCE(!(worker->flags & WORKER_UNBOUND) &&
Tejun Heo24647572013-01-24 11:01:33 -08002094 !(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heoec22ca52013-01-24 11:01:33 -08002095 raw_smp_processor_id() != pool->cpu);
Tejun Heo25511a42012-07-17 12:39:27 -07002096
Tejun Heo7e116292010-06-29 10:07:13 +02002097 /*
2098 * A single work shouldn't be executed concurrently by
2099 * multiple workers on a single cpu. Check whether anyone is
2100 * already processing the work. If so, defer the work to the
2101 * currently executing one.
2102 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002103 collision = find_worker_executing_work(pool, work);
Tejun Heo7e116292010-06-29 10:07:13 +02002104 if (unlikely(collision)) {
2105 move_linked_works(work, &collision->scheduled, NULL);
2106 return;
2107 }
2108
Tejun Heo8930cab2012-08-03 10:30:45 -07002109 /* claim and dequeue */
Tejun Heoa62428c2010-06-29 10:07:10 +02002110 debug_work_deactivate(work);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002111 hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work);
Tejun Heoc34056a2010-06-29 10:07:11 +02002112 worker->current_work = work;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002113 worker->current_func = work->func;
Tejun Heo112202d2013-02-13 19:29:12 -08002114 worker->current_pwq = pwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002115 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002116
Tejun Heoa62428c2010-06-29 10:07:10 +02002117 list_del_init(&work->entry);
2118
Tejun Heo649027d2010-06-29 10:07:14 +02002119 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02002120 * CPU intensive works don't participate in concurrency
2121 * management. They're the scheduler's responsibility.
2122 */
2123 if (unlikely(cpu_intensive))
2124 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
2125
Tejun Heo974271c2012-07-12 14:46:37 -07002126 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002127 * Unbound pool isn't concurrency managed and work items should be
Tejun Heo974271c2012-07-12 14:46:37 -07002128 * executed ASAP. Wake up another worker if necessary.
2129 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002130 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
2131 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07002132
Tejun Heo8930cab2012-08-03 10:30:45 -07002133 /*
Tejun Heo7c3eed52013-01-24 11:01:33 -08002134 * Record the last pool and clear PENDING which should be the last
Tejun Heod565ed62013-01-24 11:01:33 -08002135 * update to @work. Also, do this inside @pool->lock so that
Tejun Heo23657bb2012-08-13 17:08:19 -07002136 * PENDING and queued state changes happen together while IRQ is
2137 * disabled.
Tejun Heo8930cab2012-08-03 10:30:45 -07002138 */
Tejun Heo7c3eed52013-01-24 11:01:33 -08002139 set_work_pool_and_clear_pending(work, pool->id);
Tejun Heoa62428c2010-06-29 10:07:10 +02002140
Tejun Heod565ed62013-01-24 11:01:33 -08002141 spin_unlock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002142
Tejun Heo112202d2013-02-13 19:29:12 -08002143 lock_map_acquire_read(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002144 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002145 trace_workqueue_execute_start(work);
Tejun Heoa2c1c572012-12-18 10:35:02 -08002146 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002147 /*
2148 * While we must be careful to not use "work" after this, the trace
2149 * point will only record its address.
2150 */
2151 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002152 lock_map_release(&lockdep_map);
Tejun Heo112202d2013-02-13 19:29:12 -08002153 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002154
2155 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Valentin Ilie044c7822012-08-19 00:52:42 +03002156 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
2157 " last function: %pf\n",
Tejun Heoa2c1c572012-12-18 10:35:02 -08002158 current->comm, preempt_count(), task_pid_nr(current),
2159 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02002160 debug_show_held_locks(current);
2161 dump_stack();
2162 }
2163
Tejun Heod565ed62013-01-24 11:01:33 -08002164 spin_lock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002165
Tejun Heofb0e7be2010-06-29 10:07:15 +02002166 /* clear cpu intensive status */
2167 if (unlikely(cpu_intensive))
2168 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2169
Tejun Heoa62428c2010-06-29 10:07:10 +02002170 /* we're done with it, release */
Sasha Levin42f85702012-12-17 10:01:23 -05002171 hash_del(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002172 worker->current_work = NULL;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002173 worker->current_func = NULL;
Tejun Heo112202d2013-02-13 19:29:12 -08002174 worker->current_pwq = NULL;
2175 pwq_dec_nr_in_flight(pwq, work_color);
Tejun Heoa62428c2010-06-29 10:07:10 +02002176}
2177
Tejun Heoaffee4b2010-06-29 10:07:12 +02002178/**
2179 * process_scheduled_works - process scheduled works
2180 * @worker: self
2181 *
2182 * Process all scheduled works. Please note that the scheduled list
2183 * may change while processing a work, so this function repeatedly
2184 * fetches a work from the top and executes it.
2185 *
2186 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002187 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02002188 * multiple times.
2189 */
2190static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002192 while (!list_empty(&worker->scheduled)) {
2193 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002195 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197}
2198
Tejun Heo4690c4a2010-06-29 10:07:10 +02002199/**
2200 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02002201 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02002202 *
Tejun Heo706026c2013-01-24 11:01:34 -08002203 * The worker thread function. There are NR_CPU_WORKER_POOLS dynamic pools
2204 * of these per each cpu. These workers process all works regardless of
Tejun Heoe22bee72010-06-29 10:07:14 +02002205 * their specific target workqueue. The only exception is works which
2206 * belong to workqueues with a rescuer which will be explained in
2207 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02002208 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002209static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210{
Tejun Heoc34056a2010-06-29 10:07:11 +02002211 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07002212 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
Tejun Heoe22bee72010-06-29 10:07:14 +02002214 /* tell the scheduler that this is a workqueue worker */
2215 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002216woke_up:
Tejun Heod565ed62013-01-24 11:01:33 -08002217 spin_lock_irq(&pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002219 /* we are off idle list if destruction or rebind is requested */
2220 if (unlikely(list_empty(&worker->entry))) {
Tejun Heod565ed62013-01-24 11:01:33 -08002221 spin_unlock_irq(&pool->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07002222
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002223 /* if DIE is set, destruction is requested */
Tejun Heo25511a42012-07-17 12:39:27 -07002224 if (worker->flags & WORKER_DIE) {
2225 worker->task->flags &= ~PF_WQ_WORKER;
2226 return 0;
2227 }
2228
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002229 /* otherwise, rebind */
Tejun Heo25511a42012-07-17 12:39:27 -07002230 idle_worker_rebind(worker);
2231 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 }
2233
Tejun Heoc8e55f32010-06-29 10:07:12 +02002234 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002235recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002236 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002237 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002238 goto sleep;
2239
2240 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002241 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002242 goto recheck;
2243
Tejun Heoc8e55f32010-06-29 10:07:12 +02002244 /*
2245 * ->scheduled list can only be filled while a worker is
2246 * preparing to process a work or actually processing it.
2247 * Make sure nobody diddled with it while I was sleeping.
2248 */
Tejun Heo6183c002013-03-12 11:29:57 -07002249 WARN_ON_ONCE(!list_empty(&worker->scheduled));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002250
Tejun Heoe22bee72010-06-29 10:07:14 +02002251 /*
2252 * When control reaches this point, we're guaranteed to have
2253 * at least one idle worker or that someone else has already
2254 * assumed the manager role.
2255 */
2256 worker_clr_flags(worker, WORKER_PREP);
2257
2258 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002259 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002260 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002261 struct work_struct, entry);
2262
2263 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2264 /* optimization path, not strictly necessary */
2265 process_one_work(worker, work);
2266 if (unlikely(!list_empty(&worker->scheduled)))
2267 process_scheduled_works(worker);
2268 } else {
2269 move_linked_works(work, &worker->scheduled, NULL);
2270 process_scheduled_works(worker);
2271 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002272 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002273
Tejun Heoe22bee72010-06-29 10:07:14 +02002274 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002275sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07002276 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002277 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002278
Tejun Heoc8e55f32010-06-29 10:07:12 +02002279 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002280 * pool->lock is held and there's no work to process and no need to
2281 * manage, sleep. Workers are woken up only while holding
2282 * pool->lock or from local cpu, so setting the current state
2283 * before releasing pool->lock is enough to prevent losing any
2284 * event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002285 */
2286 worker_enter_idle(worker);
2287 __set_current_state(TASK_INTERRUPTIBLE);
Tejun Heod565ed62013-01-24 11:01:33 -08002288 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02002289 schedule();
2290 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291}
2292
Tejun Heoe22bee72010-06-29 10:07:14 +02002293/**
2294 * rescuer_thread - the rescuer thread function
Tejun Heo111c2252013-01-17 17:16:24 -08002295 * @__rescuer: self
Tejun Heoe22bee72010-06-29 10:07:14 +02002296 *
2297 * Workqueue rescuer thread function. There's one rescuer for each
2298 * workqueue which has WQ_RESCUER set.
2299 *
Tejun Heo706026c2013-01-24 11:01:34 -08002300 * Regular work processing on a pool may block trying to create a new
Tejun Heoe22bee72010-06-29 10:07:14 +02002301 * worker which uses GFP_KERNEL allocation which has slight chance of
2302 * developing into deadlock if some works currently on the same queue
2303 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2304 * the problem rescuer solves.
2305 *
Tejun Heo706026c2013-01-24 11:01:34 -08002306 * When such condition is possible, the pool summons rescuers of all
2307 * workqueues which have works queued on the pool and let them process
Tejun Heoe22bee72010-06-29 10:07:14 +02002308 * those works so that forward progress can be guaranteed.
2309 *
2310 * This should happen rarely.
2311 */
Tejun Heo111c2252013-01-17 17:16:24 -08002312static int rescuer_thread(void *__rescuer)
Tejun Heoe22bee72010-06-29 10:07:14 +02002313{
Tejun Heo111c2252013-01-17 17:16:24 -08002314 struct worker *rescuer = __rescuer;
2315 struct workqueue_struct *wq = rescuer->rescue_wq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002316 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heoe22bee72010-06-29 10:07:14 +02002317
2318 set_user_nice(current, RESCUER_NICE_LEVEL);
Tejun Heo111c2252013-01-17 17:16:24 -08002319
2320 /*
2321 * Mark rescuer as worker too. As WORKER_PREP is never cleared, it
2322 * doesn't participate in concurrency management.
2323 */
2324 rescuer->task->flags |= PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002325repeat:
2326 set_current_state(TASK_INTERRUPTIBLE);
2327
Mike Galbraith412d32e2012-11-28 07:17:18 +01002328 if (kthread_should_stop()) {
2329 __set_current_state(TASK_RUNNING);
Tejun Heo111c2252013-01-17 17:16:24 -08002330 rescuer->task->flags &= ~PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002331 return 0;
Mike Galbraith412d32e2012-11-28 07:17:18 +01002332 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002333
Tejun Heo493a1722013-03-12 11:29:59 -07002334 /* see whether any pwq is asking for help */
2335 spin_lock_irq(&workqueue_lock);
2336
2337 while (!list_empty(&wq->maydays)) {
2338 struct pool_workqueue *pwq = list_first_entry(&wq->maydays,
2339 struct pool_workqueue, mayday_node);
Tejun Heo112202d2013-02-13 19:29:12 -08002340 struct worker_pool *pool = pwq->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002341 struct work_struct *work, *n;
2342
2343 __set_current_state(TASK_RUNNING);
Tejun Heo493a1722013-03-12 11:29:59 -07002344 list_del_init(&pwq->mayday_node);
2345
2346 spin_unlock_irq(&workqueue_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002347
2348 /* migrate to the target cpu if possible */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08002349 worker_maybe_bind_and_lock(pool);
Lai Jiangshanb3104102013-02-19 12:17:02 -08002350 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002351
2352 /*
2353 * Slurp in all works issued via this workqueue and
2354 * process'em.
2355 */
Tejun Heo6183c002013-03-12 11:29:57 -07002356 WARN_ON_ONCE(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002357 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heo112202d2013-02-13 19:29:12 -08002358 if (get_work_pwq(work) == pwq)
Tejun Heoe22bee72010-06-29 10:07:14 +02002359 move_linked_works(work, scheduled, &n);
2360
2361 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002362
2363 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002364 * Leave this pool. If keep_working() is %true, notify a
Tejun Heo75769582011-02-14 14:04:46 +01002365 * regular worker; otherwise, we end up with 0 concurrency
2366 * and stalling the execution.
2367 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002368 if (keep_working(pool))
2369 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002370
Lai Jiangshanb3104102013-02-19 12:17:02 -08002371 rescuer->pool = NULL;
Tejun Heo493a1722013-03-12 11:29:59 -07002372 spin_unlock(&pool->lock);
2373 spin_lock(&workqueue_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002374 }
2375
Tejun Heo493a1722013-03-12 11:29:59 -07002376 spin_unlock_irq(&workqueue_lock);
2377
Tejun Heo111c2252013-01-17 17:16:24 -08002378 /* rescuers should never participate in concurrency management */
2379 WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
Tejun Heoe22bee72010-06-29 10:07:14 +02002380 schedule();
2381 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382}
2383
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002384struct wq_barrier {
2385 struct work_struct work;
2386 struct completion done;
2387};
2388
2389static void wq_barrier_func(struct work_struct *work)
2390{
2391 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2392 complete(&barr->done);
2393}
2394
Tejun Heo4690c4a2010-06-29 10:07:10 +02002395/**
2396 * insert_wq_barrier - insert a barrier work
Tejun Heo112202d2013-02-13 19:29:12 -08002397 * @pwq: pwq to insert barrier into
Tejun Heo4690c4a2010-06-29 10:07:10 +02002398 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002399 * @target: target work to attach @barr to
2400 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002401 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002402 * @barr is linked to @target such that @barr is completed only after
2403 * @target finishes execution. Please note that the ordering
2404 * guarantee is observed only with respect to @target and on the local
2405 * cpu.
2406 *
2407 * Currently, a queued barrier can't be canceled. This is because
2408 * try_to_grab_pending() can't determine whether the work to be
2409 * grabbed is at the head of the queue and thus can't clear LINKED
2410 * flag of the previous work while there must be a valid next work
2411 * after a work with LINKED flag set.
2412 *
2413 * Note that when @worker is non-NULL, @target may be modified
Tejun Heo112202d2013-02-13 19:29:12 -08002414 * underneath us, so we can't reliably determine pwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002415 *
2416 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002417 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002418 */
Tejun Heo112202d2013-02-13 19:29:12 -08002419static void insert_wq_barrier(struct pool_workqueue *pwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002420 struct wq_barrier *barr,
2421 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002422{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002423 struct list_head *head;
2424 unsigned int linked = 0;
2425
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002426 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002427 * debugobject calls are safe here even with pool->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002428 * as we know for sure that this will not trigger any of the
2429 * checks and call back into the fixup functions where we
2430 * might deadlock.
2431 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002432 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002433 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002434 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002435
Tejun Heoaffee4b2010-06-29 10:07:12 +02002436 /*
2437 * If @target is currently being executed, schedule the
2438 * barrier to the worker; otherwise, put it after @target.
2439 */
2440 if (worker)
2441 head = worker->scheduled.next;
2442 else {
2443 unsigned long *bits = work_data_bits(target);
2444
2445 head = target->entry.next;
2446 /* there can already be other linked works, inherit and set */
2447 linked = *bits & WORK_STRUCT_LINKED;
2448 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2449 }
2450
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002451 debug_work_activate(&barr->work);
Tejun Heo112202d2013-02-13 19:29:12 -08002452 insert_work(pwq, &barr->work, head,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002453 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002454}
2455
Tejun Heo73f53c42010-06-29 10:07:11 +02002456/**
Tejun Heo112202d2013-02-13 19:29:12 -08002457 * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing
Tejun Heo73f53c42010-06-29 10:07:11 +02002458 * @wq: workqueue being flushed
2459 * @flush_color: new flush color, < 0 for no-op
2460 * @work_color: new work color, < 0 for no-op
2461 *
Tejun Heo112202d2013-02-13 19:29:12 -08002462 * Prepare pwqs for workqueue flushing.
Tejun Heo73f53c42010-06-29 10:07:11 +02002463 *
Tejun Heo112202d2013-02-13 19:29:12 -08002464 * If @flush_color is non-negative, flush_color on all pwqs should be
2465 * -1. If no pwq has in-flight commands at the specified color, all
2466 * pwq->flush_color's stay at -1 and %false is returned. If any pwq
2467 * has in flight commands, its pwq->flush_color is set to
2468 * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq
Tejun Heo73f53c42010-06-29 10:07:11 +02002469 * wakeup logic is armed and %true is returned.
2470 *
2471 * The caller should have initialized @wq->first_flusher prior to
2472 * calling this function with non-negative @flush_color. If
2473 * @flush_color is negative, no flush color update is done and %false
2474 * is returned.
2475 *
Tejun Heo112202d2013-02-13 19:29:12 -08002476 * If @work_color is non-negative, all pwqs should have the same
Tejun Heo73f53c42010-06-29 10:07:11 +02002477 * work_color which is previous to @work_color and all will be
2478 * advanced to @work_color.
2479 *
2480 * CONTEXT:
2481 * mutex_lock(wq->flush_mutex).
2482 *
2483 * RETURNS:
2484 * %true if @flush_color >= 0 and there's something to flush. %false
2485 * otherwise.
2486 */
Tejun Heo112202d2013-02-13 19:29:12 -08002487static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
Tejun Heo73f53c42010-06-29 10:07:11 +02002488 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489{
Tejun Heo73f53c42010-06-29 10:07:11 +02002490 bool wait = false;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002491 struct pool_workqueue *pwq;
Oleg Nesterov14441962007-05-23 13:57:57 -07002492
Tejun Heo73f53c42010-06-29 10:07:11 +02002493 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002494 WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush));
Tejun Heo112202d2013-02-13 19:29:12 -08002495 atomic_set(&wq->nr_pwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002496 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002497
Tejun Heo49e3cf42013-03-12 11:29:58 -07002498 for_each_pwq(pwq, wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08002499 struct worker_pool *pool = pwq->pool;
Tejun Heo73f53c42010-06-29 10:07:11 +02002500
Tejun Heod565ed62013-01-24 11:01:33 -08002501 spin_lock_irq(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002502
2503 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002504 WARN_ON_ONCE(pwq->flush_color != -1);
Tejun Heo73f53c42010-06-29 10:07:11 +02002505
Tejun Heo112202d2013-02-13 19:29:12 -08002506 if (pwq->nr_in_flight[flush_color]) {
2507 pwq->flush_color = flush_color;
2508 atomic_inc(&wq->nr_pwqs_to_flush);
Tejun Heo73f53c42010-06-29 10:07:11 +02002509 wait = true;
2510 }
2511 }
2512
2513 if (work_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002514 WARN_ON_ONCE(work_color != work_next_color(pwq->work_color));
Tejun Heo112202d2013-02-13 19:29:12 -08002515 pwq->work_color = work_color;
Tejun Heo73f53c42010-06-29 10:07:11 +02002516 }
2517
Tejun Heod565ed62013-01-24 11:01:33 -08002518 spin_unlock_irq(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002519 }
2520
Tejun Heo112202d2013-02-13 19:29:12 -08002521 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush))
Tejun Heo73f53c42010-06-29 10:07:11 +02002522 complete(&wq->first_flusher->done);
2523
2524 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525}
2526
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002527/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002529 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 *
2531 * Forces execution of the workqueue and blocks until its completion.
2532 * This is typically used in driver shutdown handlers.
2533 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002534 * We sleep until all works which were queued on entry have been handled,
2535 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002537void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538{
Tejun Heo73f53c42010-06-29 10:07:11 +02002539 struct wq_flusher this_flusher = {
2540 .list = LIST_HEAD_INIT(this_flusher.list),
2541 .flush_color = -1,
2542 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2543 };
2544 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002545
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002546 lock_map_acquire(&wq->lockdep_map);
2547 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002548
2549 mutex_lock(&wq->flush_mutex);
2550
2551 /*
2552 * Start-to-wait phase
2553 */
2554 next_color = work_next_color(wq->work_color);
2555
2556 if (next_color != wq->flush_color) {
2557 /*
2558 * Color space is not full. The current work_color
2559 * becomes our flush_color and work_color is advanced
2560 * by one.
2561 */
Tejun Heo6183c002013-03-12 11:29:57 -07002562 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow));
Tejun Heo73f53c42010-06-29 10:07:11 +02002563 this_flusher.flush_color = wq->work_color;
2564 wq->work_color = next_color;
2565
2566 if (!wq->first_flusher) {
2567 /* no flush in progress, become the first flusher */
Tejun Heo6183c002013-03-12 11:29:57 -07002568 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002569
2570 wq->first_flusher = &this_flusher;
2571
Tejun Heo112202d2013-02-13 19:29:12 -08002572 if (!flush_workqueue_prep_pwqs(wq, wq->flush_color,
Tejun Heo73f53c42010-06-29 10:07:11 +02002573 wq->work_color)) {
2574 /* nothing to flush, done */
2575 wq->flush_color = next_color;
2576 wq->first_flusher = NULL;
2577 goto out_unlock;
2578 }
2579 } else {
2580 /* wait in queue */
Tejun Heo6183c002013-03-12 11:29:57 -07002581 WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002582 list_add_tail(&this_flusher.list, &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002583 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002584 }
2585 } else {
2586 /*
2587 * Oops, color space is full, wait on overflow queue.
2588 * The next flush completion will assign us
2589 * flush_color and transfer to flusher_queue.
2590 */
2591 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2592 }
2593
2594 mutex_unlock(&wq->flush_mutex);
2595
2596 wait_for_completion(&this_flusher.done);
2597
2598 /*
2599 * Wake-up-and-cascade phase
2600 *
2601 * First flushers are responsible for cascading flushes and
2602 * handling overflow. Non-first flushers can simply return.
2603 */
2604 if (wq->first_flusher != &this_flusher)
2605 return;
2606
2607 mutex_lock(&wq->flush_mutex);
2608
Tejun Heo4ce48b32010-07-02 10:03:51 +02002609 /* we might have raced, check again with mutex held */
2610 if (wq->first_flusher != &this_flusher)
2611 goto out_unlock;
2612
Tejun Heo73f53c42010-06-29 10:07:11 +02002613 wq->first_flusher = NULL;
2614
Tejun Heo6183c002013-03-12 11:29:57 -07002615 WARN_ON_ONCE(!list_empty(&this_flusher.list));
2616 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002617
2618 while (true) {
2619 struct wq_flusher *next, *tmp;
2620
2621 /* complete all the flushers sharing the current flush color */
2622 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2623 if (next->flush_color != wq->flush_color)
2624 break;
2625 list_del_init(&next->list);
2626 complete(&next->done);
2627 }
2628
Tejun Heo6183c002013-03-12 11:29:57 -07002629 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) &&
2630 wq->flush_color != work_next_color(wq->work_color));
Tejun Heo73f53c42010-06-29 10:07:11 +02002631
2632 /* this flush_color is finished, advance by one */
2633 wq->flush_color = work_next_color(wq->flush_color);
2634
2635 /* one color has been freed, handle overflow queue */
2636 if (!list_empty(&wq->flusher_overflow)) {
2637 /*
2638 * Assign the same color to all overflowed
2639 * flushers, advance work_color and append to
2640 * flusher_queue. This is the start-to-wait
2641 * phase for these overflowed flushers.
2642 */
2643 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2644 tmp->flush_color = wq->work_color;
2645
2646 wq->work_color = work_next_color(wq->work_color);
2647
2648 list_splice_tail_init(&wq->flusher_overflow,
2649 &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002650 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002651 }
2652
2653 if (list_empty(&wq->flusher_queue)) {
Tejun Heo6183c002013-03-12 11:29:57 -07002654 WARN_ON_ONCE(wq->flush_color != wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002655 break;
2656 }
2657
2658 /*
2659 * Need to flush more colors. Make the next flusher
Tejun Heo112202d2013-02-13 19:29:12 -08002660 * the new first flusher and arm pwqs.
Tejun Heo73f53c42010-06-29 10:07:11 +02002661 */
Tejun Heo6183c002013-03-12 11:29:57 -07002662 WARN_ON_ONCE(wq->flush_color == wq->work_color);
2663 WARN_ON_ONCE(wq->flush_color != next->flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002664
2665 list_del_init(&next->list);
2666 wq->first_flusher = next;
2667
Tejun Heo112202d2013-02-13 19:29:12 -08002668 if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1))
Tejun Heo73f53c42010-06-29 10:07:11 +02002669 break;
2670
2671 /*
2672 * Meh... this color is already done, clear first
2673 * flusher and repeat cascading.
2674 */
2675 wq->first_flusher = NULL;
2676 }
2677
2678out_unlock:
2679 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680}
Dave Jonesae90dd52006-06-30 01:40:45 -04002681EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002683/**
2684 * drain_workqueue - drain a workqueue
2685 * @wq: workqueue to drain
2686 *
2687 * Wait until the workqueue becomes empty. While draining is in progress,
2688 * only chain queueing is allowed. IOW, only currently pending or running
2689 * work items on @wq can queue further work items on it. @wq is flushed
2690 * repeatedly until it becomes empty. The number of flushing is detemined
2691 * by the depth of chaining and should be relatively short. Whine if it
2692 * takes too long.
2693 */
2694void drain_workqueue(struct workqueue_struct *wq)
2695{
2696 unsigned int flush_cnt = 0;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002697 struct pool_workqueue *pwq;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002698
2699 /*
2700 * __queue_work() needs to test whether there are drainers, is much
2701 * hotter than drain_workqueue() and already looks at @wq->flags.
2702 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2703 */
Tejun Heoe98d5b12013-03-12 11:29:57 -07002704 spin_lock_irq(&workqueue_lock);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002705 if (!wq->nr_drainers++)
2706 wq->flags |= WQ_DRAINING;
Tejun Heoe98d5b12013-03-12 11:29:57 -07002707 spin_unlock_irq(&workqueue_lock);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002708reflush:
2709 flush_workqueue(wq);
2710
Tejun Heo49e3cf42013-03-12 11:29:58 -07002711 for_each_pwq(pwq, wq) {
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002712 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002713
Tejun Heo112202d2013-02-13 19:29:12 -08002714 spin_lock_irq(&pwq->pool->lock);
2715 drained = !pwq->nr_active && list_empty(&pwq->delayed_works);
2716 spin_unlock_irq(&pwq->pool->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002717
2718 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002719 continue;
2720
2721 if (++flush_cnt == 10 ||
2722 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
Valentin Ilie044c7822012-08-19 00:52:42 +03002723 pr_warn("workqueue %s: flush on destruction isn't complete after %u tries\n",
2724 wq->name, flush_cnt);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002725 goto reflush;
2726 }
2727
Tejun Heoe98d5b12013-03-12 11:29:57 -07002728 spin_lock_irq(&workqueue_lock);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002729 if (!--wq->nr_drainers)
2730 wq->flags &= ~WQ_DRAINING;
Tejun Heoe98d5b12013-03-12 11:29:57 -07002731 spin_unlock_irq(&workqueue_lock);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002732}
2733EXPORT_SYMBOL_GPL(drain_workqueue);
2734
Tejun Heo606a5022012-08-20 14:51:23 -07002735static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
Tejun Heobaf59022010-09-16 10:42:16 +02002736{
2737 struct worker *worker = NULL;
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002738 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002739 struct pool_workqueue *pwq;
Tejun Heobaf59022010-09-16 10:42:16 +02002740
2741 might_sleep();
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002742 pool = get_work_pool(work);
2743 if (!pool)
Tejun Heobaf59022010-09-16 10:42:16 +02002744 return false;
2745
Tejun Heod565ed62013-01-24 11:01:33 -08002746 spin_lock_irq(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08002747 /* see the comment in try_to_grab_pending() with the same code */
Tejun Heo112202d2013-02-13 19:29:12 -08002748 pwq = get_work_pwq(work);
2749 if (pwq) {
2750 if (unlikely(pwq->pool != pool))
Tejun Heobaf59022010-09-16 10:42:16 +02002751 goto already_gone;
Tejun Heo606a5022012-08-20 14:51:23 -07002752 } else {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002753 worker = find_worker_executing_work(pool, work);
Tejun Heobaf59022010-09-16 10:42:16 +02002754 if (!worker)
2755 goto already_gone;
Tejun Heo112202d2013-02-13 19:29:12 -08002756 pwq = worker->current_pwq;
Tejun Heo606a5022012-08-20 14:51:23 -07002757 }
Tejun Heobaf59022010-09-16 10:42:16 +02002758
Tejun Heo112202d2013-02-13 19:29:12 -08002759 insert_wq_barrier(pwq, barr, work, worker);
Tejun Heod565ed62013-01-24 11:01:33 -08002760 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002761
Tejun Heoe1594892011-01-09 23:32:15 +01002762 /*
2763 * If @max_active is 1 or rescuer is in use, flushing another work
2764 * item on the same workqueue may lead to deadlock. Make sure the
2765 * flusher is not running on the same workqueue by verifying write
2766 * access.
2767 */
Tejun Heo112202d2013-02-13 19:29:12 -08002768 if (pwq->wq->saved_max_active == 1 || pwq->wq->flags & WQ_RESCUER)
2769 lock_map_acquire(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002770 else
Tejun Heo112202d2013-02-13 19:29:12 -08002771 lock_map_acquire_read(&pwq->wq->lockdep_map);
2772 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002773
Tejun Heobaf59022010-09-16 10:42:16 +02002774 return true;
2775already_gone:
Tejun Heod565ed62013-01-24 11:01:33 -08002776 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002777 return false;
2778}
2779
Oleg Nesterovdb700892008-07-25 01:47:49 -07002780/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002781 * flush_work - wait for a work to finish executing the last queueing instance
2782 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002783 *
Tejun Heo606a5022012-08-20 14:51:23 -07002784 * Wait until @work has finished execution. @work is guaranteed to be idle
2785 * on return if it hasn't been requeued since flush started.
Tejun Heo401a8d02010-09-16 10:36:00 +02002786 *
2787 * RETURNS:
2788 * %true if flush_work() waited for the work to finish execution,
2789 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002790 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002791bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002792{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002793 struct wq_barrier barr;
2794
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002795 lock_map_acquire(&work->lockdep_map);
2796 lock_map_release(&work->lockdep_map);
2797
Tejun Heo606a5022012-08-20 14:51:23 -07002798 if (start_flush_work(work, &barr)) {
Tejun Heobaf59022010-09-16 10:42:16 +02002799 wait_for_completion(&barr.done);
2800 destroy_work_on_stack(&barr.work);
2801 return true;
Tejun Heo606a5022012-08-20 14:51:23 -07002802 } else {
Tejun Heobaf59022010-09-16 10:42:16 +02002803 return false;
Tejun Heo606a5022012-08-20 14:51:23 -07002804 }
Oleg Nesterovdb700892008-07-25 01:47:49 -07002805}
2806EXPORT_SYMBOL_GPL(flush_work);
2807
Tejun Heo36e227d2012-08-03 10:30:46 -07002808static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
Tejun Heo401a8d02010-09-16 10:36:00 +02002809{
Tejun Heobbb68df2012-08-03 10:30:46 -07002810 unsigned long flags;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002811 int ret;
2812
2813 do {
Tejun Heobbb68df2012-08-03 10:30:46 -07002814 ret = try_to_grab_pending(work, is_dwork, &flags);
2815 /*
2816 * If someone else is canceling, wait for the same event it
2817 * would be waiting for before retrying.
2818 */
2819 if (unlikely(ret == -ENOENT))
Tejun Heo606a5022012-08-20 14:51:23 -07002820 flush_work(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002821 } while (unlikely(ret < 0));
2822
Tejun Heobbb68df2012-08-03 10:30:46 -07002823 /* tell other tasks trying to grab @work to back off */
2824 mark_work_canceling(work);
2825 local_irq_restore(flags);
2826
Tejun Heo606a5022012-08-20 14:51:23 -07002827 flush_work(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002828 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002829 return ret;
2830}
2831
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002832/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002833 * cancel_work_sync - cancel a work and wait for it to finish
2834 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002835 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002836 * Cancel @work and wait for its execution to finish. This function
2837 * can be used even if the work re-queues itself or migrates to
2838 * another workqueue. On return from this function, @work is
2839 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002840 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002841 * cancel_work_sync(&delayed_work->work) must not be used for
2842 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002843 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002844 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002845 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002846 *
2847 * RETURNS:
2848 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002849 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002850bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002851{
Tejun Heo36e227d2012-08-03 10:30:46 -07002852 return __cancel_work_timer(work, false);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002853}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002854EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002855
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002856/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002857 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2858 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002859 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002860 * Delayed timer is cancelled and the pending work is queued for
2861 * immediate execution. Like flush_work(), this function only
2862 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002863 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002864 * RETURNS:
2865 * %true if flush_work() waited for the work to finish execution,
2866 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002867 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002868bool flush_delayed_work(struct delayed_work *dwork)
2869{
Tejun Heo8930cab2012-08-03 10:30:45 -07002870 local_irq_disable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002871 if (del_timer_sync(&dwork->timer))
Lai Jiangshan60c057b2013-02-06 18:04:53 -08002872 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Tejun Heo8930cab2012-08-03 10:30:45 -07002873 local_irq_enable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002874 return flush_work(&dwork->work);
2875}
2876EXPORT_SYMBOL(flush_delayed_work);
2877
2878/**
Tejun Heo57b30ae2012-08-21 13:18:24 -07002879 * cancel_delayed_work - cancel a delayed work
2880 * @dwork: delayed_work to cancel
Tejun Heo09383492010-09-16 10:48:29 +02002881 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002882 * Kill off a pending delayed_work. Returns %true if @dwork was pending
2883 * and canceled; %false if wasn't pending. Note that the work callback
2884 * function may still be running on return, unless it returns %true and the
2885 * work doesn't re-arm itself. Explicitly flush or use
2886 * cancel_delayed_work_sync() to wait on it.
Tejun Heo09383492010-09-16 10:48:29 +02002887 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002888 * This function is safe to call from any context including IRQ handler.
Tejun Heo09383492010-09-16 10:48:29 +02002889 */
Tejun Heo57b30ae2012-08-21 13:18:24 -07002890bool cancel_delayed_work(struct delayed_work *dwork)
Tejun Heo09383492010-09-16 10:48:29 +02002891{
Tejun Heo57b30ae2012-08-21 13:18:24 -07002892 unsigned long flags;
2893 int ret;
2894
2895 do {
2896 ret = try_to_grab_pending(&dwork->work, true, &flags);
2897 } while (unlikely(ret == -EAGAIN));
2898
2899 if (unlikely(ret < 0))
2900 return false;
2901
Tejun Heo7c3eed52013-01-24 11:01:33 -08002902 set_work_pool_and_clear_pending(&dwork->work,
2903 get_work_pool_id(&dwork->work));
Tejun Heo57b30ae2012-08-21 13:18:24 -07002904 local_irq_restore(flags);
Dan Magenheimerc0158ca2012-10-18 16:31:37 -07002905 return ret;
Tejun Heo09383492010-09-16 10:48:29 +02002906}
Tejun Heo57b30ae2012-08-21 13:18:24 -07002907EXPORT_SYMBOL(cancel_delayed_work);
Tejun Heo09383492010-09-16 10:48:29 +02002908
2909/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002910 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2911 * @dwork: the delayed work cancel
2912 *
2913 * This is cancel_work_sync() for delayed works.
2914 *
2915 * RETURNS:
2916 * %true if @dwork was pending, %false otherwise.
2917 */
2918bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002919{
Tejun Heo36e227d2012-08-03 10:30:46 -07002920 return __cancel_work_timer(&dwork->work, true);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002921}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002922EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002924/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07002925 * schedule_work_on - put work task on a specific cpu
2926 * @cpu: cpu to put the work task on
2927 * @work: job to be done
2928 *
2929 * This puts a job on a specific cpu
2930 */
Tejun Heod4283e92012-08-03 10:30:44 -07002931bool schedule_work_on(int cpu, struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07002932{
Tejun Heod320c032010-06-29 10:07:14 +02002933 return queue_work_on(cpu, system_wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07002934}
2935EXPORT_SYMBOL(schedule_work_on);
2936
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002937/**
Dave Jonesae90dd52006-06-30 01:40:45 -04002938 * schedule_work - put work task in global workqueue
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 * @work: job to be done
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 *
Tejun Heod4283e92012-08-03 10:30:44 -07002941 * Returns %false if @work was already on the kernel-global workqueue and
2942 * %true otherwise.
David Howells52bad642006-11-22 14:54:01 +00002943 *
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002944 * This puts a job in the kernel-global workqueue if it was not already
2945 * queued and leaves it in the same position on the kernel-global
2946 * workqueue otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 */
Tejun Heod4283e92012-08-03 10:30:44 -07002948bool schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949{
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002950 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951}
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002952EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002954/**
2955 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
2956 * @cpu: cpu to use
2957 * @dwork: job to be done
2958 * @delay: number of jiffies to wait
2959 *
2960 * After waiting for a given time this puts a job in the kernel-global
2961 * workqueue on the specified CPU.
2962 */
Tejun Heod4283e92012-08-03 10:30:44 -07002963bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork,
2964 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965{
Tejun Heod320c032010-06-29 10:07:14 +02002966 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967}
Dave Jonesae90dd52006-06-30 01:40:45 -04002968EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
Andrew Mortonb6136772006-06-25 05:47:49 -07002970/**
Tejun Heo0a13c002012-08-03 10:30:44 -07002971 * schedule_delayed_work - put work task in global workqueue after delay
2972 * @dwork: job to be done
2973 * @delay: number of jiffies to wait or 0 for immediate execution
2974 *
2975 * After waiting for a given time this puts a job in the kernel-global
2976 * workqueue.
2977 */
Tejun Heod4283e92012-08-03 10:30:44 -07002978bool schedule_delayed_work(struct delayed_work *dwork, unsigned long delay)
Tejun Heo0a13c002012-08-03 10:30:44 -07002979{
2980 return queue_delayed_work(system_wq, dwork, delay);
2981}
2982EXPORT_SYMBOL(schedule_delayed_work);
2983
2984/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002985 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002986 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002987 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002988 * schedule_on_each_cpu() executes @func on each online CPU using the
2989 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002990 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002991 *
2992 * RETURNS:
2993 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002994 */
David Howells65f27f32006-11-22 14:55:48 +00002995int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002996{
2997 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002998 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002999
Andrew Mortonb6136772006-06-25 05:47:49 -07003000 works = alloc_percpu(struct work_struct);
3001 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08003002 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07003003
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003004 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08003005
Christoph Lameter15316ba2006-01-08 01:00:43 -08003006 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01003007 struct work_struct *work = per_cpu_ptr(works, cpu);
3008
3009 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003010 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02003011 }
Tejun Heo93981802009-11-17 14:06:20 -08003012
3013 for_each_online_cpu(cpu)
3014 flush_work(per_cpu_ptr(works, cpu));
3015
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003016 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07003017 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08003018 return 0;
3019}
3020
Alan Sterneef6a7d2010-02-12 17:39:21 +09003021/**
3022 * flush_scheduled_work - ensure that any scheduled work has run to completion.
3023 *
3024 * Forces execution of the kernel-global workqueue and blocks until its
3025 * completion.
3026 *
3027 * Think twice before calling this function! It's very easy to get into
3028 * trouble if you don't take great care. Either of the following situations
3029 * will lead to deadlock:
3030 *
3031 * One of the work items currently on the workqueue needs to acquire
3032 * a lock held by your code or its caller.
3033 *
3034 * Your code is running in the context of a work routine.
3035 *
3036 * They will be detected by lockdep when they occur, but the first might not
3037 * occur very often. It depends on what work items are on the workqueue and
3038 * what locks they need, which you have no control over.
3039 *
3040 * In most situations flushing the entire workqueue is overkill; you merely
3041 * need to know that a particular work item isn't queued and isn't running.
3042 * In such cases you should use cancel_delayed_work_sync() or
3043 * cancel_work_sync() instead.
3044 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045void flush_scheduled_work(void)
3046{
Tejun Heod320c032010-06-29 10:07:14 +02003047 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048}
Dave Jonesae90dd52006-06-30 01:40:45 -04003049EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
3051/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06003052 * execute_in_process_context - reliably execute the routine with user context
3053 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06003054 * @ew: guaranteed storage for the execute work structure (must
3055 * be available when the work executes)
3056 *
3057 * Executes the function immediately if process context is available,
3058 * otherwise schedules the function for delayed execution.
3059 *
3060 * Returns: 0 - function was executed
3061 * 1 - function was scheduled for execution
3062 */
David Howells65f27f32006-11-22 14:55:48 +00003063int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06003064{
3065 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00003066 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003067 return 0;
3068 }
3069
David Howells65f27f32006-11-22 14:55:48 +00003070 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003071 schedule_work(&ew->work);
3072
3073 return 1;
3074}
3075EXPORT_SYMBOL_GPL(execute_in_process_context);
3076
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077int keventd_up(void)
3078{
Tejun Heod320c032010-06-29 10:07:14 +02003079 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080}
3081
Tejun Heo30cdf242013-03-12 11:29:57 -07003082static int alloc_and_link_pwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003084 bool highpri = wq->flags & WQ_HIGHPRI;
Tejun Heo30cdf242013-03-12 11:29:57 -07003085 int cpu;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01003086
Tejun Heo30cdf242013-03-12 11:29:57 -07003087 if (!(wq->flags & WQ_UNBOUND)) {
3088 wq->pool_wq.pcpu = alloc_percpu(struct pool_workqueue);
3089 if (!wq->pool_wq.pcpu)
3090 return -ENOMEM;
3091
3092 for_each_possible_cpu(cpu) {
3093 struct pool_workqueue *pwq = get_pwq(cpu, wq);
3094
Tejun Heo49e3cf42013-03-12 11:29:58 -07003095 pwq->pool = get_std_worker_pool(cpu, highpri);
Tejun Heo30cdf242013-03-12 11:29:57 -07003096 list_add_tail(&pwq->pwqs_node, &wq->pwqs);
3097 }
3098 } else {
3099 struct pool_workqueue *pwq;
3100
3101 pwq = kmem_cache_zalloc(pwq_cache, GFP_KERNEL);
3102 if (!pwq)
3103 return -ENOMEM;
3104
3105 wq->pool_wq.single = pwq;
Tejun Heo49e3cf42013-03-12 11:29:58 -07003106 pwq->pool = get_std_worker_pool(WORK_CPU_UNBOUND, highpri);
Tejun Heo30cdf242013-03-12 11:29:57 -07003107 list_add_tail(&pwq->pwqs_node, &wq->pwqs);
3108 }
3109
3110 return 0;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003111}
3112
Tejun Heo112202d2013-02-13 19:29:12 -08003113static void free_pwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07003114{
Lai Jiangshane06ffa12012-03-09 18:03:20 +08003115 if (!(wq->flags & WQ_UNBOUND))
Tejun Heo112202d2013-02-13 19:29:12 -08003116 free_percpu(wq->pool_wq.pcpu);
Tejun Heoe904e6c2013-03-12 11:29:57 -07003117 else
3118 kmem_cache_free(pwq_cache, wq->pool_wq.single);
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07003119}
3120
Tejun Heof3421792010-07-02 10:03:51 +02003121static int wq_clamp_max_active(int max_active, unsigned int flags,
3122 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003123{
Tejun Heof3421792010-07-02 10:03:51 +02003124 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3125
3126 if (max_active < 1 || max_active > lim)
Valentin Ilie044c7822012-08-19 00:52:42 +03003127 pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
3128 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003129
Tejun Heof3421792010-07-02 10:03:51 +02003130 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003131}
3132
Tejun Heob196be82012-01-10 15:11:35 -08003133struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02003134 unsigned int flags,
3135 int max_active,
3136 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08003137 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003138{
Tejun Heob196be82012-01-10 15:11:35 -08003139 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003140 struct workqueue_struct *wq;
Tejun Heo49e3cf42013-03-12 11:29:58 -07003141 struct pool_workqueue *pwq;
Tejun Heob196be82012-01-10 15:11:35 -08003142 size_t namelen;
3143
3144 /* determine namelen, allocate wq and format name */
3145 va_start(args, lock_name);
3146 va_copy(args1, args);
3147 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3148
3149 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3150 if (!wq)
3151 goto err;
3152
3153 vsnprintf(wq->name, namelen, fmt, args1);
3154 va_end(args);
3155 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003156
Tejun Heof3421792010-07-02 10:03:51 +02003157 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02003158 * Workqueues which may be used during memory reclaim should
3159 * have a rescuer to guarantee forward progress.
3160 */
3161 if (flags & WQ_MEM_RECLAIM)
3162 flags |= WQ_RESCUER;
3163
Tejun Heod320c032010-06-29 10:07:14 +02003164 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003165 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003166
Tejun Heob196be82012-01-10 15:11:35 -08003167 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003168 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003169 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003170 mutex_init(&wq->flush_mutex);
Tejun Heo112202d2013-02-13 19:29:12 -08003171 atomic_set(&wq->nr_pwqs_to_flush, 0);
Tejun Heo30cdf242013-03-12 11:29:57 -07003172 INIT_LIST_HEAD(&wq->pwqs);
Tejun Heo73f53c42010-06-29 10:07:11 +02003173 INIT_LIST_HEAD(&wq->flusher_queue);
3174 INIT_LIST_HEAD(&wq->flusher_overflow);
Tejun Heo493a1722013-03-12 11:29:59 -07003175 INIT_LIST_HEAD(&wq->maydays);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003176
Johannes Bergeb13ba82008-01-16 09:51:58 +01003177 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003178 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003179
Tejun Heo30cdf242013-03-12 11:29:57 -07003180 if (alloc_and_link_pwqs(wq) < 0)
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003181 goto err;
3182
Tejun Heo49e3cf42013-03-12 11:29:58 -07003183 for_each_pwq(pwq, wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08003184 BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK);
Tejun Heo112202d2013-02-13 19:29:12 -08003185 pwq->wq = wq;
3186 pwq->flush_color = -1;
3187 pwq->max_active = max_active;
3188 INIT_LIST_HEAD(&pwq->delayed_works);
Tejun Heo493a1722013-03-12 11:29:59 -07003189 INIT_LIST_HEAD(&pwq->mayday_node);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003190 }
3191
Tejun Heoe22bee72010-06-29 10:07:14 +02003192 if (flags & WQ_RESCUER) {
3193 struct worker *rescuer;
3194
Tejun Heoe22bee72010-06-29 10:07:14 +02003195 wq->rescuer = rescuer = alloc_worker();
3196 if (!rescuer)
3197 goto err;
3198
Tejun Heo111c2252013-01-17 17:16:24 -08003199 rescuer->rescue_wq = wq;
3200 rescuer->task = kthread_create(rescuer_thread, rescuer, "%s",
Tejun Heob196be82012-01-10 15:11:35 -08003201 wq->name);
Tejun Heoe22bee72010-06-29 10:07:14 +02003202 if (IS_ERR(rescuer->task))
3203 goto err;
3204
Tejun Heoe22bee72010-06-29 10:07:14 +02003205 rescuer->task->flags |= PF_THREAD_BOUND;
3206 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003207 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003208
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003209 /*
3210 * workqueue_lock protects global freeze state and workqueues
3211 * list. Grab it, set max_active accordingly and add the new
3212 * workqueue to workqueues list.
3213 */
Tejun Heoe98d5b12013-03-12 11:29:57 -07003214 spin_lock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003215
Tejun Heo58a69cb2011-02-16 09:25:31 +01003216 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heo49e3cf42013-03-12 11:29:58 -07003217 for_each_pwq(pwq, wq)
3218 pwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003219
Tejun Heo15376632010-06-29 10:07:11 +02003220 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003221
Tejun Heoe98d5b12013-03-12 11:29:57 -07003222 spin_unlock_irq(&workqueue_lock);
Tejun Heo15376632010-06-29 10:07:11 +02003223
Oleg Nesterov3af244332007-05-09 02:34:09 -07003224 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02003225err:
3226 if (wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08003227 free_pwqs(wq);
Tejun Heoe22bee72010-06-29 10:07:14 +02003228 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003229 kfree(wq);
3230 }
3231 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003232}
Tejun Heod320c032010-06-29 10:07:14 +02003233EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003234
3235/**
3236 * destroy_workqueue - safely terminate a workqueue
3237 * @wq: target workqueue
3238 *
3239 * Safely destroy a workqueue. All work currently pending will be done first.
3240 */
3241void destroy_workqueue(struct workqueue_struct *wq)
3242{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003243 struct pool_workqueue *pwq;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003244
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003245 /* drain it before proceeding with destruction */
3246 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003247
Tejun Heo6183c002013-03-12 11:29:57 -07003248 /* sanity checks */
Tejun Heo49e3cf42013-03-12 11:29:58 -07003249 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07003250 int i;
3251
3252 for (i = 0; i < WORK_NR_COLORS; i++)
3253 if (WARN_ON(pwq->nr_in_flight[i]))
3254 return;
3255 if (WARN_ON(pwq->nr_active) ||
3256 WARN_ON(!list_empty(&pwq->delayed_works)))
3257 return;
3258 }
3259
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003260 /*
3261 * wq list is used to freeze wq, remove from list after
3262 * flushing is complete in case freeze races us.
3263 */
Tejun Heoe98d5b12013-03-12 11:29:57 -07003264 spin_lock_irq(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07003265 list_del(&wq->list);
Tejun Heoe98d5b12013-03-12 11:29:57 -07003266 spin_unlock_irq(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003267
Tejun Heoe22bee72010-06-29 10:07:14 +02003268 if (wq->flags & WQ_RESCUER) {
3269 kthread_stop(wq->rescuer->task);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003270 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003271 }
3272
Tejun Heo112202d2013-02-13 19:29:12 -08003273 free_pwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003274 kfree(wq);
3275}
3276EXPORT_SYMBOL_GPL(destroy_workqueue);
3277
Tejun Heodcd989c2010-06-29 10:07:14 +02003278/**
Tejun Heo112202d2013-02-13 19:29:12 -08003279 * pwq_set_max_active - adjust max_active of a pwq
3280 * @pwq: target pool_workqueue
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003281 * @max_active: new max_active value.
3282 *
Tejun Heo112202d2013-02-13 19:29:12 -08003283 * Set @pwq->max_active to @max_active and activate delayed works if
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003284 * increased.
3285 *
3286 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08003287 * spin_lock_irq(pool->lock).
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003288 */
Tejun Heo112202d2013-02-13 19:29:12 -08003289static void pwq_set_max_active(struct pool_workqueue *pwq, int max_active)
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003290{
Tejun Heo112202d2013-02-13 19:29:12 -08003291 pwq->max_active = max_active;
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003292
Tejun Heo112202d2013-02-13 19:29:12 -08003293 while (!list_empty(&pwq->delayed_works) &&
3294 pwq->nr_active < pwq->max_active)
3295 pwq_activate_first_delayed(pwq);
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003296}
3297
3298/**
Tejun Heodcd989c2010-06-29 10:07:14 +02003299 * workqueue_set_max_active - adjust max_active of a workqueue
3300 * @wq: target workqueue
3301 * @max_active: new max_active value.
3302 *
3303 * Set max_active of @wq to @max_active.
3304 *
3305 * CONTEXT:
3306 * Don't call from IRQ context.
3307 */
3308void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3309{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003310 struct pool_workqueue *pwq;
Tejun Heodcd989c2010-06-29 10:07:14 +02003311
Tejun Heof3421792010-07-02 10:03:51 +02003312 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003313
Tejun Heoe98d5b12013-03-12 11:29:57 -07003314 spin_lock_irq(&workqueue_lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02003315
3316 wq->saved_max_active = max_active;
3317
Tejun Heo49e3cf42013-03-12 11:29:58 -07003318 for_each_pwq(pwq, wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08003319 struct worker_pool *pool = pwq->pool;
Tejun Heodcd989c2010-06-29 10:07:14 +02003320
Tejun Heoe98d5b12013-03-12 11:29:57 -07003321 spin_lock(&pool->lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02003322
Tejun Heo58a69cb2011-02-16 09:25:31 +01003323 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heo35b6bb62013-01-24 11:01:33 -08003324 !(pool->flags & POOL_FREEZING))
Tejun Heo112202d2013-02-13 19:29:12 -08003325 pwq_set_max_active(pwq, max_active);
Tejun Heodcd989c2010-06-29 10:07:14 +02003326
Tejun Heoe98d5b12013-03-12 11:29:57 -07003327 spin_unlock(&pool->lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02003328 }
3329
Tejun Heoe98d5b12013-03-12 11:29:57 -07003330 spin_unlock_irq(&workqueue_lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02003331}
3332EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3333
3334/**
3335 * workqueue_congested - test whether a workqueue is congested
3336 * @cpu: CPU in question
3337 * @wq: target workqueue
3338 *
3339 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3340 * no synchronization around this function and the test result is
3341 * unreliable and only useful as advisory hints or for debugging.
3342 *
3343 * RETURNS:
3344 * %true if congested, %false otherwise.
3345 */
Tejun Heod84ff052013-03-12 11:29:59 -07003346bool workqueue_congested(int cpu, struct workqueue_struct *wq)
Tejun Heodcd989c2010-06-29 10:07:14 +02003347{
Tejun Heo112202d2013-02-13 19:29:12 -08003348 struct pool_workqueue *pwq = get_pwq(cpu, wq);
Tejun Heodcd989c2010-06-29 10:07:14 +02003349
Tejun Heo112202d2013-02-13 19:29:12 -08003350 return !list_empty(&pwq->delayed_works);
Tejun Heodcd989c2010-06-29 10:07:14 +02003351}
3352EXPORT_SYMBOL_GPL(workqueue_congested);
3353
3354/**
Tejun Heodcd989c2010-06-29 10:07:14 +02003355 * work_busy - test whether a work is currently pending or running
3356 * @work: the work to be tested
3357 *
3358 * Test whether @work is currently pending or running. There is no
3359 * synchronization around this function and the test result is
3360 * unreliable and only useful as advisory hints or for debugging.
Tejun Heodcd989c2010-06-29 10:07:14 +02003361 *
3362 * RETURNS:
3363 * OR'd bitmask of WORK_BUSY_* bits.
3364 */
3365unsigned int work_busy(struct work_struct *work)
3366{
Tejun Heoc9e7cf22013-01-24 11:01:33 -08003367 struct worker_pool *pool = get_work_pool(work);
Tejun Heodcd989c2010-06-29 10:07:14 +02003368 unsigned long flags;
3369 unsigned int ret = 0;
3370
Tejun Heodcd989c2010-06-29 10:07:14 +02003371 if (work_pending(work))
3372 ret |= WORK_BUSY_PENDING;
Tejun Heodcd989c2010-06-29 10:07:14 +02003373
Lai Jiangshan038366c2013-02-06 18:04:53 -08003374 if (pool) {
3375 spin_lock_irqsave(&pool->lock, flags);
3376 if (find_worker_executing_work(pool, work))
3377 ret |= WORK_BUSY_RUNNING;
3378 spin_unlock_irqrestore(&pool->lock, flags);
3379 }
Tejun Heodcd989c2010-06-29 10:07:14 +02003380
3381 return ret;
3382}
3383EXPORT_SYMBOL_GPL(work_busy);
3384
Tejun Heodb7bccf2010-06-29 10:07:12 +02003385/*
3386 * CPU hotplug.
3387 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003388 * There are two challenges in supporting CPU hotplug. Firstly, there
Tejun Heo112202d2013-02-13 19:29:12 -08003389 * are a lot of assumptions on strong associations among work, pwq and
Tejun Heo706026c2013-01-24 11:01:34 -08003390 * pool which make migrating pending and scheduled works very
Tejun Heoe22bee72010-06-29 10:07:14 +02003391 * difficult to implement without impacting hot paths. Secondly,
Tejun Heo94cf58b2013-01-24 11:01:33 -08003392 * worker pools serve mix of short, long and very long running works making
Tejun Heoe22bee72010-06-29 10:07:14 +02003393 * blocked draining impractical.
3394 *
Tejun Heo24647572013-01-24 11:01:33 -08003395 * This is solved by allowing the pools to be disassociated from the CPU
Tejun Heo628c78e2012-07-17 12:39:27 -07003396 * running as an unbound one and allowing it to be reattached later if the
3397 * cpu comes back online.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003398 */
3399
Tejun Heo706026c2013-01-24 11:01:34 -08003400static void wq_unbind_fn(struct work_struct *work)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003401{
Tejun Heo38db41d2013-01-24 11:01:34 -08003402 int cpu = smp_processor_id();
Tejun Heo4ce62e92012-07-13 22:16:44 -07003403 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003404 struct worker *worker;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003405 int i;
3406
Tejun Heo38db41d2013-01-24 11:01:34 -08003407 for_each_std_worker_pool(pool, cpu) {
Tejun Heo6183c002013-03-12 11:29:57 -07003408 WARN_ON_ONCE(cpu != smp_processor_id());
Tejun Heo94cf58b2013-01-24 11:01:33 -08003409
3410 mutex_lock(&pool->assoc_mutex);
3411 spin_lock_irq(&pool->lock);
3412
3413 /*
3414 * We've claimed all manager positions. Make all workers
3415 * unbound and set DISASSOCIATED. Before this, all workers
3416 * except for the ones which are still executing works from
3417 * before the last CPU down must be on the cpu. After
3418 * this, they may become diasporas.
3419 */
Tejun Heo4ce62e92012-07-13 22:16:44 -07003420 list_for_each_entry(worker, &pool->idle_list, entry)
Tejun Heo403c8212012-07-17 12:39:27 -07003421 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003422
Sasha Levinb67bfe02013-02-27 17:06:00 -08003423 for_each_busy_worker(worker, i, pool)
Tejun Heoc9e7cf22013-01-24 11:01:33 -08003424 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003425
Tejun Heo24647572013-01-24 11:01:33 -08003426 pool->flags |= POOL_DISASSOCIATED;
Tejun Heof2d5a0e2012-07-17 12:39:26 -07003427
Tejun Heo94cf58b2013-01-24 11:01:33 -08003428 spin_unlock_irq(&pool->lock);
3429 mutex_unlock(&pool->assoc_mutex);
3430 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003431
3432 /*
Tejun Heo628c78e2012-07-17 12:39:27 -07003433 * Call schedule() so that we cross rq->lock and thus can guarantee
3434 * sched callbacks see the %WORKER_UNBOUND flag. This is necessary
3435 * as scheduler callbacks may be invoked from other cpus.
3436 */
3437 schedule();
3438
3439 /*
3440 * Sched callbacks are disabled now. Zap nr_running. After this,
3441 * nr_running stays zero and need_more_worker() and keep_working()
Tejun Heo38db41d2013-01-24 11:01:34 -08003442 * are always true as long as the worklist is not empty. Pools on
3443 * @cpu now behave as unbound (in terms of concurrency management)
3444 * pools which are served by workers tied to the CPU.
Tejun Heo628c78e2012-07-17 12:39:27 -07003445 *
3446 * On return from this function, the current worker would trigger
3447 * unbound chain execution of pending work items if other workers
3448 * didn't already.
Tejun Heoe22bee72010-06-29 10:07:14 +02003449 */
Tejun Heo38db41d2013-01-24 11:01:34 -08003450 for_each_std_worker_pool(pool, cpu)
Tejun Heoe19e3972013-01-24 11:39:44 -08003451 atomic_set(&pool->nr_running, 0);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003452}
3453
Tejun Heo8db25e72012-07-17 12:39:28 -07003454/*
3455 * Workqueues should be brought up before normal priority CPU notifiers.
3456 * This will be registered high priority CPU notifier.
3457 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07003458static int __cpuinit workqueue_cpu_up_callback(struct notifier_block *nfb,
Tejun Heo8db25e72012-07-17 12:39:28 -07003459 unsigned long action,
3460 void *hcpu)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003461{
Tejun Heod84ff052013-03-12 11:29:59 -07003462 int cpu = (unsigned long)hcpu;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003463 struct worker_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464
Tejun Heo8db25e72012-07-17 12:39:28 -07003465 switch (action & ~CPU_TASKS_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 case CPU_UP_PREPARE:
Tejun Heo38db41d2013-01-24 11:01:34 -08003467 for_each_std_worker_pool(pool, cpu) {
Tejun Heo3ce63372012-07-17 12:39:27 -07003468 struct worker *worker;
3469
3470 if (pool->nr_workers)
3471 continue;
3472
3473 worker = create_worker(pool);
3474 if (!worker)
3475 return NOTIFY_BAD;
3476
Tejun Heod565ed62013-01-24 11:01:33 -08003477 spin_lock_irq(&pool->lock);
Tejun Heo3ce63372012-07-17 12:39:27 -07003478 start_worker(worker);
Tejun Heod565ed62013-01-24 11:01:33 -08003479 spin_unlock_irq(&pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003481 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003482
Tejun Heo65758202012-07-17 12:39:26 -07003483 case CPU_DOWN_FAILED:
3484 case CPU_ONLINE:
Tejun Heo38db41d2013-01-24 11:01:34 -08003485 for_each_std_worker_pool(pool, cpu) {
Tejun Heo94cf58b2013-01-24 11:01:33 -08003486 mutex_lock(&pool->assoc_mutex);
3487 spin_lock_irq(&pool->lock);
3488
Tejun Heo24647572013-01-24 11:01:33 -08003489 pool->flags &= ~POOL_DISASSOCIATED;
Tejun Heo94cf58b2013-01-24 11:01:33 -08003490 rebind_workers(pool);
3491
3492 spin_unlock_irq(&pool->lock);
3493 mutex_unlock(&pool->assoc_mutex);
3494 }
Tejun Heo8db25e72012-07-17 12:39:28 -07003495 break;
Tejun Heo65758202012-07-17 12:39:26 -07003496 }
3497 return NOTIFY_OK;
3498}
3499
3500/*
3501 * Workqueues should be brought down after normal priority CPU notifiers.
3502 * This will be registered as low priority CPU notifier.
3503 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07003504static int __cpuinit workqueue_cpu_down_callback(struct notifier_block *nfb,
Tejun Heo65758202012-07-17 12:39:26 -07003505 unsigned long action,
3506 void *hcpu)
3507{
Tejun Heod84ff052013-03-12 11:29:59 -07003508 int cpu = (unsigned long)hcpu;
Tejun Heo8db25e72012-07-17 12:39:28 -07003509 struct work_struct unbind_work;
3510
Tejun Heo65758202012-07-17 12:39:26 -07003511 switch (action & ~CPU_TASKS_FROZEN) {
3512 case CPU_DOWN_PREPARE:
Tejun Heo8db25e72012-07-17 12:39:28 -07003513 /* unbinding should happen on the local CPU */
Tejun Heo706026c2013-01-24 11:01:34 -08003514 INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn);
Joonsoo Kim7635d2f2012-08-15 23:25:41 +09003515 queue_work_on(cpu, system_highpri_wq, &unbind_work);
Tejun Heo8db25e72012-07-17 12:39:28 -07003516 flush_work(&unbind_work);
3517 break;
Tejun Heo65758202012-07-17 12:39:26 -07003518 }
3519 return NOTIFY_OK;
3520}
3521
Rusty Russell2d3854a2008-11-05 13:39:10 +11003522#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003523
Rusty Russell2d3854a2008-11-05 13:39:10 +11003524struct work_for_cpu {
Tejun Heoed48ece2012-09-18 12:48:43 -07003525 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003526 long (*fn)(void *);
3527 void *arg;
3528 long ret;
3529};
3530
Tejun Heoed48ece2012-09-18 12:48:43 -07003531static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003532{
Tejun Heoed48ece2012-09-18 12:48:43 -07003533 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
3534
Rusty Russell2d3854a2008-11-05 13:39:10 +11003535 wfc->ret = wfc->fn(wfc->arg);
3536}
3537
3538/**
3539 * work_on_cpu - run a function in user context on a particular cpu
3540 * @cpu: the cpu to run on
3541 * @fn: the function to run
3542 * @arg: the function arg
3543 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003544 * This will return the value @fn returns.
3545 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b44003e2009-04-09 09:50:37 -06003546 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003547 */
Tejun Heod84ff052013-03-12 11:29:59 -07003548long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003549{
Tejun Heoed48ece2012-09-18 12:48:43 -07003550 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003551
Tejun Heoed48ece2012-09-18 12:48:43 -07003552 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
3553 schedule_work_on(cpu, &wfc.work);
3554 flush_work(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003555 return wfc.ret;
3556}
3557EXPORT_SYMBOL_GPL(work_on_cpu);
3558#endif /* CONFIG_SMP */
3559
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003560#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303561
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003562/**
3563 * freeze_workqueues_begin - begin freezing workqueues
3564 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003565 * Start freezing workqueues. After this function returns, all freezable
3566 * workqueues will queue new works to their frozen_works list instead of
Tejun Heo706026c2013-01-24 11:01:34 -08003567 * pool->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003568 *
3569 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08003570 * Grabs and releases workqueue_lock and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003571 */
3572void freeze_workqueues_begin(void)
3573{
Tejun Heo17116962013-03-12 11:29:58 -07003574 struct worker_pool *pool;
Tejun Heo24b8a842013-03-12 11:29:58 -07003575 struct workqueue_struct *wq;
3576 struct pool_workqueue *pwq;
Tejun Heo17116962013-03-12 11:29:58 -07003577 int id;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003578
Tejun Heoe98d5b12013-03-12 11:29:57 -07003579 spin_lock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003580
Tejun Heo6183c002013-03-12 11:29:57 -07003581 WARN_ON_ONCE(workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003582 workqueue_freezing = true;
3583
Tejun Heo24b8a842013-03-12 11:29:58 -07003584 /* set FREEZING */
Tejun Heo17116962013-03-12 11:29:58 -07003585 for_each_pool(pool, id) {
Tejun Heo17116962013-03-12 11:29:58 -07003586 spin_lock(&pool->lock);
Tejun Heo17116962013-03-12 11:29:58 -07003587 WARN_ON_ONCE(pool->flags & POOL_FREEZING);
3588 pool->flags |= POOL_FREEZING;
Tejun Heo17116962013-03-12 11:29:58 -07003589 spin_unlock(&pool->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003590 }
3591
Tejun Heo24b8a842013-03-12 11:29:58 -07003592 /* suppress further executions by setting max_active to zero */
3593 list_for_each_entry(wq, &workqueues, list) {
3594 if (!(wq->flags & WQ_FREEZABLE))
3595 continue;
3596
3597 for_each_pwq(pwq, wq) {
3598 spin_lock(&pwq->pool->lock);
3599 pwq->max_active = 0;
3600 spin_unlock(&pwq->pool->lock);
3601 }
3602 }
3603
Tejun Heoe98d5b12013-03-12 11:29:57 -07003604 spin_unlock_irq(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003606
3607/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003608 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003609 *
3610 * Check whether freezing is complete. This function must be called
3611 * between freeze_workqueues_begin() and thaw_workqueues().
3612 *
3613 * CONTEXT:
3614 * Grabs and releases workqueue_lock.
3615 *
3616 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01003617 * %true if some freezable workqueues are still busy. %false if freezing
3618 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003619 */
3620bool freeze_workqueues_busy(void)
3621{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003622 bool busy = false;
Tejun Heo24b8a842013-03-12 11:29:58 -07003623 struct workqueue_struct *wq;
3624 struct pool_workqueue *pwq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003625
Tejun Heoe98d5b12013-03-12 11:29:57 -07003626 spin_lock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003627
Tejun Heo6183c002013-03-12 11:29:57 -07003628 WARN_ON_ONCE(!workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003629
Tejun Heo24b8a842013-03-12 11:29:58 -07003630 list_for_each_entry(wq, &workqueues, list) {
3631 if (!(wq->flags & WQ_FREEZABLE))
3632 continue;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003633 /*
3634 * nr_active is monotonically decreasing. It's safe
3635 * to peek without lock.
3636 */
Tejun Heo24b8a842013-03-12 11:29:58 -07003637 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07003638 WARN_ON_ONCE(pwq->nr_active < 0);
Tejun Heo112202d2013-02-13 19:29:12 -08003639 if (pwq->nr_active) {
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003640 busy = true;
3641 goto out_unlock;
3642 }
3643 }
3644 }
3645out_unlock:
Tejun Heoe98d5b12013-03-12 11:29:57 -07003646 spin_unlock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003647 return busy;
3648}
3649
3650/**
3651 * thaw_workqueues - thaw workqueues
3652 *
3653 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo706026c2013-01-24 11:01:34 -08003654 * frozen works are transferred to their respective pool worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003655 *
3656 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08003657 * Grabs and releases workqueue_lock and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003658 */
3659void thaw_workqueues(void)
3660{
Tejun Heo24b8a842013-03-12 11:29:58 -07003661 struct workqueue_struct *wq;
3662 struct pool_workqueue *pwq;
3663 struct worker_pool *pool;
3664 int id;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003665
Tejun Heoe98d5b12013-03-12 11:29:57 -07003666 spin_lock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003667
3668 if (!workqueue_freezing)
3669 goto out_unlock;
3670
Tejun Heo24b8a842013-03-12 11:29:58 -07003671 /* clear FREEZING */
3672 for_each_pool(pool, id) {
3673 spin_lock(&pool->lock);
3674 WARN_ON_ONCE(!(pool->flags & POOL_FREEZING));
3675 pool->flags &= ~POOL_FREEZING;
3676 spin_unlock(&pool->lock);
3677 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003678
Tejun Heo24b8a842013-03-12 11:29:58 -07003679 /* restore max_active and repopulate worklist */
3680 list_for_each_entry(wq, &workqueues, list) {
3681 if (!(wq->flags & WQ_FREEZABLE))
3682 continue;
Tejun Heod565ed62013-01-24 11:01:33 -08003683
Tejun Heo24b8a842013-03-12 11:29:58 -07003684 for_each_pwq(pwq, wq) {
3685 spin_lock(&pwq->pool->lock);
3686 pwq_set_max_active(pwq, wq->saved_max_active);
3687 spin_unlock(&pwq->pool->lock);
Tejun Heod565ed62013-01-24 11:01:33 -08003688 }
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003689 }
3690
Tejun Heo24b8a842013-03-12 11:29:58 -07003691 /* kick workers */
3692 for_each_pool(pool, id) {
3693 spin_lock(&pool->lock);
3694 wake_up_worker(pool);
3695 spin_unlock(&pool->lock);
3696 }
3697
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003698 workqueue_freezing = false;
3699out_unlock:
Tejun Heoe98d5b12013-03-12 11:29:57 -07003700 spin_unlock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003701}
3702#endif /* CONFIG_FREEZER */
3703
Suresh Siddha6ee05782010-07-30 14:57:37 -07003704static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705{
Tejun Heod84ff052013-03-12 11:29:59 -07003706 int cpu;
Tejun Heoc34056a2010-06-29 10:07:11 +02003707
Tejun Heo7c3eed52013-01-24 11:01:33 -08003708 /* make sure we have enough bits for OFFQ pool ID */
3709 BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) <
Lai Jiangshan6be19582013-02-06 18:04:53 -08003710 WORK_CPU_END * NR_STD_WORKER_POOLS);
Tejun Heob5490072012-08-03 10:30:46 -07003711
Tejun Heoe904e6c2013-03-12 11:29:57 -07003712 WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long));
3713
3714 pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC);
3715
Tejun Heo65758202012-07-17 12:39:26 -07003716 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
Lai Jiangshana5b4e572012-09-18 09:59:23 -07003717 hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003718
Tejun Heo706026c2013-01-24 11:01:34 -08003719 /* initialize CPU pools */
3720 for_each_wq_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07003721 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003722
Tejun Heo38db41d2013-01-24 11:01:34 -08003723 for_each_std_worker_pool(pool, cpu) {
Tejun Heod565ed62013-01-24 11:01:33 -08003724 spin_lock_init(&pool->lock);
Tejun Heoec22ca52013-01-24 11:01:33 -08003725 pool->cpu = cpu;
Tejun Heo24647572013-01-24 11:01:33 -08003726 pool->flags |= POOL_DISASSOCIATED;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003727 INIT_LIST_HEAD(&pool->worklist);
3728 INIT_LIST_HEAD(&pool->idle_list);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08003729 hash_init(pool->busy_hash);
Tejun Heoe22bee72010-06-29 10:07:14 +02003730
Tejun Heo4ce62e92012-07-13 22:16:44 -07003731 init_timer_deferrable(&pool->idle_timer);
3732 pool->idle_timer.function = idle_worker_timeout;
3733 pool->idle_timer.data = (unsigned long)pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003734
Tejun Heo706026c2013-01-24 11:01:34 -08003735 setup_timer(&pool->mayday_timer, pool_mayday_timeout,
Tejun Heo4ce62e92012-07-13 22:16:44 -07003736 (unsigned long)pool);
3737
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07003738 mutex_init(&pool->assoc_mutex);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003739 ida_init(&pool->worker_ida);
Tejun Heo9daf9e62013-01-24 11:01:33 -08003740
3741 /* alloc pool ID */
3742 BUG_ON(worker_pool_assign_id(pool));
Tejun Heo4ce62e92012-07-13 22:16:44 -07003743 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003744 }
3745
Tejun Heoe22bee72010-06-29 10:07:14 +02003746 /* create the initial worker */
Tejun Heo706026c2013-01-24 11:01:34 -08003747 for_each_online_wq_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07003748 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003749
Tejun Heo38db41d2013-01-24 11:01:34 -08003750 for_each_std_worker_pool(pool, cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07003751 struct worker *worker;
3752
Tejun Heo24647572013-01-24 11:01:33 -08003753 if (cpu != WORK_CPU_UNBOUND)
3754 pool->flags &= ~POOL_DISASSOCIATED;
3755
Tejun Heobc2ae0f2012-07-17 12:39:27 -07003756 worker = create_worker(pool);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003757 BUG_ON(!worker);
Tejun Heod565ed62013-01-24 11:01:33 -08003758 spin_lock_irq(&pool->lock);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003759 start_worker(worker);
Tejun Heod565ed62013-01-24 11:01:33 -08003760 spin_unlock_irq(&pool->lock);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003761 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003762 }
3763
Tejun Heod320c032010-06-29 10:07:14 +02003764 system_wq = alloc_workqueue("events", 0, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09003765 system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
Tejun Heod320c032010-06-29 10:07:14 +02003766 system_long_wq = alloc_workqueue("events_long", 0, 0);
Tejun Heof3421792010-07-02 10:03:51 +02003767 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3768 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01003769 system_freezable_wq = alloc_workqueue("events_freezable",
3770 WQ_FREEZABLE, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09003771 BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
Tejun Heoae930e02012-08-20 14:51:23 -07003772 !system_unbound_wq || !system_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07003773 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774}
Suresh Siddha6ee05782010-07-30 14:57:37 -07003775early_initcall(init_workqueues);