blob: 1801c37b28c45f49412ee7027d4850702ab5b100 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tejun Heoc54fce62010-09-10 16:51:36 +02002 * kernel/workqueue.c - generic async execution with shared worker pool
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Tejun Heoc54fce62010-09-10 16:51:36 +02004 * Copyright (C) 2002 Ingo Molnar
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Tejun Heoc54fce62010-09-10 16:51:36 +02006 * Derived from the taskqueue/keventd code by:
7 * David Woodhouse <dwmw2@infradead.org>
8 * Andrew Morton
9 * Kai Petzke <wpp@marie.physik.tu-berlin.de>
10 * Theodore Ts'o <tytso@mit.edu>
Christoph Lameter89ada672005-10-30 15:01:59 -080011 *
Christoph Lametercde53532008-07-04 09:59:22 -070012 * Made to use alloc_percpu by Christoph Lameter.
Tejun Heoc54fce62010-09-10 16:51:36 +020013 *
14 * Copyright (C) 2010 SUSE Linux Products GmbH
15 * Copyright (C) 2010 Tejun Heo <tj@kernel.org>
16 *
17 * This is the generic async execution mechanism. Work items as are
18 * executed in process context. The worker pool is shared and
19 * automatically managed. There is one worker pool for each CPU and
20 * one extra for works which are better served by workers which are
21 * not bound to any specific CPU.
22 *
23 * Please read Documentation/workqueue.txt for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 */
25
Paul Gortmaker9984de12011-05-23 14:51:41 -040026#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/kernel.h>
28#include <linux/sched.h>
29#include <linux/init.h>
30#include <linux/signal.h>
31#include <linux/completion.h>
32#include <linux/workqueue.h>
33#include <linux/slab.h>
34#include <linux/cpu.h>
35#include <linux/notifier.h>
36#include <linux/kthread.h>
James Bottomley1fa44ec2006-02-23 12:43:43 -060037#include <linux/hardirq.h>
Christoph Lameter46934022006-10-11 01:21:26 -070038#include <linux/mempolicy.h>
Rafael J. Wysocki341a5952006-12-06 20:34:49 -080039#include <linux/freezer.h>
Peter Zijlstrad5abe662006-12-06 20:37:26 -080040#include <linux/kallsyms.h>
41#include <linux/debug_locks.h>
Johannes Berg4e6045f2007-10-18 23:39:55 -070042#include <linux/lockdep.h>
Tejun Heoc34056a2010-06-29 10:07:11 +020043#include <linux/idr.h>
Sasha Levin42f85702012-12-17 10:01:23 -050044#include <linux/hashtable.h>
Tejun Heoe22bee72010-06-29 10:07:14 +020045
Tejun Heoea138442013-01-18 14:05:55 -080046#include "workqueue_internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Tejun Heoc8e55f32010-06-29 10:07:12 +020048enum {
Tejun Heobc2ae0f2012-07-17 12:39:27 -070049 /*
Tejun Heo24647572013-01-24 11:01:33 -080050 * worker_pool flags
Tejun Heobc2ae0f2012-07-17 12:39:27 -070051 *
Tejun Heo24647572013-01-24 11:01:33 -080052 * A bound pool is either associated or disassociated with its CPU.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070053 * While associated (!DISASSOCIATED), all workers are bound to the
54 * CPU and none has %WORKER_UNBOUND set and concurrency management
55 * is in effect.
56 *
57 * While DISASSOCIATED, the cpu may be offline and all workers have
58 * %WORKER_UNBOUND set and concurrency management disabled, and may
Tejun Heo24647572013-01-24 11:01:33 -080059 * be executing on any CPU. The pool behaves as an unbound one.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070060 *
61 * Note that DISASSOCIATED can be flipped only while holding
Tejun Heo24647572013-01-24 11:01:33 -080062 * assoc_mutex to avoid changing binding state while
63 * create_worker() is in progress.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070064 */
Tejun Heo11ebea52012-07-12 14:46:37 -070065 POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
Lai Jiangshan552a37e2012-09-10 10:03:33 -070066 POOL_MANAGING_WORKERS = 1 << 1, /* managing workers */
Tejun Heo24647572013-01-24 11:01:33 -080067 POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */
Tejun Heo35b6bb62013-01-24 11:01:33 -080068 POOL_FREEZING = 1 << 3, /* freeze in progress */
Tejun Heodb7bccf2010-06-29 10:07:12 +020069
Tejun Heoc8e55f32010-06-29 10:07:12 +020070 /* worker flags */
71 WORKER_STARTED = 1 << 0, /* started */
72 WORKER_DIE = 1 << 1, /* die die die */
73 WORKER_IDLE = 1 << 2, /* is idle */
Tejun Heoe22bee72010-06-29 10:07:14 +020074 WORKER_PREP = 1 << 3, /* preparing to run works */
Tejun Heofb0e7be2010-06-29 10:07:15 +020075 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
Tejun Heof3421792010-07-02 10:03:51 +020076 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
Tejun Heoe22bee72010-06-29 10:07:14 +020077
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -070078 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_UNBOUND |
Tejun Heo403c8212012-07-17 12:39:27 -070079 WORKER_CPU_INTENSIVE,
Tejun Heodb7bccf2010-06-29 10:07:12 +020080
Tejun Heoe34cdddb2013-01-24 11:01:33 -080081 NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */
Tejun Heo4ce62e92012-07-13 22:16:44 -070082
Tejun Heoc8e55f32010-06-29 10:07:12 +020083 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
Tejun Heodb7bccf2010-06-29 10:07:12 +020084
Tejun Heoe22bee72010-06-29 10:07:14 +020085 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
86 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
87
Tejun Heo3233cdb2011-02-16 18:10:19 +010088 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
89 /* call for help after 10ms
90 (min two ticks) */
Tejun Heoe22bee72010-06-29 10:07:14 +020091 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
92 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heoe22bee72010-06-29 10:07:14 +020093
94 /*
95 * Rescue workers are used only on emergencies and shared by
96 * all cpus. Give -20.
97 */
98 RESCUER_NICE_LEVEL = -20,
Tejun Heo32704762012-07-13 22:16:45 -070099 HIGHPRI_NICE_LEVEL = -20,
Tejun Heoc8e55f32010-06-29 10:07:12 +0200100};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102/*
Tejun Heo4690c4a2010-06-29 10:07:10 +0200103 * Structure fields follow one of the following exclusion rules.
104 *
Tejun Heoe41e7042010-08-24 14:22:47 +0200105 * I: Modifiable by initialization/destruction paths and read-only for
106 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200107 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200108 * P: Preemption protected. Disabling preemption is enough and should
109 * only be modified and accessed from the local cpu.
110 *
Tejun Heod565ed62013-01-24 11:01:33 -0800111 * L: pool->lock protected. Access with pool->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200112 *
Tejun Heod565ed62013-01-24 11:01:33 -0800113 * X: During normal operation, modification requires pool->lock and should
114 * be done only from local cpu. Either disabling preemption on local
115 * cpu or grabbing pool->lock is enough for read access. If
116 * POOL_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200117 *
Tejun Heo73f53c42010-06-29 10:07:11 +0200118 * F: wq->flush_mutex protected.
119 *
Tejun Heo4690c4a2010-06-29 10:07:10 +0200120 * W: workqueue_lock protected.
121 */
122
Tejun Heo2eaebdb2013-01-18 14:05:55 -0800123/* struct worker is defined in workqueue_internal.h */
Tejun Heoc34056a2010-06-29 10:07:11 +0200124
Tejun Heobd7bdd42012-07-12 14:46:37 -0700125struct worker_pool {
Tejun Heod565ed62013-01-24 11:01:33 -0800126 spinlock_t lock; /* the pool lock */
Tejun Heoec22ca52013-01-24 11:01:33 -0800127 unsigned int cpu; /* I: the associated cpu */
Tejun Heo9daf9e62013-01-24 11:01:33 -0800128 int id; /* I: pool ID */
Tejun Heo11ebea52012-07-12 14:46:37 -0700129 unsigned int flags; /* X: flags */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700130
131 struct list_head worklist; /* L: list of pending works */
132 int nr_workers; /* L: total number of workers */
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700133
134 /* nr_idle includes the ones off idle_list for rebinding */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700135 int nr_idle; /* L: currently idle ones */
136
137 struct list_head idle_list; /* X: list of idle workers */
138 struct timer_list idle_timer; /* L: worker idle timeout */
139 struct timer_list mayday_timer; /* L: SOS timer for workers */
140
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800141 /* workers are chained either in busy_hash or idle_list */
142 DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER);
143 /* L: hash of busy workers */
144
Tejun Heo24647572013-01-24 11:01:33 -0800145 struct mutex assoc_mutex; /* protect POOL_DISASSOCIATED */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700146 struct ida worker_ida; /* L: for worker IDs */
Tejun Heoe19e3972013-01-24 11:39:44 -0800147
148 /*
149 * The current concurrency level. As it's likely to be accessed
150 * from other CPUs during try_to_wake_up(), put it in a separate
151 * cacheline.
152 */
153 atomic_t nr_running ____cacheline_aligned_in_smp;
Tejun Heo8b03ae32010-06-29 10:07:12 +0200154} ____cacheline_aligned_in_smp;
155
156/*
Tejun Heo502ca9d2010-06-29 10:07:13 +0200157 * The per-CPU workqueue. The lower WORK_STRUCT_FLAG_BITS of
Tejun Heo0f900042010-06-29 10:07:11 +0200158 * work_struct->data are used for flags and thus cwqs need to be
159 * aligned at two's power of the number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 */
161struct cpu_workqueue_struct {
Tejun Heobd7bdd42012-07-12 14:46:37 -0700162 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200163 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200164 int work_color; /* L: current color */
165 int flush_color; /* L: flushing color */
166 int nr_in_flight[WORK_NR_COLORS];
167 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200168 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200169 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200170 struct list_head delayed_works; /* L: delayed works */
Tejun Heo0f900042010-06-29 10:07:11 +0200171};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200174 * Structure used to wait for workqueue flush.
175 */
176struct wq_flusher {
177 struct list_head list; /* F: list of flushers */
178 int flush_color; /* F: flush color waiting for */
179 struct completion done; /* flush completion */
180};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Tejun Heo73f53c42010-06-29 10:07:11 +0200182/*
Tejun Heof2e005a2010-07-20 15:59:09 +0200183 * All cpumasks are assumed to be always set on UP and thus can't be
184 * used to determine whether there's something to be done.
185 */
186#ifdef CONFIG_SMP
187typedef cpumask_var_t mayday_mask_t;
188#define mayday_test_and_set_cpu(cpu, mask) \
189 cpumask_test_and_set_cpu((cpu), (mask))
190#define mayday_clear_cpu(cpu, mask) cpumask_clear_cpu((cpu), (mask))
191#define for_each_mayday_cpu(cpu, mask) for_each_cpu((cpu), (mask))
Tejun Heo9c375472010-08-31 11:18:34 +0200192#define alloc_mayday_mask(maskp, gfp) zalloc_cpumask_var((maskp), (gfp))
Tejun Heof2e005a2010-07-20 15:59:09 +0200193#define free_mayday_mask(mask) free_cpumask_var((mask))
194#else
195typedef unsigned long mayday_mask_t;
196#define mayday_test_and_set_cpu(cpu, mask) test_and_set_bit(0, &(mask))
197#define mayday_clear_cpu(cpu, mask) clear_bit(0, &(mask))
198#define for_each_mayday_cpu(cpu, mask) if ((cpu) = 0, (mask))
199#define alloc_mayday_mask(maskp, gfp) true
200#define free_mayday_mask(mask) do { } while (0)
201#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203/*
204 * The externally visible workqueue abstraction is an array of
205 * per-CPU workqueues:
206 */
207struct workqueue_struct {
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200208 unsigned int flags; /* W: WQ_* flags */
Tejun Heobdbc5dd2010-07-02 10:03:51 +0200209 union {
210 struct cpu_workqueue_struct __percpu *pcpu;
211 struct cpu_workqueue_struct *single;
212 unsigned long v;
213 } cpu_wq; /* I: cwq's */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200214 struct list_head list; /* W: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200215
216 struct mutex flush_mutex; /* protects wq flushing */
217 int work_color; /* F: current work color */
218 int flush_color; /* F: current flush color */
219 atomic_t nr_cwqs_to_flush; /* flush in progress */
220 struct wq_flusher *first_flusher; /* F: first flusher */
221 struct list_head flusher_queue; /* F: flush waiters */
222 struct list_head flusher_overflow; /* F: flush overflow list */
223
Tejun Heof2e005a2010-07-20 15:59:09 +0200224 mayday_mask_t mayday_mask; /* cpus requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200225 struct worker *rescuer; /* I: rescue worker */
226
Tejun Heo9c5a2ba2011-04-05 18:01:44 +0200227 int nr_drainers; /* W: drain in progress */
Tejun Heodcd989c2010-06-29 10:07:14 +0200228 int saved_max_active; /* W: saved cwq max_active */
Johannes Berg4e6045f2007-10-18 23:39:55 -0700229#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200230 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700231#endif
Tejun Heob196be82012-01-10 15:11:35 -0800232 char name[]; /* I: workqueue name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233};
234
Tejun Heod320c032010-06-29 10:07:14 +0200235struct workqueue_struct *system_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200236EXPORT_SYMBOL_GPL(system_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300237struct workqueue_struct *system_highpri_wq __read_mostly;
Joonsoo Kim1aabe902012-08-15 23:25:39 +0900238EXPORT_SYMBOL_GPL(system_highpri_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300239struct workqueue_struct *system_long_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200240EXPORT_SYMBOL_GPL(system_long_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300241struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200242EXPORT_SYMBOL_GPL(system_unbound_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300243struct workqueue_struct *system_freezable_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100244EXPORT_SYMBOL_GPL(system_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200245
Tejun Heo97bd2342010-10-05 10:41:14 +0200246#define CREATE_TRACE_POINTS
247#include <trace/events/workqueue.h>
248
Tejun Heo38db41d2013-01-24 11:01:34 -0800249#define for_each_std_worker_pool(pool, cpu) \
Tejun Heoa60dc392013-01-24 11:01:34 -0800250 for ((pool) = &std_worker_pools(cpu)[0]; \
251 (pool) < &std_worker_pools(cpu)[NR_STD_WORKER_POOLS]; (pool)++)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700252
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800253#define for_each_busy_worker(worker, i, pos, pool) \
254 hash_for_each(pool->busy_hash, i, pos, worker, hentry)
Tejun Heodb7bccf2010-06-29 10:07:12 +0200255
Tejun Heo706026c2013-01-24 11:01:34 -0800256static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
257 unsigned int sw)
Tejun Heof3421792010-07-02 10:03:51 +0200258{
259 if (cpu < nr_cpu_ids) {
260 if (sw & 1) {
261 cpu = cpumask_next(cpu, mask);
262 if (cpu < nr_cpu_ids)
263 return cpu;
264 }
265 if (sw & 2)
266 return WORK_CPU_UNBOUND;
267 }
Lai Jiangshan6be19582013-02-06 18:04:53 -0800268 return WORK_CPU_END;
Tejun Heof3421792010-07-02 10:03:51 +0200269}
270
Tejun Heo706026c2013-01-24 11:01:34 -0800271static inline int __next_cwq_cpu(int cpu, const struct cpumask *mask,
272 struct workqueue_struct *wq)
Tejun Heof3421792010-07-02 10:03:51 +0200273{
Tejun Heo706026c2013-01-24 11:01:34 -0800274 return __next_wq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2);
Tejun Heof3421792010-07-02 10:03:51 +0200275}
276
Tejun Heo09884952010-08-01 11:50:12 +0200277/*
278 * CPU iterators
279 *
Tejun Heo706026c2013-01-24 11:01:34 -0800280 * An extra cpu number is defined using an invalid cpu number
Tejun Heo09884952010-08-01 11:50:12 +0200281 * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any
Tejun Heo706026c2013-01-24 11:01:34 -0800282 * specific CPU. The following iterators are similar to for_each_*_cpu()
283 * iterators but also considers the unbound CPU.
Tejun Heo09884952010-08-01 11:50:12 +0200284 *
Tejun Heo706026c2013-01-24 11:01:34 -0800285 * for_each_wq_cpu() : possible CPUs + WORK_CPU_UNBOUND
286 * for_each_online_wq_cpu() : online CPUs + WORK_CPU_UNBOUND
Tejun Heo09884952010-08-01 11:50:12 +0200287 * for_each_cwq_cpu() : possible CPUs for bound workqueues,
288 * WORK_CPU_UNBOUND for unbound workqueues
289 */
Tejun Heo706026c2013-01-24 11:01:34 -0800290#define for_each_wq_cpu(cpu) \
291 for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, 3); \
Lai Jiangshan6be19582013-02-06 18:04:53 -0800292 (cpu) < WORK_CPU_END; \
Tejun Heo706026c2013-01-24 11:01:34 -0800293 (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, 3))
Tejun Heof3421792010-07-02 10:03:51 +0200294
Tejun Heo706026c2013-01-24 11:01:34 -0800295#define for_each_online_wq_cpu(cpu) \
296 for ((cpu) = __next_wq_cpu(-1, cpu_online_mask, 3); \
Lai Jiangshan6be19582013-02-06 18:04:53 -0800297 (cpu) < WORK_CPU_END; \
Tejun Heo706026c2013-01-24 11:01:34 -0800298 (cpu) = __next_wq_cpu((cpu), cpu_online_mask, 3))
Tejun Heof3421792010-07-02 10:03:51 +0200299
300#define for_each_cwq_cpu(cpu, wq) \
Tejun Heo706026c2013-01-24 11:01:34 -0800301 for ((cpu) = __next_cwq_cpu(-1, cpu_possible_mask, (wq)); \
Lai Jiangshan6be19582013-02-06 18:04:53 -0800302 (cpu) < WORK_CPU_END; \
Tejun Heo706026c2013-01-24 11:01:34 -0800303 (cpu) = __next_cwq_cpu((cpu), cpu_possible_mask, (wq)))
Tejun Heof3421792010-07-02 10:03:51 +0200304
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900305#ifdef CONFIG_DEBUG_OBJECTS_WORK
306
307static struct debug_obj_descr work_debug_descr;
308
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100309static void *work_debug_hint(void *addr)
310{
311 return ((struct work_struct *) addr)->func;
312}
313
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900314/*
315 * fixup_init is called when:
316 * - an active object is initialized
317 */
318static int work_fixup_init(void *addr, enum debug_obj_state state)
319{
320 struct work_struct *work = addr;
321
322 switch (state) {
323 case ODEBUG_STATE_ACTIVE:
324 cancel_work_sync(work);
325 debug_object_init(work, &work_debug_descr);
326 return 1;
327 default:
328 return 0;
329 }
330}
331
332/*
333 * fixup_activate is called when:
334 * - an active object is activated
335 * - an unknown object is activated (might be a statically initialized object)
336 */
337static int work_fixup_activate(void *addr, enum debug_obj_state state)
338{
339 struct work_struct *work = addr;
340
341 switch (state) {
342
343 case ODEBUG_STATE_NOTAVAILABLE:
344 /*
345 * This is not really a fixup. The work struct was
346 * statically initialized. We just make sure that it
347 * is tracked in the object tracker.
348 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200349 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900350 debug_object_init(work, &work_debug_descr);
351 debug_object_activate(work, &work_debug_descr);
352 return 0;
353 }
354 WARN_ON_ONCE(1);
355 return 0;
356
357 case ODEBUG_STATE_ACTIVE:
358 WARN_ON(1);
359
360 default:
361 return 0;
362 }
363}
364
365/*
366 * fixup_free is called when:
367 * - an active object is freed
368 */
369static int work_fixup_free(void *addr, enum debug_obj_state state)
370{
371 struct work_struct *work = addr;
372
373 switch (state) {
374 case ODEBUG_STATE_ACTIVE:
375 cancel_work_sync(work);
376 debug_object_free(work, &work_debug_descr);
377 return 1;
378 default:
379 return 0;
380 }
381}
382
383static struct debug_obj_descr work_debug_descr = {
384 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100385 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900386 .fixup_init = work_fixup_init,
387 .fixup_activate = work_fixup_activate,
388 .fixup_free = work_fixup_free,
389};
390
391static inline void debug_work_activate(struct work_struct *work)
392{
393 debug_object_activate(work, &work_debug_descr);
394}
395
396static inline void debug_work_deactivate(struct work_struct *work)
397{
398 debug_object_deactivate(work, &work_debug_descr);
399}
400
401void __init_work(struct work_struct *work, int onstack)
402{
403 if (onstack)
404 debug_object_init_on_stack(work, &work_debug_descr);
405 else
406 debug_object_init(work, &work_debug_descr);
407}
408EXPORT_SYMBOL_GPL(__init_work);
409
410void destroy_work_on_stack(struct work_struct *work)
411{
412 debug_object_free(work, &work_debug_descr);
413}
414EXPORT_SYMBOL_GPL(destroy_work_on_stack);
415
416#else
417static inline void debug_work_activate(struct work_struct *work) { }
418static inline void debug_work_deactivate(struct work_struct *work) { }
419#endif
420
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100421/* Serializes the accesses to the list of workqueues. */
422static DEFINE_SPINLOCK(workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423static LIST_HEAD(workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200424static bool workqueue_freezing; /* W: have wqs started freezing? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Oleg Nesterov14441962007-05-23 13:57:57 -0700426/*
Tejun Heoe19e3972013-01-24 11:39:44 -0800427 * The CPU and unbound standard worker pools. The unbound ones have
428 * POOL_DISASSOCIATED set, and their workers have WORKER_UNBOUND set.
Oleg Nesterov14441962007-05-23 13:57:57 -0700429 */
Tejun Heoe19e3972013-01-24 11:39:44 -0800430static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS],
431 cpu_std_worker_pools);
Tejun Heoa60dc392013-01-24 11:01:34 -0800432static struct worker_pool unbound_std_worker_pools[NR_STD_WORKER_POOLS];
Tejun Heof3421792010-07-02 10:03:51 +0200433
Tejun Heo9daf9e62013-01-24 11:01:33 -0800434/* idr of all pools */
435static DEFINE_MUTEX(worker_pool_idr_mutex);
436static DEFINE_IDR(worker_pool_idr);
437
Tejun Heoc34056a2010-06-29 10:07:11 +0200438static int worker_thread(void *__worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Tejun Heoa60dc392013-01-24 11:01:34 -0800440static struct worker_pool *std_worker_pools(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Tejun Heof3421792010-07-02 10:03:51 +0200442 if (cpu != WORK_CPU_UNBOUND)
Tejun Heoa60dc392013-01-24 11:01:34 -0800443 return per_cpu(cpu_std_worker_pools, cpu);
Tejun Heof3421792010-07-02 10:03:51 +0200444 else
Tejun Heoa60dc392013-01-24 11:01:34 -0800445 return unbound_std_worker_pools;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
Tejun Heo4e8f0a62013-01-24 11:01:34 -0800448static int std_worker_pool_pri(struct worker_pool *pool)
449{
Tejun Heoa60dc392013-01-24 11:01:34 -0800450 return pool - std_worker_pools(pool->cpu);
Tejun Heo4e8f0a62013-01-24 11:01:34 -0800451}
452
Tejun Heo9daf9e62013-01-24 11:01:33 -0800453/* allocate ID and assign it to @pool */
454static int worker_pool_assign_id(struct worker_pool *pool)
455{
456 int ret;
457
458 mutex_lock(&worker_pool_idr_mutex);
459 idr_pre_get(&worker_pool_idr, GFP_KERNEL);
460 ret = idr_get_new(&worker_pool_idr, pool, &pool->id);
461 mutex_unlock(&worker_pool_idr_mutex);
462
463 return ret;
464}
465
Tejun Heo7c3eed52013-01-24 11:01:33 -0800466/*
467 * Lookup worker_pool by id. The idr currently is built during boot and
468 * never modified. Don't worry about locking for now.
469 */
470static struct worker_pool *worker_pool_by_id(int pool_id)
471{
472 return idr_find(&worker_pool_idr, pool_id);
473}
474
Tejun Heod565ed62013-01-24 11:01:33 -0800475static struct worker_pool *get_std_worker_pool(int cpu, bool highpri)
476{
Tejun Heoa60dc392013-01-24 11:01:34 -0800477 struct worker_pool *pools = std_worker_pools(cpu);
Tejun Heod565ed62013-01-24 11:01:33 -0800478
Tejun Heoa60dc392013-01-24 11:01:34 -0800479 return &pools[highpri];
Tejun Heod565ed62013-01-24 11:01:33 -0800480}
481
Tejun Heo4690c4a2010-06-29 10:07:10 +0200482static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
483 struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700484{
Tejun Heof3421792010-07-02 10:03:51 +0200485 if (!(wq->flags & WQ_UNBOUND)) {
Lai Jiangshane06ffa12012-03-09 18:03:20 +0800486 if (likely(cpu < nr_cpu_ids))
Tejun Heof3421792010-07-02 10:03:51 +0200487 return per_cpu_ptr(wq->cpu_wq.pcpu, cpu);
Tejun Heof3421792010-07-02 10:03:51 +0200488 } else if (likely(cpu == WORK_CPU_UNBOUND))
489 return wq->cpu_wq.single;
490 return NULL;
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700491}
492
Tejun Heo73f53c42010-06-29 10:07:11 +0200493static unsigned int work_color_to_flags(int color)
494{
495 return color << WORK_STRUCT_COLOR_SHIFT;
496}
497
498static int get_work_color(struct work_struct *work)
499{
500 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
501 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
502}
503
504static int work_next_color(int color)
505{
506 return (color + 1) % WORK_NR_COLORS;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700507}
508
David Howells4594bf12006-12-07 11:33:26 +0000509/*
Tejun Heob5490072012-08-03 10:30:46 -0700510 * While queued, %WORK_STRUCT_CWQ is set and non flag bits of a work's data
511 * contain the pointer to the queued cwq. Once execution starts, the flag
Tejun Heo7c3eed52013-01-24 11:01:33 -0800512 * is cleared and the high bits contain OFFQ flags and pool ID.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200513 *
Tejun Heo7c3eed52013-01-24 11:01:33 -0800514 * set_work_cwq(), set_work_pool_and_clear_pending(), mark_work_canceling()
515 * and clear_work_data() can be used to set the cwq, pool or clear
Tejun Heobbb68df2012-08-03 10:30:46 -0700516 * work->data. These functions should only be called while the work is
517 * owned - ie. while the PENDING bit is set.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200518 *
Tejun Heo7c3eed52013-01-24 11:01:33 -0800519 * get_work_pool() and get_work_cwq() can be used to obtain the pool or cwq
520 * corresponding to a work. Pool is available once the work has been
521 * queued anywhere after initialization until it is sync canceled. cwq is
522 * available only while the work item is queued.
Tejun Heobbb68df2012-08-03 10:30:46 -0700523 *
524 * %WORK_OFFQ_CANCELING is used to mark a work item which is being
525 * canceled. While being canceled, a work item may have its PENDING set
526 * but stay off timer and worklist for arbitrarily long and nobody should
527 * try to steal the PENDING bit.
David Howells4594bf12006-12-07 11:33:26 +0000528 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200529static inline void set_work_data(struct work_struct *work, unsigned long data,
530 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000531{
David Howells4594bf12006-12-07 11:33:26 +0000532 BUG_ON(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200533 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000534}
David Howells365970a2006-11-22 14:54:49 +0000535
Tejun Heo7a22ad72010-06-29 10:07:13 +0200536static void set_work_cwq(struct work_struct *work,
537 struct cpu_workqueue_struct *cwq,
538 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200539{
Tejun Heo7a22ad72010-06-29 10:07:13 +0200540 set_work_data(work, (unsigned long)cwq,
Tejun Heoe1201532010-07-22 14:14:25 +0200541 WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200542}
543
Lai Jiangshan4468a002013-02-06 18:04:53 -0800544static void set_work_pool_and_keep_pending(struct work_struct *work,
545 int pool_id)
546{
547 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT,
548 WORK_STRUCT_PENDING);
549}
550
Tejun Heo7c3eed52013-01-24 11:01:33 -0800551static void set_work_pool_and_clear_pending(struct work_struct *work,
552 int pool_id)
David Howells365970a2006-11-22 14:54:49 +0000553{
Tejun Heo23657bb2012-08-13 17:08:19 -0700554 /*
555 * The following wmb is paired with the implied mb in
556 * test_and_set_bit(PENDING) and ensures all updates to @work made
557 * here are visible to and precede any updates by the next PENDING
558 * owner.
559 */
560 smp_wmb();
Tejun Heo7c3eed52013-01-24 11:01:33 -0800561 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200562}
563
564static void clear_work_data(struct work_struct *work)
565{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800566 smp_wmb(); /* see set_work_pool_and_clear_pending() */
567 set_work_data(work, WORK_STRUCT_NO_POOL, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200568}
569
Tejun Heo7a22ad72010-06-29 10:07:13 +0200570static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work)
571{
Tejun Heoe1201532010-07-22 14:14:25 +0200572 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200573
Tejun Heoe1201532010-07-22 14:14:25 +0200574 if (data & WORK_STRUCT_CWQ)
575 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
576 else
577 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200578}
579
Tejun Heo7c3eed52013-01-24 11:01:33 -0800580/**
581 * get_work_pool - return the worker_pool a given work was associated with
582 * @work: the work item of interest
583 *
584 * Return the worker_pool @work was last associated with. %NULL if none.
585 */
586static struct worker_pool *get_work_pool(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200587{
Tejun Heoe1201532010-07-22 14:14:25 +0200588 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800589 struct worker_pool *pool;
590 int pool_id;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200591
Tejun Heoe1201532010-07-22 14:14:25 +0200592 if (data & WORK_STRUCT_CWQ)
593 return ((struct cpu_workqueue_struct *)
Tejun Heo7c3eed52013-01-24 11:01:33 -0800594 (data & WORK_STRUCT_WQ_DATA_MASK))->pool;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200595
Tejun Heo7c3eed52013-01-24 11:01:33 -0800596 pool_id = data >> WORK_OFFQ_POOL_SHIFT;
597 if (pool_id == WORK_OFFQ_POOL_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200598 return NULL;
599
Tejun Heo7c3eed52013-01-24 11:01:33 -0800600 pool = worker_pool_by_id(pool_id);
601 WARN_ON_ONCE(!pool);
602 return pool;
603}
604
605/**
606 * get_work_pool_id - return the worker pool ID a given work is associated with
607 * @work: the work item of interest
608 *
609 * Return the worker_pool ID @work was last associated with.
610 * %WORK_OFFQ_POOL_NONE if none.
611 */
612static int get_work_pool_id(struct work_struct *work)
613{
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800614 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800615
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800616 if (data & WORK_STRUCT_CWQ)
617 return ((struct cpu_workqueue_struct *)
618 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id;
619
620 return data >> WORK_OFFQ_POOL_SHIFT;
Tejun Heo7c3eed52013-01-24 11:01:33 -0800621}
622
Tejun Heobbb68df2012-08-03 10:30:46 -0700623static void mark_work_canceling(struct work_struct *work)
624{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800625 unsigned long pool_id = get_work_pool_id(work);
Tejun Heobbb68df2012-08-03 10:30:46 -0700626
Tejun Heo7c3eed52013-01-24 11:01:33 -0800627 pool_id <<= WORK_OFFQ_POOL_SHIFT;
628 set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700629}
630
631static bool work_is_canceling(struct work_struct *work)
632{
633 unsigned long data = atomic_long_read(&work->data);
634
635 return !(data & WORK_STRUCT_CWQ) && (data & WORK_OFFQ_CANCELING);
636}
637
David Howells365970a2006-11-22 14:54:49 +0000638/*
Tejun Heo32704762012-07-13 22:16:45 -0700639 * Policy functions. These define the policies on how the global worker
640 * pools are managed. Unless noted otherwise, these functions assume that
Tejun Heod565ed62013-01-24 11:01:33 -0800641 * they're being called with pool->lock held.
David Howells365970a2006-11-22 14:54:49 +0000642 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200643
Tejun Heo63d95a92012-07-12 14:46:37 -0700644static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000645{
Tejun Heoe19e3972013-01-24 11:39:44 -0800646 return !atomic_read(&pool->nr_running);
David Howells365970a2006-11-22 14:54:49 +0000647}
648
Tejun Heoe22bee72010-06-29 10:07:14 +0200649/*
650 * Need to wake up a worker? Called from anything but currently
651 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700652 *
653 * Note that, because unbound workers never contribute to nr_running, this
Tejun Heo706026c2013-01-24 11:01:34 -0800654 * function will always return %true for unbound pools as long as the
Tejun Heo974271c2012-07-12 14:46:37 -0700655 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200656 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700657static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000658{
Tejun Heo63d95a92012-07-12 14:46:37 -0700659 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000660}
661
Tejun Heoe22bee72010-06-29 10:07:14 +0200662/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700663static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200664{
Tejun Heo63d95a92012-07-12 14:46:37 -0700665 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200666}
667
668/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700669static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200670{
Tejun Heoe19e3972013-01-24 11:39:44 -0800671 return !list_empty(&pool->worklist) &&
672 atomic_read(&pool->nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200673}
674
675/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700676static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200677{
Tejun Heo63d95a92012-07-12 14:46:37 -0700678 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200679}
680
681/* Do I need to be the manager? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700682static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200683{
Tejun Heo63d95a92012-07-12 14:46:37 -0700684 return need_to_create_worker(pool) ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700685 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200686}
687
688/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700689static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200690{
Lai Jiangshan552a37e2012-09-10 10:03:33 -0700691 bool managing = pool->flags & POOL_MANAGING_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -0700692 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
693 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200694
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700695 /*
696 * nr_idle and idle_list may disagree if idle rebinding is in
697 * progress. Never return %true if idle_list is empty.
698 */
699 if (list_empty(&pool->idle_list))
700 return false;
701
Tejun Heoe22bee72010-06-29 10:07:14 +0200702 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
703}
704
705/*
706 * Wake up functions.
707 */
708
Tejun Heo7e116292010-06-29 10:07:13 +0200709/* Return the first worker. Safe with preemption disabled */
Tejun Heo63d95a92012-07-12 14:46:37 -0700710static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200711{
Tejun Heo63d95a92012-07-12 14:46:37 -0700712 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200713 return NULL;
714
Tejun Heo63d95a92012-07-12 14:46:37 -0700715 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200716}
717
718/**
719 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700720 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200721 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700722 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200723 *
724 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800725 * spin_lock_irq(pool->lock).
Tejun Heo7e116292010-06-29 10:07:13 +0200726 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700727static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200728{
Tejun Heo63d95a92012-07-12 14:46:37 -0700729 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200730
731 if (likely(worker))
732 wake_up_process(worker->task);
733}
734
Tejun Heo4690c4a2010-06-29 10:07:10 +0200735/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200736 * wq_worker_waking_up - a worker is waking up
737 * @task: task waking up
738 * @cpu: CPU @task is waking up to
739 *
740 * This function is called during try_to_wake_up() when a worker is
741 * being awoken.
742 *
743 * CONTEXT:
744 * spin_lock_irq(rq->lock)
745 */
746void wq_worker_waking_up(struct task_struct *task, unsigned int cpu)
747{
748 struct worker *worker = kthread_data(task);
749
Joonsoo Kim36576002012-10-26 23:03:49 +0900750 if (!(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoec22ca52013-01-24 11:01:33 -0800751 WARN_ON_ONCE(worker->pool->cpu != cpu);
Tejun Heoe19e3972013-01-24 11:39:44 -0800752 atomic_inc(&worker->pool->nr_running);
Joonsoo Kim36576002012-10-26 23:03:49 +0900753 }
Tejun Heoe22bee72010-06-29 10:07:14 +0200754}
755
756/**
757 * wq_worker_sleeping - a worker is going to sleep
758 * @task: task going to sleep
759 * @cpu: CPU in question, must be the current CPU number
760 *
761 * This function is called during schedule() when a busy worker is
762 * going to sleep. Worker on the same cpu can be woken up by
763 * returning pointer to its task.
764 *
765 * CONTEXT:
766 * spin_lock_irq(rq->lock)
767 *
768 * RETURNS:
769 * Worker task on @cpu to wake up, %NULL if none.
770 */
771struct task_struct *wq_worker_sleeping(struct task_struct *task,
772 unsigned int cpu)
773{
774 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heo111c2252013-01-17 17:16:24 -0800775 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200776
Tejun Heo111c2252013-01-17 17:16:24 -0800777 /*
778 * Rescuers, which may not have all the fields set up like normal
779 * workers, also reach here, let's not access anything before
780 * checking NOT_RUNNING.
781 */
Steven Rostedt2d646722010-12-03 23:12:33 -0500782 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200783 return NULL;
784
Tejun Heo111c2252013-01-17 17:16:24 -0800785 pool = worker->pool;
Tejun Heo111c2252013-01-17 17:16:24 -0800786
Tejun Heoe22bee72010-06-29 10:07:14 +0200787 /* this can only happen on the local cpu */
788 BUG_ON(cpu != raw_smp_processor_id());
789
790 /*
791 * The counterpart of the following dec_and_test, implied mb,
792 * worklist not empty test sequence is in insert_work().
793 * Please read comment there.
794 *
Tejun Heo628c78e2012-07-17 12:39:27 -0700795 * NOT_RUNNING is clear. This means that we're bound to and
796 * running on the local cpu w/ rq lock held and preemption
797 * disabled, which in turn means that none else could be
Tejun Heod565ed62013-01-24 11:01:33 -0800798 * manipulating idle_list, so dereferencing idle_list without pool
Tejun Heo628c78e2012-07-17 12:39:27 -0700799 * lock is safe.
Tejun Heoe22bee72010-06-29 10:07:14 +0200800 */
Tejun Heoe19e3972013-01-24 11:39:44 -0800801 if (atomic_dec_and_test(&pool->nr_running) &&
802 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700803 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200804 return to_wakeup ? to_wakeup->task : NULL;
805}
806
807/**
808 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200809 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200810 * @flags: flags to set
811 * @wakeup: wakeup an idle worker if necessary
812 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200813 * Set @flags in @worker->flags and adjust nr_running accordingly. If
814 * nr_running becomes zero and @wakeup is %true, an idle worker is
815 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200816 *
Tejun Heocb444762010-07-02 10:03:50 +0200817 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800818 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200819 */
820static inline void worker_set_flags(struct worker *worker, unsigned int flags,
821 bool wakeup)
822{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700823 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200824
Tejun Heocb444762010-07-02 10:03:50 +0200825 WARN_ON_ONCE(worker->task != current);
826
Tejun Heoe22bee72010-06-29 10:07:14 +0200827 /*
828 * If transitioning into NOT_RUNNING, adjust nr_running and
829 * wake up an idle worker as necessary if requested by
830 * @wakeup.
831 */
832 if ((flags & WORKER_NOT_RUNNING) &&
833 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoe22bee72010-06-29 10:07:14 +0200834 if (wakeup) {
Tejun Heoe19e3972013-01-24 11:39:44 -0800835 if (atomic_dec_and_test(&pool->nr_running) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -0700836 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700837 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200838 } else
Tejun Heoe19e3972013-01-24 11:39:44 -0800839 atomic_dec(&pool->nr_running);
Tejun Heoe22bee72010-06-29 10:07:14 +0200840 }
841
Tejun Heod302f012010-06-29 10:07:13 +0200842 worker->flags |= flags;
843}
844
845/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200846 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200847 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200848 * @flags: flags to clear
849 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200850 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200851 *
Tejun Heocb444762010-07-02 10:03:50 +0200852 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800853 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200854 */
855static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
856{
Tejun Heo63d95a92012-07-12 14:46:37 -0700857 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200858 unsigned int oflags = worker->flags;
859
Tejun Heocb444762010-07-02 10:03:50 +0200860 WARN_ON_ONCE(worker->task != current);
861
Tejun Heod302f012010-06-29 10:07:13 +0200862 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200863
Tejun Heo42c025f2011-01-11 15:58:49 +0100864 /*
865 * If transitioning out of NOT_RUNNING, increment nr_running. Note
866 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
867 * of multiple flags, not a single flag.
868 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200869 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
870 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heoe19e3972013-01-24 11:39:44 -0800871 atomic_inc(&pool->nr_running);
Tejun Heod302f012010-06-29 10:07:13 +0200872}
873
874/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200875 * find_worker_executing_work - find worker which is executing a work
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800876 * @pool: pool of interest
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200877 * @work: work to find worker for
878 *
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800879 * Find a worker which is executing @work on @pool by searching
880 * @pool->busy_hash which is keyed by the address of @work. For a worker
Tejun Heoa2c1c572012-12-18 10:35:02 -0800881 * to match, its current execution should match the address of @work and
882 * its work function. This is to avoid unwanted dependency between
883 * unrelated work executions through a work item being recycled while still
884 * being executed.
885 *
886 * This is a bit tricky. A work item may be freed once its execution
887 * starts and nothing prevents the freed area from being recycled for
888 * another work item. If the same work item address ends up being reused
889 * before the original execution finishes, workqueue will identify the
890 * recycled work item as currently executing and make it wait until the
891 * current execution finishes, introducing an unwanted dependency.
892 *
893 * This function checks the work item address, work function and workqueue
894 * to avoid false positives. Note that this isn't complete as one may
895 * construct a work function which can introduce dependency onto itself
896 * through a recycled work item. Well, if somebody wants to shoot oneself
897 * in the foot that badly, there's only so much we can do, and if such
898 * deadlock actually occurs, it should be easy to locate the culprit work
899 * function.
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200900 *
901 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800902 * spin_lock_irq(pool->lock).
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200903 *
904 * RETURNS:
905 * Pointer to worker which is executing @work if found, NULL
906 * otherwise.
907 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800908static struct worker *find_worker_executing_work(struct worker_pool *pool,
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200909 struct work_struct *work)
910{
Sasha Levin42f85702012-12-17 10:01:23 -0500911 struct worker *worker;
912 struct hlist_node *tmp;
913
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800914 hash_for_each_possible(pool->busy_hash, worker, tmp, hentry,
Tejun Heoa2c1c572012-12-18 10:35:02 -0800915 (unsigned long)work)
916 if (worker->current_work == work &&
917 worker->current_func == work->func)
Sasha Levin42f85702012-12-17 10:01:23 -0500918 return worker;
919
920 return NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200921}
922
923/**
Tejun Heobf4ede02012-08-03 10:30:46 -0700924 * move_linked_works - move linked works to a list
925 * @work: start of series of works to be scheduled
926 * @head: target list to append @work to
927 * @nextp: out paramter for nested worklist walking
928 *
929 * Schedule linked works starting from @work to @head. Work series to
930 * be scheduled starts at @work and includes any consecutive work with
931 * WORK_STRUCT_LINKED set in its predecessor.
932 *
933 * If @nextp is not NULL, it's updated to point to the next work of
934 * the last scheduled work. This allows move_linked_works() to be
935 * nested inside outer list_for_each_entry_safe().
936 *
937 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800938 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -0700939 */
940static void move_linked_works(struct work_struct *work, struct list_head *head,
941 struct work_struct **nextp)
942{
943 struct work_struct *n;
944
945 /*
946 * Linked worklist will always end before the end of the list,
947 * use NULL for list head.
948 */
949 list_for_each_entry_safe_from(work, n, NULL, entry) {
950 list_move_tail(&work->entry, head);
951 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
952 break;
953 }
954
955 /*
956 * If we're already inside safe list traversal and have moved
957 * multiple works to the scheduled queue, the next position
958 * needs to be updated.
959 */
960 if (nextp)
961 *nextp = n;
962}
963
Lai Jiangshan3aa62492012-09-18 10:40:00 -0700964static void cwq_activate_delayed_work(struct work_struct *work)
Tejun Heobf4ede02012-08-03 10:30:46 -0700965{
Lai Jiangshan3aa62492012-09-18 10:40:00 -0700966 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heobf4ede02012-08-03 10:30:46 -0700967
968 trace_workqueue_activate_work(work);
969 move_linked_works(work, &cwq->pool->worklist, NULL);
970 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
971 cwq->nr_active++;
972}
973
Lai Jiangshan3aa62492012-09-18 10:40:00 -0700974static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
975{
976 struct work_struct *work = list_first_entry(&cwq->delayed_works,
977 struct work_struct, entry);
978
979 cwq_activate_delayed_work(work);
980}
981
Tejun Heobf4ede02012-08-03 10:30:46 -0700982/**
983 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
984 * @cwq: cwq of interest
985 * @color: color of work which left the queue
Tejun Heobf4ede02012-08-03 10:30:46 -0700986 *
987 * A work either has completed or is removed from pending queue,
988 * decrement nr_in_flight of its cwq and handle workqueue flushing.
989 *
990 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800991 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -0700992 */
Lai Jiangshanb3f9f402012-09-18 10:40:00 -0700993static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color)
Tejun Heobf4ede02012-08-03 10:30:46 -0700994{
995 /* ignore uncolored works */
996 if (color == WORK_NO_COLOR)
997 return;
998
999 cwq->nr_in_flight[color]--;
1000
Lai Jiangshanb3f9f402012-09-18 10:40:00 -07001001 cwq->nr_active--;
1002 if (!list_empty(&cwq->delayed_works)) {
1003 /* one down, submit a delayed one */
1004 if (cwq->nr_active < cwq->max_active)
1005 cwq_activate_first_delayed(cwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001006 }
1007
1008 /* is flush in progress and are we at the flushing tip? */
1009 if (likely(cwq->flush_color != color))
1010 return;
1011
1012 /* are there still in-flight works? */
1013 if (cwq->nr_in_flight[color])
1014 return;
1015
1016 /* this cwq is done, clear flush_color */
1017 cwq->flush_color = -1;
1018
1019 /*
1020 * If this was the last cwq, wake up the first flusher. It
1021 * will handle the rest.
1022 */
1023 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
1024 complete(&cwq->wq->first_flusher->done);
1025}
1026
Tejun Heo36e227d2012-08-03 10:30:46 -07001027/**
Tejun Heobbb68df2012-08-03 10:30:46 -07001028 * try_to_grab_pending - steal work item from worklist and disable irq
Tejun Heo36e227d2012-08-03 10:30:46 -07001029 * @work: work item to steal
1030 * @is_dwork: @work is a delayed_work
Tejun Heobbb68df2012-08-03 10:30:46 -07001031 * @flags: place to store irq state
Tejun Heo36e227d2012-08-03 10:30:46 -07001032 *
1033 * Try to grab PENDING bit of @work. This function can handle @work in any
1034 * stable state - idle, on timer or on worklist. Return values are
1035 *
1036 * 1 if @work was pending and we successfully stole PENDING
1037 * 0 if @work was idle and we claimed PENDING
1038 * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry
Tejun Heobbb68df2012-08-03 10:30:46 -07001039 * -ENOENT if someone else is canceling @work, this state may persist
1040 * for arbitrarily long
Tejun Heo36e227d2012-08-03 10:30:46 -07001041 *
Tejun Heobbb68df2012-08-03 10:30:46 -07001042 * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001043 * interrupted while holding PENDING and @work off queue, irq must be
1044 * disabled on entry. This, combined with delayed_work->timer being
1045 * irqsafe, ensures that we return -EAGAIN for finite short period of time.
Tejun Heobbb68df2012-08-03 10:30:46 -07001046 *
1047 * On successful return, >= 0, irq is disabled and the caller is
1048 * responsible for releasing it using local_irq_restore(*@flags).
1049 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001050 * This function is safe to call from any context including IRQ handler.
Tejun Heobf4ede02012-08-03 10:30:46 -07001051 */
Tejun Heobbb68df2012-08-03 10:30:46 -07001052static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
1053 unsigned long *flags)
Tejun Heobf4ede02012-08-03 10:30:46 -07001054{
Tejun Heod565ed62013-01-24 11:01:33 -08001055 struct worker_pool *pool;
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001056 struct cpu_workqueue_struct *cwq;
Tejun Heobf4ede02012-08-03 10:30:46 -07001057
Tejun Heobbb68df2012-08-03 10:30:46 -07001058 local_irq_save(*flags);
1059
Tejun Heo36e227d2012-08-03 10:30:46 -07001060 /* try to steal the timer if it exists */
1061 if (is_dwork) {
1062 struct delayed_work *dwork = to_delayed_work(work);
1063
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001064 /*
1065 * dwork->timer is irqsafe. If del_timer() fails, it's
1066 * guaranteed that the timer is not queued anywhere and not
1067 * running on the local CPU.
1068 */
Tejun Heo36e227d2012-08-03 10:30:46 -07001069 if (likely(del_timer(&dwork->timer)))
1070 return 1;
1071 }
1072
1073 /* try to claim PENDING the normal way */
Tejun Heobf4ede02012-08-03 10:30:46 -07001074 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1075 return 0;
1076
1077 /*
1078 * The queueing is in progress, or it is already queued. Try to
1079 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1080 */
Tejun Heod565ed62013-01-24 11:01:33 -08001081 pool = get_work_pool(work);
1082 if (!pool)
Tejun Heobbb68df2012-08-03 10:30:46 -07001083 goto fail;
Tejun Heobf4ede02012-08-03 10:30:46 -07001084
Tejun Heod565ed62013-01-24 11:01:33 -08001085 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001086 /*
1087 * work->data is guaranteed to point to cwq only while the work
1088 * item is queued on cwq->wq, and both updating work->data to point
1089 * to cwq on queueing and to pool on dequeueing are done under
1090 * cwq->pool->lock. This in turn guarantees that, if work->data
1091 * points to cwq which is associated with a locked pool, the work
1092 * item is currently queued on that pool.
1093 */
1094 cwq = get_work_cwq(work);
Tejun Heo16062832013-02-06 18:04:53 -08001095 if (cwq && cwq->pool == pool) {
1096 debug_work_deactivate(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001097
Tejun Heo16062832013-02-06 18:04:53 -08001098 /*
1099 * A delayed work item cannot be grabbed directly because
1100 * it might have linked NO_COLOR work items which, if left
1101 * on the delayed_list, will confuse cwq->nr_active
1102 * management later on and cause stall. Make sure the work
1103 * item is activated before grabbing.
1104 */
1105 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
1106 cwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001107
Tejun Heo16062832013-02-06 18:04:53 -08001108 list_del_init(&work->entry);
1109 cwq_dec_nr_in_flight(get_work_cwq(work), get_work_color(work));
Tejun Heo36e227d2012-08-03 10:30:46 -07001110
Tejun Heo16062832013-02-06 18:04:53 -08001111 /* work->data points to cwq iff queued, point to pool */
1112 set_work_pool_and_keep_pending(work, pool->id);
Lai Jiangshan4468a002013-02-06 18:04:53 -08001113
Tejun Heo16062832013-02-06 18:04:53 -08001114 spin_unlock(&pool->lock);
1115 return 1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001116 }
Tejun Heod565ed62013-01-24 11:01:33 -08001117 spin_unlock(&pool->lock);
Tejun Heobbb68df2012-08-03 10:30:46 -07001118fail:
1119 local_irq_restore(*flags);
1120 if (work_is_canceling(work))
1121 return -ENOENT;
1122 cpu_relax();
Tejun Heo36e227d2012-08-03 10:30:46 -07001123 return -EAGAIN;
Tejun Heobf4ede02012-08-03 10:30:46 -07001124}
1125
1126/**
Tejun Heo706026c2013-01-24 11:01:34 -08001127 * insert_work - insert a work into a pool
Tejun Heo4690c4a2010-06-29 10:07:10 +02001128 * @cwq: cwq @work belongs to
1129 * @work: work to insert
1130 * @head: insertion point
1131 * @extra_flags: extra WORK_STRUCT_* flags to set
1132 *
Tejun Heo706026c2013-01-24 11:01:34 -08001133 * Insert @work which belongs to @cwq after @head. @extra_flags is or'd to
1134 * work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +02001135 *
1136 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001137 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02001138 */
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001139static void insert_work(struct cpu_workqueue_struct *cwq,
Tejun Heo4690c4a2010-06-29 10:07:10 +02001140 struct work_struct *work, struct list_head *head,
1141 unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001142{
Tejun Heo63d95a92012-07-12 14:46:37 -07001143 struct worker_pool *pool = cwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01001144
Tejun Heo4690c4a2010-06-29 10:07:10 +02001145 /* we own @work, set data and link */
Tejun Heo7a22ad72010-06-29 10:07:13 +02001146 set_work_cwq(work, cwq, extra_flags);
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -07001147 list_add_tail(&work->entry, head);
Tejun Heoe22bee72010-06-29 10:07:14 +02001148
1149 /*
1150 * Ensure either worker_sched_deactivated() sees the above
1151 * list_add_tail() or we see zero nr_running to avoid workers
1152 * lying around lazily while there are works to be processed.
1153 */
1154 smp_mb();
1155
Tejun Heo63d95a92012-07-12 14:46:37 -07001156 if (__need_more_worker(pool))
1157 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001158}
1159
Tejun Heoc8efcc22010-12-20 19:32:04 +01001160/*
1161 * Test whether @work is being queued from another work executing on the
1162 * same workqueue. This is rather expensive and should only be used from
1163 * cold paths.
1164 */
1165static bool is_chained_work(struct workqueue_struct *wq)
1166{
1167 unsigned long flags;
1168 unsigned int cpu;
1169
Tejun Heo706026c2013-01-24 11:01:34 -08001170 for_each_wq_cpu(cpu) {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001171 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
1172 struct worker_pool *pool = cwq->pool;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001173 struct worker *worker;
1174 struct hlist_node *pos;
1175 int i;
1176
Tejun Heod565ed62013-01-24 11:01:33 -08001177 spin_lock_irqsave(&pool->lock, flags);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001178 for_each_busy_worker(worker, i, pos, pool) {
Tejun Heoc8efcc22010-12-20 19:32:04 +01001179 if (worker->task != current)
1180 continue;
Tejun Heod565ed62013-01-24 11:01:33 -08001181 spin_unlock_irqrestore(&pool->lock, flags);
Tejun Heoc8efcc22010-12-20 19:32:04 +01001182 /*
1183 * I'm @worker, no locking necessary. See if @work
1184 * is headed to the same workqueue.
1185 */
1186 return worker->current_cwq->wq == wq;
1187 }
Tejun Heod565ed62013-01-24 11:01:33 -08001188 spin_unlock_irqrestore(&pool->lock, flags);
Tejun Heoc8efcc22010-12-20 19:32:04 +01001189 }
1190 return false;
1191}
1192
Tejun Heo4690c4a2010-06-29 10:07:10 +02001193static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 struct work_struct *work)
1195{
Tejun Heod565ed62013-01-24 11:01:33 -08001196 bool highpri = wq->flags & WQ_HIGHPRI;
1197 struct worker_pool *pool;
Tejun Heo502ca9d2010-06-29 10:07:13 +02001198 struct cpu_workqueue_struct *cwq;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001199 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001200 unsigned int work_flags;
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001201 unsigned int req_cpu = cpu;
Tejun Heo8930cab2012-08-03 10:30:45 -07001202
1203 /*
1204 * While a work item is PENDING && off queue, a task trying to
1205 * steal the PENDING will busy-loop waiting for it to either get
1206 * queued or lose PENDING. Grabbing PENDING and queueing should
1207 * happen with IRQ disabled.
1208 */
1209 WARN_ON_ONCE(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001211 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001212
Tejun Heoc8efcc22010-12-20 19:32:04 +01001213 /* if dying, only works from the same workqueue are allowed */
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02001214 if (unlikely(wq->flags & WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001215 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001216 return;
1217
Tejun Heod565ed62013-01-24 11:01:33 -08001218 /* determine pool to use */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001219 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001220 struct worker_pool *last_pool;
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001221
Tejun Heo57469822012-08-03 10:30:45 -07001222 if (cpu == WORK_CPU_UNBOUND)
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001223 cpu = raw_smp_processor_id();
1224
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001225 /*
Tejun Heodbf25762012-08-20 14:51:23 -07001226 * It's multi cpu. If @work was previously on a different
1227 * cpu, it might still be running there, in which case the
1228 * work needs to be queued on that cpu to guarantee
1229 * non-reentrancy.
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001230 */
Tejun Heod565ed62013-01-24 11:01:33 -08001231 pool = get_std_worker_pool(cpu, highpri);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001232 last_pool = get_work_pool(work);
Tejun Heodbf25762012-08-20 14:51:23 -07001233
Tejun Heod565ed62013-01-24 11:01:33 -08001234 if (last_pool && last_pool != pool) {
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001235 struct worker *worker;
1236
Tejun Heod565ed62013-01-24 11:01:33 -08001237 spin_lock(&last_pool->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001238
Tejun Heoc9e7cf22013-01-24 11:01:33 -08001239 worker = find_worker_executing_work(last_pool, work);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001240
1241 if (worker && worker->current_cwq->wq == wq)
Tejun Heod565ed62013-01-24 11:01:33 -08001242 pool = last_pool;
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001243 else {
1244 /* meh... not running there, queue here */
Tejun Heod565ed62013-01-24 11:01:33 -08001245 spin_unlock(&last_pool->lock);
1246 spin_lock(&pool->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001247 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001248 } else {
Tejun Heod565ed62013-01-24 11:01:33 -08001249 spin_lock(&pool->lock);
Tejun Heo8930cab2012-08-03 10:30:45 -07001250 }
Tejun Heof3421792010-07-02 10:03:51 +02001251 } else {
Tejun Heod565ed62013-01-24 11:01:33 -08001252 pool = get_std_worker_pool(WORK_CPU_UNBOUND, highpri);
1253 spin_lock(&pool->lock);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001254 }
1255
Tejun Heod565ed62013-01-24 11:01:33 -08001256 /* pool determined, get cwq and queue */
1257 cwq = get_cwq(pool->cpu, wq);
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001258 trace_workqueue_queue_work(req_cpu, cwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001259
Dan Carpenterf5b25522012-04-13 22:06:58 +03001260 if (WARN_ON(!list_empty(&work->entry))) {
Tejun Heod565ed62013-01-24 11:01:33 -08001261 spin_unlock(&pool->lock);
Dan Carpenterf5b25522012-04-13 22:06:58 +03001262 return;
1263 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001264
Tejun Heo73f53c42010-06-29 10:07:11 +02001265 cwq->nr_in_flight[cwq->work_color]++;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001266 work_flags = work_color_to_flags(cwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001267
1268 if (likely(cwq->nr_active < cwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001269 trace_workqueue_activate_work(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001270 cwq->nr_active++;
Tejun Heo32704762012-07-13 22:16:45 -07001271 worklist = &cwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001272 } else {
1273 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001274 worklist = &cwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001275 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001276
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001277 insert_work(cwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001278
Tejun Heod565ed62013-01-24 11:01:33 -08001279 spin_unlock(&pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280}
1281
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001282/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07001283 * queue_work_on - queue work on specific cpu
1284 * @cpu: CPU number to execute work on
1285 * @wq: workqueue to use
1286 * @work: work to queue
1287 *
Tejun Heod4283e92012-08-03 10:30:44 -07001288 * Returns %false if @work was already on a queue, %true otherwise.
Zhang Ruic1a220e2008-07-23 21:28:39 -07001289 *
1290 * We queue the work to a specific CPU, the caller must ensure it
1291 * can't go away.
1292 */
Tejun Heod4283e92012-08-03 10:30:44 -07001293bool queue_work_on(int cpu, struct workqueue_struct *wq,
1294 struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07001295{
Tejun Heod4283e92012-08-03 10:30:44 -07001296 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001297 unsigned long flags;
1298
1299 local_irq_save(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001300
Tejun Heo22df02b2010-06-29 10:07:10 +02001301 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001302 __queue_work(cpu, wq, work);
Tejun Heod4283e92012-08-03 10:30:44 -07001303 ret = true;
Zhang Ruic1a220e2008-07-23 21:28:39 -07001304 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001305
1306 local_irq_restore(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001307 return ret;
1308}
1309EXPORT_SYMBOL_GPL(queue_work_on);
1310
Tejun Heo0a13c002012-08-03 10:30:44 -07001311/**
1312 * queue_work - queue work on a workqueue
1313 * @wq: workqueue to use
1314 * @work: work to queue
1315 *
Tejun Heod4283e92012-08-03 10:30:44 -07001316 * Returns %false if @work was already on a queue, %true otherwise.
Tejun Heo0a13c002012-08-03 10:30:44 -07001317 *
1318 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1319 * it can be processed by another CPU.
1320 */
Tejun Heod4283e92012-08-03 10:30:44 -07001321bool queue_work(struct workqueue_struct *wq, struct work_struct *work)
Tejun Heo0a13c002012-08-03 10:30:44 -07001322{
Tejun Heo57469822012-08-03 10:30:45 -07001323 return queue_work_on(WORK_CPU_UNBOUND, wq, work);
Tejun Heo0a13c002012-08-03 10:30:44 -07001324}
1325EXPORT_SYMBOL_GPL(queue_work);
1326
Tejun Heod8e794d2012-08-03 10:30:45 -07001327void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
David Howells52bad642006-11-22 14:54:01 +00001329 struct delayed_work *dwork = (struct delayed_work *)__data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001331 /* should have been called from irqsafe timer with irq already off */
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001332 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333}
Tejun Heod8e794d2012-08-03 10:30:45 -07001334EXPORT_SYMBOL_GPL(delayed_work_timer_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001336static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
1337 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001339 struct timer_list *timer = &dwork->timer;
1340 struct work_struct *work = &dwork->work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001342 WARN_ON_ONCE(timer->function != delayed_work_timer_fn ||
1343 timer->data != (unsigned long)dwork);
Tejun Heofc4b5142012-12-04 07:40:39 -08001344 WARN_ON_ONCE(timer_pending(timer));
1345 WARN_ON_ONCE(!list_empty(&work->entry));
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001346
Tejun Heo8852aac2012-12-01 16:23:42 -08001347 /*
1348 * If @delay is 0, queue @dwork->work immediately. This is for
1349 * both optimization and correctness. The earliest @timer can
1350 * expire is on the closest next tick and delayed_work users depend
1351 * on that there's no such delay when @delay is 0.
1352 */
1353 if (!delay) {
1354 __queue_work(cpu, wq, &dwork->work);
1355 return;
1356 }
1357
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001358 timer_stats_timer_set_start_info(&dwork->timer);
1359
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001360 dwork->wq = wq;
Tejun Heo12650572012-08-08 09:38:42 -07001361 dwork->cpu = cpu;
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001362 timer->expires = jiffies + delay;
1363
1364 if (unlikely(cpu != WORK_CPU_UNBOUND))
1365 add_timer_on(timer, cpu);
1366 else
1367 add_timer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368}
1369
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001370/**
1371 * queue_delayed_work_on - queue work on specific CPU after delay
1372 * @cpu: CPU number to execute work on
1373 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001374 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001375 * @delay: number of jiffies to wait before queueing
1376 *
Tejun Heo715f1302012-08-03 10:30:46 -07001377 * Returns %false if @work was already on a queue, %true otherwise. If
1378 * @delay is zero and @dwork is idle, it will be scheduled for immediate
1379 * execution.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001380 */
Tejun Heod4283e92012-08-03 10:30:44 -07001381bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
1382 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001383{
David Howells52bad642006-11-22 14:54:01 +00001384 struct work_struct *work = &dwork->work;
Tejun Heod4283e92012-08-03 10:30:44 -07001385 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001386 unsigned long flags;
1387
1388 /* read the comment in __queue_work() */
1389 local_irq_save(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001390
Tejun Heo22df02b2010-06-29 10:07:10 +02001391 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001392 __queue_delayed_work(cpu, wq, dwork, delay);
Tejun Heod4283e92012-08-03 10:30:44 -07001393 ret = true;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001394 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001395
1396 local_irq_restore(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001397 return ret;
1398}
Dave Jonesae90dd52006-06-30 01:40:45 -04001399EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Tejun Heoc8e55f32010-06-29 10:07:12 +02001401/**
Tejun Heo0a13c002012-08-03 10:30:44 -07001402 * queue_delayed_work - queue work on a workqueue after delay
1403 * @wq: workqueue to use
1404 * @dwork: delayable work to queue
1405 * @delay: number of jiffies to wait before queueing
1406 *
Tejun Heo715f1302012-08-03 10:30:46 -07001407 * Equivalent to queue_delayed_work_on() but tries to use the local CPU.
Tejun Heo0a13c002012-08-03 10:30:44 -07001408 */
Tejun Heod4283e92012-08-03 10:30:44 -07001409bool queue_delayed_work(struct workqueue_struct *wq,
Tejun Heo0a13c002012-08-03 10:30:44 -07001410 struct delayed_work *dwork, unsigned long delay)
1411{
Tejun Heo57469822012-08-03 10:30:45 -07001412 return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
Tejun Heo0a13c002012-08-03 10:30:44 -07001413}
1414EXPORT_SYMBOL_GPL(queue_delayed_work);
1415
1416/**
Tejun Heo8376fe22012-08-03 10:30:47 -07001417 * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
1418 * @cpu: CPU number to execute work on
1419 * @wq: workqueue to use
1420 * @dwork: work to queue
1421 * @delay: number of jiffies to wait before queueing
1422 *
1423 * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
1424 * modify @dwork's timer so that it expires after @delay. If @delay is
1425 * zero, @work is guaranteed to be scheduled immediately regardless of its
1426 * current state.
1427 *
1428 * Returns %false if @dwork was idle and queued, %true if @dwork was
1429 * pending and its timer was modified.
1430 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001431 * This function is safe to call from any context including IRQ handler.
Tejun Heo8376fe22012-08-03 10:30:47 -07001432 * See try_to_grab_pending() for details.
1433 */
1434bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
1435 struct delayed_work *dwork, unsigned long delay)
1436{
1437 unsigned long flags;
1438 int ret;
1439
1440 do {
1441 ret = try_to_grab_pending(&dwork->work, true, &flags);
1442 } while (unlikely(ret == -EAGAIN));
1443
1444 if (likely(ret >= 0)) {
1445 __queue_delayed_work(cpu, wq, dwork, delay);
1446 local_irq_restore(flags);
1447 }
1448
1449 /* -ENOENT from try_to_grab_pending() becomes %true */
1450 return ret;
1451}
1452EXPORT_SYMBOL_GPL(mod_delayed_work_on);
1453
1454/**
1455 * mod_delayed_work - modify delay of or queue a delayed work
1456 * @wq: workqueue to use
1457 * @dwork: work to queue
1458 * @delay: number of jiffies to wait before queueing
1459 *
1460 * mod_delayed_work_on() on local CPU.
1461 */
1462bool mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dwork,
1463 unsigned long delay)
1464{
1465 return mod_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
1466}
1467EXPORT_SYMBOL_GPL(mod_delayed_work);
1468
1469/**
Tejun Heoc8e55f32010-06-29 10:07:12 +02001470 * worker_enter_idle - enter idle state
1471 * @worker: worker which is entering idle state
1472 *
1473 * @worker is entering idle state. Update stats and idle timer if
1474 * necessary.
1475 *
1476 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001477 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001478 */
1479static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001481 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Tejun Heoc8e55f32010-06-29 10:07:12 +02001483 BUG_ON(worker->flags & WORKER_IDLE);
1484 BUG_ON(!list_empty(&worker->entry) &&
1485 (worker->hentry.next || worker->hentry.pprev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Tejun Heocb444762010-07-02 10:03:50 +02001487 /* can't use worker_set_flags(), also called from start_worker() */
1488 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001489 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001490 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001491
Tejun Heoc8e55f32010-06-29 10:07:12 +02001492 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001493 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001494
Tejun Heo628c78e2012-07-17 12:39:27 -07001495 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1496 mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
Tejun Heocb444762010-07-02 10:03:50 +02001497
Tejun Heo544ecf32012-05-14 15:04:50 -07001498 /*
Tejun Heo706026c2013-01-24 11:01:34 -08001499 * Sanity check nr_running. Because wq_unbind_fn() releases
Tejun Heod565ed62013-01-24 11:01:33 -08001500 * pool->lock between setting %WORKER_UNBOUND and zapping
Tejun Heo628c78e2012-07-17 12:39:27 -07001501 * nr_running, the warning may trigger spuriously. Check iff
1502 * unbind is not in progress.
Tejun Heo544ecf32012-05-14 15:04:50 -07001503 */
Tejun Heo24647572013-01-24 11:01:33 -08001504 WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001505 pool->nr_workers == pool->nr_idle &&
Tejun Heoe19e3972013-01-24 11:39:44 -08001506 atomic_read(&pool->nr_running));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507}
1508
Tejun Heoc8e55f32010-06-29 10:07:12 +02001509/**
1510 * worker_leave_idle - leave idle state
1511 * @worker: worker which is leaving idle state
1512 *
1513 * @worker is leaving idle state. Update stats.
1514 *
1515 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001516 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001517 */
1518static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001520 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Tejun Heoc8e55f32010-06-29 10:07:12 +02001522 BUG_ON(!(worker->flags & WORKER_IDLE));
Tejun Heod302f012010-06-29 10:07:13 +02001523 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001524 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001525 list_del_init(&worker->entry);
1526}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Tejun Heoe22bee72010-06-29 10:07:14 +02001528/**
Tejun Heo706026c2013-01-24 11:01:34 -08001529 * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock pool
Tejun Heoe22bee72010-06-29 10:07:14 +02001530 * @worker: self
1531 *
1532 * Works which are scheduled while the cpu is online must at least be
1533 * scheduled to a worker which is bound to the cpu so that if they are
1534 * flushed from cpu callbacks while cpu is going down, they are
1535 * guaranteed to execute on the cpu.
1536 *
1537 * This function is to be used by rogue workers and rescuers to bind
1538 * themselves to the target cpu and may race with cpu going down or
1539 * coming online. kthread_bind() can't be used because it may put the
1540 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
Tejun Heo706026c2013-01-24 11:01:34 -08001541 * verbatim as it's best effort and blocking and pool may be
Tejun Heoe22bee72010-06-29 10:07:14 +02001542 * [dis]associated in the meantime.
1543 *
Tejun Heo706026c2013-01-24 11:01:34 -08001544 * This function tries set_cpus_allowed() and locks pool and verifies the
Tejun Heo24647572013-01-24 11:01:33 -08001545 * binding against %POOL_DISASSOCIATED which is set during
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001546 * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1547 * enters idle state or fetches works without dropping lock, it can
1548 * guarantee the scheduling requirement described in the first paragraph.
Tejun Heoe22bee72010-06-29 10:07:14 +02001549 *
1550 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001551 * Might sleep. Called without any lock but returns with pool->lock
Tejun Heoe22bee72010-06-29 10:07:14 +02001552 * held.
1553 *
1554 * RETURNS:
Tejun Heo706026c2013-01-24 11:01:34 -08001555 * %true if the associated pool is online (@worker is successfully
Tejun Heoe22bee72010-06-29 10:07:14 +02001556 * bound), %false if offline.
1557 */
1558static bool worker_maybe_bind_and_lock(struct worker *worker)
Tejun Heod565ed62013-01-24 11:01:33 -08001559__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001560{
Tejun Heo24647572013-01-24 11:01:33 -08001561 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001562 struct task_struct *task = worker->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Tejun Heoe22bee72010-06-29 10:07:14 +02001564 while (true) {
1565 /*
1566 * The following call may fail, succeed or succeed
1567 * without actually migrating the task to the cpu if
1568 * it races with cpu hotunplug operation. Verify
Tejun Heo24647572013-01-24 11:01:33 -08001569 * against POOL_DISASSOCIATED.
Tejun Heoe22bee72010-06-29 10:07:14 +02001570 */
Tejun Heo24647572013-01-24 11:01:33 -08001571 if (!(pool->flags & POOL_DISASSOCIATED))
Tejun Heoec22ca52013-01-24 11:01:33 -08001572 set_cpus_allowed_ptr(task, get_cpu_mask(pool->cpu));
Oleg Nesterov85f41862007-05-09 02:34:20 -07001573
Tejun Heod565ed62013-01-24 11:01:33 -08001574 spin_lock_irq(&pool->lock);
Tejun Heo24647572013-01-24 11:01:33 -08001575 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heoe22bee72010-06-29 10:07:14 +02001576 return false;
Tejun Heoec22ca52013-01-24 11:01:33 -08001577 if (task_cpu(task) == pool->cpu &&
Tejun Heoe22bee72010-06-29 10:07:14 +02001578 cpumask_equal(&current->cpus_allowed,
Tejun Heoec22ca52013-01-24 11:01:33 -08001579 get_cpu_mask(pool->cpu)))
Tejun Heoe22bee72010-06-29 10:07:14 +02001580 return true;
Tejun Heod565ed62013-01-24 11:01:33 -08001581 spin_unlock_irq(&pool->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001582
Tejun Heo5035b202011-04-29 18:08:37 +02001583 /*
1584 * We've raced with CPU hot[un]plug. Give it a breather
1585 * and retry migration. cond_resched() is required here;
1586 * otherwise, we might deadlock against cpu_stop trying to
1587 * bring down the CPU on non-preemptive kernel.
1588 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001589 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001590 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001591 }
1592}
1593
1594/*
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001595 * Rebind an idle @worker to its CPU. worker_thread() will test
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001596 * list_empty(@worker->entry) before leaving idle and call this function.
Tejun Heo25511a42012-07-17 12:39:27 -07001597 */
1598static void idle_worker_rebind(struct worker *worker)
1599{
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001600 /* CPU may go down again inbetween, clear UNBOUND only on success */
1601 if (worker_maybe_bind_and_lock(worker))
1602 worker_clr_flags(worker, WORKER_UNBOUND);
Tejun Heo25511a42012-07-17 12:39:27 -07001603
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001604 /* rebind complete, become available again */
1605 list_add(&worker->entry, &worker->pool->idle_list);
Tejun Heod565ed62013-01-24 11:01:33 -08001606 spin_unlock_irq(&worker->pool->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07001607}
1608
1609/*
1610 * Function for @worker->rebind.work used to rebind unbound busy workers to
Tejun Heo403c8212012-07-17 12:39:27 -07001611 * the associated cpu which is coming back online. This is scheduled by
1612 * cpu up but can race with other cpu hotplug operations and may be
1613 * executed twice without intervening cpu down.
Tejun Heoe22bee72010-06-29 10:07:14 +02001614 */
Tejun Heo25511a42012-07-17 12:39:27 -07001615static void busy_worker_rebind_fn(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001616{
1617 struct worker *worker = container_of(work, struct worker, rebind_work);
Tejun Heoe22bee72010-06-29 10:07:14 +02001618
Lai Jiangshaneab6d822012-09-18 09:59:22 -07001619 if (worker_maybe_bind_and_lock(worker))
1620 worker_clr_flags(worker, WORKER_UNBOUND);
Tejun Heoe22bee72010-06-29 10:07:14 +02001621
Tejun Heod565ed62013-01-24 11:01:33 -08001622 spin_unlock_irq(&worker->pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001623}
1624
Tejun Heo25511a42012-07-17 12:39:27 -07001625/**
Tejun Heo94cf58b2013-01-24 11:01:33 -08001626 * rebind_workers - rebind all workers of a pool to the associated CPU
1627 * @pool: pool of interest
Tejun Heo25511a42012-07-17 12:39:27 -07001628 *
Tejun Heo94cf58b2013-01-24 11:01:33 -08001629 * @pool->cpu is coming online. Rebind all workers to the CPU. Rebinding
Tejun Heo25511a42012-07-17 12:39:27 -07001630 * is different for idle and busy ones.
1631 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001632 * Idle ones will be removed from the idle_list and woken up. They will
1633 * add themselves back after completing rebind. This ensures that the
1634 * idle_list doesn't contain any unbound workers when re-bound busy workers
1635 * try to perform local wake-ups for concurrency management.
Tejun Heo25511a42012-07-17 12:39:27 -07001636 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001637 * Busy workers can rebind after they finish their current work items.
1638 * Queueing the rebind work item at the head of the scheduled list is
1639 * enough. Note that nr_running will be properly bumped as busy workers
1640 * rebind.
Tejun Heo25511a42012-07-17 12:39:27 -07001641 *
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001642 * On return, all non-manager workers are scheduled for rebind - see
1643 * manage_workers() for the manager special case. Any idle worker
1644 * including the manager will not appear on @idle_list until rebind is
1645 * complete, making local wake-ups safe.
Tejun Heo25511a42012-07-17 12:39:27 -07001646 */
Tejun Heo94cf58b2013-01-24 11:01:33 -08001647static void rebind_workers(struct worker_pool *pool)
Tejun Heo25511a42012-07-17 12:39:27 -07001648{
Lai Jiangshanea1abd62012-09-18 09:59:22 -07001649 struct worker *worker, *n;
Tejun Heo25511a42012-07-17 12:39:27 -07001650 struct hlist_node *pos;
1651 int i;
1652
Tejun Heo94cf58b2013-01-24 11:01:33 -08001653 lockdep_assert_held(&pool->assoc_mutex);
1654 lockdep_assert_held(&pool->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07001655
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07001656 /* dequeue and kick idle ones */
Tejun Heo94cf58b2013-01-24 11:01:33 -08001657 list_for_each_entry_safe(worker, n, &pool->idle_list, entry) {
1658 /*
1659 * idle workers should be off @pool->idle_list until rebind
1660 * is complete to avoid receiving premature local wake-ups.
1661 */
1662 list_del_init(&worker->entry);
Lai Jiangshan96e65302012-09-02 00:28:19 +08001663
Tejun Heo94cf58b2013-01-24 11:01:33 -08001664 /*
1665 * worker_thread() will see the above dequeuing and call
1666 * idle_worker_rebind().
1667 */
1668 wake_up_process(worker->task);
1669 }
Tejun Heo25511a42012-07-17 12:39:27 -07001670
Tejun Heo94cf58b2013-01-24 11:01:33 -08001671 /* rebind busy workers */
1672 for_each_busy_worker(worker, i, pos, pool) {
1673 struct work_struct *rebind_work = &worker->rebind_work;
1674 struct workqueue_struct *wq;
Tejun Heo25511a42012-07-17 12:39:27 -07001675
Tejun Heo94cf58b2013-01-24 11:01:33 -08001676 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
1677 work_data_bits(rebind_work)))
1678 continue;
Tejun Heo25511a42012-07-17 12:39:27 -07001679
Tejun Heo94cf58b2013-01-24 11:01:33 -08001680 debug_work_activate(rebind_work);
Tejun Heo90beca52012-09-04 23:12:33 -07001681
Tejun Heo94cf58b2013-01-24 11:01:33 -08001682 /*
1683 * wq doesn't really matter but let's keep @worker->pool
1684 * and @cwq->pool consistent for sanity.
1685 */
1686 if (std_worker_pool_pri(worker->pool))
1687 wq = system_highpri_wq;
1688 else
1689 wq = system_wq;
Tejun Heoec588152012-09-04 23:16:32 -07001690
Tejun Heo94cf58b2013-01-24 11:01:33 -08001691 insert_work(get_cwq(pool->cpu, wq), rebind_work,
1692 worker->scheduled.next,
1693 work_color_to_flags(WORK_NO_COLOR));
Tejun Heoec588152012-09-04 23:16:32 -07001694 }
Tejun Heo25511a42012-07-17 12:39:27 -07001695}
1696
Tejun Heoc34056a2010-06-29 10:07:11 +02001697static struct worker *alloc_worker(void)
1698{
1699 struct worker *worker;
1700
1701 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001702 if (worker) {
1703 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001704 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heo25511a42012-07-17 12:39:27 -07001705 INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn);
Tejun Heoe22bee72010-06-29 10:07:14 +02001706 /* on creation a worker is in !idle && prep state */
1707 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001708 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001709 return worker;
1710}
1711
1712/**
1713 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001714 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001715 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001716 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001717 * can be started by calling start_worker() or destroyed using
1718 * destroy_worker().
1719 *
1720 * CONTEXT:
1721 * Might sleep. Does GFP_KERNEL allocations.
1722 *
1723 * RETURNS:
1724 * Pointer to the newly created worker.
1725 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001726static struct worker *create_worker(struct worker_pool *pool)
Tejun Heoc34056a2010-06-29 10:07:11 +02001727{
Tejun Heoe34cdddb2013-01-24 11:01:33 -08001728 const char *pri = std_worker_pool_pri(pool) ? "H" : "";
Tejun Heoc34056a2010-06-29 10:07:11 +02001729 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001730 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001731
Tejun Heod565ed62013-01-24 11:01:33 -08001732 spin_lock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001733 while (ida_get_new(&pool->worker_ida, &id)) {
Tejun Heod565ed62013-01-24 11:01:33 -08001734 spin_unlock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001735 if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL))
Tejun Heoc34056a2010-06-29 10:07:11 +02001736 goto fail;
Tejun Heod565ed62013-01-24 11:01:33 -08001737 spin_lock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001738 }
Tejun Heod565ed62013-01-24 11:01:33 -08001739 spin_unlock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001740
1741 worker = alloc_worker();
1742 if (!worker)
1743 goto fail;
1744
Tejun Heobd7bdd42012-07-12 14:46:37 -07001745 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001746 worker->id = id;
1747
Tejun Heoec22ca52013-01-24 11:01:33 -08001748 if (pool->cpu != WORK_CPU_UNBOUND)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001749 worker->task = kthread_create_on_node(worker_thread,
Tejun Heoec22ca52013-01-24 11:01:33 -08001750 worker, cpu_to_node(pool->cpu),
1751 "kworker/%u:%d%s", pool->cpu, id, pri);
Tejun Heof3421792010-07-02 10:03:51 +02001752 else
1753 worker->task = kthread_create(worker_thread, worker,
Tejun Heo32704762012-07-13 22:16:45 -07001754 "kworker/u:%d%s", id, pri);
Tejun Heoc34056a2010-06-29 10:07:11 +02001755 if (IS_ERR(worker->task))
1756 goto fail;
1757
Tejun Heoe34cdddb2013-01-24 11:01:33 -08001758 if (std_worker_pool_pri(pool))
Tejun Heo32704762012-07-13 22:16:45 -07001759 set_user_nice(worker->task, HIGHPRI_NICE_LEVEL);
1760
Tejun Heodb7bccf2010-06-29 10:07:12 +02001761 /*
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001762 * Determine CPU binding of the new worker depending on
Tejun Heo24647572013-01-24 11:01:33 -08001763 * %POOL_DISASSOCIATED. The caller is responsible for ensuring the
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001764 * flag remains stable across this function. See the comments
1765 * above the flag definition for details.
1766 *
1767 * As an unbound worker may later become a regular one if CPU comes
1768 * online, make sure every worker has %PF_THREAD_BOUND set.
Tejun Heodb7bccf2010-06-29 10:07:12 +02001769 */
Tejun Heo24647572013-01-24 11:01:33 -08001770 if (!(pool->flags & POOL_DISASSOCIATED)) {
Tejun Heoec22ca52013-01-24 11:01:33 -08001771 kthread_bind(worker->task, pool->cpu);
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001772 } else {
Tejun Heodb7bccf2010-06-29 10:07:12 +02001773 worker->task->flags |= PF_THREAD_BOUND;
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001774 worker->flags |= WORKER_UNBOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07001776
Tejun Heoc34056a2010-06-29 10:07:11 +02001777 return worker;
1778fail:
1779 if (id >= 0) {
Tejun Heod565ed62013-01-24 11:01:33 -08001780 spin_lock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001781 ida_remove(&pool->worker_ida, id);
Tejun Heod565ed62013-01-24 11:01:33 -08001782 spin_unlock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001783 }
1784 kfree(worker);
1785 return NULL;
1786}
1787
1788/**
1789 * start_worker - start a newly created worker
1790 * @worker: worker to start
1791 *
Tejun Heo706026c2013-01-24 11:01:34 -08001792 * Make the pool aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001793 *
1794 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001795 * spin_lock_irq(pool->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001796 */
1797static void start_worker(struct worker *worker)
1798{
Tejun Heocb444762010-07-02 10:03:50 +02001799 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001800 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001801 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001802 wake_up_process(worker->task);
1803}
1804
1805/**
1806 * destroy_worker - destroy a workqueue worker
1807 * @worker: worker to be destroyed
1808 *
Tejun Heo706026c2013-01-24 11:01:34 -08001809 * Destroy @worker and adjust @pool stats accordingly.
Tejun Heoc8e55f32010-06-29 10:07:12 +02001810 *
1811 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001812 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001813 */
1814static void destroy_worker(struct worker *worker)
1815{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001816 struct worker_pool *pool = worker->pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001817 int id = worker->id;
1818
1819 /* sanity check frenzy */
1820 BUG_ON(worker->current_work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001821 BUG_ON(!list_empty(&worker->scheduled));
Tejun Heoc34056a2010-06-29 10:07:11 +02001822
Tejun Heoc8e55f32010-06-29 10:07:12 +02001823 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001824 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001825 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001826 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001827
1828 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001829 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001830
Tejun Heod565ed62013-01-24 11:01:33 -08001831 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001832
Tejun Heoc34056a2010-06-29 10:07:11 +02001833 kthread_stop(worker->task);
1834 kfree(worker);
1835
Tejun Heod565ed62013-01-24 11:01:33 -08001836 spin_lock_irq(&pool->lock);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001837 ida_remove(&pool->worker_ida, id);
Tejun Heoc34056a2010-06-29 10:07:11 +02001838}
1839
Tejun Heo63d95a92012-07-12 14:46:37 -07001840static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001841{
Tejun Heo63d95a92012-07-12 14:46:37 -07001842 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001843
Tejun Heod565ed62013-01-24 11:01:33 -08001844 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001845
Tejun Heo63d95a92012-07-12 14:46:37 -07001846 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001847 struct worker *worker;
1848 unsigned long expires;
1849
1850 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001851 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001852 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1853
1854 if (time_before(jiffies, expires))
Tejun Heo63d95a92012-07-12 14:46:37 -07001855 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001856 else {
1857 /* it's been idle for too long, wake up manager */
Tejun Heo11ebea52012-07-12 14:46:37 -07001858 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -07001859 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001860 }
1861 }
1862
Tejun Heod565ed62013-01-24 11:01:33 -08001863 spin_unlock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001864}
1865
1866static bool send_mayday(struct work_struct *work)
1867{
1868 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1869 struct workqueue_struct *wq = cwq->wq;
Tejun Heof3421792010-07-02 10:03:51 +02001870 unsigned int cpu;
Tejun Heoe22bee72010-06-29 10:07:14 +02001871
1872 if (!(wq->flags & WQ_RESCUER))
1873 return false;
1874
1875 /* mayday mayday mayday */
Tejun Heoec22ca52013-01-24 11:01:33 -08001876 cpu = cwq->pool->cpu;
Tejun Heof3421792010-07-02 10:03:51 +02001877 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1878 if (cpu == WORK_CPU_UNBOUND)
1879 cpu = 0;
Tejun Heof2e005a2010-07-20 15:59:09 +02001880 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001881 wake_up_process(wq->rescuer->task);
1882 return true;
1883}
1884
Tejun Heo706026c2013-01-24 11:01:34 -08001885static void pool_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001886{
Tejun Heo63d95a92012-07-12 14:46:37 -07001887 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001888 struct work_struct *work;
1889
Tejun Heod565ed62013-01-24 11:01:33 -08001890 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001891
Tejun Heo63d95a92012-07-12 14:46:37 -07001892 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001893 /*
1894 * We've been trying to create a new worker but
1895 * haven't been successful. We might be hitting an
1896 * allocation deadlock. Send distress signals to
1897 * rescuers.
1898 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001899 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001900 send_mayday(work);
1901 }
1902
Tejun Heod565ed62013-01-24 11:01:33 -08001903 spin_unlock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001904
Tejun Heo63d95a92012-07-12 14:46:37 -07001905 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001906}
1907
1908/**
1909 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001910 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001911 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001912 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001913 * have at least one idle worker on return from this function. If
1914 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001915 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001916 * possible allocation deadlock.
1917 *
1918 * On return, need_to_create_worker() is guaranteed to be false and
1919 * may_start_working() true.
1920 *
1921 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001922 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001923 * multiple times. Does GFP_KERNEL allocations. Called only from
1924 * manager.
1925 *
1926 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08001927 * false if no action was taken and pool->lock stayed locked, true
Tejun Heoe22bee72010-06-29 10:07:14 +02001928 * otherwise.
1929 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001930static bool maybe_create_worker(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001931__releases(&pool->lock)
1932__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001933{
Tejun Heo63d95a92012-07-12 14:46:37 -07001934 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001935 return false;
1936restart:
Tejun Heod565ed62013-01-24 11:01:33 -08001937 spin_unlock_irq(&pool->lock);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001938
Tejun Heoe22bee72010-06-29 10:07:14 +02001939 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07001940 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001941
1942 while (true) {
1943 struct worker *worker;
1944
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001945 worker = create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001946 if (worker) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001947 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001948 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001949 start_worker(worker);
Tejun Heo63d95a92012-07-12 14:46:37 -07001950 BUG_ON(need_to_create_worker(pool));
Tejun Heoe22bee72010-06-29 10:07:14 +02001951 return true;
1952 }
1953
Tejun Heo63d95a92012-07-12 14:46:37 -07001954 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001955 break;
1956
Tejun Heoe22bee72010-06-29 10:07:14 +02001957 __set_current_state(TASK_INTERRUPTIBLE);
1958 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001959
Tejun Heo63d95a92012-07-12 14:46:37 -07001960 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001961 break;
1962 }
1963
Tejun Heo63d95a92012-07-12 14:46:37 -07001964 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001965 spin_lock_irq(&pool->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07001966 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001967 goto restart;
1968 return true;
1969}
1970
1971/**
1972 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo63d95a92012-07-12 14:46:37 -07001973 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001974 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001975 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02001976 * IDLE_WORKER_TIMEOUT.
1977 *
1978 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001979 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001980 * multiple times. Called only from manager.
1981 *
1982 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08001983 * false if no action was taken and pool->lock stayed locked, true
Tejun Heoe22bee72010-06-29 10:07:14 +02001984 * otherwise.
1985 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001986static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001987{
1988 bool ret = false;
1989
Tejun Heo63d95a92012-07-12 14:46:37 -07001990 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001991 struct worker *worker;
1992 unsigned long expires;
1993
Tejun Heo63d95a92012-07-12 14:46:37 -07001994 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001995 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1996
1997 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001998 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001999 break;
2000 }
2001
2002 destroy_worker(worker);
2003 ret = true;
2004 }
2005
2006 return ret;
2007}
2008
2009/**
2010 * manage_workers - manage worker pool
2011 * @worker: self
2012 *
Tejun Heo706026c2013-01-24 11:01:34 -08002013 * Assume the manager role and manage the worker pool @worker belongs
Tejun Heoe22bee72010-06-29 10:07:14 +02002014 * to. At any given time, there can be only zero or one manager per
Tejun Heo706026c2013-01-24 11:01:34 -08002015 * pool. The exclusion is handled automatically by this function.
Tejun Heoe22bee72010-06-29 10:07:14 +02002016 *
2017 * The caller can safely start processing works on false return. On
2018 * true return, it's guaranteed that need_to_create_worker() is false
2019 * and may_start_working() is true.
2020 *
2021 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002022 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02002023 * multiple times. Does GFP_KERNEL allocations.
2024 *
2025 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08002026 * spin_lock_irq(pool->lock) which may be released and regrabbed
2027 * multiple times. Does GFP_KERNEL allocations.
Tejun Heoe22bee72010-06-29 10:07:14 +02002028 */
2029static bool manage_workers(struct worker *worker)
2030{
Tejun Heo63d95a92012-07-12 14:46:37 -07002031 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002032 bool ret = false;
2033
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002034 if (pool->flags & POOL_MANAGING_WORKERS)
Tejun Heoe22bee72010-06-29 10:07:14 +02002035 return ret;
2036
Lai Jiangshan552a37e2012-09-10 10:03:33 -07002037 pool->flags |= POOL_MANAGING_WORKERS;
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002038
2039 /*
2040 * To simplify both worker management and CPU hotplug, hold off
2041 * management while hotplug is in progress. CPU hotplug path can't
2042 * grab %POOL_MANAGING_WORKERS to achieve this because that can
2043 * lead to idle worker depletion (all become busy thinking someone
2044 * else is managing) which in turn can result in deadlock under
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002045 * extreme circumstances. Use @pool->assoc_mutex to synchronize
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002046 * manager against CPU hotplug.
2047 *
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002048 * assoc_mutex would always be free unless CPU hotplug is in
Tejun Heod565ed62013-01-24 11:01:33 -08002049 * progress. trylock first without dropping @pool->lock.
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002050 */
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002051 if (unlikely(!mutex_trylock(&pool->assoc_mutex))) {
Tejun Heod565ed62013-01-24 11:01:33 -08002052 spin_unlock_irq(&pool->lock);
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002053 mutex_lock(&pool->assoc_mutex);
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002054 /*
2055 * CPU hotplug could have happened while we were waiting
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002056 * for assoc_mutex. Hotplug itself can't handle us
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002057 * because manager isn't either on idle or busy list, and
Tejun Heo706026c2013-01-24 11:01:34 -08002058 * @pool's state and ours could have deviated.
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002059 *
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002060 * As hotplug is now excluded via assoc_mutex, we can
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002061 * simply try to bind. It will succeed or fail depending
Tejun Heo706026c2013-01-24 11:01:34 -08002062 * on @pool's current state. Try it and adjust
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002063 * %WORKER_UNBOUND accordingly.
2064 */
2065 if (worker_maybe_bind_and_lock(worker))
2066 worker->flags &= ~WORKER_UNBOUND;
2067 else
2068 worker->flags |= WORKER_UNBOUND;
2069
2070 ret = true;
2071 }
2072
Tejun Heo11ebea52012-07-12 14:46:37 -07002073 pool->flags &= ~POOL_MANAGE_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02002074
2075 /*
2076 * Destroy and then create so that may_start_working() is true
2077 * on return.
2078 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002079 ret |= maybe_destroy_workers(pool);
2080 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002081
Lai Jiangshan552a37e2012-09-10 10:03:33 -07002082 pool->flags &= ~POOL_MANAGING_WORKERS;
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07002083 mutex_unlock(&pool->assoc_mutex);
Tejun Heoe22bee72010-06-29 10:07:14 +02002084 return ret;
2085}
2086
Tejun Heoa62428c2010-06-29 10:07:10 +02002087/**
2088 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02002089 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02002090 * @work: work to process
2091 *
2092 * Process @work. This function contains all the logics necessary to
2093 * process a single work including synchronization against and
2094 * interaction with other workers on the same cpu, queueing and
2095 * flushing. As long as context requirement is met, any worker can
2096 * call this function to process a work.
2097 *
2098 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002099 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02002100 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002101static void process_one_work(struct worker *worker, struct work_struct *work)
Tejun Heod565ed62013-01-24 11:01:33 -08002102__releases(&pool->lock)
2103__acquires(&pool->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02002104{
Tejun Heo7e116292010-06-29 10:07:13 +02002105 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002106 struct worker_pool *pool = worker->pool;
Tejun Heofb0e7be2010-06-29 10:07:15 +02002107 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02002108 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02002109 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02002110#ifdef CONFIG_LOCKDEP
2111 /*
2112 * It is permissible to free the struct work_struct from
2113 * inside the function that is called from it, this we need to
2114 * take into account for lockdep too. To avoid bogus "held
2115 * lock freed" warnings as well as problems when looking into
2116 * work->lockdep_map, make a copy and use that here.
2117 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07002118 struct lockdep_map lockdep_map;
2119
2120 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002121#endif
Tejun Heo6fec10a2012-07-22 10:16:34 -07002122 /*
2123 * Ensure we're on the correct CPU. DISASSOCIATED test is
2124 * necessary to avoid spurious warnings from rescuers servicing the
Tejun Heo24647572013-01-24 11:01:33 -08002125 * unbound or a disassociated pool.
Tejun Heo6fec10a2012-07-22 10:16:34 -07002126 */
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002127 WARN_ON_ONCE(!(worker->flags & WORKER_UNBOUND) &&
Tejun Heo24647572013-01-24 11:01:33 -08002128 !(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heoec22ca52013-01-24 11:01:33 -08002129 raw_smp_processor_id() != pool->cpu);
Tejun Heo25511a42012-07-17 12:39:27 -07002130
Tejun Heo7e116292010-06-29 10:07:13 +02002131 /*
2132 * A single work shouldn't be executed concurrently by
2133 * multiple workers on a single cpu. Check whether anyone is
2134 * already processing the work. If so, defer the work to the
2135 * currently executing one.
2136 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002137 collision = find_worker_executing_work(pool, work);
Tejun Heo7e116292010-06-29 10:07:13 +02002138 if (unlikely(collision)) {
2139 move_linked_works(work, &collision->scheduled, NULL);
2140 return;
2141 }
2142
Tejun Heo8930cab2012-08-03 10:30:45 -07002143 /* claim and dequeue */
Tejun Heoa62428c2010-06-29 10:07:10 +02002144 debug_work_deactivate(work);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002145 hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work);
Tejun Heoc34056a2010-06-29 10:07:11 +02002146 worker->current_work = work;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002147 worker->current_func = work->func;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02002148 worker->current_cwq = cwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002149 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002150
Tejun Heoa62428c2010-06-29 10:07:10 +02002151 list_del_init(&work->entry);
2152
Tejun Heo649027d2010-06-29 10:07:14 +02002153 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02002154 * CPU intensive works don't participate in concurrency
2155 * management. They're the scheduler's responsibility.
2156 */
2157 if (unlikely(cpu_intensive))
2158 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
2159
Tejun Heo974271c2012-07-12 14:46:37 -07002160 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002161 * Unbound pool isn't concurrency managed and work items should be
Tejun Heo974271c2012-07-12 14:46:37 -07002162 * executed ASAP. Wake up another worker if necessary.
2163 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002164 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
2165 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07002166
Tejun Heo8930cab2012-08-03 10:30:45 -07002167 /*
Tejun Heo7c3eed52013-01-24 11:01:33 -08002168 * Record the last pool and clear PENDING which should be the last
Tejun Heod565ed62013-01-24 11:01:33 -08002169 * update to @work. Also, do this inside @pool->lock so that
Tejun Heo23657bb2012-08-13 17:08:19 -07002170 * PENDING and queued state changes happen together while IRQ is
2171 * disabled.
Tejun Heo8930cab2012-08-03 10:30:45 -07002172 */
Tejun Heo7c3eed52013-01-24 11:01:33 -08002173 set_work_pool_and_clear_pending(work, pool->id);
Tejun Heoa62428c2010-06-29 10:07:10 +02002174
Tejun Heod565ed62013-01-24 11:01:33 -08002175 spin_unlock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002176
Tejun Heoe1594892011-01-09 23:32:15 +01002177 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002178 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002179 trace_workqueue_execute_start(work);
Tejun Heoa2c1c572012-12-18 10:35:02 -08002180 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002181 /*
2182 * While we must be careful to not use "work" after this, the trace
2183 * point will only record its address.
2184 */
2185 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002186 lock_map_release(&lockdep_map);
2187 lock_map_release(&cwq->wq->lockdep_map);
2188
2189 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Valentin Ilie044c7822012-08-19 00:52:42 +03002190 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
2191 " last function: %pf\n",
Tejun Heoa2c1c572012-12-18 10:35:02 -08002192 current->comm, preempt_count(), task_pid_nr(current),
2193 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02002194 debug_show_held_locks(current);
2195 dump_stack();
2196 }
2197
Tejun Heod565ed62013-01-24 11:01:33 -08002198 spin_lock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002199
Tejun Heofb0e7be2010-06-29 10:07:15 +02002200 /* clear cpu intensive status */
2201 if (unlikely(cpu_intensive))
2202 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2203
Tejun Heoa62428c2010-06-29 10:07:10 +02002204 /* we're done with it, release */
Sasha Levin42f85702012-12-17 10:01:23 -05002205 hash_del(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002206 worker->current_work = NULL;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002207 worker->current_func = NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +02002208 worker->current_cwq = NULL;
Lai Jiangshanb3f9f402012-09-18 10:40:00 -07002209 cwq_dec_nr_in_flight(cwq, work_color);
Tejun Heoa62428c2010-06-29 10:07:10 +02002210}
2211
Tejun Heoaffee4b2010-06-29 10:07:12 +02002212/**
2213 * process_scheduled_works - process scheduled works
2214 * @worker: self
2215 *
2216 * Process all scheduled works. Please note that the scheduled list
2217 * may change while processing a work, so this function repeatedly
2218 * fetches a work from the top and executes it.
2219 *
2220 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002221 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02002222 * multiple times.
2223 */
2224static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002226 while (!list_empty(&worker->scheduled)) {
2227 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002229 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231}
2232
Tejun Heo4690c4a2010-06-29 10:07:10 +02002233/**
2234 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02002235 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02002236 *
Tejun Heo706026c2013-01-24 11:01:34 -08002237 * The worker thread function. There are NR_CPU_WORKER_POOLS dynamic pools
2238 * of these per each cpu. These workers process all works regardless of
Tejun Heoe22bee72010-06-29 10:07:14 +02002239 * their specific target workqueue. The only exception is works which
2240 * belong to workqueues with a rescuer which will be explained in
2241 * rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02002242 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002243static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244{
Tejun Heoc34056a2010-06-29 10:07:11 +02002245 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07002246 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
Tejun Heoe22bee72010-06-29 10:07:14 +02002248 /* tell the scheduler that this is a workqueue worker */
2249 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002250woke_up:
Tejun Heod565ed62013-01-24 11:01:33 -08002251 spin_lock_irq(&pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002253 /* we are off idle list if destruction or rebind is requested */
2254 if (unlikely(list_empty(&worker->entry))) {
Tejun Heod565ed62013-01-24 11:01:33 -08002255 spin_unlock_irq(&pool->lock);
Tejun Heo25511a42012-07-17 12:39:27 -07002256
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002257 /* if DIE is set, destruction is requested */
Tejun Heo25511a42012-07-17 12:39:27 -07002258 if (worker->flags & WORKER_DIE) {
2259 worker->task->flags &= ~PF_WQ_WORKER;
2260 return 0;
2261 }
2262
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002263 /* otherwise, rebind */
Tejun Heo25511a42012-07-17 12:39:27 -07002264 idle_worker_rebind(worker);
2265 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 }
2267
Tejun Heoc8e55f32010-06-29 10:07:12 +02002268 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002269recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002270 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002271 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002272 goto sleep;
2273
2274 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002275 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002276 goto recheck;
2277
Tejun Heoc8e55f32010-06-29 10:07:12 +02002278 /*
2279 * ->scheduled list can only be filled while a worker is
2280 * preparing to process a work or actually processing it.
2281 * Make sure nobody diddled with it while I was sleeping.
2282 */
2283 BUG_ON(!list_empty(&worker->scheduled));
2284
Tejun Heoe22bee72010-06-29 10:07:14 +02002285 /*
2286 * When control reaches this point, we're guaranteed to have
2287 * at least one idle worker or that someone else has already
2288 * assumed the manager role.
2289 */
2290 worker_clr_flags(worker, WORKER_PREP);
2291
2292 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002293 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002294 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002295 struct work_struct, entry);
2296
2297 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2298 /* optimization path, not strictly necessary */
2299 process_one_work(worker, work);
2300 if (unlikely(!list_empty(&worker->scheduled)))
2301 process_scheduled_works(worker);
2302 } else {
2303 move_linked_works(work, &worker->scheduled, NULL);
2304 process_scheduled_works(worker);
2305 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002306 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002307
Tejun Heoe22bee72010-06-29 10:07:14 +02002308 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002309sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07002310 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002311 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002312
Tejun Heoc8e55f32010-06-29 10:07:12 +02002313 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002314 * pool->lock is held and there's no work to process and no need to
2315 * manage, sleep. Workers are woken up only while holding
2316 * pool->lock or from local cpu, so setting the current state
2317 * before releasing pool->lock is enough to prevent losing any
2318 * event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002319 */
2320 worker_enter_idle(worker);
2321 __set_current_state(TASK_INTERRUPTIBLE);
Tejun Heod565ed62013-01-24 11:01:33 -08002322 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02002323 schedule();
2324 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325}
2326
Tejun Heoe22bee72010-06-29 10:07:14 +02002327/**
2328 * rescuer_thread - the rescuer thread function
Tejun Heo111c2252013-01-17 17:16:24 -08002329 * @__rescuer: self
Tejun Heoe22bee72010-06-29 10:07:14 +02002330 *
2331 * Workqueue rescuer thread function. There's one rescuer for each
2332 * workqueue which has WQ_RESCUER set.
2333 *
Tejun Heo706026c2013-01-24 11:01:34 -08002334 * Regular work processing on a pool may block trying to create a new
Tejun Heoe22bee72010-06-29 10:07:14 +02002335 * worker which uses GFP_KERNEL allocation which has slight chance of
2336 * developing into deadlock if some works currently on the same queue
2337 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2338 * the problem rescuer solves.
2339 *
Tejun Heo706026c2013-01-24 11:01:34 -08002340 * When such condition is possible, the pool summons rescuers of all
2341 * workqueues which have works queued on the pool and let them process
Tejun Heoe22bee72010-06-29 10:07:14 +02002342 * those works so that forward progress can be guaranteed.
2343 *
2344 * This should happen rarely.
2345 */
Tejun Heo111c2252013-01-17 17:16:24 -08002346static int rescuer_thread(void *__rescuer)
Tejun Heoe22bee72010-06-29 10:07:14 +02002347{
Tejun Heo111c2252013-01-17 17:16:24 -08002348 struct worker *rescuer = __rescuer;
2349 struct workqueue_struct *wq = rescuer->rescue_wq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002350 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heof3421792010-07-02 10:03:51 +02002351 bool is_unbound = wq->flags & WQ_UNBOUND;
Tejun Heoe22bee72010-06-29 10:07:14 +02002352 unsigned int cpu;
2353
2354 set_user_nice(current, RESCUER_NICE_LEVEL);
Tejun Heo111c2252013-01-17 17:16:24 -08002355
2356 /*
2357 * Mark rescuer as worker too. As WORKER_PREP is never cleared, it
2358 * doesn't participate in concurrency management.
2359 */
2360 rescuer->task->flags |= PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002361repeat:
2362 set_current_state(TASK_INTERRUPTIBLE);
2363
Mike Galbraith412d32e2012-11-28 07:17:18 +01002364 if (kthread_should_stop()) {
2365 __set_current_state(TASK_RUNNING);
Tejun Heo111c2252013-01-17 17:16:24 -08002366 rescuer->task->flags &= ~PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002367 return 0;
Mike Galbraith412d32e2012-11-28 07:17:18 +01002368 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002369
Tejun Heof3421792010-07-02 10:03:51 +02002370 /*
2371 * See whether any cpu is asking for help. Unbounded
2372 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
2373 */
Tejun Heof2e005a2010-07-20 15:59:09 +02002374 for_each_mayday_cpu(cpu, wq->mayday_mask) {
Tejun Heof3421792010-07-02 10:03:51 +02002375 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
2376 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002377 struct worker_pool *pool = cwq->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002378 struct work_struct *work, *n;
2379
2380 __set_current_state(TASK_RUNNING);
Tejun Heof2e005a2010-07-20 15:59:09 +02002381 mayday_clear_cpu(cpu, wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02002382
2383 /* migrate to the target cpu if possible */
Tejun Heobd7bdd42012-07-12 14:46:37 -07002384 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002385 worker_maybe_bind_and_lock(rescuer);
2386
2387 /*
2388 * Slurp in all works issued via this workqueue and
2389 * process'em.
2390 */
2391 BUG_ON(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002392 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02002393 if (get_work_cwq(work) == cwq)
2394 move_linked_works(work, scheduled, &n);
2395
2396 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002397
2398 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002399 * Leave this pool. If keep_working() is %true, notify a
Tejun Heo75769582011-02-14 14:04:46 +01002400 * regular worker; otherwise, we end up with 0 concurrency
2401 * and stalling the execution.
2402 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002403 if (keep_working(pool))
2404 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002405
Tejun Heod565ed62013-01-24 11:01:33 -08002406 spin_unlock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002407 }
2408
Tejun Heo111c2252013-01-17 17:16:24 -08002409 /* rescuers should never participate in concurrency management */
2410 WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
Tejun Heoe22bee72010-06-29 10:07:14 +02002411 schedule();
2412 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413}
2414
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002415struct wq_barrier {
2416 struct work_struct work;
2417 struct completion done;
2418};
2419
2420static void wq_barrier_func(struct work_struct *work)
2421{
2422 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2423 complete(&barr->done);
2424}
2425
Tejun Heo4690c4a2010-06-29 10:07:10 +02002426/**
2427 * insert_wq_barrier - insert a barrier work
2428 * @cwq: cwq to insert barrier into
2429 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002430 * @target: target work to attach @barr to
2431 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002432 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002433 * @barr is linked to @target such that @barr is completed only after
2434 * @target finishes execution. Please note that the ordering
2435 * guarantee is observed only with respect to @target and on the local
2436 * cpu.
2437 *
2438 * Currently, a queued barrier can't be canceled. This is because
2439 * try_to_grab_pending() can't determine whether the work to be
2440 * grabbed is at the head of the queue and thus can't clear LINKED
2441 * flag of the previous work while there must be a valid next work
2442 * after a work with LINKED flag set.
2443 *
2444 * Note that when @worker is non-NULL, @target may be modified
2445 * underneath us, so we can't reliably determine cwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002446 *
2447 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002448 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002449 */
Oleg Nesterov83c22522007-05-09 02:33:54 -07002450static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002451 struct wq_barrier *barr,
2452 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002453{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002454 struct list_head *head;
2455 unsigned int linked = 0;
2456
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002457 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002458 * debugobject calls are safe here even with pool->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002459 * as we know for sure that this will not trigger any of the
2460 * checks and call back into the fixup functions where we
2461 * might deadlock.
2462 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002463 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002464 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002465 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002466
Tejun Heoaffee4b2010-06-29 10:07:12 +02002467 /*
2468 * If @target is currently being executed, schedule the
2469 * barrier to the worker; otherwise, put it after @target.
2470 */
2471 if (worker)
2472 head = worker->scheduled.next;
2473 else {
2474 unsigned long *bits = work_data_bits(target);
2475
2476 head = target->entry.next;
2477 /* there can already be other linked works, inherit and set */
2478 linked = *bits & WORK_STRUCT_LINKED;
2479 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2480 }
2481
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002482 debug_work_activate(&barr->work);
Tejun Heoaffee4b2010-06-29 10:07:12 +02002483 insert_work(cwq, &barr->work, head,
2484 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002485}
2486
Tejun Heo73f53c42010-06-29 10:07:11 +02002487/**
2488 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2489 * @wq: workqueue being flushed
2490 * @flush_color: new flush color, < 0 for no-op
2491 * @work_color: new work color, < 0 for no-op
2492 *
2493 * Prepare cwqs for workqueue flushing.
2494 *
2495 * If @flush_color is non-negative, flush_color on all cwqs should be
2496 * -1. If no cwq has in-flight commands at the specified color, all
2497 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2498 * has in flight commands, its cwq->flush_color is set to
2499 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2500 * wakeup logic is armed and %true is returned.
2501 *
2502 * The caller should have initialized @wq->first_flusher prior to
2503 * calling this function with non-negative @flush_color. If
2504 * @flush_color is negative, no flush color update is done and %false
2505 * is returned.
2506 *
2507 * If @work_color is non-negative, all cwqs should have the same
2508 * work_color which is previous to @work_color and all will be
2509 * advanced to @work_color.
2510 *
2511 * CONTEXT:
2512 * mutex_lock(wq->flush_mutex).
2513 *
2514 * RETURNS:
2515 * %true if @flush_color >= 0 and there's something to flush. %false
2516 * otherwise.
2517 */
2518static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2519 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520{
Tejun Heo73f53c42010-06-29 10:07:11 +02002521 bool wait = false;
2522 unsigned int cpu;
Oleg Nesterov14441962007-05-23 13:57:57 -07002523
Tejun Heo73f53c42010-06-29 10:07:11 +02002524 if (flush_color >= 0) {
2525 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2526 atomic_set(&wq->nr_cwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002527 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002528
Tejun Heof3421792010-07-02 10:03:51 +02002529 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02002530 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heod565ed62013-01-24 11:01:33 -08002531 struct worker_pool *pool = cwq->pool;
Tejun Heo73f53c42010-06-29 10:07:11 +02002532
Tejun Heod565ed62013-01-24 11:01:33 -08002533 spin_lock_irq(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002534
2535 if (flush_color >= 0) {
2536 BUG_ON(cwq->flush_color != -1);
2537
2538 if (cwq->nr_in_flight[flush_color]) {
2539 cwq->flush_color = flush_color;
2540 atomic_inc(&wq->nr_cwqs_to_flush);
2541 wait = true;
2542 }
2543 }
2544
2545 if (work_color >= 0) {
2546 BUG_ON(work_color != work_next_color(cwq->work_color));
2547 cwq->work_color = work_color;
2548 }
2549
Tejun Heod565ed62013-01-24 11:01:33 -08002550 spin_unlock_irq(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002551 }
2552
2553 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2554 complete(&wq->first_flusher->done);
2555
2556 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557}
2558
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002559/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002561 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 *
2563 * Forces execution of the workqueue and blocks until its completion.
2564 * This is typically used in driver shutdown handlers.
2565 *
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002566 * We sleep until all works which were queued on entry have been handled,
2567 * but we are not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002569void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570{
Tejun Heo73f53c42010-06-29 10:07:11 +02002571 struct wq_flusher this_flusher = {
2572 .list = LIST_HEAD_INIT(this_flusher.list),
2573 .flush_color = -1,
2574 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2575 };
2576 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002577
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002578 lock_map_acquire(&wq->lockdep_map);
2579 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002580
2581 mutex_lock(&wq->flush_mutex);
2582
2583 /*
2584 * Start-to-wait phase
2585 */
2586 next_color = work_next_color(wq->work_color);
2587
2588 if (next_color != wq->flush_color) {
2589 /*
2590 * Color space is not full. The current work_color
2591 * becomes our flush_color and work_color is advanced
2592 * by one.
2593 */
2594 BUG_ON(!list_empty(&wq->flusher_overflow));
2595 this_flusher.flush_color = wq->work_color;
2596 wq->work_color = next_color;
2597
2598 if (!wq->first_flusher) {
2599 /* no flush in progress, become the first flusher */
2600 BUG_ON(wq->flush_color != this_flusher.flush_color);
2601
2602 wq->first_flusher = &this_flusher;
2603
2604 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2605 wq->work_color)) {
2606 /* nothing to flush, done */
2607 wq->flush_color = next_color;
2608 wq->first_flusher = NULL;
2609 goto out_unlock;
2610 }
2611 } else {
2612 /* wait in queue */
2613 BUG_ON(wq->flush_color == this_flusher.flush_color);
2614 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2615 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2616 }
2617 } else {
2618 /*
2619 * Oops, color space is full, wait on overflow queue.
2620 * The next flush completion will assign us
2621 * flush_color and transfer to flusher_queue.
2622 */
2623 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2624 }
2625
2626 mutex_unlock(&wq->flush_mutex);
2627
2628 wait_for_completion(&this_flusher.done);
2629
2630 /*
2631 * Wake-up-and-cascade phase
2632 *
2633 * First flushers are responsible for cascading flushes and
2634 * handling overflow. Non-first flushers can simply return.
2635 */
2636 if (wq->first_flusher != &this_flusher)
2637 return;
2638
2639 mutex_lock(&wq->flush_mutex);
2640
Tejun Heo4ce48b32010-07-02 10:03:51 +02002641 /* we might have raced, check again with mutex held */
2642 if (wq->first_flusher != &this_flusher)
2643 goto out_unlock;
2644
Tejun Heo73f53c42010-06-29 10:07:11 +02002645 wq->first_flusher = NULL;
2646
2647 BUG_ON(!list_empty(&this_flusher.list));
2648 BUG_ON(wq->flush_color != this_flusher.flush_color);
2649
2650 while (true) {
2651 struct wq_flusher *next, *tmp;
2652
2653 /* complete all the flushers sharing the current flush color */
2654 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2655 if (next->flush_color != wq->flush_color)
2656 break;
2657 list_del_init(&next->list);
2658 complete(&next->done);
2659 }
2660
2661 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2662 wq->flush_color != work_next_color(wq->work_color));
2663
2664 /* this flush_color is finished, advance by one */
2665 wq->flush_color = work_next_color(wq->flush_color);
2666
2667 /* one color has been freed, handle overflow queue */
2668 if (!list_empty(&wq->flusher_overflow)) {
2669 /*
2670 * Assign the same color to all overflowed
2671 * flushers, advance work_color and append to
2672 * flusher_queue. This is the start-to-wait
2673 * phase for these overflowed flushers.
2674 */
2675 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2676 tmp->flush_color = wq->work_color;
2677
2678 wq->work_color = work_next_color(wq->work_color);
2679
2680 list_splice_tail_init(&wq->flusher_overflow,
2681 &wq->flusher_queue);
2682 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2683 }
2684
2685 if (list_empty(&wq->flusher_queue)) {
2686 BUG_ON(wq->flush_color != wq->work_color);
2687 break;
2688 }
2689
2690 /*
2691 * Need to flush more colors. Make the next flusher
2692 * the new first flusher and arm cwqs.
2693 */
2694 BUG_ON(wq->flush_color == wq->work_color);
2695 BUG_ON(wq->flush_color != next->flush_color);
2696
2697 list_del_init(&next->list);
2698 wq->first_flusher = next;
2699
2700 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2701 break;
2702
2703 /*
2704 * Meh... this color is already done, clear first
2705 * flusher and repeat cascading.
2706 */
2707 wq->first_flusher = NULL;
2708 }
2709
2710out_unlock:
2711 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712}
Dave Jonesae90dd52006-06-30 01:40:45 -04002713EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002715/**
2716 * drain_workqueue - drain a workqueue
2717 * @wq: workqueue to drain
2718 *
2719 * Wait until the workqueue becomes empty. While draining is in progress,
2720 * only chain queueing is allowed. IOW, only currently pending or running
2721 * work items on @wq can queue further work items on it. @wq is flushed
2722 * repeatedly until it becomes empty. The number of flushing is detemined
2723 * by the depth of chaining and should be relatively short. Whine if it
2724 * takes too long.
2725 */
2726void drain_workqueue(struct workqueue_struct *wq)
2727{
2728 unsigned int flush_cnt = 0;
2729 unsigned int cpu;
2730
2731 /*
2732 * __queue_work() needs to test whether there are drainers, is much
2733 * hotter than drain_workqueue() and already looks at @wq->flags.
2734 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers.
2735 */
2736 spin_lock(&workqueue_lock);
2737 if (!wq->nr_drainers++)
2738 wq->flags |= WQ_DRAINING;
2739 spin_unlock(&workqueue_lock);
2740reflush:
2741 flush_workqueue(wq);
2742
2743 for_each_cwq_cpu(cpu, wq) {
2744 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002745 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002746
Tejun Heod565ed62013-01-24 11:01:33 -08002747 spin_lock_irq(&cwq->pool->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002748 drained = !cwq->nr_active && list_empty(&cwq->delayed_works);
Tejun Heod565ed62013-01-24 11:01:33 -08002749 spin_unlock_irq(&cwq->pool->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002750
2751 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002752 continue;
2753
2754 if (++flush_cnt == 10 ||
2755 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
Valentin Ilie044c7822012-08-19 00:52:42 +03002756 pr_warn("workqueue %s: flush on destruction isn't complete after %u tries\n",
2757 wq->name, flush_cnt);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002758 goto reflush;
2759 }
2760
2761 spin_lock(&workqueue_lock);
2762 if (!--wq->nr_drainers)
2763 wq->flags &= ~WQ_DRAINING;
2764 spin_unlock(&workqueue_lock);
2765}
2766EXPORT_SYMBOL_GPL(drain_workqueue);
2767
Tejun Heo606a5022012-08-20 14:51:23 -07002768static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
Tejun Heobaf59022010-09-16 10:42:16 +02002769{
2770 struct worker *worker = NULL;
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002771 struct worker_pool *pool;
Tejun Heobaf59022010-09-16 10:42:16 +02002772 struct cpu_workqueue_struct *cwq;
2773
2774 might_sleep();
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002775 pool = get_work_pool(work);
2776 if (!pool)
Tejun Heobaf59022010-09-16 10:42:16 +02002777 return false;
2778
Tejun Heod565ed62013-01-24 11:01:33 -08002779 spin_lock_irq(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08002780 /* see the comment in try_to_grab_pending() with the same code */
2781 cwq = get_work_cwq(work);
2782 if (cwq) {
2783 if (unlikely(cwq->pool != pool))
Tejun Heobaf59022010-09-16 10:42:16 +02002784 goto already_gone;
Tejun Heo606a5022012-08-20 14:51:23 -07002785 } else {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002786 worker = find_worker_executing_work(pool, work);
Tejun Heobaf59022010-09-16 10:42:16 +02002787 if (!worker)
2788 goto already_gone;
2789 cwq = worker->current_cwq;
Tejun Heo606a5022012-08-20 14:51:23 -07002790 }
Tejun Heobaf59022010-09-16 10:42:16 +02002791
2792 insert_wq_barrier(cwq, barr, work, worker);
Tejun Heod565ed62013-01-24 11:01:33 -08002793 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002794
Tejun Heoe1594892011-01-09 23:32:15 +01002795 /*
2796 * If @max_active is 1 or rescuer is in use, flushing another work
2797 * item on the same workqueue may lead to deadlock. Make sure the
2798 * flusher is not running on the same workqueue by verifying write
2799 * access.
2800 */
2801 if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER)
2802 lock_map_acquire(&cwq->wq->lockdep_map);
2803 else
2804 lock_map_acquire_read(&cwq->wq->lockdep_map);
Tejun Heobaf59022010-09-16 10:42:16 +02002805 lock_map_release(&cwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002806
Tejun Heobaf59022010-09-16 10:42:16 +02002807 return true;
2808already_gone:
Tejun Heod565ed62013-01-24 11:01:33 -08002809 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002810 return false;
2811}
2812
Oleg Nesterovdb700892008-07-25 01:47:49 -07002813/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002814 * flush_work - wait for a work to finish executing the last queueing instance
2815 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002816 *
Tejun Heo606a5022012-08-20 14:51:23 -07002817 * Wait until @work has finished execution. @work is guaranteed to be idle
2818 * on return if it hasn't been requeued since flush started.
Tejun Heo401a8d02010-09-16 10:36:00 +02002819 *
2820 * RETURNS:
2821 * %true if flush_work() waited for the work to finish execution,
2822 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002823 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002824bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002825{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002826 struct wq_barrier barr;
2827
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002828 lock_map_acquire(&work->lockdep_map);
2829 lock_map_release(&work->lockdep_map);
2830
Tejun Heo606a5022012-08-20 14:51:23 -07002831 if (start_flush_work(work, &barr)) {
Tejun Heobaf59022010-09-16 10:42:16 +02002832 wait_for_completion(&barr.done);
2833 destroy_work_on_stack(&barr.work);
2834 return true;
Tejun Heo606a5022012-08-20 14:51:23 -07002835 } else {
Tejun Heobaf59022010-09-16 10:42:16 +02002836 return false;
Tejun Heo606a5022012-08-20 14:51:23 -07002837 }
Oleg Nesterovdb700892008-07-25 01:47:49 -07002838}
2839EXPORT_SYMBOL_GPL(flush_work);
2840
Tejun Heo36e227d2012-08-03 10:30:46 -07002841static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
Tejun Heo401a8d02010-09-16 10:36:00 +02002842{
Tejun Heobbb68df2012-08-03 10:30:46 -07002843 unsigned long flags;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002844 int ret;
2845
2846 do {
Tejun Heobbb68df2012-08-03 10:30:46 -07002847 ret = try_to_grab_pending(work, is_dwork, &flags);
2848 /*
2849 * If someone else is canceling, wait for the same event it
2850 * would be waiting for before retrying.
2851 */
2852 if (unlikely(ret == -ENOENT))
Tejun Heo606a5022012-08-20 14:51:23 -07002853 flush_work(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002854 } while (unlikely(ret < 0));
2855
Tejun Heobbb68df2012-08-03 10:30:46 -07002856 /* tell other tasks trying to grab @work to back off */
2857 mark_work_canceling(work);
2858 local_irq_restore(flags);
2859
Tejun Heo606a5022012-08-20 14:51:23 -07002860 flush_work(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002861 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002862 return ret;
2863}
2864
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002865/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002866 * cancel_work_sync - cancel a work and wait for it to finish
2867 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002868 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002869 * Cancel @work and wait for its execution to finish. This function
2870 * can be used even if the work re-queues itself or migrates to
2871 * another workqueue. On return from this function, @work is
2872 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002873 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002874 * cancel_work_sync(&delayed_work->work) must not be used for
2875 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002876 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002877 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002878 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002879 *
2880 * RETURNS:
2881 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002882 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002883bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002884{
Tejun Heo36e227d2012-08-03 10:30:46 -07002885 return __cancel_work_timer(work, false);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002886}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002887EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002888
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002889/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002890 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2891 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002892 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002893 * Delayed timer is cancelled and the pending work is queued for
2894 * immediate execution. Like flush_work(), this function only
2895 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002896 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002897 * RETURNS:
2898 * %true if flush_work() waited for the work to finish execution,
2899 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002900 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002901bool flush_delayed_work(struct delayed_work *dwork)
2902{
Tejun Heo8930cab2012-08-03 10:30:45 -07002903 local_irq_disable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002904 if (del_timer_sync(&dwork->timer))
Lai Jiangshan60c057b2013-02-06 18:04:53 -08002905 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Tejun Heo8930cab2012-08-03 10:30:45 -07002906 local_irq_enable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002907 return flush_work(&dwork->work);
2908}
2909EXPORT_SYMBOL(flush_delayed_work);
2910
2911/**
Tejun Heo57b30ae2012-08-21 13:18:24 -07002912 * cancel_delayed_work - cancel a delayed work
2913 * @dwork: delayed_work to cancel
Tejun Heo09383492010-09-16 10:48:29 +02002914 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002915 * Kill off a pending delayed_work. Returns %true if @dwork was pending
2916 * and canceled; %false if wasn't pending. Note that the work callback
2917 * function may still be running on return, unless it returns %true and the
2918 * work doesn't re-arm itself. Explicitly flush or use
2919 * cancel_delayed_work_sync() to wait on it.
Tejun Heo09383492010-09-16 10:48:29 +02002920 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002921 * This function is safe to call from any context including IRQ handler.
Tejun Heo09383492010-09-16 10:48:29 +02002922 */
Tejun Heo57b30ae2012-08-21 13:18:24 -07002923bool cancel_delayed_work(struct delayed_work *dwork)
Tejun Heo09383492010-09-16 10:48:29 +02002924{
Tejun Heo57b30ae2012-08-21 13:18:24 -07002925 unsigned long flags;
2926 int ret;
2927
2928 do {
2929 ret = try_to_grab_pending(&dwork->work, true, &flags);
2930 } while (unlikely(ret == -EAGAIN));
2931
2932 if (unlikely(ret < 0))
2933 return false;
2934
Tejun Heo7c3eed52013-01-24 11:01:33 -08002935 set_work_pool_and_clear_pending(&dwork->work,
2936 get_work_pool_id(&dwork->work));
Tejun Heo57b30ae2012-08-21 13:18:24 -07002937 local_irq_restore(flags);
Dan Magenheimerc0158ca2012-10-18 16:31:37 -07002938 return ret;
Tejun Heo09383492010-09-16 10:48:29 +02002939}
Tejun Heo57b30ae2012-08-21 13:18:24 -07002940EXPORT_SYMBOL(cancel_delayed_work);
Tejun Heo09383492010-09-16 10:48:29 +02002941
2942/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002943 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2944 * @dwork: the delayed work cancel
2945 *
2946 * This is cancel_work_sync() for delayed works.
2947 *
2948 * RETURNS:
2949 * %true if @dwork was pending, %false otherwise.
2950 */
2951bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002952{
Tejun Heo36e227d2012-08-03 10:30:46 -07002953 return __cancel_work_timer(&dwork->work, true);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002954}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002955EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002957/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07002958 * schedule_work_on - put work task on a specific cpu
2959 * @cpu: cpu to put the work task on
2960 * @work: job to be done
2961 *
2962 * This puts a job on a specific cpu
2963 */
Tejun Heod4283e92012-08-03 10:30:44 -07002964bool schedule_work_on(int cpu, struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07002965{
Tejun Heod320c032010-06-29 10:07:14 +02002966 return queue_work_on(cpu, system_wq, work);
Zhang Ruic1a220e2008-07-23 21:28:39 -07002967}
2968EXPORT_SYMBOL(schedule_work_on);
2969
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002970/**
Dave Jonesae90dd52006-06-30 01:40:45 -04002971 * schedule_work - put work task in global workqueue
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 * @work: job to be done
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 *
Tejun Heod4283e92012-08-03 10:30:44 -07002974 * Returns %false if @work was already on the kernel-global workqueue and
2975 * %true otherwise.
David Howells52bad642006-11-22 14:54:01 +00002976 *
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002977 * This puts a job in the kernel-global workqueue if it was not already
2978 * queued and leaves it in the same position on the kernel-global
2979 * workqueue otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 */
Tejun Heod4283e92012-08-03 10:30:44 -07002981bool schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982{
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002983 return queue_work(system_wq, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984}
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002985EXPORT_SYMBOL(schedule_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002987/**
2988 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
2989 * @cpu: cpu to use
2990 * @dwork: job to be done
2991 * @delay: number of jiffies to wait
2992 *
2993 * After waiting for a given time this puts a job in the kernel-global
2994 * workqueue on the specified CPU.
2995 */
Tejun Heod4283e92012-08-03 10:30:44 -07002996bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork,
2997 unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998{
Tejun Heod320c032010-06-29 10:07:14 +02002999 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000}
Dave Jonesae90dd52006-06-30 01:40:45 -04003001EXPORT_SYMBOL(schedule_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
Andrew Mortonb6136772006-06-25 05:47:49 -07003003/**
Tejun Heo0a13c002012-08-03 10:30:44 -07003004 * schedule_delayed_work - put work task in global workqueue after delay
3005 * @dwork: job to be done
3006 * @delay: number of jiffies to wait or 0 for immediate execution
3007 *
3008 * After waiting for a given time this puts a job in the kernel-global
3009 * workqueue.
3010 */
Tejun Heod4283e92012-08-03 10:30:44 -07003011bool schedule_delayed_work(struct delayed_work *dwork, unsigned long delay)
Tejun Heo0a13c002012-08-03 10:30:44 -07003012{
3013 return queue_delayed_work(system_wq, dwork, delay);
3014}
3015EXPORT_SYMBOL(schedule_delayed_work);
3016
3017/**
Tejun Heo31ddd872010-10-19 11:14:49 +02003018 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07003019 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07003020 *
Tejun Heo31ddd872010-10-19 11:14:49 +02003021 * schedule_on_each_cpu() executes @func on each online CPU using the
3022 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07003023 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02003024 *
3025 * RETURNS:
3026 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07003027 */
David Howells65f27f32006-11-22 14:55:48 +00003028int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08003029{
3030 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02003031 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08003032
Andrew Mortonb6136772006-06-25 05:47:49 -07003033 works = alloc_percpu(struct work_struct);
3034 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08003035 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07003036
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003037 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08003038
Christoph Lameter15316ba2006-01-08 01:00:43 -08003039 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01003040 struct work_struct *work = per_cpu_ptr(works, cpu);
3041
3042 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003043 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02003044 }
Tejun Heo93981802009-11-17 14:06:20 -08003045
3046 for_each_online_cpu(cpu)
3047 flush_work(per_cpu_ptr(works, cpu));
3048
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003049 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07003050 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08003051 return 0;
3052}
3053
Alan Sterneef6a7d2010-02-12 17:39:21 +09003054/**
3055 * flush_scheduled_work - ensure that any scheduled work has run to completion.
3056 *
3057 * Forces execution of the kernel-global workqueue and blocks until its
3058 * completion.
3059 *
3060 * Think twice before calling this function! It's very easy to get into
3061 * trouble if you don't take great care. Either of the following situations
3062 * will lead to deadlock:
3063 *
3064 * One of the work items currently on the workqueue needs to acquire
3065 * a lock held by your code or its caller.
3066 *
3067 * Your code is running in the context of a work routine.
3068 *
3069 * They will be detected by lockdep when they occur, but the first might not
3070 * occur very often. It depends on what work items are on the workqueue and
3071 * what locks they need, which you have no control over.
3072 *
3073 * In most situations flushing the entire workqueue is overkill; you merely
3074 * need to know that a particular work item isn't queued and isn't running.
3075 * In such cases you should use cancel_delayed_work_sync() or
3076 * cancel_work_sync() instead.
3077 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078void flush_scheduled_work(void)
3079{
Tejun Heod320c032010-06-29 10:07:14 +02003080 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081}
Dave Jonesae90dd52006-06-30 01:40:45 -04003082EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083
3084/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06003085 * execute_in_process_context - reliably execute the routine with user context
3086 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06003087 * @ew: guaranteed storage for the execute work structure (must
3088 * be available when the work executes)
3089 *
3090 * Executes the function immediately if process context is available,
3091 * otherwise schedules the function for delayed execution.
3092 *
3093 * Returns: 0 - function was executed
3094 * 1 - function was scheduled for execution
3095 */
David Howells65f27f32006-11-22 14:55:48 +00003096int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06003097{
3098 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00003099 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003100 return 0;
3101 }
3102
David Howells65f27f32006-11-22 14:55:48 +00003103 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003104 schedule_work(&ew->work);
3105
3106 return 1;
3107}
3108EXPORT_SYMBOL_GPL(execute_in_process_context);
3109
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110int keventd_up(void)
3111{
Tejun Heod320c032010-06-29 10:07:14 +02003112 return system_wq != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113}
3114
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003115static int alloc_cwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116{
Oleg Nesterov3af244332007-05-09 02:34:09 -07003117 /*
Tejun Heo0f900042010-06-29 10:07:11 +02003118 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
3119 * Make sure that the alignment isn't lower than that of
3120 * unsigned long long.
Oleg Nesterov3af244332007-05-09 02:34:09 -07003121 */
Tejun Heo0f900042010-06-29 10:07:11 +02003122 const size_t size = sizeof(struct cpu_workqueue_struct);
3123 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
3124 __alignof__(unsigned long long));
Oleg Nesterov3af244332007-05-09 02:34:09 -07003125
Lai Jiangshane06ffa12012-03-09 18:03:20 +08003126 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02003127 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
Tejun Heo931ac772010-07-20 11:07:48 +02003128 else {
Tejun Heof3421792010-07-02 10:03:51 +02003129 void *ptr;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01003130
Tejun Heof3421792010-07-02 10:03:51 +02003131 /*
3132 * Allocate enough room to align cwq and put an extra
3133 * pointer at the end pointing back to the originally
3134 * allocated pointer which will be used for free.
3135 */
3136 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
3137 if (ptr) {
3138 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
3139 *(void **)(wq->cpu_wq.single + 1) = ptr;
3140 }
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003141 }
Tejun Heof3421792010-07-02 10:03:51 +02003142
Tejun Heo0415b00d12011-03-24 18:50:09 +01003143 /* just in case, make sure it's actually aligned */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003144 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
3145 return wq->cpu_wq.v ? 0 : -ENOMEM;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003146}
3147
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003148static void free_cwqs(struct workqueue_struct *wq)
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07003149{
Lai Jiangshane06ffa12012-03-09 18:03:20 +08003150 if (!(wq->flags & WQ_UNBOUND))
Tejun Heof3421792010-07-02 10:03:51 +02003151 free_percpu(wq->cpu_wq.pcpu);
3152 else if (wq->cpu_wq.single) {
3153 /* the pointer to free is stored right after the cwq */
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003154 kfree(*(void **)(wq->cpu_wq.single + 1));
Oleg Nesterov06ba38a2007-05-09 02:34:15 -07003155 }
3156}
3157
Tejun Heof3421792010-07-02 10:03:51 +02003158static int wq_clamp_max_active(int max_active, unsigned int flags,
3159 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003160{
Tejun Heof3421792010-07-02 10:03:51 +02003161 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3162
3163 if (max_active < 1 || max_active > lim)
Valentin Ilie044c7822012-08-19 00:52:42 +03003164 pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
3165 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003166
Tejun Heof3421792010-07-02 10:03:51 +02003167 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003168}
3169
Tejun Heob196be82012-01-10 15:11:35 -08003170struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02003171 unsigned int flags,
3172 int max_active,
3173 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08003174 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003175{
Tejun Heob196be82012-01-10 15:11:35 -08003176 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003177 struct workqueue_struct *wq;
Tejun Heoc34056a2010-06-29 10:07:11 +02003178 unsigned int cpu;
Tejun Heob196be82012-01-10 15:11:35 -08003179 size_t namelen;
3180
3181 /* determine namelen, allocate wq and format name */
3182 va_start(args, lock_name);
3183 va_copy(args1, args);
3184 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3185
3186 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3187 if (!wq)
3188 goto err;
3189
3190 vsnprintf(wq->name, namelen, fmt, args1);
3191 va_end(args);
3192 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003193
Tejun Heof3421792010-07-02 10:03:51 +02003194 /*
Tejun Heo6370a6a2010-10-11 15:12:27 +02003195 * Workqueues which may be used during memory reclaim should
3196 * have a rescuer to guarantee forward progress.
3197 */
3198 if (flags & WQ_MEM_RECLAIM)
3199 flags |= WQ_RESCUER;
3200
Tejun Heod320c032010-06-29 10:07:14 +02003201 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003202 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003203
Tejun Heob196be82012-01-10 15:11:35 -08003204 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003205 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003206 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003207 mutex_init(&wq->flush_mutex);
3208 atomic_set(&wq->nr_cwqs_to_flush, 0);
3209 INIT_LIST_HEAD(&wq->flusher_queue);
3210 INIT_LIST_HEAD(&wq->flusher_overflow);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003211
Johannes Bergeb13ba82008-01-16 09:51:58 +01003212 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003213 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003214
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003215 if (alloc_cwqs(wq) < 0)
3216 goto err;
3217
Tejun Heof3421792010-07-02 10:03:51 +02003218 for_each_cwq_cpu(cpu, wq) {
Tejun Heo15376632010-06-29 10:07:11 +02003219 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3220
Tejun Heo0f900042010-06-29 10:07:11 +02003221 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
Tejun Heoa60dc392013-01-24 11:01:34 -08003222 cwq->pool = get_std_worker_pool(cpu, flags & WQ_HIGHPRI);
Tejun Heoc34056a2010-06-29 10:07:11 +02003223 cwq->wq = wq;
Tejun Heo73f53c42010-06-29 10:07:11 +02003224 cwq->flush_color = -1;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003225 cwq->max_active = max_active;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003226 INIT_LIST_HEAD(&cwq->delayed_works);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003227 }
3228
Tejun Heoe22bee72010-06-29 10:07:14 +02003229 if (flags & WQ_RESCUER) {
3230 struct worker *rescuer;
3231
Tejun Heof2e005a2010-07-20 15:59:09 +02003232 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL))
Tejun Heoe22bee72010-06-29 10:07:14 +02003233 goto err;
3234
3235 wq->rescuer = rescuer = alloc_worker();
3236 if (!rescuer)
3237 goto err;
3238
Tejun Heo111c2252013-01-17 17:16:24 -08003239 rescuer->rescue_wq = wq;
3240 rescuer->task = kthread_create(rescuer_thread, rescuer, "%s",
Tejun Heob196be82012-01-10 15:11:35 -08003241 wq->name);
Tejun Heoe22bee72010-06-29 10:07:14 +02003242 if (IS_ERR(rescuer->task))
3243 goto err;
3244
Tejun Heoe22bee72010-06-29 10:07:14 +02003245 rescuer->task->flags |= PF_THREAD_BOUND;
3246 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003247 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003248
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003249 /*
3250 * workqueue_lock protects global freeze state and workqueues
3251 * list. Grab it, set max_active accordingly and add the new
3252 * workqueue to workqueues list.
3253 */
Tejun Heo15376632010-06-29 10:07:11 +02003254 spin_lock(&workqueue_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003255
Tejun Heo58a69cb2011-02-16 09:25:31 +01003256 if (workqueue_freezing && wq->flags & WQ_FREEZABLE)
Tejun Heof3421792010-07-02 10:03:51 +02003257 for_each_cwq_cpu(cpu, wq)
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003258 get_cwq(cpu, wq)->max_active = 0;
3259
Tejun Heo15376632010-06-29 10:07:11 +02003260 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003261
Tejun Heo15376632010-06-29 10:07:11 +02003262 spin_unlock(&workqueue_lock);
3263
Oleg Nesterov3af244332007-05-09 02:34:09 -07003264 return wq;
Tejun Heo4690c4a2010-06-29 10:07:10 +02003265err:
3266 if (wq) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003267 free_cwqs(wq);
Tejun Heof2e005a2010-07-20 15:59:09 +02003268 free_mayday_mask(wq->mayday_mask);
Tejun Heoe22bee72010-06-29 10:07:14 +02003269 kfree(wq->rescuer);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003270 kfree(wq);
3271 }
3272 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003273}
Tejun Heod320c032010-06-29 10:07:14 +02003274EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003275
3276/**
3277 * destroy_workqueue - safely terminate a workqueue
3278 * @wq: target workqueue
3279 *
3280 * Safely destroy a workqueue. All work currently pending will be done first.
3281 */
3282void destroy_workqueue(struct workqueue_struct *wq)
3283{
Tejun Heoc8e55f32010-06-29 10:07:12 +02003284 unsigned int cpu;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003285
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003286 /* drain it before proceeding with destruction */
3287 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003288
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003289 /*
3290 * wq list is used to freeze wq, remove from list after
3291 * flushing is complete in case freeze races us.
3292 */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003293 spin_lock(&workqueue_lock);
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07003294 list_del(&wq->list);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01003295 spin_unlock(&workqueue_lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003296
Tejun Heoe22bee72010-06-29 10:07:14 +02003297 /* sanity check */
Tejun Heof3421792010-07-02 10:03:51 +02003298 for_each_cwq_cpu(cpu, wq) {
Tejun Heo73f53c42010-06-29 10:07:11 +02003299 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3300 int i;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003301
Tejun Heo73f53c42010-06-29 10:07:11 +02003302 for (i = 0; i < WORK_NR_COLORS; i++)
3303 BUG_ON(cwq->nr_in_flight[i]);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02003304 BUG_ON(cwq->nr_active);
3305 BUG_ON(!list_empty(&cwq->delayed_works));
Tejun Heo73f53c42010-06-29 10:07:11 +02003306 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003307
Tejun Heoe22bee72010-06-29 10:07:14 +02003308 if (wq->flags & WQ_RESCUER) {
3309 kthread_stop(wq->rescuer->task);
Tejun Heof2e005a2010-07-20 15:59:09 +02003310 free_mayday_mask(wq->mayday_mask);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003311 kfree(wq->rescuer);
Tejun Heoe22bee72010-06-29 10:07:14 +02003312 }
3313
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003314 free_cwqs(wq);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003315 kfree(wq);
3316}
3317EXPORT_SYMBOL_GPL(destroy_workqueue);
3318
Tejun Heodcd989c2010-06-29 10:07:14 +02003319/**
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003320 * cwq_set_max_active - adjust max_active of a cwq
3321 * @cwq: target cpu_workqueue_struct
3322 * @max_active: new max_active value.
3323 *
3324 * Set @cwq->max_active to @max_active and activate delayed works if
3325 * increased.
3326 *
3327 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08003328 * spin_lock_irq(pool->lock).
Lai Jiangshan9f4bd4c2012-09-19 10:40:48 -07003329 */
3330static void cwq_set_max_active(struct cpu_workqueue_struct *cwq, int max_active)
3331{
3332 cwq->max_active = max_active;
3333
3334 while (!list_empty(&cwq->delayed_works) &&
3335 cwq->nr_active < cwq->max_active)
3336 cwq_activate_first_delayed(cwq);
3337}
3338
3339/**
Tejun Heodcd989c2010-06-29 10:07:14 +02003340 * workqueue_set_max_active - adjust max_active of a workqueue
3341 * @wq: target workqueue
3342 * @max_active: new max_active value.
3343 *
3344 * Set max_active of @wq to @max_active.
3345 *
3346 * CONTEXT:
3347 * Don't call from IRQ context.
3348 */
3349void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3350{
3351 unsigned int cpu;
3352
Tejun Heof3421792010-07-02 10:03:51 +02003353 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003354
3355 spin_lock(&workqueue_lock);
3356
3357 wq->saved_max_active = max_active;
3358
Tejun Heof3421792010-07-02 10:03:51 +02003359 for_each_cwq_cpu(cpu, wq) {
Tejun Heo35b6bb62013-01-24 11:01:33 -08003360 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3361 struct worker_pool *pool = cwq->pool;
Tejun Heodcd989c2010-06-29 10:07:14 +02003362
Tejun Heod565ed62013-01-24 11:01:33 -08003363 spin_lock_irq(&pool->lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02003364
Tejun Heo58a69cb2011-02-16 09:25:31 +01003365 if (!(wq->flags & WQ_FREEZABLE) ||
Tejun Heo35b6bb62013-01-24 11:01:33 -08003366 !(pool->flags & POOL_FREEZING))
3367 cwq_set_max_active(cwq, max_active);
Tejun Heodcd989c2010-06-29 10:07:14 +02003368
Tejun Heod565ed62013-01-24 11:01:33 -08003369 spin_unlock_irq(&pool->lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02003370 }
3371
3372 spin_unlock(&workqueue_lock);
3373}
3374EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3375
3376/**
3377 * workqueue_congested - test whether a workqueue is congested
3378 * @cpu: CPU in question
3379 * @wq: target workqueue
3380 *
3381 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3382 * no synchronization around this function and the test result is
3383 * unreliable and only useful as advisory hints or for debugging.
3384 *
3385 * RETURNS:
3386 * %true if congested, %false otherwise.
3387 */
3388bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
3389{
3390 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3391
3392 return !list_empty(&cwq->delayed_works);
3393}
3394EXPORT_SYMBOL_GPL(workqueue_congested);
3395
3396/**
Tejun Heodcd989c2010-06-29 10:07:14 +02003397 * work_busy - test whether a work is currently pending or running
3398 * @work: the work to be tested
3399 *
3400 * Test whether @work is currently pending or running. There is no
3401 * synchronization around this function and the test result is
3402 * unreliable and only useful as advisory hints or for debugging.
Tejun Heodcd989c2010-06-29 10:07:14 +02003403 *
3404 * RETURNS:
3405 * OR'd bitmask of WORK_BUSY_* bits.
3406 */
3407unsigned int work_busy(struct work_struct *work)
3408{
Tejun Heoc9e7cf22013-01-24 11:01:33 -08003409 struct worker_pool *pool = get_work_pool(work);
Tejun Heodcd989c2010-06-29 10:07:14 +02003410 unsigned long flags;
3411 unsigned int ret = 0;
3412
Tejun Heodcd989c2010-06-29 10:07:14 +02003413 if (work_pending(work))
3414 ret |= WORK_BUSY_PENDING;
Tejun Heodcd989c2010-06-29 10:07:14 +02003415
Lai Jiangshan038366c2013-02-06 18:04:53 -08003416 if (pool) {
3417 spin_lock_irqsave(&pool->lock, flags);
3418 if (find_worker_executing_work(pool, work))
3419 ret |= WORK_BUSY_RUNNING;
3420 spin_unlock_irqrestore(&pool->lock, flags);
3421 }
Tejun Heodcd989c2010-06-29 10:07:14 +02003422
3423 return ret;
3424}
3425EXPORT_SYMBOL_GPL(work_busy);
3426
Tejun Heodb7bccf2010-06-29 10:07:12 +02003427/*
3428 * CPU hotplug.
3429 *
Tejun Heoe22bee72010-06-29 10:07:14 +02003430 * There are two challenges in supporting CPU hotplug. Firstly, there
3431 * are a lot of assumptions on strong associations among work, cwq and
Tejun Heo706026c2013-01-24 11:01:34 -08003432 * pool which make migrating pending and scheduled works very
Tejun Heoe22bee72010-06-29 10:07:14 +02003433 * difficult to implement without impacting hot paths. Secondly,
Tejun Heo94cf58b2013-01-24 11:01:33 -08003434 * worker pools serve mix of short, long and very long running works making
Tejun Heoe22bee72010-06-29 10:07:14 +02003435 * blocked draining impractical.
3436 *
Tejun Heo24647572013-01-24 11:01:33 -08003437 * This is solved by allowing the pools to be disassociated from the CPU
Tejun Heo628c78e2012-07-17 12:39:27 -07003438 * running as an unbound one and allowing it to be reattached later if the
3439 * cpu comes back online.
Tejun Heodb7bccf2010-06-29 10:07:12 +02003440 */
3441
Tejun Heo706026c2013-01-24 11:01:34 -08003442static void wq_unbind_fn(struct work_struct *work)
Tejun Heodb7bccf2010-06-29 10:07:12 +02003443{
Tejun Heo38db41d2013-01-24 11:01:34 -08003444 int cpu = smp_processor_id();
Tejun Heo4ce62e92012-07-13 22:16:44 -07003445 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003446 struct worker *worker;
3447 struct hlist_node *pos;
3448 int i;
3449
Tejun Heo38db41d2013-01-24 11:01:34 -08003450 for_each_std_worker_pool(pool, cpu) {
3451 BUG_ON(cpu != smp_processor_id());
Tejun Heo94cf58b2013-01-24 11:01:33 -08003452
3453 mutex_lock(&pool->assoc_mutex);
3454 spin_lock_irq(&pool->lock);
3455
3456 /*
3457 * We've claimed all manager positions. Make all workers
3458 * unbound and set DISASSOCIATED. Before this, all workers
3459 * except for the ones which are still executing works from
3460 * before the last CPU down must be on the cpu. After
3461 * this, they may become diasporas.
3462 */
Tejun Heo4ce62e92012-07-13 22:16:44 -07003463 list_for_each_entry(worker, &pool->idle_list, entry)
Tejun Heo403c8212012-07-17 12:39:27 -07003464 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003465
Tejun Heoc9e7cf22013-01-24 11:01:33 -08003466 for_each_busy_worker(worker, i, pos, pool)
3467 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003468
Tejun Heo24647572013-01-24 11:01:33 -08003469 pool->flags |= POOL_DISASSOCIATED;
Tejun Heof2d5a0e2012-07-17 12:39:26 -07003470
Tejun Heo94cf58b2013-01-24 11:01:33 -08003471 spin_unlock_irq(&pool->lock);
3472 mutex_unlock(&pool->assoc_mutex);
3473 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003474
3475 /*
Tejun Heo628c78e2012-07-17 12:39:27 -07003476 * Call schedule() so that we cross rq->lock and thus can guarantee
3477 * sched callbacks see the %WORKER_UNBOUND flag. This is necessary
3478 * as scheduler callbacks may be invoked from other cpus.
3479 */
3480 schedule();
3481
3482 /*
3483 * Sched callbacks are disabled now. Zap nr_running. After this,
3484 * nr_running stays zero and need_more_worker() and keep_working()
Tejun Heo38db41d2013-01-24 11:01:34 -08003485 * are always true as long as the worklist is not empty. Pools on
3486 * @cpu now behave as unbound (in terms of concurrency management)
3487 * pools which are served by workers tied to the CPU.
Tejun Heo628c78e2012-07-17 12:39:27 -07003488 *
3489 * On return from this function, the current worker would trigger
3490 * unbound chain execution of pending work items if other workers
3491 * didn't already.
Tejun Heoe22bee72010-06-29 10:07:14 +02003492 */
Tejun Heo38db41d2013-01-24 11:01:34 -08003493 for_each_std_worker_pool(pool, cpu)
Tejun Heoe19e3972013-01-24 11:39:44 -08003494 atomic_set(&pool->nr_running, 0);
Tejun Heodb7bccf2010-06-29 10:07:12 +02003495}
3496
Tejun Heo8db25e72012-07-17 12:39:28 -07003497/*
3498 * Workqueues should be brought up before normal priority CPU notifiers.
3499 * This will be registered high priority CPU notifier.
3500 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07003501static int __cpuinit workqueue_cpu_up_callback(struct notifier_block *nfb,
Tejun Heo8db25e72012-07-17 12:39:28 -07003502 unsigned long action,
3503 void *hcpu)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003504{
3505 unsigned int cpu = (unsigned long)hcpu;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003506 struct worker_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507
Tejun Heo8db25e72012-07-17 12:39:28 -07003508 switch (action & ~CPU_TASKS_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 case CPU_UP_PREPARE:
Tejun Heo38db41d2013-01-24 11:01:34 -08003510 for_each_std_worker_pool(pool, cpu) {
Tejun Heo3ce63372012-07-17 12:39:27 -07003511 struct worker *worker;
3512
3513 if (pool->nr_workers)
3514 continue;
3515
3516 worker = create_worker(pool);
3517 if (!worker)
3518 return NOTIFY_BAD;
3519
Tejun Heod565ed62013-01-24 11:01:33 -08003520 spin_lock_irq(&pool->lock);
Tejun Heo3ce63372012-07-17 12:39:27 -07003521 start_worker(worker);
Tejun Heod565ed62013-01-24 11:01:33 -08003522 spin_unlock_irq(&pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02003524 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07003525
Tejun Heo65758202012-07-17 12:39:26 -07003526 case CPU_DOWN_FAILED:
3527 case CPU_ONLINE:
Tejun Heo38db41d2013-01-24 11:01:34 -08003528 for_each_std_worker_pool(pool, cpu) {
Tejun Heo94cf58b2013-01-24 11:01:33 -08003529 mutex_lock(&pool->assoc_mutex);
3530 spin_lock_irq(&pool->lock);
3531
Tejun Heo24647572013-01-24 11:01:33 -08003532 pool->flags &= ~POOL_DISASSOCIATED;
Tejun Heo94cf58b2013-01-24 11:01:33 -08003533 rebind_workers(pool);
3534
3535 spin_unlock_irq(&pool->lock);
3536 mutex_unlock(&pool->assoc_mutex);
3537 }
Tejun Heo8db25e72012-07-17 12:39:28 -07003538 break;
Tejun Heo65758202012-07-17 12:39:26 -07003539 }
3540 return NOTIFY_OK;
3541}
3542
3543/*
3544 * Workqueues should be brought down after normal priority CPU notifiers.
3545 * This will be registered as low priority CPU notifier.
3546 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07003547static int __cpuinit workqueue_cpu_down_callback(struct notifier_block *nfb,
Tejun Heo65758202012-07-17 12:39:26 -07003548 unsigned long action,
3549 void *hcpu)
3550{
Tejun Heo8db25e72012-07-17 12:39:28 -07003551 unsigned int cpu = (unsigned long)hcpu;
3552 struct work_struct unbind_work;
3553
Tejun Heo65758202012-07-17 12:39:26 -07003554 switch (action & ~CPU_TASKS_FROZEN) {
3555 case CPU_DOWN_PREPARE:
Tejun Heo8db25e72012-07-17 12:39:28 -07003556 /* unbinding should happen on the local CPU */
Tejun Heo706026c2013-01-24 11:01:34 -08003557 INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn);
Joonsoo Kim7635d2f2012-08-15 23:25:41 +09003558 queue_work_on(cpu, system_highpri_wq, &unbind_work);
Tejun Heo8db25e72012-07-17 12:39:28 -07003559 flush_work(&unbind_work);
3560 break;
Tejun Heo65758202012-07-17 12:39:26 -07003561 }
3562 return NOTIFY_OK;
3563}
3564
Rusty Russell2d3854a2008-11-05 13:39:10 +11003565#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08003566
Rusty Russell2d3854a2008-11-05 13:39:10 +11003567struct work_for_cpu {
Tejun Heoed48ece2012-09-18 12:48:43 -07003568 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11003569 long (*fn)(void *);
3570 void *arg;
3571 long ret;
3572};
3573
Tejun Heoed48ece2012-09-18 12:48:43 -07003574static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11003575{
Tejun Heoed48ece2012-09-18 12:48:43 -07003576 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
3577
Rusty Russell2d3854a2008-11-05 13:39:10 +11003578 wfc->ret = wfc->fn(wfc->arg);
3579}
3580
3581/**
3582 * work_on_cpu - run a function in user context on a particular cpu
3583 * @cpu: the cpu to run on
3584 * @fn: the function to run
3585 * @arg: the function arg
3586 *
Rusty Russell31ad9082009-01-16 15:31:15 -08003587 * This will return the value @fn returns.
3588 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06003589 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11003590 */
3591long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3592{
Tejun Heoed48ece2012-09-18 12:48:43 -07003593 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11003594
Tejun Heoed48ece2012-09-18 12:48:43 -07003595 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
3596 schedule_work_on(cpu, &wfc.work);
3597 flush_work(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11003598 return wfc.ret;
3599}
3600EXPORT_SYMBOL_GPL(work_on_cpu);
3601#endif /* CONFIG_SMP */
3602
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003603#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10303604
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003605/**
3606 * freeze_workqueues_begin - begin freezing workqueues
3607 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01003608 * Start freezing workqueues. After this function returns, all freezable
3609 * workqueues will queue new works to their frozen_works list instead of
Tejun Heo706026c2013-01-24 11:01:34 -08003610 * pool->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003611 *
3612 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08003613 * Grabs and releases workqueue_lock and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003614 */
3615void freeze_workqueues_begin(void)
3616{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003617 unsigned int cpu;
3618
3619 spin_lock(&workqueue_lock);
3620
3621 BUG_ON(workqueue_freezing);
3622 workqueue_freezing = true;
3623
Tejun Heo706026c2013-01-24 11:01:34 -08003624 for_each_wq_cpu(cpu) {
Tejun Heo35b6bb62013-01-24 11:01:33 -08003625 struct worker_pool *pool;
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003626 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003627
Tejun Heo38db41d2013-01-24 11:01:34 -08003628 for_each_std_worker_pool(pool, cpu) {
Tejun Heoa1056302013-01-24 11:01:33 -08003629 spin_lock_irq(&pool->lock);
Tejun Heod565ed62013-01-24 11:01:33 -08003630
Tejun Heo35b6bb62013-01-24 11:01:33 -08003631 WARN_ON_ONCE(pool->flags & POOL_FREEZING);
3632 pool->flags |= POOL_FREEZING;
Tejun Heoa1056302013-01-24 11:01:33 -08003633
3634 list_for_each_entry(wq, &workqueues, list) {
3635 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3636
3637 if (cwq && cwq->pool == pool &&
3638 (wq->flags & WQ_FREEZABLE))
3639 cwq->max_active = 0;
3640 }
3641
3642 spin_unlock_irq(&pool->lock);
Tejun Heo35b6bb62013-01-24 11:01:33 -08003643 }
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003644 }
3645
3646 spin_unlock(&workqueue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003648
3649/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01003650 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003651 *
3652 * Check whether freezing is complete. This function must be called
3653 * between freeze_workqueues_begin() and thaw_workqueues().
3654 *
3655 * CONTEXT:
3656 * Grabs and releases workqueue_lock.
3657 *
3658 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01003659 * %true if some freezable workqueues are still busy. %false if freezing
3660 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003661 */
3662bool freeze_workqueues_busy(void)
3663{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003664 unsigned int cpu;
3665 bool busy = false;
3666
3667 spin_lock(&workqueue_lock);
3668
3669 BUG_ON(!workqueue_freezing);
3670
Tejun Heo706026c2013-01-24 11:01:34 -08003671 for_each_wq_cpu(cpu) {
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003672 struct workqueue_struct *wq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003673 /*
3674 * nr_active is monotonically decreasing. It's safe
3675 * to peek without lock.
3676 */
3677 list_for_each_entry(wq, &workqueues, list) {
3678 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3679
Tejun Heo58a69cb2011-02-16 09:25:31 +01003680 if (!cwq || !(wq->flags & WQ_FREEZABLE))
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003681 continue;
3682
3683 BUG_ON(cwq->nr_active < 0);
3684 if (cwq->nr_active) {
3685 busy = true;
3686 goto out_unlock;
3687 }
3688 }
3689 }
3690out_unlock:
3691 spin_unlock(&workqueue_lock);
3692 return busy;
3693}
3694
3695/**
3696 * thaw_workqueues - thaw workqueues
3697 *
3698 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo706026c2013-01-24 11:01:34 -08003699 * frozen works are transferred to their respective pool worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003700 *
3701 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08003702 * Grabs and releases workqueue_lock and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003703 */
3704void thaw_workqueues(void)
3705{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003706 unsigned int cpu;
3707
3708 spin_lock(&workqueue_lock);
3709
3710 if (!workqueue_freezing)
3711 goto out_unlock;
3712
Tejun Heo706026c2013-01-24 11:01:34 -08003713 for_each_wq_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07003714 struct worker_pool *pool;
Tejun Heobdbc5dd2010-07-02 10:03:51 +02003715 struct workqueue_struct *wq;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003716
Tejun Heo38db41d2013-01-24 11:01:34 -08003717 for_each_std_worker_pool(pool, cpu) {
Tejun Heoa1056302013-01-24 11:01:33 -08003718 spin_lock_irq(&pool->lock);
Tejun Heod565ed62013-01-24 11:01:33 -08003719
Tejun Heo35b6bb62013-01-24 11:01:33 -08003720 WARN_ON_ONCE(!(pool->flags & POOL_FREEZING));
3721 pool->flags &= ~POOL_FREEZING;
Tejun Heodb7bccf2010-06-29 10:07:12 +02003722
Tejun Heoa1056302013-01-24 11:01:33 -08003723 list_for_each_entry(wq, &workqueues, list) {
3724 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003725
Tejun Heoa1056302013-01-24 11:01:33 -08003726 if (!cwq || cwq->pool != pool ||
3727 !(wq->flags & WQ_FREEZABLE))
3728 continue;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003729
Tejun Heoa1056302013-01-24 11:01:33 -08003730 /* restore max_active and repopulate worklist */
3731 cwq_set_max_active(cwq, wq->saved_max_active);
3732 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003733
Tejun Heo4ce62e92012-07-13 22:16:44 -07003734 wake_up_worker(pool);
Tejun Heoa1056302013-01-24 11:01:33 -08003735
3736 spin_unlock_irq(&pool->lock);
Tejun Heod565ed62013-01-24 11:01:33 -08003737 }
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003738 }
3739
3740 workqueue_freezing = false;
3741out_unlock:
3742 spin_unlock(&workqueue_lock);
3743}
3744#endif /* CONFIG_FREEZER */
3745
Suresh Siddha6ee05782010-07-30 14:57:37 -07003746static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747{
Tejun Heoc34056a2010-06-29 10:07:11 +02003748 unsigned int cpu;
3749
Tejun Heo7c3eed52013-01-24 11:01:33 -08003750 /* make sure we have enough bits for OFFQ pool ID */
3751 BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) <
Lai Jiangshan6be19582013-02-06 18:04:53 -08003752 WORK_CPU_END * NR_STD_WORKER_POOLS);
Tejun Heob5490072012-08-03 10:30:46 -07003753
Tejun Heo65758202012-07-17 12:39:26 -07003754 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
Lai Jiangshana5b4e572012-09-18 09:59:23 -07003755 hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02003756
Tejun Heo706026c2013-01-24 11:01:34 -08003757 /* initialize CPU pools */
3758 for_each_wq_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07003759 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02003760
Tejun Heo38db41d2013-01-24 11:01:34 -08003761 for_each_std_worker_pool(pool, cpu) {
Tejun Heod565ed62013-01-24 11:01:33 -08003762 spin_lock_init(&pool->lock);
Tejun Heoec22ca52013-01-24 11:01:33 -08003763 pool->cpu = cpu;
Tejun Heo24647572013-01-24 11:01:33 -08003764 pool->flags |= POOL_DISASSOCIATED;
Tejun Heo4ce62e92012-07-13 22:16:44 -07003765 INIT_LIST_HEAD(&pool->worklist);
3766 INIT_LIST_HEAD(&pool->idle_list);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08003767 hash_init(pool->busy_hash);
Tejun Heoe22bee72010-06-29 10:07:14 +02003768
Tejun Heo4ce62e92012-07-13 22:16:44 -07003769 init_timer_deferrable(&pool->idle_timer);
3770 pool->idle_timer.function = idle_worker_timeout;
3771 pool->idle_timer.data = (unsigned long)pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003772
Tejun Heo706026c2013-01-24 11:01:34 -08003773 setup_timer(&pool->mayday_timer, pool_mayday_timeout,
Tejun Heo4ce62e92012-07-13 22:16:44 -07003774 (unsigned long)pool);
3775
Lai Jiangshanb2eb83d2012-09-18 09:59:23 -07003776 mutex_init(&pool->assoc_mutex);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003777 ida_init(&pool->worker_ida);
Tejun Heo9daf9e62013-01-24 11:01:33 -08003778
3779 /* alloc pool ID */
3780 BUG_ON(worker_pool_assign_id(pool));
Tejun Heo4ce62e92012-07-13 22:16:44 -07003781 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02003782 }
3783
Tejun Heoe22bee72010-06-29 10:07:14 +02003784 /* create the initial worker */
Tejun Heo706026c2013-01-24 11:01:34 -08003785 for_each_online_wq_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07003786 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02003787
Tejun Heo38db41d2013-01-24 11:01:34 -08003788 for_each_std_worker_pool(pool, cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07003789 struct worker *worker;
3790
Tejun Heo24647572013-01-24 11:01:33 -08003791 if (cpu != WORK_CPU_UNBOUND)
3792 pool->flags &= ~POOL_DISASSOCIATED;
3793
Tejun Heobc2ae0f2012-07-17 12:39:27 -07003794 worker = create_worker(pool);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003795 BUG_ON(!worker);
Tejun Heod565ed62013-01-24 11:01:33 -08003796 spin_lock_irq(&pool->lock);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003797 start_worker(worker);
Tejun Heod565ed62013-01-24 11:01:33 -08003798 spin_unlock_irq(&pool->lock);
Tejun Heo4ce62e92012-07-13 22:16:44 -07003799 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003800 }
3801
Tejun Heod320c032010-06-29 10:07:14 +02003802 system_wq = alloc_workqueue("events", 0, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09003803 system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
Tejun Heod320c032010-06-29 10:07:14 +02003804 system_long_wq = alloc_workqueue("events_long", 0, 0);
Tejun Heof3421792010-07-02 10:03:51 +02003805 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3806 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01003807 system_freezable_wq = alloc_workqueue("events_freezable",
3808 WQ_FREEZABLE, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09003809 BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
Tejun Heoae930e02012-08-20 14:51:23 -07003810 !system_unbound_wq || !system_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07003811 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812}
Suresh Siddha6ee05782010-07-30 14:57:37 -07003813early_initcall(init_workqueues);