blob: 89480fc8eaa3e50cff15f0e20a20bb427908c2ce [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tejun Heoc54fce62010-09-10 16:51:36 +02002 * kernel/workqueue.c - generic async execution with shared worker pool
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Tejun Heoc54fce62010-09-10 16:51:36 +02004 * Copyright (C) 2002 Ingo Molnar
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Tejun Heoc54fce62010-09-10 16:51:36 +02006 * Derived from the taskqueue/keventd code by:
7 * David Woodhouse <dwmw2@infradead.org>
8 * Andrew Morton
9 * Kai Petzke <wpp@marie.physik.tu-berlin.de>
10 * Theodore Ts'o <tytso@mit.edu>
Christoph Lameter89ada672005-10-30 15:01:59 -080011 *
Christoph Lametercde53532008-07-04 09:59:22 -070012 * Made to use alloc_percpu by Christoph Lameter.
Tejun Heoc54fce62010-09-10 16:51:36 +020013 *
14 * Copyright (C) 2010 SUSE Linux Products GmbH
15 * Copyright (C) 2010 Tejun Heo <tj@kernel.org>
16 *
17 * This is the generic async execution mechanism. Work items as are
18 * executed in process context. The worker pool is shared and
19 * automatically managed. There is one worker pool for each CPU and
20 * one extra for works which are better served by workers which are
21 * not bound to any specific CPU.
22 *
23 * Please read Documentation/workqueue.txt for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 */
25
Paul Gortmaker9984de12011-05-23 14:51:41 -040026#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/kernel.h>
28#include <linux/sched.h>
29#include <linux/init.h>
30#include <linux/signal.h>
31#include <linux/completion.h>
32#include <linux/workqueue.h>
33#include <linux/slab.h>
34#include <linux/cpu.h>
35#include <linux/notifier.h>
36#include <linux/kthread.h>
James Bottomley1fa44ec2006-02-23 12:43:43 -060037#include <linux/hardirq.h>
Christoph Lameter46934022006-10-11 01:21:26 -070038#include <linux/mempolicy.h>
Rafael J. Wysocki341a5952006-12-06 20:34:49 -080039#include <linux/freezer.h>
Peter Zijlstrad5abe662006-12-06 20:37:26 -080040#include <linux/kallsyms.h>
41#include <linux/debug_locks.h>
Johannes Berg4e6045f2007-10-18 23:39:55 -070042#include <linux/lockdep.h>
Tejun Heoc34056a2010-06-29 10:07:11 +020043#include <linux/idr.h>
Tejun Heo29c91e92013-03-12 11:30:03 -070044#include <linux/jhash.h>
Sasha Levin42f85702012-12-17 10:01:23 -050045#include <linux/hashtable.h>
Tejun Heo76af4d92013-03-12 11:30:00 -070046#include <linux/rculist.h>
Tejun Heoe22bee72010-06-29 10:07:14 +020047
Tejun Heoea138442013-01-18 14:05:55 -080048#include "workqueue_internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Tejun Heoc8e55f32010-06-29 10:07:12 +020050enum {
Tejun Heobc2ae0f2012-07-17 12:39:27 -070051 /*
Tejun Heo24647572013-01-24 11:01:33 -080052 * worker_pool flags
Tejun Heobc2ae0f2012-07-17 12:39:27 -070053 *
Tejun Heo24647572013-01-24 11:01:33 -080054 * A bound pool is either associated or disassociated with its CPU.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070055 * While associated (!DISASSOCIATED), all workers are bound to the
56 * CPU and none has %WORKER_UNBOUND set and concurrency management
57 * is in effect.
58 *
59 * While DISASSOCIATED, the cpu may be offline and all workers have
60 * %WORKER_UNBOUND set and concurrency management disabled, and may
Tejun Heo24647572013-01-24 11:01:33 -080061 * be executing on any CPU. The pool behaves as an unbound one.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070062 *
Tejun Heobc3a1af2013-03-13 19:47:39 -070063 * Note that DISASSOCIATED should be flipped only while holding
64 * manager_mutex to avoid changing binding state while
Tejun Heo24647572013-01-24 11:01:33 -080065 * create_worker() is in progress.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070066 */
Tejun Heo11ebea52012-07-12 14:46:37 -070067 POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
Tejun Heo24647572013-01-24 11:01:33 -080068 POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */
Tejun Heo35b6bb62013-01-24 11:01:33 -080069 POOL_FREEZING = 1 << 3, /* freeze in progress */
Tejun Heodb7bccf2010-06-29 10:07:12 +020070
Tejun Heoc8e55f32010-06-29 10:07:12 +020071 /* worker flags */
72 WORKER_STARTED = 1 << 0, /* started */
73 WORKER_DIE = 1 << 1, /* die die die */
74 WORKER_IDLE = 1 << 2, /* is idle */
Tejun Heoe22bee72010-06-29 10:07:14 +020075 WORKER_PREP = 1 << 3, /* preparing to run works */
Tejun Heofb0e7be2010-06-29 10:07:15 +020076 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
Tejun Heof3421792010-07-02 10:03:51 +020077 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
Tejun Heoa9ab7752013-03-19 13:45:21 -070078 WORKER_REBOUND = 1 << 8, /* worker was rebound */
Tejun Heoe22bee72010-06-29 10:07:14 +020079
Tejun Heoa9ab7752013-03-19 13:45:21 -070080 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_CPU_INTENSIVE |
81 WORKER_UNBOUND | WORKER_REBOUND,
Tejun Heodb7bccf2010-06-29 10:07:12 +020082
Tejun Heoe34cdddb2013-01-24 11:01:33 -080083 NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */
Tejun Heo4ce62e92012-07-13 22:16:44 -070084
Tejun Heo29c91e92013-03-12 11:30:03 -070085 UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */
Tejun Heoc8e55f32010-06-29 10:07:12 +020086 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
Tejun Heodb7bccf2010-06-29 10:07:12 +020087
Tejun Heoe22bee72010-06-29 10:07:14 +020088 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
89 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
90
Tejun Heo3233cdb2011-02-16 18:10:19 +010091 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
92 /* call for help after 10ms
93 (min two ticks) */
Tejun Heoe22bee72010-06-29 10:07:14 +020094 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
95 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heoe22bee72010-06-29 10:07:14 +020096
97 /*
98 * Rescue workers are used only on emergencies and shared by
99 * all cpus. Give -20.
100 */
101 RESCUER_NICE_LEVEL = -20,
Tejun Heo32704762012-07-13 22:16:45 -0700102 HIGHPRI_NICE_LEVEL = -20,
Tejun Heoc8e55f32010-06-29 10:07:12 +0200103};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105/*
Tejun Heo4690c4a2010-06-29 10:07:10 +0200106 * Structure fields follow one of the following exclusion rules.
107 *
Tejun Heoe41e7042010-08-24 14:22:47 +0200108 * I: Modifiable by initialization/destruction paths and read-only for
109 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200110 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200111 * P: Preemption protected. Disabling preemption is enough and should
112 * only be modified and accessed from the local cpu.
113 *
Tejun Heod565ed62013-01-24 11:01:33 -0800114 * L: pool->lock protected. Access with pool->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200115 *
Tejun Heod565ed62013-01-24 11:01:33 -0800116 * X: During normal operation, modification requires pool->lock and should
117 * be done only from local cpu. Either disabling preemption on local
118 * cpu or grabbing pool->lock is enough for read access. If
119 * POOL_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200120 *
Tejun Heo822d8402013-03-19 13:45:21 -0700121 * MG: pool->manager_mutex and pool->lock protected. Writes require both
122 * locks. Reads can happen under either lock.
123 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700124 * PL: wq_pool_mutex protected.
Tejun Heo76af4d92013-03-12 11:30:00 -0700125 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700126 * PR: wq_pool_mutex protected for writes. Sched-RCU protected for reads.
Tejun Heo5bcab332013-03-13 19:47:40 -0700127 *
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700128 * WQ: wq->mutex protected.
129 *
Lai Jiangshanb5927602013-03-25 16:57:19 -0700130 * WR: wq->mutex protected for writes. Sched-RCU protected for reads.
Tejun Heo2e109a22013-03-13 19:47:40 -0700131 *
132 * MD: wq_mayday_lock protected.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200133 */
134
Tejun Heo2eaebdb2013-01-18 14:05:55 -0800135/* struct worker is defined in workqueue_internal.h */
Tejun Heoc34056a2010-06-29 10:07:11 +0200136
Tejun Heobd7bdd42012-07-12 14:46:37 -0700137struct worker_pool {
Tejun Heod565ed62013-01-24 11:01:33 -0800138 spinlock_t lock; /* the pool lock */
Tejun Heod84ff052013-03-12 11:29:59 -0700139 int cpu; /* I: the associated cpu */
Tejun Heo9daf9e62013-01-24 11:01:33 -0800140 int id; /* I: pool ID */
Tejun Heo11ebea52012-07-12 14:46:37 -0700141 unsigned int flags; /* X: flags */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700142
143 struct list_head worklist; /* L: list of pending works */
144 int nr_workers; /* L: total number of workers */
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700145
146 /* nr_idle includes the ones off idle_list for rebinding */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700147 int nr_idle; /* L: currently idle ones */
148
149 struct list_head idle_list; /* X: list of idle workers */
150 struct timer_list idle_timer; /* L: worker idle timeout */
151 struct timer_list mayday_timer; /* L: SOS timer for workers */
152
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700153 /* a workers is either on busy_hash or idle_list, or the manager */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800154 DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER);
155 /* L: hash of busy workers */
156
Tejun Heobc3a1af2013-03-13 19:47:39 -0700157 /* see manage_workers() for details on the two manager mutexes */
Tejun Heo34a06bd2013-03-12 11:30:00 -0700158 struct mutex manager_arb; /* manager arbitration */
Tejun Heobc3a1af2013-03-13 19:47:39 -0700159 struct mutex manager_mutex; /* manager exclusion */
Tejun Heo822d8402013-03-19 13:45:21 -0700160 struct idr worker_idr; /* MG: worker IDs and iteration */
Tejun Heoe19e3972013-01-24 11:39:44 -0800161
Tejun Heo7a4e3442013-03-12 11:30:00 -0700162 struct workqueue_attrs *attrs; /* I: worker attributes */
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700163 struct hlist_node hash_node; /* PL: unbound_pool_hash node */
164 int refcnt; /* PL: refcnt for unbound pools */
Tejun Heo7a4e3442013-03-12 11:30:00 -0700165
Tejun Heoe19e3972013-01-24 11:39:44 -0800166 /*
167 * The current concurrency level. As it's likely to be accessed
168 * from other CPUs during try_to_wake_up(), put it in a separate
169 * cacheline.
170 */
171 atomic_t nr_running ____cacheline_aligned_in_smp;
Tejun Heo29c91e92013-03-12 11:30:03 -0700172
173 /*
174 * Destruction of pool is sched-RCU protected to allow dereferences
175 * from get_work_pool().
176 */
177 struct rcu_head rcu;
Tejun Heo8b03ae32010-06-29 10:07:12 +0200178} ____cacheline_aligned_in_smp;
179
180/*
Tejun Heo112202d2013-02-13 19:29:12 -0800181 * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS
182 * of work_struct->data are used for flags and the remaining high bits
183 * point to the pwq; thus, pwqs need to be aligned at two's power of the
184 * number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 */
Tejun Heo112202d2013-02-13 19:29:12 -0800186struct pool_workqueue {
Tejun Heobd7bdd42012-07-12 14:46:37 -0700187 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200188 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200189 int work_color; /* L: current color */
190 int flush_color; /* L: flushing color */
Tejun Heo8864b4e2013-03-12 11:30:04 -0700191 int refcnt; /* L: reference count */
Tejun Heo73f53c42010-06-29 10:07:11 +0200192 int nr_in_flight[WORK_NR_COLORS];
193 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200194 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200195 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200196 struct list_head delayed_works; /* L: delayed works */
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700197 struct list_head pwqs_node; /* WR: node on wq->pwqs */
Tejun Heo2e109a22013-03-13 19:47:40 -0700198 struct list_head mayday_node; /* MD: node on wq->maydays */
Tejun Heo8864b4e2013-03-12 11:30:04 -0700199
200 /*
201 * Release of unbound pwq is punted to system_wq. See put_pwq()
202 * and pwq_unbound_release_workfn() for details. pool_workqueue
203 * itself is also sched-RCU protected so that the first pwq can be
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700204 * determined without grabbing wq->mutex.
Tejun Heo8864b4e2013-03-12 11:30:04 -0700205 */
206 struct work_struct unbound_release_work;
207 struct rcu_head rcu;
Tejun Heoe904e6c2013-03-12 11:29:57 -0700208} __aligned(1 << WORK_STRUCT_FLAG_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200211 * Structure used to wait for workqueue flush.
212 */
213struct wq_flusher {
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700214 struct list_head list; /* WQ: list of flushers */
215 int flush_color; /* WQ: flush color waiting for */
Tejun Heo73f53c42010-06-29 10:07:11 +0200216 struct completion done; /* flush completion */
217};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Tejun Heo226223a2013-03-12 11:30:05 -0700219struct wq_device;
220
Tejun Heo73f53c42010-06-29 10:07:11 +0200221/*
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700222 * The externally visible workqueue. It relays the issued work items to
223 * the appropriate worker_pool through its pool_workqueues.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 */
225struct workqueue_struct {
Lai Jiangshan87fc7412013-03-25 16:57:18 -0700226 unsigned int flags; /* WQ: WQ_* flags */
Tejun Heo420c0dd2013-03-12 11:29:59 -0700227 struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwq's */
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700228 struct list_head pwqs; /* WR: all pwqs of this wq */
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700229 struct list_head list; /* PL: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200230
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700231 struct mutex mutex; /* protects this wq */
232 int work_color; /* WQ: current work color */
233 int flush_color; /* WQ: current flush color */
Tejun Heo112202d2013-02-13 19:29:12 -0800234 atomic_t nr_pwqs_to_flush; /* flush in progress */
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700235 struct wq_flusher *first_flusher; /* WQ: first flusher */
236 struct list_head flusher_queue; /* WQ: flush waiters */
237 struct list_head flusher_overflow; /* WQ: flush overflow list */
Tejun Heo73f53c42010-06-29 10:07:11 +0200238
Tejun Heo2e109a22013-03-13 19:47:40 -0700239 struct list_head maydays; /* MD: pwqs requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200240 struct worker *rescuer; /* I: rescue worker */
241
Lai Jiangshan87fc7412013-03-25 16:57:18 -0700242 int nr_drainers; /* WQ: drain in progress */
Lai Jiangshana357fc02013-03-25 16:57:19 -0700243 int saved_max_active; /* WQ: saved pwq max_active */
Tejun Heo226223a2013-03-12 11:30:05 -0700244
245#ifdef CONFIG_SYSFS
246 struct wq_device *wq_dev; /* I: for sysfs interface */
247#endif
Johannes Berg4e6045f2007-10-18 23:39:55 -0700248#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200249 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700250#endif
Tejun Heob196be82012-01-10 15:11:35 -0800251 char name[]; /* I: workqueue name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252};
253
Tejun Heoe904e6c2013-03-12 11:29:57 -0700254static struct kmem_cache *pwq_cache;
255
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700256static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */
Tejun Heo2e109a22013-03-13 19:47:40 -0700257static DEFINE_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */
Tejun Heo5bcab332013-03-13 19:47:40 -0700258
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700259static LIST_HEAD(workqueues); /* PL: list of all workqueues */
260static bool workqueue_freezing; /* PL: have wqs started freezing? */
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700261
262/* the per-cpu worker pools */
263static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS],
264 cpu_worker_pools);
265
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700266static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700267
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700268/* PL: hash of all unbound pools keyed by pool->attrs */
Tejun Heo29c91e92013-03-12 11:30:03 -0700269static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER);
270
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700271/* I: attributes used when instantiating standard unbound pools on demand */
Tejun Heo29c91e92013-03-12 11:30:03 -0700272static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS];
273
Tejun Heod320c032010-06-29 10:07:14 +0200274struct workqueue_struct *system_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200275EXPORT_SYMBOL_GPL(system_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300276struct workqueue_struct *system_highpri_wq __read_mostly;
Joonsoo Kim1aabe902012-08-15 23:25:39 +0900277EXPORT_SYMBOL_GPL(system_highpri_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300278struct workqueue_struct *system_long_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200279EXPORT_SYMBOL_GPL(system_long_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300280struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200281EXPORT_SYMBOL_GPL(system_unbound_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300282struct workqueue_struct *system_freezable_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100283EXPORT_SYMBOL_GPL(system_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200284
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700285static int worker_thread(void *__worker);
286static void copy_workqueue_attrs(struct workqueue_attrs *to,
287 const struct workqueue_attrs *from);
288
Tejun Heo97bd2342010-10-05 10:41:14 +0200289#define CREATE_TRACE_POINTS
290#include <trace/events/workqueue.h>
291
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700292#define assert_rcu_or_pool_mutex() \
Tejun Heo5bcab332013-03-13 19:47:40 -0700293 rcu_lockdep_assert(rcu_read_lock_sched_held() || \
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700294 lockdep_is_held(&wq_pool_mutex), \
295 "sched RCU or wq_pool_mutex should be held")
Tejun Heo5bcab332013-03-13 19:47:40 -0700296
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700297#define assert_rcu_or_wq_mutex(wq) \
Tejun Heo76af4d92013-03-12 11:30:00 -0700298 rcu_lockdep_assert(rcu_read_lock_sched_held() || \
Lai Jiangshanb5927602013-03-25 16:57:19 -0700299 lockdep_is_held(&wq->mutex), \
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700300 "sched RCU or wq->mutex should be held")
Tejun Heo76af4d92013-03-12 11:30:00 -0700301
Tejun Heo822d8402013-03-19 13:45:21 -0700302#ifdef CONFIG_LOCKDEP
303#define assert_manager_or_pool_lock(pool) \
Lai Jiangshan519e3c12013-03-20 03:28:21 +0800304 WARN_ONCE(debug_locks && \
305 !lockdep_is_held(&(pool)->manager_mutex) && \
Tejun Heo822d8402013-03-19 13:45:21 -0700306 !lockdep_is_held(&(pool)->lock), \
307 "pool->manager_mutex or ->lock should be held")
308#else
309#define assert_manager_or_pool_lock(pool) do { } while (0)
310#endif
311
Tejun Heof02ae732013-03-12 11:30:03 -0700312#define for_each_cpu_worker_pool(pool, cpu) \
313 for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \
314 (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \
Tejun Heo7a62c2c2013-03-12 11:30:03 -0700315 (pool)++)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700316
Tejun Heo49e3cf42013-03-12 11:29:58 -0700317/**
Tejun Heo17116962013-03-12 11:29:58 -0700318 * for_each_pool - iterate through all worker_pools in the system
319 * @pool: iteration cursor
Tejun Heo611c92a2013-03-13 16:51:36 -0700320 * @pi: integer used for iteration
Tejun Heofa1b54e2013-03-12 11:30:00 -0700321 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700322 * This must be called either with wq_pool_mutex held or sched RCU read
323 * locked. If the pool needs to be used beyond the locking in effect, the
324 * caller is responsible for guaranteeing that the pool stays online.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700325 *
326 * The if/else clause exists only for the lockdep assertion and can be
327 * ignored.
Tejun Heo17116962013-03-12 11:29:58 -0700328 */
Tejun Heo611c92a2013-03-13 16:51:36 -0700329#define for_each_pool(pool, pi) \
330 idr_for_each_entry(&worker_pool_idr, pool, pi) \
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700331 if (({ assert_rcu_or_pool_mutex(); false; })) { } \
Tejun Heofa1b54e2013-03-12 11:30:00 -0700332 else
Tejun Heo17116962013-03-12 11:29:58 -0700333
334/**
Tejun Heo822d8402013-03-19 13:45:21 -0700335 * for_each_pool_worker - iterate through all workers of a worker_pool
336 * @worker: iteration cursor
337 * @wi: integer used for iteration
338 * @pool: worker_pool to iterate workers of
339 *
340 * This must be called with either @pool->manager_mutex or ->lock held.
341 *
342 * The if/else clause exists only for the lockdep assertion and can be
343 * ignored.
344 */
345#define for_each_pool_worker(worker, wi, pool) \
346 idr_for_each_entry(&(pool)->worker_idr, (worker), (wi)) \
347 if (({ assert_manager_or_pool_lock((pool)); false; })) { } \
348 else
349
350/**
Tejun Heo49e3cf42013-03-12 11:29:58 -0700351 * for_each_pwq - iterate through all pool_workqueues of the specified workqueue
352 * @pwq: iteration cursor
353 * @wq: the target workqueue
Tejun Heo76af4d92013-03-12 11:30:00 -0700354 *
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700355 * This must be called either with wq->mutex held or sched RCU read locked.
Tejun Heo794b18b2013-03-13 19:47:40 -0700356 * If the pwq needs to be used beyond the locking in effect, the caller is
357 * responsible for guaranteeing that the pwq stays online.
Tejun Heo76af4d92013-03-12 11:30:00 -0700358 *
359 * The if/else clause exists only for the lockdep assertion and can be
360 * ignored.
Tejun Heo49e3cf42013-03-12 11:29:58 -0700361 */
362#define for_each_pwq(pwq, wq) \
Tejun Heo76af4d92013-03-12 11:30:00 -0700363 list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node) \
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700364 if (({ assert_rcu_or_wq_mutex(wq); false; })) { } \
Tejun Heo76af4d92013-03-12 11:30:00 -0700365 else
Tejun Heof3421792010-07-02 10:03:51 +0200366
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900367#ifdef CONFIG_DEBUG_OBJECTS_WORK
368
369static struct debug_obj_descr work_debug_descr;
370
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100371static void *work_debug_hint(void *addr)
372{
373 return ((struct work_struct *) addr)->func;
374}
375
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900376/*
377 * fixup_init is called when:
378 * - an active object is initialized
379 */
380static int work_fixup_init(void *addr, enum debug_obj_state state)
381{
382 struct work_struct *work = addr;
383
384 switch (state) {
385 case ODEBUG_STATE_ACTIVE:
386 cancel_work_sync(work);
387 debug_object_init(work, &work_debug_descr);
388 return 1;
389 default:
390 return 0;
391 }
392}
393
394/*
395 * fixup_activate is called when:
396 * - an active object is activated
397 * - an unknown object is activated (might be a statically initialized object)
398 */
399static int work_fixup_activate(void *addr, enum debug_obj_state state)
400{
401 struct work_struct *work = addr;
402
403 switch (state) {
404
405 case ODEBUG_STATE_NOTAVAILABLE:
406 /*
407 * This is not really a fixup. The work struct was
408 * statically initialized. We just make sure that it
409 * is tracked in the object tracker.
410 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200411 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900412 debug_object_init(work, &work_debug_descr);
413 debug_object_activate(work, &work_debug_descr);
414 return 0;
415 }
416 WARN_ON_ONCE(1);
417 return 0;
418
419 case ODEBUG_STATE_ACTIVE:
420 WARN_ON(1);
421
422 default:
423 return 0;
424 }
425}
426
427/*
428 * fixup_free is called when:
429 * - an active object is freed
430 */
431static int work_fixup_free(void *addr, enum debug_obj_state state)
432{
433 struct work_struct *work = addr;
434
435 switch (state) {
436 case ODEBUG_STATE_ACTIVE:
437 cancel_work_sync(work);
438 debug_object_free(work, &work_debug_descr);
439 return 1;
440 default:
441 return 0;
442 }
443}
444
445static struct debug_obj_descr work_debug_descr = {
446 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100447 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900448 .fixup_init = work_fixup_init,
449 .fixup_activate = work_fixup_activate,
450 .fixup_free = work_fixup_free,
451};
452
453static inline void debug_work_activate(struct work_struct *work)
454{
455 debug_object_activate(work, &work_debug_descr);
456}
457
458static inline void debug_work_deactivate(struct work_struct *work)
459{
460 debug_object_deactivate(work, &work_debug_descr);
461}
462
463void __init_work(struct work_struct *work, int onstack)
464{
465 if (onstack)
466 debug_object_init_on_stack(work, &work_debug_descr);
467 else
468 debug_object_init(work, &work_debug_descr);
469}
470EXPORT_SYMBOL_GPL(__init_work);
471
472void destroy_work_on_stack(struct work_struct *work)
473{
474 debug_object_free(work, &work_debug_descr);
475}
476EXPORT_SYMBOL_GPL(destroy_work_on_stack);
477
478#else
479static inline void debug_work_activate(struct work_struct *work) { }
480static inline void debug_work_deactivate(struct work_struct *work) { }
481#endif
482
Tejun Heo9daf9e62013-01-24 11:01:33 -0800483/* allocate ID and assign it to @pool */
484static int worker_pool_assign_id(struct worker_pool *pool)
485{
486 int ret;
487
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700488 lockdep_assert_held(&wq_pool_mutex);
Tejun Heo5bcab332013-03-13 19:47:40 -0700489
Tejun Heofa1b54e2013-03-12 11:30:00 -0700490 do {
491 if (!idr_pre_get(&worker_pool_idr, GFP_KERNEL))
492 return -ENOMEM;
Tejun Heofa1b54e2013-03-12 11:30:00 -0700493 ret = idr_get_new(&worker_pool_idr, pool, &pool->id);
Tejun Heofa1b54e2013-03-12 11:30:00 -0700494 } while (ret == -EAGAIN);
Tejun Heo9daf9e62013-01-24 11:01:33 -0800495
496 return ret;
497}
498
Tejun Heo76af4d92013-03-12 11:30:00 -0700499/**
500 * first_pwq - return the first pool_workqueue of the specified workqueue
501 * @wq: the target workqueue
502 *
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700503 * This must be called either with wq->mutex held or sched RCU read locked.
Tejun Heo794b18b2013-03-13 19:47:40 -0700504 * If the pwq needs to be used beyond the locking in effect, the caller is
505 * responsible for guaranteeing that the pwq stays online.
Tejun Heo76af4d92013-03-12 11:30:00 -0700506 */
Tejun Heo7fb98ea2013-03-12 11:30:00 -0700507static struct pool_workqueue *first_pwq(struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700508{
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700509 assert_rcu_or_wq_mutex(wq);
Tejun Heo76af4d92013-03-12 11:30:00 -0700510 return list_first_or_null_rcu(&wq->pwqs, struct pool_workqueue,
511 pwqs_node);
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700512}
513
Tejun Heo73f53c42010-06-29 10:07:11 +0200514static unsigned int work_color_to_flags(int color)
515{
516 return color << WORK_STRUCT_COLOR_SHIFT;
517}
518
519static int get_work_color(struct work_struct *work)
520{
521 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
522 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
523}
524
525static int work_next_color(int color)
526{
527 return (color + 1) % WORK_NR_COLORS;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700528}
529
David Howells4594bf12006-12-07 11:33:26 +0000530/*
Tejun Heo112202d2013-02-13 19:29:12 -0800531 * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data
532 * contain the pointer to the queued pwq. Once execution starts, the flag
Tejun Heo7c3eed52013-01-24 11:01:33 -0800533 * is cleared and the high bits contain OFFQ flags and pool ID.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200534 *
Tejun Heo112202d2013-02-13 19:29:12 -0800535 * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling()
536 * and clear_work_data() can be used to set the pwq, pool or clear
Tejun Heobbb68df2012-08-03 10:30:46 -0700537 * work->data. These functions should only be called while the work is
538 * owned - ie. while the PENDING bit is set.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200539 *
Tejun Heo112202d2013-02-13 19:29:12 -0800540 * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq
Tejun Heo7c3eed52013-01-24 11:01:33 -0800541 * corresponding to a work. Pool is available once the work has been
Tejun Heo112202d2013-02-13 19:29:12 -0800542 * queued anywhere after initialization until it is sync canceled. pwq is
Tejun Heo7c3eed52013-01-24 11:01:33 -0800543 * available only while the work item is queued.
Tejun Heobbb68df2012-08-03 10:30:46 -0700544 *
545 * %WORK_OFFQ_CANCELING is used to mark a work item which is being
546 * canceled. While being canceled, a work item may have its PENDING set
547 * but stay off timer and worklist for arbitrarily long and nobody should
548 * try to steal the PENDING bit.
David Howells4594bf12006-12-07 11:33:26 +0000549 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200550static inline void set_work_data(struct work_struct *work, unsigned long data,
551 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000552{
Tejun Heo6183c002013-03-12 11:29:57 -0700553 WARN_ON_ONCE(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200554 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000555}
David Howells365970a2006-11-22 14:54:49 +0000556
Tejun Heo112202d2013-02-13 19:29:12 -0800557static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq,
Tejun Heo7a22ad72010-06-29 10:07:13 +0200558 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200559{
Tejun Heo112202d2013-02-13 19:29:12 -0800560 set_work_data(work, (unsigned long)pwq,
561 WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200562}
563
Lai Jiangshan4468a002013-02-06 18:04:53 -0800564static void set_work_pool_and_keep_pending(struct work_struct *work,
565 int pool_id)
566{
567 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT,
568 WORK_STRUCT_PENDING);
569}
570
Tejun Heo7c3eed52013-01-24 11:01:33 -0800571static void set_work_pool_and_clear_pending(struct work_struct *work,
572 int pool_id)
David Howells365970a2006-11-22 14:54:49 +0000573{
Tejun Heo23657bb2012-08-13 17:08:19 -0700574 /*
575 * The following wmb is paired with the implied mb in
576 * test_and_set_bit(PENDING) and ensures all updates to @work made
577 * here are visible to and precede any updates by the next PENDING
578 * owner.
579 */
580 smp_wmb();
Tejun Heo7c3eed52013-01-24 11:01:33 -0800581 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200582}
583
584static void clear_work_data(struct work_struct *work)
585{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800586 smp_wmb(); /* see set_work_pool_and_clear_pending() */
587 set_work_data(work, WORK_STRUCT_NO_POOL, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200588}
589
Tejun Heo112202d2013-02-13 19:29:12 -0800590static struct pool_workqueue *get_work_pwq(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200591{
Tejun Heoe1201532010-07-22 14:14:25 +0200592 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200593
Tejun Heo112202d2013-02-13 19:29:12 -0800594 if (data & WORK_STRUCT_PWQ)
Tejun Heoe1201532010-07-22 14:14:25 +0200595 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
596 else
597 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200598}
599
Tejun Heo7c3eed52013-01-24 11:01:33 -0800600/**
601 * get_work_pool - return the worker_pool a given work was associated with
602 * @work: the work item of interest
603 *
604 * Return the worker_pool @work was last associated with. %NULL if none.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700605 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700606 * Pools are created and destroyed under wq_pool_mutex, and allows read
607 * access under sched-RCU read lock. As such, this function should be
608 * called under wq_pool_mutex or with preemption disabled.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700609 *
610 * All fields of the returned pool are accessible as long as the above
611 * mentioned locking is in effect. If the returned pool needs to be used
612 * beyond the critical section, the caller is responsible for ensuring the
613 * returned pool is and stays online.
Tejun Heo7c3eed52013-01-24 11:01:33 -0800614 */
615static struct worker_pool *get_work_pool(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200616{
Tejun Heoe1201532010-07-22 14:14:25 +0200617 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800618 int pool_id;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200619
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700620 assert_rcu_or_pool_mutex();
Tejun Heofa1b54e2013-03-12 11:30:00 -0700621
Tejun Heo112202d2013-02-13 19:29:12 -0800622 if (data & WORK_STRUCT_PWQ)
623 return ((struct pool_workqueue *)
Tejun Heo7c3eed52013-01-24 11:01:33 -0800624 (data & WORK_STRUCT_WQ_DATA_MASK))->pool;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200625
Tejun Heo7c3eed52013-01-24 11:01:33 -0800626 pool_id = data >> WORK_OFFQ_POOL_SHIFT;
627 if (pool_id == WORK_OFFQ_POOL_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200628 return NULL;
629
Tejun Heofa1b54e2013-03-12 11:30:00 -0700630 return idr_find(&worker_pool_idr, pool_id);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800631}
632
633/**
634 * get_work_pool_id - return the worker pool ID a given work is associated with
635 * @work: the work item of interest
636 *
637 * Return the worker_pool ID @work was last associated with.
638 * %WORK_OFFQ_POOL_NONE if none.
639 */
640static int get_work_pool_id(struct work_struct *work)
641{
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800642 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800643
Tejun Heo112202d2013-02-13 19:29:12 -0800644 if (data & WORK_STRUCT_PWQ)
645 return ((struct pool_workqueue *)
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800646 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id;
647
648 return data >> WORK_OFFQ_POOL_SHIFT;
Tejun Heo7c3eed52013-01-24 11:01:33 -0800649}
650
Tejun Heobbb68df2012-08-03 10:30:46 -0700651static void mark_work_canceling(struct work_struct *work)
652{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800653 unsigned long pool_id = get_work_pool_id(work);
Tejun Heobbb68df2012-08-03 10:30:46 -0700654
Tejun Heo7c3eed52013-01-24 11:01:33 -0800655 pool_id <<= WORK_OFFQ_POOL_SHIFT;
656 set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700657}
658
659static bool work_is_canceling(struct work_struct *work)
660{
661 unsigned long data = atomic_long_read(&work->data);
662
Tejun Heo112202d2013-02-13 19:29:12 -0800663 return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700664}
665
David Howells365970a2006-11-22 14:54:49 +0000666/*
Tejun Heo32704762012-07-13 22:16:45 -0700667 * Policy functions. These define the policies on how the global worker
668 * pools are managed. Unless noted otherwise, these functions assume that
Tejun Heod565ed62013-01-24 11:01:33 -0800669 * they're being called with pool->lock held.
David Howells365970a2006-11-22 14:54:49 +0000670 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200671
Tejun Heo63d95a92012-07-12 14:46:37 -0700672static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000673{
Tejun Heoe19e3972013-01-24 11:39:44 -0800674 return !atomic_read(&pool->nr_running);
David Howells365970a2006-11-22 14:54:49 +0000675}
676
Tejun Heoe22bee72010-06-29 10:07:14 +0200677/*
678 * Need to wake up a worker? Called from anything but currently
679 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700680 *
681 * Note that, because unbound workers never contribute to nr_running, this
Tejun Heo706026c2013-01-24 11:01:34 -0800682 * function will always return %true for unbound pools as long as the
Tejun Heo974271c2012-07-12 14:46:37 -0700683 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200684 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700685static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000686{
Tejun Heo63d95a92012-07-12 14:46:37 -0700687 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000688}
689
Tejun Heoe22bee72010-06-29 10:07:14 +0200690/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700691static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200692{
Tejun Heo63d95a92012-07-12 14:46:37 -0700693 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200694}
695
696/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700697static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200698{
Tejun Heoe19e3972013-01-24 11:39:44 -0800699 return !list_empty(&pool->worklist) &&
700 atomic_read(&pool->nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200701}
702
703/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700704static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200705{
Tejun Heo63d95a92012-07-12 14:46:37 -0700706 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200707}
708
709/* Do I need to be the manager? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700710static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200711{
Tejun Heo63d95a92012-07-12 14:46:37 -0700712 return need_to_create_worker(pool) ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700713 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200714}
715
716/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700717static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200718{
Tejun Heo34a06bd2013-03-12 11:30:00 -0700719 bool managing = mutex_is_locked(&pool->manager_arb);
Tejun Heo63d95a92012-07-12 14:46:37 -0700720 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
721 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200722
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700723 /*
724 * nr_idle and idle_list may disagree if idle rebinding is in
725 * progress. Never return %true if idle_list is empty.
726 */
727 if (list_empty(&pool->idle_list))
728 return false;
729
Tejun Heoe22bee72010-06-29 10:07:14 +0200730 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
731}
732
733/*
734 * Wake up functions.
735 */
736
Tejun Heo7e116292010-06-29 10:07:13 +0200737/* Return the first worker. Safe with preemption disabled */
Tejun Heo63d95a92012-07-12 14:46:37 -0700738static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200739{
Tejun Heo63d95a92012-07-12 14:46:37 -0700740 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200741 return NULL;
742
Tejun Heo63d95a92012-07-12 14:46:37 -0700743 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200744}
745
746/**
747 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700748 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200749 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700750 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200751 *
752 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800753 * spin_lock_irq(pool->lock).
Tejun Heo7e116292010-06-29 10:07:13 +0200754 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700755static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200756{
Tejun Heo63d95a92012-07-12 14:46:37 -0700757 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200758
759 if (likely(worker))
760 wake_up_process(worker->task);
761}
762
Tejun Heo4690c4a2010-06-29 10:07:10 +0200763/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200764 * wq_worker_waking_up - a worker is waking up
765 * @task: task waking up
766 * @cpu: CPU @task is waking up to
767 *
768 * This function is called during try_to_wake_up() when a worker is
769 * being awoken.
770 *
771 * CONTEXT:
772 * spin_lock_irq(rq->lock)
773 */
Tejun Heod84ff052013-03-12 11:29:59 -0700774void wq_worker_waking_up(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200775{
776 struct worker *worker = kthread_data(task);
777
Joonsoo Kim36576002012-10-26 23:03:49 +0900778 if (!(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoec22ca52013-01-24 11:01:33 -0800779 WARN_ON_ONCE(worker->pool->cpu != cpu);
Tejun Heoe19e3972013-01-24 11:39:44 -0800780 atomic_inc(&worker->pool->nr_running);
Joonsoo Kim36576002012-10-26 23:03:49 +0900781 }
Tejun Heoe22bee72010-06-29 10:07:14 +0200782}
783
784/**
785 * wq_worker_sleeping - a worker is going to sleep
786 * @task: task going to sleep
787 * @cpu: CPU in question, must be the current CPU number
788 *
789 * This function is called during schedule() when a busy worker is
790 * going to sleep. Worker on the same cpu can be woken up by
791 * returning pointer to its task.
792 *
793 * CONTEXT:
794 * spin_lock_irq(rq->lock)
795 *
796 * RETURNS:
797 * Worker task on @cpu to wake up, %NULL if none.
798 */
Tejun Heod84ff052013-03-12 11:29:59 -0700799struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200800{
801 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heo111c2252013-01-17 17:16:24 -0800802 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200803
Tejun Heo111c2252013-01-17 17:16:24 -0800804 /*
805 * Rescuers, which may not have all the fields set up like normal
806 * workers, also reach here, let's not access anything before
807 * checking NOT_RUNNING.
808 */
Steven Rostedt2d646722010-12-03 23:12:33 -0500809 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200810 return NULL;
811
Tejun Heo111c2252013-01-17 17:16:24 -0800812 pool = worker->pool;
Tejun Heo111c2252013-01-17 17:16:24 -0800813
Tejun Heoe22bee72010-06-29 10:07:14 +0200814 /* this can only happen on the local cpu */
Tejun Heo6183c002013-03-12 11:29:57 -0700815 if (WARN_ON_ONCE(cpu != raw_smp_processor_id()))
816 return NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +0200817
818 /*
819 * The counterpart of the following dec_and_test, implied mb,
820 * worklist not empty test sequence is in insert_work().
821 * Please read comment there.
822 *
Tejun Heo628c78e2012-07-17 12:39:27 -0700823 * NOT_RUNNING is clear. This means that we're bound to and
824 * running on the local cpu w/ rq lock held and preemption
825 * disabled, which in turn means that none else could be
Tejun Heod565ed62013-01-24 11:01:33 -0800826 * manipulating idle_list, so dereferencing idle_list without pool
Tejun Heo628c78e2012-07-17 12:39:27 -0700827 * lock is safe.
Tejun Heoe22bee72010-06-29 10:07:14 +0200828 */
Tejun Heoe19e3972013-01-24 11:39:44 -0800829 if (atomic_dec_and_test(&pool->nr_running) &&
830 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700831 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200832 return to_wakeup ? to_wakeup->task : NULL;
833}
834
835/**
836 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200837 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200838 * @flags: flags to set
839 * @wakeup: wakeup an idle worker if necessary
840 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200841 * Set @flags in @worker->flags and adjust nr_running accordingly. If
842 * nr_running becomes zero and @wakeup is %true, an idle worker is
843 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200844 *
Tejun Heocb444762010-07-02 10:03:50 +0200845 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800846 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200847 */
848static inline void worker_set_flags(struct worker *worker, unsigned int flags,
849 bool wakeup)
850{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700851 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200852
Tejun Heocb444762010-07-02 10:03:50 +0200853 WARN_ON_ONCE(worker->task != current);
854
Tejun Heoe22bee72010-06-29 10:07:14 +0200855 /*
856 * If transitioning into NOT_RUNNING, adjust nr_running and
857 * wake up an idle worker as necessary if requested by
858 * @wakeup.
859 */
860 if ((flags & WORKER_NOT_RUNNING) &&
861 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoe22bee72010-06-29 10:07:14 +0200862 if (wakeup) {
Tejun Heoe19e3972013-01-24 11:39:44 -0800863 if (atomic_dec_and_test(&pool->nr_running) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -0700864 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700865 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200866 } else
Tejun Heoe19e3972013-01-24 11:39:44 -0800867 atomic_dec(&pool->nr_running);
Tejun Heoe22bee72010-06-29 10:07:14 +0200868 }
869
Tejun Heod302f012010-06-29 10:07:13 +0200870 worker->flags |= flags;
871}
872
873/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200874 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200875 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200876 * @flags: flags to clear
877 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200878 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200879 *
Tejun Heocb444762010-07-02 10:03:50 +0200880 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800881 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200882 */
883static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
884{
Tejun Heo63d95a92012-07-12 14:46:37 -0700885 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200886 unsigned int oflags = worker->flags;
887
Tejun Heocb444762010-07-02 10:03:50 +0200888 WARN_ON_ONCE(worker->task != current);
889
Tejun Heod302f012010-06-29 10:07:13 +0200890 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200891
Tejun Heo42c025f2011-01-11 15:58:49 +0100892 /*
893 * If transitioning out of NOT_RUNNING, increment nr_running. Note
894 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
895 * of multiple flags, not a single flag.
896 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200897 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
898 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heoe19e3972013-01-24 11:39:44 -0800899 atomic_inc(&pool->nr_running);
Tejun Heod302f012010-06-29 10:07:13 +0200900}
901
902/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200903 * find_worker_executing_work - find worker which is executing a work
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800904 * @pool: pool of interest
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200905 * @work: work to find worker for
906 *
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800907 * Find a worker which is executing @work on @pool by searching
908 * @pool->busy_hash which is keyed by the address of @work. For a worker
Tejun Heoa2c1c572012-12-18 10:35:02 -0800909 * to match, its current execution should match the address of @work and
910 * its work function. This is to avoid unwanted dependency between
911 * unrelated work executions through a work item being recycled while still
912 * being executed.
913 *
914 * This is a bit tricky. A work item may be freed once its execution
915 * starts and nothing prevents the freed area from being recycled for
916 * another work item. If the same work item address ends up being reused
917 * before the original execution finishes, workqueue will identify the
918 * recycled work item as currently executing and make it wait until the
919 * current execution finishes, introducing an unwanted dependency.
920 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700921 * This function checks the work item address and work function to avoid
922 * false positives. Note that this isn't complete as one may construct a
923 * work function which can introduce dependency onto itself through a
924 * recycled work item. Well, if somebody wants to shoot oneself in the
925 * foot that badly, there's only so much we can do, and if such deadlock
926 * actually occurs, it should be easy to locate the culprit work function.
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200927 *
928 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800929 * spin_lock_irq(pool->lock).
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200930 *
931 * RETURNS:
932 * Pointer to worker which is executing @work if found, NULL
933 * otherwise.
934 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800935static struct worker *find_worker_executing_work(struct worker_pool *pool,
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200936 struct work_struct *work)
937{
Sasha Levin42f85702012-12-17 10:01:23 -0500938 struct worker *worker;
Sasha Levin42f85702012-12-17 10:01:23 -0500939
Sasha Levinb67bfe02013-02-27 17:06:00 -0800940 hash_for_each_possible(pool->busy_hash, worker, hentry,
Tejun Heoa2c1c572012-12-18 10:35:02 -0800941 (unsigned long)work)
942 if (worker->current_work == work &&
943 worker->current_func == work->func)
Sasha Levin42f85702012-12-17 10:01:23 -0500944 return worker;
945
946 return NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200947}
948
949/**
Tejun Heobf4ede02012-08-03 10:30:46 -0700950 * move_linked_works - move linked works to a list
951 * @work: start of series of works to be scheduled
952 * @head: target list to append @work to
953 * @nextp: out paramter for nested worklist walking
954 *
955 * Schedule linked works starting from @work to @head. Work series to
956 * be scheduled starts at @work and includes any consecutive work with
957 * WORK_STRUCT_LINKED set in its predecessor.
958 *
959 * If @nextp is not NULL, it's updated to point to the next work of
960 * the last scheduled work. This allows move_linked_works() to be
961 * nested inside outer list_for_each_entry_safe().
962 *
963 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800964 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -0700965 */
966static void move_linked_works(struct work_struct *work, struct list_head *head,
967 struct work_struct **nextp)
968{
969 struct work_struct *n;
970
971 /*
972 * Linked worklist will always end before the end of the list,
973 * use NULL for list head.
974 */
975 list_for_each_entry_safe_from(work, n, NULL, entry) {
976 list_move_tail(&work->entry, head);
977 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
978 break;
979 }
980
981 /*
982 * If we're already inside safe list traversal and have moved
983 * multiple works to the scheduled queue, the next position
984 * needs to be updated.
985 */
986 if (nextp)
987 *nextp = n;
988}
989
Tejun Heo8864b4e2013-03-12 11:30:04 -0700990/**
991 * get_pwq - get an extra reference on the specified pool_workqueue
992 * @pwq: pool_workqueue to get
993 *
994 * Obtain an extra reference on @pwq. The caller should guarantee that
995 * @pwq has positive refcnt and be holding the matching pool->lock.
996 */
997static void get_pwq(struct pool_workqueue *pwq)
998{
999 lockdep_assert_held(&pwq->pool->lock);
1000 WARN_ON_ONCE(pwq->refcnt <= 0);
1001 pwq->refcnt++;
1002}
1003
1004/**
1005 * put_pwq - put a pool_workqueue reference
1006 * @pwq: pool_workqueue to put
1007 *
1008 * Drop a reference of @pwq. If its refcnt reaches zero, schedule its
1009 * destruction. The caller should be holding the matching pool->lock.
1010 */
1011static void put_pwq(struct pool_workqueue *pwq)
1012{
1013 lockdep_assert_held(&pwq->pool->lock);
1014 if (likely(--pwq->refcnt))
1015 return;
1016 if (WARN_ON_ONCE(!(pwq->wq->flags & WQ_UNBOUND)))
1017 return;
1018 /*
1019 * @pwq can't be released under pool->lock, bounce to
1020 * pwq_unbound_release_workfn(). This never recurses on the same
1021 * pool->lock as this path is taken only for unbound workqueues and
1022 * the release work item is scheduled on a per-cpu workqueue. To
1023 * avoid lockdep warning, unbound pool->locks are given lockdep
1024 * subclass of 1 in get_unbound_pool().
1025 */
1026 schedule_work(&pwq->unbound_release_work);
1027}
1028
Tejun Heo112202d2013-02-13 19:29:12 -08001029static void pwq_activate_delayed_work(struct work_struct *work)
Tejun Heobf4ede02012-08-03 10:30:46 -07001030{
Tejun Heo112202d2013-02-13 19:29:12 -08001031 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobf4ede02012-08-03 10:30:46 -07001032
1033 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001034 move_linked_works(work, &pwq->pool->worklist, NULL);
Tejun Heobf4ede02012-08-03 10:30:46 -07001035 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo112202d2013-02-13 19:29:12 -08001036 pwq->nr_active++;
Tejun Heobf4ede02012-08-03 10:30:46 -07001037}
1038
Tejun Heo112202d2013-02-13 19:29:12 -08001039static void pwq_activate_first_delayed(struct pool_workqueue *pwq)
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001040{
Tejun Heo112202d2013-02-13 19:29:12 -08001041 struct work_struct *work = list_first_entry(&pwq->delayed_works,
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001042 struct work_struct, entry);
1043
Tejun Heo112202d2013-02-13 19:29:12 -08001044 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001045}
1046
Tejun Heobf4ede02012-08-03 10:30:46 -07001047/**
Tejun Heo112202d2013-02-13 19:29:12 -08001048 * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight
1049 * @pwq: pwq of interest
Tejun Heobf4ede02012-08-03 10:30:46 -07001050 * @color: color of work which left the queue
Tejun Heobf4ede02012-08-03 10:30:46 -07001051 *
1052 * A work either has completed or is removed from pending queue,
Tejun Heo112202d2013-02-13 19:29:12 -08001053 * decrement nr_in_flight of its pwq and handle workqueue flushing.
Tejun Heobf4ede02012-08-03 10:30:46 -07001054 *
1055 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001056 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -07001057 */
Tejun Heo112202d2013-02-13 19:29:12 -08001058static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color)
Tejun Heobf4ede02012-08-03 10:30:46 -07001059{
Tejun Heo8864b4e2013-03-12 11:30:04 -07001060 /* uncolored work items don't participate in flushing or nr_active */
Tejun Heobf4ede02012-08-03 10:30:46 -07001061 if (color == WORK_NO_COLOR)
Tejun Heo8864b4e2013-03-12 11:30:04 -07001062 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001063
Tejun Heo112202d2013-02-13 19:29:12 -08001064 pwq->nr_in_flight[color]--;
Tejun Heobf4ede02012-08-03 10:30:46 -07001065
Tejun Heo112202d2013-02-13 19:29:12 -08001066 pwq->nr_active--;
1067 if (!list_empty(&pwq->delayed_works)) {
Lai Jiangshanb3f9f402012-09-18 10:40:00 -07001068 /* one down, submit a delayed one */
Tejun Heo112202d2013-02-13 19:29:12 -08001069 if (pwq->nr_active < pwq->max_active)
1070 pwq_activate_first_delayed(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001071 }
1072
1073 /* is flush in progress and are we at the flushing tip? */
Tejun Heo112202d2013-02-13 19:29:12 -08001074 if (likely(pwq->flush_color != color))
Tejun Heo8864b4e2013-03-12 11:30:04 -07001075 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001076
1077 /* are there still in-flight works? */
Tejun Heo112202d2013-02-13 19:29:12 -08001078 if (pwq->nr_in_flight[color])
Tejun Heo8864b4e2013-03-12 11:30:04 -07001079 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001080
Tejun Heo112202d2013-02-13 19:29:12 -08001081 /* this pwq is done, clear flush_color */
1082 pwq->flush_color = -1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001083
1084 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001085 * If this was the last pwq, wake up the first flusher. It
Tejun Heobf4ede02012-08-03 10:30:46 -07001086 * will handle the rest.
1087 */
Tejun Heo112202d2013-02-13 19:29:12 -08001088 if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush))
1089 complete(&pwq->wq->first_flusher->done);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001090out_put:
1091 put_pwq(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001092}
1093
Tejun Heo36e227d2012-08-03 10:30:46 -07001094/**
Tejun Heobbb68df2012-08-03 10:30:46 -07001095 * try_to_grab_pending - steal work item from worklist and disable irq
Tejun Heo36e227d2012-08-03 10:30:46 -07001096 * @work: work item to steal
1097 * @is_dwork: @work is a delayed_work
Tejun Heobbb68df2012-08-03 10:30:46 -07001098 * @flags: place to store irq state
Tejun Heo36e227d2012-08-03 10:30:46 -07001099 *
1100 * Try to grab PENDING bit of @work. This function can handle @work in any
1101 * stable state - idle, on timer or on worklist. Return values are
1102 *
1103 * 1 if @work was pending and we successfully stole PENDING
1104 * 0 if @work was idle and we claimed PENDING
1105 * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry
Tejun Heobbb68df2012-08-03 10:30:46 -07001106 * -ENOENT if someone else is canceling @work, this state may persist
1107 * for arbitrarily long
Tejun Heo36e227d2012-08-03 10:30:46 -07001108 *
Tejun Heobbb68df2012-08-03 10:30:46 -07001109 * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001110 * interrupted while holding PENDING and @work off queue, irq must be
1111 * disabled on entry. This, combined with delayed_work->timer being
1112 * irqsafe, ensures that we return -EAGAIN for finite short period of time.
Tejun Heobbb68df2012-08-03 10:30:46 -07001113 *
1114 * On successful return, >= 0, irq is disabled and the caller is
1115 * responsible for releasing it using local_irq_restore(*@flags).
1116 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001117 * This function is safe to call from any context including IRQ handler.
Tejun Heobf4ede02012-08-03 10:30:46 -07001118 */
Tejun Heobbb68df2012-08-03 10:30:46 -07001119static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
1120 unsigned long *flags)
Tejun Heobf4ede02012-08-03 10:30:46 -07001121{
Tejun Heod565ed62013-01-24 11:01:33 -08001122 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08001123 struct pool_workqueue *pwq;
Tejun Heobf4ede02012-08-03 10:30:46 -07001124
Tejun Heobbb68df2012-08-03 10:30:46 -07001125 local_irq_save(*flags);
1126
Tejun Heo36e227d2012-08-03 10:30:46 -07001127 /* try to steal the timer if it exists */
1128 if (is_dwork) {
1129 struct delayed_work *dwork = to_delayed_work(work);
1130
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001131 /*
1132 * dwork->timer is irqsafe. If del_timer() fails, it's
1133 * guaranteed that the timer is not queued anywhere and not
1134 * running on the local CPU.
1135 */
Tejun Heo36e227d2012-08-03 10:30:46 -07001136 if (likely(del_timer(&dwork->timer)))
1137 return 1;
1138 }
1139
1140 /* try to claim PENDING the normal way */
Tejun Heobf4ede02012-08-03 10:30:46 -07001141 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1142 return 0;
1143
1144 /*
1145 * The queueing is in progress, or it is already queued. Try to
1146 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1147 */
Tejun Heod565ed62013-01-24 11:01:33 -08001148 pool = get_work_pool(work);
1149 if (!pool)
Tejun Heobbb68df2012-08-03 10:30:46 -07001150 goto fail;
Tejun Heobf4ede02012-08-03 10:30:46 -07001151
Tejun Heod565ed62013-01-24 11:01:33 -08001152 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001153 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001154 * work->data is guaranteed to point to pwq only while the work
1155 * item is queued on pwq->wq, and both updating work->data to point
1156 * to pwq on queueing and to pool on dequeueing are done under
1157 * pwq->pool->lock. This in turn guarantees that, if work->data
1158 * points to pwq which is associated with a locked pool, the work
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001159 * item is currently queued on that pool.
1160 */
Tejun Heo112202d2013-02-13 19:29:12 -08001161 pwq = get_work_pwq(work);
1162 if (pwq && pwq->pool == pool) {
Tejun Heo16062832013-02-06 18:04:53 -08001163 debug_work_deactivate(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001164
Tejun Heo16062832013-02-06 18:04:53 -08001165 /*
1166 * A delayed work item cannot be grabbed directly because
1167 * it might have linked NO_COLOR work items which, if left
Tejun Heo112202d2013-02-13 19:29:12 -08001168 * on the delayed_list, will confuse pwq->nr_active
Tejun Heo16062832013-02-06 18:04:53 -08001169 * management later on and cause stall. Make sure the work
1170 * item is activated before grabbing.
1171 */
1172 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
Tejun Heo112202d2013-02-13 19:29:12 -08001173 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001174
Tejun Heo16062832013-02-06 18:04:53 -08001175 list_del_init(&work->entry);
Tejun Heo112202d2013-02-13 19:29:12 -08001176 pwq_dec_nr_in_flight(get_work_pwq(work), get_work_color(work));
Tejun Heo36e227d2012-08-03 10:30:46 -07001177
Tejun Heo112202d2013-02-13 19:29:12 -08001178 /* work->data points to pwq iff queued, point to pool */
Tejun Heo16062832013-02-06 18:04:53 -08001179 set_work_pool_and_keep_pending(work, pool->id);
Lai Jiangshan4468a002013-02-06 18:04:53 -08001180
Tejun Heo16062832013-02-06 18:04:53 -08001181 spin_unlock(&pool->lock);
1182 return 1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001183 }
Tejun Heod565ed62013-01-24 11:01:33 -08001184 spin_unlock(&pool->lock);
Tejun Heobbb68df2012-08-03 10:30:46 -07001185fail:
1186 local_irq_restore(*flags);
1187 if (work_is_canceling(work))
1188 return -ENOENT;
1189 cpu_relax();
Tejun Heo36e227d2012-08-03 10:30:46 -07001190 return -EAGAIN;
Tejun Heobf4ede02012-08-03 10:30:46 -07001191}
1192
1193/**
Tejun Heo706026c2013-01-24 11:01:34 -08001194 * insert_work - insert a work into a pool
Tejun Heo112202d2013-02-13 19:29:12 -08001195 * @pwq: pwq @work belongs to
Tejun Heo4690c4a2010-06-29 10:07:10 +02001196 * @work: work to insert
1197 * @head: insertion point
1198 * @extra_flags: extra WORK_STRUCT_* flags to set
1199 *
Tejun Heo112202d2013-02-13 19:29:12 -08001200 * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to
Tejun Heo706026c2013-01-24 11:01:34 -08001201 * work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +02001202 *
1203 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001204 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02001205 */
Tejun Heo112202d2013-02-13 19:29:12 -08001206static void insert_work(struct pool_workqueue *pwq, struct work_struct *work,
1207 struct list_head *head, unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001208{
Tejun Heo112202d2013-02-13 19:29:12 -08001209 struct worker_pool *pool = pwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01001210
Tejun Heo4690c4a2010-06-29 10:07:10 +02001211 /* we own @work, set data and link */
Tejun Heo112202d2013-02-13 19:29:12 -08001212 set_work_pwq(work, pwq, extra_flags);
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -07001213 list_add_tail(&work->entry, head);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001214 get_pwq(pwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02001215
1216 /*
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001217 * Ensure either wq_worker_sleeping() sees the above
1218 * list_add_tail() or we see zero nr_running to avoid workers lying
1219 * around lazily while there are works to be processed.
Tejun Heoe22bee72010-06-29 10:07:14 +02001220 */
1221 smp_mb();
1222
Tejun Heo63d95a92012-07-12 14:46:37 -07001223 if (__need_more_worker(pool))
1224 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001225}
1226
Tejun Heoc8efcc22010-12-20 19:32:04 +01001227/*
1228 * Test whether @work is being queued from another work executing on the
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001229 * same workqueue.
Tejun Heoc8efcc22010-12-20 19:32:04 +01001230 */
1231static bool is_chained_work(struct workqueue_struct *wq)
1232{
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001233 struct worker *worker;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001234
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001235 worker = current_wq_worker();
1236 /*
1237 * Return %true iff I'm a worker execuing a work item on @wq. If
1238 * I'm @worker, it's safe to dereference it without locking.
1239 */
Tejun Heo112202d2013-02-13 19:29:12 -08001240 return worker && worker->current_pwq->wq == wq;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001241}
1242
Tejun Heod84ff052013-03-12 11:29:59 -07001243static void __queue_work(int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 struct work_struct *work)
1245{
Tejun Heo112202d2013-02-13 19:29:12 -08001246 struct pool_workqueue *pwq;
Tejun Heoc9178082013-03-12 11:30:04 -07001247 struct worker_pool *last_pool;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001248 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001249 unsigned int work_flags;
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001250 unsigned int req_cpu = cpu;
Tejun Heo8930cab2012-08-03 10:30:45 -07001251
1252 /*
1253 * While a work item is PENDING && off queue, a task trying to
1254 * steal the PENDING will busy-loop waiting for it to either get
1255 * queued or lose PENDING. Grabbing PENDING and queueing should
1256 * happen with IRQ disabled.
1257 */
1258 WARN_ON_ONCE(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001260 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001261
Tejun Heoc8efcc22010-12-20 19:32:04 +01001262 /* if dying, only works from the same workqueue are allowed */
Tejun Heo618b01e2013-03-12 11:30:04 -07001263 if (unlikely(wq->flags & __WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001264 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001265 return;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001266retry:
Tejun Heoc9178082013-03-12 11:30:04 -07001267 /* pwq which will be used unless @work is executing elsewhere */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001268 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo57469822012-08-03 10:30:45 -07001269 if (cpu == WORK_CPU_UNBOUND)
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001270 cpu = raw_smp_processor_id();
Tejun Heo7fb98ea2013-03-12 11:30:00 -07001271 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heoc9178082013-03-12 11:30:04 -07001272 } else {
1273 pwq = first_pwq(wq);
1274 }
Tejun Heodbf25762012-08-20 14:51:23 -07001275
Tejun Heoc9178082013-03-12 11:30:04 -07001276 /*
1277 * If @work was previously on a different pool, it might still be
1278 * running there, in which case the work needs to be queued on that
1279 * pool to guarantee non-reentrancy.
1280 */
1281 last_pool = get_work_pool(work);
1282 if (last_pool && last_pool != pwq->pool) {
1283 struct worker *worker;
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001284
Tejun Heoc9178082013-03-12 11:30:04 -07001285 spin_lock(&last_pool->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001286
Tejun Heoc9178082013-03-12 11:30:04 -07001287 worker = find_worker_executing_work(last_pool, work);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001288
Tejun Heoc9178082013-03-12 11:30:04 -07001289 if (worker && worker->current_pwq->wq == wq) {
1290 pwq = worker->current_pwq;
Tejun Heo8930cab2012-08-03 10:30:45 -07001291 } else {
Tejun Heoc9178082013-03-12 11:30:04 -07001292 /* meh... not running there, queue here */
1293 spin_unlock(&last_pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08001294 spin_lock(&pwq->pool->lock);
Tejun Heo8930cab2012-08-03 10:30:45 -07001295 }
Tejun Heof3421792010-07-02 10:03:51 +02001296 } else {
Tejun Heo112202d2013-02-13 19:29:12 -08001297 spin_lock(&pwq->pool->lock);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001298 }
1299
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001300 /*
1301 * pwq is determined and locked. For unbound pools, we could have
1302 * raced with pwq release and it could already be dead. If its
1303 * refcnt is zero, repeat pwq selection. Note that pwqs never die
1304 * without another pwq replacing it as the first pwq or while a
1305 * work item is executing on it, so the retying is guaranteed to
1306 * make forward-progress.
1307 */
1308 if (unlikely(!pwq->refcnt)) {
1309 if (wq->flags & WQ_UNBOUND) {
1310 spin_unlock(&pwq->pool->lock);
1311 cpu_relax();
1312 goto retry;
1313 }
1314 /* oops */
1315 WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt",
1316 wq->name, cpu);
1317 }
1318
Tejun Heo112202d2013-02-13 19:29:12 -08001319 /* pwq determined, queue */
1320 trace_workqueue_queue_work(req_cpu, pwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001321
Dan Carpenterf5b25522012-04-13 22:06:58 +03001322 if (WARN_ON(!list_empty(&work->entry))) {
Tejun Heo112202d2013-02-13 19:29:12 -08001323 spin_unlock(&pwq->pool->lock);
Dan Carpenterf5b25522012-04-13 22:06:58 +03001324 return;
1325 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001326
Tejun Heo112202d2013-02-13 19:29:12 -08001327 pwq->nr_in_flight[pwq->work_color]++;
1328 work_flags = work_color_to_flags(pwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001329
Tejun Heo112202d2013-02-13 19:29:12 -08001330 if (likely(pwq->nr_active < pwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001331 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001332 pwq->nr_active++;
1333 worklist = &pwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001334 } else {
1335 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo112202d2013-02-13 19:29:12 -08001336 worklist = &pwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001337 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001338
Tejun Heo112202d2013-02-13 19:29:12 -08001339 insert_work(pwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001340
Tejun Heo112202d2013-02-13 19:29:12 -08001341 spin_unlock(&pwq->pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342}
1343
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001344/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07001345 * queue_work_on - queue work on specific cpu
1346 * @cpu: CPU number to execute work on
1347 * @wq: workqueue to use
1348 * @work: work to queue
1349 *
Tejun Heod4283e92012-08-03 10:30:44 -07001350 * Returns %false if @work was already on a queue, %true otherwise.
Zhang Ruic1a220e2008-07-23 21:28:39 -07001351 *
1352 * We queue the work to a specific CPU, the caller must ensure it
1353 * can't go away.
1354 */
Tejun Heod4283e92012-08-03 10:30:44 -07001355bool queue_work_on(int cpu, struct workqueue_struct *wq,
1356 struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07001357{
Tejun Heod4283e92012-08-03 10:30:44 -07001358 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001359 unsigned long flags;
1360
1361 local_irq_save(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001362
Tejun Heo22df02b2010-06-29 10:07:10 +02001363 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001364 __queue_work(cpu, wq, work);
Tejun Heod4283e92012-08-03 10:30:44 -07001365 ret = true;
Zhang Ruic1a220e2008-07-23 21:28:39 -07001366 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001367
1368 local_irq_restore(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001369 return ret;
1370}
1371EXPORT_SYMBOL_GPL(queue_work_on);
1372
Tejun Heod8e794d2012-08-03 10:30:45 -07001373void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
David Howells52bad642006-11-22 14:54:01 +00001375 struct delayed_work *dwork = (struct delayed_work *)__data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001377 /* should have been called from irqsafe timer with irq already off */
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001378 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379}
Konstantin Khlebnikov1438ade52013-01-24 16:36:31 +04001380EXPORT_SYMBOL(delayed_work_timer_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001382static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
1383 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001385 struct timer_list *timer = &dwork->timer;
1386 struct work_struct *work = &dwork->work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001388 WARN_ON_ONCE(timer->function != delayed_work_timer_fn ||
1389 timer->data != (unsigned long)dwork);
Tejun Heofc4b5142012-12-04 07:40:39 -08001390 WARN_ON_ONCE(timer_pending(timer));
1391 WARN_ON_ONCE(!list_empty(&work->entry));
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001392
Tejun Heo8852aac2012-12-01 16:23:42 -08001393 /*
1394 * If @delay is 0, queue @dwork->work immediately. This is for
1395 * both optimization and correctness. The earliest @timer can
1396 * expire is on the closest next tick and delayed_work users depend
1397 * on that there's no such delay when @delay is 0.
1398 */
1399 if (!delay) {
1400 __queue_work(cpu, wq, &dwork->work);
1401 return;
1402 }
1403
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001404 timer_stats_timer_set_start_info(&dwork->timer);
1405
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001406 dwork->wq = wq;
Tejun Heo12650572012-08-08 09:38:42 -07001407 dwork->cpu = cpu;
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001408 timer->expires = jiffies + delay;
1409
1410 if (unlikely(cpu != WORK_CPU_UNBOUND))
1411 add_timer_on(timer, cpu);
1412 else
1413 add_timer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414}
1415
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001416/**
1417 * queue_delayed_work_on - queue work on specific CPU after delay
1418 * @cpu: CPU number to execute work on
1419 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001420 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001421 * @delay: number of jiffies to wait before queueing
1422 *
Tejun Heo715f1302012-08-03 10:30:46 -07001423 * Returns %false if @work was already on a queue, %true otherwise. If
1424 * @delay is zero and @dwork is idle, it will be scheduled for immediate
1425 * execution.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001426 */
Tejun Heod4283e92012-08-03 10:30:44 -07001427bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
1428 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001429{
David Howells52bad642006-11-22 14:54:01 +00001430 struct work_struct *work = &dwork->work;
Tejun Heod4283e92012-08-03 10:30:44 -07001431 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001432 unsigned long flags;
1433
1434 /* read the comment in __queue_work() */
1435 local_irq_save(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001436
Tejun Heo22df02b2010-06-29 10:07:10 +02001437 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001438 __queue_delayed_work(cpu, wq, dwork, delay);
Tejun Heod4283e92012-08-03 10:30:44 -07001439 ret = true;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001440 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001441
1442 local_irq_restore(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001443 return ret;
1444}
Dave Jonesae90dd52006-06-30 01:40:45 -04001445EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Tejun Heoc8e55f32010-06-29 10:07:12 +02001447/**
Tejun Heo8376fe22012-08-03 10:30:47 -07001448 * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
1449 * @cpu: CPU number to execute work on
1450 * @wq: workqueue to use
1451 * @dwork: work to queue
1452 * @delay: number of jiffies to wait before queueing
1453 *
1454 * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
1455 * modify @dwork's timer so that it expires after @delay. If @delay is
1456 * zero, @work is guaranteed to be scheduled immediately regardless of its
1457 * current state.
1458 *
1459 * Returns %false if @dwork was idle and queued, %true if @dwork was
1460 * pending and its timer was modified.
1461 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001462 * This function is safe to call from any context including IRQ handler.
Tejun Heo8376fe22012-08-03 10:30:47 -07001463 * See try_to_grab_pending() for details.
1464 */
1465bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
1466 struct delayed_work *dwork, unsigned long delay)
1467{
1468 unsigned long flags;
1469 int ret;
1470
1471 do {
1472 ret = try_to_grab_pending(&dwork->work, true, &flags);
1473 } while (unlikely(ret == -EAGAIN));
1474
1475 if (likely(ret >= 0)) {
1476 __queue_delayed_work(cpu, wq, dwork, delay);
1477 local_irq_restore(flags);
1478 }
1479
1480 /* -ENOENT from try_to_grab_pending() becomes %true */
1481 return ret;
1482}
1483EXPORT_SYMBOL_GPL(mod_delayed_work_on);
1484
1485/**
Tejun Heoc8e55f32010-06-29 10:07:12 +02001486 * worker_enter_idle - enter idle state
1487 * @worker: worker which is entering idle state
1488 *
1489 * @worker is entering idle state. Update stats and idle timer if
1490 * necessary.
1491 *
1492 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001493 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001494 */
1495static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001497 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Tejun Heo6183c002013-03-12 11:29:57 -07001499 if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) ||
1500 WARN_ON_ONCE(!list_empty(&worker->entry) &&
1501 (worker->hentry.next || worker->hentry.pprev)))
1502 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Tejun Heocb444762010-07-02 10:03:50 +02001504 /* can't use worker_set_flags(), also called from start_worker() */
1505 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001506 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001507 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001508
Tejun Heoc8e55f32010-06-29 10:07:12 +02001509 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001510 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001511
Tejun Heo628c78e2012-07-17 12:39:27 -07001512 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1513 mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
Tejun Heocb444762010-07-02 10:03:50 +02001514
Tejun Heo544ecf32012-05-14 15:04:50 -07001515 /*
Tejun Heo706026c2013-01-24 11:01:34 -08001516 * Sanity check nr_running. Because wq_unbind_fn() releases
Tejun Heod565ed62013-01-24 11:01:33 -08001517 * pool->lock between setting %WORKER_UNBOUND and zapping
Tejun Heo628c78e2012-07-17 12:39:27 -07001518 * nr_running, the warning may trigger spuriously. Check iff
1519 * unbind is not in progress.
Tejun Heo544ecf32012-05-14 15:04:50 -07001520 */
Tejun Heo24647572013-01-24 11:01:33 -08001521 WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001522 pool->nr_workers == pool->nr_idle &&
Tejun Heoe19e3972013-01-24 11:39:44 -08001523 atomic_read(&pool->nr_running));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
1525
Tejun Heoc8e55f32010-06-29 10:07:12 +02001526/**
1527 * worker_leave_idle - leave idle state
1528 * @worker: worker which is leaving idle state
1529 *
1530 * @worker is leaving idle state. Update stats.
1531 *
1532 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001533 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001534 */
1535static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001537 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Tejun Heo6183c002013-03-12 11:29:57 -07001539 if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE)))
1540 return;
Tejun Heod302f012010-06-29 10:07:13 +02001541 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001542 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001543 list_del_init(&worker->entry);
1544}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Tejun Heoe22bee72010-06-29 10:07:14 +02001546/**
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001547 * worker_maybe_bind_and_lock - try to bind %current to worker_pool and lock it
1548 * @pool: target worker_pool
1549 *
1550 * Bind %current to the cpu of @pool if it is associated and lock @pool.
Tejun Heoe22bee72010-06-29 10:07:14 +02001551 *
1552 * Works which are scheduled while the cpu is online must at least be
1553 * scheduled to a worker which is bound to the cpu so that if they are
1554 * flushed from cpu callbacks while cpu is going down, they are
1555 * guaranteed to execute on the cpu.
1556 *
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001557 * This function is to be used by unbound workers and rescuers to bind
Tejun Heoe22bee72010-06-29 10:07:14 +02001558 * themselves to the target cpu and may race with cpu going down or
1559 * coming online. kthread_bind() can't be used because it may put the
1560 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
Tejun Heo706026c2013-01-24 11:01:34 -08001561 * verbatim as it's best effort and blocking and pool may be
Tejun Heoe22bee72010-06-29 10:07:14 +02001562 * [dis]associated in the meantime.
1563 *
Tejun Heo706026c2013-01-24 11:01:34 -08001564 * This function tries set_cpus_allowed() and locks pool and verifies the
Tejun Heo24647572013-01-24 11:01:33 -08001565 * binding against %POOL_DISASSOCIATED which is set during
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001566 * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1567 * enters idle state or fetches works without dropping lock, it can
1568 * guarantee the scheduling requirement described in the first paragraph.
Tejun Heoe22bee72010-06-29 10:07:14 +02001569 *
1570 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001571 * Might sleep. Called without any lock but returns with pool->lock
Tejun Heoe22bee72010-06-29 10:07:14 +02001572 * held.
1573 *
1574 * RETURNS:
Tejun Heo706026c2013-01-24 11:01:34 -08001575 * %true if the associated pool is online (@worker is successfully
Tejun Heoe22bee72010-06-29 10:07:14 +02001576 * bound), %false if offline.
1577 */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001578static bool worker_maybe_bind_and_lock(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001579__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001580{
Tejun Heoe22bee72010-06-29 10:07:14 +02001581 while (true) {
1582 /*
1583 * The following call may fail, succeed or succeed
1584 * without actually migrating the task to the cpu if
1585 * it races with cpu hotunplug operation. Verify
Tejun Heo24647572013-01-24 11:01:33 -08001586 * against POOL_DISASSOCIATED.
Tejun Heoe22bee72010-06-29 10:07:14 +02001587 */
Tejun Heo24647572013-01-24 11:01:33 -08001588 if (!(pool->flags & POOL_DISASSOCIATED))
Tejun Heo7a4e3442013-03-12 11:30:00 -07001589 set_cpus_allowed_ptr(current, pool->attrs->cpumask);
Oleg Nesterov85f41862007-05-09 02:34:20 -07001590
Tejun Heod565ed62013-01-24 11:01:33 -08001591 spin_lock_irq(&pool->lock);
Tejun Heo24647572013-01-24 11:01:33 -08001592 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heoe22bee72010-06-29 10:07:14 +02001593 return false;
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001594 if (task_cpu(current) == pool->cpu &&
Tejun Heo7a4e3442013-03-12 11:30:00 -07001595 cpumask_equal(&current->cpus_allowed, pool->attrs->cpumask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001596 return true;
Tejun Heod565ed62013-01-24 11:01:33 -08001597 spin_unlock_irq(&pool->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001598
Tejun Heo5035b202011-04-29 18:08:37 +02001599 /*
1600 * We've raced with CPU hot[un]plug. Give it a breather
1601 * and retry migration. cond_resched() is required here;
1602 * otherwise, we might deadlock against cpu_stop trying to
1603 * bring down the CPU on non-preemptive kernel.
1604 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001605 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001606 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001607 }
1608}
1609
Tejun Heoc34056a2010-06-29 10:07:11 +02001610static struct worker *alloc_worker(void)
1611{
1612 struct worker *worker;
1613
1614 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001615 if (worker) {
1616 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001617 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heoe22bee72010-06-29 10:07:14 +02001618 /* on creation a worker is in !idle && prep state */
1619 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001620 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001621 return worker;
1622}
1623
1624/**
1625 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001626 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001627 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001628 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001629 * can be started by calling start_worker() or destroyed using
1630 * destroy_worker().
1631 *
1632 * CONTEXT:
1633 * Might sleep. Does GFP_KERNEL allocations.
1634 *
1635 * RETURNS:
1636 * Pointer to the newly created worker.
1637 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001638static struct worker *create_worker(struct worker_pool *pool)
Tejun Heoc34056a2010-06-29 10:07:11 +02001639{
Tejun Heo7a4e3442013-03-12 11:30:00 -07001640 const char *pri = pool->attrs->nice < 0 ? "H" : "";
Tejun Heoc34056a2010-06-29 10:07:11 +02001641 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001642 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001643
Tejun Heocd549682013-03-13 19:47:39 -07001644 lockdep_assert_held(&pool->manager_mutex);
1645
Tejun Heo822d8402013-03-19 13:45:21 -07001646 /*
1647 * ID is needed to determine kthread name. Allocate ID first
1648 * without installing the pointer.
1649 */
1650 idr_preload(GFP_KERNEL);
Tejun Heod565ed62013-01-24 11:01:33 -08001651 spin_lock_irq(&pool->lock);
Tejun Heo822d8402013-03-19 13:45:21 -07001652
1653 id = idr_alloc(&pool->worker_idr, NULL, 0, 0, GFP_NOWAIT);
1654
Tejun Heod565ed62013-01-24 11:01:33 -08001655 spin_unlock_irq(&pool->lock);
Tejun Heo822d8402013-03-19 13:45:21 -07001656 idr_preload_end();
1657 if (id < 0)
1658 goto fail;
Tejun Heoc34056a2010-06-29 10:07:11 +02001659
1660 worker = alloc_worker();
1661 if (!worker)
1662 goto fail;
1663
Tejun Heobd7bdd42012-07-12 14:46:37 -07001664 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001665 worker->id = id;
1666
Tejun Heo29c91e92013-03-12 11:30:03 -07001667 if (pool->cpu >= 0)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001668 worker->task = kthread_create_on_node(worker_thread,
Tejun Heoec22ca52013-01-24 11:01:33 -08001669 worker, cpu_to_node(pool->cpu),
Tejun Heod84ff052013-03-12 11:29:59 -07001670 "kworker/%d:%d%s", pool->cpu, id, pri);
Tejun Heof3421792010-07-02 10:03:51 +02001671 else
1672 worker->task = kthread_create(worker_thread, worker,
Tejun Heoac6104c2013-03-12 11:30:03 -07001673 "kworker/u%d:%d%s",
1674 pool->id, id, pri);
Tejun Heoc34056a2010-06-29 10:07:11 +02001675 if (IS_ERR(worker->task))
1676 goto fail;
1677
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001678 /*
1679 * set_cpus_allowed_ptr() will fail if the cpumask doesn't have any
1680 * online CPUs. It'll be re-applied when any of the CPUs come up.
1681 */
Tejun Heo7a4e3442013-03-12 11:30:00 -07001682 set_user_nice(worker->task, pool->attrs->nice);
1683 set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask);
Tejun Heo32704762012-07-13 22:16:45 -07001684
Tejun Heo14a40ff2013-03-19 13:45:20 -07001685 /* prevent userland from meddling with cpumask of workqueue workers */
1686 worker->task->flags |= PF_NO_SETAFFINITY;
Tejun Heo7a4e3442013-03-12 11:30:00 -07001687
1688 /*
1689 * The caller is responsible for ensuring %POOL_DISASSOCIATED
1690 * remains stable across this function. See the comments above the
1691 * flag definition for details.
1692 */
1693 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001694 worker->flags |= WORKER_UNBOUND;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001695
Tejun Heo822d8402013-03-19 13:45:21 -07001696 /* successful, commit the pointer to idr */
1697 spin_lock_irq(&pool->lock);
1698 idr_replace(&pool->worker_idr, worker, worker->id);
1699 spin_unlock_irq(&pool->lock);
1700
Tejun Heoc34056a2010-06-29 10:07:11 +02001701 return worker;
Tejun Heo822d8402013-03-19 13:45:21 -07001702
Tejun Heoc34056a2010-06-29 10:07:11 +02001703fail:
1704 if (id >= 0) {
Tejun Heod565ed62013-01-24 11:01:33 -08001705 spin_lock_irq(&pool->lock);
Tejun Heo822d8402013-03-19 13:45:21 -07001706 idr_remove(&pool->worker_idr, id);
Tejun Heod565ed62013-01-24 11:01:33 -08001707 spin_unlock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001708 }
1709 kfree(worker);
1710 return NULL;
1711}
1712
1713/**
1714 * start_worker - start a newly created worker
1715 * @worker: worker to start
1716 *
Tejun Heo706026c2013-01-24 11:01:34 -08001717 * Make the pool aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001718 *
1719 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001720 * spin_lock_irq(pool->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001721 */
1722static void start_worker(struct worker *worker)
1723{
Tejun Heocb444762010-07-02 10:03:50 +02001724 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001725 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001726 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001727 wake_up_process(worker->task);
1728}
1729
1730/**
Tejun Heoebf44d12013-03-13 19:47:39 -07001731 * create_and_start_worker - create and start a worker for a pool
1732 * @pool: the target pool
1733 *
Tejun Heocd549682013-03-13 19:47:39 -07001734 * Grab the managership of @pool and create and start a new worker for it.
Tejun Heoebf44d12013-03-13 19:47:39 -07001735 */
1736static int create_and_start_worker(struct worker_pool *pool)
1737{
1738 struct worker *worker;
1739
Tejun Heocd549682013-03-13 19:47:39 -07001740 mutex_lock(&pool->manager_mutex);
1741
Tejun Heoebf44d12013-03-13 19:47:39 -07001742 worker = create_worker(pool);
1743 if (worker) {
1744 spin_lock_irq(&pool->lock);
1745 start_worker(worker);
1746 spin_unlock_irq(&pool->lock);
1747 }
1748
Tejun Heocd549682013-03-13 19:47:39 -07001749 mutex_unlock(&pool->manager_mutex);
1750
Tejun Heoebf44d12013-03-13 19:47:39 -07001751 return worker ? 0 : -ENOMEM;
1752}
1753
1754/**
Tejun Heoc34056a2010-06-29 10:07:11 +02001755 * destroy_worker - destroy a workqueue worker
1756 * @worker: worker to be destroyed
1757 *
Tejun Heo706026c2013-01-24 11:01:34 -08001758 * Destroy @worker and adjust @pool stats accordingly.
Tejun Heoc8e55f32010-06-29 10:07:12 +02001759 *
1760 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001761 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001762 */
1763static void destroy_worker(struct worker *worker)
1764{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001765 struct worker_pool *pool = worker->pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001766
Tejun Heocd549682013-03-13 19:47:39 -07001767 lockdep_assert_held(&pool->manager_mutex);
1768 lockdep_assert_held(&pool->lock);
1769
Tejun Heoc34056a2010-06-29 10:07:11 +02001770 /* sanity check frenzy */
Tejun Heo6183c002013-03-12 11:29:57 -07001771 if (WARN_ON(worker->current_work) ||
1772 WARN_ON(!list_empty(&worker->scheduled)))
1773 return;
Tejun Heoc34056a2010-06-29 10:07:11 +02001774
Tejun Heoc8e55f32010-06-29 10:07:12 +02001775 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001776 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001777 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001778 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001779
1780 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001781 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001782
Tejun Heo822d8402013-03-19 13:45:21 -07001783 idr_remove(&pool->worker_idr, worker->id);
1784
Tejun Heod565ed62013-01-24 11:01:33 -08001785 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001786
Tejun Heoc34056a2010-06-29 10:07:11 +02001787 kthread_stop(worker->task);
1788 kfree(worker);
1789
Tejun Heod565ed62013-01-24 11:01:33 -08001790 spin_lock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001791}
1792
Tejun Heo63d95a92012-07-12 14:46:37 -07001793static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001794{
Tejun Heo63d95a92012-07-12 14:46:37 -07001795 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001796
Tejun Heod565ed62013-01-24 11:01:33 -08001797 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001798
Tejun Heo63d95a92012-07-12 14:46:37 -07001799 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001800 struct worker *worker;
1801 unsigned long expires;
1802
1803 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001804 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001805 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1806
1807 if (time_before(jiffies, expires))
Tejun Heo63d95a92012-07-12 14:46:37 -07001808 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001809 else {
1810 /* it's been idle for too long, wake up manager */
Tejun Heo11ebea52012-07-12 14:46:37 -07001811 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -07001812 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001813 }
1814 }
1815
Tejun Heod565ed62013-01-24 11:01:33 -08001816 spin_unlock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001817}
1818
Tejun Heo493a1722013-03-12 11:29:59 -07001819static void send_mayday(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001820{
Tejun Heo112202d2013-02-13 19:29:12 -08001821 struct pool_workqueue *pwq = get_work_pwq(work);
1822 struct workqueue_struct *wq = pwq->wq;
Tejun Heo493a1722013-03-12 11:29:59 -07001823
Tejun Heo2e109a22013-03-13 19:47:40 -07001824 lockdep_assert_held(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001825
Tejun Heo493008a2013-03-12 11:30:03 -07001826 if (!wq->rescuer)
Tejun Heo493a1722013-03-12 11:29:59 -07001827 return;
Tejun Heoe22bee72010-06-29 10:07:14 +02001828
1829 /* mayday mayday mayday */
Tejun Heo493a1722013-03-12 11:29:59 -07001830 if (list_empty(&pwq->mayday_node)) {
1831 list_add_tail(&pwq->mayday_node, &wq->maydays);
Tejun Heoe22bee72010-06-29 10:07:14 +02001832 wake_up_process(wq->rescuer->task);
Tejun Heo493a1722013-03-12 11:29:59 -07001833 }
Tejun Heoe22bee72010-06-29 10:07:14 +02001834}
1835
Tejun Heo706026c2013-01-24 11:01:34 -08001836static void pool_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001837{
Tejun Heo63d95a92012-07-12 14:46:37 -07001838 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001839 struct work_struct *work;
1840
Tejun Heo2e109a22013-03-13 19:47:40 -07001841 spin_lock_irq(&wq_mayday_lock); /* for wq->maydays */
Tejun Heo493a1722013-03-12 11:29:59 -07001842 spin_lock(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001843
Tejun Heo63d95a92012-07-12 14:46:37 -07001844 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001845 /*
1846 * We've been trying to create a new worker but
1847 * haven't been successful. We might be hitting an
1848 * allocation deadlock. Send distress signals to
1849 * rescuers.
1850 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001851 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001852 send_mayday(work);
1853 }
1854
Tejun Heo493a1722013-03-12 11:29:59 -07001855 spin_unlock(&pool->lock);
Tejun Heo2e109a22013-03-13 19:47:40 -07001856 spin_unlock_irq(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001857
Tejun Heo63d95a92012-07-12 14:46:37 -07001858 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001859}
1860
1861/**
1862 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001863 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001864 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001865 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001866 * have at least one idle worker on return from this function. If
1867 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001868 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001869 * possible allocation deadlock.
1870 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001871 * On return, need_to_create_worker() is guaranteed to be %false and
1872 * may_start_working() %true.
Tejun Heoe22bee72010-06-29 10:07:14 +02001873 *
1874 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001875 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001876 * multiple times. Does GFP_KERNEL allocations. Called only from
1877 * manager.
1878 *
1879 * RETURNS:
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001880 * %false if no action was taken and pool->lock stayed locked, %true
Tejun Heoe22bee72010-06-29 10:07:14 +02001881 * otherwise.
1882 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001883static bool maybe_create_worker(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001884__releases(&pool->lock)
1885__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001886{
Tejun Heo63d95a92012-07-12 14:46:37 -07001887 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001888 return false;
1889restart:
Tejun Heod565ed62013-01-24 11:01:33 -08001890 spin_unlock_irq(&pool->lock);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001891
Tejun Heoe22bee72010-06-29 10:07:14 +02001892 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07001893 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001894
1895 while (true) {
1896 struct worker *worker;
1897
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001898 worker = create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001899 if (worker) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001900 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001901 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001902 start_worker(worker);
Tejun Heo6183c002013-03-12 11:29:57 -07001903 if (WARN_ON_ONCE(need_to_create_worker(pool)))
1904 goto restart;
Tejun Heoe22bee72010-06-29 10:07:14 +02001905 return true;
1906 }
1907
Tejun Heo63d95a92012-07-12 14:46:37 -07001908 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001909 break;
1910
Tejun Heoe22bee72010-06-29 10:07:14 +02001911 __set_current_state(TASK_INTERRUPTIBLE);
1912 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001913
Tejun Heo63d95a92012-07-12 14:46:37 -07001914 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001915 break;
1916 }
1917
Tejun Heo63d95a92012-07-12 14:46:37 -07001918 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001919 spin_lock_irq(&pool->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07001920 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001921 goto restart;
1922 return true;
1923}
1924
1925/**
1926 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo63d95a92012-07-12 14:46:37 -07001927 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001928 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001929 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02001930 * IDLE_WORKER_TIMEOUT.
1931 *
1932 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001933 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001934 * multiple times. Called only from manager.
1935 *
1936 * RETURNS:
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001937 * %false if no action was taken and pool->lock stayed locked, %true
Tejun Heoe22bee72010-06-29 10:07:14 +02001938 * otherwise.
1939 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001940static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001941{
1942 bool ret = false;
1943
Tejun Heo63d95a92012-07-12 14:46:37 -07001944 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001945 struct worker *worker;
1946 unsigned long expires;
1947
Tejun Heo63d95a92012-07-12 14:46:37 -07001948 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001949 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1950
1951 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001952 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001953 break;
1954 }
1955
1956 destroy_worker(worker);
1957 ret = true;
1958 }
1959
1960 return ret;
1961}
1962
1963/**
1964 * manage_workers - manage worker pool
1965 * @worker: self
1966 *
Tejun Heo706026c2013-01-24 11:01:34 -08001967 * Assume the manager role and manage the worker pool @worker belongs
Tejun Heoe22bee72010-06-29 10:07:14 +02001968 * to. At any given time, there can be only zero or one manager per
Tejun Heo706026c2013-01-24 11:01:34 -08001969 * pool. The exclusion is handled automatically by this function.
Tejun Heoe22bee72010-06-29 10:07:14 +02001970 *
1971 * The caller can safely start processing works on false return. On
1972 * true return, it's guaranteed that need_to_create_worker() is false
1973 * and may_start_working() is true.
1974 *
1975 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001976 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001977 * multiple times. Does GFP_KERNEL allocations.
1978 *
1979 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08001980 * spin_lock_irq(pool->lock) which may be released and regrabbed
1981 * multiple times. Does GFP_KERNEL allocations.
Tejun Heoe22bee72010-06-29 10:07:14 +02001982 */
1983static bool manage_workers(struct worker *worker)
1984{
Tejun Heo63d95a92012-07-12 14:46:37 -07001985 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001986 bool ret = false;
1987
Tejun Heobc3a1af2013-03-13 19:47:39 -07001988 /*
1989 * Managership is governed by two mutexes - manager_arb and
1990 * manager_mutex. manager_arb handles arbitration of manager role.
1991 * Anyone who successfully grabs manager_arb wins the arbitration
1992 * and becomes the manager. mutex_trylock() on pool->manager_arb
1993 * failure while holding pool->lock reliably indicates that someone
1994 * else is managing the pool and the worker which failed trylock
1995 * can proceed to executing work items. This means that anyone
1996 * grabbing manager_arb is responsible for actually performing
1997 * manager duties. If manager_arb is grabbed and released without
1998 * actual management, the pool may stall indefinitely.
1999 *
2000 * manager_mutex is used for exclusion of actual management
2001 * operations. The holder of manager_mutex can be sure that none
2002 * of management operations, including creation and destruction of
2003 * workers, won't take place until the mutex is released. Because
2004 * manager_mutex doesn't interfere with manager role arbitration,
2005 * it is guaranteed that the pool's management, while may be
2006 * delayed, won't be disturbed by someone else grabbing
2007 * manager_mutex.
2008 */
Tejun Heo34a06bd2013-03-12 11:30:00 -07002009 if (!mutex_trylock(&pool->manager_arb))
Tejun Heoe22bee72010-06-29 10:07:14 +02002010 return ret;
2011
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002012 /*
Tejun Heobc3a1af2013-03-13 19:47:39 -07002013 * With manager arbitration won, manager_mutex would be free in
2014 * most cases. trylock first without dropping @pool->lock.
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002015 */
Tejun Heobc3a1af2013-03-13 19:47:39 -07002016 if (unlikely(!mutex_trylock(&pool->manager_mutex))) {
Tejun Heod565ed62013-01-24 11:01:33 -08002017 spin_unlock_irq(&pool->lock);
Tejun Heobc3a1af2013-03-13 19:47:39 -07002018 mutex_lock(&pool->manager_mutex);
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002019 ret = true;
2020 }
2021
Tejun Heo11ebea52012-07-12 14:46:37 -07002022 pool->flags &= ~POOL_MANAGE_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02002023
2024 /*
2025 * Destroy and then create so that may_start_working() is true
2026 * on return.
2027 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002028 ret |= maybe_destroy_workers(pool);
2029 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002030
Tejun Heobc3a1af2013-03-13 19:47:39 -07002031 mutex_unlock(&pool->manager_mutex);
Tejun Heo34a06bd2013-03-12 11:30:00 -07002032 mutex_unlock(&pool->manager_arb);
Tejun Heoe22bee72010-06-29 10:07:14 +02002033 return ret;
2034}
2035
Tejun Heoa62428c2010-06-29 10:07:10 +02002036/**
2037 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02002038 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02002039 * @work: work to process
2040 *
2041 * Process @work. This function contains all the logics necessary to
2042 * process a single work including synchronization against and
2043 * interaction with other workers on the same cpu, queueing and
2044 * flushing. As long as context requirement is met, any worker can
2045 * call this function to process a work.
2046 *
2047 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002048 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02002049 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002050static void process_one_work(struct worker *worker, struct work_struct *work)
Tejun Heod565ed62013-01-24 11:01:33 -08002051__releases(&pool->lock)
2052__acquires(&pool->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02002053{
Tejun Heo112202d2013-02-13 19:29:12 -08002054 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002055 struct worker_pool *pool = worker->pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002056 bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02002057 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02002058 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02002059#ifdef CONFIG_LOCKDEP
2060 /*
2061 * It is permissible to free the struct work_struct from
2062 * inside the function that is called from it, this we need to
2063 * take into account for lockdep too. To avoid bogus "held
2064 * lock freed" warnings as well as problems when looking into
2065 * work->lockdep_map, make a copy and use that here.
2066 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07002067 struct lockdep_map lockdep_map;
2068
2069 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002070#endif
Tejun Heo6fec10a2012-07-22 10:16:34 -07002071 /*
2072 * Ensure we're on the correct CPU. DISASSOCIATED test is
2073 * necessary to avoid spurious warnings from rescuers servicing the
Tejun Heo24647572013-01-24 11:01:33 -08002074 * unbound or a disassociated pool.
Tejun Heo6fec10a2012-07-22 10:16:34 -07002075 */
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002076 WARN_ON_ONCE(!(worker->flags & WORKER_UNBOUND) &&
Tejun Heo24647572013-01-24 11:01:33 -08002077 !(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heoec22ca52013-01-24 11:01:33 -08002078 raw_smp_processor_id() != pool->cpu);
Tejun Heo25511a42012-07-17 12:39:27 -07002079
Tejun Heo7e116292010-06-29 10:07:13 +02002080 /*
2081 * A single work shouldn't be executed concurrently by
2082 * multiple workers on a single cpu. Check whether anyone is
2083 * already processing the work. If so, defer the work to the
2084 * currently executing one.
2085 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002086 collision = find_worker_executing_work(pool, work);
Tejun Heo7e116292010-06-29 10:07:13 +02002087 if (unlikely(collision)) {
2088 move_linked_works(work, &collision->scheduled, NULL);
2089 return;
2090 }
2091
Tejun Heo8930cab2012-08-03 10:30:45 -07002092 /* claim and dequeue */
Tejun Heoa62428c2010-06-29 10:07:10 +02002093 debug_work_deactivate(work);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002094 hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work);
Tejun Heoc34056a2010-06-29 10:07:11 +02002095 worker->current_work = work;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002096 worker->current_func = work->func;
Tejun Heo112202d2013-02-13 19:29:12 -08002097 worker->current_pwq = pwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002098 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002099
Tejun Heoa62428c2010-06-29 10:07:10 +02002100 list_del_init(&work->entry);
2101
Tejun Heo649027d2010-06-29 10:07:14 +02002102 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02002103 * CPU intensive works don't participate in concurrency
2104 * management. They're the scheduler's responsibility.
2105 */
2106 if (unlikely(cpu_intensive))
2107 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
2108
Tejun Heo974271c2012-07-12 14:46:37 -07002109 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002110 * Unbound pool isn't concurrency managed and work items should be
Tejun Heo974271c2012-07-12 14:46:37 -07002111 * executed ASAP. Wake up another worker if necessary.
2112 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002113 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
2114 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07002115
Tejun Heo8930cab2012-08-03 10:30:45 -07002116 /*
Tejun Heo7c3eed52013-01-24 11:01:33 -08002117 * Record the last pool and clear PENDING which should be the last
Tejun Heod565ed62013-01-24 11:01:33 -08002118 * update to @work. Also, do this inside @pool->lock so that
Tejun Heo23657bb2012-08-13 17:08:19 -07002119 * PENDING and queued state changes happen together while IRQ is
2120 * disabled.
Tejun Heo8930cab2012-08-03 10:30:45 -07002121 */
Tejun Heo7c3eed52013-01-24 11:01:33 -08002122 set_work_pool_and_clear_pending(work, pool->id);
Tejun Heoa62428c2010-06-29 10:07:10 +02002123
Tejun Heod565ed62013-01-24 11:01:33 -08002124 spin_unlock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002125
Tejun Heo112202d2013-02-13 19:29:12 -08002126 lock_map_acquire_read(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002127 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002128 trace_workqueue_execute_start(work);
Tejun Heoa2c1c572012-12-18 10:35:02 -08002129 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002130 /*
2131 * While we must be careful to not use "work" after this, the trace
2132 * point will only record its address.
2133 */
2134 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002135 lock_map_release(&lockdep_map);
Tejun Heo112202d2013-02-13 19:29:12 -08002136 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002137
2138 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Valentin Ilie044c7822012-08-19 00:52:42 +03002139 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
2140 " last function: %pf\n",
Tejun Heoa2c1c572012-12-18 10:35:02 -08002141 current->comm, preempt_count(), task_pid_nr(current),
2142 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02002143 debug_show_held_locks(current);
2144 dump_stack();
2145 }
2146
Tejun Heod565ed62013-01-24 11:01:33 -08002147 spin_lock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002148
Tejun Heofb0e7be2010-06-29 10:07:15 +02002149 /* clear cpu intensive status */
2150 if (unlikely(cpu_intensive))
2151 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2152
Tejun Heoa62428c2010-06-29 10:07:10 +02002153 /* we're done with it, release */
Sasha Levin42f85702012-12-17 10:01:23 -05002154 hash_del(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002155 worker->current_work = NULL;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002156 worker->current_func = NULL;
Tejun Heo112202d2013-02-13 19:29:12 -08002157 worker->current_pwq = NULL;
2158 pwq_dec_nr_in_flight(pwq, work_color);
Tejun Heoa62428c2010-06-29 10:07:10 +02002159}
2160
Tejun Heoaffee4b2010-06-29 10:07:12 +02002161/**
2162 * process_scheduled_works - process scheduled works
2163 * @worker: self
2164 *
2165 * Process all scheduled works. Please note that the scheduled list
2166 * may change while processing a work, so this function repeatedly
2167 * fetches a work from the top and executes it.
2168 *
2169 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002170 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02002171 * multiple times.
2172 */
2173static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002175 while (!list_empty(&worker->scheduled)) {
2176 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002178 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180}
2181
Tejun Heo4690c4a2010-06-29 10:07:10 +02002182/**
2183 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02002184 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02002185 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002186 * The worker thread function. All workers belong to a worker_pool -
2187 * either a per-cpu one or dynamic unbound one. These workers process all
2188 * work items regardless of their specific target workqueue. The only
2189 * exception is work items which belong to workqueues with a rescuer which
2190 * will be explained in rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02002191 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002192static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193{
Tejun Heoc34056a2010-06-29 10:07:11 +02002194 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07002195 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
Tejun Heoe22bee72010-06-29 10:07:14 +02002197 /* tell the scheduler that this is a workqueue worker */
2198 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002199woke_up:
Tejun Heod565ed62013-01-24 11:01:33 -08002200 spin_lock_irq(&pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
Tejun Heoa9ab7752013-03-19 13:45:21 -07002202 /* am I supposed to die? */
2203 if (unlikely(worker->flags & WORKER_DIE)) {
Tejun Heod565ed62013-01-24 11:01:33 -08002204 spin_unlock_irq(&pool->lock);
Tejun Heoa9ab7752013-03-19 13:45:21 -07002205 WARN_ON_ONCE(!list_empty(&worker->entry));
2206 worker->task->flags &= ~PF_WQ_WORKER;
2207 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 }
2209
Tejun Heoc8e55f32010-06-29 10:07:12 +02002210 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002211recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002212 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002213 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002214 goto sleep;
2215
2216 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002217 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002218 goto recheck;
2219
Tejun Heoc8e55f32010-06-29 10:07:12 +02002220 /*
2221 * ->scheduled list can only be filled while a worker is
2222 * preparing to process a work or actually processing it.
2223 * Make sure nobody diddled with it while I was sleeping.
2224 */
Tejun Heo6183c002013-03-12 11:29:57 -07002225 WARN_ON_ONCE(!list_empty(&worker->scheduled));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002226
Tejun Heoe22bee72010-06-29 10:07:14 +02002227 /*
Tejun Heoa9ab7752013-03-19 13:45:21 -07002228 * Finish PREP stage. We're guaranteed to have at least one idle
2229 * worker or that someone else has already assumed the manager
2230 * role. This is where @worker starts participating in concurrency
2231 * management if applicable and concurrency management is restored
2232 * after being rebound. See rebind_workers() for details.
Tejun Heoe22bee72010-06-29 10:07:14 +02002233 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07002234 worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND);
Tejun Heoe22bee72010-06-29 10:07:14 +02002235
2236 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002237 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002238 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002239 struct work_struct, entry);
2240
2241 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2242 /* optimization path, not strictly necessary */
2243 process_one_work(worker, work);
2244 if (unlikely(!list_empty(&worker->scheduled)))
2245 process_scheduled_works(worker);
2246 } else {
2247 move_linked_works(work, &worker->scheduled, NULL);
2248 process_scheduled_works(worker);
2249 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002250 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002251
Tejun Heoe22bee72010-06-29 10:07:14 +02002252 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002253sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07002254 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002255 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002256
Tejun Heoc8e55f32010-06-29 10:07:12 +02002257 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002258 * pool->lock is held and there's no work to process and no need to
2259 * manage, sleep. Workers are woken up only while holding
2260 * pool->lock or from local cpu, so setting the current state
2261 * before releasing pool->lock is enough to prevent losing any
2262 * event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002263 */
2264 worker_enter_idle(worker);
2265 __set_current_state(TASK_INTERRUPTIBLE);
Tejun Heod565ed62013-01-24 11:01:33 -08002266 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02002267 schedule();
2268 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269}
2270
Tejun Heoe22bee72010-06-29 10:07:14 +02002271/**
2272 * rescuer_thread - the rescuer thread function
Tejun Heo111c2252013-01-17 17:16:24 -08002273 * @__rescuer: self
Tejun Heoe22bee72010-06-29 10:07:14 +02002274 *
2275 * Workqueue rescuer thread function. There's one rescuer for each
Tejun Heo493008a2013-03-12 11:30:03 -07002276 * workqueue which has WQ_MEM_RECLAIM set.
Tejun Heoe22bee72010-06-29 10:07:14 +02002277 *
Tejun Heo706026c2013-01-24 11:01:34 -08002278 * Regular work processing on a pool may block trying to create a new
Tejun Heoe22bee72010-06-29 10:07:14 +02002279 * worker which uses GFP_KERNEL allocation which has slight chance of
2280 * developing into deadlock if some works currently on the same queue
2281 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2282 * the problem rescuer solves.
2283 *
Tejun Heo706026c2013-01-24 11:01:34 -08002284 * When such condition is possible, the pool summons rescuers of all
2285 * workqueues which have works queued on the pool and let them process
Tejun Heoe22bee72010-06-29 10:07:14 +02002286 * those works so that forward progress can be guaranteed.
2287 *
2288 * This should happen rarely.
2289 */
Tejun Heo111c2252013-01-17 17:16:24 -08002290static int rescuer_thread(void *__rescuer)
Tejun Heoe22bee72010-06-29 10:07:14 +02002291{
Tejun Heo111c2252013-01-17 17:16:24 -08002292 struct worker *rescuer = __rescuer;
2293 struct workqueue_struct *wq = rescuer->rescue_wq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002294 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heoe22bee72010-06-29 10:07:14 +02002295
2296 set_user_nice(current, RESCUER_NICE_LEVEL);
Tejun Heo111c2252013-01-17 17:16:24 -08002297
2298 /*
2299 * Mark rescuer as worker too. As WORKER_PREP is never cleared, it
2300 * doesn't participate in concurrency management.
2301 */
2302 rescuer->task->flags |= PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002303repeat:
2304 set_current_state(TASK_INTERRUPTIBLE);
2305
Mike Galbraith412d32e2012-11-28 07:17:18 +01002306 if (kthread_should_stop()) {
2307 __set_current_state(TASK_RUNNING);
Tejun Heo111c2252013-01-17 17:16:24 -08002308 rescuer->task->flags &= ~PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002309 return 0;
Mike Galbraith412d32e2012-11-28 07:17:18 +01002310 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002311
Tejun Heo493a1722013-03-12 11:29:59 -07002312 /* see whether any pwq is asking for help */
Tejun Heo2e109a22013-03-13 19:47:40 -07002313 spin_lock_irq(&wq_mayday_lock);
Tejun Heo493a1722013-03-12 11:29:59 -07002314
2315 while (!list_empty(&wq->maydays)) {
2316 struct pool_workqueue *pwq = list_first_entry(&wq->maydays,
2317 struct pool_workqueue, mayday_node);
Tejun Heo112202d2013-02-13 19:29:12 -08002318 struct worker_pool *pool = pwq->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002319 struct work_struct *work, *n;
2320
2321 __set_current_state(TASK_RUNNING);
Tejun Heo493a1722013-03-12 11:29:59 -07002322 list_del_init(&pwq->mayday_node);
2323
Tejun Heo2e109a22013-03-13 19:47:40 -07002324 spin_unlock_irq(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002325
2326 /* migrate to the target cpu if possible */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08002327 worker_maybe_bind_and_lock(pool);
Lai Jiangshanb3104102013-02-19 12:17:02 -08002328 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002329
2330 /*
2331 * Slurp in all works issued via this workqueue and
2332 * process'em.
2333 */
Tejun Heo6183c002013-03-12 11:29:57 -07002334 WARN_ON_ONCE(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002335 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heo112202d2013-02-13 19:29:12 -08002336 if (get_work_pwq(work) == pwq)
Tejun Heoe22bee72010-06-29 10:07:14 +02002337 move_linked_works(work, scheduled, &n);
2338
2339 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002340
2341 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002342 * Leave this pool. If keep_working() is %true, notify a
Tejun Heo75769582011-02-14 14:04:46 +01002343 * regular worker; otherwise, we end up with 0 concurrency
2344 * and stalling the execution.
2345 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002346 if (keep_working(pool))
2347 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002348
Lai Jiangshanb3104102013-02-19 12:17:02 -08002349 rescuer->pool = NULL;
Tejun Heo493a1722013-03-12 11:29:59 -07002350 spin_unlock(&pool->lock);
Tejun Heo2e109a22013-03-13 19:47:40 -07002351 spin_lock(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002352 }
2353
Tejun Heo2e109a22013-03-13 19:47:40 -07002354 spin_unlock_irq(&wq_mayday_lock);
Tejun Heo493a1722013-03-12 11:29:59 -07002355
Tejun Heo111c2252013-01-17 17:16:24 -08002356 /* rescuers should never participate in concurrency management */
2357 WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
Tejun Heoe22bee72010-06-29 10:07:14 +02002358 schedule();
2359 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360}
2361
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002362struct wq_barrier {
2363 struct work_struct work;
2364 struct completion done;
2365};
2366
2367static void wq_barrier_func(struct work_struct *work)
2368{
2369 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2370 complete(&barr->done);
2371}
2372
Tejun Heo4690c4a2010-06-29 10:07:10 +02002373/**
2374 * insert_wq_barrier - insert a barrier work
Tejun Heo112202d2013-02-13 19:29:12 -08002375 * @pwq: pwq to insert barrier into
Tejun Heo4690c4a2010-06-29 10:07:10 +02002376 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002377 * @target: target work to attach @barr to
2378 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002379 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002380 * @barr is linked to @target such that @barr is completed only after
2381 * @target finishes execution. Please note that the ordering
2382 * guarantee is observed only with respect to @target and on the local
2383 * cpu.
2384 *
2385 * Currently, a queued barrier can't be canceled. This is because
2386 * try_to_grab_pending() can't determine whether the work to be
2387 * grabbed is at the head of the queue and thus can't clear LINKED
2388 * flag of the previous work while there must be a valid next work
2389 * after a work with LINKED flag set.
2390 *
2391 * Note that when @worker is non-NULL, @target may be modified
Tejun Heo112202d2013-02-13 19:29:12 -08002392 * underneath us, so we can't reliably determine pwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002393 *
2394 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002395 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002396 */
Tejun Heo112202d2013-02-13 19:29:12 -08002397static void insert_wq_barrier(struct pool_workqueue *pwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002398 struct wq_barrier *barr,
2399 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002400{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002401 struct list_head *head;
2402 unsigned int linked = 0;
2403
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002404 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002405 * debugobject calls are safe here even with pool->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002406 * as we know for sure that this will not trigger any of the
2407 * checks and call back into the fixup functions where we
2408 * might deadlock.
2409 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002410 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002411 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002412 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002413
Tejun Heoaffee4b2010-06-29 10:07:12 +02002414 /*
2415 * If @target is currently being executed, schedule the
2416 * barrier to the worker; otherwise, put it after @target.
2417 */
2418 if (worker)
2419 head = worker->scheduled.next;
2420 else {
2421 unsigned long *bits = work_data_bits(target);
2422
2423 head = target->entry.next;
2424 /* there can already be other linked works, inherit and set */
2425 linked = *bits & WORK_STRUCT_LINKED;
2426 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2427 }
2428
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002429 debug_work_activate(&barr->work);
Tejun Heo112202d2013-02-13 19:29:12 -08002430 insert_work(pwq, &barr->work, head,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002431 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002432}
2433
Tejun Heo73f53c42010-06-29 10:07:11 +02002434/**
Tejun Heo112202d2013-02-13 19:29:12 -08002435 * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing
Tejun Heo73f53c42010-06-29 10:07:11 +02002436 * @wq: workqueue being flushed
2437 * @flush_color: new flush color, < 0 for no-op
2438 * @work_color: new work color, < 0 for no-op
2439 *
Tejun Heo112202d2013-02-13 19:29:12 -08002440 * Prepare pwqs for workqueue flushing.
Tejun Heo73f53c42010-06-29 10:07:11 +02002441 *
Tejun Heo112202d2013-02-13 19:29:12 -08002442 * If @flush_color is non-negative, flush_color on all pwqs should be
2443 * -1. If no pwq has in-flight commands at the specified color, all
2444 * pwq->flush_color's stay at -1 and %false is returned. If any pwq
2445 * has in flight commands, its pwq->flush_color is set to
2446 * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq
Tejun Heo73f53c42010-06-29 10:07:11 +02002447 * wakeup logic is armed and %true is returned.
2448 *
2449 * The caller should have initialized @wq->first_flusher prior to
2450 * calling this function with non-negative @flush_color. If
2451 * @flush_color is negative, no flush color update is done and %false
2452 * is returned.
2453 *
Tejun Heo112202d2013-02-13 19:29:12 -08002454 * If @work_color is non-negative, all pwqs should have the same
Tejun Heo73f53c42010-06-29 10:07:11 +02002455 * work_color which is previous to @work_color and all will be
2456 * advanced to @work_color.
2457 *
2458 * CONTEXT:
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002459 * mutex_lock(wq->mutex).
Tejun Heo73f53c42010-06-29 10:07:11 +02002460 *
2461 * RETURNS:
2462 * %true if @flush_color >= 0 and there's something to flush. %false
2463 * otherwise.
2464 */
Tejun Heo112202d2013-02-13 19:29:12 -08002465static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
Tejun Heo73f53c42010-06-29 10:07:11 +02002466 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467{
Tejun Heo73f53c42010-06-29 10:07:11 +02002468 bool wait = false;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002469 struct pool_workqueue *pwq;
Oleg Nesterov14441962007-05-23 13:57:57 -07002470
Tejun Heo73f53c42010-06-29 10:07:11 +02002471 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002472 WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush));
Tejun Heo112202d2013-02-13 19:29:12 -08002473 atomic_set(&wq->nr_pwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002474 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002475
Tejun Heo49e3cf42013-03-12 11:29:58 -07002476 for_each_pwq(pwq, wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08002477 struct worker_pool *pool = pwq->pool;
Tejun Heo73f53c42010-06-29 10:07:11 +02002478
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002479 spin_lock_irq(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002480
2481 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002482 WARN_ON_ONCE(pwq->flush_color != -1);
Tejun Heo73f53c42010-06-29 10:07:11 +02002483
Tejun Heo112202d2013-02-13 19:29:12 -08002484 if (pwq->nr_in_flight[flush_color]) {
2485 pwq->flush_color = flush_color;
2486 atomic_inc(&wq->nr_pwqs_to_flush);
Tejun Heo73f53c42010-06-29 10:07:11 +02002487 wait = true;
2488 }
2489 }
2490
2491 if (work_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002492 WARN_ON_ONCE(work_color != work_next_color(pwq->work_color));
Tejun Heo112202d2013-02-13 19:29:12 -08002493 pwq->work_color = work_color;
Tejun Heo73f53c42010-06-29 10:07:11 +02002494 }
2495
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002496 spin_unlock_irq(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002497 }
2498
Tejun Heo112202d2013-02-13 19:29:12 -08002499 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush))
Tejun Heo73f53c42010-06-29 10:07:11 +02002500 complete(&wq->first_flusher->done);
2501
2502 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503}
2504
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002505/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002507 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002509 * This function sleeps until all work items which were queued on entry
2510 * have finished execution, but it is not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002512void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513{
Tejun Heo73f53c42010-06-29 10:07:11 +02002514 struct wq_flusher this_flusher = {
2515 .list = LIST_HEAD_INIT(this_flusher.list),
2516 .flush_color = -1,
2517 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2518 };
2519 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002520
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002521 lock_map_acquire(&wq->lockdep_map);
2522 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002523
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002524 mutex_lock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002525
2526 /*
2527 * Start-to-wait phase
2528 */
2529 next_color = work_next_color(wq->work_color);
2530
2531 if (next_color != wq->flush_color) {
2532 /*
2533 * Color space is not full. The current work_color
2534 * becomes our flush_color and work_color is advanced
2535 * by one.
2536 */
Tejun Heo6183c002013-03-12 11:29:57 -07002537 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow));
Tejun Heo73f53c42010-06-29 10:07:11 +02002538 this_flusher.flush_color = wq->work_color;
2539 wq->work_color = next_color;
2540
2541 if (!wq->first_flusher) {
2542 /* no flush in progress, become the first flusher */
Tejun Heo6183c002013-03-12 11:29:57 -07002543 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002544
2545 wq->first_flusher = &this_flusher;
2546
Tejun Heo112202d2013-02-13 19:29:12 -08002547 if (!flush_workqueue_prep_pwqs(wq, wq->flush_color,
Tejun Heo73f53c42010-06-29 10:07:11 +02002548 wq->work_color)) {
2549 /* nothing to flush, done */
2550 wq->flush_color = next_color;
2551 wq->first_flusher = NULL;
2552 goto out_unlock;
2553 }
2554 } else {
2555 /* wait in queue */
Tejun Heo6183c002013-03-12 11:29:57 -07002556 WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002557 list_add_tail(&this_flusher.list, &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002558 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002559 }
2560 } else {
2561 /*
2562 * Oops, color space is full, wait on overflow queue.
2563 * The next flush completion will assign us
2564 * flush_color and transfer to flusher_queue.
2565 */
2566 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2567 }
2568
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002569 mutex_unlock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002570
2571 wait_for_completion(&this_flusher.done);
2572
2573 /*
2574 * Wake-up-and-cascade phase
2575 *
2576 * First flushers are responsible for cascading flushes and
2577 * handling overflow. Non-first flushers can simply return.
2578 */
2579 if (wq->first_flusher != &this_flusher)
2580 return;
2581
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002582 mutex_lock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002583
Tejun Heo4ce48b32010-07-02 10:03:51 +02002584 /* we might have raced, check again with mutex held */
2585 if (wq->first_flusher != &this_flusher)
2586 goto out_unlock;
2587
Tejun Heo73f53c42010-06-29 10:07:11 +02002588 wq->first_flusher = NULL;
2589
Tejun Heo6183c002013-03-12 11:29:57 -07002590 WARN_ON_ONCE(!list_empty(&this_flusher.list));
2591 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002592
2593 while (true) {
2594 struct wq_flusher *next, *tmp;
2595
2596 /* complete all the flushers sharing the current flush color */
2597 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2598 if (next->flush_color != wq->flush_color)
2599 break;
2600 list_del_init(&next->list);
2601 complete(&next->done);
2602 }
2603
Tejun Heo6183c002013-03-12 11:29:57 -07002604 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) &&
2605 wq->flush_color != work_next_color(wq->work_color));
Tejun Heo73f53c42010-06-29 10:07:11 +02002606
2607 /* this flush_color is finished, advance by one */
2608 wq->flush_color = work_next_color(wq->flush_color);
2609
2610 /* one color has been freed, handle overflow queue */
2611 if (!list_empty(&wq->flusher_overflow)) {
2612 /*
2613 * Assign the same color to all overflowed
2614 * flushers, advance work_color and append to
2615 * flusher_queue. This is the start-to-wait
2616 * phase for these overflowed flushers.
2617 */
2618 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2619 tmp->flush_color = wq->work_color;
2620
2621 wq->work_color = work_next_color(wq->work_color);
2622
2623 list_splice_tail_init(&wq->flusher_overflow,
2624 &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002625 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002626 }
2627
2628 if (list_empty(&wq->flusher_queue)) {
Tejun Heo6183c002013-03-12 11:29:57 -07002629 WARN_ON_ONCE(wq->flush_color != wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002630 break;
2631 }
2632
2633 /*
2634 * Need to flush more colors. Make the next flusher
Tejun Heo112202d2013-02-13 19:29:12 -08002635 * the new first flusher and arm pwqs.
Tejun Heo73f53c42010-06-29 10:07:11 +02002636 */
Tejun Heo6183c002013-03-12 11:29:57 -07002637 WARN_ON_ONCE(wq->flush_color == wq->work_color);
2638 WARN_ON_ONCE(wq->flush_color != next->flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002639
2640 list_del_init(&next->list);
2641 wq->first_flusher = next;
2642
Tejun Heo112202d2013-02-13 19:29:12 -08002643 if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1))
Tejun Heo73f53c42010-06-29 10:07:11 +02002644 break;
2645
2646 /*
2647 * Meh... this color is already done, clear first
2648 * flusher and repeat cascading.
2649 */
2650 wq->first_flusher = NULL;
2651 }
2652
2653out_unlock:
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002654 mutex_unlock(&wq->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655}
Dave Jonesae90dd52006-06-30 01:40:45 -04002656EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002658/**
2659 * drain_workqueue - drain a workqueue
2660 * @wq: workqueue to drain
2661 *
2662 * Wait until the workqueue becomes empty. While draining is in progress,
2663 * only chain queueing is allowed. IOW, only currently pending or running
2664 * work items on @wq can queue further work items on it. @wq is flushed
2665 * repeatedly until it becomes empty. The number of flushing is detemined
2666 * by the depth of chaining and should be relatively short. Whine if it
2667 * takes too long.
2668 */
2669void drain_workqueue(struct workqueue_struct *wq)
2670{
2671 unsigned int flush_cnt = 0;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002672 struct pool_workqueue *pwq;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002673
2674 /*
2675 * __queue_work() needs to test whether there are drainers, is much
2676 * hotter than drain_workqueue() and already looks at @wq->flags.
Tejun Heo618b01e2013-03-12 11:30:04 -07002677 * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers.
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002678 */
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002679 mutex_lock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002680 if (!wq->nr_drainers++)
Tejun Heo618b01e2013-03-12 11:30:04 -07002681 wq->flags |= __WQ_DRAINING;
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002682 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002683reflush:
2684 flush_workqueue(wq);
2685
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002686 mutex_lock(&wq->mutex);
Tejun Heo76af4d92013-03-12 11:30:00 -07002687
Tejun Heo49e3cf42013-03-12 11:29:58 -07002688 for_each_pwq(pwq, wq) {
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002689 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002690
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002691 spin_lock_irq(&pwq->pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08002692 drained = !pwq->nr_active && list_empty(&pwq->delayed_works);
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002693 spin_unlock_irq(&pwq->pool->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002694
2695 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002696 continue;
2697
2698 if (++flush_cnt == 10 ||
2699 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002700 pr_warn("workqueue %s: drain_workqueue() isn't complete after %u tries\n",
Valentin Ilie044c7822012-08-19 00:52:42 +03002701 wq->name, flush_cnt);
Tejun Heo76af4d92013-03-12 11:30:00 -07002702
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002703 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002704 goto reflush;
2705 }
2706
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002707 if (!--wq->nr_drainers)
Tejun Heo618b01e2013-03-12 11:30:04 -07002708 wq->flags &= ~__WQ_DRAINING;
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002709 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002710}
2711EXPORT_SYMBOL_GPL(drain_workqueue);
2712
Tejun Heo606a5022012-08-20 14:51:23 -07002713static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
Tejun Heobaf59022010-09-16 10:42:16 +02002714{
2715 struct worker *worker = NULL;
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002716 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002717 struct pool_workqueue *pwq;
Tejun Heobaf59022010-09-16 10:42:16 +02002718
2719 might_sleep();
Tejun Heobaf59022010-09-16 10:42:16 +02002720
Tejun Heofa1b54e2013-03-12 11:30:00 -07002721 local_irq_disable();
2722 pool = get_work_pool(work);
2723 if (!pool) {
2724 local_irq_enable();
2725 return false;
2726 }
2727
2728 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08002729 /* see the comment in try_to_grab_pending() with the same code */
Tejun Heo112202d2013-02-13 19:29:12 -08002730 pwq = get_work_pwq(work);
2731 if (pwq) {
2732 if (unlikely(pwq->pool != pool))
Tejun Heobaf59022010-09-16 10:42:16 +02002733 goto already_gone;
Tejun Heo606a5022012-08-20 14:51:23 -07002734 } else {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002735 worker = find_worker_executing_work(pool, work);
Tejun Heobaf59022010-09-16 10:42:16 +02002736 if (!worker)
2737 goto already_gone;
Tejun Heo112202d2013-02-13 19:29:12 -08002738 pwq = worker->current_pwq;
Tejun Heo606a5022012-08-20 14:51:23 -07002739 }
Tejun Heobaf59022010-09-16 10:42:16 +02002740
Tejun Heo112202d2013-02-13 19:29:12 -08002741 insert_wq_barrier(pwq, barr, work, worker);
Tejun Heod565ed62013-01-24 11:01:33 -08002742 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002743
Tejun Heoe1594892011-01-09 23:32:15 +01002744 /*
2745 * If @max_active is 1 or rescuer is in use, flushing another work
2746 * item on the same workqueue may lead to deadlock. Make sure the
2747 * flusher is not running on the same workqueue by verifying write
2748 * access.
2749 */
Tejun Heo493008a2013-03-12 11:30:03 -07002750 if (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)
Tejun Heo112202d2013-02-13 19:29:12 -08002751 lock_map_acquire(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002752 else
Tejun Heo112202d2013-02-13 19:29:12 -08002753 lock_map_acquire_read(&pwq->wq->lockdep_map);
2754 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002755
Tejun Heobaf59022010-09-16 10:42:16 +02002756 return true;
2757already_gone:
Tejun Heod565ed62013-01-24 11:01:33 -08002758 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002759 return false;
2760}
2761
Oleg Nesterovdb700892008-07-25 01:47:49 -07002762/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002763 * flush_work - wait for a work to finish executing the last queueing instance
2764 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002765 *
Tejun Heo606a5022012-08-20 14:51:23 -07002766 * Wait until @work has finished execution. @work is guaranteed to be idle
2767 * on return if it hasn't been requeued since flush started.
Tejun Heo401a8d02010-09-16 10:36:00 +02002768 *
2769 * RETURNS:
2770 * %true if flush_work() waited for the work to finish execution,
2771 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002772 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002773bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002774{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002775 struct wq_barrier barr;
2776
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002777 lock_map_acquire(&work->lockdep_map);
2778 lock_map_release(&work->lockdep_map);
2779
Tejun Heo606a5022012-08-20 14:51:23 -07002780 if (start_flush_work(work, &barr)) {
Tejun Heobaf59022010-09-16 10:42:16 +02002781 wait_for_completion(&barr.done);
2782 destroy_work_on_stack(&barr.work);
2783 return true;
Tejun Heo606a5022012-08-20 14:51:23 -07002784 } else {
Tejun Heobaf59022010-09-16 10:42:16 +02002785 return false;
Tejun Heo606a5022012-08-20 14:51:23 -07002786 }
Oleg Nesterovdb700892008-07-25 01:47:49 -07002787}
2788EXPORT_SYMBOL_GPL(flush_work);
2789
Tejun Heo36e227d2012-08-03 10:30:46 -07002790static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
Tejun Heo401a8d02010-09-16 10:36:00 +02002791{
Tejun Heobbb68df2012-08-03 10:30:46 -07002792 unsigned long flags;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002793 int ret;
2794
2795 do {
Tejun Heobbb68df2012-08-03 10:30:46 -07002796 ret = try_to_grab_pending(work, is_dwork, &flags);
2797 /*
2798 * If someone else is canceling, wait for the same event it
2799 * would be waiting for before retrying.
2800 */
2801 if (unlikely(ret == -ENOENT))
Tejun Heo606a5022012-08-20 14:51:23 -07002802 flush_work(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002803 } while (unlikely(ret < 0));
2804
Tejun Heobbb68df2012-08-03 10:30:46 -07002805 /* tell other tasks trying to grab @work to back off */
2806 mark_work_canceling(work);
2807 local_irq_restore(flags);
2808
Tejun Heo606a5022012-08-20 14:51:23 -07002809 flush_work(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002810 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002811 return ret;
2812}
2813
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002814/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002815 * cancel_work_sync - cancel a work and wait for it to finish
2816 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002817 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002818 * Cancel @work and wait for its execution to finish. This function
2819 * can be used even if the work re-queues itself or migrates to
2820 * another workqueue. On return from this function, @work is
2821 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002822 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002823 * cancel_work_sync(&delayed_work->work) must not be used for
2824 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002825 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002826 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002827 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002828 *
2829 * RETURNS:
2830 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002831 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002832bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002833{
Tejun Heo36e227d2012-08-03 10:30:46 -07002834 return __cancel_work_timer(work, false);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002835}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002836EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002837
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002838/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002839 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2840 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002841 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002842 * Delayed timer is cancelled and the pending work is queued for
2843 * immediate execution. Like flush_work(), this function only
2844 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002845 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002846 * RETURNS:
2847 * %true if flush_work() waited for the work to finish execution,
2848 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002849 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002850bool flush_delayed_work(struct delayed_work *dwork)
2851{
Tejun Heo8930cab2012-08-03 10:30:45 -07002852 local_irq_disable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002853 if (del_timer_sync(&dwork->timer))
Lai Jiangshan60c057b2013-02-06 18:04:53 -08002854 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Tejun Heo8930cab2012-08-03 10:30:45 -07002855 local_irq_enable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002856 return flush_work(&dwork->work);
2857}
2858EXPORT_SYMBOL(flush_delayed_work);
2859
2860/**
Tejun Heo57b30ae2012-08-21 13:18:24 -07002861 * cancel_delayed_work - cancel a delayed work
2862 * @dwork: delayed_work to cancel
Tejun Heo09383492010-09-16 10:48:29 +02002863 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002864 * Kill off a pending delayed_work. Returns %true if @dwork was pending
2865 * and canceled; %false if wasn't pending. Note that the work callback
2866 * function may still be running on return, unless it returns %true and the
2867 * work doesn't re-arm itself. Explicitly flush or use
2868 * cancel_delayed_work_sync() to wait on it.
Tejun Heo09383492010-09-16 10:48:29 +02002869 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002870 * This function is safe to call from any context including IRQ handler.
Tejun Heo09383492010-09-16 10:48:29 +02002871 */
Tejun Heo57b30ae2012-08-21 13:18:24 -07002872bool cancel_delayed_work(struct delayed_work *dwork)
Tejun Heo09383492010-09-16 10:48:29 +02002873{
Tejun Heo57b30ae2012-08-21 13:18:24 -07002874 unsigned long flags;
2875 int ret;
2876
2877 do {
2878 ret = try_to_grab_pending(&dwork->work, true, &flags);
2879 } while (unlikely(ret == -EAGAIN));
2880
2881 if (unlikely(ret < 0))
2882 return false;
2883
Tejun Heo7c3eed52013-01-24 11:01:33 -08002884 set_work_pool_and_clear_pending(&dwork->work,
2885 get_work_pool_id(&dwork->work));
Tejun Heo57b30ae2012-08-21 13:18:24 -07002886 local_irq_restore(flags);
Dan Magenheimerc0158ca2012-10-18 16:31:37 -07002887 return ret;
Tejun Heo09383492010-09-16 10:48:29 +02002888}
Tejun Heo57b30ae2012-08-21 13:18:24 -07002889EXPORT_SYMBOL(cancel_delayed_work);
Tejun Heo09383492010-09-16 10:48:29 +02002890
2891/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002892 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2893 * @dwork: the delayed work cancel
2894 *
2895 * This is cancel_work_sync() for delayed works.
2896 *
2897 * RETURNS:
2898 * %true if @dwork was pending, %false otherwise.
2899 */
2900bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002901{
Tejun Heo36e227d2012-08-03 10:30:46 -07002902 return __cancel_work_timer(&dwork->work, true);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002903}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002904EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002906/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002907 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002908 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002909 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002910 * schedule_on_each_cpu() executes @func on each online CPU using the
2911 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002912 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002913 *
2914 * RETURNS:
2915 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002916 */
David Howells65f27f32006-11-22 14:55:48 +00002917int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002918{
2919 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002920 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002921
Andrew Mortonb6136772006-06-25 05:47:49 -07002922 works = alloc_percpu(struct work_struct);
2923 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002924 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002925
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002926 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002927
Christoph Lameter15316ba2006-01-08 01:00:43 -08002928 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002929 struct work_struct *work = per_cpu_ptr(works, cpu);
2930
2931 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002932 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002933 }
Tejun Heo93981802009-11-17 14:06:20 -08002934
2935 for_each_online_cpu(cpu)
2936 flush_work(per_cpu_ptr(works, cpu));
2937
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002938 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002939 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002940 return 0;
2941}
2942
Alan Sterneef6a7d2010-02-12 17:39:21 +09002943/**
2944 * flush_scheduled_work - ensure that any scheduled work has run to completion.
2945 *
2946 * Forces execution of the kernel-global workqueue and blocks until its
2947 * completion.
2948 *
2949 * Think twice before calling this function! It's very easy to get into
2950 * trouble if you don't take great care. Either of the following situations
2951 * will lead to deadlock:
2952 *
2953 * One of the work items currently on the workqueue needs to acquire
2954 * a lock held by your code or its caller.
2955 *
2956 * Your code is running in the context of a work routine.
2957 *
2958 * They will be detected by lockdep when they occur, but the first might not
2959 * occur very often. It depends on what work items are on the workqueue and
2960 * what locks they need, which you have no control over.
2961 *
2962 * In most situations flushing the entire workqueue is overkill; you merely
2963 * need to know that a particular work item isn't queued and isn't running.
2964 * In such cases you should use cancel_delayed_work_sync() or
2965 * cancel_work_sync() instead.
2966 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967void flush_scheduled_work(void)
2968{
Tejun Heod320c032010-06-29 10:07:14 +02002969 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970}
Dave Jonesae90dd52006-06-30 01:40:45 -04002971EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
2973/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06002974 * execute_in_process_context - reliably execute the routine with user context
2975 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06002976 * @ew: guaranteed storage for the execute work structure (must
2977 * be available when the work executes)
2978 *
2979 * Executes the function immediately if process context is available,
2980 * otherwise schedules the function for delayed execution.
2981 *
2982 * Returns: 0 - function was executed
2983 * 1 - function was scheduled for execution
2984 */
David Howells65f27f32006-11-22 14:55:48 +00002985int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06002986{
2987 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00002988 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002989 return 0;
2990 }
2991
David Howells65f27f32006-11-22 14:55:48 +00002992 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002993 schedule_work(&ew->work);
2994
2995 return 1;
2996}
2997EXPORT_SYMBOL_GPL(execute_in_process_context);
2998
Tejun Heo226223a2013-03-12 11:30:05 -07002999#ifdef CONFIG_SYSFS
3000/*
3001 * Workqueues with WQ_SYSFS flag set is visible to userland via
3002 * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the
3003 * following attributes.
3004 *
3005 * per_cpu RO bool : whether the workqueue is per-cpu or unbound
3006 * max_active RW int : maximum number of in-flight work items
3007 *
3008 * Unbound workqueues have the following extra attributes.
3009 *
3010 * id RO int : the associated pool ID
3011 * nice RW int : nice value of the workers
3012 * cpumask RW mask : bitmask of allowed CPUs for the workers
3013 */
3014struct wq_device {
3015 struct workqueue_struct *wq;
3016 struct device dev;
3017};
3018
3019static struct workqueue_struct *dev_to_wq(struct device *dev)
3020{
3021 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
3022
3023 return wq_dev->wq;
3024}
3025
3026static ssize_t wq_per_cpu_show(struct device *dev,
3027 struct device_attribute *attr, char *buf)
3028{
3029 struct workqueue_struct *wq = dev_to_wq(dev);
3030
3031 return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND));
3032}
3033
3034static ssize_t wq_max_active_show(struct device *dev,
3035 struct device_attribute *attr, char *buf)
3036{
3037 struct workqueue_struct *wq = dev_to_wq(dev);
3038
3039 return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active);
3040}
3041
3042static ssize_t wq_max_active_store(struct device *dev,
3043 struct device_attribute *attr,
3044 const char *buf, size_t count)
3045{
3046 struct workqueue_struct *wq = dev_to_wq(dev);
3047 int val;
3048
3049 if (sscanf(buf, "%d", &val) != 1 || val <= 0)
3050 return -EINVAL;
3051
3052 workqueue_set_max_active(wq, val);
3053 return count;
3054}
3055
3056static struct device_attribute wq_sysfs_attrs[] = {
3057 __ATTR(per_cpu, 0444, wq_per_cpu_show, NULL),
3058 __ATTR(max_active, 0644, wq_max_active_show, wq_max_active_store),
3059 __ATTR_NULL,
3060};
3061
3062static ssize_t wq_pool_id_show(struct device *dev,
3063 struct device_attribute *attr, char *buf)
3064{
3065 struct workqueue_struct *wq = dev_to_wq(dev);
3066 struct worker_pool *pool;
3067 int written;
3068
3069 rcu_read_lock_sched();
3070 pool = first_pwq(wq)->pool;
3071 written = scnprintf(buf, PAGE_SIZE, "%d\n", pool->id);
3072 rcu_read_unlock_sched();
3073
3074 return written;
3075}
3076
3077static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr,
3078 char *buf)
3079{
3080 struct workqueue_struct *wq = dev_to_wq(dev);
3081 int written;
3082
3083 rcu_read_lock_sched();
3084 written = scnprintf(buf, PAGE_SIZE, "%d\n",
3085 first_pwq(wq)->pool->attrs->nice);
3086 rcu_read_unlock_sched();
3087
3088 return written;
3089}
3090
3091/* prepare workqueue_attrs for sysfs store operations */
3092static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq)
3093{
3094 struct workqueue_attrs *attrs;
3095
3096 attrs = alloc_workqueue_attrs(GFP_KERNEL);
3097 if (!attrs)
3098 return NULL;
3099
3100 rcu_read_lock_sched();
3101 copy_workqueue_attrs(attrs, first_pwq(wq)->pool->attrs);
3102 rcu_read_unlock_sched();
3103 return attrs;
3104}
3105
3106static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr,
3107 const char *buf, size_t count)
3108{
3109 struct workqueue_struct *wq = dev_to_wq(dev);
3110 struct workqueue_attrs *attrs;
3111 int ret;
3112
3113 attrs = wq_sysfs_prep_attrs(wq);
3114 if (!attrs)
3115 return -ENOMEM;
3116
3117 if (sscanf(buf, "%d", &attrs->nice) == 1 &&
3118 attrs->nice >= -20 && attrs->nice <= 19)
3119 ret = apply_workqueue_attrs(wq, attrs);
3120 else
3121 ret = -EINVAL;
3122
3123 free_workqueue_attrs(attrs);
3124 return ret ?: count;
3125}
3126
3127static ssize_t wq_cpumask_show(struct device *dev,
3128 struct device_attribute *attr, char *buf)
3129{
3130 struct workqueue_struct *wq = dev_to_wq(dev);
3131 int written;
3132
3133 rcu_read_lock_sched();
3134 written = cpumask_scnprintf(buf, PAGE_SIZE,
3135 first_pwq(wq)->pool->attrs->cpumask);
3136 rcu_read_unlock_sched();
3137
3138 written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
3139 return written;
3140}
3141
3142static ssize_t wq_cpumask_store(struct device *dev,
3143 struct device_attribute *attr,
3144 const char *buf, size_t count)
3145{
3146 struct workqueue_struct *wq = dev_to_wq(dev);
3147 struct workqueue_attrs *attrs;
3148 int ret;
3149
3150 attrs = wq_sysfs_prep_attrs(wq);
3151 if (!attrs)
3152 return -ENOMEM;
3153
3154 ret = cpumask_parse(buf, attrs->cpumask);
3155 if (!ret)
3156 ret = apply_workqueue_attrs(wq, attrs);
3157
3158 free_workqueue_attrs(attrs);
3159 return ret ?: count;
3160}
3161
3162static struct device_attribute wq_sysfs_unbound_attrs[] = {
3163 __ATTR(pool_id, 0444, wq_pool_id_show, NULL),
3164 __ATTR(nice, 0644, wq_nice_show, wq_nice_store),
3165 __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store),
3166 __ATTR_NULL,
3167};
3168
3169static struct bus_type wq_subsys = {
3170 .name = "workqueue",
3171 .dev_attrs = wq_sysfs_attrs,
3172};
3173
3174static int __init wq_sysfs_init(void)
3175{
3176 return subsys_virtual_register(&wq_subsys, NULL);
3177}
3178core_initcall(wq_sysfs_init);
3179
3180static void wq_device_release(struct device *dev)
3181{
3182 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
3183
3184 kfree(wq_dev);
3185}
3186
3187/**
3188 * workqueue_sysfs_register - make a workqueue visible in sysfs
3189 * @wq: the workqueue to register
3190 *
3191 * Expose @wq in sysfs under /sys/bus/workqueue/devices.
3192 * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set
3193 * which is the preferred method.
3194 *
3195 * Workqueue user should use this function directly iff it wants to apply
3196 * workqueue_attrs before making the workqueue visible in sysfs; otherwise,
3197 * apply_workqueue_attrs() may race against userland updating the
3198 * attributes.
3199 *
3200 * Returns 0 on success, -errno on failure.
3201 */
3202int workqueue_sysfs_register(struct workqueue_struct *wq)
3203{
3204 struct wq_device *wq_dev;
3205 int ret;
3206
3207 /*
3208 * Adjusting max_active or creating new pwqs by applyting
3209 * attributes breaks ordering guarantee. Disallow exposing ordered
3210 * workqueues.
3211 */
3212 if (WARN_ON(wq->flags & __WQ_ORDERED))
3213 return -EINVAL;
3214
3215 wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL);
3216 if (!wq_dev)
3217 return -ENOMEM;
3218
3219 wq_dev->wq = wq;
3220 wq_dev->dev.bus = &wq_subsys;
3221 wq_dev->dev.init_name = wq->name;
3222 wq_dev->dev.release = wq_device_release;
3223
3224 /*
3225 * unbound_attrs are created separately. Suppress uevent until
3226 * everything is ready.
3227 */
3228 dev_set_uevent_suppress(&wq_dev->dev, true);
3229
3230 ret = device_register(&wq_dev->dev);
3231 if (ret) {
3232 kfree(wq_dev);
3233 wq->wq_dev = NULL;
3234 return ret;
3235 }
3236
3237 if (wq->flags & WQ_UNBOUND) {
3238 struct device_attribute *attr;
3239
3240 for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) {
3241 ret = device_create_file(&wq_dev->dev, attr);
3242 if (ret) {
3243 device_unregister(&wq_dev->dev);
3244 wq->wq_dev = NULL;
3245 return ret;
3246 }
3247 }
3248 }
3249
3250 kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD);
3251 return 0;
3252}
3253
3254/**
3255 * workqueue_sysfs_unregister - undo workqueue_sysfs_register()
3256 * @wq: the workqueue to unregister
3257 *
3258 * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister.
3259 */
3260static void workqueue_sysfs_unregister(struct workqueue_struct *wq)
3261{
3262 struct wq_device *wq_dev = wq->wq_dev;
3263
3264 if (!wq->wq_dev)
3265 return;
3266
3267 wq->wq_dev = NULL;
3268 device_unregister(&wq_dev->dev);
3269}
3270#else /* CONFIG_SYSFS */
3271static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { }
3272#endif /* CONFIG_SYSFS */
3273
Tejun Heo7a4e3442013-03-12 11:30:00 -07003274/**
3275 * free_workqueue_attrs - free a workqueue_attrs
3276 * @attrs: workqueue_attrs to free
3277 *
3278 * Undo alloc_workqueue_attrs().
3279 */
3280void free_workqueue_attrs(struct workqueue_attrs *attrs)
3281{
3282 if (attrs) {
3283 free_cpumask_var(attrs->cpumask);
3284 kfree(attrs);
3285 }
3286}
3287
3288/**
3289 * alloc_workqueue_attrs - allocate a workqueue_attrs
3290 * @gfp_mask: allocation mask to use
3291 *
3292 * Allocate a new workqueue_attrs, initialize with default settings and
3293 * return it. Returns NULL on failure.
3294 */
3295struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask)
3296{
3297 struct workqueue_attrs *attrs;
3298
3299 attrs = kzalloc(sizeof(*attrs), gfp_mask);
3300 if (!attrs)
3301 goto fail;
3302 if (!alloc_cpumask_var(&attrs->cpumask, gfp_mask))
3303 goto fail;
3304
Tejun Heo13e2e552013-04-01 11:23:31 -07003305 cpumask_copy(attrs->cpumask, cpu_possible_mask);
Tejun Heo7a4e3442013-03-12 11:30:00 -07003306 return attrs;
3307fail:
3308 free_workqueue_attrs(attrs);
3309 return NULL;
3310}
3311
Tejun Heo29c91e92013-03-12 11:30:03 -07003312static void copy_workqueue_attrs(struct workqueue_attrs *to,
3313 const struct workqueue_attrs *from)
3314{
3315 to->nice = from->nice;
3316 cpumask_copy(to->cpumask, from->cpumask);
3317}
3318
Tejun Heo29c91e92013-03-12 11:30:03 -07003319/* hash value of the content of @attr */
3320static u32 wqattrs_hash(const struct workqueue_attrs *attrs)
3321{
3322 u32 hash = 0;
3323
3324 hash = jhash_1word(attrs->nice, hash);
Tejun Heo13e2e552013-04-01 11:23:31 -07003325 hash = jhash(cpumask_bits(attrs->cpumask),
3326 BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash);
Tejun Heo29c91e92013-03-12 11:30:03 -07003327 return hash;
3328}
3329
3330/* content equality test */
3331static bool wqattrs_equal(const struct workqueue_attrs *a,
3332 const struct workqueue_attrs *b)
3333{
3334 if (a->nice != b->nice)
3335 return false;
3336 if (!cpumask_equal(a->cpumask, b->cpumask))
3337 return false;
3338 return true;
3339}
3340
Tejun Heo7a4e3442013-03-12 11:30:00 -07003341/**
3342 * init_worker_pool - initialize a newly zalloc'd worker_pool
3343 * @pool: worker_pool to initialize
3344 *
3345 * Initiailize a newly zalloc'd @pool. It also allocates @pool->attrs.
Tejun Heo29c91e92013-03-12 11:30:03 -07003346 * Returns 0 on success, -errno on failure. Even on failure, all fields
3347 * inside @pool proper are initialized and put_unbound_pool() can be called
3348 * on @pool safely to release it.
Tejun Heo7a4e3442013-03-12 11:30:00 -07003349 */
3350static int init_worker_pool(struct worker_pool *pool)
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003351{
3352 spin_lock_init(&pool->lock);
Tejun Heo29c91e92013-03-12 11:30:03 -07003353 pool->id = -1;
3354 pool->cpu = -1;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003355 pool->flags |= POOL_DISASSOCIATED;
3356 INIT_LIST_HEAD(&pool->worklist);
3357 INIT_LIST_HEAD(&pool->idle_list);
3358 hash_init(pool->busy_hash);
3359
3360 init_timer_deferrable(&pool->idle_timer);
3361 pool->idle_timer.function = idle_worker_timeout;
3362 pool->idle_timer.data = (unsigned long)pool;
3363
3364 setup_timer(&pool->mayday_timer, pool_mayday_timeout,
3365 (unsigned long)pool);
3366
3367 mutex_init(&pool->manager_arb);
Tejun Heobc3a1af2013-03-13 19:47:39 -07003368 mutex_init(&pool->manager_mutex);
Tejun Heo822d8402013-03-19 13:45:21 -07003369 idr_init(&pool->worker_idr);
Tejun Heo7a4e3442013-03-12 11:30:00 -07003370
Tejun Heo29c91e92013-03-12 11:30:03 -07003371 INIT_HLIST_NODE(&pool->hash_node);
3372 pool->refcnt = 1;
3373
3374 /* shouldn't fail above this point */
Tejun Heo7a4e3442013-03-12 11:30:00 -07003375 pool->attrs = alloc_workqueue_attrs(GFP_KERNEL);
3376 if (!pool->attrs)
3377 return -ENOMEM;
3378 return 0;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003379}
3380
Tejun Heo29c91e92013-03-12 11:30:03 -07003381static void rcu_free_pool(struct rcu_head *rcu)
3382{
3383 struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu);
3384
Tejun Heo822d8402013-03-19 13:45:21 -07003385 idr_destroy(&pool->worker_idr);
Tejun Heo29c91e92013-03-12 11:30:03 -07003386 free_workqueue_attrs(pool->attrs);
3387 kfree(pool);
3388}
3389
3390/**
3391 * put_unbound_pool - put a worker_pool
3392 * @pool: worker_pool to put
3393 *
3394 * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003395 * safe manner. get_unbound_pool() calls this function on its failure path
3396 * and this function should be able to release pools which went through,
3397 * successfully or not, init_worker_pool().
Tejun Heo29c91e92013-03-12 11:30:03 -07003398 */
3399static void put_unbound_pool(struct worker_pool *pool)
3400{
3401 struct worker *worker;
3402
Lai Jiangshan68e13a62013-03-25 16:57:17 -07003403 mutex_lock(&wq_pool_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003404 if (--pool->refcnt) {
Lai Jiangshan68e13a62013-03-25 16:57:17 -07003405 mutex_unlock(&wq_pool_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003406 return;
3407 }
3408
3409 /* sanity checks */
3410 if (WARN_ON(!(pool->flags & POOL_DISASSOCIATED)) ||
3411 WARN_ON(!list_empty(&pool->worklist))) {
Lai Jiangshan68e13a62013-03-25 16:57:17 -07003412 mutex_unlock(&wq_pool_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003413 return;
3414 }
3415
3416 /* release id and unhash */
3417 if (pool->id >= 0)
3418 idr_remove(&worker_pool_idr, pool->id);
3419 hash_del(&pool->hash_node);
3420
Lai Jiangshan68e13a62013-03-25 16:57:17 -07003421 mutex_unlock(&wq_pool_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003422
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003423 /*
3424 * Become the manager and destroy all workers. Grabbing
3425 * manager_arb prevents @pool's workers from blocking on
3426 * manager_mutex.
3427 */
Tejun Heo29c91e92013-03-12 11:30:03 -07003428 mutex_lock(&pool->manager_arb);
Tejun Heocd549682013-03-13 19:47:39 -07003429 mutex_lock(&pool->manager_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003430 spin_lock_irq(&pool->lock);
3431
3432 while ((worker = first_worker(pool)))
3433 destroy_worker(worker);
3434 WARN_ON(pool->nr_workers || pool->nr_idle);
3435
3436 spin_unlock_irq(&pool->lock);
Tejun Heocd549682013-03-13 19:47:39 -07003437 mutex_unlock(&pool->manager_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003438 mutex_unlock(&pool->manager_arb);
3439
3440 /* shut down the timers */
3441 del_timer_sync(&pool->idle_timer);
3442 del_timer_sync(&pool->mayday_timer);
3443
3444 /* sched-RCU protected to allow dereferences from get_work_pool() */
3445 call_rcu_sched(&pool->rcu, rcu_free_pool);
3446}
3447
3448/**
3449 * get_unbound_pool - get a worker_pool with the specified attributes
3450 * @attrs: the attributes of the worker_pool to get
3451 *
3452 * Obtain a worker_pool which has the same attributes as @attrs, bump the
3453 * reference count and return it. If there already is a matching
3454 * worker_pool, it will be used; otherwise, this function attempts to
3455 * create a new one. On failure, returns NULL.
3456 */
3457static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs)
3458{
Tejun Heo29c91e92013-03-12 11:30:03 -07003459 u32 hash = wqattrs_hash(attrs);
3460 struct worker_pool *pool;
Tejun Heo29c91e92013-03-12 11:30:03 -07003461
Lai Jiangshan68e13a62013-03-25 16:57:17 -07003462 mutex_lock(&wq_pool_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003463
3464 /* do we already have a matching pool? */
Tejun Heo29c91e92013-03-12 11:30:03 -07003465 hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) {
3466 if (wqattrs_equal(pool->attrs, attrs)) {
3467 pool->refcnt++;
3468 goto out_unlock;
3469 }
3470 }
Tejun Heo29c91e92013-03-12 11:30:03 -07003471
3472 /* nope, create a new one */
3473 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
3474 if (!pool || init_worker_pool(pool) < 0)
3475 goto fail;
3476
Lai Jiangshan12ee4fc2013-03-20 03:28:01 +08003477 if (workqueue_freezing)
3478 pool->flags |= POOL_FREEZING;
3479
Tejun Heo8864b4e2013-03-12 11:30:04 -07003480 lockdep_set_subclass(&pool->lock, 1); /* see put_pwq() */
Tejun Heo29c91e92013-03-12 11:30:03 -07003481 copy_workqueue_attrs(pool->attrs, attrs);
3482
3483 if (worker_pool_assign_id(pool) < 0)
3484 goto fail;
3485
3486 /* create and start the initial worker */
Tejun Heoebf44d12013-03-13 19:47:39 -07003487 if (create_and_start_worker(pool) < 0)
Tejun Heo29c91e92013-03-12 11:30:03 -07003488 goto fail;
3489
Tejun Heo29c91e92013-03-12 11:30:03 -07003490 /* install */
Tejun Heo29c91e92013-03-12 11:30:03 -07003491 hash_add(unbound_pool_hash, &pool->hash_node, hash);
3492out_unlock:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07003493 mutex_unlock(&wq_pool_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003494 return pool;
3495fail:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07003496 mutex_unlock(&wq_pool_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003497 if (pool)
3498 put_unbound_pool(pool);
3499 return NULL;
3500}
3501
Tejun Heo8864b4e2013-03-12 11:30:04 -07003502static void rcu_free_pwq(struct rcu_head *rcu)
3503{
3504 kmem_cache_free(pwq_cache,
3505 container_of(rcu, struct pool_workqueue, rcu));
3506}
3507
3508/*
3509 * Scheduled on system_wq by put_pwq() when an unbound pwq hits zero refcnt
3510 * and needs to be destroyed.
3511 */
3512static void pwq_unbound_release_workfn(struct work_struct *work)
3513{
3514 struct pool_workqueue *pwq = container_of(work, struct pool_workqueue,
3515 unbound_release_work);
3516 struct workqueue_struct *wq = pwq->wq;
3517 struct worker_pool *pool = pwq->pool;
Tejun Heobc0caf02013-04-01 11:23:31 -07003518 bool is_last;
Tejun Heo8864b4e2013-03-12 11:30:04 -07003519
3520 if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)))
3521 return;
3522
Tejun Heo75ccf592013-03-12 11:30:04 -07003523 /*
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003524 * Unlink @pwq. Synchronization against wq->mutex isn't strictly
Tejun Heo75ccf592013-03-12 11:30:04 -07003525 * necessary on release but do it anyway. It's easier to verify
3526 * and consistent with the linking path.
3527 */
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003528 mutex_lock(&wq->mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003529 list_del_rcu(&pwq->pwqs_node);
Tejun Heobc0caf02013-04-01 11:23:31 -07003530 is_last = list_empty(&wq->pwqs);
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003531 mutex_unlock(&wq->mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003532
3533 put_unbound_pool(pool);
3534 call_rcu_sched(&pwq->rcu, rcu_free_pwq);
3535
3536 /*
3537 * If we're the last pwq going away, @wq is already dead and no one
3538 * is gonna access it anymore. Free it.
3539 */
Tejun Heobc0caf02013-04-01 11:23:31 -07003540 if (is_last)
Tejun Heo8864b4e2013-03-12 11:30:04 -07003541 kfree(wq);
3542}
3543
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003544/**
Tejun Heo699ce092013-03-13 16:51:35 -07003545 * pwq_adjust_max_active - update a pwq's max_active to the current setting
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003546 * @pwq: target pool_workqueue
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003547 *
Tejun Heo699ce092013-03-13 16:51:35 -07003548 * If @pwq isn't freezing, set @pwq->max_active to the associated
3549 * workqueue's saved_max_active and activate delayed work items
3550 * accordingly. If @pwq is freezing, clear @pwq->max_active to zero.
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003551 */
Tejun Heo699ce092013-03-13 16:51:35 -07003552static void pwq_adjust_max_active(struct pool_workqueue *pwq)
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003553{
Tejun Heo699ce092013-03-13 16:51:35 -07003554 struct workqueue_struct *wq = pwq->wq;
3555 bool freezable = wq->flags & WQ_FREEZABLE;
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003556
Tejun Heo699ce092013-03-13 16:51:35 -07003557 /* for @wq->saved_max_active */
Lai Jiangshana357fc02013-03-25 16:57:19 -07003558 lockdep_assert_held(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07003559
3560 /* fast exit for non-freezable wqs */
3561 if (!freezable && pwq->max_active == wq->saved_max_active)
3562 return;
3563
Lai Jiangshana357fc02013-03-25 16:57:19 -07003564 spin_lock_irq(&pwq->pool->lock);
Tejun Heo699ce092013-03-13 16:51:35 -07003565
3566 if (!freezable || !(pwq->pool->flags & POOL_FREEZING)) {
3567 pwq->max_active = wq->saved_max_active;
3568
3569 while (!list_empty(&pwq->delayed_works) &&
3570 pwq->nr_active < pwq->max_active)
3571 pwq_activate_first_delayed(pwq);
Lai Jiangshan951a0782013-03-20 10:52:30 -07003572
3573 /*
3574 * Need to kick a worker after thawed or an unbound wq's
3575 * max_active is bumped. It's a slow path. Do it always.
3576 */
3577 wake_up_worker(pwq->pool);
Tejun Heo699ce092013-03-13 16:51:35 -07003578 } else {
3579 pwq->max_active = 0;
3580 }
3581
Lai Jiangshana357fc02013-03-25 16:57:19 -07003582 spin_unlock_irq(&pwq->pool->lock);
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003583}
3584
Tejun Heod2c1d402013-03-12 11:30:04 -07003585static void init_and_link_pwq(struct pool_workqueue *pwq,
3586 struct workqueue_struct *wq,
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003587 struct worker_pool *pool,
3588 struct pool_workqueue **p_last_pwq)
Tejun Heod2c1d402013-03-12 11:30:04 -07003589{
3590 BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK);
3591
3592 pwq->pool = pool;
3593 pwq->wq = wq;
3594 pwq->flush_color = -1;
Tejun Heo8864b4e2013-03-12 11:30:04 -07003595 pwq->refcnt = 1;
Tejun Heod2c1d402013-03-12 11:30:04 -07003596 INIT_LIST_HEAD(&pwq->delayed_works);
3597 INIT_LIST_HEAD(&pwq->mayday_node);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003598 INIT_WORK(&pwq->unbound_release_work, pwq_unbound_release_workfn);
Tejun Heod2c1d402013-03-12 11:30:04 -07003599
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003600 mutex_lock(&wq->mutex);
Tejun Heo75ccf592013-03-12 11:30:04 -07003601
Tejun Heo983ca252013-03-13 16:51:35 -07003602 /*
3603 * Set the matching work_color. This is synchronized with
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003604 * wq->mutex to avoid confusing flush_workqueue().
Tejun Heo983ca252013-03-13 16:51:35 -07003605 */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003606 if (p_last_pwq)
3607 *p_last_pwq = first_pwq(wq);
Tejun Heo75ccf592013-03-12 11:30:04 -07003608 pwq->work_color = wq->work_color;
Tejun Heo983ca252013-03-13 16:51:35 -07003609
3610 /* sync max_active to the current setting */
3611 pwq_adjust_max_active(pwq);
3612
3613 /* link in @pwq */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003614 list_add_rcu(&pwq->pwqs_node, &wq->pwqs);
Lai Jiangshana357fc02013-03-25 16:57:19 -07003615
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003616 mutex_unlock(&wq->mutex);
Tejun Heod2c1d402013-03-12 11:30:04 -07003617}
3618
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003619/**
3620 * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue
3621 * @wq: the target workqueue
3622 * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs()
3623 *
3624 * Apply @attrs to an unbound workqueue @wq. If @attrs doesn't match the
3625 * current attributes, a new pwq is created and made the first pwq which
3626 * will serve all new work items. Older pwqs are released as in-flight
3627 * work items finish. Note that a work item which repeatedly requeues
3628 * itself back-to-back will stay on its current pwq.
3629 *
3630 * Performs GFP_KERNEL allocations. Returns 0 on success and -errno on
3631 * failure.
3632 */
3633int apply_workqueue_attrs(struct workqueue_struct *wq,
3634 const struct workqueue_attrs *attrs)
3635{
Tejun Heo13e2e552013-04-01 11:23:31 -07003636 struct workqueue_attrs *new_attrs;
3637 struct pool_workqueue *pwq = NULL, *last_pwq;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003638 struct worker_pool *pool;
Tejun Heo48621252013-04-01 11:23:31 -07003639 int ret;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003640
Tejun Heo8719dce2013-03-12 11:30:04 -07003641 /* only unbound workqueues can change attributes */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003642 if (WARN_ON(!(wq->flags & WQ_UNBOUND)))
3643 return -EINVAL;
3644
Tejun Heo8719dce2013-03-12 11:30:04 -07003645 /* creating multiple pwqs breaks ordering guarantee */
3646 if (WARN_ON((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs)))
3647 return -EINVAL;
3648
Tejun Heo13e2e552013-04-01 11:23:31 -07003649 /* make a copy of @attrs and sanitize it */
3650 new_attrs = alloc_workqueue_attrs(GFP_KERNEL);
3651 if (!new_attrs)
3652 goto enomem;
3653
3654 copy_workqueue_attrs(new_attrs, attrs);
3655 cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask);
3656
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003657 pwq = kmem_cache_zalloc(pwq_cache, GFP_KERNEL);
3658 if (!pwq)
Tejun Heo13e2e552013-04-01 11:23:31 -07003659 goto enomem;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003660
Tejun Heo13e2e552013-04-01 11:23:31 -07003661 pool = get_unbound_pool(new_attrs);
3662 if (!pool)
3663 goto enomem;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003664
3665 init_and_link_pwq(pwq, wq, pool, &last_pwq);
3666 if (last_pwq) {
3667 spin_lock_irq(&last_pwq->pool->lock);
3668 put_pwq(last_pwq);
3669 spin_unlock_irq(&last_pwq->pool->lock);
3670 }
3671
Tejun Heo48621252013-04-01 11:23:31 -07003672 ret = 0;
3673 /* fall through */
3674out_free:
3675 free_workqueue_attrs(new_attrs);
3676 return ret;
Tejun Heo13e2e552013-04-01 11:23:31 -07003677
3678enomem:
3679 kmem_cache_free(pwq_cache, pwq);
Tejun Heo48621252013-04-01 11:23:31 -07003680 ret = -ENOMEM;
3681 goto out_free;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003682}
3683
Tejun Heo30cdf242013-03-12 11:29:57 -07003684static int alloc_and_link_pwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003686 bool highpri = wq->flags & WQ_HIGHPRI;
Tejun Heo30cdf242013-03-12 11:29:57 -07003687 int cpu;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01003688
Tejun Heo30cdf242013-03-12 11:29:57 -07003689 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo420c0dd2013-03-12 11:29:59 -07003690 wq->cpu_pwqs = alloc_percpu(struct pool_workqueue);
3691 if (!wq->cpu_pwqs)
Tejun Heo30cdf242013-03-12 11:29:57 -07003692 return -ENOMEM;
3693
3694 for_each_possible_cpu(cpu) {
Tejun Heo7fb98ea2013-03-12 11:30:00 -07003695 struct pool_workqueue *pwq =
3696 per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heo7a62c2c2013-03-12 11:30:03 -07003697 struct worker_pool *cpu_pools =
Tejun Heof02ae732013-03-12 11:30:03 -07003698 per_cpu(cpu_worker_pools, cpu);
Tejun Heo30cdf242013-03-12 11:29:57 -07003699
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003700 init_and_link_pwq(pwq, wq, &cpu_pools[highpri], NULL);
Tejun Heo30cdf242013-03-12 11:29:57 -07003701 }
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003702 return 0;
Tejun Heo30cdf242013-03-12 11:29:57 -07003703 } else {
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003704 return apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]);
Tejun Heo30cdf242013-03-12 11:29:57 -07003705 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003706}
3707
Tejun Heof3421792010-07-02 10:03:51 +02003708static int wq_clamp_max_active(int max_active, unsigned int flags,
3709 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003710{
Tejun Heof3421792010-07-02 10:03:51 +02003711 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3712
3713 if (max_active < 1 || max_active > lim)
Valentin Ilie044c7822012-08-19 00:52:42 +03003714 pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
3715 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003716
Tejun Heof3421792010-07-02 10:03:51 +02003717 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003718}
3719
Tejun Heob196be82012-01-10 15:11:35 -08003720struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02003721 unsigned int flags,
3722 int max_active,
3723 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08003724 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003725{
Tejun Heob196be82012-01-10 15:11:35 -08003726 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003727 struct workqueue_struct *wq;
Tejun Heo49e3cf42013-03-12 11:29:58 -07003728 struct pool_workqueue *pwq;
Tejun Heob196be82012-01-10 15:11:35 -08003729 size_t namelen;
3730
3731 /* determine namelen, allocate wq and format name */
3732 va_start(args, lock_name);
3733 va_copy(args1, args);
3734 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3735
3736 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3737 if (!wq)
Tejun Heod2c1d402013-03-12 11:30:04 -07003738 return NULL;
Tejun Heob196be82012-01-10 15:11:35 -08003739
3740 vsnprintf(wq->name, namelen, fmt, args1);
3741 va_end(args);
3742 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003743
Tejun Heod320c032010-06-29 10:07:14 +02003744 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003745 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003746
Tejun Heob196be82012-01-10 15:11:35 -08003747 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003748 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003749 wq->saved_max_active = max_active;
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003750 mutex_init(&wq->mutex);
Tejun Heo112202d2013-02-13 19:29:12 -08003751 atomic_set(&wq->nr_pwqs_to_flush, 0);
Tejun Heo30cdf242013-03-12 11:29:57 -07003752 INIT_LIST_HEAD(&wq->pwqs);
Tejun Heo73f53c42010-06-29 10:07:11 +02003753 INIT_LIST_HEAD(&wq->flusher_queue);
3754 INIT_LIST_HEAD(&wq->flusher_overflow);
Tejun Heo493a1722013-03-12 11:29:59 -07003755 INIT_LIST_HEAD(&wq->maydays);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003756
Johannes Bergeb13ba82008-01-16 09:51:58 +01003757 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003758 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003759
Tejun Heo30cdf242013-03-12 11:29:57 -07003760 if (alloc_and_link_pwqs(wq) < 0)
Tejun Heod2c1d402013-03-12 11:30:04 -07003761 goto err_free_wq;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003762
Tejun Heo493008a2013-03-12 11:30:03 -07003763 /*
3764 * Workqueues which may be used during memory reclaim should
3765 * have a rescuer to guarantee forward progress.
3766 */
3767 if (flags & WQ_MEM_RECLAIM) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003768 struct worker *rescuer;
3769
Tejun Heod2c1d402013-03-12 11:30:04 -07003770 rescuer = alloc_worker();
Tejun Heoe22bee72010-06-29 10:07:14 +02003771 if (!rescuer)
Tejun Heod2c1d402013-03-12 11:30:04 -07003772 goto err_destroy;
Tejun Heoe22bee72010-06-29 10:07:14 +02003773
Tejun Heo111c2252013-01-17 17:16:24 -08003774 rescuer->rescue_wq = wq;
3775 rescuer->task = kthread_create(rescuer_thread, rescuer, "%s",
Tejun Heob196be82012-01-10 15:11:35 -08003776 wq->name);
Tejun Heod2c1d402013-03-12 11:30:04 -07003777 if (IS_ERR(rescuer->task)) {
3778 kfree(rescuer);
3779 goto err_destroy;
3780 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003781
Tejun Heod2c1d402013-03-12 11:30:04 -07003782 wq->rescuer = rescuer;
Tejun Heo14a40ff2013-03-19 13:45:20 -07003783 rescuer->task->flags |= PF_NO_SETAFFINITY;
Tejun Heoe22bee72010-06-29 10:07:14 +02003784 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003785 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003786
Tejun Heo226223a2013-03-12 11:30:05 -07003787 if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq))
3788 goto err_destroy;
3789
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003790 /*
Lai Jiangshan68e13a62013-03-25 16:57:17 -07003791 * wq_pool_mutex protects global freeze state and workqueues list.
3792 * Grab it, adjust max_active and add the new @wq to workqueues
3793 * list.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003794 */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07003795 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003796
Lai Jiangshana357fc02013-03-25 16:57:19 -07003797 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07003798 for_each_pwq(pwq, wq)
3799 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07003800 mutex_unlock(&wq->mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003801
Tejun Heo15376632010-06-29 10:07:11 +02003802 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003803
Lai Jiangshan68e13a62013-03-25 16:57:17 -07003804 mutex_unlock(&wq_pool_mutex);
Tejun Heo15376632010-06-29 10:07:11 +02003805
Oleg Nesterov3af244332007-05-09 02:34:09 -07003806 return wq;
Tejun Heod2c1d402013-03-12 11:30:04 -07003807
3808err_free_wq:
3809 kfree(wq);
3810 return NULL;
3811err_destroy:
3812 destroy_workqueue(wq);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003813 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003814}
Tejun Heod320c032010-06-29 10:07:14 +02003815EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003816
3817/**
3818 * destroy_workqueue - safely terminate a workqueue
3819 * @wq: target workqueue
3820 *
3821 * Safely destroy a workqueue. All work currently pending will be done first.
3822 */
3823void destroy_workqueue(struct workqueue_struct *wq)
3824{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003825 struct pool_workqueue *pwq;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003826
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003827 /* drain it before proceeding with destruction */
3828 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003829
Tejun Heo6183c002013-03-12 11:29:57 -07003830 /* sanity checks */
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07003831 mutex_lock(&wq->mutex);
Tejun Heo49e3cf42013-03-12 11:29:58 -07003832 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07003833 int i;
3834
Tejun Heo76af4d92013-03-12 11:30:00 -07003835 for (i = 0; i < WORK_NR_COLORS; i++) {
3836 if (WARN_ON(pwq->nr_in_flight[i])) {
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07003837 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07003838 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07003839 }
3840 }
3841
Tejun Heo8864b4e2013-03-12 11:30:04 -07003842 if (WARN_ON(pwq->refcnt > 1) ||
3843 WARN_ON(pwq->nr_active) ||
Tejun Heo76af4d92013-03-12 11:30:00 -07003844 WARN_ON(!list_empty(&pwq->delayed_works))) {
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07003845 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07003846 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07003847 }
Tejun Heo6183c002013-03-12 11:29:57 -07003848 }
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07003849 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07003850
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003851 /*
3852 * wq list is used to freeze wq, remove from list after
3853 * flushing is complete in case freeze races us.
3854 */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07003855 mutex_lock(&wq_pool_mutex);
Tejun Heod2c1d402013-03-12 11:30:04 -07003856 list_del_init(&wq->list);
Lai Jiangshan68e13a62013-03-25 16:57:17 -07003857 mutex_unlock(&wq_pool_mutex);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003858
Tejun Heo226223a2013-03-12 11:30:05 -07003859 workqueue_sysfs_unregister(wq);
3860
Tejun Heo493008a2013-03-12 11:30:03 -07003861 if (wq->rescuer) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003862 kthread_stop(wq->rescuer->task);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003863 kfree(wq->rescuer);
Tejun Heo493008a2013-03-12 11:30:03 -07003864 wq->rescuer = NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +02003865 }
3866
Tejun Heo8864b4e2013-03-12 11:30:04 -07003867 if (!(wq->flags & WQ_UNBOUND)) {
3868 /*
3869 * The base ref is never dropped on per-cpu pwqs. Directly
3870 * free the pwqs and wq.
3871 */
3872 free_percpu(wq->cpu_pwqs);
3873 kfree(wq);
3874 } else {
3875 /*
3876 * We're the sole accessor of @wq at this point. Directly
3877 * access the first pwq and put the base ref. As both pwqs
3878 * and pools are sched-RCU protected, the lock operations
3879 * are safe. @wq will be freed when the last pwq is
3880 * released.
3881 */
Tejun Heo29c91e92013-03-12 11:30:03 -07003882 pwq = list_first_entry(&wq->pwqs, struct pool_workqueue,
3883 pwqs_node);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003884 spin_lock_irq(&pwq->pool->lock);
3885 put_pwq(pwq);
3886 spin_unlock_irq(&pwq->pool->lock);
Tejun Heo29c91e92013-03-12 11:30:03 -07003887 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003888}
3889EXPORT_SYMBOL_GPL(destroy_workqueue);
3890
Tejun Heodcd989c2010-06-29 10:07:14 +02003891/**
3892 * workqueue_set_max_active - adjust max_active of a workqueue
3893 * @wq: target workqueue
3894 * @max_active: new max_active value.
3895 *
3896 * Set max_active of @wq to @max_active.
3897 *
3898 * CONTEXT:
3899 * Don't call from IRQ context.
3900 */
3901void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3902{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003903 struct pool_workqueue *pwq;
Tejun Heodcd989c2010-06-29 10:07:14 +02003904
Tejun Heo8719dce2013-03-12 11:30:04 -07003905 /* disallow meddling with max_active for ordered workqueues */
3906 if (WARN_ON(wq->flags & __WQ_ORDERED))
3907 return;
3908
Tejun Heof3421792010-07-02 10:03:51 +02003909 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003910
Lai Jiangshana357fc02013-03-25 16:57:19 -07003911 mutex_lock(&wq->mutex);
Tejun Heodcd989c2010-06-29 10:07:14 +02003912
3913 wq->saved_max_active = max_active;
3914
Tejun Heo699ce092013-03-13 16:51:35 -07003915 for_each_pwq(pwq, wq)
3916 pwq_adjust_max_active(pwq);
Tejun Heodcd989c2010-06-29 10:07:14 +02003917
Lai Jiangshana357fc02013-03-25 16:57:19 -07003918 mutex_unlock(&wq->mutex);
Tejun Heodcd989c2010-06-29 10:07:14 +02003919}
3920EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3921
3922/**
Tejun Heoe6267612013-03-12 17:41:37 -07003923 * current_is_workqueue_rescuer - is %current workqueue rescuer?
3924 *
3925 * Determine whether %current is a workqueue rescuer. Can be used from
3926 * work functions to determine whether it's being run off the rescuer task.
3927 */
3928bool current_is_workqueue_rescuer(void)
3929{
3930 struct worker *worker = current_wq_worker();
3931
Lai Jiangshan6a092df2013-03-20 03:28:03 +08003932 return worker && worker->rescue_wq;
Tejun Heoe6267612013-03-12 17:41:37 -07003933}
3934
3935/**
Tejun Heodcd989c2010-06-29 10:07:14 +02003936 * workqueue_congested - test whether a workqueue is congested
3937 * @cpu: CPU in question
3938 * @wq: target workqueue
3939 *
3940 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3941 * no synchronization around this function and the test result is
3942 * unreliable and only useful as advisory hints or for debugging.
3943 *
3944 * RETURNS:
3945 * %true if congested, %false otherwise.
3946 */
Tejun Heod84ff052013-03-12 11:29:59 -07003947bool workqueue_congested(int cpu, struct workqueue_struct *wq)
Tejun Heodcd989c2010-06-29 10:07:14 +02003948{
Tejun Heo7fb98ea2013-03-12 11:30:00 -07003949 struct pool_workqueue *pwq;
Tejun Heo76af4d92013-03-12 11:30:00 -07003950 bool ret;
3951
Lai Jiangshan88109452013-03-20 03:28:10 +08003952 rcu_read_lock_sched();
Tejun Heo7fb98ea2013-03-12 11:30:00 -07003953
3954 if (!(wq->flags & WQ_UNBOUND))
3955 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
3956 else
3957 pwq = first_pwq(wq);
Tejun Heodcd989c2010-06-29 10:07:14 +02003958
Tejun Heo76af4d92013-03-12 11:30:00 -07003959 ret = !list_empty(&pwq->delayed_works);
Lai Jiangshan88109452013-03-20 03:28:10 +08003960 rcu_read_unlock_sched();
Tejun Heo76af4d92013-03-12 11:30:00 -07003961
3962 return ret;
Tejun Heodcd989c2010-06-29 10:07:14 +02003963}
3964EXPORT_SYMBOL_GPL(workqueue_congested);
3965
3966/**
Tejun Heodcd989c2010-06-29 10:07:14 +02003967 * work_busy - test whether a work is currently pending or running
3968 * @work: the work to be tested
3969 *
3970 * Test whether @work is currently pending or running. There is no
3971 * synchronization around this function and the test result is
3972 * unreliable and only useful as advisory hints or for debugging.
Tejun Heodcd989c2010-06-29 10:07:14 +02003973 *
3974 * RETURNS:
3975 * OR'd bitmask of WORK_BUSY_* bits.
3976 */
3977unsigned int work_busy(struct work_struct *work)
3978{
Tejun Heofa1b54e2013-03-12 11:30:00 -07003979 struct worker_pool *pool;
Tejun Heodcd989c2010-06-29 10:07:14 +02003980 unsigned long flags;
3981 unsigned int ret = 0;
3982
Tejun Heodcd989c2010-06-29 10:07:14 +02003983 if (work_pending(work))
3984 ret |= WORK_BUSY_PENDING;
Tejun Heodcd989c2010-06-29 10:07:14 +02003985
Tejun Heofa1b54e2013-03-12 11:30:00 -07003986 local_irq_save(flags);
3987 pool = get_work_pool(work);
Lai Jiangshan038366c2013-02-06 18:04:53 -08003988 if (pool) {
Tejun Heofa1b54e2013-03-12 11:30:00 -07003989 spin_lock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08003990 if (find_worker_executing_work(pool, work))
3991 ret |= WORK_BUSY_RUNNING;
Tejun Heofa1b54e2013-03-12 11:30:00 -07003992 spin_unlock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08003993 }
Tejun Heofa1b54e2013-03-12 11:30:00 -07003994 local_irq_restore(flags);
Tejun Heodcd989c2010-06-29 10:07:14 +02003995
3996 return ret;
3997}
3998EXPORT_SYMBOL_GPL(work_busy);
3999
Tejun Heodb7bccf2010-06-29 10:07:12 +02004000/*
4001 * CPU hotplug.
4002 *
Tejun Heoe22bee72010-06-29 10:07:14 +02004003 * There are two challenges in supporting CPU hotplug. Firstly, there
Tejun Heo112202d2013-02-13 19:29:12 -08004004 * are a lot of assumptions on strong associations among work, pwq and
Tejun Heo706026c2013-01-24 11:01:34 -08004005 * pool which make migrating pending and scheduled works very
Tejun Heoe22bee72010-06-29 10:07:14 +02004006 * difficult to implement without impacting hot paths. Secondly,
Tejun Heo94cf58b2013-01-24 11:01:33 -08004007 * worker pools serve mix of short, long and very long running works making
Tejun Heoe22bee72010-06-29 10:07:14 +02004008 * blocked draining impractical.
4009 *
Tejun Heo24647572013-01-24 11:01:33 -08004010 * This is solved by allowing the pools to be disassociated from the CPU
Tejun Heo628c78e2012-07-17 12:39:27 -07004011 * running as an unbound one and allowing it to be reattached later if the
4012 * cpu comes back online.
Tejun Heodb7bccf2010-06-29 10:07:12 +02004013 */
4014
Tejun Heo706026c2013-01-24 11:01:34 -08004015static void wq_unbind_fn(struct work_struct *work)
Tejun Heodb7bccf2010-06-29 10:07:12 +02004016{
Tejun Heo38db41d2013-01-24 11:01:34 -08004017 int cpu = smp_processor_id();
Tejun Heo4ce62e92012-07-13 22:16:44 -07004018 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004019 struct worker *worker;
Tejun Heoa9ab7752013-03-19 13:45:21 -07004020 int wi;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004021
Tejun Heof02ae732013-03-12 11:30:03 -07004022 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo6183c002013-03-12 11:29:57 -07004023 WARN_ON_ONCE(cpu != smp_processor_id());
Tejun Heo94cf58b2013-01-24 11:01:33 -08004024
Tejun Heobc3a1af2013-03-13 19:47:39 -07004025 mutex_lock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004026 spin_lock_irq(&pool->lock);
4027
4028 /*
Tejun Heobc3a1af2013-03-13 19:47:39 -07004029 * We've blocked all manager operations. Make all workers
Tejun Heo94cf58b2013-01-24 11:01:33 -08004030 * unbound and set DISASSOCIATED. Before this, all workers
4031 * except for the ones which are still executing works from
4032 * before the last CPU down must be on the cpu. After
4033 * this, they may become diasporas.
4034 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07004035 for_each_pool_worker(worker, wi, pool)
Tejun Heoc9e7cf22013-01-24 11:01:33 -08004036 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004037
Tejun Heo24647572013-01-24 11:01:33 -08004038 pool->flags |= POOL_DISASSOCIATED;
Tejun Heof2d5a0e2012-07-17 12:39:26 -07004039
Tejun Heo94cf58b2013-01-24 11:01:33 -08004040 spin_unlock_irq(&pool->lock);
Tejun Heobc3a1af2013-03-13 19:47:39 -07004041 mutex_unlock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004042 }
Tejun Heoe22bee72010-06-29 10:07:14 +02004043
4044 /*
Tejun Heo628c78e2012-07-17 12:39:27 -07004045 * Call schedule() so that we cross rq->lock and thus can guarantee
4046 * sched callbacks see the %WORKER_UNBOUND flag. This is necessary
4047 * as scheduler callbacks may be invoked from other cpus.
4048 */
4049 schedule();
4050
4051 /*
4052 * Sched callbacks are disabled now. Zap nr_running. After this,
4053 * nr_running stays zero and need_more_worker() and keep_working()
Tejun Heo38db41d2013-01-24 11:01:34 -08004054 * are always true as long as the worklist is not empty. Pools on
4055 * @cpu now behave as unbound (in terms of concurrency management)
4056 * pools which are served by workers tied to the CPU.
Tejun Heo628c78e2012-07-17 12:39:27 -07004057 *
4058 * On return from this function, the current worker would trigger
4059 * unbound chain execution of pending work items if other workers
4060 * didn't already.
Tejun Heoe22bee72010-06-29 10:07:14 +02004061 */
Tejun Heof02ae732013-03-12 11:30:03 -07004062 for_each_cpu_worker_pool(pool, cpu)
Tejun Heoe19e3972013-01-24 11:39:44 -08004063 atomic_set(&pool->nr_running, 0);
Tejun Heodb7bccf2010-06-29 10:07:12 +02004064}
4065
Tejun Heobd7c0892013-03-19 13:45:21 -07004066/**
4067 * rebind_workers - rebind all workers of a pool to the associated CPU
4068 * @pool: pool of interest
4069 *
Tejun Heoa9ab7752013-03-19 13:45:21 -07004070 * @pool->cpu is coming online. Rebind all workers to the CPU.
Tejun Heobd7c0892013-03-19 13:45:21 -07004071 */
4072static void rebind_workers(struct worker_pool *pool)
4073{
Tejun Heoa9ab7752013-03-19 13:45:21 -07004074 struct worker *worker;
4075 int wi;
Tejun Heobd7c0892013-03-19 13:45:21 -07004076
4077 lockdep_assert_held(&pool->manager_mutex);
Tejun Heobd7c0892013-03-19 13:45:21 -07004078
Tejun Heoa9ab7752013-03-19 13:45:21 -07004079 /*
4080 * Restore CPU affinity of all workers. As all idle workers should
4081 * be on the run-queue of the associated CPU before any local
4082 * wake-ups for concurrency management happen, restore CPU affinty
4083 * of all workers first and then clear UNBOUND. As we're called
4084 * from CPU_ONLINE, the following shouldn't fail.
4085 */
4086 for_each_pool_worker(worker, wi, pool)
4087 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
4088 pool->attrs->cpumask) < 0);
4089
4090 spin_lock_irq(&pool->lock);
4091
4092 for_each_pool_worker(worker, wi, pool) {
4093 unsigned int worker_flags = worker->flags;
Tejun Heobd7c0892013-03-19 13:45:21 -07004094
4095 /*
Tejun Heoa9ab7752013-03-19 13:45:21 -07004096 * A bound idle worker should actually be on the runqueue
4097 * of the associated CPU for local wake-ups targeting it to
4098 * work. Kick all idle workers so that they migrate to the
4099 * associated CPU. Doing this in the same loop as
4100 * replacing UNBOUND with REBOUND is safe as no worker will
4101 * be bound before @pool->lock is released.
Tejun Heobd7c0892013-03-19 13:45:21 -07004102 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07004103 if (worker_flags & WORKER_IDLE)
4104 wake_up_process(worker->task);
4105
4106 /*
4107 * We want to clear UNBOUND but can't directly call
4108 * worker_clr_flags() or adjust nr_running. Atomically
4109 * replace UNBOUND with another NOT_RUNNING flag REBOUND.
4110 * @worker will clear REBOUND using worker_clr_flags() when
4111 * it initiates the next execution cycle thus restoring
4112 * concurrency management. Note that when or whether
4113 * @worker clears REBOUND doesn't affect correctness.
4114 *
4115 * ACCESS_ONCE() is necessary because @worker->flags may be
4116 * tested without holding any lock in
4117 * wq_worker_waking_up(). Without it, NOT_RUNNING test may
4118 * fail incorrectly leading to premature concurrency
4119 * management operations.
4120 */
4121 WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND));
4122 worker_flags |= WORKER_REBOUND;
4123 worker_flags &= ~WORKER_UNBOUND;
4124 ACCESS_ONCE(worker->flags) = worker_flags;
Tejun Heobd7c0892013-03-19 13:45:21 -07004125 }
4126
Tejun Heoa9ab7752013-03-19 13:45:21 -07004127 spin_unlock_irq(&pool->lock);
Tejun Heobd7c0892013-03-19 13:45:21 -07004128}
4129
Tejun Heo7dbc7252013-03-19 13:45:21 -07004130/**
4131 * restore_unbound_workers_cpumask - restore cpumask of unbound workers
4132 * @pool: unbound pool of interest
4133 * @cpu: the CPU which is coming up
4134 *
4135 * An unbound pool may end up with a cpumask which doesn't have any online
4136 * CPUs. When a worker of such pool get scheduled, the scheduler resets
4137 * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any
4138 * online CPU before, cpus_allowed of all its workers should be restored.
4139 */
4140static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu)
4141{
4142 static cpumask_t cpumask;
4143 struct worker *worker;
4144 int wi;
4145
4146 lockdep_assert_held(&pool->manager_mutex);
4147
4148 /* is @cpu allowed for @pool? */
4149 if (!cpumask_test_cpu(cpu, pool->attrs->cpumask))
4150 return;
4151
4152 /* is @cpu the only online CPU? */
4153 cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask);
4154 if (cpumask_weight(&cpumask) != 1)
4155 return;
4156
4157 /* as we're called from CPU_ONLINE, the following shouldn't fail */
4158 for_each_pool_worker(worker, wi, pool)
4159 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
4160 pool->attrs->cpumask) < 0);
4161}
4162
Tejun Heo8db25e72012-07-17 12:39:28 -07004163/*
4164 * Workqueues should be brought up before normal priority CPU notifiers.
4165 * This will be registered high priority CPU notifier.
4166 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07004167static int __cpuinit workqueue_cpu_up_callback(struct notifier_block *nfb,
Tejun Heo8db25e72012-07-17 12:39:28 -07004168 unsigned long action,
4169 void *hcpu)
Oleg Nesterov3af244332007-05-09 02:34:09 -07004170{
Tejun Heod84ff052013-03-12 11:29:59 -07004171 int cpu = (unsigned long)hcpu;
Tejun Heo4ce62e92012-07-13 22:16:44 -07004172 struct worker_pool *pool;
Tejun Heo7dbc7252013-03-19 13:45:21 -07004173 int pi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174
Tejun Heo8db25e72012-07-17 12:39:28 -07004175 switch (action & ~CPU_TASKS_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 case CPU_UP_PREPARE:
Tejun Heof02ae732013-03-12 11:30:03 -07004177 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo3ce63372012-07-17 12:39:27 -07004178 if (pool->nr_workers)
4179 continue;
Tejun Heoebf44d12013-03-13 19:47:39 -07004180 if (create_and_start_worker(pool) < 0)
Tejun Heo3ce63372012-07-17 12:39:27 -07004181 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02004183 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07004184
Tejun Heo65758202012-07-17 12:39:26 -07004185 case CPU_DOWN_FAILED:
4186 case CPU_ONLINE:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004187 mutex_lock(&wq_pool_mutex);
Tejun Heo7dbc7252013-03-19 13:45:21 -07004188
4189 for_each_pool(pool, pi) {
Tejun Heobc3a1af2013-03-13 19:47:39 -07004190 mutex_lock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004191
Tejun Heo7dbc7252013-03-19 13:45:21 -07004192 if (pool->cpu == cpu) {
4193 spin_lock_irq(&pool->lock);
4194 pool->flags &= ~POOL_DISASSOCIATED;
4195 spin_unlock_irq(&pool->lock);
Tejun Heoa9ab7752013-03-19 13:45:21 -07004196
Tejun Heo7dbc7252013-03-19 13:45:21 -07004197 rebind_workers(pool);
4198 } else if (pool->cpu < 0) {
4199 restore_unbound_workers_cpumask(pool, cpu);
4200 }
Tejun Heo94cf58b2013-01-24 11:01:33 -08004201
Tejun Heobc3a1af2013-03-13 19:47:39 -07004202 mutex_unlock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004203 }
Tejun Heo7dbc7252013-03-19 13:45:21 -07004204
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004205 mutex_unlock(&wq_pool_mutex);
Tejun Heo8db25e72012-07-17 12:39:28 -07004206 break;
Tejun Heo65758202012-07-17 12:39:26 -07004207 }
4208 return NOTIFY_OK;
4209}
4210
4211/*
4212 * Workqueues should be brought down after normal priority CPU notifiers.
4213 * This will be registered as low priority CPU notifier.
4214 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07004215static int __cpuinit workqueue_cpu_down_callback(struct notifier_block *nfb,
Tejun Heo65758202012-07-17 12:39:26 -07004216 unsigned long action,
4217 void *hcpu)
4218{
Tejun Heod84ff052013-03-12 11:29:59 -07004219 int cpu = (unsigned long)hcpu;
Tejun Heo8db25e72012-07-17 12:39:28 -07004220 struct work_struct unbind_work;
4221
Tejun Heo65758202012-07-17 12:39:26 -07004222 switch (action & ~CPU_TASKS_FROZEN) {
4223 case CPU_DOWN_PREPARE:
Tejun Heo8db25e72012-07-17 12:39:28 -07004224 /* unbinding should happen on the local CPU */
Tejun Heo706026c2013-01-24 11:01:34 -08004225 INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn);
Joonsoo Kim7635d2f2012-08-15 23:25:41 +09004226 queue_work_on(cpu, system_highpri_wq, &unbind_work);
Tejun Heo8db25e72012-07-17 12:39:28 -07004227 flush_work(&unbind_work);
4228 break;
Tejun Heo65758202012-07-17 12:39:26 -07004229 }
4230 return NOTIFY_OK;
4231}
4232
Rusty Russell2d3854a2008-11-05 13:39:10 +11004233#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08004234
Rusty Russell2d3854a2008-11-05 13:39:10 +11004235struct work_for_cpu {
Tejun Heoed48ece2012-09-18 12:48:43 -07004236 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11004237 long (*fn)(void *);
4238 void *arg;
4239 long ret;
4240};
4241
Tejun Heoed48ece2012-09-18 12:48:43 -07004242static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11004243{
Tejun Heoed48ece2012-09-18 12:48:43 -07004244 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
4245
Rusty Russell2d3854a2008-11-05 13:39:10 +11004246 wfc->ret = wfc->fn(wfc->arg);
4247}
4248
4249/**
4250 * work_on_cpu - run a function in user context on a particular cpu
4251 * @cpu: the cpu to run on
4252 * @fn: the function to run
4253 * @arg: the function arg
4254 *
Rusty Russell31ad9082009-01-16 15:31:15 -08004255 * This will return the value @fn returns.
4256 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06004257 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11004258 */
Tejun Heod84ff052013-03-12 11:29:59 -07004259long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
Rusty Russell2d3854a2008-11-05 13:39:10 +11004260{
Tejun Heoed48ece2012-09-18 12:48:43 -07004261 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11004262
Tejun Heoed48ece2012-09-18 12:48:43 -07004263 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
4264 schedule_work_on(cpu, &wfc.work);
4265 flush_work(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11004266 return wfc.ret;
4267}
4268EXPORT_SYMBOL_GPL(work_on_cpu);
4269#endif /* CONFIG_SMP */
4270
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004271#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10304272
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004273/**
4274 * freeze_workqueues_begin - begin freezing workqueues
4275 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01004276 * Start freezing workqueues. After this function returns, all freezable
Tejun Heoc5aa87b2013-03-13 16:51:36 -07004277 * workqueues will queue new works to their delayed_works list instead of
Tejun Heo706026c2013-01-24 11:01:34 -08004278 * pool->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004279 *
4280 * CONTEXT:
Lai Jiangshana357fc02013-03-25 16:57:19 -07004281 * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004282 */
4283void freeze_workqueues_begin(void)
4284{
Tejun Heo17116962013-03-12 11:29:58 -07004285 struct worker_pool *pool;
Tejun Heo24b8a842013-03-12 11:29:58 -07004286 struct workqueue_struct *wq;
4287 struct pool_workqueue *pwq;
Tejun Heo611c92a2013-03-13 16:51:36 -07004288 int pi;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004289
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004290 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004291
Tejun Heo6183c002013-03-12 11:29:57 -07004292 WARN_ON_ONCE(workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004293 workqueue_freezing = true;
4294
Tejun Heo24b8a842013-03-12 11:29:58 -07004295 /* set FREEZING */
Tejun Heo611c92a2013-03-13 16:51:36 -07004296 for_each_pool(pool, pi) {
Tejun Heo5bcab332013-03-13 19:47:40 -07004297 spin_lock_irq(&pool->lock);
Tejun Heo17116962013-03-12 11:29:58 -07004298 WARN_ON_ONCE(pool->flags & POOL_FREEZING);
4299 pool->flags |= POOL_FREEZING;
Tejun Heo5bcab332013-03-13 19:47:40 -07004300 spin_unlock_irq(&pool->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004301 }
4302
Tejun Heo24b8a842013-03-12 11:29:58 -07004303 list_for_each_entry(wq, &workqueues, list) {
Lai Jiangshana357fc02013-03-25 16:57:19 -07004304 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07004305 for_each_pwq(pwq, wq)
4306 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07004307 mutex_unlock(&wq->mutex);
Tejun Heo24b8a842013-03-12 11:29:58 -07004308 }
Tejun Heo5bcab332013-03-13 19:47:40 -07004309
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004310 mutex_unlock(&wq_pool_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004312
4313/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01004314 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004315 *
4316 * Check whether freezing is complete. This function must be called
4317 * between freeze_workqueues_begin() and thaw_workqueues().
4318 *
4319 * CONTEXT:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004320 * Grabs and releases wq_pool_mutex.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004321 *
4322 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01004323 * %true if some freezable workqueues are still busy. %false if freezing
4324 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004325 */
4326bool freeze_workqueues_busy(void)
4327{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004328 bool busy = false;
Tejun Heo24b8a842013-03-12 11:29:58 -07004329 struct workqueue_struct *wq;
4330 struct pool_workqueue *pwq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004331
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004332 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004333
Tejun Heo6183c002013-03-12 11:29:57 -07004334 WARN_ON_ONCE(!workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004335
Tejun Heo24b8a842013-03-12 11:29:58 -07004336 list_for_each_entry(wq, &workqueues, list) {
4337 if (!(wq->flags & WQ_FREEZABLE))
4338 continue;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004339 /*
4340 * nr_active is monotonically decreasing. It's safe
4341 * to peek without lock.
4342 */
Lai Jiangshan88109452013-03-20 03:28:10 +08004343 rcu_read_lock_sched();
Tejun Heo24b8a842013-03-12 11:29:58 -07004344 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07004345 WARN_ON_ONCE(pwq->nr_active < 0);
Tejun Heo112202d2013-02-13 19:29:12 -08004346 if (pwq->nr_active) {
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004347 busy = true;
Lai Jiangshan88109452013-03-20 03:28:10 +08004348 rcu_read_unlock_sched();
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004349 goto out_unlock;
4350 }
4351 }
Lai Jiangshan88109452013-03-20 03:28:10 +08004352 rcu_read_unlock_sched();
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004353 }
4354out_unlock:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004355 mutex_unlock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004356 return busy;
4357}
4358
4359/**
4360 * thaw_workqueues - thaw workqueues
4361 *
4362 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo706026c2013-01-24 11:01:34 -08004363 * frozen works are transferred to their respective pool worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004364 *
4365 * CONTEXT:
Lai Jiangshana357fc02013-03-25 16:57:19 -07004366 * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004367 */
4368void thaw_workqueues(void)
4369{
Tejun Heo24b8a842013-03-12 11:29:58 -07004370 struct workqueue_struct *wq;
4371 struct pool_workqueue *pwq;
4372 struct worker_pool *pool;
Tejun Heo611c92a2013-03-13 16:51:36 -07004373 int pi;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004374
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004375 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004376
4377 if (!workqueue_freezing)
4378 goto out_unlock;
4379
Tejun Heo24b8a842013-03-12 11:29:58 -07004380 /* clear FREEZING */
Tejun Heo611c92a2013-03-13 16:51:36 -07004381 for_each_pool(pool, pi) {
Tejun Heo5bcab332013-03-13 19:47:40 -07004382 spin_lock_irq(&pool->lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004383 WARN_ON_ONCE(!(pool->flags & POOL_FREEZING));
4384 pool->flags &= ~POOL_FREEZING;
Tejun Heo5bcab332013-03-13 19:47:40 -07004385 spin_unlock_irq(&pool->lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004386 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02004387
Tejun Heo24b8a842013-03-12 11:29:58 -07004388 /* restore max_active and repopulate worklist */
4389 list_for_each_entry(wq, &workqueues, list) {
Lai Jiangshana357fc02013-03-25 16:57:19 -07004390 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07004391 for_each_pwq(pwq, wq)
4392 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07004393 mutex_unlock(&wq->mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004394 }
4395
4396 workqueue_freezing = false;
4397out_unlock:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004398 mutex_unlock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004399}
4400#endif /* CONFIG_FREEZER */
4401
Suresh Siddha6ee05782010-07-30 14:57:37 -07004402static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403{
Tejun Heo7a4e3442013-03-12 11:30:00 -07004404 int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL };
4405 int i, cpu;
Tejun Heoc34056a2010-06-29 10:07:11 +02004406
Tejun Heo7c3eed52013-01-24 11:01:33 -08004407 /* make sure we have enough bits for OFFQ pool ID */
4408 BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) <
Lai Jiangshan6be19582013-02-06 18:04:53 -08004409 WORK_CPU_END * NR_STD_WORKER_POOLS);
Tejun Heob5490072012-08-03 10:30:46 -07004410
Tejun Heoe904e6c2013-03-12 11:29:57 -07004411 WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long));
4412
4413 pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC);
4414
Tejun Heo65758202012-07-17 12:39:26 -07004415 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
Lai Jiangshana5b4e572012-09-18 09:59:23 -07004416 hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02004417
Tejun Heo706026c2013-01-24 11:01:34 -08004418 /* initialize CPU pools */
Tejun Heo29c91e92013-03-12 11:30:03 -07004419 for_each_possible_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07004420 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02004421
Tejun Heo7a4e3442013-03-12 11:30:00 -07004422 i = 0;
Tejun Heof02ae732013-03-12 11:30:03 -07004423 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo7a4e3442013-03-12 11:30:00 -07004424 BUG_ON(init_worker_pool(pool));
Tejun Heoec22ca52013-01-24 11:01:33 -08004425 pool->cpu = cpu;
Tejun Heo29c91e92013-03-12 11:30:03 -07004426 cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu));
Tejun Heo7a4e3442013-03-12 11:30:00 -07004427 pool->attrs->nice = std_nice[i++];
4428
Tejun Heo9daf9e62013-01-24 11:01:33 -08004429 /* alloc pool ID */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004430 mutex_lock(&wq_pool_mutex);
Tejun Heo9daf9e62013-01-24 11:01:33 -08004431 BUG_ON(worker_pool_assign_id(pool));
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004432 mutex_unlock(&wq_pool_mutex);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004433 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02004434 }
4435
Tejun Heoe22bee72010-06-29 10:07:14 +02004436 /* create the initial worker */
Tejun Heo29c91e92013-03-12 11:30:03 -07004437 for_each_online_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07004438 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02004439
Tejun Heof02ae732013-03-12 11:30:03 -07004440 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo29c91e92013-03-12 11:30:03 -07004441 pool->flags &= ~POOL_DISASSOCIATED;
Tejun Heoebf44d12013-03-13 19:47:39 -07004442 BUG_ON(create_and_start_worker(pool) < 0);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004443 }
Tejun Heoe22bee72010-06-29 10:07:14 +02004444 }
4445
Tejun Heo29c91e92013-03-12 11:30:03 -07004446 /* create default unbound wq attrs */
4447 for (i = 0; i < NR_STD_WORKER_POOLS; i++) {
4448 struct workqueue_attrs *attrs;
4449
4450 BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL)));
Tejun Heo29c91e92013-03-12 11:30:03 -07004451 attrs->nice = std_nice[i];
Tejun Heo29c91e92013-03-12 11:30:03 -07004452 unbound_std_wq_attrs[i] = attrs;
4453 }
4454
Tejun Heod320c032010-06-29 10:07:14 +02004455 system_wq = alloc_workqueue("events", 0, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09004456 system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
Tejun Heod320c032010-06-29 10:07:14 +02004457 system_long_wq = alloc_workqueue("events_long", 0, 0);
Tejun Heof3421792010-07-02 10:03:51 +02004458 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
4459 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01004460 system_freezable_wq = alloc_workqueue("events_freezable",
4461 WQ_FREEZABLE, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09004462 BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
Tejun Heoae930e02012-08-20 14:51:23 -07004463 !system_unbound_wq || !system_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07004464 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465}
Suresh Siddha6ee05782010-07-30 14:57:37 -07004466early_initcall(init_workqueues);