blob: e060ff2bc20c00c113d9c1f3a32a3d569540a752 [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 Heo76af4d92013-03-12 11:30:00 -070045#include <linux/rculist.h>
Tejun Heoe22bee72010-06-29 10:07:14 +020046
Tejun Heoea138442013-01-18 14:05:55 -080047#include "workqueue_internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Tejun Heoc8e55f32010-06-29 10:07:12 +020049enum {
Tejun Heobc2ae0f2012-07-17 12:39:27 -070050 /*
Tejun Heo24647572013-01-24 11:01:33 -080051 * worker_pool flags
Tejun Heobc2ae0f2012-07-17 12:39:27 -070052 *
Tejun Heo24647572013-01-24 11:01:33 -080053 * A bound pool is either associated or disassociated with its CPU.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070054 * While associated (!DISASSOCIATED), all workers are bound to the
55 * CPU and none has %WORKER_UNBOUND set and concurrency management
56 * is in effect.
57 *
58 * While DISASSOCIATED, the cpu may be offline and all workers have
59 * %WORKER_UNBOUND set and concurrency management disabled, and may
Tejun Heo24647572013-01-24 11:01:33 -080060 * be executing on any CPU. The pool behaves as an unbound one.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070061 *
62 * Note that DISASSOCIATED can be flipped only while holding
Tejun Heo24647572013-01-24 11:01:33 -080063 * assoc_mutex to avoid changing binding state while
64 * create_worker() is in progress.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070065 */
Tejun Heo11ebea52012-07-12 14:46:37 -070066 POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
Lai Jiangshan552a37e2012-09-10 10:03:33 -070067 POOL_MANAGING_WORKERS = 1 << 1, /* managing workers */
Tejun Heo24647572013-01-24 11:01:33 -080068 POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */
Tejun Heo35b6bb62013-01-24 11:01:33 -080069 POOL_FREEZING = 1 << 3, /* freeze in progress */
Tejun Heodb7bccf2010-06-29 10:07:12 +020070
Tejun Heoc8e55f32010-06-29 10:07:12 +020071 /* worker flags */
72 WORKER_STARTED = 1 << 0, /* started */
73 WORKER_DIE = 1 << 1, /* die die die */
74 WORKER_IDLE = 1 << 2, /* is idle */
Tejun Heoe22bee72010-06-29 10:07:14 +020075 WORKER_PREP = 1 << 3, /* preparing to run works */
Tejun Heofb0e7be2010-06-29 10:07:15 +020076 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
Tejun Heof3421792010-07-02 10:03:51 +020077 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
Tejun Heoe22bee72010-06-29 10:07:14 +020078
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -070079 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_UNBOUND |
Tejun Heo403c8212012-07-17 12:39:27 -070080 WORKER_CPU_INTENSIVE,
Tejun Heodb7bccf2010-06-29 10:07:12 +020081
Tejun Heoe34cdddb2013-01-24 11:01:33 -080082 NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */
Tejun Heo4ce62e92012-07-13 22:16:44 -070083
Tejun Heoc8e55f32010-06-29 10:07:12 +020084 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
Tejun Heodb7bccf2010-06-29 10:07:12 +020085
Tejun Heoe22bee72010-06-29 10:07:14 +020086 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
87 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
88
Tejun Heo3233cdb2011-02-16 18:10:19 +010089 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
90 /* call for help after 10ms
91 (min two ticks) */
Tejun Heoe22bee72010-06-29 10:07:14 +020092 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
93 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heoe22bee72010-06-29 10:07:14 +020094
95 /*
96 * Rescue workers are used only on emergencies and shared by
97 * all cpus. Give -20.
98 */
99 RESCUER_NICE_LEVEL = -20,
Tejun Heo32704762012-07-13 22:16:45 -0700100 HIGHPRI_NICE_LEVEL = -20,
Tejun Heoc8e55f32010-06-29 10:07:12 +0200101};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103/*
Tejun Heo4690c4a2010-06-29 10:07:10 +0200104 * Structure fields follow one of the following exclusion rules.
105 *
Tejun Heoe41e7042010-08-24 14:22:47 +0200106 * I: Modifiable by initialization/destruction paths and read-only for
107 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200108 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200109 * P: Preemption protected. Disabling preemption is enough and should
110 * only be modified and accessed from the local cpu.
111 *
Tejun Heod565ed62013-01-24 11:01:33 -0800112 * L: pool->lock protected. Access with pool->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200113 *
Tejun Heod565ed62013-01-24 11:01:33 -0800114 * X: During normal operation, modification requires pool->lock and should
115 * be done only from local cpu. Either disabling preemption on local
116 * cpu or grabbing pool->lock is enough for read access. If
117 * POOL_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200118 *
Tejun Heo73f53c42010-06-29 10:07:11 +0200119 * F: wq->flush_mutex protected.
120 *
Tejun Heo4690c4a2010-06-29 10:07:10 +0200121 * W: workqueue_lock protected.
Tejun Heo76af4d92013-03-12 11:30:00 -0700122 *
123 * R: workqueue_lock protected for writes. Sched-RCU protected for reads.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200124 */
125
Tejun Heo2eaebdb2013-01-18 14:05:55 -0800126/* struct worker is defined in workqueue_internal.h */
Tejun Heoc34056a2010-06-29 10:07:11 +0200127
Tejun Heobd7bdd42012-07-12 14:46:37 -0700128struct worker_pool {
Tejun Heod565ed62013-01-24 11:01:33 -0800129 spinlock_t lock; /* the pool lock */
Tejun Heod84ff052013-03-12 11:29:59 -0700130 int cpu; /* I: the associated cpu */
Tejun Heo9daf9e62013-01-24 11:01:33 -0800131 int id; /* I: pool ID */
Tejun Heo11ebea52012-07-12 14:46:37 -0700132 unsigned int flags; /* X: flags */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700133
134 struct list_head worklist; /* L: list of pending works */
135 int nr_workers; /* L: total number of workers */
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700136
137 /* nr_idle includes the ones off idle_list for rebinding */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700138 int nr_idle; /* L: currently idle ones */
139
140 struct list_head idle_list; /* X: list of idle workers */
141 struct timer_list idle_timer; /* L: worker idle timeout */
142 struct timer_list mayday_timer; /* L: SOS timer for workers */
143
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800144 /* workers are chained either in busy_hash or idle_list */
145 DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER);
146 /* L: hash of busy workers */
147
Tejun Heo24647572013-01-24 11:01:33 -0800148 struct mutex assoc_mutex; /* protect POOL_DISASSOCIATED */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700149 struct ida worker_ida; /* L: for worker IDs */
Tejun Heoe19e3972013-01-24 11:39:44 -0800150
151 /*
152 * The current concurrency level. As it's likely to be accessed
153 * from other CPUs during try_to_wake_up(), put it in a separate
154 * cacheline.
155 */
156 atomic_t nr_running ____cacheline_aligned_in_smp;
Tejun Heo8b03ae32010-06-29 10:07:12 +0200157} ____cacheline_aligned_in_smp;
158
159/*
Tejun Heo112202d2013-02-13 19:29:12 -0800160 * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS
161 * of work_struct->data are used for flags and the remaining high bits
162 * point to the pwq; thus, pwqs need to be aligned at two's power of the
163 * number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 */
Tejun Heo112202d2013-02-13 19:29:12 -0800165struct pool_workqueue {
Tejun Heobd7bdd42012-07-12 14:46:37 -0700166 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200167 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200168 int work_color; /* L: current color */
169 int flush_color; /* L: flushing color */
170 int nr_in_flight[WORK_NR_COLORS];
171 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200172 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200173 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200174 struct list_head delayed_works; /* L: delayed works */
Tejun Heo76af4d92013-03-12 11:30:00 -0700175 struct list_head pwqs_node; /* R: node on wq->pwqs */
Tejun Heo493a1722013-03-12 11:29:59 -0700176 struct list_head mayday_node; /* W: node on wq->maydays */
Tejun Heoe904e6c2013-03-12 11:29:57 -0700177} __aligned(1 << WORK_STRUCT_FLAG_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200180 * Structure used to wait for workqueue flush.
181 */
182struct wq_flusher {
183 struct list_head list; /* F: list of flushers */
184 int flush_color; /* F: flush color waiting for */
185 struct completion done; /* flush completion */
186};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Tejun Heo73f53c42010-06-29 10:07:11 +0200188/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 * The externally visible workqueue abstraction is an array of
190 * per-CPU workqueues:
191 */
192struct workqueue_struct {
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200193 unsigned int flags; /* W: WQ_* flags */
Tejun Heo420c0dd2013-03-12 11:29:59 -0700194 struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwq's */
Tejun Heo76af4d92013-03-12 11:30:00 -0700195 struct list_head pwqs; /* R: all pwqs of this wq */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200196 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200197
198 struct mutex flush_mutex; /* protects wq flushing */
199 int work_color; /* F: current work color */
200 int flush_color; /* F: current flush color */
Tejun Heo112202d2013-02-13 19:29:12 -0800201 atomic_t nr_pwqs_to_flush; /* flush in progress */
Tejun Heo73f53c42010-06-29 10:07:11 +0200202 struct wq_flusher *first_flusher; /* F: first flusher */
203 struct list_head flusher_queue; /* F: flush waiters */
204 struct list_head flusher_overflow; /* F: flush overflow list */
205
Tejun Heo493a1722013-03-12 11:29:59 -0700206 struct list_head maydays; /* W: pwqs requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200207 struct worker *rescuer; /* I: rescue worker */
208
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200209 int nr_drainers; /* W: drain in progress */
Tejun Heo112202d2013-02-13 19:29:12 -0800210 int saved_max_active; /* W: saved pwq max_active */
Johannes Berg4e6045f2007-10-18 23:39:55 -0700211#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200212 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700213#endif
Tejun Heob196be82012-01-10 15:11:35 -0800214 char name[]; /* I: workqueue name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215};
216
Tejun Heoe904e6c2013-03-12 11:29:57 -0700217static struct kmem_cache *pwq_cache;
218
Tejun Heod320c032010-06-29 10:07:14 +0200219struct workqueue_struct *system_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200220EXPORT_SYMBOL_GPL(system_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300221struct workqueue_struct *system_highpri_wq __read_mostly;
Joonsoo Kim1aabe902012-08-15 23:25:39 +0900222EXPORT_SYMBOL_GPL(system_highpri_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300223struct workqueue_struct *system_long_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200224EXPORT_SYMBOL_GPL(system_long_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300225struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200226EXPORT_SYMBOL_GPL(system_unbound_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300227struct workqueue_struct *system_freezable_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100228EXPORT_SYMBOL_GPL(system_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200229
Tejun Heo97bd2342010-10-05 10:41:14 +0200230#define CREATE_TRACE_POINTS
231#include <trace/events/workqueue.h>
232
Tejun Heo76af4d92013-03-12 11:30:00 -0700233#define assert_rcu_or_wq_lock() \
234 rcu_lockdep_assert(rcu_read_lock_sched_held() || \
235 lockdep_is_held(&workqueue_lock), \
236 "sched RCU or workqueue lock should be held")
237
Tejun Heo38db41d2013-01-24 11:01:34 -0800238#define for_each_std_worker_pool(pool, cpu) \
Tejun Heoa60dc392013-01-24 11:01:34 -0800239 for ((pool) = &std_worker_pools(cpu)[0]; \
240 (pool) < &std_worker_pools(cpu)[NR_STD_WORKER_POOLS]; (pool)++)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700241
Sasha Levinb67bfe02013-02-27 17:06:00 -0800242#define for_each_busy_worker(worker, i, pool) \
243 hash_for_each(pool->busy_hash, i, worker, hentry)
Tejun Heodb7bccf2010-06-29 10:07:12 +0200244
Tejun Heo706026c2013-01-24 11:01:34 -0800245static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
246 unsigned int sw)
Tejun Heof3421792010-07-02 10:03:51 +0200247{
248 if (cpu < nr_cpu_ids) {
249 if (sw & 1) {
250 cpu = cpumask_next(cpu, mask);
251 if (cpu < nr_cpu_ids)
252 return cpu;
253 }
254 if (sw & 2)
255 return WORK_CPU_UNBOUND;
256 }
Lai Jiangshan6be19582013-02-06 18:04:53 -0800257 return WORK_CPU_END;
Tejun Heof3421792010-07-02 10:03:51 +0200258}
259
Tejun Heo09884952010-08-01 11:50:12 +0200260/*
261 * CPU iterators
262 *
Tejun Heo706026c2013-01-24 11:01:34 -0800263 * An extra cpu number is defined using an invalid cpu number
Tejun Heo09884952010-08-01 11:50:12 +0200264 * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any
Tejun Heo706026c2013-01-24 11:01:34 -0800265 * specific CPU. The following iterators are similar to for_each_*_cpu()
266 * iterators but also considers the unbound CPU.
Tejun Heo09884952010-08-01 11:50:12 +0200267 *
Tejun Heo706026c2013-01-24 11:01:34 -0800268 * for_each_wq_cpu() : possible CPUs + WORK_CPU_UNBOUND
269 * for_each_online_wq_cpu() : online CPUs + WORK_CPU_UNBOUND
Tejun Heo09884952010-08-01 11:50:12 +0200270 */
Tejun Heo706026c2013-01-24 11:01:34 -0800271#define for_each_wq_cpu(cpu) \
272 for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, 3); \
Lai Jiangshan6be19582013-02-06 18:04:53 -0800273 (cpu) < WORK_CPU_END; \
Tejun Heo706026c2013-01-24 11:01:34 -0800274 (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, 3))
Tejun Heof3421792010-07-02 10:03:51 +0200275
Tejun Heo706026c2013-01-24 11:01:34 -0800276#define for_each_online_wq_cpu(cpu) \
277 for ((cpu) = __next_wq_cpu(-1, cpu_online_mask, 3); \
Lai Jiangshan6be19582013-02-06 18:04:53 -0800278 (cpu) < WORK_CPU_END; \
Tejun Heo706026c2013-01-24 11:01:34 -0800279 (cpu) = __next_wq_cpu((cpu), cpu_online_mask, 3))
Tejun Heof3421792010-07-02 10:03:51 +0200280
Tejun Heo49e3cf42013-03-12 11:29:58 -0700281/**
Tejun Heo17116962013-03-12 11:29:58 -0700282 * for_each_pool - iterate through all worker_pools in the system
283 * @pool: iteration cursor
284 * @id: integer used for iteration
285 */
286#define for_each_pool(pool, id) \
287 idr_for_each_entry(&worker_pool_idr, pool, id)
288
289/**
Tejun Heo49e3cf42013-03-12 11:29:58 -0700290 * for_each_pwq - iterate through all pool_workqueues of the specified workqueue
291 * @pwq: iteration cursor
292 * @wq: the target workqueue
Tejun Heo76af4d92013-03-12 11:30:00 -0700293 *
294 * This must be called either with workqueue_lock held or sched RCU read
295 * locked. If the pwq needs to be used beyond the locking in effect, the
296 * caller is responsible for guaranteeing that the pwq stays online.
297 *
298 * The if/else clause exists only for the lockdep assertion and can be
299 * ignored.
Tejun Heo49e3cf42013-03-12 11:29:58 -0700300 */
301#define for_each_pwq(pwq, wq) \
Tejun Heo76af4d92013-03-12 11:30:00 -0700302 list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node) \
303 if (({ assert_rcu_or_wq_lock(); false; })) { } \
304 else
Tejun Heof3421792010-07-02 10:03:51 +0200305
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900306#ifdef CONFIG_DEBUG_OBJECTS_WORK
307
308static struct debug_obj_descr work_debug_descr;
309
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100310static void *work_debug_hint(void *addr)
311{
312 return ((struct work_struct *) addr)->func;
313}
314
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900315/*
316 * fixup_init is called when:
317 * - an active object is initialized
318 */
319static int work_fixup_init(void *addr, enum debug_obj_state state)
320{
321 struct work_struct *work = addr;
322
323 switch (state) {
324 case ODEBUG_STATE_ACTIVE:
325 cancel_work_sync(work);
326 debug_object_init(work, &work_debug_descr);
327 return 1;
328 default:
329 return 0;
330 }
331}
332
333/*
334 * fixup_activate is called when:
335 * - an active object is activated
336 * - an unknown object is activated (might be a statically initialized object)
337 */
338static int work_fixup_activate(void *addr, enum debug_obj_state state)
339{
340 struct work_struct *work = addr;
341
342 switch (state) {
343
344 case ODEBUG_STATE_NOTAVAILABLE:
345 /*
346 * This is not really a fixup. The work struct was
347 * statically initialized. We just make sure that it
348 * is tracked in the object tracker.
349 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200350 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900351 debug_object_init(work, &work_debug_descr);
352 debug_object_activate(work, &work_debug_descr);
353 return 0;
354 }
355 WARN_ON_ONCE(1);
356 return 0;
357
358 case ODEBUG_STATE_ACTIVE:
359 WARN_ON(1);
360
361 default:
362 return 0;
363 }
364}
365
366/*
367 * fixup_free is called when:
368 * - an active object is freed
369 */
370static int work_fixup_free(void *addr, enum debug_obj_state state)
371{
372 struct work_struct *work = addr;
373
374 switch (state) {
375 case ODEBUG_STATE_ACTIVE:
376 cancel_work_sync(work);
377 debug_object_free(work, &work_debug_descr);
378 return 1;
379 default:
380 return 0;
381 }
382}
383
384static struct debug_obj_descr work_debug_descr = {
385 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100386 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900387 .fixup_init = work_fixup_init,
388 .fixup_activate = work_fixup_activate,
389 .fixup_free = work_fixup_free,
390};
391
392static inline void debug_work_activate(struct work_struct *work)
393{
394 debug_object_activate(work, &work_debug_descr);
395}
396
397static inline void debug_work_deactivate(struct work_struct *work)
398{
399 debug_object_deactivate(work, &work_debug_descr);
400}
401
402void __init_work(struct work_struct *work, int onstack)
403{
404 if (onstack)
405 debug_object_init_on_stack(work, &work_debug_descr);
406 else
407 debug_object_init(work, &work_debug_descr);
408}
409EXPORT_SYMBOL_GPL(__init_work);
410
411void destroy_work_on_stack(struct work_struct *work)
412{
413 debug_object_free(work, &work_debug_descr);
414}
415EXPORT_SYMBOL_GPL(destroy_work_on_stack);
416
417#else
418static inline void debug_work_activate(struct work_struct *work) { }
419static inline void debug_work_deactivate(struct work_struct *work) { }
420#endif
421
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100422/* Serializes the accesses to the list of workqueues. */
423static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424static LIST_HEAD(workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200425static bool workqueue_freezing; /* W: have wqs started freezing? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Oleg Nesterov14441962007-05-23 13:57:57 -0700427/*
Tejun Heoe19e3972013-01-24 11:39:44 -0800428 * The CPU and unbound standard worker pools. The unbound ones have
429 * POOL_DISASSOCIATED set, and their workers have WORKER_UNBOUND set.
Oleg Nesterov14441962007-05-23 13:57:57 -0700430 */
Tejun Heoe19e3972013-01-24 11:39:44 -0800431static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS],
432 cpu_std_worker_pools);
Tejun Heoa60dc392013-01-24 11:01:34 -0800433static struct worker_pool unbound_std_worker_pools[NR_STD_WORKER_POOLS];
Tejun Heof3421792010-07-02 10:03:51 +0200434
Tejun Heo9daf9e62013-01-24 11:01:33 -0800435/* idr of all pools */
436static DEFINE_MUTEX(worker_pool_idr_mutex);
437static DEFINE_IDR(worker_pool_idr);
438
Tejun Heoc34056a2010-06-29 10:07:11 +0200439static int worker_thread(void *__worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Tejun Heoa60dc392013-01-24 11:01:34 -0800441static struct worker_pool *std_worker_pools(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Tejun Heof3421792010-07-02 10:03:51 +0200443 if (cpu != WORK_CPU_UNBOUND)
Tejun Heoa60dc392013-01-24 11:01:34 -0800444 return per_cpu(cpu_std_worker_pools, cpu);
Tejun Heof3421792010-07-02 10:03:51 +0200445 else
Tejun Heoa60dc392013-01-24 11:01:34 -0800446 return unbound_std_worker_pools;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
Tejun Heo4e8f0a62013-01-24 11:01:34 -0800449static int std_worker_pool_pri(struct worker_pool *pool)
450{
Tejun Heoa60dc392013-01-24 11:01:34 -0800451 return pool - std_worker_pools(pool->cpu);
Tejun Heo4e8f0a62013-01-24 11:01:34 -0800452}
453
Tejun Heo9daf9e62013-01-24 11:01:33 -0800454/* allocate ID and assign it to @pool */
455static int worker_pool_assign_id(struct worker_pool *pool)
456{
457 int ret;
458
459 mutex_lock(&worker_pool_idr_mutex);
460 idr_pre_get(&worker_pool_idr, GFP_KERNEL);
461 ret = idr_get_new(&worker_pool_idr, pool, &pool->id);
462 mutex_unlock(&worker_pool_idr_mutex);
463
464 return ret;
465}
466
Tejun Heo7c3eed52013-01-24 11:01:33 -0800467/*
468 * Lookup worker_pool by id. The idr currently is built during boot and
469 * never modified. Don't worry about locking for now.
470 */
471static struct worker_pool *worker_pool_by_id(int pool_id)
472{
473 return idr_find(&worker_pool_idr, pool_id);
474}
475
Tejun Heod565ed62013-01-24 11:01:33 -0800476static struct worker_pool *get_std_worker_pool(int cpu, bool highpri)
477{
Tejun Heoa60dc392013-01-24 11:01:34 -0800478 struct worker_pool *pools = std_worker_pools(cpu);
Tejun Heod565ed62013-01-24 11:01:33 -0800479
Tejun Heoa60dc392013-01-24 11:01:34 -0800480 return &pools[highpri];
Tejun Heod565ed62013-01-24 11:01:33 -0800481}
482
Tejun Heo76af4d92013-03-12 11:30:00 -0700483/**
484 * first_pwq - return the first pool_workqueue of the specified workqueue
485 * @wq: the target workqueue
486 *
487 * This must be called either with workqueue_lock held or sched RCU read
488 * locked. If the pwq needs to be used beyond the locking in effect, the
489 * caller is responsible for guaranteeing that the pwq stays online.
490 */
Tejun Heo7fb98ea2013-03-12 11:30:00 -0700491static struct pool_workqueue *first_pwq(struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700492{
Tejun Heo76af4d92013-03-12 11:30:00 -0700493 assert_rcu_or_wq_lock();
494 return list_first_or_null_rcu(&wq->pwqs, struct pool_workqueue,
495 pwqs_node);
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700496}
497
Tejun Heo73f53c42010-06-29 10:07:11 +0200498static unsigned int work_color_to_flags(int color)
499{
500 return color << WORK_STRUCT_COLOR_SHIFT;
501}
502
503static int get_work_color(struct work_struct *work)
504{
505 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
506 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
507}
508
509static int work_next_color(int color)
510{
511 return (color + 1) % WORK_NR_COLORS;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700512}
513
David Howells4594bf12006-12-07 11:33:26 +0000514/*
Tejun Heo112202d2013-02-13 19:29:12 -0800515 * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data
516 * contain the pointer to the queued pwq. Once execution starts, the flag
Tejun Heo7c3eed52013-01-24 11:01:33 -0800517 * is cleared and the high bits contain OFFQ flags and pool ID.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200518 *
Tejun Heo112202d2013-02-13 19:29:12 -0800519 * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling()
520 * and clear_work_data() can be used to set the pwq, pool or clear
Tejun Heobbb68df2012-08-03 10:30:46 -0700521 * work->data. These functions should only be called while the work is
522 * owned - ie. while the PENDING bit is set.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200523 *
Tejun Heo112202d2013-02-13 19:29:12 -0800524 * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq
Tejun Heo7c3eed52013-01-24 11:01:33 -0800525 * corresponding to a work. Pool is available once the work has been
Tejun Heo112202d2013-02-13 19:29:12 -0800526 * queued anywhere after initialization until it is sync canceled. pwq is
Tejun Heo7c3eed52013-01-24 11:01:33 -0800527 * available only while the work item is queued.
Tejun Heobbb68df2012-08-03 10:30:46 -0700528 *
529 * %WORK_OFFQ_CANCELING is used to mark a work item which is being
530 * canceled. While being canceled, a work item may have its PENDING set
531 * but stay off timer and worklist for arbitrarily long and nobody should
532 * try to steal the PENDING bit.
David Howells4594bf12006-12-07 11:33:26 +0000533 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200534static inline void set_work_data(struct work_struct *work, unsigned long data,
535 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000536{
Tejun Heo6183c002013-03-12 11:29:57 -0700537 WARN_ON_ONCE(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200538 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000539}
David Howells365970a2006-11-22 14:54:49 +0000540
Tejun Heo112202d2013-02-13 19:29:12 -0800541static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq,
Tejun Heo7a22ad72010-06-29 10:07:13 +0200542 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200543{
Tejun Heo112202d2013-02-13 19:29:12 -0800544 set_work_data(work, (unsigned long)pwq,
545 WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200546}
547
Lai Jiangshan4468a002013-02-06 18:04:53 -0800548static void set_work_pool_and_keep_pending(struct work_struct *work,
549 int pool_id)
550{
551 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT,
552 WORK_STRUCT_PENDING);
553}
554
Tejun Heo7c3eed52013-01-24 11:01:33 -0800555static void set_work_pool_and_clear_pending(struct work_struct *work,
556 int pool_id)
David Howells365970a2006-11-22 14:54:49 +0000557{
Tejun Heo23657bb2012-08-13 17:08:19 -0700558 /*
559 * The following wmb is paired with the implied mb in
560 * test_and_set_bit(PENDING) and ensures all updates to @work made
561 * here are visible to and precede any updates by the next PENDING
562 * owner.
563 */
564 smp_wmb();
Tejun Heo7c3eed52013-01-24 11:01:33 -0800565 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200566}
567
568static void clear_work_data(struct work_struct *work)
569{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800570 smp_wmb(); /* see set_work_pool_and_clear_pending() */
571 set_work_data(work, WORK_STRUCT_NO_POOL, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200572}
573
Tejun Heo112202d2013-02-13 19:29:12 -0800574static struct pool_workqueue *get_work_pwq(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200575{
Tejun Heoe1201532010-07-22 14:14:25 +0200576 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200577
Tejun Heo112202d2013-02-13 19:29:12 -0800578 if (data & WORK_STRUCT_PWQ)
Tejun Heoe1201532010-07-22 14:14:25 +0200579 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
580 else
581 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200582}
583
Tejun Heo7c3eed52013-01-24 11:01:33 -0800584/**
585 * get_work_pool - return the worker_pool a given work was associated with
586 * @work: the work item of interest
587 *
588 * Return the worker_pool @work was last associated with. %NULL if none.
589 */
590static struct worker_pool *get_work_pool(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200591{
Tejun Heoe1201532010-07-22 14:14:25 +0200592 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800593 struct worker_pool *pool;
594 int pool_id;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200595
Tejun Heo112202d2013-02-13 19:29:12 -0800596 if (data & WORK_STRUCT_PWQ)
597 return ((struct pool_workqueue *)
Tejun Heo7c3eed52013-01-24 11:01:33 -0800598 (data & WORK_STRUCT_WQ_DATA_MASK))->pool;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200599
Tejun Heo7c3eed52013-01-24 11:01:33 -0800600 pool_id = data >> WORK_OFFQ_POOL_SHIFT;
601 if (pool_id == WORK_OFFQ_POOL_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200602 return NULL;
603
Tejun Heo7c3eed52013-01-24 11:01:33 -0800604 pool = worker_pool_by_id(pool_id);
605 WARN_ON_ONCE(!pool);
606 return pool;
607}
608
609/**
610 * get_work_pool_id - return the worker pool ID a given work is associated with
611 * @work: the work item of interest
612 *
613 * Return the worker_pool ID @work was last associated with.
614 * %WORK_OFFQ_POOL_NONE if none.
615 */
616static int get_work_pool_id(struct work_struct *work)
617{
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800618 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800619
Tejun Heo112202d2013-02-13 19:29:12 -0800620 if (data & WORK_STRUCT_PWQ)
621 return ((struct pool_workqueue *)
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800622 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id;
623
624 return data >> WORK_OFFQ_POOL_SHIFT;
Tejun Heo7c3eed52013-01-24 11:01:33 -0800625}
626
Tejun Heobbb68df2012-08-03 10:30:46 -0700627static void mark_work_canceling(struct work_struct *work)
628{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800629 unsigned long pool_id = get_work_pool_id(work);
Tejun Heobbb68df2012-08-03 10:30:46 -0700630
Tejun Heo7c3eed52013-01-24 11:01:33 -0800631 pool_id <<= WORK_OFFQ_POOL_SHIFT;
632 set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700633}
634
635static bool work_is_canceling(struct work_struct *work)
636{
637 unsigned long data = atomic_long_read(&work->data);
638
Tejun Heo112202d2013-02-13 19:29:12 -0800639 return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700640}
641
David Howells365970a2006-11-22 14:54:49 +0000642/*
Tejun Heo32704762012-07-13 22:16:45 -0700643 * Policy functions. These define the policies on how the global worker
644 * pools are managed. Unless noted otherwise, these functions assume that
Tejun Heod565ed62013-01-24 11:01:33 -0800645 * they're being called with pool->lock held.
David Howells365970a2006-11-22 14:54:49 +0000646 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200647
Tejun Heo63d95a92012-07-12 14:46:37 -0700648static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000649{
Tejun Heoe19e3972013-01-24 11:39:44 -0800650 return !atomic_read(&pool->nr_running);
David Howells365970a2006-11-22 14:54:49 +0000651}
652
Tejun Heoe22bee72010-06-29 10:07:14 +0200653/*
654 * Need to wake up a worker? Called from anything but currently
655 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700656 *
657 * Note that, because unbound workers never contribute to nr_running, this
Tejun Heo706026c2013-01-24 11:01:34 -0800658 * function will always return %true for unbound pools as long as the
Tejun Heo974271c2012-07-12 14:46:37 -0700659 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200660 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700661static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000662{
Tejun Heo63d95a92012-07-12 14:46:37 -0700663 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000664}
665
Tejun Heoe22bee72010-06-29 10:07:14 +0200666/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700667static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200668{
Tejun Heo63d95a92012-07-12 14:46:37 -0700669 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200670}
671
672/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700673static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200674{
Tejun Heoe19e3972013-01-24 11:39:44 -0800675 return !list_empty(&pool->worklist) &&
676 atomic_read(&pool->nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200677}
678
679/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700680static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200681{
Tejun Heo63d95a92012-07-12 14:46:37 -0700682 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200683}
684
685/* Do I need to be the manager? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700686static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200687{
Tejun Heo63d95a92012-07-12 14:46:37 -0700688 return need_to_create_worker(pool) ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700689 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200690}
691
692/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700693static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200694{
Lai Jiangshan552a37e2012-09-10 10:03:33 -0700695 bool managing = pool->flags & POOL_MANAGING_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -0700696 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
697 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200698
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700699 /*
700 * nr_idle and idle_list may disagree if idle rebinding is in
701 * progress. Never return %true if idle_list is empty.
702 */
703 if (list_empty(&pool->idle_list))
704 return false;
705
Tejun Heoe22bee72010-06-29 10:07:14 +0200706 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
707}
708
709/*
710 * Wake up functions.
711 */
712
Tejun Heo7e116292010-06-29 10:07:13 +0200713/* Return the first worker. Safe with preemption disabled */
Tejun Heo63d95a92012-07-12 14:46:37 -0700714static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200715{
Tejun Heo63d95a92012-07-12 14:46:37 -0700716 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200717 return NULL;
718
Tejun Heo63d95a92012-07-12 14:46:37 -0700719 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200720}
721
722/**
723 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700724 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200725 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700726 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200727 *
728 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800729 * spin_lock_irq(pool->lock).
Tejun Heo7e116292010-06-29 10:07:13 +0200730 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700731static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200732{
Tejun Heo63d95a92012-07-12 14:46:37 -0700733 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200734
735 if (likely(worker))
736 wake_up_process(worker->task);
737}
738
Tejun Heo4690c4a2010-06-29 10:07:10 +0200739/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200740 * wq_worker_waking_up - a worker is waking up
741 * @task: task waking up
742 * @cpu: CPU @task is waking up to
743 *
744 * This function is called during try_to_wake_up() when a worker is
745 * being awoken.
746 *
747 * CONTEXT:
748 * spin_lock_irq(rq->lock)
749 */
Tejun Heod84ff052013-03-12 11:29:59 -0700750void wq_worker_waking_up(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200751{
752 struct worker *worker = kthread_data(task);
753
Joonsoo Kim36576002012-10-26 23:03:49 +0900754 if (!(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoec22ca52013-01-24 11:01:33 -0800755 WARN_ON_ONCE(worker->pool->cpu != cpu);
Tejun Heoe19e3972013-01-24 11:39:44 -0800756 atomic_inc(&worker->pool->nr_running);
Joonsoo Kim36576002012-10-26 23:03:49 +0900757 }
Tejun Heoe22bee72010-06-29 10:07:14 +0200758}
759
760/**
761 * wq_worker_sleeping - a worker is going to sleep
762 * @task: task going to sleep
763 * @cpu: CPU in question, must be the current CPU number
764 *
765 * This function is called during schedule() when a busy worker is
766 * going to sleep. Worker on the same cpu can be woken up by
767 * returning pointer to its task.
768 *
769 * CONTEXT:
770 * spin_lock_irq(rq->lock)
771 *
772 * RETURNS:
773 * Worker task on @cpu to wake up, %NULL if none.
774 */
Tejun Heod84ff052013-03-12 11:29:59 -0700775struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200776{
777 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heo111c2252013-01-17 17:16:24 -0800778 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200779
Tejun Heo111c2252013-01-17 17:16:24 -0800780 /*
781 * Rescuers, which may not have all the fields set up like normal
782 * workers, also reach here, let's not access anything before
783 * checking NOT_RUNNING.
784 */
Steven Rostedt2d646722010-12-03 23:12:33 -0500785 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200786 return NULL;
787
Tejun Heo111c2252013-01-17 17:16:24 -0800788 pool = worker->pool;
Tejun Heo111c2252013-01-17 17:16:24 -0800789
Tejun Heoe22bee72010-06-29 10:07:14 +0200790 /* this can only happen on the local cpu */
Tejun Heo6183c002013-03-12 11:29:57 -0700791 if (WARN_ON_ONCE(cpu != raw_smp_processor_id()))
792 return NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +0200793
794 /*
795 * The counterpart of the following dec_and_test, implied mb,
796 * worklist not empty test sequence is in insert_work().
797 * Please read comment there.
798 *
Tejun Heo628c78e2012-07-17 12:39:27 -0700799 * NOT_RUNNING is clear. This means that we're bound to and
800 * running on the local cpu w/ rq lock held and preemption
801 * disabled, which in turn means that none else could be
Tejun Heod565ed62013-01-24 11:01:33 -0800802 * manipulating idle_list, so dereferencing idle_list without pool
Tejun Heo628c78e2012-07-17 12:39:27 -0700803 * lock is safe.
Tejun Heoe22bee72010-06-29 10:07:14 +0200804 */
Tejun Heoe19e3972013-01-24 11:39:44 -0800805 if (atomic_dec_and_test(&pool->nr_running) &&
806 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700807 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200808 return to_wakeup ? to_wakeup->task : NULL;
809}
810
811/**
812 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200813 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200814 * @flags: flags to set
815 * @wakeup: wakeup an idle worker if necessary
816 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200817 * Set @flags in @worker->flags and adjust nr_running accordingly. If
818 * nr_running becomes zero and @wakeup is %true, an idle worker is
819 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200820 *
Tejun Heocb444762010-07-02 10:03:50 +0200821 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800822 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200823 */
824static inline void worker_set_flags(struct worker *worker, unsigned int flags,
825 bool wakeup)
826{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700827 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200828
Tejun Heocb444762010-07-02 10:03:50 +0200829 WARN_ON_ONCE(worker->task != current);
830
Tejun Heoe22bee72010-06-29 10:07:14 +0200831 /*
832 * If transitioning into NOT_RUNNING, adjust nr_running and
833 * wake up an idle worker as necessary if requested by
834 * @wakeup.
835 */
836 if ((flags & WORKER_NOT_RUNNING) &&
837 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoe22bee72010-06-29 10:07:14 +0200838 if (wakeup) {
Tejun Heoe19e3972013-01-24 11:39:44 -0800839 if (atomic_dec_and_test(&pool->nr_running) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -0700840 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700841 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200842 } else
Tejun Heoe19e3972013-01-24 11:39:44 -0800843 atomic_dec(&pool->nr_running);
Tejun Heoe22bee72010-06-29 10:07:14 +0200844 }
845
Tejun Heod302f012010-06-29 10:07:13 +0200846 worker->flags |= flags;
847}
848
849/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200850 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200851 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200852 * @flags: flags to clear
853 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200854 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200855 *
Tejun Heocb444762010-07-02 10:03:50 +0200856 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800857 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200858 */
859static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
860{
Tejun Heo63d95a92012-07-12 14:46:37 -0700861 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200862 unsigned int oflags = worker->flags;
863
Tejun Heocb444762010-07-02 10:03:50 +0200864 WARN_ON_ONCE(worker->task != current);
865
Tejun Heod302f012010-06-29 10:07:13 +0200866 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200867
Tejun Heo42c025f2011-01-11 15:58:49 +0100868 /*
869 * If transitioning out of NOT_RUNNING, increment nr_running. Note
870 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
871 * of multiple flags, not a single flag.
872 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200873 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
874 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heoe19e3972013-01-24 11:39:44 -0800875 atomic_inc(&pool->nr_running);
Tejun Heod302f012010-06-29 10:07:13 +0200876}
877
878/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200879 * find_worker_executing_work - find worker which is executing a work
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800880 * @pool: pool of interest
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200881 * @work: work to find worker for
882 *
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800883 * Find a worker which is executing @work on @pool by searching
884 * @pool->busy_hash which is keyed by the address of @work. For a worker
Tejun Heoa2c1c572012-12-18 10:35:02 -0800885 * to match, its current execution should match the address of @work and
886 * its work function. This is to avoid unwanted dependency between
887 * unrelated work executions through a work item being recycled while still
888 * being executed.
889 *
890 * This is a bit tricky. A work item may be freed once its execution
891 * starts and nothing prevents the freed area from being recycled for
892 * another work item. If the same work item address ends up being reused
893 * before the original execution finishes, workqueue will identify the
894 * recycled work item as currently executing and make it wait until the
895 * current execution finishes, introducing an unwanted dependency.
896 *
897 * This function checks the work item address, work function and workqueue
898 * to avoid false positives. Note that this isn't complete as one may
899 * construct a work function which can introduce dependency onto itself
900 * through a recycled work item. Well, if somebody wants to shoot oneself
901 * in the foot that badly, there's only so much we can do, and if such
902 * deadlock actually occurs, it should be easy to locate the culprit work
903 * function.
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200904 *
905 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800906 * spin_lock_irq(pool->lock).
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200907 *
908 * RETURNS:
909 * Pointer to worker which is executing @work if found, NULL
910 * otherwise.
911 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800912static struct worker *find_worker_executing_work(struct worker_pool *pool,
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200913 struct work_struct *work)
914{
Sasha Levin42f85702012-12-17 10:01:23 -0500915 struct worker *worker;
Sasha Levin42f85702012-12-17 10:01:23 -0500916
Sasha Levinb67bfe02013-02-27 17:06:00 -0800917 hash_for_each_possible(pool->busy_hash, worker, hentry,
Tejun Heoa2c1c572012-12-18 10:35:02 -0800918 (unsigned long)work)
919 if (worker->current_work == work &&
920 worker->current_func == work->func)
Sasha Levin42f85702012-12-17 10:01:23 -0500921 return worker;
922
923 return NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200924}
925
926/**
Tejun Heobf4ede02012-08-03 10:30:46 -0700927 * move_linked_works - move linked works to a list
928 * @work: start of series of works to be scheduled
929 * @head: target list to append @work to
930 * @nextp: out paramter for nested worklist walking
931 *
932 * Schedule linked works starting from @work to @head. Work series to
933 * be scheduled starts at @work and includes any consecutive work with
934 * WORK_STRUCT_LINKED set in its predecessor.
935 *
936 * If @nextp is not NULL, it's updated to point to the next work of
937 * the last scheduled work. This allows move_linked_works() to be
938 * nested inside outer list_for_each_entry_safe().
939 *
940 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800941 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -0700942 */
943static void move_linked_works(struct work_struct *work, struct list_head *head,
944 struct work_struct **nextp)
945{
946 struct work_struct *n;
947
948 /*
949 * Linked worklist will always end before the end of the list,
950 * use NULL for list head.
951 */
952 list_for_each_entry_safe_from(work, n, NULL, entry) {
953 list_move_tail(&work->entry, head);
954 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
955 break;
956 }
957
958 /*
959 * If we're already inside safe list traversal and have moved
960 * multiple works to the scheduled queue, the next position
961 * needs to be updated.
962 */
963 if (nextp)
964 *nextp = n;
965}
966
Tejun Heo112202d2013-02-13 19:29:12 -0800967static void pwq_activate_delayed_work(struct work_struct *work)
Tejun Heobf4ede02012-08-03 10:30:46 -0700968{
Tejun Heo112202d2013-02-13 19:29:12 -0800969 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobf4ede02012-08-03 10:30:46 -0700970
971 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -0800972 move_linked_works(work, &pwq->pool->worklist, NULL);
Tejun Heobf4ede02012-08-03 10:30:46 -0700973 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo112202d2013-02-13 19:29:12 -0800974 pwq->nr_active++;
Tejun Heobf4ede02012-08-03 10:30:46 -0700975}
976
Tejun Heo112202d2013-02-13 19:29:12 -0800977static void pwq_activate_first_delayed(struct pool_workqueue *pwq)
Lai Jiangshan3aa62492012-09-18 10:40:00 -0700978{
Tejun Heo112202d2013-02-13 19:29:12 -0800979 struct work_struct *work = list_first_entry(&pwq->delayed_works,
Lai Jiangshan3aa62492012-09-18 10:40:00 -0700980 struct work_struct, entry);
981
Tejun Heo112202d2013-02-13 19:29:12 -0800982 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -0700983}
984
Tejun Heobf4ede02012-08-03 10:30:46 -0700985/**
Tejun Heo112202d2013-02-13 19:29:12 -0800986 * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight
987 * @pwq: pwq of interest
Tejun Heobf4ede02012-08-03 10:30:46 -0700988 * @color: color of work which left the queue
Tejun Heobf4ede02012-08-03 10:30:46 -0700989 *
990 * A work either has completed or is removed from pending queue,
Tejun Heo112202d2013-02-13 19:29:12 -0800991 * decrement nr_in_flight of its pwq and handle workqueue flushing.
Tejun Heobf4ede02012-08-03 10:30:46 -0700992 *
993 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800994 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -0700995 */
Tejun Heo112202d2013-02-13 19:29:12 -0800996static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color)
Tejun Heobf4ede02012-08-03 10:30:46 -0700997{
998 /* ignore uncolored works */
999 if (color == WORK_NO_COLOR)
1000 return;
1001
Tejun Heo112202d2013-02-13 19:29:12 -08001002 pwq->nr_in_flight[color]--;
Tejun Heobf4ede02012-08-03 10:30:46 -07001003
Tejun Heo112202d2013-02-13 19:29:12 -08001004 pwq->nr_active--;
1005 if (!list_empty(&pwq->delayed_works)) {
Lai Jiangshanb3f9f402012-09-18 10:40:00 -07001006 /* one down, submit a delayed one */
Tejun Heo112202d2013-02-13 19:29:12 -08001007 if (pwq->nr_active < pwq->max_active)
1008 pwq_activate_first_delayed(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001009 }
1010
1011 /* is flush in progress and are we at the flushing tip? */
Tejun Heo112202d2013-02-13 19:29:12 -08001012 if (likely(pwq->flush_color != color))
Tejun Heobf4ede02012-08-03 10:30:46 -07001013 return;
1014
1015 /* are there still in-flight works? */
Tejun Heo112202d2013-02-13 19:29:12 -08001016 if (pwq->nr_in_flight[color])
Tejun Heobf4ede02012-08-03 10:30:46 -07001017 return;
1018
Tejun Heo112202d2013-02-13 19:29:12 -08001019 /* this pwq is done, clear flush_color */
1020 pwq->flush_color = -1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001021
1022 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001023 * If this was the last pwq, wake up the first flusher. It
Tejun Heobf4ede02012-08-03 10:30:46 -07001024 * will handle the rest.
1025 */
Tejun Heo112202d2013-02-13 19:29:12 -08001026 if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush))
1027 complete(&pwq->wq->first_flusher->done);
Tejun Heobf4ede02012-08-03 10:30:46 -07001028}
1029
Tejun Heo36e227d2012-08-03 10:30:46 -07001030/**
Tejun Heobbb68df2012-08-03 10:30:46 -07001031 * try_to_grab_pending - steal work item from worklist and disable irq
Tejun Heo36e227d2012-08-03 10:30:46 -07001032 * @work: work item to steal
1033 * @is_dwork: @work is a delayed_work
Tejun Heobbb68df2012-08-03 10:30:46 -07001034 * @flags: place to store irq state
Tejun Heo36e227d2012-08-03 10:30:46 -07001035 *
1036 * Try to grab PENDING bit of @work. This function can handle @work in any
1037 * stable state - idle, on timer or on worklist. Return values are
1038 *
1039 * 1 if @work was pending and we successfully stole PENDING
1040 * 0 if @work was idle and we claimed PENDING
1041 * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry
Tejun Heobbb68df2012-08-03 10:30:46 -07001042 * -ENOENT if someone else is canceling @work, this state may persist
1043 * for arbitrarily long
Tejun Heo36e227d2012-08-03 10:30:46 -07001044 *
Tejun Heobbb68df2012-08-03 10:30:46 -07001045 * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001046 * interrupted while holding PENDING and @work off queue, irq must be
1047 * disabled on entry. This, combined with delayed_work->timer being
1048 * irqsafe, ensures that we return -EAGAIN for finite short period of time.
Tejun Heobbb68df2012-08-03 10:30:46 -07001049 *
1050 * On successful return, >= 0, irq is disabled and the caller is
1051 * responsible for releasing it using local_irq_restore(*@flags).
1052 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001053 * This function is safe to call from any context including IRQ handler.
Tejun Heobf4ede02012-08-03 10:30:46 -07001054 */
Tejun Heobbb68df2012-08-03 10:30:46 -07001055static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
1056 unsigned long *flags)
Tejun Heobf4ede02012-08-03 10:30:46 -07001057{
Tejun Heod565ed62013-01-24 11:01:33 -08001058 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08001059 struct pool_workqueue *pwq;
Tejun Heobf4ede02012-08-03 10:30:46 -07001060
Tejun Heobbb68df2012-08-03 10:30:46 -07001061 local_irq_save(*flags);
1062
Tejun Heo36e227d2012-08-03 10:30:46 -07001063 /* try to steal the timer if it exists */
1064 if (is_dwork) {
1065 struct delayed_work *dwork = to_delayed_work(work);
1066
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001067 /*
1068 * dwork->timer is irqsafe. If del_timer() fails, it's
1069 * guaranteed that the timer is not queued anywhere and not
1070 * running on the local CPU.
1071 */
Tejun Heo36e227d2012-08-03 10:30:46 -07001072 if (likely(del_timer(&dwork->timer)))
1073 return 1;
1074 }
1075
1076 /* try to claim PENDING the normal way */
Tejun Heobf4ede02012-08-03 10:30:46 -07001077 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1078 return 0;
1079
1080 /*
1081 * The queueing is in progress, or it is already queued. Try to
1082 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1083 */
Tejun Heod565ed62013-01-24 11:01:33 -08001084 pool = get_work_pool(work);
1085 if (!pool)
Tejun Heobbb68df2012-08-03 10:30:46 -07001086 goto fail;
Tejun Heobf4ede02012-08-03 10:30:46 -07001087
Tejun Heod565ed62013-01-24 11:01:33 -08001088 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001089 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001090 * work->data is guaranteed to point to pwq only while the work
1091 * item is queued on pwq->wq, and both updating work->data to point
1092 * to pwq on queueing and to pool on dequeueing are done under
1093 * pwq->pool->lock. This in turn guarantees that, if work->data
1094 * points to pwq which is associated with a locked pool, the work
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001095 * item is currently queued on that pool.
1096 */
Tejun Heo112202d2013-02-13 19:29:12 -08001097 pwq = get_work_pwq(work);
1098 if (pwq && pwq->pool == pool) {
Tejun Heo16062832013-02-06 18:04:53 -08001099 debug_work_deactivate(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001100
Tejun Heo16062832013-02-06 18:04:53 -08001101 /*
1102 * A delayed work item cannot be grabbed directly because
1103 * it might have linked NO_COLOR work items which, if left
Tejun Heo112202d2013-02-13 19:29:12 -08001104 * on the delayed_list, will confuse pwq->nr_active
Tejun Heo16062832013-02-06 18:04:53 -08001105 * management later on and cause stall. Make sure the work
1106 * item is activated before grabbing.
1107 */
1108 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
Tejun Heo112202d2013-02-13 19:29:12 -08001109 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001110
Tejun Heo16062832013-02-06 18:04:53 -08001111 list_del_init(&work->entry);
Tejun Heo112202d2013-02-13 19:29:12 -08001112 pwq_dec_nr_in_flight(get_work_pwq(work), get_work_color(work));
Tejun Heo36e227d2012-08-03 10:30:46 -07001113
Tejun Heo112202d2013-02-13 19:29:12 -08001114 /* work->data points to pwq iff queued, point to pool */
Tejun Heo16062832013-02-06 18:04:53 -08001115 set_work_pool_and_keep_pending(work, pool->id);
Lai Jiangshan4468a002013-02-06 18:04:53 -08001116
Tejun Heo16062832013-02-06 18:04:53 -08001117 spin_unlock(&pool->lock);
1118 return 1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001119 }
Tejun Heod565ed62013-01-24 11:01:33 -08001120 spin_unlock(&pool->lock);
Tejun Heobbb68df2012-08-03 10:30:46 -07001121fail:
1122 local_irq_restore(*flags);
1123 if (work_is_canceling(work))
1124 return -ENOENT;
1125 cpu_relax();
Tejun Heo36e227d2012-08-03 10:30:46 -07001126 return -EAGAIN;
Tejun Heobf4ede02012-08-03 10:30:46 -07001127}
1128
1129/**
Tejun Heo706026c2013-01-24 11:01:34 -08001130 * insert_work - insert a work into a pool
Tejun Heo112202d2013-02-13 19:29:12 -08001131 * @pwq: pwq @work belongs to
Tejun Heo4690c4a2010-06-29 10:07:10 +02001132 * @work: work to insert
1133 * @head: insertion point
1134 * @extra_flags: extra WORK_STRUCT_* flags to set
1135 *
Tejun Heo112202d2013-02-13 19:29:12 -08001136 * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to
Tejun Heo706026c2013-01-24 11:01:34 -08001137 * work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +02001138 *
1139 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001140 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02001141 */
Tejun Heo112202d2013-02-13 19:29:12 -08001142static void insert_work(struct pool_workqueue *pwq, struct work_struct *work,
1143 struct list_head *head, unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001144{
Tejun Heo112202d2013-02-13 19:29:12 -08001145 struct worker_pool *pool = pwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01001146
Tejun Heo4690c4a2010-06-29 10:07:10 +02001147 /* we own @work, set data and link */
Tejun Heo112202d2013-02-13 19:29:12 -08001148 set_work_pwq(work, pwq, extra_flags);
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -07001149 list_add_tail(&work->entry, head);
Tejun Heoe22bee72010-06-29 10:07:14 +02001150
1151 /*
1152 * Ensure either worker_sched_deactivated() sees the above
1153 * list_add_tail() or we see zero nr_running to avoid workers
1154 * lying around lazily while there are works to be processed.
1155 */
1156 smp_mb();
1157
Tejun Heo63d95a92012-07-12 14:46:37 -07001158 if (__need_more_worker(pool))
1159 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001160}
1161
Tejun Heoc8efcc22010-12-20 19:32:04 +01001162/*
1163 * Test whether @work is being queued from another work executing on the
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001164 * same workqueue.
Tejun Heoc8efcc22010-12-20 19:32:04 +01001165 */
1166static bool is_chained_work(struct workqueue_struct *wq)
1167{
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001168 struct worker *worker;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001169
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001170 worker = current_wq_worker();
1171 /*
1172 * Return %true iff I'm a worker execuing a work item on @wq. If
1173 * I'm @worker, it's safe to dereference it without locking.
1174 */
Tejun Heo112202d2013-02-13 19:29:12 -08001175 return worker && worker->current_pwq->wq == wq;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001176}
1177
Tejun Heod84ff052013-03-12 11:29:59 -07001178static void __queue_work(int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 struct work_struct *work)
1180{
Tejun Heo112202d2013-02-13 19:29:12 -08001181 struct pool_workqueue *pwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001182 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001183 unsigned int work_flags;
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001184 unsigned int req_cpu = cpu;
Tejun Heo8930cab2012-08-03 10:30:45 -07001185
1186 /*
1187 * While a work item is PENDING && off queue, a task trying to
1188 * steal the PENDING will busy-loop waiting for it to either get
1189 * queued or lose PENDING. Grabbing PENDING and queueing should
1190 * happen with IRQ disabled.
1191 */
1192 WARN_ON_ONCE(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001194 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001195
Tejun Heoc8efcc22010-12-20 19:32:04 +01001196 /* if dying, only works from the same workqueue are allowed */
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02001197 if (unlikely(wq->flags & WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001198 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001199 return;
1200
Tejun Heo112202d2013-02-13 19:29:12 -08001201 /* determine the pwq to use */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001202 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001203 struct worker_pool *last_pool;
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001204
Tejun Heo57469822012-08-03 10:30:45 -07001205 if (cpu == WORK_CPU_UNBOUND)
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001206 cpu = raw_smp_processor_id();
1207
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001208 /*
Tejun Heodbf25762012-08-20 14:51:23 -07001209 * It's multi cpu. If @work was previously on a different
1210 * cpu, it might still be running there, in which case the
1211 * work needs to be queued on that cpu to guarantee
1212 * non-reentrancy.
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001213 */
Tejun Heo7fb98ea2013-03-12 11:30:00 -07001214 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001215 last_pool = get_work_pool(work);
Tejun Heodbf25762012-08-20 14:51:23 -07001216
Tejun Heo112202d2013-02-13 19:29:12 -08001217 if (last_pool && last_pool != pwq->pool) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001218 struct worker *worker;
1219
Tejun Heod565ed62013-01-24 11:01:33 -08001220 spin_lock(&last_pool->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001221
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001222 worker = find_worker_executing_work(last_pool, work);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001223
Tejun Heo112202d2013-02-13 19:29:12 -08001224 if (worker && worker->current_pwq->wq == wq) {
Tejun Heo7fb98ea2013-03-12 11:30:00 -07001225 pwq = per_cpu_ptr(wq->cpu_pwqs, last_pool->cpu);
Lai Jiangshan8594fad2013-02-07 13:14:20 -08001226 } else {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001227 /* meh... not running there, queue here */
Tejun Heod565ed62013-01-24 11:01:33 -08001228 spin_unlock(&last_pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08001229 spin_lock(&pwq->pool->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001230 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001231 } else {
Tejun Heo112202d2013-02-13 19:29:12 -08001232 spin_lock(&pwq->pool->lock);
Tejun Heo8930cab2012-08-03 10:30:45 -07001233 }
Tejun Heof3421792010-07-02 10:03:51 +02001234 } else {
Tejun Heo7fb98ea2013-03-12 11:30:00 -07001235 pwq = first_pwq(wq);
Tejun Heo112202d2013-02-13 19:29:12 -08001236 spin_lock(&pwq->pool->lock);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001237 }
1238
Tejun Heo112202d2013-02-13 19:29:12 -08001239 /* pwq determined, queue */
1240 trace_workqueue_queue_work(req_cpu, pwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001241
Dan Carpenterf5b25522012-04-13 22:06:58 +03001242 if (WARN_ON(!list_empty(&work->entry))) {
Tejun Heo112202d2013-02-13 19:29:12 -08001243 spin_unlock(&pwq->pool->lock);
Dan Carpenterf5b25522012-04-13 22:06:58 +03001244 return;
1245 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001246
Tejun Heo112202d2013-02-13 19:29:12 -08001247 pwq->nr_in_flight[pwq->work_color]++;
1248 work_flags = work_color_to_flags(pwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001249
Tejun Heo112202d2013-02-13 19:29:12 -08001250 if (likely(pwq->nr_active < pwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001251 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001252 pwq->nr_active++;
1253 worklist = &pwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001254 } else {
1255 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo112202d2013-02-13 19:29:12 -08001256 worklist = &pwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001257 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001258
Tejun Heo112202d2013-02-13 19:29:12 -08001259 insert_work(pwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001260
Tejun Heo112202d2013-02-13 19:29:12 -08001261 spin_unlock(&pwq->pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262}
1263
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001264/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07001265 * queue_work_on - queue work on specific cpu
1266 * @cpu: CPU number to execute work on
1267 * @wq: workqueue to use
1268 * @work: work to queue
1269 *
Tejun Heod4283e92012-08-03 10:30:44 -07001270 * Returns %false if @work was already on a queue, %true otherwise.
Zhang Ruic1a220e2008-07-23 21:28:39 -07001271 *
1272 * We queue the work to a specific CPU, the caller must ensure it
1273 * can't go away.
1274 */
Tejun Heod4283e92012-08-03 10:30:44 -07001275bool queue_work_on(int cpu, struct workqueue_struct *wq,
1276 struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07001277{
Tejun Heod4283e92012-08-03 10:30:44 -07001278 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001279 unsigned long flags;
1280
1281 local_irq_save(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001282
Tejun Heo22df02b2010-06-29 10:07:10 +02001283 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001284 __queue_work(cpu, wq, work);
Tejun Heod4283e92012-08-03 10:30:44 -07001285 ret = true;
Zhang Ruic1a220e2008-07-23 21:28:39 -07001286 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001287
1288 local_irq_restore(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001289 return ret;
1290}
1291EXPORT_SYMBOL_GPL(queue_work_on);
1292
Tejun Heo0a13c002012-08-03 10:30:44 -07001293/**
1294 * queue_work - queue work on a workqueue
1295 * @wq: workqueue to use
1296 * @work: work to queue
1297 *
Tejun Heod4283e92012-08-03 10:30:44 -07001298 * Returns %false if @work was already on a queue, %true otherwise.
Tejun Heo0a13c002012-08-03 10:30:44 -07001299 *
1300 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1301 * it can be processed by another CPU.
1302 */
Tejun Heod4283e92012-08-03 10:30:44 -07001303bool queue_work(struct workqueue_struct *wq, struct work_struct *work)
Tejun Heo0a13c002012-08-03 10:30:44 -07001304{
Tejun Heo57469822012-08-03 10:30:45 -07001305 return queue_work_on(WORK_CPU_UNBOUND, wq, work);
Tejun Heo0a13c002012-08-03 10:30:44 -07001306}
1307EXPORT_SYMBOL_GPL(queue_work);
1308
Tejun Heod8e794d2012-08-03 10:30:45 -07001309void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
David Howells52bad642006-11-22 14:54:01 +00001311 struct delayed_work *dwork = (struct delayed_work *)__data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001313 /* should have been called from irqsafe timer with irq already off */
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001314 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315}
Konstantin Khlebnikov1438ade52013-01-24 16:36:31 +04001316EXPORT_SYMBOL(delayed_work_timer_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001318static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
1319 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001321 struct timer_list *timer = &dwork->timer;
1322 struct work_struct *work = &dwork->work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001324 WARN_ON_ONCE(timer->function != delayed_work_timer_fn ||
1325 timer->data != (unsigned long)dwork);
Tejun Heofc4b5142012-12-04 07:40:39 -08001326 WARN_ON_ONCE(timer_pending(timer));
1327 WARN_ON_ONCE(!list_empty(&work->entry));
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001328
Tejun Heo8852aac2012-12-01 16:23:42 -08001329 /*
1330 * If @delay is 0, queue @dwork->work immediately. This is for
1331 * both optimization and correctness. The earliest @timer can
1332 * expire is on the closest next tick and delayed_work users depend
1333 * on that there's no such delay when @delay is 0.
1334 */
1335 if (!delay) {
1336 __queue_work(cpu, wq, &dwork->work);
1337 return;
1338 }
1339
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001340 timer_stats_timer_set_start_info(&dwork->timer);
1341
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001342 dwork->wq = wq;
Tejun Heo12650572012-08-08 09:38:42 -07001343 dwork->cpu = cpu;
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001344 timer->expires = jiffies + delay;
1345
1346 if (unlikely(cpu != WORK_CPU_UNBOUND))
1347 add_timer_on(timer, cpu);
1348 else
1349 add_timer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350}
1351
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001352/**
1353 * queue_delayed_work_on - queue work on specific CPU after delay
1354 * @cpu: CPU number to execute work on
1355 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001356 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001357 * @delay: number of jiffies to wait before queueing
1358 *
Tejun Heo715f1302012-08-03 10:30:46 -07001359 * Returns %false if @work was already on a queue, %true otherwise. If
1360 * @delay is zero and @dwork is idle, it will be scheduled for immediate
1361 * execution.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001362 */
Tejun Heod4283e92012-08-03 10:30:44 -07001363bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
1364 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001365{
David Howells52bad642006-11-22 14:54:01 +00001366 struct work_struct *work = &dwork->work;
Tejun Heod4283e92012-08-03 10:30:44 -07001367 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001368 unsigned long flags;
1369
1370 /* read the comment in __queue_work() */
1371 local_irq_save(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001372
Tejun Heo22df02b2010-06-29 10:07:10 +02001373 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001374 __queue_delayed_work(cpu, wq, dwork, delay);
Tejun Heod4283e92012-08-03 10:30:44 -07001375 ret = true;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001376 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001377
1378 local_irq_restore(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001379 return ret;
1380}
Dave Jonesae90dd52006-06-30 01:40:45 -04001381EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Tejun Heoc8e55f32010-06-29 10:07:12 +02001383/**
Tejun Heo0a13c002012-08-03 10:30:44 -07001384 * queue_delayed_work - queue work on a workqueue after delay
1385 * @wq: workqueue to use
1386 * @dwork: delayable work to queue
1387 * @delay: number of jiffies to wait before queueing
1388 *
Tejun Heo715f1302012-08-03 10:30:46 -07001389 * Equivalent to queue_delayed_work_on() but tries to use the local CPU.
Tejun Heo0a13c002012-08-03 10:30:44 -07001390 */
Tejun Heod4283e92012-08-03 10:30:44 -07001391bool queue_delayed_work(struct workqueue_struct *wq,
Tejun Heo0a13c002012-08-03 10:30:44 -07001392 struct delayed_work *dwork, unsigned long delay)
1393{
Tejun Heo57469822012-08-03 10:30:45 -07001394 return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
Tejun Heo0a13c002012-08-03 10:30:44 -07001395}
1396EXPORT_SYMBOL_GPL(queue_delayed_work);
1397
1398/**
Tejun Heo8376fe22012-08-03 10:30:47 -07001399 * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
1400 * @cpu: CPU number to execute work on
1401 * @wq: workqueue to use
1402 * @dwork: work to queue
1403 * @delay: number of jiffies to wait before queueing
1404 *
1405 * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
1406 * modify @dwork's timer so that it expires after @delay. If @delay is
1407 * zero, @work is guaranteed to be scheduled immediately regardless of its
1408 * current state.
1409 *
1410 * Returns %false if @dwork was idle and queued, %true if @dwork was
1411 * pending and its timer was modified.
1412 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001413 * This function is safe to call from any context including IRQ handler.
Tejun Heo8376fe22012-08-03 10:30:47 -07001414 * See try_to_grab_pending() for details.
1415 */
1416bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
1417 struct delayed_work *dwork, unsigned long delay)
1418{
1419 unsigned long flags;
1420 int ret;
1421
1422 do {
1423 ret = try_to_grab_pending(&dwork->work, true, &flags);
1424 } while (unlikely(ret == -EAGAIN));
1425
1426 if (likely(ret >= 0)) {
1427 __queue_delayed_work(cpu, wq, dwork, delay);
1428 local_irq_restore(flags);
1429 }
1430
1431 /* -ENOENT from try_to_grab_pending() becomes %true */
1432 return ret;
1433}
1434EXPORT_SYMBOL_GPL(mod_delayed_work_on);
1435
1436/**
1437 * mod_delayed_work - modify delay of or queue a delayed work
1438 * @wq: workqueue to use
1439 * @dwork: work to queue
1440 * @delay: number of jiffies to wait before queueing
1441 *
1442 * mod_delayed_work_on() on local CPU.
1443 */
1444bool mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dwork,
1445 unsigned long delay)
1446{
1447 return mod_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
1448}
1449EXPORT_SYMBOL_GPL(mod_delayed_work);
1450
1451/**
Tejun Heoc8e55f32010-06-29 10:07:12 +02001452 * worker_enter_idle - enter idle state
1453 * @worker: worker which is entering idle state
1454 *
1455 * @worker is entering idle state. Update stats and idle timer if
1456 * necessary.
1457 *
1458 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001459 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001460 */
1461static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001463 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Tejun Heo6183c002013-03-12 11:29:57 -07001465 if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) ||
1466 WARN_ON_ONCE(!list_empty(&worker->entry) &&
1467 (worker->hentry.next || worker->hentry.pprev)))
1468 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Tejun Heocb444762010-07-02 10:03:50 +02001470 /* can't use worker_set_flags(), also called from start_worker() */
1471 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001472 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001473 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001474
Tejun Heoc8e55f32010-06-29 10:07:12 +02001475 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001476 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001477
Tejun Heo628c78e2012-07-17 12:39:27 -07001478 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1479 mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
Tejun Heocb444762010-07-02 10:03:50 +02001480
Tejun Heo544ecf32012-05-14 15:04:50 -07001481 /*
Tejun Heo706026c2013-01-24 11:01:34 -08001482 * Sanity check nr_running. Because wq_unbind_fn() releases
Tejun Heod565ed62013-01-24 11:01:33 -08001483 * pool->lock between setting %WORKER_UNBOUND and zapping
Tejun Heo628c78e2012-07-17 12:39:27 -07001484 * nr_running, the warning may trigger spuriously. Check iff
1485 * unbind is not in progress.
Tejun Heo544ecf32012-05-14 15:04:50 -07001486 */
Tejun Heo24647572013-01-24 11:01:33 -08001487 WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001488 pool->nr_workers == pool->nr_idle &&
Tejun Heoe19e3972013-01-24 11:39:44 -08001489 atomic_read(&pool->nr_running));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490}
1491
Tejun Heoc8e55f32010-06-29 10:07:12 +02001492/**
1493 * worker_leave_idle - leave idle state
1494 * @worker: worker which is leaving idle state
1495 *
1496 * @worker is leaving idle state. Update stats.
1497 *
1498 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001499 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001500 */
1501static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001503 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Tejun Heo6183c002013-03-12 11:29:57 -07001505 if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE)))
1506 return;
Tejun Heod302f012010-06-29 10:07:13 +02001507 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001508 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001509 list_del_init(&worker->entry);
1510}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Tejun Heoe22bee72010-06-29 10:07:14 +02001512/**
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001513 * worker_maybe_bind_and_lock - try to bind %current to worker_pool and lock it
1514 * @pool: target worker_pool
1515 *
1516 * Bind %current to the cpu of @pool if it is associated and lock @pool.
Tejun Heoe22bee72010-06-29 10:07:14 +02001517 *
1518 * Works which are scheduled while the cpu is online must at least be
1519 * scheduled to a worker which is bound to the cpu so that if they are
1520 * flushed from cpu callbacks while cpu is going down, they are
1521 * guaranteed to execute on the cpu.
1522 *
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001523 * This function is to be used by unbound workers and rescuers to bind
Tejun Heoe22bee72010-06-29 10:07:14 +02001524 * themselves to the target cpu and may race with cpu going down or
1525 * coming online. kthread_bind() can't be used because it may put the
1526 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
Tejun Heo706026c2013-01-24 11:01:34 -08001527 * verbatim as it's best effort and blocking and pool may be
Tejun Heoe22bee72010-06-29 10:07:14 +02001528 * [dis]associated in the meantime.
1529 *
Tejun Heo706026c2013-01-24 11:01:34 -08001530 * This function tries set_cpus_allowed() and locks pool and verifies the
Tejun Heo24647572013-01-24 11:01:33 -08001531 * binding against %POOL_DISASSOCIATED which is set during
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001532 * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1533 * enters idle state or fetches works without dropping lock, it can
1534 * guarantee the scheduling requirement described in the first paragraph.
Tejun Heoe22bee72010-06-29 10:07:14 +02001535 *
1536 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001537 * Might sleep. Called without any lock but returns with pool->lock
Tejun Heoe22bee72010-06-29 10:07:14 +02001538 * held.
1539 *
1540 * RETURNS:
Tejun Heo706026c2013-01-24 11:01:34 -08001541 * %true if the associated pool is online (@worker is successfully
Tejun Heoe22bee72010-06-29 10:07:14 +02001542 * bound), %false if offline.
1543 */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001544static bool worker_maybe_bind_and_lock(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001545__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001546{
Tejun Heoe22bee72010-06-29 10:07:14 +02001547 while (true) {
1548 /*
1549 * The following call may fail, succeed or succeed
1550 * without actually migrating the task to the cpu if
1551 * it races with cpu hotunplug operation. Verify
Tejun Heo24647572013-01-24 11:01:33 -08001552 * against POOL_DISASSOCIATED.
Tejun Heoe22bee72010-06-29 10:07:14 +02001553 */
Tejun Heo24647572013-01-24 11:01:33 -08001554 if (!(pool->flags & POOL_DISASSOCIATED))
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001555 set_cpus_allowed_ptr(current, get_cpu_mask(pool->cpu));
Oleg Nesterov85f41862007-05-09 02:34:20 -07001556
Tejun Heod565ed62013-01-24 11:01:33 -08001557 spin_lock_irq(&pool->lock);
Tejun Heo24647572013-01-24 11:01:33 -08001558 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heoe22bee72010-06-29 10:07:14 +02001559 return false;
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001560 if (task_cpu(current) == pool->cpu &&
Tejun Heoe22bee72010-06-29 10:07:14 +02001561 cpumask_equal(&current->cpus_allowed,
Tejun Heoec22ca52013-01-24 11:01:33 -08001562 get_cpu_mask(pool->cpu)))
Tejun Heoe22bee72010-06-29 10:07:14 +02001563 return true;
Tejun Heod565ed62013-01-24 11:01:33 -08001564 spin_unlock_irq(&pool->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001565
Tejun Heo5035b202011-04-29 18:08:37 +02001566 /*
1567 * We've raced with CPU hot[un]plug. Give it a breather
1568 * and retry migration. cond_resched() is required here;
1569 * otherwise, we might deadlock against cpu_stop trying to
1570 * bring down the CPU on non-preemptive kernel.
1571 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001572 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001573 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001574 }
1575}
1576
1577/*
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001578 * Rebind an idle @worker to its CPU. worker_thread() will test
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001579 * list_empty(@worker->entry) before leaving idle and call this function.
Tejun Heo25511a42012-07-17 12:39:27 -07001580 */
1581static void idle_worker_rebind(struct worker *worker)
1582{
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001583 /* CPU may go down again inbetween, clear UNBOUND only on success */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001584 if (worker_maybe_bind_and_lock(worker->pool))
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001585 worker_clr_flags(worker, WORKER_UNBOUND);
Tejun Heo25511a42012-07-17 12:39:27 -07001586
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001587 /* rebind complete, become available again */
1588 list_add(&worker->entry, &worker->pool->idle_list);
Tejun Heod565ed62013-01-24 11:01:33 -08001589 spin_unlock_irq(&worker->pool->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07001590}
1591
1592/*
1593 * Function for @worker->rebind.work used to rebind unbound busy workers to
Tejun Heo403c8212012-07-17 12:39:27 -07001594 * the associated cpu which is coming back online. This is scheduled by
1595 * cpu up but can race with other cpu hotplug operations and may be
1596 * executed twice without intervening cpu down.
Tejun Heoe22bee72010-06-29 10:07:14 +02001597 */
Tejun Heo25511a42012-07-17 12:39:27 -07001598static void busy_worker_rebind_fn(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001599{
1600 struct worker *worker = container_of(work, struct worker, rebind_work);
Tejun Heoe22bee72010-06-29 10:07:14 +02001601
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001602 if (worker_maybe_bind_and_lock(worker->pool))
Lai Jiangshaneab6d822012-09-18 09:59:22 -07001603 worker_clr_flags(worker, WORKER_UNBOUND);
Tejun Heoe22bee72010-06-29 10:07:14 +02001604
Tejun Heod565ed62013-01-24 11:01:33 -08001605 spin_unlock_irq(&worker->pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001606}
1607
Tejun Heo25511a42012-07-17 12:39:27 -07001608/**
Tejun Heo94cf58b2013-01-24 11:01:33 -08001609 * rebind_workers - rebind all workers of a pool to the associated CPU
1610 * @pool: pool of interest
Tejun Heo25511a42012-07-17 12:39:27 -07001611 *
Tejun Heo94cf58b2013-01-24 11:01:33 -08001612 * @pool->cpu is coming online. Rebind all workers to the CPU. Rebinding
Tejun Heo25511a42012-07-17 12:39:27 -07001613 * is different for idle and busy ones.
1614 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001615 * Idle ones will be removed from the idle_list and woken up. They will
1616 * add themselves back after completing rebind. This ensures that the
1617 * idle_list doesn't contain any unbound workers when re-bound busy workers
1618 * try to perform local wake-ups for concurrency management.
Tejun Heo25511a42012-07-17 12:39:27 -07001619 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001620 * Busy workers can rebind after they finish their current work items.
1621 * Queueing the rebind work item at the head of the scheduled list is
1622 * enough. Note that nr_running will be properly bumped as busy workers
1623 * rebind.
Tejun Heo25511a42012-07-17 12:39:27 -07001624 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001625 * On return, all non-manager workers are scheduled for rebind - see
1626 * manage_workers() for the manager special case. Any idle worker
1627 * including the manager will not appear on @idle_list until rebind is
1628 * complete, making local wake-ups safe.
Tejun Heo25511a42012-07-17 12:39:27 -07001629 */
Tejun Heo94cf58b2013-01-24 11:01:33 -08001630static void rebind_workers(struct worker_pool *pool)
Tejun Heo25511a42012-07-17 12:39:27 -07001631{
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001632 struct worker *worker, *n;
Tejun Heo25511a42012-07-17 12:39:27 -07001633 int i;
1634
Tejun Heo94cf58b2013-01-24 11:01:33 -08001635 lockdep_assert_held(&pool->assoc_mutex);
1636 lockdep_assert_held(&pool->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07001637
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001638 /* dequeue and kick idle ones */
Tejun Heo94cf58b2013-01-24 11:01:33 -08001639 list_for_each_entry_safe(worker, n, &pool->idle_list, entry) {
1640 /*
1641 * idle workers should be off @pool->idle_list until rebind
1642 * is complete to avoid receiving premature local wake-ups.
1643 */
1644 list_del_init(&worker->entry);
Lai Jiangshan96e65302012-09-02 00:28:19 +08001645
Tejun Heo94cf58b2013-01-24 11:01:33 -08001646 /*
1647 * worker_thread() will see the above dequeuing and call
1648 * idle_worker_rebind().
1649 */
1650 wake_up_process(worker->task);
1651 }
Tejun Heo25511a42012-07-17 12:39:27 -07001652
Tejun Heo94cf58b2013-01-24 11:01:33 -08001653 /* rebind busy workers */
Sasha Levinb67bfe02013-02-27 17:06:00 -08001654 for_each_busy_worker(worker, i, pool) {
Tejun Heo94cf58b2013-01-24 11:01:33 -08001655 struct work_struct *rebind_work = &worker->rebind_work;
1656 struct workqueue_struct *wq;
Tejun Heo25511a42012-07-17 12:39:27 -07001657
Tejun Heo94cf58b2013-01-24 11:01:33 -08001658 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
1659 work_data_bits(rebind_work)))
1660 continue;
Tejun Heo25511a42012-07-17 12:39:27 -07001661
Tejun Heo94cf58b2013-01-24 11:01:33 -08001662 debug_work_activate(rebind_work);
Tejun Heo90beca52012-09-04 23:12:33 -07001663
Tejun Heo94cf58b2013-01-24 11:01:33 -08001664 /*
1665 * wq doesn't really matter but let's keep @worker->pool
Tejun Heo112202d2013-02-13 19:29:12 -08001666 * and @pwq->pool consistent for sanity.
Tejun Heo94cf58b2013-01-24 11:01:33 -08001667 */
1668 if (std_worker_pool_pri(worker->pool))
1669 wq = system_highpri_wq;
1670 else
1671 wq = system_wq;
Tejun Heoec588152012-09-04 23:16:32 -07001672
Tejun Heo7fb98ea2013-03-12 11:30:00 -07001673 insert_work(per_cpu_ptr(wq->cpu_pwqs, pool->cpu), rebind_work,
Tejun Heo94cf58b2013-01-24 11:01:33 -08001674 worker->scheduled.next,
1675 work_color_to_flags(WORK_NO_COLOR));
Tejun Heoec588152012-09-04 23:16:32 -07001676 }
Tejun Heo25511a42012-07-17 12:39:27 -07001677}
1678
Tejun Heoc34056a2010-06-29 10:07:11 +02001679static struct worker *alloc_worker(void)
1680{
1681 struct worker *worker;
1682
1683 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001684 if (worker) {
1685 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001686 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heo25511a42012-07-17 12:39:27 -07001687 INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn);
Tejun Heoe22bee72010-06-29 10:07:14 +02001688 /* on creation a worker is in !idle && prep state */
1689 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001690 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001691 return worker;
1692}
1693
1694/**
1695 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001696 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001697 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001698 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001699 * can be started by calling start_worker() or destroyed using
1700 * destroy_worker().
1701 *
1702 * CONTEXT:
1703 * Might sleep. Does GFP_KERNEL allocations.
1704 *
1705 * RETURNS:
1706 * Pointer to the newly created worker.
1707 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001708static struct worker *create_worker(struct worker_pool *pool)
Tejun Heoc34056a2010-06-29 10:07:11 +02001709{
Tejun Heoe34cdddb2013-01-24 11:01:33 -08001710 const char *pri = std_worker_pool_pri(pool) ? "H" : "";
Tejun Heoc34056a2010-06-29 10:07:11 +02001711 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001712 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001713
Tejun Heod565ed62013-01-24 11:01:33 -08001714 spin_lock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001715 while (ida_get_new(&pool->worker_ida, &id)) {
Tejun Heod565ed62013-01-24 11:01:33 -08001716 spin_unlock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001717 if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001718 goto fail;
Tejun Heod565ed62013-01-24 11:01:33 -08001719 spin_lock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001720 }
Tejun Heod565ed62013-01-24 11:01:33 -08001721 spin_unlock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001722
1723 worker = alloc_worker();
1724 if (!worker)
1725 goto fail;
1726
Tejun Heobd7bdd42012-07-12 14:46:37 -07001727 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001728 worker->id = id;
1729
Tejun Heoec22ca52013-01-24 11:01:33 -08001730 if (pool->cpu != WORK_CPU_UNBOUND)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001731 worker->task = kthread_create_on_node(worker_thread,
Tejun Heoec22ca52013-01-24 11:01:33 -08001732 worker, cpu_to_node(pool->cpu),
Tejun Heod84ff052013-03-12 11:29:59 -07001733 "kworker/%d:%d%s", pool->cpu, id, pri);
Tejun Heof3421792010-07-02 10:03:51 +02001734 else
1735 worker->task = kthread_create(worker_thread, worker,
Tejun Heo32704762012-07-13 22:16:45 -07001736 "kworker/u:%d%s", id, pri);
Tejun Heoc34056a2010-06-29 10:07:11 +02001737 if (IS_ERR(worker->task))
1738 goto fail;
1739
Tejun Heoe34cdddb2013-01-24 11:01:33 -08001740 if (std_worker_pool_pri(pool))
Tejun Heo32704762012-07-13 22:16:45 -07001741 set_user_nice(worker->task, HIGHPRI_NICE_LEVEL);
1742
Tejun Heodb7bccf2010-06-29 10:07:12 +02001743 /*
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001744 * Determine CPU binding of the new worker depending on
Tejun Heo24647572013-01-24 11:01:33 -08001745 * %POOL_DISASSOCIATED. The caller is responsible for ensuring the
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001746 * flag remains stable across this function. See the comments
1747 * above the flag definition for details.
1748 *
1749 * As an unbound worker may later become a regular one if CPU comes
1750 * online, make sure every worker has %PF_THREAD_BOUND set.
Tejun Heodb7bccf2010-06-29 10:07:12 +02001751 */
Tejun Heo24647572013-01-24 11:01:33 -08001752 if (!(pool->flags & POOL_DISASSOCIATED)) {
Tejun Heoec22ca52013-01-24 11:01:33 -08001753 kthread_bind(worker->task, pool->cpu);
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001754 } else {
Tejun Heodb7bccf2010-06-29 10:07:12 +02001755 worker->task->flags |= PF_THREAD_BOUND;
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001756 worker->flags |= WORKER_UNBOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001758
Tejun Heoc34056a2010-06-29 10:07:11 +02001759 return worker;
1760fail:
1761 if (id >= 0) {
Tejun Heod565ed62013-01-24 11:01:33 -08001762 spin_lock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001763 ida_remove(&pool->worker_ida, id);
Tejun Heod565ed62013-01-24 11:01:33 -08001764 spin_unlock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001765 }
1766 kfree(worker);
1767 return NULL;
1768}
1769
1770/**
1771 * start_worker - start a newly created worker
1772 * @worker: worker to start
1773 *
Tejun Heo706026c2013-01-24 11:01:34 -08001774 * Make the pool aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001775 *
1776 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001777 * spin_lock_irq(pool->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001778 */
1779static void start_worker(struct worker *worker)
1780{
Tejun Heocb444762010-07-02 10:03:50 +02001781 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001782 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001783 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001784 wake_up_process(worker->task);
1785}
1786
1787/**
1788 * destroy_worker - destroy a workqueue worker
1789 * @worker: worker to be destroyed
1790 *
Tejun Heo706026c2013-01-24 11:01:34 -08001791 * Destroy @worker and adjust @pool stats accordingly.
Tejun Heoc8e55f32010-06-29 10:07:12 +02001792 *
1793 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001794 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001795 */
1796static void destroy_worker(struct worker *worker)
1797{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001798 struct worker_pool *pool = worker->pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001799 int id = worker->id;
1800
1801 /* sanity check frenzy */
Tejun Heo6183c002013-03-12 11:29:57 -07001802 if (WARN_ON(worker->current_work) ||
1803 WARN_ON(!list_empty(&worker->scheduled)))
1804 return;
Tejun Heoc34056a2010-06-29 10:07:11 +02001805
Tejun Heoc8e55f32010-06-29 10:07:12 +02001806 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001807 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001808 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001809 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001810
1811 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001812 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001813
Tejun Heod565ed62013-01-24 11:01:33 -08001814 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001815
Tejun Heoc34056a2010-06-29 10:07:11 +02001816 kthread_stop(worker->task);
1817 kfree(worker);
1818
Tejun Heod565ed62013-01-24 11:01:33 -08001819 spin_lock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001820 ida_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001821}
1822
Tejun Heo63d95a92012-07-12 14:46:37 -07001823static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001824{
Tejun Heo63d95a92012-07-12 14:46:37 -07001825 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001826
Tejun Heod565ed62013-01-24 11:01:33 -08001827 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001828
Tejun Heo63d95a92012-07-12 14:46:37 -07001829 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001830 struct worker *worker;
1831 unsigned long expires;
1832
1833 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001834 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001835 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1836
1837 if (time_before(jiffies, expires))
Tejun Heo63d95a92012-07-12 14:46:37 -07001838 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001839 else {
1840 /* it's been idle for too long, wake up manager */
Tejun Heo11ebea52012-07-12 14:46:37 -07001841 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -07001842 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001843 }
1844 }
1845
Tejun Heod565ed62013-01-24 11:01:33 -08001846 spin_unlock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001847}
1848
Tejun Heo493a1722013-03-12 11:29:59 -07001849static void send_mayday(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001850{
Tejun Heo112202d2013-02-13 19:29:12 -08001851 struct pool_workqueue *pwq = get_work_pwq(work);
1852 struct workqueue_struct *wq = pwq->wq;
Tejun Heo493a1722013-03-12 11:29:59 -07001853
1854 lockdep_assert_held(&workqueue_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001855
1856 if (!(wq->flags & WQ_RESCUER))
Tejun Heo493a1722013-03-12 11:29:59 -07001857 return;
Tejun Heoe22bee72010-06-29 10:07:14 +02001858
1859 /* mayday mayday mayday */
Tejun Heo493a1722013-03-12 11:29:59 -07001860 if (list_empty(&pwq->mayday_node)) {
1861 list_add_tail(&pwq->mayday_node, &wq->maydays);
Tejun Heoe22bee72010-06-29 10:07:14 +02001862 wake_up_process(wq->rescuer->task);
Tejun Heo493a1722013-03-12 11:29:59 -07001863 }
Tejun Heoe22bee72010-06-29 10:07:14 +02001864}
1865
Tejun Heo706026c2013-01-24 11:01:34 -08001866static void pool_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001867{
Tejun Heo63d95a92012-07-12 14:46:37 -07001868 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001869 struct work_struct *work;
1870
Tejun Heo493a1722013-03-12 11:29:59 -07001871 spin_lock_irq(&workqueue_lock); /* for wq->maydays */
1872 spin_lock(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001873
Tejun Heo63d95a92012-07-12 14:46:37 -07001874 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001875 /*
1876 * We've been trying to create a new worker but
1877 * haven't been successful. We might be hitting an
1878 * allocation deadlock. Send distress signals to
1879 * rescuers.
1880 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001881 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001882 send_mayday(work);
1883 }
1884
Tejun Heo493a1722013-03-12 11:29:59 -07001885 spin_unlock(&pool->lock);
1886 spin_unlock_irq(&workqueue_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001887
Tejun Heo63d95a92012-07-12 14:46:37 -07001888 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001889}
1890
1891/**
1892 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001893 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001894 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001895 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001896 * have at least one idle worker on return from this function. If
1897 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001898 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001899 * possible allocation deadlock.
1900 *
1901 * On return, need_to_create_worker() is guaranteed to be false and
1902 * may_start_working() true.
1903 *
1904 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001905 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001906 * multiple times. Does GFP_KERNEL allocations. Called only from
1907 * manager.
1908 *
1909 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08001910 * false if no action was taken and pool->lock stayed locked, true
Tejun Heoe22bee72010-06-29 10:07:14 +02001911 * otherwise.
1912 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001913static bool maybe_create_worker(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001914__releases(&pool->lock)
1915__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001916{
Tejun Heo63d95a92012-07-12 14:46:37 -07001917 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001918 return false;
1919restart:
Tejun Heod565ed62013-01-24 11:01:33 -08001920 spin_unlock_irq(&pool->lock);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001921
Tejun Heoe22bee72010-06-29 10:07:14 +02001922 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07001923 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001924
1925 while (true) {
1926 struct worker *worker;
1927
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001928 worker = create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001929 if (worker) {
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 Heoe22bee72010-06-29 10:07:14 +02001932 start_worker(worker);
Tejun Heo6183c002013-03-12 11:29:57 -07001933 if (WARN_ON_ONCE(need_to_create_worker(pool)))
1934 goto restart;
Tejun Heoe22bee72010-06-29 10:07:14 +02001935 return true;
1936 }
1937
Tejun Heo63d95a92012-07-12 14:46:37 -07001938 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001939 break;
1940
Tejun Heoe22bee72010-06-29 10:07:14 +02001941 __set_current_state(TASK_INTERRUPTIBLE);
1942 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001943
Tejun Heo63d95a92012-07-12 14:46:37 -07001944 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001945 break;
1946 }
1947
Tejun Heo63d95a92012-07-12 14:46:37 -07001948 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001949 spin_lock_irq(&pool->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07001950 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001951 goto restart;
1952 return true;
1953}
1954
1955/**
1956 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo63d95a92012-07-12 14:46:37 -07001957 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001958 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001959 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02001960 * IDLE_WORKER_TIMEOUT.
1961 *
1962 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001963 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001964 * multiple times. Called only from manager.
1965 *
1966 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08001967 * false if no action was taken and pool->lock stayed locked, true
Tejun Heoe22bee72010-06-29 10:07:14 +02001968 * otherwise.
1969 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001970static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001971{
1972 bool ret = false;
1973
Tejun Heo63d95a92012-07-12 14:46:37 -07001974 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001975 struct worker *worker;
1976 unsigned long expires;
1977
Tejun Heo63d95a92012-07-12 14:46:37 -07001978 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001979 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1980
1981 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001982 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001983 break;
1984 }
1985
1986 destroy_worker(worker);
1987 ret = true;
1988 }
1989
1990 return ret;
1991}
1992
1993/**
1994 * manage_workers - manage worker pool
1995 * @worker: self
1996 *
Tejun Heo706026c2013-01-24 11:01:34 -08001997 * Assume the manager role and manage the worker pool @worker belongs
Tejun Heoe22bee72010-06-29 10:07:14 +02001998 * to. At any given time, there can be only zero or one manager per
Tejun Heo706026c2013-01-24 11:01:34 -08001999 * pool. The exclusion is handled automatically by this function.
Tejun Heoe22bee72010-06-29 10:07:14 +02002000 *
2001 * The caller can safely start processing works on false return. On
2002 * true return, it's guaranteed that need_to_create_worker() is false
2003 * and may_start_working() is true.
2004 *
2005 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002006 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02002007 * multiple times. Does GFP_KERNEL allocations.
2008 *
2009 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08002010 * spin_lock_irq(pool->lock) which may be released and regrabbed
2011 * multiple times. Does GFP_KERNEL allocations.
Tejun Heoe22bee72010-06-29 10:07:14 +02002012 */
2013static bool manage_workers(struct worker *worker)
2014{
Tejun Heo63d95a92012-07-12 14:46:37 -07002015 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002016 bool ret = false;
2017
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002018 if (pool->flags & POOL_MANAGING_WORKERS)
Tejun Heoe22bee72010-06-29 10:07:14 +02002019 return ret;
2020
Lai Jiangshan552a37e2012-09-10 10:03:33 -07002021 pool->flags |= POOL_MANAGING_WORKERS;
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002022
2023 /*
2024 * To simplify both worker management and CPU hotplug, hold off
2025 * management while hotplug is in progress. CPU hotplug path can't
2026 * grab %POOL_MANAGING_WORKERS to achieve this because that can
2027 * lead to idle worker depletion (all become busy thinking someone
2028 * else is managing) which in turn can result in deadlock under
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002029 * extreme circumstances. Use @pool->assoc_mutex to synchronize
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002030 * manager against CPU hotplug.
2031 *
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002032 * assoc_mutex would always be free unless CPU hotplug is in
Tejun Heod565ed62013-01-24 11:01:33 -08002033 * progress. trylock first without dropping @pool->lock.
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002034 */
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002035 if (unlikely(!mutex_trylock(&pool->assoc_mutex))) {
Tejun Heod565ed62013-01-24 11:01:33 -08002036 spin_unlock_irq(&pool->lock);
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002037 mutex_lock(&pool->assoc_mutex);
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002038 /*
2039 * CPU hotplug could have happened while we were waiting
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002040 * for assoc_mutex. Hotplug itself can't handle us
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002041 * because manager isn't either on idle or busy list, and
Tejun Heo706026c2013-01-24 11:01:34 -08002042 * @pool's state and ours could have deviated.
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002043 *
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002044 * As hotplug is now excluded via assoc_mutex, we can
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002045 * simply try to bind. It will succeed or fail depending
Tejun Heo706026c2013-01-24 11:01:34 -08002046 * on @pool's current state. Try it and adjust
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002047 * %WORKER_UNBOUND accordingly.
2048 */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08002049 if (worker_maybe_bind_and_lock(pool))
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002050 worker->flags &= ~WORKER_UNBOUND;
2051 else
2052 worker->flags |= WORKER_UNBOUND;
2053
2054 ret = true;
2055 }
2056
Tejun Heo11ebea52012-07-12 14:46:37 -07002057 pool->flags &= ~POOL_MANAGE_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02002058
2059 /*
2060 * Destroy and then create so that may_start_working() is true
2061 * on return.
2062 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002063 ret |= maybe_destroy_workers(pool);
2064 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002065
Lai Jiangshan552a37e2012-09-10 10:03:33 -07002066 pool->flags &= ~POOL_MANAGING_WORKERS;
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002067 mutex_unlock(&pool->assoc_mutex);
Tejun Heoe22bee72010-06-29 10:07:14 +02002068 return ret;
2069}
2070
Tejun Heoa62428c2010-06-29 10:07:10 +02002071/**
2072 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02002073 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02002074 * @work: work to process
2075 *
2076 * Process @work. This function contains all the logics necessary to
2077 * process a single work including synchronization against and
2078 * interaction with other workers on the same cpu, queueing and
2079 * flushing. As long as context requirement is met, any worker can
2080 * call this function to process a work.
2081 *
2082 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002083 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02002084 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002085static void process_one_work(struct worker *worker, struct work_struct *work)
Tejun Heod565ed62013-01-24 11:01:33 -08002086__releases(&pool->lock)
2087__acquires(&pool->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02002088{
Tejun Heo112202d2013-02-13 19:29:12 -08002089 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002090 struct worker_pool *pool = worker->pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002091 bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02002092 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02002093 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02002094#ifdef CONFIG_LOCKDEP
2095 /*
2096 * It is permissible to free the struct work_struct from
2097 * inside the function that is called from it, this we need to
2098 * take into account for lockdep too. To avoid bogus "held
2099 * lock freed" warnings as well as problems when looking into
2100 * work->lockdep_map, make a copy and use that here.
2101 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07002102 struct lockdep_map lockdep_map;
2103
2104 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002105#endif
Tejun Heo6fec10a2012-07-22 10:16:34 -07002106 /*
2107 * Ensure we're on the correct CPU. DISASSOCIATED test is
2108 * necessary to avoid spurious warnings from rescuers servicing the
Tejun Heo24647572013-01-24 11:01:33 -08002109 * unbound or a disassociated pool.
Tejun Heo6fec10a2012-07-22 10:16:34 -07002110 */
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002111 WARN_ON_ONCE(!(worker->flags & WORKER_UNBOUND) &&
Tejun Heo24647572013-01-24 11:01:33 -08002112 !(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heoec22ca52013-01-24 11:01:33 -08002113 raw_smp_processor_id() != pool->cpu);
Tejun Heo25511a42012-07-17 12:39:27 -07002114
Tejun Heo7e116292010-06-29 10:07:13 +02002115 /*
2116 * A single work shouldn't be executed concurrently by
2117 * multiple workers on a single cpu. Check whether anyone is
2118 * already processing the work. If so, defer the work to the
2119 * currently executing one.
2120 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002121 collision = find_worker_executing_work(pool, work);
Tejun Heo7e116292010-06-29 10:07:13 +02002122 if (unlikely(collision)) {
2123 move_linked_works(work, &collision->scheduled, NULL);
2124 return;
2125 }
2126
Tejun Heo8930cab2012-08-03 10:30:45 -07002127 /* claim and dequeue */
Tejun Heoa62428c2010-06-29 10:07:10 +02002128 debug_work_deactivate(work);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002129 hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work);
Tejun Heoc34056a2010-06-29 10:07:11 +02002130 worker->current_work = work;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002131 worker->current_func = work->func;
Tejun Heo112202d2013-02-13 19:29:12 -08002132 worker->current_pwq = pwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002133 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002134
Tejun Heoa62428c2010-06-29 10:07:10 +02002135 list_del_init(&work->entry);
2136
Tejun Heo649027d2010-06-29 10:07:14 +02002137 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02002138 * CPU intensive works don't participate in concurrency
2139 * management. They're the scheduler's responsibility.
2140 */
2141 if (unlikely(cpu_intensive))
2142 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
2143
Tejun Heo974271c2012-07-12 14:46:37 -07002144 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002145 * Unbound pool isn't concurrency managed and work items should be
Tejun Heo974271c2012-07-12 14:46:37 -07002146 * executed ASAP. Wake up another worker if necessary.
2147 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002148 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
2149 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07002150
Tejun Heo8930cab2012-08-03 10:30:45 -07002151 /*
Tejun Heo7c3eed52013-01-24 11:01:33 -08002152 * Record the last pool and clear PENDING which should be the last
Tejun Heod565ed62013-01-24 11:01:33 -08002153 * update to @work. Also, do this inside @pool->lock so that
Tejun Heo23657bb2012-08-13 17:08:19 -07002154 * PENDING and queued state changes happen together while IRQ is
2155 * disabled.
Tejun Heo8930cab2012-08-03 10:30:45 -07002156 */
Tejun Heo7c3eed52013-01-24 11:01:33 -08002157 set_work_pool_and_clear_pending(work, pool->id);
Tejun Heoa62428c2010-06-29 10:07:10 +02002158
Tejun Heod565ed62013-01-24 11:01:33 -08002159 spin_unlock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002160
Tejun Heo112202d2013-02-13 19:29:12 -08002161 lock_map_acquire_read(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002162 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002163 trace_workqueue_execute_start(work);
Tejun Heoa2c1c572012-12-18 10:35:02 -08002164 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002165 /*
2166 * While we must be careful to not use "work" after this, the trace
2167 * point will only record its address.
2168 */
2169 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002170 lock_map_release(&lockdep_map);
Tejun Heo112202d2013-02-13 19:29:12 -08002171 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002172
2173 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Valentin Ilie044c7822012-08-19 00:52:42 +03002174 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
2175 " last function: %pf\n",
Tejun Heoa2c1c572012-12-18 10:35:02 -08002176 current->comm, preempt_count(), task_pid_nr(current),
2177 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02002178 debug_show_held_locks(current);
2179 dump_stack();
2180 }
2181
Tejun Heod565ed62013-01-24 11:01:33 -08002182 spin_lock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002183
Tejun Heofb0e7be2010-06-29 10:07:15 +02002184 /* clear cpu intensive status */
2185 if (unlikely(cpu_intensive))
2186 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2187
Tejun Heoa62428c2010-06-29 10:07:10 +02002188 /* we're done with it, release */
Sasha Levin42f85702012-12-17 10:01:23 -05002189 hash_del(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002190 worker->current_work = NULL;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002191 worker->current_func = NULL;
Tejun Heo112202d2013-02-13 19:29:12 -08002192 worker->current_pwq = NULL;
2193 pwq_dec_nr_in_flight(pwq, work_color);
Tejun Heoa62428c2010-06-29 10:07:10 +02002194}
2195
Tejun Heoaffee4b2010-06-29 10:07:12 +02002196/**
2197 * process_scheduled_works - process scheduled works
2198 * @worker: self
2199 *
2200 * Process all scheduled works. Please note that the scheduled list
2201 * may change while processing a work, so this function repeatedly
2202 * fetches a work from the top and executes it.
2203 *
2204 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002205 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02002206 * multiple times.
2207 */
2208static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002210 while (!list_empty(&worker->scheduled)) {
2211 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002213 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215}
2216
Tejun Heo4690c4a2010-06-29 10:07:10 +02002217/**
2218 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02002219 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02002220 *
Tejun Heo706026c2013-01-24 11:01:34 -08002221 * The worker thread function. There are NR_CPU_WORKER_POOLS dynamic pools
2222 * of these per each cpu. These workers process all works regardless of
Tejun Heoe22bee72010-06-29 10:07:14 +02002223 * their specific target workqueue. The only exception is works which
2224 * belong to workqueues with a rescuer which will be explained in
2225 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02002226 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002227static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228{
Tejun Heoc34056a2010-06-29 10:07:11 +02002229 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07002230 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
Tejun Heoe22bee72010-06-29 10:07:14 +02002232 /* tell the scheduler that this is a workqueue worker */
2233 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002234woke_up:
Tejun Heod565ed62013-01-24 11:01:33 -08002235 spin_lock_irq(&pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002237 /* we are off idle list if destruction or rebind is requested */
2238 if (unlikely(list_empty(&worker->entry))) {
Tejun Heod565ed62013-01-24 11:01:33 -08002239 spin_unlock_irq(&pool->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07002240
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002241 /* if DIE is set, destruction is requested */
Tejun Heo25511a42012-07-17 12:39:27 -07002242 if (worker->flags & WORKER_DIE) {
2243 worker->task->flags &= ~PF_WQ_WORKER;
2244 return 0;
2245 }
2246
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002247 /* otherwise, rebind */
Tejun Heo25511a42012-07-17 12:39:27 -07002248 idle_worker_rebind(worker);
2249 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 }
2251
Tejun Heoc8e55f32010-06-29 10:07:12 +02002252 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002253recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002254 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002255 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002256 goto sleep;
2257
2258 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002259 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002260 goto recheck;
2261
Tejun Heoc8e55f32010-06-29 10:07:12 +02002262 /*
2263 * ->scheduled list can only be filled while a worker is
2264 * preparing to process a work or actually processing it.
2265 * Make sure nobody diddled with it while I was sleeping.
2266 */
Tejun Heo6183c002013-03-12 11:29:57 -07002267 WARN_ON_ONCE(!list_empty(&worker->scheduled));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002268
Tejun Heoe22bee72010-06-29 10:07:14 +02002269 /*
2270 * When control reaches this point, we're guaranteed to have
2271 * at least one idle worker or that someone else has already
2272 * assumed the manager role.
2273 */
2274 worker_clr_flags(worker, WORKER_PREP);
2275
2276 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002277 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002278 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002279 struct work_struct, entry);
2280
2281 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2282 /* optimization path, not strictly necessary */
2283 process_one_work(worker, work);
2284 if (unlikely(!list_empty(&worker->scheduled)))
2285 process_scheduled_works(worker);
2286 } else {
2287 move_linked_works(work, &worker->scheduled, NULL);
2288 process_scheduled_works(worker);
2289 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002290 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002291
Tejun Heoe22bee72010-06-29 10:07:14 +02002292 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002293sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07002294 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002295 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002296
Tejun Heoc8e55f32010-06-29 10:07:12 +02002297 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002298 * pool->lock is held and there's no work to process and no need to
2299 * manage, sleep. Workers are woken up only while holding
2300 * pool->lock or from local cpu, so setting the current state
2301 * before releasing pool->lock is enough to prevent losing any
2302 * event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002303 */
2304 worker_enter_idle(worker);
2305 __set_current_state(TASK_INTERRUPTIBLE);
Tejun Heod565ed62013-01-24 11:01:33 -08002306 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02002307 schedule();
2308 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309}
2310
Tejun Heoe22bee72010-06-29 10:07:14 +02002311/**
2312 * rescuer_thread - the rescuer thread function
Tejun Heo111c2252013-01-17 17:16:24 -08002313 * @__rescuer: self
Tejun Heoe22bee72010-06-29 10:07:14 +02002314 *
2315 * Workqueue rescuer thread function. There's one rescuer for each
2316 * workqueue which has WQ_RESCUER set.
2317 *
Tejun Heo706026c2013-01-24 11:01:34 -08002318 * Regular work processing on a pool may block trying to create a new
Tejun Heoe22bee72010-06-29 10:07:14 +02002319 * worker which uses GFP_KERNEL allocation which has slight chance of
2320 * developing into deadlock if some works currently on the same queue
2321 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2322 * the problem rescuer solves.
2323 *
Tejun Heo706026c2013-01-24 11:01:34 -08002324 * When such condition is possible, the pool summons rescuers of all
2325 * workqueues which have works queued on the pool and let them process
Tejun Heoe22bee72010-06-29 10:07:14 +02002326 * those works so that forward progress can be guaranteed.
2327 *
2328 * This should happen rarely.
2329 */
Tejun Heo111c2252013-01-17 17:16:24 -08002330static int rescuer_thread(void *__rescuer)
Tejun Heoe22bee72010-06-29 10:07:14 +02002331{
Tejun Heo111c2252013-01-17 17:16:24 -08002332 struct worker *rescuer = __rescuer;
2333 struct workqueue_struct *wq = rescuer->rescue_wq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002334 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heoe22bee72010-06-29 10:07:14 +02002335
2336 set_user_nice(current, RESCUER_NICE_LEVEL);
Tejun Heo111c2252013-01-17 17:16:24 -08002337
2338 /*
2339 * Mark rescuer as worker too. As WORKER_PREP is never cleared, it
2340 * doesn't participate in concurrency management.
2341 */
2342 rescuer->task->flags |= PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002343repeat:
2344 set_current_state(TASK_INTERRUPTIBLE);
2345
Mike Galbraith412d32e2012-11-28 07:17:18 +01002346 if (kthread_should_stop()) {
2347 __set_current_state(TASK_RUNNING);
Tejun Heo111c2252013-01-17 17:16:24 -08002348 rescuer->task->flags &= ~PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002349 return 0;
Mike Galbraith412d32e2012-11-28 07:17:18 +01002350 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002351
Tejun Heo493a1722013-03-12 11:29:59 -07002352 /* see whether any pwq is asking for help */
2353 spin_lock_irq(&workqueue_lock);
2354
2355 while (!list_empty(&wq->maydays)) {
2356 struct pool_workqueue *pwq = list_first_entry(&wq->maydays,
2357 struct pool_workqueue, mayday_node);
Tejun Heo112202d2013-02-13 19:29:12 -08002358 struct worker_pool *pool = pwq->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002359 struct work_struct *work, *n;
2360
2361 __set_current_state(TASK_RUNNING);
Tejun Heo493a1722013-03-12 11:29:59 -07002362 list_del_init(&pwq->mayday_node);
2363
2364 spin_unlock_irq(&workqueue_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002365
2366 /* migrate to the target cpu if possible */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08002367 worker_maybe_bind_and_lock(pool);
Lai Jiangshanb3104102013-02-19 12:17:02 -08002368 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002369
2370 /*
2371 * Slurp in all works issued via this workqueue and
2372 * process'em.
2373 */
Tejun Heo6183c002013-03-12 11:29:57 -07002374 WARN_ON_ONCE(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002375 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heo112202d2013-02-13 19:29:12 -08002376 if (get_work_pwq(work) == pwq)
Tejun Heoe22bee72010-06-29 10:07:14 +02002377 move_linked_works(work, scheduled, &n);
2378
2379 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002380
2381 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002382 * Leave this pool. If keep_working() is %true, notify a
Tejun Heo75769582011-02-14 14:04:46 +01002383 * regular worker; otherwise, we end up with 0 concurrency
2384 * and stalling the execution.
2385 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002386 if (keep_working(pool))
2387 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002388
Lai Jiangshanb3104102013-02-19 12:17:02 -08002389 rescuer->pool = NULL;
Tejun Heo493a1722013-03-12 11:29:59 -07002390 spin_unlock(&pool->lock);
2391 spin_lock(&workqueue_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002392 }
2393
Tejun Heo493a1722013-03-12 11:29:59 -07002394 spin_unlock_irq(&workqueue_lock);
2395
Tejun Heo111c2252013-01-17 17:16:24 -08002396 /* rescuers should never participate in concurrency management */
2397 WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
Tejun Heoe22bee72010-06-29 10:07:14 +02002398 schedule();
2399 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400}
2401
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002402struct wq_barrier {
2403 struct work_struct work;
2404 struct completion done;
2405};
2406
2407static void wq_barrier_func(struct work_struct *work)
2408{
2409 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2410 complete(&barr->done);
2411}
2412
Tejun Heo4690c4a2010-06-29 10:07:10 +02002413/**
2414 * insert_wq_barrier - insert a barrier work
Tejun Heo112202d2013-02-13 19:29:12 -08002415 * @pwq: pwq to insert barrier into
Tejun Heo4690c4a2010-06-29 10:07:10 +02002416 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002417 * @target: target work to attach @barr to
2418 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002419 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002420 * @barr is linked to @target such that @barr is completed only after
2421 * @target finishes execution. Please note that the ordering
2422 * guarantee is observed only with respect to @target and on the local
2423 * cpu.
2424 *
2425 * Currently, a queued barrier can't be canceled. This is because
2426 * try_to_grab_pending() can't determine whether the work to be
2427 * grabbed is at the head of the queue and thus can't clear LINKED
2428 * flag of the previous work while there must be a valid next work
2429 * after a work with LINKED flag set.
2430 *
2431 * Note that when @worker is non-NULL, @target may be modified
Tejun Heo112202d2013-02-13 19:29:12 -08002432 * underneath us, so we can't reliably determine pwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002433 *
2434 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002435 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002436 */
Tejun Heo112202d2013-02-13 19:29:12 -08002437static void insert_wq_barrier(struct pool_workqueue *pwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002438 struct wq_barrier *barr,
2439 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002440{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002441 struct list_head *head;
2442 unsigned int linked = 0;
2443
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002444 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002445 * debugobject calls are safe here even with pool->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002446 * as we know for sure that this will not trigger any of the
2447 * checks and call back into the fixup functions where we
2448 * might deadlock.
2449 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002450 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002451 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002452 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002453
Tejun Heoaffee4b2010-06-29 10:07:12 +02002454 /*
2455 * If @target is currently being executed, schedule the
2456 * barrier to the worker; otherwise, put it after @target.
2457 */
2458 if (worker)
2459 head = worker->scheduled.next;
2460 else {
2461 unsigned long *bits = work_data_bits(target);
2462
2463 head = target->entry.next;
2464 /* there can already be other linked works, inherit and set */
2465 linked = *bits & WORK_STRUCT_LINKED;
2466 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2467 }
2468
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002469 debug_work_activate(&barr->work);
Tejun Heo112202d2013-02-13 19:29:12 -08002470 insert_work(pwq, &barr->work, head,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002471 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002472}
2473
Tejun Heo73f53c42010-06-29 10:07:11 +02002474/**
Tejun Heo112202d2013-02-13 19:29:12 -08002475 * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing
Tejun Heo73f53c42010-06-29 10:07:11 +02002476 * @wq: workqueue being flushed
2477 * @flush_color: new flush color, < 0 for no-op
2478 * @work_color: new work color, < 0 for no-op
2479 *
Tejun Heo112202d2013-02-13 19:29:12 -08002480 * Prepare pwqs for workqueue flushing.
Tejun Heo73f53c42010-06-29 10:07:11 +02002481 *
Tejun Heo112202d2013-02-13 19:29:12 -08002482 * If @flush_color is non-negative, flush_color on all pwqs should be
2483 * -1. If no pwq has in-flight commands at the specified color, all
2484 * pwq->flush_color's stay at -1 and %false is returned. If any pwq
2485 * has in flight commands, its pwq->flush_color is set to
2486 * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq
Tejun Heo73f53c42010-06-29 10:07:11 +02002487 * wakeup logic is armed and %true is returned.
2488 *
2489 * The caller should have initialized @wq->first_flusher prior to
2490 * calling this function with non-negative @flush_color. If
2491 * @flush_color is negative, no flush color update is done and %false
2492 * is returned.
2493 *
Tejun Heo112202d2013-02-13 19:29:12 -08002494 * If @work_color is non-negative, all pwqs should have the same
Tejun Heo73f53c42010-06-29 10:07:11 +02002495 * work_color which is previous to @work_color and all will be
2496 * advanced to @work_color.
2497 *
2498 * CONTEXT:
2499 * mutex_lock(wq->flush_mutex).
2500 *
2501 * RETURNS:
2502 * %true if @flush_color >= 0 and there's something to flush. %false
2503 * otherwise.
2504 */
Tejun Heo112202d2013-02-13 19:29:12 -08002505static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
Tejun Heo73f53c42010-06-29 10:07:11 +02002506 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507{
Tejun Heo73f53c42010-06-29 10:07:11 +02002508 bool wait = false;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002509 struct pool_workqueue *pwq;
Oleg Nesterov14441962007-05-23 13:57:57 -07002510
Tejun Heo73f53c42010-06-29 10:07:11 +02002511 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002512 WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush));
Tejun Heo112202d2013-02-13 19:29:12 -08002513 atomic_set(&wq->nr_pwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002514 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002515
Tejun Heo76af4d92013-03-12 11:30:00 -07002516 local_irq_disable();
2517
Tejun Heo49e3cf42013-03-12 11:29:58 -07002518 for_each_pwq(pwq, wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08002519 struct worker_pool *pool = pwq->pool;
Tejun Heo73f53c42010-06-29 10:07:11 +02002520
Tejun Heo76af4d92013-03-12 11:30:00 -07002521 spin_lock(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002522
2523 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002524 WARN_ON_ONCE(pwq->flush_color != -1);
Tejun Heo73f53c42010-06-29 10:07:11 +02002525
Tejun Heo112202d2013-02-13 19:29:12 -08002526 if (pwq->nr_in_flight[flush_color]) {
2527 pwq->flush_color = flush_color;
2528 atomic_inc(&wq->nr_pwqs_to_flush);
Tejun Heo73f53c42010-06-29 10:07:11 +02002529 wait = true;
2530 }
2531 }
2532
2533 if (work_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002534 WARN_ON_ONCE(work_color != work_next_color(pwq->work_color));
Tejun Heo112202d2013-02-13 19:29:12 -08002535 pwq->work_color = work_color;
Tejun Heo73f53c42010-06-29 10:07:11 +02002536 }
2537
Tejun Heo76af4d92013-03-12 11:30:00 -07002538 spin_unlock(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002539 }
2540
Tejun Heo76af4d92013-03-12 11:30:00 -07002541 local_irq_enable();
2542
Tejun Heo112202d2013-02-13 19:29:12 -08002543 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush))
Tejun Heo73f53c42010-06-29 10:07:11 +02002544 complete(&wq->first_flusher->done);
2545
2546 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547}
2548
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002549/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002551 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 *
2553 * Forces execution of the workqueue and blocks until its completion.
2554 * This is typically used in driver shutdown handlers.
2555 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002556 * We sleep until all works which were queued on entry have been handled,
2557 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002559void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560{
Tejun Heo73f53c42010-06-29 10:07:11 +02002561 struct wq_flusher this_flusher = {
2562 .list = LIST_HEAD_INIT(this_flusher.list),
2563 .flush_color = -1,
2564 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2565 };
2566 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002567
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002568 lock_map_acquire(&wq->lockdep_map);
2569 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002570
2571 mutex_lock(&wq->flush_mutex);
2572
2573 /*
2574 * Start-to-wait phase
2575 */
2576 next_color = work_next_color(wq->work_color);
2577
2578 if (next_color != wq->flush_color) {
2579 /*
2580 * Color space is not full. The current work_color
2581 * becomes our flush_color and work_color is advanced
2582 * by one.
2583 */
Tejun Heo6183c002013-03-12 11:29:57 -07002584 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow));
Tejun Heo73f53c42010-06-29 10:07:11 +02002585 this_flusher.flush_color = wq->work_color;
2586 wq->work_color = next_color;
2587
2588 if (!wq->first_flusher) {
2589 /* no flush in progress, become the first flusher */
Tejun Heo6183c002013-03-12 11:29:57 -07002590 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002591
2592 wq->first_flusher = &this_flusher;
2593
Tejun Heo112202d2013-02-13 19:29:12 -08002594 if (!flush_workqueue_prep_pwqs(wq, wq->flush_color,
Tejun Heo73f53c42010-06-29 10:07:11 +02002595 wq->work_color)) {
2596 /* nothing to flush, done */
2597 wq->flush_color = next_color;
2598 wq->first_flusher = NULL;
2599 goto out_unlock;
2600 }
2601 } else {
2602 /* wait in queue */
Tejun Heo6183c002013-03-12 11:29:57 -07002603 WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002604 list_add_tail(&this_flusher.list, &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002605 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002606 }
2607 } else {
2608 /*
2609 * Oops, color space is full, wait on overflow queue.
2610 * The next flush completion will assign us
2611 * flush_color and transfer to flusher_queue.
2612 */
2613 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2614 }
2615
2616 mutex_unlock(&wq->flush_mutex);
2617
2618 wait_for_completion(&this_flusher.done);
2619
2620 /*
2621 * Wake-up-and-cascade phase
2622 *
2623 * First flushers are responsible for cascading flushes and
2624 * handling overflow. Non-first flushers can simply return.
2625 */
2626 if (wq->first_flusher != &this_flusher)
2627 return;
2628
2629 mutex_lock(&wq->flush_mutex);
2630
Tejun Heo4ce48b32010-07-02 10:03:51 +02002631 /* we might have raced, check again with mutex held */
2632 if (wq->first_flusher != &this_flusher)
2633 goto out_unlock;
2634
Tejun Heo73f53c42010-06-29 10:07:11 +02002635 wq->first_flusher = NULL;
2636
Tejun Heo6183c002013-03-12 11:29:57 -07002637 WARN_ON_ONCE(!list_empty(&this_flusher.list));
2638 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002639
2640 while (true) {
2641 struct wq_flusher *next, *tmp;
2642
2643 /* complete all the flushers sharing the current flush color */
2644 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2645 if (next->flush_color != wq->flush_color)
2646 break;
2647 list_del_init(&next->list);
2648 complete(&next->done);
2649 }
2650
Tejun Heo6183c002013-03-12 11:29:57 -07002651 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) &&
2652 wq->flush_color != work_next_color(wq->work_color));
Tejun Heo73f53c42010-06-29 10:07:11 +02002653
2654 /* this flush_color is finished, advance by one */
2655 wq->flush_color = work_next_color(wq->flush_color);
2656
2657 /* one color has been freed, handle overflow queue */
2658 if (!list_empty(&wq->flusher_overflow)) {
2659 /*
2660 * Assign the same color to all overflowed
2661 * flushers, advance work_color and append to
2662 * flusher_queue. This is the start-to-wait
2663 * phase for these overflowed flushers.
2664 */
2665 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2666 tmp->flush_color = wq->work_color;
2667
2668 wq->work_color = work_next_color(wq->work_color);
2669
2670 list_splice_tail_init(&wq->flusher_overflow,
2671 &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002672 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002673 }
2674
2675 if (list_empty(&wq->flusher_queue)) {
Tejun Heo6183c002013-03-12 11:29:57 -07002676 WARN_ON_ONCE(wq->flush_color != wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002677 break;
2678 }
2679
2680 /*
2681 * Need to flush more colors. Make the next flusher
Tejun Heo112202d2013-02-13 19:29:12 -08002682 * the new first flusher and arm pwqs.
Tejun Heo73f53c42010-06-29 10:07:11 +02002683 */
Tejun Heo6183c002013-03-12 11:29:57 -07002684 WARN_ON_ONCE(wq->flush_color == wq->work_color);
2685 WARN_ON_ONCE(wq->flush_color != next->flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002686
2687 list_del_init(&next->list);
2688 wq->first_flusher = next;
2689
Tejun Heo112202d2013-02-13 19:29:12 -08002690 if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1))
Tejun Heo73f53c42010-06-29 10:07:11 +02002691 break;
2692
2693 /*
2694 * Meh... this color is already done, clear first
2695 * flusher and repeat cascading.
2696 */
2697 wq->first_flusher = NULL;
2698 }
2699
2700out_unlock:
2701 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702}
Dave Jonesae90dd52006-06-30 01:40:45 -04002703EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002705/**
2706 * drain_workqueue - drain a workqueue
2707 * @wq: workqueue to drain
2708 *
2709 * Wait until the workqueue becomes empty. While draining is in progress,
2710 * only chain queueing is allowed. IOW, only currently pending or running
2711 * work items on @wq can queue further work items on it. @wq is flushed
2712 * repeatedly until it becomes empty. The number of flushing is detemined
2713 * by the depth of chaining and should be relatively short. Whine if it
2714 * takes too long.
2715 */
2716void drain_workqueue(struct workqueue_struct *wq)
2717{
2718 unsigned int flush_cnt = 0;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002719 struct pool_workqueue *pwq;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002720
2721 /*
2722 * __queue_work() needs to test whether there are drainers, is much
2723 * hotter than drain_workqueue() and already looks at @wq->flags.
2724 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2725 */
Tejun Heoe98d5b12013-03-12 11:29:57 -07002726 spin_lock_irq(&workqueue_lock);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002727 if (!wq->nr_drainers++)
2728 wq->flags |= WQ_DRAINING;
Tejun Heoe98d5b12013-03-12 11:29:57 -07002729 spin_unlock_irq(&workqueue_lock);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002730reflush:
2731 flush_workqueue(wq);
2732
Tejun Heo76af4d92013-03-12 11:30:00 -07002733 local_irq_disable();
2734
Tejun Heo49e3cf42013-03-12 11:29:58 -07002735 for_each_pwq(pwq, wq) {
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002736 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002737
Tejun Heo76af4d92013-03-12 11:30:00 -07002738 spin_lock(&pwq->pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08002739 drained = !pwq->nr_active && list_empty(&pwq->delayed_works);
Tejun Heo76af4d92013-03-12 11:30:00 -07002740 spin_unlock(&pwq->pool->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002741
2742 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002743 continue;
2744
2745 if (++flush_cnt == 10 ||
2746 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
Valentin Ilie044c7822012-08-19 00:52:42 +03002747 pr_warn("workqueue %s: flush on destruction isn't complete after %u tries\n",
2748 wq->name, flush_cnt);
Tejun Heo76af4d92013-03-12 11:30:00 -07002749
2750 local_irq_enable();
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002751 goto reflush;
2752 }
2753
Tejun Heo76af4d92013-03-12 11:30:00 -07002754 spin_lock(&workqueue_lock);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002755 if (!--wq->nr_drainers)
2756 wq->flags &= ~WQ_DRAINING;
Tejun Heo76af4d92013-03-12 11:30:00 -07002757 spin_unlock(&workqueue_lock);
2758
2759 local_irq_enable();
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002760}
2761EXPORT_SYMBOL_GPL(drain_workqueue);
2762
Tejun Heo606a5022012-08-20 14:51:23 -07002763static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
Tejun Heobaf59022010-09-16 10:42:16 +02002764{
2765 struct worker *worker = NULL;
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002766 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002767 struct pool_workqueue *pwq;
Tejun Heobaf59022010-09-16 10:42:16 +02002768
2769 might_sleep();
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002770 pool = get_work_pool(work);
2771 if (!pool)
Tejun Heobaf59022010-09-16 10:42:16 +02002772 return false;
2773
Tejun Heod565ed62013-01-24 11:01:33 -08002774 spin_lock_irq(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08002775 /* see the comment in try_to_grab_pending() with the same code */
Tejun Heo112202d2013-02-13 19:29:12 -08002776 pwq = get_work_pwq(work);
2777 if (pwq) {
2778 if (unlikely(pwq->pool != pool))
Tejun Heobaf59022010-09-16 10:42:16 +02002779 goto already_gone;
Tejun Heo606a5022012-08-20 14:51:23 -07002780 } else {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002781 worker = find_worker_executing_work(pool, work);
Tejun Heobaf59022010-09-16 10:42:16 +02002782 if (!worker)
2783 goto already_gone;
Tejun Heo112202d2013-02-13 19:29:12 -08002784 pwq = worker->current_pwq;
Tejun Heo606a5022012-08-20 14:51:23 -07002785 }
Tejun Heobaf59022010-09-16 10:42:16 +02002786
Tejun Heo112202d2013-02-13 19:29:12 -08002787 insert_wq_barrier(pwq, barr, work, worker);
Tejun Heod565ed62013-01-24 11:01:33 -08002788 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002789
Tejun Heoe1594892011-01-09 23:32:15 +01002790 /*
2791 * If @max_active is 1 or rescuer is in use, flushing another work
2792 * item on the same workqueue may lead to deadlock. Make sure the
2793 * flusher is not running on the same workqueue by verifying write
2794 * access.
2795 */
Tejun Heo112202d2013-02-13 19:29:12 -08002796 if (pwq->wq->saved_max_active == 1 || pwq->wq->flags & WQ_RESCUER)
2797 lock_map_acquire(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002798 else
Tejun Heo112202d2013-02-13 19:29:12 -08002799 lock_map_acquire_read(&pwq->wq->lockdep_map);
2800 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002801
Tejun Heobaf59022010-09-16 10:42:16 +02002802 return true;
2803already_gone:
Tejun Heod565ed62013-01-24 11:01:33 -08002804 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002805 return false;
2806}
2807
Oleg Nesterovdb700892008-07-25 01:47:49 -07002808/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002809 * flush_work - wait for a work to finish executing the last queueing instance
2810 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002811 *
Tejun Heo606a5022012-08-20 14:51:23 -07002812 * Wait until @work has finished execution. @work is guaranteed to be idle
2813 * on return if it hasn't been requeued since flush started.
Tejun Heo401a8d02010-09-16 10:36:00 +02002814 *
2815 * RETURNS:
2816 * %true if flush_work() waited for the work to finish execution,
2817 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002818 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002819bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002820{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002821 struct wq_barrier barr;
2822
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002823 lock_map_acquire(&work->lockdep_map);
2824 lock_map_release(&work->lockdep_map);
2825
Tejun Heo606a5022012-08-20 14:51:23 -07002826 if (start_flush_work(work, &barr)) {
Tejun Heobaf59022010-09-16 10:42:16 +02002827 wait_for_completion(&barr.done);
2828 destroy_work_on_stack(&barr.work);
2829 return true;
Tejun Heo606a5022012-08-20 14:51:23 -07002830 } else {
Tejun Heobaf59022010-09-16 10:42:16 +02002831 return false;
Tejun Heo606a5022012-08-20 14:51:23 -07002832 }
Oleg Nesterovdb700892008-07-25 01:47:49 -07002833}
2834EXPORT_SYMBOL_GPL(flush_work);
2835
Tejun Heo36e227d2012-08-03 10:30:46 -07002836static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
Tejun Heo401a8d02010-09-16 10:36:00 +02002837{
Tejun Heobbb68df2012-08-03 10:30:46 -07002838 unsigned long flags;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002839 int ret;
2840
2841 do {
Tejun Heobbb68df2012-08-03 10:30:46 -07002842 ret = try_to_grab_pending(work, is_dwork, &flags);
2843 /*
2844 * If someone else is canceling, wait for the same event it
2845 * would be waiting for before retrying.
2846 */
2847 if (unlikely(ret == -ENOENT))
Tejun Heo606a5022012-08-20 14:51:23 -07002848 flush_work(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002849 } while (unlikely(ret < 0));
2850
Tejun Heobbb68df2012-08-03 10:30:46 -07002851 /* tell other tasks trying to grab @work to back off */
2852 mark_work_canceling(work);
2853 local_irq_restore(flags);
2854
Tejun Heo606a5022012-08-20 14:51:23 -07002855 flush_work(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002856 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002857 return ret;
2858}
2859
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002860/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002861 * cancel_work_sync - cancel a work and wait for it to finish
2862 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002863 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002864 * Cancel @work and wait for its execution to finish. This function
2865 * can be used even if the work re-queues itself or migrates to
2866 * another workqueue. On return from this function, @work is
2867 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002868 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002869 * cancel_work_sync(&delayed_work->work) must not be used for
2870 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002871 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002872 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002873 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002874 *
2875 * RETURNS:
2876 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002877 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002878bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002879{
Tejun Heo36e227d2012-08-03 10:30:46 -07002880 return __cancel_work_timer(work, false);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002881}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002882EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002883
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002884/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002885 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2886 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002887 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002888 * Delayed timer is cancelled and the pending work is queued for
2889 * immediate execution. Like flush_work(), this function only
2890 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002891 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002892 * RETURNS:
2893 * %true if flush_work() waited for the work to finish execution,
2894 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002895 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002896bool flush_delayed_work(struct delayed_work *dwork)
2897{
Tejun Heo8930cab2012-08-03 10:30:45 -07002898 local_irq_disable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002899 if (del_timer_sync(&dwork->timer))
Lai Jiangshan60c057b2013-02-06 18:04:53 -08002900 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Tejun Heo8930cab2012-08-03 10:30:45 -07002901 local_irq_enable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002902 return flush_work(&dwork->work);
2903}
2904EXPORT_SYMBOL(flush_delayed_work);
2905
2906/**
Tejun Heo57b30ae2012-08-21 13:18:24 -07002907 * cancel_delayed_work - cancel a delayed work
2908 * @dwork: delayed_work to cancel
Tejun Heo09383492010-09-16 10:48:29 +02002909 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002910 * Kill off a pending delayed_work. Returns %true if @dwork was pending
2911 * and canceled; %false if wasn't pending. Note that the work callback
2912 * function may still be running on return, unless it returns %true and the
2913 * work doesn't re-arm itself. Explicitly flush or use
2914 * cancel_delayed_work_sync() to wait on it.
Tejun Heo09383492010-09-16 10:48:29 +02002915 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002916 * This function is safe to call from any context including IRQ handler.
Tejun Heo09383492010-09-16 10:48:29 +02002917 */
Tejun Heo57b30ae2012-08-21 13:18:24 -07002918bool cancel_delayed_work(struct delayed_work *dwork)
Tejun Heo09383492010-09-16 10:48:29 +02002919{
Tejun Heo57b30ae2012-08-21 13:18:24 -07002920 unsigned long flags;
2921 int ret;
2922
2923 do {
2924 ret = try_to_grab_pending(&dwork->work, true, &flags);
2925 } while (unlikely(ret == -EAGAIN));
2926
2927 if (unlikely(ret < 0))
2928 return false;
2929
Tejun Heo7c3eed52013-01-24 11:01:33 -08002930 set_work_pool_and_clear_pending(&dwork->work,
2931 get_work_pool_id(&dwork->work));
Tejun Heo57b30ae2012-08-21 13:18:24 -07002932 local_irq_restore(flags);
Dan Magenheimerc0158ca2012-10-18 16:31:37 -07002933 return ret;
Tejun Heo09383492010-09-16 10:48:29 +02002934}
Tejun Heo57b30ae2012-08-21 13:18:24 -07002935EXPORT_SYMBOL(cancel_delayed_work);
Tejun Heo09383492010-09-16 10:48:29 +02002936
2937/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002938 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2939 * @dwork: the delayed work cancel
2940 *
2941 * This is cancel_work_sync() for delayed works.
2942 *
2943 * RETURNS:
2944 * %true if @dwork was pending, %false otherwise.
2945 */
2946bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002947{
Tejun Heo36e227d2012-08-03 10:30:46 -07002948 return __cancel_work_timer(&dwork->work, true);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002949}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002950EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002952/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07002953 * schedule_work_on - put work task on a specific cpu
2954 * @cpu: cpu to put the work task on
2955 * @work: job to be done
2956 *
2957 * This puts a job on a specific cpu
2958 */
Tejun Heod4283e92012-08-03 10:30:44 -07002959bool schedule_work_on(int cpu, struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07002960{
Tejun Heod320c032010-06-29 10:07:14 +02002961 return queue_work_on(cpu, system_wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07002962}
2963EXPORT_SYMBOL(schedule_work_on);
2964
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002965/**
Dave Jonesae90dd52006-06-30 01:40:45 -04002966 * schedule_work - put work task in global workqueue
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 * @work: job to be done
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 *
Tejun Heod4283e92012-08-03 10:30:44 -07002969 * Returns %false if @work was already on the kernel-global workqueue and
2970 * %true otherwise.
David Howells52bad642006-11-22 14:54:01 +00002971 *
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002972 * This puts a job in the kernel-global workqueue if it was not already
2973 * queued and leaves it in the same position on the kernel-global
2974 * workqueue otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 */
Tejun Heod4283e92012-08-03 10:30:44 -07002976bool schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977{
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002978 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979}
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002980EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002982/**
2983 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
2984 * @cpu: cpu to use
2985 * @dwork: job to be done
2986 * @delay: number of jiffies to wait
2987 *
2988 * After waiting for a given time this puts a job in the kernel-global
2989 * workqueue on the specified CPU.
2990 */
Tejun Heod4283e92012-08-03 10:30:44 -07002991bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork,
2992 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993{
Tejun Heod320c032010-06-29 10:07:14 +02002994 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995}
Dave Jonesae90dd52006-06-30 01:40:45 -04002996EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
Andrew Mortonb6136772006-06-25 05:47:49 -07002998/**
Tejun Heo0a13c002012-08-03 10:30:44 -07002999 * schedule_delayed_work - put work task in global workqueue after delay
3000 * @dwork: job to be done
3001 * @delay: number of jiffies to wait or 0 for immediate execution
3002 *
3003 * After waiting for a given time this puts a job in the kernel-global
3004 * workqueue.
3005 */
Tejun Heod4283e92012-08-03 10:30:44 -07003006bool schedule_delayed_work(struct delayed_work *dwork, unsigned long delay)
Tejun Heo0a13c002012-08-03 10:30:44 -07003007{
3008 return queue_delayed_work(system_wq, dwork, delay);
3009}
3010EXPORT_SYMBOL(schedule_delayed_work);
3011
3012/**
Tejun Heo31ddd872010-10-19 11:14:49 +02003013 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07003014 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07003015 *
Tejun Heo31ddd872010-10-19 11:14:49 +02003016 * schedule_on_each_cpu() executes @func on each online CPU using the
3017 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07003018 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02003019 *
3020 * RETURNS:
3021 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07003022 */
David Howells65f27f32006-11-22 14:55:48 +00003023int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08003024{
3025 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02003026 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08003027
Andrew Mortonb6136772006-06-25 05:47:49 -07003028 works = alloc_percpu(struct work_struct);
3029 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08003030 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07003031
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003032 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08003033
Christoph Lameter15316ba2006-01-08 01:00:43 -08003034 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01003035 struct work_struct *work = per_cpu_ptr(works, cpu);
3036
3037 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003038 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02003039 }
Tejun Heo93981802009-11-17 14:06:20 -08003040
3041 for_each_online_cpu(cpu)
3042 flush_work(per_cpu_ptr(works, cpu));
3043
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003044 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07003045 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08003046 return 0;
3047}
3048
Alan Sterneef6a7d2010-02-12 17:39:21 +09003049/**
3050 * flush_scheduled_work - ensure that any scheduled work has run to completion.
3051 *
3052 * Forces execution of the kernel-global workqueue and blocks until its
3053 * completion.
3054 *
3055 * Think twice before calling this function! It's very easy to get into
3056 * trouble if you don't take great care. Either of the following situations
3057 * will lead to deadlock:
3058 *
3059 * One of the work items currently on the workqueue needs to acquire
3060 * a lock held by your code or its caller.
3061 *
3062 * Your code is running in the context of a work routine.
3063 *
3064 * They will be detected by lockdep when they occur, but the first might not
3065 * occur very often. It depends on what work items are on the workqueue and
3066 * what locks they need, which you have no control over.
3067 *
3068 * In most situations flushing the entire workqueue is overkill; you merely
3069 * need to know that a particular work item isn't queued and isn't running.
3070 * In such cases you should use cancel_delayed_work_sync() or
3071 * cancel_work_sync() instead.
3072 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073void flush_scheduled_work(void)
3074{
Tejun Heod320c032010-06-29 10:07:14 +02003075 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076}
Dave Jonesae90dd52006-06-30 01:40:45 -04003077EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
3079/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06003080 * execute_in_process_context - reliably execute the routine with user context
3081 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06003082 * @ew: guaranteed storage for the execute work structure (must
3083 * be available when the work executes)
3084 *
3085 * Executes the function immediately if process context is available,
3086 * otherwise schedules the function for delayed execution.
3087 *
3088 * Returns: 0 - function was executed
3089 * 1 - function was scheduled for execution
3090 */
David Howells65f27f32006-11-22 14:55:48 +00003091int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06003092{
3093 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00003094 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003095 return 0;
3096 }
3097
David Howells65f27f32006-11-22 14:55:48 +00003098 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003099 schedule_work(&ew->work);
3100
3101 return 1;
3102}
3103EXPORT_SYMBOL_GPL(execute_in_process_context);
3104
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105int keventd_up(void)
3106{
Tejun Heod320c032010-06-29 10:07:14 +02003107 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108}
3109
Tejun Heo30cdf242013-03-12 11:29:57 -07003110static int alloc_and_link_pwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003112 bool highpri = wq->flags & WQ_HIGHPRI;
Tejun Heo30cdf242013-03-12 11:29:57 -07003113 int cpu;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01003114
Tejun Heo30cdf242013-03-12 11:29:57 -07003115 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo420c0dd2013-03-12 11:29:59 -07003116 wq->cpu_pwqs = alloc_percpu(struct pool_workqueue);
3117 if (!wq->cpu_pwqs)
Tejun Heo30cdf242013-03-12 11:29:57 -07003118 return -ENOMEM;
3119
3120 for_each_possible_cpu(cpu) {
Tejun Heo7fb98ea2013-03-12 11:30:00 -07003121 struct pool_workqueue *pwq =
3122 per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heo30cdf242013-03-12 11:29:57 -07003123
Tejun Heo49e3cf42013-03-12 11:29:58 -07003124 pwq->pool = get_std_worker_pool(cpu, highpri);
Tejun Heo76af4d92013-03-12 11:30:00 -07003125 list_add_tail_rcu(&pwq->pwqs_node, &wq->pwqs);
Tejun Heo30cdf242013-03-12 11:29:57 -07003126 }
3127 } else {
3128 struct pool_workqueue *pwq;
3129
3130 pwq = kmem_cache_zalloc(pwq_cache, GFP_KERNEL);
3131 if (!pwq)
3132 return -ENOMEM;
3133
Tejun Heo49e3cf42013-03-12 11:29:58 -07003134 pwq->pool = get_std_worker_pool(WORK_CPU_UNBOUND, highpri);
Tejun Heo76af4d92013-03-12 11:30:00 -07003135 list_add_tail_rcu(&pwq->pwqs_node, &wq->pwqs);
Tejun Heo30cdf242013-03-12 11:29:57 -07003136 }
3137
3138 return 0;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003139}
3140
Tejun Heo112202d2013-02-13 19:29:12 -08003141static void free_pwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07003142{
Lai Jiangshane06ffa12012-03-09 18:03:20 +08003143 if (!(wq->flags & WQ_UNBOUND))
Tejun Heo420c0dd2013-03-12 11:29:59 -07003144 free_percpu(wq->cpu_pwqs);
3145 else if (!list_empty(&wq->pwqs))
3146 kmem_cache_free(pwq_cache, list_first_entry(&wq->pwqs,
3147 struct pool_workqueue, pwqs_node));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07003148}
3149
Tejun Heof3421792010-07-02 10:03:51 +02003150static int wq_clamp_max_active(int max_active, unsigned int flags,
3151 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003152{
Tejun Heof3421792010-07-02 10:03:51 +02003153 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3154
3155 if (max_active < 1 || max_active > lim)
Valentin Ilie044c7822012-08-19 00:52:42 +03003156 pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
3157 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003158
Tejun Heof3421792010-07-02 10:03:51 +02003159 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003160}
3161
Tejun Heob196be82012-01-10 15:11:35 -08003162struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02003163 unsigned int flags,
3164 int max_active,
3165 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08003166 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003167{
Tejun Heob196be82012-01-10 15:11:35 -08003168 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003169 struct workqueue_struct *wq;
Tejun Heo49e3cf42013-03-12 11:29:58 -07003170 struct pool_workqueue *pwq;
Tejun Heob196be82012-01-10 15:11:35 -08003171 size_t namelen;
3172
3173 /* determine namelen, allocate wq and format name */
3174 va_start(args, lock_name);
3175 va_copy(args1, args);
3176 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3177
3178 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3179 if (!wq)
3180 goto err;
3181
3182 vsnprintf(wq->name, namelen, fmt, args1);
3183 va_end(args);
3184 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003185
Tejun Heof3421792010-07-02 10:03:51 +02003186 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02003187 * Workqueues which may be used during memory reclaim should
3188 * have a rescuer to guarantee forward progress.
3189 */
3190 if (flags & WQ_MEM_RECLAIM)
3191 flags |= WQ_RESCUER;
3192
Tejun Heod320c032010-06-29 10:07:14 +02003193 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003194 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003195
Tejun Heob196be82012-01-10 15:11:35 -08003196 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003197 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003198 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003199 mutex_init(&wq->flush_mutex);
Tejun Heo112202d2013-02-13 19:29:12 -08003200 atomic_set(&wq->nr_pwqs_to_flush, 0);
Tejun Heo30cdf242013-03-12 11:29:57 -07003201 INIT_LIST_HEAD(&wq->pwqs);
Tejun Heo73f53c42010-06-29 10:07:11 +02003202 INIT_LIST_HEAD(&wq->flusher_queue);
3203 INIT_LIST_HEAD(&wq->flusher_overflow);
Tejun Heo493a1722013-03-12 11:29:59 -07003204 INIT_LIST_HEAD(&wq->maydays);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003205
Johannes Bergeb13ba82008-01-16 09:51:58 +01003206 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003207 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003208
Tejun Heo30cdf242013-03-12 11:29:57 -07003209 if (alloc_and_link_pwqs(wq) < 0)
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003210 goto err;
3211
Tejun Heo76af4d92013-03-12 11:30:00 -07003212 local_irq_disable();
Tejun Heo49e3cf42013-03-12 11:29:58 -07003213 for_each_pwq(pwq, wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08003214 BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK);
Tejun Heo112202d2013-02-13 19:29:12 -08003215 pwq->wq = wq;
3216 pwq->flush_color = -1;
3217 pwq->max_active = max_active;
3218 INIT_LIST_HEAD(&pwq->delayed_works);
Tejun Heo493a1722013-03-12 11:29:59 -07003219 INIT_LIST_HEAD(&pwq->mayday_node);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003220 }
Tejun Heo76af4d92013-03-12 11:30:00 -07003221 local_irq_enable();
Oleg Nesterov3af244332007-05-09 02:34:09 -07003222
Tejun Heoe22bee72010-06-29 10:07:14 +02003223 if (flags & WQ_RESCUER) {
3224 struct worker *rescuer;
3225
Tejun Heoe22bee72010-06-29 10:07:14 +02003226 wq->rescuer = rescuer = alloc_worker();
3227 if (!rescuer)
3228 goto err;
3229
Tejun Heo111c2252013-01-17 17:16:24 -08003230 rescuer->rescue_wq = wq;
3231 rescuer->task = kthread_create(rescuer_thread, rescuer, "%s",
Tejun Heob196be82012-01-10 15:11:35 -08003232 wq->name);
Tejun Heoe22bee72010-06-29 10:07:14 +02003233 if (IS_ERR(rescuer->task))
3234 goto err;
3235
Tejun Heoe22bee72010-06-29 10:07:14 +02003236 rescuer->task->flags |= PF_THREAD_BOUND;
3237 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003238 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003239
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003240 /*
3241 * workqueue_lock protects global freeze state and workqueues
3242 * list. Grab it, set max_active accordingly and add the new
3243 * workqueue to workqueues list.
3244 */
Tejun Heoe98d5b12013-03-12 11:29:57 -07003245 spin_lock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003246
Tejun Heo58a69cb2011-02-16 09:25:31 +01003247 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heo49e3cf42013-03-12 11:29:58 -07003248 for_each_pwq(pwq, wq)
3249 pwq->max_active = 0;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003250
Tejun Heo15376632010-06-29 10:07:11 +02003251 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003252
Tejun Heoe98d5b12013-03-12 11:29:57 -07003253 spin_unlock_irq(&workqueue_lock);
Tejun Heo15376632010-06-29 10:07:11 +02003254
Oleg Nesterov3af244332007-05-09 02:34:09 -07003255 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02003256err:
3257 if (wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08003258 free_pwqs(wq);
Tejun Heoe22bee72010-06-29 10:07:14 +02003259 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003260 kfree(wq);
3261 }
3262 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003263}
Tejun Heod320c032010-06-29 10:07:14 +02003264EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003265
3266/**
3267 * destroy_workqueue - safely terminate a workqueue
3268 * @wq: target workqueue
3269 *
3270 * Safely destroy a workqueue. All work currently pending will be done first.
3271 */
3272void destroy_workqueue(struct workqueue_struct *wq)
3273{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003274 struct pool_workqueue *pwq;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003275
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003276 /* drain it before proceeding with destruction */
3277 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003278
Tejun Heo76af4d92013-03-12 11:30:00 -07003279 spin_lock_irq(&workqueue_lock);
3280
Tejun Heo6183c002013-03-12 11:29:57 -07003281 /* sanity checks */
Tejun Heo49e3cf42013-03-12 11:29:58 -07003282 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07003283 int i;
3284
Tejun Heo76af4d92013-03-12 11:30:00 -07003285 for (i = 0; i < WORK_NR_COLORS; i++) {
3286 if (WARN_ON(pwq->nr_in_flight[i])) {
3287 spin_unlock_irq(&workqueue_lock);
Tejun Heo6183c002013-03-12 11:29:57 -07003288 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07003289 }
3290 }
3291
Tejun Heo6183c002013-03-12 11:29:57 -07003292 if (WARN_ON(pwq->nr_active) ||
Tejun Heo76af4d92013-03-12 11:30:00 -07003293 WARN_ON(!list_empty(&pwq->delayed_works))) {
3294 spin_unlock_irq(&workqueue_lock);
Tejun Heo6183c002013-03-12 11:29:57 -07003295 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07003296 }
Tejun Heo6183c002013-03-12 11:29:57 -07003297 }
3298
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003299 /*
3300 * wq list is used to freeze wq, remove from list after
3301 * flushing is complete in case freeze races us.
3302 */
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07003303 list_del(&wq->list);
Tejun Heo76af4d92013-03-12 11:30:00 -07003304
Tejun Heoe98d5b12013-03-12 11:29:57 -07003305 spin_unlock_irq(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003306
Tejun Heoe22bee72010-06-29 10:07:14 +02003307 if (wq->flags & WQ_RESCUER) {
3308 kthread_stop(wq->rescuer->task);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003309 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003310 }
3311
Tejun Heo112202d2013-02-13 19:29:12 -08003312 free_pwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003313 kfree(wq);
3314}
3315EXPORT_SYMBOL_GPL(destroy_workqueue);
3316
Tejun Heodcd989c2010-06-29 10:07:14 +02003317/**
Tejun Heo112202d2013-02-13 19:29:12 -08003318 * pwq_set_max_active - adjust max_active of a pwq
3319 * @pwq: target pool_workqueue
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003320 * @max_active: new max_active value.
3321 *
Tejun Heo112202d2013-02-13 19:29:12 -08003322 * Set @pwq->max_active to @max_active and activate delayed works if
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003323 * increased.
3324 *
3325 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08003326 * spin_lock_irq(pool->lock).
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003327 */
Tejun Heo112202d2013-02-13 19:29:12 -08003328static void pwq_set_max_active(struct pool_workqueue *pwq, int max_active)
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003329{
Tejun Heo112202d2013-02-13 19:29:12 -08003330 pwq->max_active = max_active;
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003331
Tejun Heo112202d2013-02-13 19:29:12 -08003332 while (!list_empty(&pwq->delayed_works) &&
3333 pwq->nr_active < pwq->max_active)
3334 pwq_activate_first_delayed(pwq);
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003335}
3336
3337/**
Tejun Heodcd989c2010-06-29 10:07:14 +02003338 * workqueue_set_max_active - adjust max_active of a workqueue
3339 * @wq: target workqueue
3340 * @max_active: new max_active value.
3341 *
3342 * Set max_active of @wq to @max_active.
3343 *
3344 * CONTEXT:
3345 * Don't call from IRQ context.
3346 */
3347void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3348{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003349 struct pool_workqueue *pwq;
Tejun Heodcd989c2010-06-29 10:07:14 +02003350
Tejun Heof3421792010-07-02 10:03:51 +02003351 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003352
Tejun Heoe98d5b12013-03-12 11:29:57 -07003353 spin_lock_irq(&workqueue_lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02003354
3355 wq->saved_max_active = max_active;
3356
Tejun Heo49e3cf42013-03-12 11:29:58 -07003357 for_each_pwq(pwq, wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08003358 struct worker_pool *pool = pwq->pool;
Tejun Heodcd989c2010-06-29 10:07:14 +02003359
Tejun Heoe98d5b12013-03-12 11:29:57 -07003360 spin_lock(&pool->lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02003361
Tejun Heo58a69cb2011-02-16 09:25:31 +01003362 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heo35b6bb62013-01-24 11:01:33 -08003363 !(pool->flags & POOL_FREEZING))
Tejun Heo112202d2013-02-13 19:29:12 -08003364 pwq_set_max_active(pwq, max_active);
Tejun Heodcd989c2010-06-29 10:07:14 +02003365
Tejun Heoe98d5b12013-03-12 11:29:57 -07003366 spin_unlock(&pool->lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02003367 }
3368
Tejun Heoe98d5b12013-03-12 11:29:57 -07003369 spin_unlock_irq(&workqueue_lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02003370}
3371EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3372
3373/**
3374 * workqueue_congested - test whether a workqueue is congested
3375 * @cpu: CPU in question
3376 * @wq: target workqueue
3377 *
3378 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3379 * no synchronization around this function and the test result is
3380 * unreliable and only useful as advisory hints or for debugging.
3381 *
3382 * RETURNS:
3383 * %true if congested, %false otherwise.
3384 */
Tejun Heod84ff052013-03-12 11:29:59 -07003385bool workqueue_congested(int cpu, struct workqueue_struct *wq)
Tejun Heodcd989c2010-06-29 10:07:14 +02003386{
Tejun Heo7fb98ea2013-03-12 11:30:00 -07003387 struct pool_workqueue *pwq;
Tejun Heo76af4d92013-03-12 11:30:00 -07003388 bool ret;
3389
3390 preempt_disable();
Tejun Heo7fb98ea2013-03-12 11:30:00 -07003391
3392 if (!(wq->flags & WQ_UNBOUND))
3393 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
3394 else
3395 pwq = first_pwq(wq);
Tejun Heodcd989c2010-06-29 10:07:14 +02003396
Tejun Heo76af4d92013-03-12 11:30:00 -07003397 ret = !list_empty(&pwq->delayed_works);
3398 preempt_enable();
3399
3400 return ret;
Tejun Heodcd989c2010-06-29 10:07:14 +02003401}
3402EXPORT_SYMBOL_GPL(workqueue_congested);
3403
3404/**
Tejun Heodcd989c2010-06-29 10:07:14 +02003405 * work_busy - test whether a work is currently pending or running
3406 * @work: the work to be tested
3407 *
3408 * Test whether @work is currently pending or running. There is no
3409 * synchronization around this function and the test result is
3410 * unreliable and only useful as advisory hints or for debugging.
Tejun Heodcd989c2010-06-29 10:07:14 +02003411 *
3412 * RETURNS:
3413 * OR'd bitmask of WORK_BUSY_* bits.
3414 */
3415unsigned int work_busy(struct work_struct *work)
3416{
Tejun Heoc9e7cf22013-01-24 11:01:33 -08003417 struct worker_pool *pool = get_work_pool(work);
Tejun Heodcd989c2010-06-29 10:07:14 +02003418 unsigned long flags;
3419 unsigned int ret = 0;
3420
Tejun Heodcd989c2010-06-29 10:07:14 +02003421 if (work_pending(work))
3422 ret |= WORK_BUSY_PENDING;
Tejun Heodcd989c2010-06-29 10:07:14 +02003423
Lai Jiangshan038366c2013-02-06 18:04:53 -08003424 if (pool) {
3425 spin_lock_irqsave(&pool->lock, flags);
3426 if (find_worker_executing_work(pool, work))
3427 ret |= WORK_BUSY_RUNNING;
3428 spin_unlock_irqrestore(&pool->lock, flags);
3429 }
Tejun Heodcd989c2010-06-29 10:07:14 +02003430
3431 return ret;
3432}
3433EXPORT_SYMBOL_GPL(work_busy);
3434
Tejun Heodb7bccf2010-06-29 10:07:12 +02003435/*
3436 * CPU hotplug.
3437 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003438 * There are two challenges in supporting CPU hotplug. Firstly, there
Tejun Heo112202d2013-02-13 19:29:12 -08003439 * are a lot of assumptions on strong associations among work, pwq and
Tejun Heo706026c2013-01-24 11:01:34 -08003440 * pool which make migrating pending and scheduled works very
Tejun Heoe22bee72010-06-29 10:07:14 +02003441 * difficult to implement without impacting hot paths. Secondly,
Tejun Heo94cf58b2013-01-24 11:01:33 -08003442 * worker pools serve mix of short, long and very long running works making
Tejun Heoe22bee72010-06-29 10:07:14 +02003443 * blocked draining impractical.
3444 *
Tejun Heo24647572013-01-24 11:01:33 -08003445 * This is solved by allowing the pools to be disassociated from the CPU
Tejun Heo628c78e2012-07-17 12:39:27 -07003446 * running as an unbound one and allowing it to be reattached later if the
3447 * cpu comes back online.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003448 */
3449
Tejun Heo706026c2013-01-24 11:01:34 -08003450static void wq_unbind_fn(struct work_struct *work)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003451{
Tejun Heo38db41d2013-01-24 11:01:34 -08003452 int cpu = smp_processor_id();
Tejun Heo4ce62e92012-07-13 22:16:44 -07003453 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003454 struct worker *worker;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003455 int i;
3456
Tejun Heo38db41d2013-01-24 11:01:34 -08003457 for_each_std_worker_pool(pool, cpu) {
Tejun Heo6183c002013-03-12 11:29:57 -07003458 WARN_ON_ONCE(cpu != smp_processor_id());
Tejun Heo94cf58b2013-01-24 11:01:33 -08003459
3460 mutex_lock(&pool->assoc_mutex);
3461 spin_lock_irq(&pool->lock);
3462
3463 /*
3464 * We've claimed all manager positions. Make all workers
3465 * unbound and set DISASSOCIATED. Before this, all workers
3466 * except for the ones which are still executing works from
3467 * before the last CPU down must be on the cpu. After
3468 * this, they may become diasporas.
3469 */
Tejun Heo4ce62e92012-07-13 22:16:44 -07003470 list_for_each_entry(worker, &pool->idle_list, entry)
Tejun Heo403c8212012-07-17 12:39:27 -07003471 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003472
Sasha Levinb67bfe02013-02-27 17:06:00 -08003473 for_each_busy_worker(worker, i, pool)
Tejun Heoc9e7cf22013-01-24 11:01:33 -08003474 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003475
Tejun Heo24647572013-01-24 11:01:33 -08003476 pool->flags |= POOL_DISASSOCIATED;
Tejun Heof2d5a0e2012-07-17 12:39:26 -07003477
Tejun Heo94cf58b2013-01-24 11:01:33 -08003478 spin_unlock_irq(&pool->lock);
3479 mutex_unlock(&pool->assoc_mutex);
3480 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003481
3482 /*
Tejun Heo628c78e2012-07-17 12:39:27 -07003483 * Call schedule() so that we cross rq->lock and thus can guarantee
3484 * sched callbacks see the %WORKER_UNBOUND flag. This is necessary
3485 * as scheduler callbacks may be invoked from other cpus.
3486 */
3487 schedule();
3488
3489 /*
3490 * Sched callbacks are disabled now. Zap nr_running. After this,
3491 * nr_running stays zero and need_more_worker() and keep_working()
Tejun Heo38db41d2013-01-24 11:01:34 -08003492 * are always true as long as the worklist is not empty. Pools on
3493 * @cpu now behave as unbound (in terms of concurrency management)
3494 * pools which are served by workers tied to the CPU.
Tejun Heo628c78e2012-07-17 12:39:27 -07003495 *
3496 * On return from this function, the current worker would trigger
3497 * unbound chain execution of pending work items if other workers
3498 * didn't already.
Tejun Heoe22bee72010-06-29 10:07:14 +02003499 */
Tejun Heo38db41d2013-01-24 11:01:34 -08003500 for_each_std_worker_pool(pool, cpu)
Tejun Heoe19e3972013-01-24 11:39:44 -08003501 atomic_set(&pool->nr_running, 0);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003502}
3503
Tejun Heo8db25e72012-07-17 12:39:28 -07003504/*
3505 * Workqueues should be brought up before normal priority CPU notifiers.
3506 * This will be registered high priority CPU notifier.
3507 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07003508static int __cpuinit workqueue_cpu_up_callback(struct notifier_block *nfb,
Tejun Heo8db25e72012-07-17 12:39:28 -07003509 unsigned long action,
3510 void *hcpu)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003511{
Tejun Heod84ff052013-03-12 11:29:59 -07003512 int cpu = (unsigned long)hcpu;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003513 struct worker_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514
Tejun Heo8db25e72012-07-17 12:39:28 -07003515 switch (action & ~CPU_TASKS_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 case CPU_UP_PREPARE:
Tejun Heo38db41d2013-01-24 11:01:34 -08003517 for_each_std_worker_pool(pool, cpu) {
Tejun Heo3ce63372012-07-17 12:39:27 -07003518 struct worker *worker;
3519
3520 if (pool->nr_workers)
3521 continue;
3522
3523 worker = create_worker(pool);
3524 if (!worker)
3525 return NOTIFY_BAD;
3526
Tejun Heod565ed62013-01-24 11:01:33 -08003527 spin_lock_irq(&pool->lock);
Tejun Heo3ce63372012-07-17 12:39:27 -07003528 start_worker(worker);
Tejun Heod565ed62013-01-24 11:01:33 -08003529 spin_unlock_irq(&pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003531 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003532
Tejun Heo65758202012-07-17 12:39:26 -07003533 case CPU_DOWN_FAILED:
3534 case CPU_ONLINE:
Tejun Heo38db41d2013-01-24 11:01:34 -08003535 for_each_std_worker_pool(pool, cpu) {
Tejun Heo94cf58b2013-01-24 11:01:33 -08003536 mutex_lock(&pool->assoc_mutex);
3537 spin_lock_irq(&pool->lock);
3538
Tejun Heo24647572013-01-24 11:01:33 -08003539 pool->flags &= ~POOL_DISASSOCIATED;
Tejun Heo94cf58b2013-01-24 11:01:33 -08003540 rebind_workers(pool);
3541
3542 spin_unlock_irq(&pool->lock);
3543 mutex_unlock(&pool->assoc_mutex);
3544 }
Tejun Heo8db25e72012-07-17 12:39:28 -07003545 break;
Tejun Heo65758202012-07-17 12:39:26 -07003546 }
3547 return NOTIFY_OK;
3548}
3549
3550/*
3551 * Workqueues should be brought down after normal priority CPU notifiers.
3552 * This will be registered as low priority CPU notifier.
3553 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07003554static int __cpuinit workqueue_cpu_down_callback(struct notifier_block *nfb,
Tejun Heo65758202012-07-17 12:39:26 -07003555 unsigned long action,
3556 void *hcpu)
3557{
Tejun Heod84ff052013-03-12 11:29:59 -07003558 int cpu = (unsigned long)hcpu;
Tejun Heo8db25e72012-07-17 12:39:28 -07003559 struct work_struct unbind_work;
3560
Tejun Heo65758202012-07-17 12:39:26 -07003561 switch (action & ~CPU_TASKS_FROZEN) {
3562 case CPU_DOWN_PREPARE:
Tejun Heo8db25e72012-07-17 12:39:28 -07003563 /* unbinding should happen on the local CPU */
Tejun Heo706026c2013-01-24 11:01:34 -08003564 INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn);
Joonsoo Kim7635d2f2012-08-15 23:25:41 +09003565 queue_work_on(cpu, system_highpri_wq, &unbind_work);
Tejun Heo8db25e72012-07-17 12:39:28 -07003566 flush_work(&unbind_work);
3567 break;
Tejun Heo65758202012-07-17 12:39:26 -07003568 }
3569 return NOTIFY_OK;
3570}
3571
Rusty Russell2d3854a2008-11-05 13:39:10 +11003572#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003573
Rusty Russell2d3854a2008-11-05 13:39:10 +11003574struct work_for_cpu {
Tejun Heoed48ece2012-09-18 12:48:43 -07003575 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003576 long (*fn)(void *);
3577 void *arg;
3578 long ret;
3579};
3580
Tejun Heoed48ece2012-09-18 12:48:43 -07003581static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003582{
Tejun Heoed48ece2012-09-18 12:48:43 -07003583 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
3584
Rusty Russell2d3854a2008-11-05 13:39:10 +11003585 wfc->ret = wfc->fn(wfc->arg);
3586}
3587
3588/**
3589 * work_on_cpu - run a function in user context on a particular cpu
3590 * @cpu: the cpu to run on
3591 * @fn: the function to run
3592 * @arg: the function arg
3593 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003594 * This will return the value @fn returns.
3595 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06003596 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003597 */
Tejun Heod84ff052013-03-12 11:29:59 -07003598long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003599{
Tejun Heoed48ece2012-09-18 12:48:43 -07003600 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003601
Tejun Heoed48ece2012-09-18 12:48:43 -07003602 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
3603 schedule_work_on(cpu, &wfc.work);
3604 flush_work(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003605 return wfc.ret;
3606}
3607EXPORT_SYMBOL_GPL(work_on_cpu);
3608#endif /* CONFIG_SMP */
3609
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003610#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303611
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003612/**
3613 * freeze_workqueues_begin - begin freezing workqueues
3614 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003615 * Start freezing workqueues. After this function returns, all freezable
3616 * workqueues will queue new works to their frozen_works list instead of
Tejun Heo706026c2013-01-24 11:01:34 -08003617 * pool->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003618 *
3619 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08003620 * Grabs and releases workqueue_lock and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003621 */
3622void freeze_workqueues_begin(void)
3623{
Tejun Heo17116962013-03-12 11:29:58 -07003624 struct worker_pool *pool;
Tejun Heo24b8a842013-03-12 11:29:58 -07003625 struct workqueue_struct *wq;
3626 struct pool_workqueue *pwq;
Tejun Heo17116962013-03-12 11:29:58 -07003627 int id;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003628
Tejun Heoe98d5b12013-03-12 11:29:57 -07003629 spin_lock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003630
Tejun Heo6183c002013-03-12 11:29:57 -07003631 WARN_ON_ONCE(workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003632 workqueue_freezing = true;
3633
Tejun Heo24b8a842013-03-12 11:29:58 -07003634 /* set FREEZING */
Tejun Heo17116962013-03-12 11:29:58 -07003635 for_each_pool(pool, id) {
Tejun Heo17116962013-03-12 11:29:58 -07003636 spin_lock(&pool->lock);
Tejun Heo17116962013-03-12 11:29:58 -07003637 WARN_ON_ONCE(pool->flags & POOL_FREEZING);
3638 pool->flags |= POOL_FREEZING;
Tejun Heo17116962013-03-12 11:29:58 -07003639 spin_unlock(&pool->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003640 }
3641
Tejun Heo24b8a842013-03-12 11:29:58 -07003642 /* suppress further executions by setting max_active to zero */
3643 list_for_each_entry(wq, &workqueues, list) {
3644 if (!(wq->flags & WQ_FREEZABLE))
3645 continue;
3646
3647 for_each_pwq(pwq, wq) {
3648 spin_lock(&pwq->pool->lock);
3649 pwq->max_active = 0;
3650 spin_unlock(&pwq->pool->lock);
3651 }
3652 }
3653
Tejun Heoe98d5b12013-03-12 11:29:57 -07003654 spin_unlock_irq(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003656
3657/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003658 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003659 *
3660 * Check whether freezing is complete. This function must be called
3661 * between freeze_workqueues_begin() and thaw_workqueues().
3662 *
3663 * CONTEXT:
3664 * Grabs and releases workqueue_lock.
3665 *
3666 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01003667 * %true if some freezable workqueues are still busy. %false if freezing
3668 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003669 */
3670bool freeze_workqueues_busy(void)
3671{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003672 bool busy = false;
Tejun Heo24b8a842013-03-12 11:29:58 -07003673 struct workqueue_struct *wq;
3674 struct pool_workqueue *pwq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003675
Tejun Heoe98d5b12013-03-12 11:29:57 -07003676 spin_lock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003677
Tejun Heo6183c002013-03-12 11:29:57 -07003678 WARN_ON_ONCE(!workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003679
Tejun Heo24b8a842013-03-12 11:29:58 -07003680 list_for_each_entry(wq, &workqueues, list) {
3681 if (!(wq->flags & WQ_FREEZABLE))
3682 continue;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003683 /*
3684 * nr_active is monotonically decreasing. It's safe
3685 * to peek without lock.
3686 */
Tejun Heo24b8a842013-03-12 11:29:58 -07003687 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07003688 WARN_ON_ONCE(pwq->nr_active < 0);
Tejun Heo112202d2013-02-13 19:29:12 -08003689 if (pwq->nr_active) {
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003690 busy = true;
3691 goto out_unlock;
3692 }
3693 }
3694 }
3695out_unlock:
Tejun Heoe98d5b12013-03-12 11:29:57 -07003696 spin_unlock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003697 return busy;
3698}
3699
3700/**
3701 * thaw_workqueues - thaw workqueues
3702 *
3703 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo706026c2013-01-24 11:01:34 -08003704 * frozen works are transferred to their respective pool worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003705 *
3706 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08003707 * Grabs and releases workqueue_lock and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003708 */
3709void thaw_workqueues(void)
3710{
Tejun Heo24b8a842013-03-12 11:29:58 -07003711 struct workqueue_struct *wq;
3712 struct pool_workqueue *pwq;
3713 struct worker_pool *pool;
3714 int id;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003715
Tejun Heoe98d5b12013-03-12 11:29:57 -07003716 spin_lock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003717
3718 if (!workqueue_freezing)
3719 goto out_unlock;
3720
Tejun Heo24b8a842013-03-12 11:29:58 -07003721 /* clear FREEZING */
3722 for_each_pool(pool, id) {
3723 spin_lock(&pool->lock);
3724 WARN_ON_ONCE(!(pool->flags & POOL_FREEZING));
3725 pool->flags &= ~POOL_FREEZING;
3726 spin_unlock(&pool->lock);
3727 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003728
Tejun Heo24b8a842013-03-12 11:29:58 -07003729 /* restore max_active and repopulate worklist */
3730 list_for_each_entry(wq, &workqueues, list) {
3731 if (!(wq->flags & WQ_FREEZABLE))
3732 continue;
Tejun Heod565ed62013-01-24 11:01:33 -08003733
Tejun Heo24b8a842013-03-12 11:29:58 -07003734 for_each_pwq(pwq, wq) {
3735 spin_lock(&pwq->pool->lock);
3736 pwq_set_max_active(pwq, wq->saved_max_active);
3737 spin_unlock(&pwq->pool->lock);
Tejun Heod565ed62013-01-24 11:01:33 -08003738 }
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003739 }
3740
Tejun Heo24b8a842013-03-12 11:29:58 -07003741 /* kick workers */
3742 for_each_pool(pool, id) {
3743 spin_lock(&pool->lock);
3744 wake_up_worker(pool);
3745 spin_unlock(&pool->lock);
3746 }
3747
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003748 workqueue_freezing = false;
3749out_unlock:
Tejun Heoe98d5b12013-03-12 11:29:57 -07003750 spin_unlock_irq(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003751}
3752#endif /* CONFIG_FREEZER */
3753
Suresh Siddha6ee05782010-07-30 14:57:37 -07003754static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755{
Tejun Heod84ff052013-03-12 11:29:59 -07003756 int cpu;
Tejun Heoc34056a2010-06-29 10:07:11 +02003757
Tejun Heo7c3eed52013-01-24 11:01:33 -08003758 /* make sure we have enough bits for OFFQ pool ID */
3759 BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) <
Lai Jiangshan6be19582013-02-06 18:04:53 -08003760 WORK_CPU_END * NR_STD_WORKER_POOLS);
Tejun Heob5490072012-08-03 10:30:46 -07003761
Tejun Heoe904e6c2013-03-12 11:29:57 -07003762 WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long));
3763
3764 pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC);
3765
Tejun Heo65758202012-07-17 12:39:26 -07003766 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
Lai Jiangshana5b4e572012-09-18 09:59:23 -07003767 hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003768
Tejun Heo706026c2013-01-24 11:01:34 -08003769 /* initialize CPU pools */
3770 for_each_wq_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07003771 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003772
Tejun Heo38db41d2013-01-24 11:01:34 -08003773 for_each_std_worker_pool(pool, cpu) {
Tejun Heod565ed62013-01-24 11:01:33 -08003774 spin_lock_init(&pool->lock);
Tejun Heoec22ca52013-01-24 11:01:33 -08003775 pool->cpu = cpu;
Tejun Heo24647572013-01-24 11:01:33 -08003776 pool->flags |= POOL_DISASSOCIATED;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003777 INIT_LIST_HEAD(&pool->worklist);
3778 INIT_LIST_HEAD(&pool->idle_list);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08003779 hash_init(pool->busy_hash);
Tejun Heoe22bee72010-06-29 10:07:14 +02003780
Tejun Heo4ce62e92012-07-13 22:16:44 -07003781 init_timer_deferrable(&pool->idle_timer);
3782 pool->idle_timer.function = idle_worker_timeout;
3783 pool->idle_timer.data = (unsigned long)pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003784
Tejun Heo706026c2013-01-24 11:01:34 -08003785 setup_timer(&pool->mayday_timer, pool_mayday_timeout,
Tejun Heo4ce62e92012-07-13 22:16:44 -07003786 (unsigned long)pool);
3787
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07003788 mutex_init(&pool->assoc_mutex);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003789 ida_init(&pool->worker_ida);
Tejun Heo9daf9e62013-01-24 11:01:33 -08003790
3791 /* alloc pool ID */
3792 BUG_ON(worker_pool_assign_id(pool));
Tejun Heo4ce62e92012-07-13 22:16:44 -07003793 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003794 }
3795
Tejun Heoe22bee72010-06-29 10:07:14 +02003796 /* create the initial worker */
Tejun Heo706026c2013-01-24 11:01:34 -08003797 for_each_online_wq_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07003798 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003799
Tejun Heo38db41d2013-01-24 11:01:34 -08003800 for_each_std_worker_pool(pool, cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07003801 struct worker *worker;
3802
Tejun Heo24647572013-01-24 11:01:33 -08003803 if (cpu != WORK_CPU_UNBOUND)
3804 pool->flags &= ~POOL_DISASSOCIATED;
3805
Tejun Heobc2ae0f2012-07-17 12:39:27 -07003806 worker = create_worker(pool);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003807 BUG_ON(!worker);
Tejun Heod565ed62013-01-24 11:01:33 -08003808 spin_lock_irq(&pool->lock);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003809 start_worker(worker);
Tejun Heod565ed62013-01-24 11:01:33 -08003810 spin_unlock_irq(&pool->lock);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003811 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003812 }
3813
Tejun Heod320c032010-06-29 10:07:14 +02003814 system_wq = alloc_workqueue("events", 0, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09003815 system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
Tejun Heod320c032010-06-29 10:07:14 +02003816 system_long_wq = alloc_workqueue("events_long", 0, 0);
Tejun Heof3421792010-07-02 10:03:51 +02003817 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3818 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01003819 system_freezable_wq = alloc_workqueue("events_freezable",
3820 WQ_FREEZABLE, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09003821 BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
Tejun Heoae930e02012-08-20 14:51:23 -07003822 !system_unbound_wq || !system_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07003823 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824}
Suresh Siddha6ee05782010-07-30 14:57:37 -07003825early_initcall(init_workqueues);