blob: 47f258799bf2f17ed4e1c29ad50b134cb7e6de92 [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 Lameter469340232006-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 Heo73f53c42010-06-29 10:07:11 +0200121 * F: wq->flush_mutex protected.
122 *
Tejun Heo822d8402013-03-19 13:45:21 -0700123 * MG: pool->manager_mutex and pool->lock protected. Writes require both
124 * locks. Reads can happen under either lock.
125 *
Tejun Heo5bcab332013-03-13 19:47:40 -0700126 * WQ: wq_mutex protected.
Tejun Heo76af4d92013-03-12 11:30:00 -0700127 *
Tejun Heo5bcab332013-03-13 19:47:40 -0700128 * WR: wq_mutex protected for writes. Sched-RCU protected for reads.
129 *
Tejun Heo794b18b2013-03-13 19:47:40 -0700130 * PW: pwq_lock protected.
131 *
Tejun Heo794b18b2013-03-13 19:47:40 -0700132 * FR: wq->flush_mutex and pwq_lock protected for writes. Sched-RCU
Tejun Heo75ccf592013-03-12 11:30:04 -0700133 * protected for reads.
Tejun Heo2e109a22013-03-13 19:47:40 -0700134 *
135 * MD: wq_mayday_lock protected.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200136 */
137
Tejun Heo2eaebdb2013-01-18 14:05:55 -0800138/* struct worker is defined in workqueue_internal.h */
Tejun Heoc34056a2010-06-29 10:07:11 +0200139
Tejun Heobd7bdd42012-07-12 14:46:37 -0700140struct worker_pool {
Tejun Heod565ed62013-01-24 11:01:33 -0800141 spinlock_t lock; /* the pool lock */
Tejun Heod84ff052013-03-12 11:29:59 -0700142 int cpu; /* I: the associated cpu */
Tejun Heo9daf9e62013-01-24 11:01:33 -0800143 int id; /* I: pool ID */
Tejun Heo11ebea52012-07-12 14:46:37 -0700144 unsigned int flags; /* X: flags */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700145
146 struct list_head worklist; /* L: list of pending works */
147 int nr_workers; /* L: total number of workers */
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700148
149 /* nr_idle includes the ones off idle_list for rebinding */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700150 int nr_idle; /* L: currently idle ones */
151
152 struct list_head idle_list; /* X: list of idle workers */
153 struct timer_list idle_timer; /* L: worker idle timeout */
154 struct timer_list mayday_timer; /* L: SOS timer for workers */
155
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700156 /* a workers is either on busy_hash or idle_list, or the manager */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800157 DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER);
158 /* L: hash of busy workers */
159
Tejun Heobc3a1af2013-03-13 19:47:39 -0700160 /* see manage_workers() for details on the two manager mutexes */
Tejun Heo34a06bd2013-03-12 11:30:00 -0700161 struct mutex manager_arb; /* manager arbitration */
Tejun Heobc3a1af2013-03-13 19:47:39 -0700162 struct mutex manager_mutex; /* manager exclusion */
Tejun Heo822d8402013-03-19 13:45:21 -0700163 struct idr worker_idr; /* MG: worker IDs and iteration */
Tejun Heoe19e3972013-01-24 11:39:44 -0800164
Tejun Heo7a4e3442013-03-12 11:30:00 -0700165 struct workqueue_attrs *attrs; /* I: worker attributes */
Tejun Heo5bcab332013-03-13 19:47:40 -0700166 struct hlist_node hash_node; /* WQ: unbound_pool_hash node */
167 int refcnt; /* WQ: refcnt for unbound pools */
Tejun Heo7a4e3442013-03-12 11:30:00 -0700168
Tejun Heoe19e3972013-01-24 11:39:44 -0800169 /*
170 * The current concurrency level. As it's likely to be accessed
171 * from other CPUs during try_to_wake_up(), put it in a separate
172 * cacheline.
173 */
174 atomic_t nr_running ____cacheline_aligned_in_smp;
Tejun Heo29c91e92013-03-12 11:30:03 -0700175
176 /*
177 * Destruction of pool is sched-RCU protected to allow dereferences
178 * from get_work_pool().
179 */
180 struct rcu_head rcu;
Tejun Heo8b03ae32010-06-29 10:07:12 +0200181} ____cacheline_aligned_in_smp;
182
183/*
Tejun Heo112202d2013-02-13 19:29:12 -0800184 * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS
185 * of work_struct->data are used for flags and the remaining high bits
186 * point to the pwq; thus, pwqs need to be aligned at two's power of the
187 * number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 */
Tejun Heo112202d2013-02-13 19:29:12 -0800189struct pool_workqueue {
Tejun Heobd7bdd42012-07-12 14:46:37 -0700190 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200191 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200192 int work_color; /* L: current color */
193 int flush_color; /* L: flushing color */
Tejun Heo8864b4e2013-03-12 11:30:04 -0700194 int refcnt; /* L: reference count */
Tejun Heo73f53c42010-06-29 10:07:11 +0200195 int nr_in_flight[WORK_NR_COLORS];
196 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200197 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200198 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200199 struct list_head delayed_works; /* L: delayed works */
Tejun Heo75ccf592013-03-12 11:30:04 -0700200 struct list_head pwqs_node; /* FR: node on wq->pwqs */
Tejun Heo2e109a22013-03-13 19:47:40 -0700201 struct list_head mayday_node; /* MD: node on wq->maydays */
Tejun Heo8864b4e2013-03-12 11:30:04 -0700202
203 /*
204 * Release of unbound pwq is punted to system_wq. See put_pwq()
205 * and pwq_unbound_release_workfn() for details. pool_workqueue
206 * itself is also sched-RCU protected so that the first pwq can be
Tejun Heo794b18b2013-03-13 19:47:40 -0700207 * determined without grabbing pwq_lock.
Tejun Heo8864b4e2013-03-12 11:30:04 -0700208 */
209 struct work_struct unbound_release_work;
210 struct rcu_head rcu;
Tejun Heoe904e6c2013-03-12 11:29:57 -0700211} __aligned(1 << WORK_STRUCT_FLAG_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200214 * Structure used to wait for workqueue flush.
215 */
216struct wq_flusher {
217 struct list_head list; /* F: list of flushers */
218 int flush_color; /* F: flush color waiting for */
219 struct completion done; /* flush completion */
220};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Tejun Heo226223a2013-03-12 11:30:05 -0700222struct wq_device;
223
Tejun Heo73f53c42010-06-29 10:07:11 +0200224/*
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700225 * The externally visible workqueue. It relays the issued work items to
226 * the appropriate worker_pool through its pool_workqueues.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 */
228struct workqueue_struct {
Tejun Heo5bcab332013-03-13 19:47:40 -0700229 unsigned int flags; /* WQ: WQ_* flags */
Tejun Heo420c0dd2013-03-12 11:29:59 -0700230 struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwq's */
Tejun Heo75ccf592013-03-12 11:30:04 -0700231 struct list_head pwqs; /* FR: all pwqs of this wq */
Tejun Heo5bcab332013-03-13 19:47:40 -0700232 struct list_head list; /* WQ: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200233
234 struct mutex flush_mutex; /* protects wq flushing */
235 int work_color; /* F: current work color */
236 int flush_color; /* F: current flush color */
Tejun Heo112202d2013-02-13 19:29:12 -0800237 atomic_t nr_pwqs_to_flush; /* flush in progress */
Tejun Heo73f53c42010-06-29 10:07:11 +0200238 struct wq_flusher *first_flusher; /* F: first flusher */
239 struct list_head flusher_queue; /* F: flush waiters */
240 struct list_head flusher_overflow; /* F: flush overflow list */
241
Tejun Heo2e109a22013-03-13 19:47:40 -0700242 struct list_head maydays; /* MD: pwqs requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200243 struct worker *rescuer; /* I: rescue worker */
244
Tejun Heo5bcab332013-03-13 19:47:40 -0700245 int nr_drainers; /* WQ: drain in progress */
Tejun Heo794b18b2013-03-13 19:47:40 -0700246 int saved_max_active; /* PW: saved pwq max_active */
Tejun Heo226223a2013-03-12 11:30:05 -0700247
248#ifdef CONFIG_SYSFS
249 struct wq_device *wq_dev; /* I: for sysfs interface */
250#endif
Johannes Berg4e6045f2007-10-18 23:39:55 -0700251#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200252 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700253#endif
Tejun Heob196be82012-01-10 15:11:35 -0800254 char name[]; /* I: workqueue name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255};
256
Tejun Heoe904e6c2013-03-12 11:29:57 -0700257static struct kmem_cache *pwq_cache;
258
Tejun Heo5bcab332013-03-13 19:47:40 -0700259static DEFINE_MUTEX(wq_mutex); /* protects workqueues and pools */
Tejun Heo794b18b2013-03-13 19:47:40 -0700260static DEFINE_SPINLOCK(pwq_lock); /* protects pool_workqueues */
Tejun Heo2e109a22013-03-13 19:47:40 -0700261static DEFINE_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */
Tejun Heo5bcab332013-03-13 19:47:40 -0700262
263static LIST_HEAD(workqueues); /* WQ: list of all workqueues */
264static bool workqueue_freezing; /* WQ: have wqs started freezing? */
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700265
266/* the per-cpu worker pools */
267static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS],
268 cpu_worker_pools);
269
Tejun Heo5bcab332013-03-13 19:47:40 -0700270static DEFINE_IDR(worker_pool_idr); /* WR: idr of all pools */
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700271
Tejun Heo5bcab332013-03-13 19:47:40 -0700272/* WQ: hash of all unbound pools keyed by pool->attrs */
Tejun Heo29c91e92013-03-12 11:30:03 -0700273static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER);
274
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700275/* I: attributes used when instantiating standard unbound pools on demand */
Tejun Heo29c91e92013-03-12 11:30:03 -0700276static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS];
277
Tejun Heod320c032010-06-29 10:07:14 +0200278struct workqueue_struct *system_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200279EXPORT_SYMBOL_GPL(system_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300280struct workqueue_struct *system_highpri_wq __read_mostly;
Joonsoo Kim1aabe902012-08-15 23:25:39 +0900281EXPORT_SYMBOL_GPL(system_highpri_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300282struct workqueue_struct *system_long_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200283EXPORT_SYMBOL_GPL(system_long_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300284struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200285EXPORT_SYMBOL_GPL(system_unbound_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300286struct workqueue_struct *system_freezable_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100287EXPORT_SYMBOL_GPL(system_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200288
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700289static int worker_thread(void *__worker);
290static void copy_workqueue_attrs(struct workqueue_attrs *to,
291 const struct workqueue_attrs *from);
292
Tejun Heo97bd2342010-10-05 10:41:14 +0200293#define CREATE_TRACE_POINTS
294#include <trace/events/workqueue.h>
295
Tejun Heo5bcab332013-03-13 19:47:40 -0700296#define assert_rcu_or_wq_mutex() \
297 rcu_lockdep_assert(rcu_read_lock_sched_held() || \
298 lockdep_is_held(&wq_mutex), \
299 "sched RCU or wq_mutex should be held")
300
Tejun Heo794b18b2013-03-13 19:47:40 -0700301#define assert_rcu_or_pwq_lock() \
Tejun Heo76af4d92013-03-12 11:30:00 -0700302 rcu_lockdep_assert(rcu_read_lock_sched_held() || \
Tejun Heo794b18b2013-03-13 19:47:40 -0700303 lockdep_is_held(&pwq_lock), \
304 "sched RCU or pwq_lock should be held")
Tejun Heo76af4d92013-03-12 11:30:00 -0700305
Tejun Heo822d8402013-03-19 13:45:21 -0700306#ifdef CONFIG_LOCKDEP
307#define assert_manager_or_pool_lock(pool) \
Lai Jiangshan519e3c12013-03-20 03:28:21 +0800308 WARN_ONCE(debug_locks && \
309 !lockdep_is_held(&(pool)->manager_mutex) && \
Tejun Heo822d8402013-03-19 13:45:21 -0700310 !lockdep_is_held(&(pool)->lock), \
311 "pool->manager_mutex or ->lock should be held")
312#else
313#define assert_manager_or_pool_lock(pool) do { } while (0)
314#endif
315
Tejun Heof02ae732013-03-12 11:30:03 -0700316#define for_each_cpu_worker_pool(pool, cpu) \
317 for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \
318 (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \
Tejun Heo7a62c2c2013-03-12 11:30:03 -0700319 (pool)++)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700320
Tejun Heo49e3cf42013-03-12 11:29:58 -0700321/**
Tejun Heo17116962013-03-12 11:29:58 -0700322 * for_each_pool - iterate through all worker_pools in the system
323 * @pool: iteration cursor
Tejun Heo611c92a2013-03-13 16:51:36 -0700324 * @pi: integer used for iteration
Tejun Heofa1b54e2013-03-12 11:30:00 -0700325 *
Tejun Heo5bcab332013-03-13 19:47:40 -0700326 * This must be called either with wq_mutex held or sched RCU read locked.
327 * If the pool needs to be used beyond the locking in effect, the caller is
328 * responsible for guaranteeing that the pool stays online.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700329 *
330 * The if/else clause exists only for the lockdep assertion and can be
331 * ignored.
Tejun Heo17116962013-03-12 11:29:58 -0700332 */
Tejun Heo611c92a2013-03-13 16:51:36 -0700333#define for_each_pool(pool, pi) \
334 idr_for_each_entry(&worker_pool_idr, pool, pi) \
Tejun Heo5bcab332013-03-13 19:47:40 -0700335 if (({ assert_rcu_or_wq_mutex(); false; })) { } \
Tejun Heofa1b54e2013-03-12 11:30:00 -0700336 else
Tejun Heo17116962013-03-12 11:29:58 -0700337
338/**
Tejun Heo822d8402013-03-19 13:45:21 -0700339 * for_each_pool_worker - iterate through all workers of a worker_pool
340 * @worker: iteration cursor
341 * @wi: integer used for iteration
342 * @pool: worker_pool to iterate workers of
343 *
344 * This must be called with either @pool->manager_mutex or ->lock held.
345 *
346 * The if/else clause exists only for the lockdep assertion and can be
347 * ignored.
348 */
349#define for_each_pool_worker(worker, wi, pool) \
350 idr_for_each_entry(&(pool)->worker_idr, (worker), (wi)) \
351 if (({ assert_manager_or_pool_lock((pool)); false; })) { } \
352 else
353
354/**
Tejun Heo49e3cf42013-03-12 11:29:58 -0700355 * for_each_pwq - iterate through all pool_workqueues of the specified workqueue
356 * @pwq: iteration cursor
357 * @wq: the target workqueue
Tejun Heo76af4d92013-03-12 11:30:00 -0700358 *
Tejun Heo794b18b2013-03-13 19:47:40 -0700359 * This must be called either with pwq_lock held or sched RCU read locked.
360 * If the pwq needs to be used beyond the locking in effect, the caller is
361 * responsible for guaranteeing that the pwq stays online.
Tejun Heo76af4d92013-03-12 11:30:00 -0700362 *
363 * The if/else clause exists only for the lockdep assertion and can be
364 * ignored.
Tejun Heo49e3cf42013-03-12 11:29:58 -0700365 */
366#define for_each_pwq(pwq, wq) \
Tejun Heo76af4d92013-03-12 11:30:00 -0700367 list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node) \
Tejun Heo794b18b2013-03-13 19:47:40 -0700368 if (({ assert_rcu_or_pwq_lock(); false; })) { } \
Tejun Heo76af4d92013-03-12 11:30:00 -0700369 else
Tejun Heof3421792010-07-02 10:03:51 +0200370
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900371#ifdef CONFIG_DEBUG_OBJECTS_WORK
372
373static struct debug_obj_descr work_debug_descr;
374
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100375static void *work_debug_hint(void *addr)
376{
377 return ((struct work_struct *) addr)->func;
378}
379
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900380/*
381 * fixup_init is called when:
382 * - an active object is initialized
383 */
384static int work_fixup_init(void *addr, enum debug_obj_state state)
385{
386 struct work_struct *work = addr;
387
388 switch (state) {
389 case ODEBUG_STATE_ACTIVE:
390 cancel_work_sync(work);
391 debug_object_init(work, &work_debug_descr);
392 return 1;
393 default:
394 return 0;
395 }
396}
397
398/*
399 * fixup_activate is called when:
400 * - an active object is activated
401 * - an unknown object is activated (might be a statically initialized object)
402 */
403static int work_fixup_activate(void *addr, enum debug_obj_state state)
404{
405 struct work_struct *work = addr;
406
407 switch (state) {
408
409 case ODEBUG_STATE_NOTAVAILABLE:
410 /*
411 * This is not really a fixup. The work struct was
412 * statically initialized. We just make sure that it
413 * is tracked in the object tracker.
414 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200415 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900416 debug_object_init(work, &work_debug_descr);
417 debug_object_activate(work, &work_debug_descr);
418 return 0;
419 }
420 WARN_ON_ONCE(1);
421 return 0;
422
423 case ODEBUG_STATE_ACTIVE:
424 WARN_ON(1);
425
426 default:
427 return 0;
428 }
429}
430
431/*
432 * fixup_free is called when:
433 * - an active object is freed
434 */
435static int work_fixup_free(void *addr, enum debug_obj_state state)
436{
437 struct work_struct *work = addr;
438
439 switch (state) {
440 case ODEBUG_STATE_ACTIVE:
441 cancel_work_sync(work);
442 debug_object_free(work, &work_debug_descr);
443 return 1;
444 default:
445 return 0;
446 }
447}
448
449static struct debug_obj_descr work_debug_descr = {
450 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100451 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900452 .fixup_init = work_fixup_init,
453 .fixup_activate = work_fixup_activate,
454 .fixup_free = work_fixup_free,
455};
456
457static inline void debug_work_activate(struct work_struct *work)
458{
459 debug_object_activate(work, &work_debug_descr);
460}
461
462static inline void debug_work_deactivate(struct work_struct *work)
463{
464 debug_object_deactivate(work, &work_debug_descr);
465}
466
467void __init_work(struct work_struct *work, int onstack)
468{
469 if (onstack)
470 debug_object_init_on_stack(work, &work_debug_descr);
471 else
472 debug_object_init(work, &work_debug_descr);
473}
474EXPORT_SYMBOL_GPL(__init_work);
475
476void destroy_work_on_stack(struct work_struct *work)
477{
478 debug_object_free(work, &work_debug_descr);
479}
480EXPORT_SYMBOL_GPL(destroy_work_on_stack);
481
482#else
483static inline void debug_work_activate(struct work_struct *work) { }
484static inline void debug_work_deactivate(struct work_struct *work) { }
485#endif
486
Tejun Heo9daf9e62013-01-24 11:01:33 -0800487/* allocate ID and assign it to @pool */
488static int worker_pool_assign_id(struct worker_pool *pool)
489{
490 int ret;
491
Tejun Heo5bcab332013-03-13 19:47:40 -0700492 lockdep_assert_held(&wq_mutex);
493
Tejun Heofa1b54e2013-03-12 11:30:00 -0700494 do {
495 if (!idr_pre_get(&worker_pool_idr, GFP_KERNEL))
496 return -ENOMEM;
Tejun Heofa1b54e2013-03-12 11:30:00 -0700497 ret = idr_get_new(&worker_pool_idr, pool, &pool->id);
Tejun Heofa1b54e2013-03-12 11:30:00 -0700498 } while (ret == -EAGAIN);
Tejun Heo9daf9e62013-01-24 11:01:33 -0800499
500 return ret;
501}
502
Tejun Heo76af4d92013-03-12 11:30:00 -0700503/**
504 * first_pwq - return the first pool_workqueue of the specified workqueue
505 * @wq: the target workqueue
506 *
Tejun Heo794b18b2013-03-13 19:47:40 -0700507 * This must be called either with pwq_lock held or sched RCU read locked.
508 * If the pwq needs to be used beyond the locking in effect, the caller is
509 * responsible for guaranteeing that the pwq stays online.
Tejun Heo76af4d92013-03-12 11:30:00 -0700510 */
Tejun Heo7fb98ea2013-03-12 11:30:00 -0700511static struct pool_workqueue *first_pwq(struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700512{
Tejun Heo794b18b2013-03-13 19:47:40 -0700513 assert_rcu_or_pwq_lock();
Tejun Heo76af4d92013-03-12 11:30:00 -0700514 return list_first_or_null_rcu(&wq->pwqs, struct pool_workqueue,
515 pwqs_node);
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700516}
517
Tejun Heo73f53c42010-06-29 10:07:11 +0200518static unsigned int work_color_to_flags(int color)
519{
520 return color << WORK_STRUCT_COLOR_SHIFT;
521}
522
523static int get_work_color(struct work_struct *work)
524{
525 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
526 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
527}
528
529static int work_next_color(int color)
530{
531 return (color + 1) % WORK_NR_COLORS;
Oleg Nesterovb1f4ec172007-05-09 02:34:12 -0700532}
533
David Howells4594bf12006-12-07 11:33:26 +0000534/*
Tejun Heo112202d2013-02-13 19:29:12 -0800535 * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data
536 * contain the pointer to the queued pwq. Once execution starts, the flag
Tejun Heo7c3eed52013-01-24 11:01:33 -0800537 * is cleared and the high bits contain OFFQ flags and pool ID.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200538 *
Tejun Heo112202d2013-02-13 19:29:12 -0800539 * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling()
540 * and clear_work_data() can be used to set the pwq, pool or clear
Tejun Heobbb68df2012-08-03 10:30:46 -0700541 * work->data. These functions should only be called while the work is
542 * owned - ie. while the PENDING bit is set.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200543 *
Tejun Heo112202d2013-02-13 19:29:12 -0800544 * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq
Tejun Heo7c3eed52013-01-24 11:01:33 -0800545 * corresponding to a work. Pool is available once the work has been
Tejun Heo112202d2013-02-13 19:29:12 -0800546 * queued anywhere after initialization until it is sync canceled. pwq is
Tejun Heo7c3eed52013-01-24 11:01:33 -0800547 * available only while the work item is queued.
Tejun Heobbb68df2012-08-03 10:30:46 -0700548 *
549 * %WORK_OFFQ_CANCELING is used to mark a work item which is being
550 * canceled. While being canceled, a work item may have its PENDING set
551 * but stay off timer and worklist for arbitrarily long and nobody should
552 * try to steal the PENDING bit.
David Howells4594bf12006-12-07 11:33:26 +0000553 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200554static inline void set_work_data(struct work_struct *work, unsigned long data,
555 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000556{
Tejun Heo6183c002013-03-12 11:29:57 -0700557 WARN_ON_ONCE(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200558 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000559}
David Howells365970a2006-11-22 14:54:49 +0000560
Tejun Heo112202d2013-02-13 19:29:12 -0800561static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq,
Tejun Heo7a22ad72010-06-29 10:07:13 +0200562 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200563{
Tejun Heo112202d2013-02-13 19:29:12 -0800564 set_work_data(work, (unsigned long)pwq,
565 WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200566}
567
Lai Jiangshan4468a002013-02-06 18:04:53 -0800568static void set_work_pool_and_keep_pending(struct work_struct *work,
569 int pool_id)
570{
571 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT,
572 WORK_STRUCT_PENDING);
573}
574
Tejun Heo7c3eed52013-01-24 11:01:33 -0800575static void set_work_pool_and_clear_pending(struct work_struct *work,
576 int pool_id)
David Howells365970a2006-11-22 14:54:49 +0000577{
Tejun Heo23657bb2012-08-13 17:08:19 -0700578 /*
579 * The following wmb is paired with the implied mb in
580 * test_and_set_bit(PENDING) and ensures all updates to @work made
581 * here are visible to and precede any updates by the next PENDING
582 * owner.
583 */
584 smp_wmb();
Tejun Heo7c3eed52013-01-24 11:01:33 -0800585 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200586}
587
588static void clear_work_data(struct work_struct *work)
589{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800590 smp_wmb(); /* see set_work_pool_and_clear_pending() */
591 set_work_data(work, WORK_STRUCT_NO_POOL, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200592}
593
Tejun Heo112202d2013-02-13 19:29:12 -0800594static struct pool_workqueue *get_work_pwq(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200595{
Tejun Heoe1201532010-07-22 14:14:25 +0200596 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200597
Tejun Heo112202d2013-02-13 19:29:12 -0800598 if (data & WORK_STRUCT_PWQ)
Tejun Heoe1201532010-07-22 14:14:25 +0200599 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
600 else
601 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200602}
603
Tejun Heo7c3eed52013-01-24 11:01:33 -0800604/**
605 * get_work_pool - return the worker_pool a given work was associated with
606 * @work: the work item of interest
607 *
608 * Return the worker_pool @work was last associated with. %NULL if none.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700609 *
Tejun Heo5bcab332013-03-13 19:47:40 -0700610 * Pools are created and destroyed under wq_mutex, and allows read access
611 * under sched-RCU read lock. As such, this function should be called
612 * under wq_mutex or with preemption disabled.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700613 *
614 * All fields of the returned pool are accessible as long as the above
615 * mentioned locking is in effect. If the returned pool needs to be used
616 * beyond the critical section, the caller is responsible for ensuring the
617 * returned pool is and stays online.
Tejun Heo7c3eed52013-01-24 11:01:33 -0800618 */
619static struct worker_pool *get_work_pool(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200620{
Tejun Heoe1201532010-07-22 14:14:25 +0200621 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800622 int pool_id;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200623
Tejun Heo5bcab332013-03-13 19:47:40 -0700624 assert_rcu_or_wq_mutex();
Tejun Heofa1b54e2013-03-12 11:30:00 -0700625
Tejun Heo112202d2013-02-13 19:29:12 -0800626 if (data & WORK_STRUCT_PWQ)
627 return ((struct pool_workqueue *)
Tejun Heo7c3eed52013-01-24 11:01:33 -0800628 (data & WORK_STRUCT_WQ_DATA_MASK))->pool;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200629
Tejun Heo7c3eed52013-01-24 11:01:33 -0800630 pool_id = data >> WORK_OFFQ_POOL_SHIFT;
631 if (pool_id == WORK_OFFQ_POOL_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200632 return NULL;
633
Tejun Heofa1b54e2013-03-12 11:30:00 -0700634 return idr_find(&worker_pool_idr, pool_id);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800635}
636
637/**
638 * get_work_pool_id - return the worker pool ID a given work is associated with
639 * @work: the work item of interest
640 *
641 * Return the worker_pool ID @work was last associated with.
642 * %WORK_OFFQ_POOL_NONE if none.
643 */
644static int get_work_pool_id(struct work_struct *work)
645{
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800646 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800647
Tejun Heo112202d2013-02-13 19:29:12 -0800648 if (data & WORK_STRUCT_PWQ)
649 return ((struct pool_workqueue *)
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800650 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id;
651
652 return data >> WORK_OFFQ_POOL_SHIFT;
Tejun Heo7c3eed52013-01-24 11:01:33 -0800653}
654
Tejun Heobbb68df2012-08-03 10:30:46 -0700655static void mark_work_canceling(struct work_struct *work)
656{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800657 unsigned long pool_id = get_work_pool_id(work);
Tejun Heobbb68df2012-08-03 10:30:46 -0700658
Tejun Heo7c3eed52013-01-24 11:01:33 -0800659 pool_id <<= WORK_OFFQ_POOL_SHIFT;
660 set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700661}
662
663static bool work_is_canceling(struct work_struct *work)
664{
665 unsigned long data = atomic_long_read(&work->data);
666
Tejun Heo112202d2013-02-13 19:29:12 -0800667 return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700668}
669
David Howells365970a2006-11-22 14:54:49 +0000670/*
Tejun Heo32704762012-07-13 22:16:45 -0700671 * Policy functions. These define the policies on how the global worker
672 * pools are managed. Unless noted otherwise, these functions assume that
Tejun Heod565ed62013-01-24 11:01:33 -0800673 * they're being called with pool->lock held.
David Howells365970a2006-11-22 14:54:49 +0000674 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200675
Tejun Heo63d95a92012-07-12 14:46:37 -0700676static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000677{
Tejun Heoe19e3972013-01-24 11:39:44 -0800678 return !atomic_read(&pool->nr_running);
David Howells365970a2006-11-22 14:54:49 +0000679}
680
Tejun Heoe22bee72010-06-29 10:07:14 +0200681/*
682 * Need to wake up a worker? Called from anything but currently
683 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700684 *
685 * Note that, because unbound workers never contribute to nr_running, this
Tejun Heo706026c2013-01-24 11:01:34 -0800686 * function will always return %true for unbound pools as long as the
Tejun Heo974271c2012-07-12 14:46:37 -0700687 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200688 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700689static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000690{
Tejun Heo63d95a92012-07-12 14:46:37 -0700691 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000692}
693
Tejun Heoe22bee72010-06-29 10:07:14 +0200694/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700695static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200696{
Tejun Heo63d95a92012-07-12 14:46:37 -0700697 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200698}
699
700/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700701static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200702{
Tejun Heoe19e3972013-01-24 11:39:44 -0800703 return !list_empty(&pool->worklist) &&
704 atomic_read(&pool->nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200705}
706
707/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700708static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200709{
Tejun Heo63d95a92012-07-12 14:46:37 -0700710 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200711}
712
713/* Do I need to be the manager? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700714static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200715{
Tejun Heo63d95a92012-07-12 14:46:37 -0700716 return need_to_create_worker(pool) ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700717 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200718}
719
720/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700721static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200722{
Tejun Heo34a06bd2013-03-12 11:30:00 -0700723 bool managing = mutex_is_locked(&pool->manager_arb);
Tejun Heo63d95a92012-07-12 14:46:37 -0700724 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
725 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200726
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700727 /*
728 * nr_idle and idle_list may disagree if idle rebinding is in
729 * progress. Never return %true if idle_list is empty.
730 */
731 if (list_empty(&pool->idle_list))
732 return false;
733
Tejun Heoe22bee72010-06-29 10:07:14 +0200734 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
735}
736
737/*
738 * Wake up functions.
739 */
740
Tejun Heo7e116292010-06-29 10:07:13 +0200741/* Return the first worker. Safe with preemption disabled */
Tejun Heo63d95a92012-07-12 14:46:37 -0700742static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200743{
Tejun Heo63d95a92012-07-12 14:46:37 -0700744 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200745 return NULL;
746
Tejun Heo63d95a92012-07-12 14:46:37 -0700747 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200748}
749
750/**
751 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700752 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200753 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700754 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200755 *
756 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800757 * spin_lock_irq(pool->lock).
Tejun Heo7e116292010-06-29 10:07:13 +0200758 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700759static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200760{
Tejun Heo63d95a92012-07-12 14:46:37 -0700761 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200762
763 if (likely(worker))
764 wake_up_process(worker->task);
765}
766
Tejun Heo4690c4a2010-06-29 10:07:10 +0200767/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200768 * wq_worker_waking_up - a worker is waking up
769 * @task: task waking up
770 * @cpu: CPU @task is waking up to
771 *
772 * This function is called during try_to_wake_up() when a worker is
773 * being awoken.
774 *
775 * CONTEXT:
776 * spin_lock_irq(rq->lock)
777 */
Tejun Heod84ff052013-03-12 11:29:59 -0700778void wq_worker_waking_up(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200779{
780 struct worker *worker = kthread_data(task);
781
Joonsoo Kim36576002012-10-26 23:03:49 +0900782 if (!(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoec22ca52013-01-24 11:01:33 -0800783 WARN_ON_ONCE(worker->pool->cpu != cpu);
Tejun Heoe19e3972013-01-24 11:39:44 -0800784 atomic_inc(&worker->pool->nr_running);
Joonsoo Kim36576002012-10-26 23:03:49 +0900785 }
Tejun Heoe22bee72010-06-29 10:07:14 +0200786}
787
788/**
789 * wq_worker_sleeping - a worker is going to sleep
790 * @task: task going to sleep
791 * @cpu: CPU in question, must be the current CPU number
792 *
793 * This function is called during schedule() when a busy worker is
794 * going to sleep. Worker on the same cpu can be woken up by
795 * returning pointer to its task.
796 *
797 * CONTEXT:
798 * spin_lock_irq(rq->lock)
799 *
800 * RETURNS:
801 * Worker task on @cpu to wake up, %NULL if none.
802 */
Tejun Heod84ff052013-03-12 11:29:59 -0700803struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200804{
805 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heo111c2252013-01-17 17:16:24 -0800806 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200807
Tejun Heo111c2252013-01-17 17:16:24 -0800808 /*
809 * Rescuers, which may not have all the fields set up like normal
810 * workers, also reach here, let's not access anything before
811 * checking NOT_RUNNING.
812 */
Steven Rostedt2d646722010-12-03 23:12:33 -0500813 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200814 return NULL;
815
Tejun Heo111c2252013-01-17 17:16:24 -0800816 pool = worker->pool;
Tejun Heo111c2252013-01-17 17:16:24 -0800817
Tejun Heoe22bee72010-06-29 10:07:14 +0200818 /* this can only happen on the local cpu */
Tejun Heo6183c002013-03-12 11:29:57 -0700819 if (WARN_ON_ONCE(cpu != raw_smp_processor_id()))
820 return NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +0200821
822 /*
823 * The counterpart of the following dec_and_test, implied mb,
824 * worklist not empty test sequence is in insert_work().
825 * Please read comment there.
826 *
Tejun Heo628c78e2012-07-17 12:39:27 -0700827 * NOT_RUNNING is clear. This means that we're bound to and
828 * running on the local cpu w/ rq lock held and preemption
829 * disabled, which in turn means that none else could be
Tejun Heod565ed62013-01-24 11:01:33 -0800830 * manipulating idle_list, so dereferencing idle_list without pool
Tejun Heo628c78e2012-07-17 12:39:27 -0700831 * lock is safe.
Tejun Heoe22bee72010-06-29 10:07:14 +0200832 */
Tejun Heoe19e3972013-01-24 11:39:44 -0800833 if (atomic_dec_and_test(&pool->nr_running) &&
834 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700835 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200836 return to_wakeup ? to_wakeup->task : NULL;
837}
838
839/**
840 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200841 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200842 * @flags: flags to set
843 * @wakeup: wakeup an idle worker if necessary
844 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200845 * Set @flags in @worker->flags and adjust nr_running accordingly. If
846 * nr_running becomes zero and @wakeup is %true, an idle worker is
847 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200848 *
Tejun Heocb444762010-07-02 10:03:50 +0200849 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800850 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200851 */
852static inline void worker_set_flags(struct worker *worker, unsigned int flags,
853 bool wakeup)
854{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700855 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200856
Tejun Heocb444762010-07-02 10:03:50 +0200857 WARN_ON_ONCE(worker->task != current);
858
Tejun Heoe22bee72010-06-29 10:07:14 +0200859 /*
860 * If transitioning into NOT_RUNNING, adjust nr_running and
861 * wake up an idle worker as necessary if requested by
862 * @wakeup.
863 */
864 if ((flags & WORKER_NOT_RUNNING) &&
865 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoe22bee72010-06-29 10:07:14 +0200866 if (wakeup) {
Tejun Heoe19e3972013-01-24 11:39:44 -0800867 if (atomic_dec_and_test(&pool->nr_running) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -0700868 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700869 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200870 } else
Tejun Heoe19e3972013-01-24 11:39:44 -0800871 atomic_dec(&pool->nr_running);
Tejun Heoe22bee72010-06-29 10:07:14 +0200872 }
873
Tejun Heod302f012010-06-29 10:07:13 +0200874 worker->flags |= flags;
875}
876
877/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200878 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200879 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200880 * @flags: flags to clear
881 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200882 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200883 *
Tejun Heocb444762010-07-02 10:03:50 +0200884 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800885 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200886 */
887static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
888{
Tejun Heo63d95a92012-07-12 14:46:37 -0700889 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200890 unsigned int oflags = worker->flags;
891
Tejun Heocb444762010-07-02 10:03:50 +0200892 WARN_ON_ONCE(worker->task != current);
893
Tejun Heod302f012010-06-29 10:07:13 +0200894 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200895
Tejun Heo42c025f2011-01-11 15:58:49 +0100896 /*
897 * If transitioning out of NOT_RUNNING, increment nr_running. Note
898 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
899 * of multiple flags, not a single flag.
900 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200901 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
902 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heoe19e3972013-01-24 11:39:44 -0800903 atomic_inc(&pool->nr_running);
Tejun Heod302f012010-06-29 10:07:13 +0200904}
905
906/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200907 * find_worker_executing_work - find worker which is executing a work
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800908 * @pool: pool of interest
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200909 * @work: work to find worker for
910 *
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800911 * Find a worker which is executing @work on @pool by searching
912 * @pool->busy_hash which is keyed by the address of @work. For a worker
Tejun Heoa2c1c572012-12-18 10:35:02 -0800913 * to match, its current execution should match the address of @work and
914 * its work function. This is to avoid unwanted dependency between
915 * unrelated work executions through a work item being recycled while still
916 * being executed.
917 *
918 * This is a bit tricky. A work item may be freed once its execution
919 * starts and nothing prevents the freed area from being recycled for
920 * another work item. If the same work item address ends up being reused
921 * before the original execution finishes, workqueue will identify the
922 * recycled work item as currently executing and make it wait until the
923 * current execution finishes, introducing an unwanted dependency.
924 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700925 * This function checks the work item address and work function to avoid
926 * false positives. Note that this isn't complete as one may construct a
927 * work function which can introduce dependency onto itself through a
928 * recycled work item. Well, if somebody wants to shoot oneself in the
929 * foot that badly, there's only so much we can do, and if such deadlock
930 * actually occurs, it should be easy to locate the culprit work function.
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200931 *
932 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800933 * spin_lock_irq(pool->lock).
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200934 *
935 * RETURNS:
936 * Pointer to worker which is executing @work if found, NULL
937 * otherwise.
938 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800939static struct worker *find_worker_executing_work(struct worker_pool *pool,
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200940 struct work_struct *work)
941{
Sasha Levin42f85702012-12-17 10:01:23 -0500942 struct worker *worker;
Sasha Levin42f85702012-12-17 10:01:23 -0500943
Sasha Levinb67bfe02013-02-27 17:06:00 -0800944 hash_for_each_possible(pool->busy_hash, worker, hentry,
Tejun Heoa2c1c572012-12-18 10:35:02 -0800945 (unsigned long)work)
946 if (worker->current_work == work &&
947 worker->current_func == work->func)
Sasha Levin42f85702012-12-17 10:01:23 -0500948 return worker;
949
950 return NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200951}
952
953/**
Tejun Heobf4ede02012-08-03 10:30:46 -0700954 * move_linked_works - move linked works to a list
955 * @work: start of series of works to be scheduled
956 * @head: target list to append @work to
957 * @nextp: out paramter for nested worklist walking
958 *
959 * Schedule linked works starting from @work to @head. Work series to
960 * be scheduled starts at @work and includes any consecutive work with
961 * WORK_STRUCT_LINKED set in its predecessor.
962 *
963 * If @nextp is not NULL, it's updated to point to the next work of
964 * the last scheduled work. This allows move_linked_works() to be
965 * nested inside outer list_for_each_entry_safe().
966 *
967 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800968 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -0700969 */
970static void move_linked_works(struct work_struct *work, struct list_head *head,
971 struct work_struct **nextp)
972{
973 struct work_struct *n;
974
975 /*
976 * Linked worklist will always end before the end of the list,
977 * use NULL for list head.
978 */
979 list_for_each_entry_safe_from(work, n, NULL, entry) {
980 list_move_tail(&work->entry, head);
981 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
982 break;
983 }
984
985 /*
986 * If we're already inside safe list traversal and have moved
987 * multiple works to the scheduled queue, the next position
988 * needs to be updated.
989 */
990 if (nextp)
991 *nextp = n;
992}
993
Tejun Heo8864b4e2013-03-12 11:30:04 -0700994/**
995 * get_pwq - get an extra reference on the specified pool_workqueue
996 * @pwq: pool_workqueue to get
997 *
998 * Obtain an extra reference on @pwq. The caller should guarantee that
999 * @pwq has positive refcnt and be holding the matching pool->lock.
1000 */
1001static void get_pwq(struct pool_workqueue *pwq)
1002{
1003 lockdep_assert_held(&pwq->pool->lock);
1004 WARN_ON_ONCE(pwq->refcnt <= 0);
1005 pwq->refcnt++;
1006}
1007
1008/**
1009 * put_pwq - put a pool_workqueue reference
1010 * @pwq: pool_workqueue to put
1011 *
1012 * Drop a reference of @pwq. If its refcnt reaches zero, schedule its
1013 * destruction. The caller should be holding the matching pool->lock.
1014 */
1015static void put_pwq(struct pool_workqueue *pwq)
1016{
1017 lockdep_assert_held(&pwq->pool->lock);
1018 if (likely(--pwq->refcnt))
1019 return;
1020 if (WARN_ON_ONCE(!(pwq->wq->flags & WQ_UNBOUND)))
1021 return;
1022 /*
1023 * @pwq can't be released under pool->lock, bounce to
1024 * pwq_unbound_release_workfn(). This never recurses on the same
1025 * pool->lock as this path is taken only for unbound workqueues and
1026 * the release work item is scheduled on a per-cpu workqueue. To
1027 * avoid lockdep warning, unbound pool->locks are given lockdep
1028 * subclass of 1 in get_unbound_pool().
1029 */
1030 schedule_work(&pwq->unbound_release_work);
1031}
1032
Tejun Heo112202d2013-02-13 19:29:12 -08001033static void pwq_activate_delayed_work(struct work_struct *work)
Tejun Heobf4ede02012-08-03 10:30:46 -07001034{
Tejun Heo112202d2013-02-13 19:29:12 -08001035 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobf4ede02012-08-03 10:30:46 -07001036
1037 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001038 move_linked_works(work, &pwq->pool->worklist, NULL);
Tejun Heobf4ede02012-08-03 10:30:46 -07001039 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo112202d2013-02-13 19:29:12 -08001040 pwq->nr_active++;
Tejun Heobf4ede02012-08-03 10:30:46 -07001041}
1042
Tejun Heo112202d2013-02-13 19:29:12 -08001043static void pwq_activate_first_delayed(struct pool_workqueue *pwq)
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001044{
Tejun Heo112202d2013-02-13 19:29:12 -08001045 struct work_struct *work = list_first_entry(&pwq->delayed_works,
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001046 struct work_struct, entry);
1047
Tejun Heo112202d2013-02-13 19:29:12 -08001048 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001049}
1050
Tejun Heobf4ede02012-08-03 10:30:46 -07001051/**
Tejun Heo112202d2013-02-13 19:29:12 -08001052 * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight
1053 * @pwq: pwq of interest
Tejun Heobf4ede02012-08-03 10:30:46 -07001054 * @color: color of work which left the queue
Tejun Heobf4ede02012-08-03 10:30:46 -07001055 *
1056 * A work either has completed or is removed from pending queue,
Tejun Heo112202d2013-02-13 19:29:12 -08001057 * decrement nr_in_flight of its pwq and handle workqueue flushing.
Tejun Heobf4ede02012-08-03 10:30:46 -07001058 *
1059 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001060 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -07001061 */
Tejun Heo112202d2013-02-13 19:29:12 -08001062static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color)
Tejun Heobf4ede02012-08-03 10:30:46 -07001063{
Tejun Heo8864b4e2013-03-12 11:30:04 -07001064 /* uncolored work items don't participate in flushing or nr_active */
Tejun Heobf4ede02012-08-03 10:30:46 -07001065 if (color == WORK_NO_COLOR)
Tejun Heo8864b4e2013-03-12 11:30:04 -07001066 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001067
Tejun Heo112202d2013-02-13 19:29:12 -08001068 pwq->nr_in_flight[color]--;
Tejun Heobf4ede02012-08-03 10:30:46 -07001069
Tejun Heo112202d2013-02-13 19:29:12 -08001070 pwq->nr_active--;
1071 if (!list_empty(&pwq->delayed_works)) {
Lai Jiangshanb3f9f402012-09-18 10:40:00 -07001072 /* one down, submit a delayed one */
Tejun Heo112202d2013-02-13 19:29:12 -08001073 if (pwq->nr_active < pwq->max_active)
1074 pwq_activate_first_delayed(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001075 }
1076
1077 /* is flush in progress and are we at the flushing tip? */
Tejun Heo112202d2013-02-13 19:29:12 -08001078 if (likely(pwq->flush_color != color))
Tejun Heo8864b4e2013-03-12 11:30:04 -07001079 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001080
1081 /* are there still in-flight works? */
Tejun Heo112202d2013-02-13 19:29:12 -08001082 if (pwq->nr_in_flight[color])
Tejun Heo8864b4e2013-03-12 11:30:04 -07001083 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001084
Tejun Heo112202d2013-02-13 19:29:12 -08001085 /* this pwq is done, clear flush_color */
1086 pwq->flush_color = -1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001087
1088 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001089 * If this was the last pwq, wake up the first flusher. It
Tejun Heobf4ede02012-08-03 10:30:46 -07001090 * will handle the rest.
1091 */
Tejun Heo112202d2013-02-13 19:29:12 -08001092 if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush))
1093 complete(&pwq->wq->first_flusher->done);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001094out_put:
1095 put_pwq(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001096}
1097
Tejun Heo36e227d2012-08-03 10:30:46 -07001098/**
Tejun Heobbb68df2012-08-03 10:30:46 -07001099 * try_to_grab_pending - steal work item from worklist and disable irq
Tejun Heo36e227d2012-08-03 10:30:46 -07001100 * @work: work item to steal
1101 * @is_dwork: @work is a delayed_work
Tejun Heobbb68df2012-08-03 10:30:46 -07001102 * @flags: place to store irq state
Tejun Heo36e227d2012-08-03 10:30:46 -07001103 *
1104 * Try to grab PENDING bit of @work. This function can handle @work in any
1105 * stable state - idle, on timer or on worklist. Return values are
1106 *
1107 * 1 if @work was pending and we successfully stole PENDING
1108 * 0 if @work was idle and we claimed PENDING
1109 * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry
Tejun Heobbb68df2012-08-03 10:30:46 -07001110 * -ENOENT if someone else is canceling @work, this state may persist
1111 * for arbitrarily long
Tejun Heo36e227d2012-08-03 10:30:46 -07001112 *
Tejun Heobbb68df2012-08-03 10:30:46 -07001113 * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001114 * interrupted while holding PENDING and @work off queue, irq must be
1115 * disabled on entry. This, combined with delayed_work->timer being
1116 * irqsafe, ensures that we return -EAGAIN for finite short period of time.
Tejun Heobbb68df2012-08-03 10:30:46 -07001117 *
1118 * On successful return, >= 0, irq is disabled and the caller is
1119 * responsible for releasing it using local_irq_restore(*@flags).
1120 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001121 * This function is safe to call from any context including IRQ handler.
Tejun Heobf4ede02012-08-03 10:30:46 -07001122 */
Tejun Heobbb68df2012-08-03 10:30:46 -07001123static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
1124 unsigned long *flags)
Tejun Heobf4ede02012-08-03 10:30:46 -07001125{
Tejun Heod565ed62013-01-24 11:01:33 -08001126 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08001127 struct pool_workqueue *pwq;
Tejun Heobf4ede02012-08-03 10:30:46 -07001128
Tejun Heobbb68df2012-08-03 10:30:46 -07001129 local_irq_save(*flags);
1130
Tejun Heo36e227d2012-08-03 10:30:46 -07001131 /* try to steal the timer if it exists */
1132 if (is_dwork) {
1133 struct delayed_work *dwork = to_delayed_work(work);
1134
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001135 /*
1136 * dwork->timer is irqsafe. If del_timer() fails, it's
1137 * guaranteed that the timer is not queued anywhere and not
1138 * running on the local CPU.
1139 */
Tejun Heo36e227d2012-08-03 10:30:46 -07001140 if (likely(del_timer(&dwork->timer)))
1141 return 1;
1142 }
1143
1144 /* try to claim PENDING the normal way */
Tejun Heobf4ede02012-08-03 10:30:46 -07001145 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1146 return 0;
1147
1148 /*
1149 * The queueing is in progress, or it is already queued. Try to
1150 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1151 */
Tejun Heod565ed62013-01-24 11:01:33 -08001152 pool = get_work_pool(work);
1153 if (!pool)
Tejun Heobbb68df2012-08-03 10:30:46 -07001154 goto fail;
Tejun Heobf4ede02012-08-03 10:30:46 -07001155
Tejun Heod565ed62013-01-24 11:01:33 -08001156 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001157 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001158 * work->data is guaranteed to point to pwq only while the work
1159 * item is queued on pwq->wq, and both updating work->data to point
1160 * to pwq on queueing and to pool on dequeueing are done under
1161 * pwq->pool->lock. This in turn guarantees that, if work->data
1162 * points to pwq which is associated with a locked pool, the work
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001163 * item is currently queued on that pool.
1164 */
Tejun Heo112202d2013-02-13 19:29:12 -08001165 pwq = get_work_pwq(work);
1166 if (pwq && pwq->pool == pool) {
Tejun Heo16062832013-02-06 18:04:53 -08001167 debug_work_deactivate(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001168
Tejun Heo16062832013-02-06 18:04:53 -08001169 /*
1170 * A delayed work item cannot be grabbed directly because
1171 * it might have linked NO_COLOR work items which, if left
Tejun Heo112202d2013-02-13 19:29:12 -08001172 * on the delayed_list, will confuse pwq->nr_active
Tejun Heo16062832013-02-06 18:04:53 -08001173 * management later on and cause stall. Make sure the work
1174 * item is activated before grabbing.
1175 */
1176 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
Tejun Heo112202d2013-02-13 19:29:12 -08001177 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001178
Tejun Heo16062832013-02-06 18:04:53 -08001179 list_del_init(&work->entry);
Tejun Heo112202d2013-02-13 19:29:12 -08001180 pwq_dec_nr_in_flight(get_work_pwq(work), get_work_color(work));
Tejun Heo36e227d2012-08-03 10:30:46 -07001181
Tejun Heo112202d2013-02-13 19:29:12 -08001182 /* work->data points to pwq iff queued, point to pool */
Tejun Heo16062832013-02-06 18:04:53 -08001183 set_work_pool_and_keep_pending(work, pool->id);
Lai Jiangshan4468a002013-02-06 18:04:53 -08001184
Tejun Heo16062832013-02-06 18:04:53 -08001185 spin_unlock(&pool->lock);
1186 return 1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001187 }
Tejun Heod565ed62013-01-24 11:01:33 -08001188 spin_unlock(&pool->lock);
Tejun Heobbb68df2012-08-03 10:30:46 -07001189fail:
1190 local_irq_restore(*flags);
1191 if (work_is_canceling(work))
1192 return -ENOENT;
1193 cpu_relax();
Tejun Heo36e227d2012-08-03 10:30:46 -07001194 return -EAGAIN;
Tejun Heobf4ede02012-08-03 10:30:46 -07001195}
1196
1197/**
Tejun Heo706026c2013-01-24 11:01:34 -08001198 * insert_work - insert a work into a pool
Tejun Heo112202d2013-02-13 19:29:12 -08001199 * @pwq: pwq @work belongs to
Tejun Heo4690c4a2010-06-29 10:07:10 +02001200 * @work: work to insert
1201 * @head: insertion point
1202 * @extra_flags: extra WORK_STRUCT_* flags to set
1203 *
Tejun Heo112202d2013-02-13 19:29:12 -08001204 * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to
Tejun Heo706026c2013-01-24 11:01:34 -08001205 * work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +02001206 *
1207 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001208 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02001209 */
Tejun Heo112202d2013-02-13 19:29:12 -08001210static void insert_work(struct pool_workqueue *pwq, struct work_struct *work,
1211 struct list_head *head, unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001212{
Tejun Heo112202d2013-02-13 19:29:12 -08001213 struct worker_pool *pool = pwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01001214
Tejun Heo4690c4a2010-06-29 10:07:10 +02001215 /* we own @work, set data and link */
Tejun Heo112202d2013-02-13 19:29:12 -08001216 set_work_pwq(work, pwq, extra_flags);
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -07001217 list_add_tail(&work->entry, head);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001218 get_pwq(pwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02001219
1220 /*
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001221 * Ensure either wq_worker_sleeping() sees the above
1222 * list_add_tail() or we see zero nr_running to avoid workers lying
1223 * around lazily while there are works to be processed.
Tejun Heoe22bee72010-06-29 10:07:14 +02001224 */
1225 smp_mb();
1226
Tejun Heo63d95a92012-07-12 14:46:37 -07001227 if (__need_more_worker(pool))
1228 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001229}
1230
Tejun Heoc8efcc22010-12-20 19:32:04 +01001231/*
1232 * Test whether @work is being queued from another work executing on the
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001233 * same workqueue.
Tejun Heoc8efcc22010-12-20 19:32:04 +01001234 */
1235static bool is_chained_work(struct workqueue_struct *wq)
1236{
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001237 struct worker *worker;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001238
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001239 worker = current_wq_worker();
1240 /*
1241 * Return %true iff I'm a worker execuing a work item on @wq. If
1242 * I'm @worker, it's safe to dereference it without locking.
1243 */
Tejun Heo112202d2013-02-13 19:29:12 -08001244 return worker && worker->current_pwq->wq == wq;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001245}
1246
Tejun Heod84ff052013-03-12 11:29:59 -07001247static void __queue_work(int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 struct work_struct *work)
1249{
Tejun Heo112202d2013-02-13 19:29:12 -08001250 struct pool_workqueue *pwq;
Tejun Heoc9178082013-03-12 11:30:04 -07001251 struct worker_pool *last_pool;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001252 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001253 unsigned int work_flags;
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001254 unsigned int req_cpu = cpu;
Tejun Heo8930cab2012-08-03 10:30:45 -07001255
1256 /*
1257 * While a work item is PENDING && off queue, a task trying to
1258 * steal the PENDING will busy-loop waiting for it to either get
1259 * queued or lose PENDING. Grabbing PENDING and queueing should
1260 * happen with IRQ disabled.
1261 */
1262 WARN_ON_ONCE(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001264 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001265
Tejun Heoc8efcc22010-12-20 19:32:04 +01001266 /* if dying, only works from the same workqueue are allowed */
Tejun Heo618b01e2013-03-12 11:30:04 -07001267 if (unlikely(wq->flags & __WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001268 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001269 return;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001270retry:
Tejun Heoc9178082013-03-12 11:30:04 -07001271 /* pwq which will be used unless @work is executing elsewhere */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001272 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo57469822012-08-03 10:30:45 -07001273 if (cpu == WORK_CPU_UNBOUND)
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001274 cpu = raw_smp_processor_id();
Tejun Heo7fb98ea2013-03-12 11:30:00 -07001275 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heoc9178082013-03-12 11:30:04 -07001276 } else {
1277 pwq = first_pwq(wq);
1278 }
Tejun Heodbf25762012-08-20 14:51:23 -07001279
Tejun Heoc9178082013-03-12 11:30:04 -07001280 /*
1281 * If @work was previously on a different pool, it might still be
1282 * running there, in which case the work needs to be queued on that
1283 * pool to guarantee non-reentrancy.
1284 */
1285 last_pool = get_work_pool(work);
1286 if (last_pool && last_pool != pwq->pool) {
1287 struct worker *worker;
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001288
Tejun Heoc9178082013-03-12 11:30:04 -07001289 spin_lock(&last_pool->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001290
Tejun Heoc9178082013-03-12 11:30:04 -07001291 worker = find_worker_executing_work(last_pool, work);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001292
Tejun Heoc9178082013-03-12 11:30:04 -07001293 if (worker && worker->current_pwq->wq == wq) {
1294 pwq = worker->current_pwq;
Tejun Heo8930cab2012-08-03 10:30:45 -07001295 } else {
Tejun Heoc9178082013-03-12 11:30:04 -07001296 /* meh... not running there, queue here */
1297 spin_unlock(&last_pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08001298 spin_lock(&pwq->pool->lock);
Tejun Heo8930cab2012-08-03 10:30:45 -07001299 }
Tejun Heof3421792010-07-02 10:03:51 +02001300 } else {
Tejun Heo112202d2013-02-13 19:29:12 -08001301 spin_lock(&pwq->pool->lock);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001302 }
1303
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001304 /*
1305 * pwq is determined and locked. For unbound pools, we could have
1306 * raced with pwq release and it could already be dead. If its
1307 * refcnt is zero, repeat pwq selection. Note that pwqs never die
1308 * without another pwq replacing it as the first pwq or while a
1309 * work item is executing on it, so the retying is guaranteed to
1310 * make forward-progress.
1311 */
1312 if (unlikely(!pwq->refcnt)) {
1313 if (wq->flags & WQ_UNBOUND) {
1314 spin_unlock(&pwq->pool->lock);
1315 cpu_relax();
1316 goto retry;
1317 }
1318 /* oops */
1319 WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt",
1320 wq->name, cpu);
1321 }
1322
Tejun Heo112202d2013-02-13 19:29:12 -08001323 /* pwq determined, queue */
1324 trace_workqueue_queue_work(req_cpu, pwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001325
Dan Carpenterf5b25522012-04-13 22:06:58 +03001326 if (WARN_ON(!list_empty(&work->entry))) {
Tejun Heo112202d2013-02-13 19:29:12 -08001327 spin_unlock(&pwq->pool->lock);
Dan Carpenterf5b25522012-04-13 22:06:58 +03001328 return;
1329 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001330
Tejun Heo112202d2013-02-13 19:29:12 -08001331 pwq->nr_in_flight[pwq->work_color]++;
1332 work_flags = work_color_to_flags(pwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001333
Tejun Heo112202d2013-02-13 19:29:12 -08001334 if (likely(pwq->nr_active < pwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001335 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001336 pwq->nr_active++;
1337 worklist = &pwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001338 } else {
1339 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo112202d2013-02-13 19:29:12 -08001340 worklist = &pwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001341 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001342
Tejun Heo112202d2013-02-13 19:29:12 -08001343 insert_work(pwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001344
Tejun Heo112202d2013-02-13 19:29:12 -08001345 spin_unlock(&pwq->pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346}
1347
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001348/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07001349 * queue_work_on - queue work on specific cpu
1350 * @cpu: CPU number to execute work on
1351 * @wq: workqueue to use
1352 * @work: work to queue
1353 *
Tejun Heod4283e92012-08-03 10:30:44 -07001354 * Returns %false if @work was already on a queue, %true otherwise.
Zhang Ruic1a220e2008-07-23 21:28:39 -07001355 *
1356 * We queue the work to a specific CPU, the caller must ensure it
1357 * can't go away.
1358 */
Tejun Heod4283e92012-08-03 10:30:44 -07001359bool queue_work_on(int cpu, struct workqueue_struct *wq,
1360 struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07001361{
Tejun Heod4283e92012-08-03 10:30:44 -07001362 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001363 unsigned long flags;
1364
1365 local_irq_save(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001366
Tejun Heo22df02b2010-06-29 10:07:10 +02001367 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001368 __queue_work(cpu, wq, work);
Tejun Heod4283e92012-08-03 10:30:44 -07001369 ret = true;
Zhang Ruic1a220e2008-07-23 21:28:39 -07001370 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001371
1372 local_irq_restore(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001373 return ret;
1374}
1375EXPORT_SYMBOL_GPL(queue_work_on);
1376
Tejun Heod8e794d2012-08-03 10:30:45 -07001377void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
David Howells52bad642006-11-22 14:54:01 +00001379 struct delayed_work *dwork = (struct delayed_work *)__data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001381 /* should have been called from irqsafe timer with irq already off */
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001382 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383}
Konstantin Khlebnikov1438ade52013-01-24 16:36:31 +04001384EXPORT_SYMBOL(delayed_work_timer_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001386static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
1387 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001389 struct timer_list *timer = &dwork->timer;
1390 struct work_struct *work = &dwork->work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001392 WARN_ON_ONCE(timer->function != delayed_work_timer_fn ||
1393 timer->data != (unsigned long)dwork);
Tejun Heofc4b5142012-12-04 07:40:39 -08001394 WARN_ON_ONCE(timer_pending(timer));
1395 WARN_ON_ONCE(!list_empty(&work->entry));
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001396
Tejun Heo8852aac2012-12-01 16:23:42 -08001397 /*
1398 * If @delay is 0, queue @dwork->work immediately. This is for
1399 * both optimization and correctness. The earliest @timer can
1400 * expire is on the closest next tick and delayed_work users depend
1401 * on that there's no such delay when @delay is 0.
1402 */
1403 if (!delay) {
1404 __queue_work(cpu, wq, &dwork->work);
1405 return;
1406 }
1407
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001408 timer_stats_timer_set_start_info(&dwork->timer);
1409
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001410 dwork->wq = wq;
Tejun Heo12650572012-08-08 09:38:42 -07001411 dwork->cpu = cpu;
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001412 timer->expires = jiffies + delay;
1413
1414 if (unlikely(cpu != WORK_CPU_UNBOUND))
1415 add_timer_on(timer, cpu);
1416 else
1417 add_timer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418}
1419
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001420/**
1421 * queue_delayed_work_on - queue work on specific CPU after delay
1422 * @cpu: CPU number to execute work on
1423 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001424 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001425 * @delay: number of jiffies to wait before queueing
1426 *
Tejun Heo715f1302012-08-03 10:30:46 -07001427 * Returns %false if @work was already on a queue, %true otherwise. If
1428 * @delay is zero and @dwork is idle, it will be scheduled for immediate
1429 * execution.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001430 */
Tejun Heod4283e92012-08-03 10:30:44 -07001431bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
1432 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001433{
David Howells52bad642006-11-22 14:54:01 +00001434 struct work_struct *work = &dwork->work;
Tejun Heod4283e92012-08-03 10:30:44 -07001435 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001436 unsigned long flags;
1437
1438 /* read the comment in __queue_work() */
1439 local_irq_save(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001440
Tejun Heo22df02b2010-06-29 10:07:10 +02001441 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001442 __queue_delayed_work(cpu, wq, dwork, delay);
Tejun Heod4283e92012-08-03 10:30:44 -07001443 ret = true;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001444 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001445
1446 local_irq_restore(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001447 return ret;
1448}
Dave Jonesae90dd52006-06-30 01:40:45 -04001449EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Tejun Heoc8e55f32010-06-29 10:07:12 +02001451/**
Tejun Heo8376fe22012-08-03 10:30:47 -07001452 * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
1453 * @cpu: CPU number to execute work on
1454 * @wq: workqueue to use
1455 * @dwork: work to queue
1456 * @delay: number of jiffies to wait before queueing
1457 *
1458 * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
1459 * modify @dwork's timer so that it expires after @delay. If @delay is
1460 * zero, @work is guaranteed to be scheduled immediately regardless of its
1461 * current state.
1462 *
1463 * Returns %false if @dwork was idle and queued, %true if @dwork was
1464 * pending and its timer was modified.
1465 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001466 * This function is safe to call from any context including IRQ handler.
Tejun Heo8376fe22012-08-03 10:30:47 -07001467 * See try_to_grab_pending() for details.
1468 */
1469bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
1470 struct delayed_work *dwork, unsigned long delay)
1471{
1472 unsigned long flags;
1473 int ret;
1474
1475 do {
1476 ret = try_to_grab_pending(&dwork->work, true, &flags);
1477 } while (unlikely(ret == -EAGAIN));
1478
1479 if (likely(ret >= 0)) {
1480 __queue_delayed_work(cpu, wq, dwork, delay);
1481 local_irq_restore(flags);
1482 }
1483
1484 /* -ENOENT from try_to_grab_pending() becomes %true */
1485 return ret;
1486}
1487EXPORT_SYMBOL_GPL(mod_delayed_work_on);
1488
1489/**
Tejun Heoc8e55f32010-06-29 10:07:12 +02001490 * worker_enter_idle - enter idle state
1491 * @worker: worker which is entering idle state
1492 *
1493 * @worker is entering idle state. Update stats and idle timer if
1494 * necessary.
1495 *
1496 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001497 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001498 */
1499static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001501 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Tejun Heo6183c002013-03-12 11:29:57 -07001503 if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) ||
1504 WARN_ON_ONCE(!list_empty(&worker->entry) &&
1505 (worker->hentry.next || worker->hentry.pprev)))
1506 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Tejun Heocb444762010-07-02 10:03:50 +02001508 /* can't use worker_set_flags(), also called from start_worker() */
1509 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001510 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001511 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001512
Tejun Heoc8e55f32010-06-29 10:07:12 +02001513 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001514 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001515
Tejun Heo628c78e2012-07-17 12:39:27 -07001516 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1517 mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
Tejun Heocb444762010-07-02 10:03:50 +02001518
Tejun Heo544ecf32012-05-14 15:04:50 -07001519 /*
Tejun Heo706026c2013-01-24 11:01:34 -08001520 * Sanity check nr_running. Because wq_unbind_fn() releases
Tejun Heod565ed62013-01-24 11:01:33 -08001521 * pool->lock between setting %WORKER_UNBOUND and zapping
Tejun Heo628c78e2012-07-17 12:39:27 -07001522 * nr_running, the warning may trigger spuriously. Check iff
1523 * unbind is not in progress.
Tejun Heo544ecf32012-05-14 15:04:50 -07001524 */
Tejun Heo24647572013-01-24 11:01:33 -08001525 WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001526 pool->nr_workers == pool->nr_idle &&
Tejun Heoe19e3972013-01-24 11:39:44 -08001527 atomic_read(&pool->nr_running));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528}
1529
Tejun Heoc8e55f32010-06-29 10:07:12 +02001530/**
1531 * worker_leave_idle - leave idle state
1532 * @worker: worker which is leaving idle state
1533 *
1534 * @worker is leaving idle state. Update stats.
1535 *
1536 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001537 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001538 */
1539static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001541 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Tejun Heo6183c002013-03-12 11:29:57 -07001543 if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE)))
1544 return;
Tejun Heod302f012010-06-29 10:07:13 +02001545 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001546 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001547 list_del_init(&worker->entry);
1548}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Tejun Heoe22bee72010-06-29 10:07:14 +02001550/**
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001551 * worker_maybe_bind_and_lock - try to bind %current to worker_pool and lock it
1552 * @pool: target worker_pool
1553 *
1554 * Bind %current to the cpu of @pool if it is associated and lock @pool.
Tejun Heoe22bee72010-06-29 10:07:14 +02001555 *
1556 * Works which are scheduled while the cpu is online must at least be
1557 * scheduled to a worker which is bound to the cpu so that if they are
1558 * flushed from cpu callbacks while cpu is going down, they are
1559 * guaranteed to execute on the cpu.
1560 *
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001561 * This function is to be used by unbound workers and rescuers to bind
Tejun Heoe22bee72010-06-29 10:07:14 +02001562 * themselves to the target cpu and may race with cpu going down or
1563 * coming online. kthread_bind() can't be used because it may put the
1564 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
Tejun Heo706026c2013-01-24 11:01:34 -08001565 * verbatim as it's best effort and blocking and pool may be
Tejun Heoe22bee72010-06-29 10:07:14 +02001566 * [dis]associated in the meantime.
1567 *
Tejun Heo706026c2013-01-24 11:01:34 -08001568 * This function tries set_cpus_allowed() and locks pool and verifies the
Tejun Heo24647572013-01-24 11:01:33 -08001569 * binding against %POOL_DISASSOCIATED which is set during
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001570 * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1571 * enters idle state or fetches works without dropping lock, it can
1572 * guarantee the scheduling requirement described in the first paragraph.
Tejun Heoe22bee72010-06-29 10:07:14 +02001573 *
1574 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001575 * Might sleep. Called without any lock but returns with pool->lock
Tejun Heoe22bee72010-06-29 10:07:14 +02001576 * held.
1577 *
1578 * RETURNS:
Tejun Heo706026c2013-01-24 11:01:34 -08001579 * %true if the associated pool is online (@worker is successfully
Tejun Heoe22bee72010-06-29 10:07:14 +02001580 * bound), %false if offline.
1581 */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001582static bool worker_maybe_bind_and_lock(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001583__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001584{
Tejun Heoe22bee72010-06-29 10:07:14 +02001585 while (true) {
1586 /*
1587 * The following call may fail, succeed or succeed
1588 * without actually migrating the task to the cpu if
1589 * it races with cpu hotunplug operation. Verify
Tejun Heo24647572013-01-24 11:01:33 -08001590 * against POOL_DISASSOCIATED.
Tejun Heoe22bee72010-06-29 10:07:14 +02001591 */
Tejun Heo24647572013-01-24 11:01:33 -08001592 if (!(pool->flags & POOL_DISASSOCIATED))
Tejun Heo7a4e3442013-03-12 11:30:00 -07001593 set_cpus_allowed_ptr(current, pool->attrs->cpumask);
Oleg Nesterov85f41862007-05-09 02:34:20 -07001594
Tejun Heod565ed62013-01-24 11:01:33 -08001595 spin_lock_irq(&pool->lock);
Tejun Heo24647572013-01-24 11:01:33 -08001596 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heoe22bee72010-06-29 10:07:14 +02001597 return false;
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001598 if (task_cpu(current) == pool->cpu &&
Tejun Heo7a4e3442013-03-12 11:30:00 -07001599 cpumask_equal(&current->cpus_allowed, pool->attrs->cpumask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001600 return true;
Tejun Heod565ed62013-01-24 11:01:33 -08001601 spin_unlock_irq(&pool->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001602
Tejun Heo5035b202011-04-29 18:08:37 +02001603 /*
1604 * We've raced with CPU hot[un]plug. Give it a breather
1605 * and retry migration. cond_resched() is required here;
1606 * otherwise, we might deadlock against cpu_stop trying to
1607 * bring down the CPU on non-preemptive kernel.
1608 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001609 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001610 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001611 }
1612}
1613
Tejun Heoc34056a2010-06-29 10:07:11 +02001614static struct worker *alloc_worker(void)
1615{
1616 struct worker *worker;
1617
1618 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001619 if (worker) {
1620 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001621 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heoe22bee72010-06-29 10:07:14 +02001622 /* on creation a worker is in !idle && prep state */
1623 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001624 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001625 return worker;
1626}
1627
1628/**
1629 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001630 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001631 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001632 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001633 * can be started by calling start_worker() or destroyed using
1634 * destroy_worker().
1635 *
1636 * CONTEXT:
1637 * Might sleep. Does GFP_KERNEL allocations.
1638 *
1639 * RETURNS:
1640 * Pointer to the newly created worker.
1641 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001642static struct worker *create_worker(struct worker_pool *pool)
Tejun Heoc34056a2010-06-29 10:07:11 +02001643{
Tejun Heo7a4e3442013-03-12 11:30:00 -07001644 const char *pri = pool->attrs->nice < 0 ? "H" : "";
Tejun Heoc34056a2010-06-29 10:07:11 +02001645 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001646 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001647
Tejun Heocd549682013-03-13 19:47:39 -07001648 lockdep_assert_held(&pool->manager_mutex);
1649
Tejun Heo822d8402013-03-19 13:45:21 -07001650 /*
1651 * ID is needed to determine kthread name. Allocate ID first
1652 * without installing the pointer.
1653 */
1654 idr_preload(GFP_KERNEL);
Tejun Heod565ed62013-01-24 11:01:33 -08001655 spin_lock_irq(&pool->lock);
Tejun Heo822d8402013-03-19 13:45:21 -07001656
1657 id = idr_alloc(&pool->worker_idr, NULL, 0, 0, GFP_NOWAIT);
1658
Tejun Heod565ed62013-01-24 11:01:33 -08001659 spin_unlock_irq(&pool->lock);
Tejun Heo822d8402013-03-19 13:45:21 -07001660 idr_preload_end();
1661 if (id < 0)
1662 goto fail;
Tejun Heoc34056a2010-06-29 10:07:11 +02001663
1664 worker = alloc_worker();
1665 if (!worker)
1666 goto fail;
1667
Tejun Heobd7bdd42012-07-12 14:46:37 -07001668 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001669 worker->id = id;
1670
Tejun Heo29c91e92013-03-12 11:30:03 -07001671 if (pool->cpu >= 0)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001672 worker->task = kthread_create_on_node(worker_thread,
Tejun Heoec22ca52013-01-24 11:01:33 -08001673 worker, cpu_to_node(pool->cpu),
Tejun Heod84ff052013-03-12 11:29:59 -07001674 "kworker/%d:%d%s", pool->cpu, id, pri);
Tejun Heof3421792010-07-02 10:03:51 +02001675 else
1676 worker->task = kthread_create(worker_thread, worker,
Tejun Heoac6104c2013-03-12 11:30:03 -07001677 "kworker/u%d:%d%s",
1678 pool->id, id, pri);
Tejun Heoc34056a2010-06-29 10:07:11 +02001679 if (IS_ERR(worker->task))
1680 goto fail;
1681
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001682 /*
1683 * set_cpus_allowed_ptr() will fail if the cpumask doesn't have any
1684 * online CPUs. It'll be re-applied when any of the CPUs come up.
1685 */
Tejun Heo7a4e3442013-03-12 11:30:00 -07001686 set_user_nice(worker->task, pool->attrs->nice);
1687 set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask);
Tejun Heo32704762012-07-13 22:16:45 -07001688
Tejun Heo14a40ff2013-03-19 13:45:20 -07001689 /* prevent userland from meddling with cpumask of workqueue workers */
1690 worker->task->flags |= PF_NO_SETAFFINITY;
Tejun Heo7a4e3442013-03-12 11:30:00 -07001691
1692 /*
1693 * The caller is responsible for ensuring %POOL_DISASSOCIATED
1694 * remains stable across this function. See the comments above the
1695 * flag definition for details.
1696 */
1697 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001698 worker->flags |= WORKER_UNBOUND;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001699
Tejun Heo822d8402013-03-19 13:45:21 -07001700 /* successful, commit the pointer to idr */
1701 spin_lock_irq(&pool->lock);
1702 idr_replace(&pool->worker_idr, worker, worker->id);
1703 spin_unlock_irq(&pool->lock);
1704
Tejun Heoc34056a2010-06-29 10:07:11 +02001705 return worker;
Tejun Heo822d8402013-03-19 13:45:21 -07001706
Tejun Heoc34056a2010-06-29 10:07:11 +02001707fail:
1708 if (id >= 0) {
Tejun Heod565ed62013-01-24 11:01:33 -08001709 spin_lock_irq(&pool->lock);
Tejun Heo822d8402013-03-19 13:45:21 -07001710 idr_remove(&pool->worker_idr, id);
Tejun Heod565ed62013-01-24 11:01:33 -08001711 spin_unlock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001712 }
1713 kfree(worker);
1714 return NULL;
1715}
1716
1717/**
1718 * start_worker - start a newly created worker
1719 * @worker: worker to start
1720 *
Tejun Heo706026c2013-01-24 11:01:34 -08001721 * Make the pool aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001722 *
1723 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001724 * spin_lock_irq(pool->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001725 */
1726static void start_worker(struct worker *worker)
1727{
Tejun Heocb444762010-07-02 10:03:50 +02001728 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001729 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001730 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001731 wake_up_process(worker->task);
1732}
1733
1734/**
Tejun Heoebf44d12013-03-13 19:47:39 -07001735 * create_and_start_worker - create and start a worker for a pool
1736 * @pool: the target pool
1737 *
Tejun Heocd549682013-03-13 19:47:39 -07001738 * Grab the managership of @pool and create and start a new worker for it.
Tejun Heoebf44d12013-03-13 19:47:39 -07001739 */
1740static int create_and_start_worker(struct worker_pool *pool)
1741{
1742 struct worker *worker;
1743
Tejun Heocd549682013-03-13 19:47:39 -07001744 mutex_lock(&pool->manager_mutex);
1745
Tejun Heoebf44d12013-03-13 19:47:39 -07001746 worker = create_worker(pool);
1747 if (worker) {
1748 spin_lock_irq(&pool->lock);
1749 start_worker(worker);
1750 spin_unlock_irq(&pool->lock);
1751 }
1752
Tejun Heocd549682013-03-13 19:47:39 -07001753 mutex_unlock(&pool->manager_mutex);
1754
Tejun Heoebf44d12013-03-13 19:47:39 -07001755 return worker ? 0 : -ENOMEM;
1756}
1757
1758/**
Tejun Heoc34056a2010-06-29 10:07:11 +02001759 * destroy_worker - destroy a workqueue worker
1760 * @worker: worker to be destroyed
1761 *
Tejun Heo706026c2013-01-24 11:01:34 -08001762 * Destroy @worker and adjust @pool stats accordingly.
Tejun Heoc8e55f32010-06-29 10:07:12 +02001763 *
1764 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001765 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001766 */
1767static void destroy_worker(struct worker *worker)
1768{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001769 struct worker_pool *pool = worker->pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001770
Tejun Heocd549682013-03-13 19:47:39 -07001771 lockdep_assert_held(&pool->manager_mutex);
1772 lockdep_assert_held(&pool->lock);
1773
Tejun Heoc34056a2010-06-29 10:07:11 +02001774 /* sanity check frenzy */
Tejun Heo6183c002013-03-12 11:29:57 -07001775 if (WARN_ON(worker->current_work) ||
1776 WARN_ON(!list_empty(&worker->scheduled)))
1777 return;
Tejun Heoc34056a2010-06-29 10:07:11 +02001778
Tejun Heoc8e55f32010-06-29 10:07:12 +02001779 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001780 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001781 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001782 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001783
1784 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001785 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001786
Tejun Heo822d8402013-03-19 13:45:21 -07001787 idr_remove(&pool->worker_idr, worker->id);
1788
Tejun Heod565ed62013-01-24 11:01:33 -08001789 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001790
Tejun Heoc34056a2010-06-29 10:07:11 +02001791 kthread_stop(worker->task);
1792 kfree(worker);
1793
Tejun Heod565ed62013-01-24 11:01:33 -08001794 spin_lock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001795}
1796
Tejun Heo63d95a92012-07-12 14:46:37 -07001797static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001798{
Tejun Heo63d95a92012-07-12 14:46:37 -07001799 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001800
Tejun Heod565ed62013-01-24 11:01:33 -08001801 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001802
Tejun Heo63d95a92012-07-12 14:46:37 -07001803 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001804 struct worker *worker;
1805 unsigned long expires;
1806
1807 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001808 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001809 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1810
1811 if (time_before(jiffies, expires))
Tejun Heo63d95a92012-07-12 14:46:37 -07001812 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001813 else {
1814 /* it's been idle for too long, wake up manager */
Tejun Heo11ebea52012-07-12 14:46:37 -07001815 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -07001816 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001817 }
1818 }
1819
Tejun Heod565ed62013-01-24 11:01:33 -08001820 spin_unlock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001821}
1822
Tejun Heo493a1722013-03-12 11:29:59 -07001823static void send_mayday(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001824{
Tejun Heo112202d2013-02-13 19:29:12 -08001825 struct pool_workqueue *pwq = get_work_pwq(work);
1826 struct workqueue_struct *wq = pwq->wq;
Tejun Heo493a1722013-03-12 11:29:59 -07001827
Tejun Heo2e109a22013-03-13 19:47:40 -07001828 lockdep_assert_held(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001829
Tejun Heo493008a2013-03-12 11:30:03 -07001830 if (!wq->rescuer)
Tejun Heo493a1722013-03-12 11:29:59 -07001831 return;
Tejun Heoe22bee72010-06-29 10:07:14 +02001832
1833 /* mayday mayday mayday */
Tejun Heo493a1722013-03-12 11:29:59 -07001834 if (list_empty(&pwq->mayday_node)) {
1835 list_add_tail(&pwq->mayday_node, &wq->maydays);
Tejun Heoe22bee72010-06-29 10:07:14 +02001836 wake_up_process(wq->rescuer->task);
Tejun Heo493a1722013-03-12 11:29:59 -07001837 }
Tejun Heoe22bee72010-06-29 10:07:14 +02001838}
1839
Tejun Heo706026c2013-01-24 11:01:34 -08001840static void pool_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001841{
Tejun Heo63d95a92012-07-12 14:46:37 -07001842 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001843 struct work_struct *work;
1844
Tejun Heo2e109a22013-03-13 19:47:40 -07001845 spin_lock_irq(&wq_mayday_lock); /* for wq->maydays */
Tejun Heo493a1722013-03-12 11:29:59 -07001846 spin_lock(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001847
Tejun Heo63d95a92012-07-12 14:46:37 -07001848 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001849 /*
1850 * We've been trying to create a new worker but
1851 * haven't been successful. We might be hitting an
1852 * allocation deadlock. Send distress signals to
1853 * rescuers.
1854 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001855 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001856 send_mayday(work);
1857 }
1858
Tejun Heo493a1722013-03-12 11:29:59 -07001859 spin_unlock(&pool->lock);
Tejun Heo2e109a22013-03-13 19:47:40 -07001860 spin_unlock_irq(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001861
Tejun Heo63d95a92012-07-12 14:46:37 -07001862 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001863}
1864
1865/**
1866 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001867 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001868 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001869 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001870 * have at least one idle worker on return from this function. If
1871 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001872 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001873 * possible allocation deadlock.
1874 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001875 * On return, need_to_create_worker() is guaranteed to be %false and
1876 * may_start_working() %true.
Tejun Heoe22bee72010-06-29 10:07:14 +02001877 *
1878 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001879 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001880 * multiple times. Does GFP_KERNEL allocations. Called only from
1881 * manager.
1882 *
1883 * RETURNS:
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001884 * %false if no action was taken and pool->lock stayed locked, %true
Tejun Heoe22bee72010-06-29 10:07:14 +02001885 * otherwise.
1886 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001887static bool maybe_create_worker(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001888__releases(&pool->lock)
1889__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001890{
Tejun Heo63d95a92012-07-12 14:46:37 -07001891 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001892 return false;
1893restart:
Tejun Heod565ed62013-01-24 11:01:33 -08001894 spin_unlock_irq(&pool->lock);
Tejun Heo9f9c236442010-07-14 11:31:20 +02001895
Tejun Heoe22bee72010-06-29 10:07:14 +02001896 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07001897 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001898
1899 while (true) {
1900 struct worker *worker;
1901
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001902 worker = create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001903 if (worker) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001904 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001905 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001906 start_worker(worker);
Tejun Heo6183c002013-03-12 11:29:57 -07001907 if (WARN_ON_ONCE(need_to_create_worker(pool)))
1908 goto restart;
Tejun Heoe22bee72010-06-29 10:07:14 +02001909 return true;
1910 }
1911
Tejun Heo63d95a92012-07-12 14:46:37 -07001912 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001913 break;
1914
Tejun Heoe22bee72010-06-29 10:07:14 +02001915 __set_current_state(TASK_INTERRUPTIBLE);
1916 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c236442010-07-14 11:31:20 +02001917
Tejun Heo63d95a92012-07-12 14:46:37 -07001918 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001919 break;
1920 }
1921
Tejun Heo63d95a92012-07-12 14:46:37 -07001922 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001923 spin_lock_irq(&pool->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07001924 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001925 goto restart;
1926 return true;
1927}
1928
1929/**
1930 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo63d95a92012-07-12 14:46:37 -07001931 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001932 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001933 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02001934 * IDLE_WORKER_TIMEOUT.
1935 *
1936 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001937 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001938 * multiple times. Called only from manager.
1939 *
1940 * RETURNS:
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001941 * %false if no action was taken and pool->lock stayed locked, %true
Tejun Heoe22bee72010-06-29 10:07:14 +02001942 * otherwise.
1943 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001944static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001945{
1946 bool ret = false;
1947
Tejun Heo63d95a92012-07-12 14:46:37 -07001948 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001949 struct worker *worker;
1950 unsigned long expires;
1951
Tejun Heo63d95a92012-07-12 14:46:37 -07001952 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001953 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1954
1955 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001956 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001957 break;
1958 }
1959
1960 destroy_worker(worker);
1961 ret = true;
1962 }
1963
1964 return ret;
1965}
1966
1967/**
1968 * manage_workers - manage worker pool
1969 * @worker: self
1970 *
Tejun Heo706026c2013-01-24 11:01:34 -08001971 * Assume the manager role and manage the worker pool @worker belongs
Tejun Heoe22bee72010-06-29 10:07:14 +02001972 * to. At any given time, there can be only zero or one manager per
Tejun Heo706026c2013-01-24 11:01:34 -08001973 * pool. The exclusion is handled automatically by this function.
Tejun Heoe22bee72010-06-29 10:07:14 +02001974 *
1975 * The caller can safely start processing works on false return. On
1976 * true return, it's guaranteed that need_to_create_worker() is false
1977 * and may_start_working() is true.
1978 *
1979 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001980 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001981 * multiple times. Does GFP_KERNEL allocations.
1982 *
1983 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08001984 * spin_lock_irq(pool->lock) which may be released and regrabbed
1985 * multiple times. Does GFP_KERNEL allocations.
Tejun Heoe22bee72010-06-29 10:07:14 +02001986 */
1987static bool manage_workers(struct worker *worker)
1988{
Tejun Heo63d95a92012-07-12 14:46:37 -07001989 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001990 bool ret = false;
1991
Tejun Heobc3a1af2013-03-13 19:47:39 -07001992 /*
1993 * Managership is governed by two mutexes - manager_arb and
1994 * manager_mutex. manager_arb handles arbitration of manager role.
1995 * Anyone who successfully grabs manager_arb wins the arbitration
1996 * and becomes the manager. mutex_trylock() on pool->manager_arb
1997 * failure while holding pool->lock reliably indicates that someone
1998 * else is managing the pool and the worker which failed trylock
1999 * can proceed to executing work items. This means that anyone
2000 * grabbing manager_arb is responsible for actually performing
2001 * manager duties. If manager_arb is grabbed and released without
2002 * actual management, the pool may stall indefinitely.
2003 *
2004 * manager_mutex is used for exclusion of actual management
2005 * operations. The holder of manager_mutex can be sure that none
2006 * of management operations, including creation and destruction of
2007 * workers, won't take place until the mutex is released. Because
2008 * manager_mutex doesn't interfere with manager role arbitration,
2009 * it is guaranteed that the pool's management, while may be
2010 * delayed, won't be disturbed by someone else grabbing
2011 * manager_mutex.
2012 */
Tejun Heo34a06bd2013-03-12 11:30:00 -07002013 if (!mutex_trylock(&pool->manager_arb))
Tejun Heoe22bee72010-06-29 10:07:14 +02002014 return ret;
2015
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002016 /*
Tejun Heobc3a1af2013-03-13 19:47:39 -07002017 * With manager arbitration won, manager_mutex would be free in
2018 * most cases. trylock first without dropping @pool->lock.
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002019 */
Tejun Heobc3a1af2013-03-13 19:47:39 -07002020 if (unlikely(!mutex_trylock(&pool->manager_mutex))) {
Tejun Heod565ed62013-01-24 11:01:33 -08002021 spin_unlock_irq(&pool->lock);
Tejun Heobc3a1af2013-03-13 19:47:39 -07002022 mutex_lock(&pool->manager_mutex);
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002023 ret = true;
2024 }
2025
Tejun Heo11ebea52012-07-12 14:46:37 -07002026 pool->flags &= ~POOL_MANAGE_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02002027
2028 /*
2029 * Destroy and then create so that may_start_working() is true
2030 * on return.
2031 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002032 ret |= maybe_destroy_workers(pool);
2033 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002034
Tejun Heobc3a1af2013-03-13 19:47:39 -07002035 mutex_unlock(&pool->manager_mutex);
Tejun Heo34a06bd2013-03-12 11:30:00 -07002036 mutex_unlock(&pool->manager_arb);
Tejun Heoe22bee72010-06-29 10:07:14 +02002037 return ret;
2038}
2039
Tejun Heoa62428c2010-06-29 10:07:10 +02002040/**
2041 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02002042 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02002043 * @work: work to process
2044 *
2045 * Process @work. This function contains all the logics necessary to
2046 * process a single work including synchronization against and
2047 * interaction with other workers on the same cpu, queueing and
2048 * flushing. As long as context requirement is met, any worker can
2049 * call this function to process a work.
2050 *
2051 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002052 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02002053 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002054static void process_one_work(struct worker *worker, struct work_struct *work)
Tejun Heod565ed62013-01-24 11:01:33 -08002055__releases(&pool->lock)
2056__acquires(&pool->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02002057{
Tejun Heo112202d2013-02-13 19:29:12 -08002058 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002059 struct worker_pool *pool = worker->pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002060 bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02002061 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02002062 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02002063#ifdef CONFIG_LOCKDEP
2064 /*
2065 * It is permissible to free the struct work_struct from
2066 * inside the function that is called from it, this we need to
2067 * take into account for lockdep too. To avoid bogus "held
2068 * lock freed" warnings as well as problems when looking into
2069 * work->lockdep_map, make a copy and use that here.
2070 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07002071 struct lockdep_map lockdep_map;
2072
2073 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002074#endif
Tejun Heo6fec10a2012-07-22 10:16:34 -07002075 /*
2076 * Ensure we're on the correct CPU. DISASSOCIATED test is
2077 * necessary to avoid spurious warnings from rescuers servicing the
Tejun Heo24647572013-01-24 11:01:33 -08002078 * unbound or a disassociated pool.
Tejun Heo6fec10a2012-07-22 10:16:34 -07002079 */
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002080 WARN_ON_ONCE(!(worker->flags & WORKER_UNBOUND) &&
Tejun Heo24647572013-01-24 11:01:33 -08002081 !(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heoec22ca52013-01-24 11:01:33 -08002082 raw_smp_processor_id() != pool->cpu);
Tejun Heo25511a42012-07-17 12:39:27 -07002083
Tejun Heo7e116292010-06-29 10:07:13 +02002084 /*
2085 * A single work shouldn't be executed concurrently by
2086 * multiple workers on a single cpu. Check whether anyone is
2087 * already processing the work. If so, defer the work to the
2088 * currently executing one.
2089 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002090 collision = find_worker_executing_work(pool, work);
Tejun Heo7e116292010-06-29 10:07:13 +02002091 if (unlikely(collision)) {
2092 move_linked_works(work, &collision->scheduled, NULL);
2093 return;
2094 }
2095
Tejun Heo8930cab2012-08-03 10:30:45 -07002096 /* claim and dequeue */
Tejun Heoa62428c2010-06-29 10:07:10 +02002097 debug_work_deactivate(work);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002098 hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work);
Tejun Heoc34056a2010-06-29 10:07:11 +02002099 worker->current_work = work;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002100 worker->current_func = work->func;
Tejun Heo112202d2013-02-13 19:29:12 -08002101 worker->current_pwq = pwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002102 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002103
Tejun Heoa62428c2010-06-29 10:07:10 +02002104 list_del_init(&work->entry);
2105
Tejun Heo649027d2010-06-29 10:07:14 +02002106 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02002107 * CPU intensive works don't participate in concurrency
2108 * management. They're the scheduler's responsibility.
2109 */
2110 if (unlikely(cpu_intensive))
2111 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
2112
Tejun Heo974271c2012-07-12 14:46:37 -07002113 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002114 * Unbound pool isn't concurrency managed and work items should be
Tejun Heo974271c2012-07-12 14:46:37 -07002115 * executed ASAP. Wake up another worker if necessary.
2116 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002117 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
2118 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07002119
Tejun Heo8930cab2012-08-03 10:30:45 -07002120 /*
Tejun Heo7c3eed52013-01-24 11:01:33 -08002121 * Record the last pool and clear PENDING which should be the last
Tejun Heod565ed62013-01-24 11:01:33 -08002122 * update to @work. Also, do this inside @pool->lock so that
Tejun Heo23657bb2012-08-13 17:08:19 -07002123 * PENDING and queued state changes happen together while IRQ is
2124 * disabled.
Tejun Heo8930cab2012-08-03 10:30:45 -07002125 */
Tejun Heo7c3eed52013-01-24 11:01:33 -08002126 set_work_pool_and_clear_pending(work, pool->id);
Tejun Heoa62428c2010-06-29 10:07:10 +02002127
Tejun Heod565ed62013-01-24 11:01:33 -08002128 spin_unlock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002129
Tejun Heo112202d2013-02-13 19:29:12 -08002130 lock_map_acquire_read(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002131 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002132 trace_workqueue_execute_start(work);
Tejun Heoa2c1c572012-12-18 10:35:02 -08002133 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002134 /*
2135 * While we must be careful to not use "work" after this, the trace
2136 * point will only record its address.
2137 */
2138 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002139 lock_map_release(&lockdep_map);
Tejun Heo112202d2013-02-13 19:29:12 -08002140 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002141
2142 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Valentin Ilie044c7822012-08-19 00:52:42 +03002143 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
2144 " last function: %pf\n",
Tejun Heoa2c1c572012-12-18 10:35:02 -08002145 current->comm, preempt_count(), task_pid_nr(current),
2146 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02002147 debug_show_held_locks(current);
2148 dump_stack();
2149 }
2150
Tejun Heod565ed62013-01-24 11:01:33 -08002151 spin_lock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002152
Tejun Heofb0e7be2010-06-29 10:07:15 +02002153 /* clear cpu intensive status */
2154 if (unlikely(cpu_intensive))
2155 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2156
Tejun Heoa62428c2010-06-29 10:07:10 +02002157 /* we're done with it, release */
Sasha Levin42f85702012-12-17 10:01:23 -05002158 hash_del(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002159 worker->current_work = NULL;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002160 worker->current_func = NULL;
Tejun Heo112202d2013-02-13 19:29:12 -08002161 worker->current_pwq = NULL;
2162 pwq_dec_nr_in_flight(pwq, work_color);
Tejun Heoa62428c2010-06-29 10:07:10 +02002163}
2164
Tejun Heoaffee4b2010-06-29 10:07:12 +02002165/**
2166 * process_scheduled_works - process scheduled works
2167 * @worker: self
2168 *
2169 * Process all scheduled works. Please note that the scheduled list
2170 * may change while processing a work, so this function repeatedly
2171 * fetches a work from the top and executes it.
2172 *
2173 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002174 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02002175 * multiple times.
2176 */
2177static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002179 while (!list_empty(&worker->scheduled)) {
2180 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002182 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184}
2185
Tejun Heo4690c4a2010-06-29 10:07:10 +02002186/**
2187 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02002188 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02002189 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002190 * The worker thread function. All workers belong to a worker_pool -
2191 * either a per-cpu one or dynamic unbound one. These workers process all
2192 * work items regardless of their specific target workqueue. The only
2193 * exception is work items which belong to workqueues with a rescuer which
2194 * will be explained in rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02002195 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002196static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
Tejun Heoc34056a2010-06-29 10:07:11 +02002198 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07002199 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Tejun Heoe22bee72010-06-29 10:07:14 +02002201 /* tell the scheduler that this is a workqueue worker */
2202 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002203woke_up:
Tejun Heod565ed62013-01-24 11:01:33 -08002204 spin_lock_irq(&pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Tejun Heoa9ab7752013-03-19 13:45:21 -07002206 /* am I supposed to die? */
2207 if (unlikely(worker->flags & WORKER_DIE)) {
Tejun Heod565ed62013-01-24 11:01:33 -08002208 spin_unlock_irq(&pool->lock);
Tejun Heoa9ab7752013-03-19 13:45:21 -07002209 WARN_ON_ONCE(!list_empty(&worker->entry));
2210 worker->task->flags &= ~PF_WQ_WORKER;
2211 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 }
2213
Tejun Heoc8e55f32010-06-29 10:07:12 +02002214 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002215recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002216 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002217 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002218 goto sleep;
2219
2220 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002221 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002222 goto recheck;
2223
Tejun Heoc8e55f32010-06-29 10:07:12 +02002224 /*
2225 * ->scheduled list can only be filled while a worker is
2226 * preparing to process a work or actually processing it.
2227 * Make sure nobody diddled with it while I was sleeping.
2228 */
Tejun Heo6183c002013-03-12 11:29:57 -07002229 WARN_ON_ONCE(!list_empty(&worker->scheduled));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002230
Tejun Heoe22bee72010-06-29 10:07:14 +02002231 /*
Tejun Heoa9ab7752013-03-19 13:45:21 -07002232 * Finish PREP stage. We're guaranteed to have at least one idle
2233 * worker or that someone else has already assumed the manager
2234 * role. This is where @worker starts participating in concurrency
2235 * management if applicable and concurrency management is restored
2236 * after being rebound. See rebind_workers() for details.
Tejun Heoe22bee72010-06-29 10:07:14 +02002237 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07002238 worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND);
Tejun Heoe22bee72010-06-29 10:07:14 +02002239
2240 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002241 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002242 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002243 struct work_struct, entry);
2244
2245 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2246 /* optimization path, not strictly necessary */
2247 process_one_work(worker, work);
2248 if (unlikely(!list_empty(&worker->scheduled)))
2249 process_scheduled_works(worker);
2250 } else {
2251 move_linked_works(work, &worker->scheduled, NULL);
2252 process_scheduled_works(worker);
2253 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002254 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002255
Tejun Heoe22bee72010-06-29 10:07:14 +02002256 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002257sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07002258 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002259 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002260
Tejun Heoc8e55f32010-06-29 10:07:12 +02002261 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002262 * pool->lock is held and there's no work to process and no need to
2263 * manage, sleep. Workers are woken up only while holding
2264 * pool->lock or from local cpu, so setting the current state
2265 * before releasing pool->lock is enough to prevent losing any
2266 * event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002267 */
2268 worker_enter_idle(worker);
2269 __set_current_state(TASK_INTERRUPTIBLE);
Tejun Heod565ed62013-01-24 11:01:33 -08002270 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02002271 schedule();
2272 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273}
2274
Tejun Heoe22bee72010-06-29 10:07:14 +02002275/**
2276 * rescuer_thread - the rescuer thread function
Tejun Heo111c2252013-01-17 17:16:24 -08002277 * @__rescuer: self
Tejun Heoe22bee72010-06-29 10:07:14 +02002278 *
2279 * Workqueue rescuer thread function. There's one rescuer for each
Tejun Heo493008a2013-03-12 11:30:03 -07002280 * workqueue which has WQ_MEM_RECLAIM set.
Tejun Heoe22bee72010-06-29 10:07:14 +02002281 *
Tejun Heo706026c2013-01-24 11:01:34 -08002282 * Regular work processing on a pool may block trying to create a new
Tejun Heoe22bee72010-06-29 10:07:14 +02002283 * worker which uses GFP_KERNEL allocation which has slight chance of
2284 * developing into deadlock if some works currently on the same queue
2285 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2286 * the problem rescuer solves.
2287 *
Tejun Heo706026c2013-01-24 11:01:34 -08002288 * When such condition is possible, the pool summons rescuers of all
2289 * workqueues which have works queued on the pool and let them process
Tejun Heoe22bee72010-06-29 10:07:14 +02002290 * those works so that forward progress can be guaranteed.
2291 *
2292 * This should happen rarely.
2293 */
Tejun Heo111c2252013-01-17 17:16:24 -08002294static int rescuer_thread(void *__rescuer)
Tejun Heoe22bee72010-06-29 10:07:14 +02002295{
Tejun Heo111c2252013-01-17 17:16:24 -08002296 struct worker *rescuer = __rescuer;
2297 struct workqueue_struct *wq = rescuer->rescue_wq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002298 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heoe22bee72010-06-29 10:07:14 +02002299
2300 set_user_nice(current, RESCUER_NICE_LEVEL);
Tejun Heo111c2252013-01-17 17:16:24 -08002301
2302 /*
2303 * Mark rescuer as worker too. As WORKER_PREP is never cleared, it
2304 * doesn't participate in concurrency management.
2305 */
2306 rescuer->task->flags |= PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002307repeat:
2308 set_current_state(TASK_INTERRUPTIBLE);
2309
Mike Galbraith412d32e2012-11-28 07:17:18 +01002310 if (kthread_should_stop()) {
2311 __set_current_state(TASK_RUNNING);
Tejun Heo111c2252013-01-17 17:16:24 -08002312 rescuer->task->flags &= ~PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002313 return 0;
Mike Galbraith412d32e2012-11-28 07:17:18 +01002314 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002315
Tejun Heo493a1722013-03-12 11:29:59 -07002316 /* see whether any pwq is asking for help */
Tejun Heo2e109a22013-03-13 19:47:40 -07002317 spin_lock_irq(&wq_mayday_lock);
Tejun Heo493a1722013-03-12 11:29:59 -07002318
2319 while (!list_empty(&wq->maydays)) {
2320 struct pool_workqueue *pwq = list_first_entry(&wq->maydays,
2321 struct pool_workqueue, mayday_node);
Tejun Heo112202d2013-02-13 19:29:12 -08002322 struct worker_pool *pool = pwq->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002323 struct work_struct *work, *n;
2324
2325 __set_current_state(TASK_RUNNING);
Tejun Heo493a1722013-03-12 11:29:59 -07002326 list_del_init(&pwq->mayday_node);
2327
Tejun Heo2e109a22013-03-13 19:47:40 -07002328 spin_unlock_irq(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002329
2330 /* migrate to the target cpu if possible */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08002331 worker_maybe_bind_and_lock(pool);
Lai Jiangshanb3104102013-02-19 12:17:02 -08002332 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002333
2334 /*
2335 * Slurp in all works issued via this workqueue and
2336 * process'em.
2337 */
Tejun Heo6183c002013-03-12 11:29:57 -07002338 WARN_ON_ONCE(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002339 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heo112202d2013-02-13 19:29:12 -08002340 if (get_work_pwq(work) == pwq)
Tejun Heoe22bee72010-06-29 10:07:14 +02002341 move_linked_works(work, scheduled, &n);
2342
2343 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002344
2345 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002346 * Leave this pool. If keep_working() is %true, notify a
Tejun Heo75769582011-02-14 14:04:46 +01002347 * regular worker; otherwise, we end up with 0 concurrency
2348 * and stalling the execution.
2349 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002350 if (keep_working(pool))
2351 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002352
Lai Jiangshanb3104102013-02-19 12:17:02 -08002353 rescuer->pool = NULL;
Tejun Heo493a1722013-03-12 11:29:59 -07002354 spin_unlock(&pool->lock);
Tejun Heo2e109a22013-03-13 19:47:40 -07002355 spin_lock(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002356 }
2357
Tejun Heo2e109a22013-03-13 19:47:40 -07002358 spin_unlock_irq(&wq_mayday_lock);
Tejun Heo493a1722013-03-12 11:29:59 -07002359
Tejun Heo111c2252013-01-17 17:16:24 -08002360 /* rescuers should never participate in concurrency management */
2361 WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
Tejun Heoe22bee72010-06-29 10:07:14 +02002362 schedule();
2363 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364}
2365
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002366struct wq_barrier {
2367 struct work_struct work;
2368 struct completion done;
2369};
2370
2371static void wq_barrier_func(struct work_struct *work)
2372{
2373 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2374 complete(&barr->done);
2375}
2376
Tejun Heo4690c4a2010-06-29 10:07:10 +02002377/**
2378 * insert_wq_barrier - insert a barrier work
Tejun Heo112202d2013-02-13 19:29:12 -08002379 * @pwq: pwq to insert barrier into
Tejun Heo4690c4a2010-06-29 10:07:10 +02002380 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002381 * @target: target work to attach @barr to
2382 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002383 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002384 * @barr is linked to @target such that @barr is completed only after
2385 * @target finishes execution. Please note that the ordering
2386 * guarantee is observed only with respect to @target and on the local
2387 * cpu.
2388 *
2389 * Currently, a queued barrier can't be canceled. This is because
2390 * try_to_grab_pending() can't determine whether the work to be
2391 * grabbed is at the head of the queue and thus can't clear LINKED
2392 * flag of the previous work while there must be a valid next work
2393 * after a work with LINKED flag set.
2394 *
2395 * Note that when @worker is non-NULL, @target may be modified
Tejun Heo112202d2013-02-13 19:29:12 -08002396 * underneath us, so we can't reliably determine pwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002397 *
2398 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002399 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002400 */
Tejun Heo112202d2013-02-13 19:29:12 -08002401static void insert_wq_barrier(struct pool_workqueue *pwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002402 struct wq_barrier *barr,
2403 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002404{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002405 struct list_head *head;
2406 unsigned int linked = 0;
2407
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002408 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002409 * debugobject calls are safe here even with pool->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002410 * as we know for sure that this will not trigger any of the
2411 * checks and call back into the fixup functions where we
2412 * might deadlock.
2413 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002414 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002415 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002416 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002417
Tejun Heoaffee4b2010-06-29 10:07:12 +02002418 /*
2419 * If @target is currently being executed, schedule the
2420 * barrier to the worker; otherwise, put it after @target.
2421 */
2422 if (worker)
2423 head = worker->scheduled.next;
2424 else {
2425 unsigned long *bits = work_data_bits(target);
2426
2427 head = target->entry.next;
2428 /* there can already be other linked works, inherit and set */
2429 linked = *bits & WORK_STRUCT_LINKED;
2430 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2431 }
2432
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002433 debug_work_activate(&barr->work);
Tejun Heo112202d2013-02-13 19:29:12 -08002434 insert_work(pwq, &barr->work, head,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002435 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002436}
2437
Tejun Heo73f53c42010-06-29 10:07:11 +02002438/**
Tejun Heo112202d2013-02-13 19:29:12 -08002439 * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing
Tejun Heo73f53c42010-06-29 10:07:11 +02002440 * @wq: workqueue being flushed
2441 * @flush_color: new flush color, < 0 for no-op
2442 * @work_color: new work color, < 0 for no-op
2443 *
Tejun Heo112202d2013-02-13 19:29:12 -08002444 * Prepare pwqs for workqueue flushing.
Tejun Heo73f53c42010-06-29 10:07:11 +02002445 *
Tejun Heo112202d2013-02-13 19:29:12 -08002446 * If @flush_color is non-negative, flush_color on all pwqs should be
2447 * -1. If no pwq has in-flight commands at the specified color, all
2448 * pwq->flush_color's stay at -1 and %false is returned. If any pwq
2449 * has in flight commands, its pwq->flush_color is set to
2450 * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq
Tejun Heo73f53c42010-06-29 10:07:11 +02002451 * wakeup logic is armed and %true is returned.
2452 *
2453 * The caller should have initialized @wq->first_flusher prior to
2454 * calling this function with non-negative @flush_color. If
2455 * @flush_color is negative, no flush color update is done and %false
2456 * is returned.
2457 *
Tejun Heo112202d2013-02-13 19:29:12 -08002458 * If @work_color is non-negative, all pwqs should have the same
Tejun Heo73f53c42010-06-29 10:07:11 +02002459 * work_color which is previous to @work_color and all will be
2460 * advanced to @work_color.
2461 *
2462 * CONTEXT:
2463 * mutex_lock(wq->flush_mutex).
2464 *
2465 * RETURNS:
2466 * %true if @flush_color >= 0 and there's something to flush. %false
2467 * otherwise.
2468 */
Tejun Heo112202d2013-02-13 19:29:12 -08002469static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
Tejun Heo73f53c42010-06-29 10:07:11 +02002470 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471{
Tejun Heo73f53c42010-06-29 10:07:11 +02002472 bool wait = false;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002473 struct pool_workqueue *pwq;
Oleg Nesterov14441962007-05-23 13:57:57 -07002474
Tejun Heo73f53c42010-06-29 10:07:11 +02002475 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002476 WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush));
Tejun Heo112202d2013-02-13 19:29:12 -08002477 atomic_set(&wq->nr_pwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002478 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002479
Tejun Heo76af4d92013-03-12 11:30:00 -07002480 local_irq_disable();
2481
Tejun Heo49e3cf42013-03-12 11:29:58 -07002482 for_each_pwq(pwq, wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08002483 struct worker_pool *pool = pwq->pool;
Tejun Heo73f53c42010-06-29 10:07:11 +02002484
Tejun Heo76af4d92013-03-12 11:30:00 -07002485 spin_lock(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002486
2487 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002488 WARN_ON_ONCE(pwq->flush_color != -1);
Tejun Heo73f53c42010-06-29 10:07:11 +02002489
Tejun Heo112202d2013-02-13 19:29:12 -08002490 if (pwq->nr_in_flight[flush_color]) {
2491 pwq->flush_color = flush_color;
2492 atomic_inc(&wq->nr_pwqs_to_flush);
Tejun Heo73f53c42010-06-29 10:07:11 +02002493 wait = true;
2494 }
2495 }
2496
2497 if (work_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002498 WARN_ON_ONCE(work_color != work_next_color(pwq->work_color));
Tejun Heo112202d2013-02-13 19:29:12 -08002499 pwq->work_color = work_color;
Tejun Heo73f53c42010-06-29 10:07:11 +02002500 }
2501
Tejun Heo76af4d92013-03-12 11:30:00 -07002502 spin_unlock(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002503 }
2504
Tejun Heo76af4d92013-03-12 11:30:00 -07002505 local_irq_enable();
2506
Tejun Heo112202d2013-02-13 19:29:12 -08002507 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush))
Tejun Heo73f53c42010-06-29 10:07:11 +02002508 complete(&wq->first_flusher->done);
2509
2510 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511}
2512
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002513/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002515 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002517 * This function sleeps until all work items which were queued on entry
2518 * have finished execution, but it is not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002520void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521{
Tejun Heo73f53c42010-06-29 10:07:11 +02002522 struct wq_flusher this_flusher = {
2523 .list = LIST_HEAD_INIT(this_flusher.list),
2524 .flush_color = -1,
2525 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2526 };
2527 int next_color;
Oleg Nesterovb1f4ec172007-05-09 02:34:12 -07002528
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002529 lock_map_acquire(&wq->lockdep_map);
2530 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002531
2532 mutex_lock(&wq->flush_mutex);
2533
2534 /*
2535 * Start-to-wait phase
2536 */
2537 next_color = work_next_color(wq->work_color);
2538
2539 if (next_color != wq->flush_color) {
2540 /*
2541 * Color space is not full. The current work_color
2542 * becomes our flush_color and work_color is advanced
2543 * by one.
2544 */
Tejun Heo6183c002013-03-12 11:29:57 -07002545 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow));
Tejun Heo73f53c42010-06-29 10:07:11 +02002546 this_flusher.flush_color = wq->work_color;
2547 wq->work_color = next_color;
2548
2549 if (!wq->first_flusher) {
2550 /* no flush in progress, become the first flusher */
Tejun Heo6183c002013-03-12 11:29:57 -07002551 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002552
2553 wq->first_flusher = &this_flusher;
2554
Tejun Heo112202d2013-02-13 19:29:12 -08002555 if (!flush_workqueue_prep_pwqs(wq, wq->flush_color,
Tejun Heo73f53c42010-06-29 10:07:11 +02002556 wq->work_color)) {
2557 /* nothing to flush, done */
2558 wq->flush_color = next_color;
2559 wq->first_flusher = NULL;
2560 goto out_unlock;
2561 }
2562 } else {
2563 /* wait in queue */
Tejun Heo6183c002013-03-12 11:29:57 -07002564 WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002565 list_add_tail(&this_flusher.list, &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002566 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002567 }
2568 } else {
2569 /*
2570 * Oops, color space is full, wait on overflow queue.
2571 * The next flush completion will assign us
2572 * flush_color and transfer to flusher_queue.
2573 */
2574 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2575 }
2576
2577 mutex_unlock(&wq->flush_mutex);
2578
2579 wait_for_completion(&this_flusher.done);
2580
2581 /*
2582 * Wake-up-and-cascade phase
2583 *
2584 * First flushers are responsible for cascading flushes and
2585 * handling overflow. Non-first flushers can simply return.
2586 */
2587 if (wq->first_flusher != &this_flusher)
2588 return;
2589
2590 mutex_lock(&wq->flush_mutex);
2591
Tejun Heo4ce48b32010-07-02 10:03:51 +02002592 /* we might have raced, check again with mutex held */
2593 if (wq->first_flusher != &this_flusher)
2594 goto out_unlock;
2595
Tejun Heo73f53c42010-06-29 10:07:11 +02002596 wq->first_flusher = NULL;
2597
Tejun Heo6183c002013-03-12 11:29:57 -07002598 WARN_ON_ONCE(!list_empty(&this_flusher.list));
2599 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002600
2601 while (true) {
2602 struct wq_flusher *next, *tmp;
2603
2604 /* complete all the flushers sharing the current flush color */
2605 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2606 if (next->flush_color != wq->flush_color)
2607 break;
2608 list_del_init(&next->list);
2609 complete(&next->done);
2610 }
2611
Tejun Heo6183c002013-03-12 11:29:57 -07002612 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) &&
2613 wq->flush_color != work_next_color(wq->work_color));
Tejun Heo73f53c42010-06-29 10:07:11 +02002614
2615 /* this flush_color is finished, advance by one */
2616 wq->flush_color = work_next_color(wq->flush_color);
2617
2618 /* one color has been freed, handle overflow queue */
2619 if (!list_empty(&wq->flusher_overflow)) {
2620 /*
2621 * Assign the same color to all overflowed
2622 * flushers, advance work_color and append to
2623 * flusher_queue. This is the start-to-wait
2624 * phase for these overflowed flushers.
2625 */
2626 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2627 tmp->flush_color = wq->work_color;
2628
2629 wq->work_color = work_next_color(wq->work_color);
2630
2631 list_splice_tail_init(&wq->flusher_overflow,
2632 &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002633 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002634 }
2635
2636 if (list_empty(&wq->flusher_queue)) {
Tejun Heo6183c002013-03-12 11:29:57 -07002637 WARN_ON_ONCE(wq->flush_color != wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002638 break;
2639 }
2640
2641 /*
2642 * Need to flush more colors. Make the next flusher
Tejun Heo112202d2013-02-13 19:29:12 -08002643 * the new first flusher and arm pwqs.
Tejun Heo73f53c42010-06-29 10:07:11 +02002644 */
Tejun Heo6183c002013-03-12 11:29:57 -07002645 WARN_ON_ONCE(wq->flush_color == wq->work_color);
2646 WARN_ON_ONCE(wq->flush_color != next->flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002647
2648 list_del_init(&next->list);
2649 wq->first_flusher = next;
2650
Tejun Heo112202d2013-02-13 19:29:12 -08002651 if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1))
Tejun Heo73f53c42010-06-29 10:07:11 +02002652 break;
2653
2654 /*
2655 * Meh... this color is already done, clear first
2656 * flusher and repeat cascading.
2657 */
2658 wq->first_flusher = NULL;
2659 }
2660
2661out_unlock:
2662 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663}
Dave Jonesae90dd52006-06-30 01:40:45 -04002664EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002666/**
2667 * drain_workqueue - drain a workqueue
2668 * @wq: workqueue to drain
2669 *
2670 * Wait until the workqueue becomes empty. While draining is in progress,
2671 * only chain queueing is allowed. IOW, only currently pending or running
2672 * work items on @wq can queue further work items on it. @wq is flushed
2673 * repeatedly until it becomes empty. The number of flushing is detemined
2674 * by the depth of chaining and should be relatively short. Whine if it
2675 * takes too long.
2676 */
2677void drain_workqueue(struct workqueue_struct *wq)
2678{
2679 unsigned int flush_cnt = 0;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002680 struct pool_workqueue *pwq;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002681
2682 /*
2683 * __queue_work() needs to test whether there are drainers, is much
2684 * hotter than drain_workqueue() and already looks at @wq->flags.
Tejun Heo618b01e2013-03-12 11:30:04 -07002685 * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers.
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002686 */
Tejun Heo5bcab332013-03-13 19:47:40 -07002687 mutex_lock(&wq_mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002688 if (!wq->nr_drainers++)
Tejun Heo618b01e2013-03-12 11:30:04 -07002689 wq->flags |= __WQ_DRAINING;
Tejun Heo5bcab332013-03-13 19:47:40 -07002690 mutex_unlock(&wq_mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002691reflush:
2692 flush_workqueue(wq);
2693
Tejun Heo76af4d92013-03-12 11:30:00 -07002694 local_irq_disable();
2695
Tejun Heo49e3cf42013-03-12 11:29:58 -07002696 for_each_pwq(pwq, wq) {
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002697 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002698
Tejun Heo76af4d92013-03-12 11:30:00 -07002699 spin_lock(&pwq->pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08002700 drained = !pwq->nr_active && list_empty(&pwq->delayed_works);
Tejun Heo76af4d92013-03-12 11:30:00 -07002701 spin_unlock(&pwq->pool->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002702
2703 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002704 continue;
2705
2706 if (++flush_cnt == 10 ||
2707 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002708 pr_warn("workqueue %s: drain_workqueue() isn't complete after %u tries\n",
Valentin Ilie044c7822012-08-19 00:52:42 +03002709 wq->name, flush_cnt);
Tejun Heo76af4d92013-03-12 11:30:00 -07002710
2711 local_irq_enable();
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002712 goto reflush;
2713 }
2714
Tejun Heo5bcab332013-03-13 19:47:40 -07002715 local_irq_enable();
2716
2717 mutex_lock(&wq_mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002718 if (!--wq->nr_drainers)
Tejun Heo618b01e2013-03-12 11:30:04 -07002719 wq->flags &= ~__WQ_DRAINING;
Tejun Heo5bcab332013-03-13 19:47:40 -07002720 mutex_unlock(&wq_mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002721}
2722EXPORT_SYMBOL_GPL(drain_workqueue);
2723
Tejun Heo606a5022012-08-20 14:51:23 -07002724static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
Tejun Heobaf59022010-09-16 10:42:16 +02002725{
2726 struct worker *worker = NULL;
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002727 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002728 struct pool_workqueue *pwq;
Tejun Heobaf59022010-09-16 10:42:16 +02002729
2730 might_sleep();
Tejun Heobaf59022010-09-16 10:42:16 +02002731
Tejun Heofa1b54e2013-03-12 11:30:00 -07002732 local_irq_disable();
2733 pool = get_work_pool(work);
2734 if (!pool) {
2735 local_irq_enable();
2736 return false;
2737 }
2738
2739 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08002740 /* see the comment in try_to_grab_pending() with the same code */
Tejun Heo112202d2013-02-13 19:29:12 -08002741 pwq = get_work_pwq(work);
2742 if (pwq) {
2743 if (unlikely(pwq->pool != pool))
Tejun Heobaf59022010-09-16 10:42:16 +02002744 goto already_gone;
Tejun Heo606a5022012-08-20 14:51:23 -07002745 } else {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002746 worker = find_worker_executing_work(pool, work);
Tejun Heobaf59022010-09-16 10:42:16 +02002747 if (!worker)
2748 goto already_gone;
Tejun Heo112202d2013-02-13 19:29:12 -08002749 pwq = worker->current_pwq;
Tejun Heo606a5022012-08-20 14:51:23 -07002750 }
Tejun Heobaf59022010-09-16 10:42:16 +02002751
Tejun Heo112202d2013-02-13 19:29:12 -08002752 insert_wq_barrier(pwq, barr, work, worker);
Tejun Heod565ed62013-01-24 11:01:33 -08002753 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002754
Tejun Heoe159489b2011-01-09 23:32:15 +01002755 /*
2756 * If @max_active is 1 or rescuer is in use, flushing another work
2757 * item on the same workqueue may lead to deadlock. Make sure the
2758 * flusher is not running on the same workqueue by verifying write
2759 * access.
2760 */
Tejun Heo493008a2013-03-12 11:30:03 -07002761 if (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)
Tejun Heo112202d2013-02-13 19:29:12 -08002762 lock_map_acquire(&pwq->wq->lockdep_map);
Tejun Heoe159489b2011-01-09 23:32:15 +01002763 else
Tejun Heo112202d2013-02-13 19:29:12 -08002764 lock_map_acquire_read(&pwq->wq->lockdep_map);
2765 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoe159489b2011-01-09 23:32:15 +01002766
Tejun Heobaf59022010-09-16 10:42:16 +02002767 return true;
2768already_gone:
Tejun Heod565ed62013-01-24 11:01:33 -08002769 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002770 return false;
2771}
2772
Oleg Nesterovdb700892008-07-25 01:47:49 -07002773/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002774 * flush_work - wait for a work to finish executing the last queueing instance
2775 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002776 *
Tejun Heo606a5022012-08-20 14:51:23 -07002777 * Wait until @work has finished execution. @work is guaranteed to be idle
2778 * on return if it hasn't been requeued since flush started.
Tejun Heo401a8d02010-09-16 10:36:00 +02002779 *
2780 * RETURNS:
2781 * %true if flush_work() waited for the work to finish execution,
2782 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002783 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002784bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002785{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002786 struct wq_barrier barr;
2787
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002788 lock_map_acquire(&work->lockdep_map);
2789 lock_map_release(&work->lockdep_map);
2790
Tejun Heo606a5022012-08-20 14:51:23 -07002791 if (start_flush_work(work, &barr)) {
Tejun Heobaf59022010-09-16 10:42:16 +02002792 wait_for_completion(&barr.done);
2793 destroy_work_on_stack(&barr.work);
2794 return true;
Tejun Heo606a5022012-08-20 14:51:23 -07002795 } else {
Tejun Heobaf59022010-09-16 10:42:16 +02002796 return false;
Tejun Heo606a5022012-08-20 14:51:23 -07002797 }
Oleg Nesterovdb700892008-07-25 01:47:49 -07002798}
2799EXPORT_SYMBOL_GPL(flush_work);
2800
Tejun Heo36e227d2012-08-03 10:30:46 -07002801static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
Tejun Heo401a8d02010-09-16 10:36:00 +02002802{
Tejun Heobbb68df2012-08-03 10:30:46 -07002803 unsigned long flags;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002804 int ret;
2805
2806 do {
Tejun Heobbb68df2012-08-03 10:30:46 -07002807 ret = try_to_grab_pending(work, is_dwork, &flags);
2808 /*
2809 * If someone else is canceling, wait for the same event it
2810 * would be waiting for before retrying.
2811 */
2812 if (unlikely(ret == -ENOENT))
Tejun Heo606a5022012-08-20 14:51:23 -07002813 flush_work(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002814 } while (unlikely(ret < 0));
2815
Tejun Heobbb68df2012-08-03 10:30:46 -07002816 /* tell other tasks trying to grab @work to back off */
2817 mark_work_canceling(work);
2818 local_irq_restore(flags);
2819
Tejun Heo606a5022012-08-20 14:51:23 -07002820 flush_work(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002821 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002822 return ret;
2823}
2824
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002825/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002826 * cancel_work_sync - cancel a work and wait for it to finish
2827 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002828 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002829 * Cancel @work and wait for its execution to finish. This function
2830 * can be used even if the work re-queues itself or migrates to
2831 * another workqueue. On return from this function, @work is
2832 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002833 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002834 * cancel_work_sync(&delayed_work->work) must not be used for
2835 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002836 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002837 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002838 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002839 *
2840 * RETURNS:
2841 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002842 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002843bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002844{
Tejun Heo36e227d2012-08-03 10:30:46 -07002845 return __cancel_work_timer(work, false);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002846}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002847EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002848
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002849/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002850 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2851 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002852 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002853 * Delayed timer is cancelled and the pending work is queued for
2854 * immediate execution. Like flush_work(), this function only
2855 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002856 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002857 * RETURNS:
2858 * %true if flush_work() waited for the work to finish execution,
2859 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002860 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002861bool flush_delayed_work(struct delayed_work *dwork)
2862{
Tejun Heo8930cab2012-08-03 10:30:45 -07002863 local_irq_disable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002864 if (del_timer_sync(&dwork->timer))
Lai Jiangshan60c057b2013-02-06 18:04:53 -08002865 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Tejun Heo8930cab2012-08-03 10:30:45 -07002866 local_irq_enable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002867 return flush_work(&dwork->work);
2868}
2869EXPORT_SYMBOL(flush_delayed_work);
2870
2871/**
Tejun Heo57b30ae2012-08-21 13:18:24 -07002872 * cancel_delayed_work - cancel a delayed work
2873 * @dwork: delayed_work to cancel
Tejun Heo09383492010-09-16 10:48:29 +02002874 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002875 * Kill off a pending delayed_work. Returns %true if @dwork was pending
2876 * and canceled; %false if wasn't pending. Note that the work callback
2877 * function may still be running on return, unless it returns %true and the
2878 * work doesn't re-arm itself. Explicitly flush or use
2879 * cancel_delayed_work_sync() to wait on it.
Tejun Heo09383492010-09-16 10:48:29 +02002880 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002881 * This function is safe to call from any context including IRQ handler.
Tejun Heo09383492010-09-16 10:48:29 +02002882 */
Tejun Heo57b30ae2012-08-21 13:18:24 -07002883bool cancel_delayed_work(struct delayed_work *dwork)
Tejun Heo09383492010-09-16 10:48:29 +02002884{
Tejun Heo57b30ae2012-08-21 13:18:24 -07002885 unsigned long flags;
2886 int ret;
2887
2888 do {
2889 ret = try_to_grab_pending(&dwork->work, true, &flags);
2890 } while (unlikely(ret == -EAGAIN));
2891
2892 if (unlikely(ret < 0))
2893 return false;
2894
Tejun Heo7c3eed52013-01-24 11:01:33 -08002895 set_work_pool_and_clear_pending(&dwork->work,
2896 get_work_pool_id(&dwork->work));
Tejun Heo57b30ae2012-08-21 13:18:24 -07002897 local_irq_restore(flags);
Dan Magenheimerc0158ca2012-10-18 16:31:37 -07002898 return ret;
Tejun Heo09383492010-09-16 10:48:29 +02002899}
Tejun Heo57b30ae2012-08-21 13:18:24 -07002900EXPORT_SYMBOL(cancel_delayed_work);
Tejun Heo09383492010-09-16 10:48:29 +02002901
2902/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002903 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2904 * @dwork: the delayed work cancel
2905 *
2906 * This is cancel_work_sync() for delayed works.
2907 *
2908 * RETURNS:
2909 * %true if @dwork was pending, %false otherwise.
2910 */
2911bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002912{
Tejun Heo36e227d2012-08-03 10:30:46 -07002913 return __cancel_work_timer(&dwork->work, true);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002914}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002915EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002917/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002918 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002919 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002920 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002921 * schedule_on_each_cpu() executes @func on each online CPU using the
2922 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002923 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002924 *
2925 * RETURNS:
2926 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002927 */
David Howells65f27f32006-11-22 14:55:48 +00002928int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002929{
2930 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002931 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002932
Andrew Mortonb6136772006-06-25 05:47:49 -07002933 works = alloc_percpu(struct work_struct);
2934 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002935 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002936
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002937 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002938
Christoph Lameter15316ba2006-01-08 01:00:43 -08002939 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002940 struct work_struct *work = per_cpu_ptr(works, cpu);
2941
2942 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002943 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002944 }
Tejun Heo93981802009-11-17 14:06:20 -08002945
2946 for_each_online_cpu(cpu)
2947 flush_work(per_cpu_ptr(works, cpu));
2948
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002949 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002950 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002951 return 0;
2952}
2953
Alan Sterneef6a7d2010-02-12 17:39:21 +09002954/**
2955 * flush_scheduled_work - ensure that any scheduled work has run to completion.
2956 *
2957 * Forces execution of the kernel-global workqueue and blocks until its
2958 * completion.
2959 *
2960 * Think twice before calling this function! It's very easy to get into
2961 * trouble if you don't take great care. Either of the following situations
2962 * will lead to deadlock:
2963 *
2964 * One of the work items currently on the workqueue needs to acquire
2965 * a lock held by your code or its caller.
2966 *
2967 * Your code is running in the context of a work routine.
2968 *
2969 * They will be detected by lockdep when they occur, but the first might not
2970 * occur very often. It depends on what work items are on the workqueue and
2971 * what locks they need, which you have no control over.
2972 *
2973 * In most situations flushing the entire workqueue is overkill; you merely
2974 * need to know that a particular work item isn't queued and isn't running.
2975 * In such cases you should use cancel_delayed_work_sync() or
2976 * cancel_work_sync() instead.
2977 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978void flush_scheduled_work(void)
2979{
Tejun Heod320c032010-06-29 10:07:14 +02002980 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981}
Dave Jonesae90dd52006-06-30 01:40:45 -04002982EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
2984/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06002985 * execute_in_process_context - reliably execute the routine with user context
2986 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06002987 * @ew: guaranteed storage for the execute work structure (must
2988 * be available when the work executes)
2989 *
2990 * Executes the function immediately if process context is available,
2991 * otherwise schedules the function for delayed execution.
2992 *
2993 * Returns: 0 - function was executed
2994 * 1 - function was scheduled for execution
2995 */
David Howells65f27f32006-11-22 14:55:48 +00002996int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06002997{
2998 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00002999 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003000 return 0;
3001 }
3002
David Howells65f27f32006-11-22 14:55:48 +00003003 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003004 schedule_work(&ew->work);
3005
3006 return 1;
3007}
3008EXPORT_SYMBOL_GPL(execute_in_process_context);
3009
Tejun Heo226223a2013-03-12 11:30:05 -07003010#ifdef CONFIG_SYSFS
3011/*
3012 * Workqueues with WQ_SYSFS flag set is visible to userland via
3013 * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the
3014 * following attributes.
3015 *
3016 * per_cpu RO bool : whether the workqueue is per-cpu or unbound
3017 * max_active RW int : maximum number of in-flight work items
3018 *
3019 * Unbound workqueues have the following extra attributes.
3020 *
3021 * id RO int : the associated pool ID
3022 * nice RW int : nice value of the workers
3023 * cpumask RW mask : bitmask of allowed CPUs for the workers
3024 */
3025struct wq_device {
3026 struct workqueue_struct *wq;
3027 struct device dev;
3028};
3029
3030static struct workqueue_struct *dev_to_wq(struct device *dev)
3031{
3032 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
3033
3034 return wq_dev->wq;
3035}
3036
3037static ssize_t wq_per_cpu_show(struct device *dev,
3038 struct device_attribute *attr, char *buf)
3039{
3040 struct workqueue_struct *wq = dev_to_wq(dev);
3041
3042 return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND));
3043}
3044
3045static ssize_t wq_max_active_show(struct device *dev,
3046 struct device_attribute *attr, char *buf)
3047{
3048 struct workqueue_struct *wq = dev_to_wq(dev);
3049
3050 return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active);
3051}
3052
3053static ssize_t wq_max_active_store(struct device *dev,
3054 struct device_attribute *attr,
3055 const char *buf, size_t count)
3056{
3057 struct workqueue_struct *wq = dev_to_wq(dev);
3058 int val;
3059
3060 if (sscanf(buf, "%d", &val) != 1 || val <= 0)
3061 return -EINVAL;
3062
3063 workqueue_set_max_active(wq, val);
3064 return count;
3065}
3066
3067static struct device_attribute wq_sysfs_attrs[] = {
3068 __ATTR(per_cpu, 0444, wq_per_cpu_show, NULL),
3069 __ATTR(max_active, 0644, wq_max_active_show, wq_max_active_store),
3070 __ATTR_NULL,
3071};
3072
3073static ssize_t wq_pool_id_show(struct device *dev,
3074 struct device_attribute *attr, char *buf)
3075{
3076 struct workqueue_struct *wq = dev_to_wq(dev);
3077 struct worker_pool *pool;
3078 int written;
3079
3080 rcu_read_lock_sched();
3081 pool = first_pwq(wq)->pool;
3082 written = scnprintf(buf, PAGE_SIZE, "%d\n", pool->id);
3083 rcu_read_unlock_sched();
3084
3085 return written;
3086}
3087
3088static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr,
3089 char *buf)
3090{
3091 struct workqueue_struct *wq = dev_to_wq(dev);
3092 int written;
3093
3094 rcu_read_lock_sched();
3095 written = scnprintf(buf, PAGE_SIZE, "%d\n",
3096 first_pwq(wq)->pool->attrs->nice);
3097 rcu_read_unlock_sched();
3098
3099 return written;
3100}
3101
3102/* prepare workqueue_attrs for sysfs store operations */
3103static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq)
3104{
3105 struct workqueue_attrs *attrs;
3106
3107 attrs = alloc_workqueue_attrs(GFP_KERNEL);
3108 if (!attrs)
3109 return NULL;
3110
3111 rcu_read_lock_sched();
3112 copy_workqueue_attrs(attrs, first_pwq(wq)->pool->attrs);
3113 rcu_read_unlock_sched();
3114 return attrs;
3115}
3116
3117static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr,
3118 const char *buf, size_t count)
3119{
3120 struct workqueue_struct *wq = dev_to_wq(dev);
3121 struct workqueue_attrs *attrs;
3122 int ret;
3123
3124 attrs = wq_sysfs_prep_attrs(wq);
3125 if (!attrs)
3126 return -ENOMEM;
3127
3128 if (sscanf(buf, "%d", &attrs->nice) == 1 &&
3129 attrs->nice >= -20 && attrs->nice <= 19)
3130 ret = apply_workqueue_attrs(wq, attrs);
3131 else
3132 ret = -EINVAL;
3133
3134 free_workqueue_attrs(attrs);
3135 return ret ?: count;
3136}
3137
3138static ssize_t wq_cpumask_show(struct device *dev,
3139 struct device_attribute *attr, char *buf)
3140{
3141 struct workqueue_struct *wq = dev_to_wq(dev);
3142 int written;
3143
3144 rcu_read_lock_sched();
3145 written = cpumask_scnprintf(buf, PAGE_SIZE,
3146 first_pwq(wq)->pool->attrs->cpumask);
3147 rcu_read_unlock_sched();
3148
3149 written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
3150 return written;
3151}
3152
3153static ssize_t wq_cpumask_store(struct device *dev,
3154 struct device_attribute *attr,
3155 const char *buf, size_t count)
3156{
3157 struct workqueue_struct *wq = dev_to_wq(dev);
3158 struct workqueue_attrs *attrs;
3159 int ret;
3160
3161 attrs = wq_sysfs_prep_attrs(wq);
3162 if (!attrs)
3163 return -ENOMEM;
3164
3165 ret = cpumask_parse(buf, attrs->cpumask);
3166 if (!ret)
3167 ret = apply_workqueue_attrs(wq, attrs);
3168
3169 free_workqueue_attrs(attrs);
3170 return ret ?: count;
3171}
3172
3173static struct device_attribute wq_sysfs_unbound_attrs[] = {
3174 __ATTR(pool_id, 0444, wq_pool_id_show, NULL),
3175 __ATTR(nice, 0644, wq_nice_show, wq_nice_store),
3176 __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store),
3177 __ATTR_NULL,
3178};
3179
3180static struct bus_type wq_subsys = {
3181 .name = "workqueue",
3182 .dev_attrs = wq_sysfs_attrs,
3183};
3184
3185static int __init wq_sysfs_init(void)
3186{
3187 return subsys_virtual_register(&wq_subsys, NULL);
3188}
3189core_initcall(wq_sysfs_init);
3190
3191static void wq_device_release(struct device *dev)
3192{
3193 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
3194
3195 kfree(wq_dev);
3196}
3197
3198/**
3199 * workqueue_sysfs_register - make a workqueue visible in sysfs
3200 * @wq: the workqueue to register
3201 *
3202 * Expose @wq in sysfs under /sys/bus/workqueue/devices.
3203 * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set
3204 * which is the preferred method.
3205 *
3206 * Workqueue user should use this function directly iff it wants to apply
3207 * workqueue_attrs before making the workqueue visible in sysfs; otherwise,
3208 * apply_workqueue_attrs() may race against userland updating the
3209 * attributes.
3210 *
3211 * Returns 0 on success, -errno on failure.
3212 */
3213int workqueue_sysfs_register(struct workqueue_struct *wq)
3214{
3215 struct wq_device *wq_dev;
3216 int ret;
3217
3218 /*
3219 * Adjusting max_active or creating new pwqs by applyting
3220 * attributes breaks ordering guarantee. Disallow exposing ordered
3221 * workqueues.
3222 */
3223 if (WARN_ON(wq->flags & __WQ_ORDERED))
3224 return -EINVAL;
3225
3226 wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL);
3227 if (!wq_dev)
3228 return -ENOMEM;
3229
3230 wq_dev->wq = wq;
3231 wq_dev->dev.bus = &wq_subsys;
3232 wq_dev->dev.init_name = wq->name;
3233 wq_dev->dev.release = wq_device_release;
3234
3235 /*
3236 * unbound_attrs are created separately. Suppress uevent until
3237 * everything is ready.
3238 */
3239 dev_set_uevent_suppress(&wq_dev->dev, true);
3240
3241 ret = device_register(&wq_dev->dev);
3242 if (ret) {
3243 kfree(wq_dev);
3244 wq->wq_dev = NULL;
3245 return ret;
3246 }
3247
3248 if (wq->flags & WQ_UNBOUND) {
3249 struct device_attribute *attr;
3250
3251 for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) {
3252 ret = device_create_file(&wq_dev->dev, attr);
3253 if (ret) {
3254 device_unregister(&wq_dev->dev);
3255 wq->wq_dev = NULL;
3256 return ret;
3257 }
3258 }
3259 }
3260
3261 kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD);
3262 return 0;
3263}
3264
3265/**
3266 * workqueue_sysfs_unregister - undo workqueue_sysfs_register()
3267 * @wq: the workqueue to unregister
3268 *
3269 * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister.
3270 */
3271static void workqueue_sysfs_unregister(struct workqueue_struct *wq)
3272{
3273 struct wq_device *wq_dev = wq->wq_dev;
3274
3275 if (!wq->wq_dev)
3276 return;
3277
3278 wq->wq_dev = NULL;
3279 device_unregister(&wq_dev->dev);
3280}
3281#else /* CONFIG_SYSFS */
3282static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { }
3283#endif /* CONFIG_SYSFS */
3284
Tejun Heo7a4e3442013-03-12 11:30:00 -07003285/**
3286 * free_workqueue_attrs - free a workqueue_attrs
3287 * @attrs: workqueue_attrs to free
3288 *
3289 * Undo alloc_workqueue_attrs().
3290 */
3291void free_workqueue_attrs(struct workqueue_attrs *attrs)
3292{
3293 if (attrs) {
3294 free_cpumask_var(attrs->cpumask);
3295 kfree(attrs);
3296 }
3297}
3298
3299/**
3300 * alloc_workqueue_attrs - allocate a workqueue_attrs
3301 * @gfp_mask: allocation mask to use
3302 *
3303 * Allocate a new workqueue_attrs, initialize with default settings and
3304 * return it. Returns NULL on failure.
3305 */
3306struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask)
3307{
3308 struct workqueue_attrs *attrs;
3309
3310 attrs = kzalloc(sizeof(*attrs), gfp_mask);
3311 if (!attrs)
3312 goto fail;
3313 if (!alloc_cpumask_var(&attrs->cpumask, gfp_mask))
3314 goto fail;
3315
3316 cpumask_setall(attrs->cpumask);
3317 return attrs;
3318fail:
3319 free_workqueue_attrs(attrs);
3320 return NULL;
3321}
3322
Tejun Heo29c91e92013-03-12 11:30:03 -07003323static void copy_workqueue_attrs(struct workqueue_attrs *to,
3324 const struct workqueue_attrs *from)
3325{
3326 to->nice = from->nice;
3327 cpumask_copy(to->cpumask, from->cpumask);
3328}
3329
3330/*
3331 * Hacky implementation of jhash of bitmaps which only considers the
3332 * specified number of bits. We probably want a proper implementation in
3333 * include/linux/jhash.h.
3334 */
3335static u32 jhash_bitmap(const unsigned long *bitmap, int bits, u32 hash)
3336{
3337 int nr_longs = bits / BITS_PER_LONG;
3338 int nr_leftover = bits % BITS_PER_LONG;
3339 unsigned long leftover = 0;
3340
3341 if (nr_longs)
3342 hash = jhash(bitmap, nr_longs * sizeof(long), hash);
3343 if (nr_leftover) {
3344 bitmap_copy(&leftover, bitmap + nr_longs, nr_leftover);
3345 hash = jhash(&leftover, sizeof(long), hash);
3346 }
3347 return hash;
3348}
3349
3350/* hash value of the content of @attr */
3351static u32 wqattrs_hash(const struct workqueue_attrs *attrs)
3352{
3353 u32 hash = 0;
3354
3355 hash = jhash_1word(attrs->nice, hash);
3356 hash = jhash_bitmap(cpumask_bits(attrs->cpumask), nr_cpu_ids, hash);
3357 return hash;
3358}
3359
3360/* content equality test */
3361static bool wqattrs_equal(const struct workqueue_attrs *a,
3362 const struct workqueue_attrs *b)
3363{
3364 if (a->nice != b->nice)
3365 return false;
3366 if (!cpumask_equal(a->cpumask, b->cpumask))
3367 return false;
3368 return true;
3369}
3370
Tejun Heo7a4e3442013-03-12 11:30:00 -07003371/**
3372 * init_worker_pool - initialize a newly zalloc'd worker_pool
3373 * @pool: worker_pool to initialize
3374 *
3375 * Initiailize a newly zalloc'd @pool. It also allocates @pool->attrs.
Tejun Heo29c91e92013-03-12 11:30:03 -07003376 * Returns 0 on success, -errno on failure. Even on failure, all fields
3377 * inside @pool proper are initialized and put_unbound_pool() can be called
3378 * on @pool safely to release it.
Tejun Heo7a4e3442013-03-12 11:30:00 -07003379 */
3380static int init_worker_pool(struct worker_pool *pool)
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003381{
3382 spin_lock_init(&pool->lock);
Tejun Heo29c91e92013-03-12 11:30:03 -07003383 pool->id = -1;
3384 pool->cpu = -1;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003385 pool->flags |= POOL_DISASSOCIATED;
3386 INIT_LIST_HEAD(&pool->worklist);
3387 INIT_LIST_HEAD(&pool->idle_list);
3388 hash_init(pool->busy_hash);
3389
3390 init_timer_deferrable(&pool->idle_timer);
3391 pool->idle_timer.function = idle_worker_timeout;
3392 pool->idle_timer.data = (unsigned long)pool;
3393
3394 setup_timer(&pool->mayday_timer, pool_mayday_timeout,
3395 (unsigned long)pool);
3396
3397 mutex_init(&pool->manager_arb);
Tejun Heobc3a1af2013-03-13 19:47:39 -07003398 mutex_init(&pool->manager_mutex);
Tejun Heo822d8402013-03-19 13:45:21 -07003399 idr_init(&pool->worker_idr);
Tejun Heo7a4e3442013-03-12 11:30:00 -07003400
Tejun Heo29c91e92013-03-12 11:30:03 -07003401 INIT_HLIST_NODE(&pool->hash_node);
3402 pool->refcnt = 1;
3403
3404 /* shouldn't fail above this point */
Tejun Heo7a4e3442013-03-12 11:30:00 -07003405 pool->attrs = alloc_workqueue_attrs(GFP_KERNEL);
3406 if (!pool->attrs)
3407 return -ENOMEM;
3408 return 0;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003409}
3410
Tejun Heo29c91e92013-03-12 11:30:03 -07003411static void rcu_free_pool(struct rcu_head *rcu)
3412{
3413 struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu);
3414
Tejun Heo822d8402013-03-19 13:45:21 -07003415 idr_destroy(&pool->worker_idr);
Tejun Heo29c91e92013-03-12 11:30:03 -07003416 free_workqueue_attrs(pool->attrs);
3417 kfree(pool);
3418}
3419
3420/**
3421 * put_unbound_pool - put a worker_pool
3422 * @pool: worker_pool to put
3423 *
3424 * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003425 * safe manner. get_unbound_pool() calls this function on its failure path
3426 * and this function should be able to release pools which went through,
3427 * successfully or not, init_worker_pool().
Tejun Heo29c91e92013-03-12 11:30:03 -07003428 */
3429static void put_unbound_pool(struct worker_pool *pool)
3430{
3431 struct worker *worker;
3432
Tejun Heo5bcab332013-03-13 19:47:40 -07003433 mutex_lock(&wq_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003434 if (--pool->refcnt) {
Tejun Heo5bcab332013-03-13 19:47:40 -07003435 mutex_unlock(&wq_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003436 return;
3437 }
3438
3439 /* sanity checks */
3440 if (WARN_ON(!(pool->flags & POOL_DISASSOCIATED)) ||
3441 WARN_ON(!list_empty(&pool->worklist))) {
Tejun Heo5bcab332013-03-13 19:47:40 -07003442 mutex_unlock(&wq_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003443 return;
3444 }
3445
3446 /* release id and unhash */
3447 if (pool->id >= 0)
3448 idr_remove(&worker_pool_idr, pool->id);
3449 hash_del(&pool->hash_node);
3450
Tejun Heo5bcab332013-03-13 19:47:40 -07003451 mutex_unlock(&wq_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003452
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003453 /*
3454 * Become the manager and destroy all workers. Grabbing
3455 * manager_arb prevents @pool's workers from blocking on
3456 * manager_mutex.
3457 */
Tejun Heo29c91e92013-03-12 11:30:03 -07003458 mutex_lock(&pool->manager_arb);
Tejun Heocd549682013-03-13 19:47:39 -07003459 mutex_lock(&pool->manager_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003460 spin_lock_irq(&pool->lock);
3461
3462 while ((worker = first_worker(pool)))
3463 destroy_worker(worker);
3464 WARN_ON(pool->nr_workers || pool->nr_idle);
3465
3466 spin_unlock_irq(&pool->lock);
Tejun Heocd549682013-03-13 19:47:39 -07003467 mutex_unlock(&pool->manager_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003468 mutex_unlock(&pool->manager_arb);
3469
3470 /* shut down the timers */
3471 del_timer_sync(&pool->idle_timer);
3472 del_timer_sync(&pool->mayday_timer);
3473
3474 /* sched-RCU protected to allow dereferences from get_work_pool() */
3475 call_rcu_sched(&pool->rcu, rcu_free_pool);
3476}
3477
3478/**
3479 * get_unbound_pool - get a worker_pool with the specified attributes
3480 * @attrs: the attributes of the worker_pool to get
3481 *
3482 * Obtain a worker_pool which has the same attributes as @attrs, bump the
3483 * reference count and return it. If there already is a matching
3484 * worker_pool, it will be used; otherwise, this function attempts to
3485 * create a new one. On failure, returns NULL.
3486 */
3487static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs)
3488{
Tejun Heo29c91e92013-03-12 11:30:03 -07003489 u32 hash = wqattrs_hash(attrs);
3490 struct worker_pool *pool;
Tejun Heo29c91e92013-03-12 11:30:03 -07003491
Tejun Heo5bcab332013-03-13 19:47:40 -07003492 mutex_lock(&wq_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003493
3494 /* do we already have a matching pool? */
Tejun Heo29c91e92013-03-12 11:30:03 -07003495 hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) {
3496 if (wqattrs_equal(pool->attrs, attrs)) {
3497 pool->refcnt++;
3498 goto out_unlock;
3499 }
3500 }
Tejun Heo29c91e92013-03-12 11:30:03 -07003501
3502 /* nope, create a new one */
3503 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
3504 if (!pool || init_worker_pool(pool) < 0)
3505 goto fail;
3506
Lai Jiangshan12ee4fc2013-03-20 03:28:01 +08003507 if (workqueue_freezing)
3508 pool->flags |= POOL_FREEZING;
3509
Tejun Heo8864b4e2013-03-12 11:30:04 -07003510 lockdep_set_subclass(&pool->lock, 1); /* see put_pwq() */
Tejun Heo29c91e92013-03-12 11:30:03 -07003511 copy_workqueue_attrs(pool->attrs, attrs);
3512
3513 if (worker_pool_assign_id(pool) < 0)
3514 goto fail;
3515
3516 /* create and start the initial worker */
Tejun Heoebf44d12013-03-13 19:47:39 -07003517 if (create_and_start_worker(pool) < 0)
Tejun Heo29c91e92013-03-12 11:30:03 -07003518 goto fail;
3519
Tejun Heo29c91e92013-03-12 11:30:03 -07003520 /* install */
Tejun Heo29c91e92013-03-12 11:30:03 -07003521 hash_add(unbound_pool_hash, &pool->hash_node, hash);
3522out_unlock:
Tejun Heo5bcab332013-03-13 19:47:40 -07003523 mutex_unlock(&wq_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003524 return pool;
3525fail:
Tejun Heo5bcab332013-03-13 19:47:40 -07003526 mutex_unlock(&wq_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003527 if (pool)
3528 put_unbound_pool(pool);
3529 return NULL;
3530}
3531
Tejun Heo8864b4e2013-03-12 11:30:04 -07003532static void rcu_free_pwq(struct rcu_head *rcu)
3533{
3534 kmem_cache_free(pwq_cache,
3535 container_of(rcu, struct pool_workqueue, rcu));
3536}
3537
3538/*
3539 * Scheduled on system_wq by put_pwq() when an unbound pwq hits zero refcnt
3540 * and needs to be destroyed.
3541 */
3542static void pwq_unbound_release_workfn(struct work_struct *work)
3543{
3544 struct pool_workqueue *pwq = container_of(work, struct pool_workqueue,
3545 unbound_release_work);
3546 struct workqueue_struct *wq = pwq->wq;
3547 struct worker_pool *pool = pwq->pool;
3548
3549 if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)))
3550 return;
3551
Tejun Heo75ccf592013-03-12 11:30:04 -07003552 /*
3553 * Unlink @pwq. Synchronization against flush_mutex isn't strictly
3554 * necessary on release but do it anyway. It's easier to verify
3555 * and consistent with the linking path.
3556 */
3557 mutex_lock(&wq->flush_mutex);
Tejun Heo794b18b2013-03-13 19:47:40 -07003558 spin_lock_irq(&pwq_lock);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003559 list_del_rcu(&pwq->pwqs_node);
Tejun Heo794b18b2013-03-13 19:47:40 -07003560 spin_unlock_irq(&pwq_lock);
Tejun Heo75ccf592013-03-12 11:30:04 -07003561 mutex_unlock(&wq->flush_mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003562
3563 put_unbound_pool(pool);
3564 call_rcu_sched(&pwq->rcu, rcu_free_pwq);
3565
3566 /*
3567 * If we're the last pwq going away, @wq is already dead and no one
3568 * is gonna access it anymore. Free it.
3569 */
3570 if (list_empty(&wq->pwqs))
3571 kfree(wq);
3572}
3573
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003574/**
Tejun Heo699ce092013-03-13 16:51:35 -07003575 * pwq_adjust_max_active - update a pwq's max_active to the current setting
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003576 * @pwq: target pool_workqueue
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003577 *
Tejun Heo699ce092013-03-13 16:51:35 -07003578 * If @pwq isn't freezing, set @pwq->max_active to the associated
3579 * workqueue's saved_max_active and activate delayed work items
3580 * accordingly. If @pwq is freezing, clear @pwq->max_active to zero.
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003581 */
Tejun Heo699ce092013-03-13 16:51:35 -07003582static void pwq_adjust_max_active(struct pool_workqueue *pwq)
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003583{
Tejun Heo699ce092013-03-13 16:51:35 -07003584 struct workqueue_struct *wq = pwq->wq;
3585 bool freezable = wq->flags & WQ_FREEZABLE;
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003586
Tejun Heo699ce092013-03-13 16:51:35 -07003587 /* for @wq->saved_max_active */
Tejun Heo794b18b2013-03-13 19:47:40 -07003588 lockdep_assert_held(&pwq_lock);
Tejun Heo699ce092013-03-13 16:51:35 -07003589
3590 /* fast exit for non-freezable wqs */
3591 if (!freezable && pwq->max_active == wq->saved_max_active)
3592 return;
3593
3594 spin_lock(&pwq->pool->lock);
3595
3596 if (!freezable || !(pwq->pool->flags & POOL_FREEZING)) {
3597 pwq->max_active = wq->saved_max_active;
3598
3599 while (!list_empty(&pwq->delayed_works) &&
3600 pwq->nr_active < pwq->max_active)
3601 pwq_activate_first_delayed(pwq);
Lai Jiangshan951a0782013-03-20 10:52:30 -07003602
3603 /*
3604 * Need to kick a worker after thawed or an unbound wq's
3605 * max_active is bumped. It's a slow path. Do it always.
3606 */
3607 wake_up_worker(pwq->pool);
Tejun Heo699ce092013-03-13 16:51:35 -07003608 } else {
3609 pwq->max_active = 0;
3610 }
3611
3612 spin_unlock(&pwq->pool->lock);
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003613}
3614
Tejun Heod2c1d402013-03-12 11:30:04 -07003615static void init_and_link_pwq(struct pool_workqueue *pwq,
3616 struct workqueue_struct *wq,
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003617 struct worker_pool *pool,
3618 struct pool_workqueue **p_last_pwq)
Tejun Heod2c1d402013-03-12 11:30:04 -07003619{
3620 BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK);
3621
3622 pwq->pool = pool;
3623 pwq->wq = wq;
3624 pwq->flush_color = -1;
Tejun Heo8864b4e2013-03-12 11:30:04 -07003625 pwq->refcnt = 1;
Tejun Heod2c1d402013-03-12 11:30:04 -07003626 INIT_LIST_HEAD(&pwq->delayed_works);
3627 INIT_LIST_HEAD(&pwq->mayday_node);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003628 INIT_WORK(&pwq->unbound_release_work, pwq_unbound_release_workfn);
Tejun Heod2c1d402013-03-12 11:30:04 -07003629
Tejun Heo75ccf592013-03-12 11:30:04 -07003630 mutex_lock(&wq->flush_mutex);
Tejun Heo794b18b2013-03-13 19:47:40 -07003631 spin_lock_irq(&pwq_lock);
Tejun Heo75ccf592013-03-12 11:30:04 -07003632
Tejun Heo983ca252013-03-13 16:51:35 -07003633 /*
3634 * Set the matching work_color. This is synchronized with
3635 * flush_mutex to avoid confusing flush_workqueue().
3636 */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003637 if (p_last_pwq)
3638 *p_last_pwq = first_pwq(wq);
Tejun Heo75ccf592013-03-12 11:30:04 -07003639 pwq->work_color = wq->work_color;
Tejun Heo983ca252013-03-13 16:51:35 -07003640
3641 /* sync max_active to the current setting */
3642 pwq_adjust_max_active(pwq);
3643
3644 /* link in @pwq */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003645 list_add_rcu(&pwq->pwqs_node, &wq->pwqs);
Tejun Heo75ccf592013-03-12 11:30:04 -07003646
Tejun Heo794b18b2013-03-13 19:47:40 -07003647 spin_unlock_irq(&pwq_lock);
Tejun Heo75ccf592013-03-12 11:30:04 -07003648 mutex_unlock(&wq->flush_mutex);
Tejun Heod2c1d402013-03-12 11:30:04 -07003649}
3650
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003651/**
3652 * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue
3653 * @wq: the target workqueue
3654 * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs()
3655 *
3656 * Apply @attrs to an unbound workqueue @wq. If @attrs doesn't match the
3657 * current attributes, a new pwq is created and made the first pwq which
3658 * will serve all new work items. Older pwqs are released as in-flight
3659 * work items finish. Note that a work item which repeatedly requeues
3660 * itself back-to-back will stay on its current pwq.
3661 *
3662 * Performs GFP_KERNEL allocations. Returns 0 on success and -errno on
3663 * failure.
3664 */
3665int apply_workqueue_attrs(struct workqueue_struct *wq,
3666 const struct workqueue_attrs *attrs)
3667{
3668 struct pool_workqueue *pwq, *last_pwq;
3669 struct worker_pool *pool;
3670
Tejun Heo8719dce2013-03-12 11:30:04 -07003671 /* only unbound workqueues can change attributes */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003672 if (WARN_ON(!(wq->flags & WQ_UNBOUND)))
3673 return -EINVAL;
3674
Tejun Heo8719dce2013-03-12 11:30:04 -07003675 /* creating multiple pwqs breaks ordering guarantee */
3676 if (WARN_ON((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs)))
3677 return -EINVAL;
3678
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003679 pwq = kmem_cache_zalloc(pwq_cache, GFP_KERNEL);
3680 if (!pwq)
3681 return -ENOMEM;
3682
3683 pool = get_unbound_pool(attrs);
3684 if (!pool) {
3685 kmem_cache_free(pwq_cache, pwq);
3686 return -ENOMEM;
3687 }
3688
3689 init_and_link_pwq(pwq, wq, pool, &last_pwq);
3690 if (last_pwq) {
3691 spin_lock_irq(&last_pwq->pool->lock);
3692 put_pwq(last_pwq);
3693 spin_unlock_irq(&last_pwq->pool->lock);
3694 }
3695
3696 return 0;
3697}
3698
Tejun Heo30cdf2492013-03-12 11:29:57 -07003699static int alloc_and_link_pwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003701 bool highpri = wq->flags & WQ_HIGHPRI;
Tejun Heo30cdf2492013-03-12 11:29:57 -07003702 int cpu;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01003703
Tejun Heo30cdf2492013-03-12 11:29:57 -07003704 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo420c0dd2013-03-12 11:29:59 -07003705 wq->cpu_pwqs = alloc_percpu(struct pool_workqueue);
3706 if (!wq->cpu_pwqs)
Tejun Heo30cdf2492013-03-12 11:29:57 -07003707 return -ENOMEM;
3708
3709 for_each_possible_cpu(cpu) {
Tejun Heo7fb98ea2013-03-12 11:30:00 -07003710 struct pool_workqueue *pwq =
3711 per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heo7a62c2c2013-03-12 11:30:03 -07003712 struct worker_pool *cpu_pools =
Tejun Heof02ae732013-03-12 11:30:03 -07003713 per_cpu(cpu_worker_pools, cpu);
Tejun Heo30cdf2492013-03-12 11:29:57 -07003714
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003715 init_and_link_pwq(pwq, wq, &cpu_pools[highpri], NULL);
Tejun Heo30cdf2492013-03-12 11:29:57 -07003716 }
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003717 return 0;
Tejun Heo30cdf2492013-03-12 11:29:57 -07003718 } else {
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003719 return apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]);
Tejun Heo30cdf2492013-03-12 11:29:57 -07003720 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003721}
3722
Tejun Heof3421792010-07-02 10:03:51 +02003723static int wq_clamp_max_active(int max_active, unsigned int flags,
3724 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003725{
Tejun Heof3421792010-07-02 10:03:51 +02003726 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3727
3728 if (max_active < 1 || max_active > lim)
Valentin Ilie044c7822012-08-19 00:52:42 +03003729 pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
3730 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003731
Tejun Heof3421792010-07-02 10:03:51 +02003732 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003733}
3734
Tejun Heob196be82012-01-10 15:11:35 -08003735struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02003736 unsigned int flags,
3737 int max_active,
3738 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08003739 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003740{
Tejun Heob196be82012-01-10 15:11:35 -08003741 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003742 struct workqueue_struct *wq;
Tejun Heo49e3cf42013-03-12 11:29:58 -07003743 struct pool_workqueue *pwq;
Tejun Heob196be82012-01-10 15:11:35 -08003744 size_t namelen;
3745
3746 /* determine namelen, allocate wq and format name */
3747 va_start(args, lock_name);
3748 va_copy(args1, args);
3749 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3750
3751 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3752 if (!wq)
Tejun Heod2c1d402013-03-12 11:30:04 -07003753 return NULL;
Tejun Heob196be82012-01-10 15:11:35 -08003754
3755 vsnprintf(wq->name, namelen, fmt, args1);
3756 va_end(args);
3757 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003758
Tejun Heod320c032010-06-29 10:07:14 +02003759 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003760 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003761
Tejun Heob196be82012-01-10 15:11:35 -08003762 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003763 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003764 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003765 mutex_init(&wq->flush_mutex);
Tejun Heo112202d2013-02-13 19:29:12 -08003766 atomic_set(&wq->nr_pwqs_to_flush, 0);
Tejun Heo30cdf2492013-03-12 11:29:57 -07003767 INIT_LIST_HEAD(&wq->pwqs);
Tejun Heo73f53c42010-06-29 10:07:11 +02003768 INIT_LIST_HEAD(&wq->flusher_queue);
3769 INIT_LIST_HEAD(&wq->flusher_overflow);
Tejun Heo493a1722013-03-12 11:29:59 -07003770 INIT_LIST_HEAD(&wq->maydays);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003771
Johannes Bergeb13ba82008-01-16 09:51:58 +01003772 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003773 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003774
Tejun Heo30cdf2492013-03-12 11:29:57 -07003775 if (alloc_and_link_pwqs(wq) < 0)
Tejun Heod2c1d402013-03-12 11:30:04 -07003776 goto err_free_wq;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003777
Tejun Heo493008a2013-03-12 11:30:03 -07003778 /*
3779 * Workqueues which may be used during memory reclaim should
3780 * have a rescuer to guarantee forward progress.
3781 */
3782 if (flags & WQ_MEM_RECLAIM) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003783 struct worker *rescuer;
3784
Tejun Heod2c1d402013-03-12 11:30:04 -07003785 rescuer = alloc_worker();
Tejun Heoe22bee72010-06-29 10:07:14 +02003786 if (!rescuer)
Tejun Heod2c1d402013-03-12 11:30:04 -07003787 goto err_destroy;
Tejun Heoe22bee72010-06-29 10:07:14 +02003788
Tejun Heo111c2252013-01-17 17:16:24 -08003789 rescuer->rescue_wq = wq;
3790 rescuer->task = kthread_create(rescuer_thread, rescuer, "%s",
Tejun Heob196be82012-01-10 15:11:35 -08003791 wq->name);
Tejun Heod2c1d402013-03-12 11:30:04 -07003792 if (IS_ERR(rescuer->task)) {
3793 kfree(rescuer);
3794 goto err_destroy;
3795 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003796
Tejun Heod2c1d402013-03-12 11:30:04 -07003797 wq->rescuer = rescuer;
Tejun Heo14a40ff2013-03-19 13:45:20 -07003798 rescuer->task->flags |= PF_NO_SETAFFINITY;
Tejun Heoe22bee72010-06-29 10:07:14 +02003799 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003800 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003801
Tejun Heo226223a2013-03-12 11:30:05 -07003802 if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq))
3803 goto err_destroy;
3804
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003805 /*
Tejun Heo5bcab332013-03-13 19:47:40 -07003806 * wq_mutex protects global freeze state and workqueues list. Grab
3807 * it, adjust max_active and add the new @wq to workqueues list.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003808 */
Tejun Heo5bcab332013-03-13 19:47:40 -07003809 mutex_lock(&wq_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003810
Tejun Heo794b18b2013-03-13 19:47:40 -07003811 spin_lock_irq(&pwq_lock);
Tejun Heo699ce092013-03-13 16:51:35 -07003812 for_each_pwq(pwq, wq)
3813 pwq_adjust_max_active(pwq);
Tejun Heo794b18b2013-03-13 19:47:40 -07003814 spin_unlock_irq(&pwq_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003815
Tejun Heo15376632010-06-29 10:07:11 +02003816 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003817
Tejun Heo5bcab332013-03-13 19:47:40 -07003818 mutex_unlock(&wq_mutex);
Tejun Heo15376632010-06-29 10:07:11 +02003819
Oleg Nesterov3af244332007-05-09 02:34:09 -07003820 return wq;
Tejun Heod2c1d402013-03-12 11:30:04 -07003821
3822err_free_wq:
3823 kfree(wq);
3824 return NULL;
3825err_destroy:
3826 destroy_workqueue(wq);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003827 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003828}
Tejun Heod320c032010-06-29 10:07:14 +02003829EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003830
3831/**
3832 * destroy_workqueue - safely terminate a workqueue
3833 * @wq: target workqueue
3834 *
3835 * Safely destroy a workqueue. All work currently pending will be done first.
3836 */
3837void destroy_workqueue(struct workqueue_struct *wq)
3838{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003839 struct pool_workqueue *pwq;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003840
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003841 /* drain it before proceeding with destruction */
3842 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003843
Tejun Heo6183c002013-03-12 11:29:57 -07003844 /* sanity checks */
Tejun Heo794b18b2013-03-13 19:47:40 -07003845 spin_lock_irq(&pwq_lock);
Tejun Heo49e3cf42013-03-12 11:29:58 -07003846 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07003847 int i;
3848
Tejun Heo76af4d92013-03-12 11:30:00 -07003849 for (i = 0; i < WORK_NR_COLORS; i++) {
3850 if (WARN_ON(pwq->nr_in_flight[i])) {
Tejun Heo794b18b2013-03-13 19:47:40 -07003851 spin_unlock_irq(&pwq_lock);
Tejun Heo6183c002013-03-12 11:29:57 -07003852 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07003853 }
3854 }
3855
Tejun Heo8864b4e2013-03-12 11:30:04 -07003856 if (WARN_ON(pwq->refcnt > 1) ||
3857 WARN_ON(pwq->nr_active) ||
Tejun Heo76af4d92013-03-12 11:30:00 -07003858 WARN_ON(!list_empty(&pwq->delayed_works))) {
Tejun Heo794b18b2013-03-13 19:47:40 -07003859 spin_unlock_irq(&pwq_lock);
Tejun Heo6183c002013-03-12 11:29:57 -07003860 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07003861 }
Tejun Heo6183c002013-03-12 11:29:57 -07003862 }
Tejun Heo794b18b2013-03-13 19:47:40 -07003863 spin_unlock_irq(&pwq_lock);
Tejun Heo6183c002013-03-12 11:29:57 -07003864
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003865 /*
3866 * wq list is used to freeze wq, remove from list after
3867 * flushing is complete in case freeze races us.
3868 */
Tejun Heo5bcab332013-03-13 19:47:40 -07003869 mutex_lock(&wq_mutex);
Tejun Heod2c1d402013-03-12 11:30:04 -07003870 list_del_init(&wq->list);
Tejun Heo5bcab332013-03-13 19:47:40 -07003871 mutex_unlock(&wq_mutex);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003872
Tejun Heo226223a2013-03-12 11:30:05 -07003873 workqueue_sysfs_unregister(wq);
3874
Tejun Heo493008a2013-03-12 11:30:03 -07003875 if (wq->rescuer) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003876 kthread_stop(wq->rescuer->task);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003877 kfree(wq->rescuer);
Tejun Heo493008a2013-03-12 11:30:03 -07003878 wq->rescuer = NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +02003879 }
3880
Tejun Heo8864b4e2013-03-12 11:30:04 -07003881 if (!(wq->flags & WQ_UNBOUND)) {
3882 /*
3883 * The base ref is never dropped on per-cpu pwqs. Directly
3884 * free the pwqs and wq.
3885 */
3886 free_percpu(wq->cpu_pwqs);
3887 kfree(wq);
3888 } else {
3889 /*
3890 * We're the sole accessor of @wq at this point. Directly
3891 * access the first pwq and put the base ref. As both pwqs
3892 * and pools are sched-RCU protected, the lock operations
3893 * are safe. @wq will be freed when the last pwq is
3894 * released.
3895 */
Tejun Heo29c91e92013-03-12 11:30:03 -07003896 pwq = list_first_entry(&wq->pwqs, struct pool_workqueue,
3897 pwqs_node);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003898 spin_lock_irq(&pwq->pool->lock);
3899 put_pwq(pwq);
3900 spin_unlock_irq(&pwq->pool->lock);
Tejun Heo29c91e92013-03-12 11:30:03 -07003901 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003902}
3903EXPORT_SYMBOL_GPL(destroy_workqueue);
3904
Tejun Heodcd989c2010-06-29 10:07:14 +02003905/**
3906 * workqueue_set_max_active - adjust max_active of a workqueue
3907 * @wq: target workqueue
3908 * @max_active: new max_active value.
3909 *
3910 * Set max_active of @wq to @max_active.
3911 *
3912 * CONTEXT:
3913 * Don't call from IRQ context.
3914 */
3915void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3916{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003917 struct pool_workqueue *pwq;
Tejun Heodcd989c2010-06-29 10:07:14 +02003918
Tejun Heo8719dce2013-03-12 11:30:04 -07003919 /* disallow meddling with max_active for ordered workqueues */
3920 if (WARN_ON(wq->flags & __WQ_ORDERED))
3921 return;
3922
Tejun Heof3421792010-07-02 10:03:51 +02003923 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003924
Tejun Heo794b18b2013-03-13 19:47:40 -07003925 spin_lock_irq(&pwq_lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02003926
3927 wq->saved_max_active = max_active;
3928
Tejun Heo699ce092013-03-13 16:51:35 -07003929 for_each_pwq(pwq, wq)
3930 pwq_adjust_max_active(pwq);
Tejun Heodcd989c2010-06-29 10:07:14 +02003931
Tejun Heo794b18b2013-03-13 19:47:40 -07003932 spin_unlock_irq(&pwq_lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02003933}
3934EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3935
3936/**
Tejun Heoe62676162013-03-12 17:41:37 -07003937 * current_is_workqueue_rescuer - is %current workqueue rescuer?
3938 *
3939 * Determine whether %current is a workqueue rescuer. Can be used from
3940 * work functions to determine whether it's being run off the rescuer task.
3941 */
3942bool current_is_workqueue_rescuer(void)
3943{
3944 struct worker *worker = current_wq_worker();
3945
Lai Jiangshan6a092df2013-03-20 03:28:03 +08003946 return worker && worker->rescue_wq;
Tejun Heoe62676162013-03-12 17:41:37 -07003947}
3948
3949/**
Tejun Heodcd989c2010-06-29 10:07:14 +02003950 * workqueue_congested - test whether a workqueue is congested
3951 * @cpu: CPU in question
3952 * @wq: target workqueue
3953 *
3954 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3955 * no synchronization around this function and the test result is
3956 * unreliable and only useful as advisory hints or for debugging.
3957 *
3958 * RETURNS:
3959 * %true if congested, %false otherwise.
3960 */
Tejun Heod84ff052013-03-12 11:29:59 -07003961bool workqueue_congested(int cpu, struct workqueue_struct *wq)
Tejun Heodcd989c2010-06-29 10:07:14 +02003962{
Tejun Heo7fb98ea2013-03-12 11:30:00 -07003963 struct pool_workqueue *pwq;
Tejun Heo76af4d92013-03-12 11:30:00 -07003964 bool ret;
3965
Lai Jiangshan88109452013-03-20 03:28:10 +08003966 rcu_read_lock_sched();
Tejun Heo7fb98ea2013-03-12 11:30:00 -07003967
3968 if (!(wq->flags & WQ_UNBOUND))
3969 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
3970 else
3971 pwq = first_pwq(wq);
Tejun Heodcd989c2010-06-29 10:07:14 +02003972
Tejun Heo76af4d92013-03-12 11:30:00 -07003973 ret = !list_empty(&pwq->delayed_works);
Lai Jiangshan88109452013-03-20 03:28:10 +08003974 rcu_read_unlock_sched();
Tejun Heo76af4d92013-03-12 11:30:00 -07003975
3976 return ret;
Tejun Heodcd989c2010-06-29 10:07:14 +02003977}
3978EXPORT_SYMBOL_GPL(workqueue_congested);
3979
3980/**
Tejun Heodcd989c2010-06-29 10:07:14 +02003981 * work_busy - test whether a work is currently pending or running
3982 * @work: the work to be tested
3983 *
3984 * Test whether @work is currently pending or running. There is no
3985 * synchronization around this function and the test result is
3986 * unreliable and only useful as advisory hints or for debugging.
Tejun Heodcd989c2010-06-29 10:07:14 +02003987 *
3988 * RETURNS:
3989 * OR'd bitmask of WORK_BUSY_* bits.
3990 */
3991unsigned int work_busy(struct work_struct *work)
3992{
Tejun Heofa1b54e2013-03-12 11:30:00 -07003993 struct worker_pool *pool;
Tejun Heodcd989c2010-06-29 10:07:14 +02003994 unsigned long flags;
3995 unsigned int ret = 0;
3996
Tejun Heodcd989c2010-06-29 10:07:14 +02003997 if (work_pending(work))
3998 ret |= WORK_BUSY_PENDING;
Tejun Heodcd989c2010-06-29 10:07:14 +02003999
Tejun Heofa1b54e2013-03-12 11:30:00 -07004000 local_irq_save(flags);
4001 pool = get_work_pool(work);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004002 if (pool) {
Tejun Heofa1b54e2013-03-12 11:30:00 -07004003 spin_lock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004004 if (find_worker_executing_work(pool, work))
4005 ret |= WORK_BUSY_RUNNING;
Tejun Heofa1b54e2013-03-12 11:30:00 -07004006 spin_unlock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004007 }
Tejun Heofa1b54e2013-03-12 11:30:00 -07004008 local_irq_restore(flags);
Tejun Heodcd989c2010-06-29 10:07:14 +02004009
4010 return ret;
4011}
4012EXPORT_SYMBOL_GPL(work_busy);
4013
Tejun Heodb7bccf2010-06-29 10:07:12 +02004014/*
4015 * CPU hotplug.
4016 *
Tejun Heoe22bee72010-06-29 10:07:14 +02004017 * There are two challenges in supporting CPU hotplug. Firstly, there
Tejun Heo112202d2013-02-13 19:29:12 -08004018 * are a lot of assumptions on strong associations among work, pwq and
Tejun Heo706026c2013-01-24 11:01:34 -08004019 * pool which make migrating pending and scheduled works very
Tejun Heoe22bee72010-06-29 10:07:14 +02004020 * difficult to implement without impacting hot paths. Secondly,
Tejun Heo94cf58b2013-01-24 11:01:33 -08004021 * worker pools serve mix of short, long and very long running works making
Tejun Heoe22bee72010-06-29 10:07:14 +02004022 * blocked draining impractical.
4023 *
Tejun Heo24647572013-01-24 11:01:33 -08004024 * This is solved by allowing the pools to be disassociated from the CPU
Tejun Heo628c78e2012-07-17 12:39:27 -07004025 * running as an unbound one and allowing it to be reattached later if the
4026 * cpu comes back online.
Tejun Heodb7bccf2010-06-29 10:07:12 +02004027 */
4028
Tejun Heo706026c2013-01-24 11:01:34 -08004029static void wq_unbind_fn(struct work_struct *work)
Tejun Heodb7bccf2010-06-29 10:07:12 +02004030{
Tejun Heo38db41d2013-01-24 11:01:34 -08004031 int cpu = smp_processor_id();
Tejun Heo4ce62e92012-07-13 22:16:44 -07004032 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004033 struct worker *worker;
Tejun Heoa9ab7752013-03-19 13:45:21 -07004034 int wi;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004035
Tejun Heof02ae732013-03-12 11:30:03 -07004036 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo6183c002013-03-12 11:29:57 -07004037 WARN_ON_ONCE(cpu != smp_processor_id());
Tejun Heo94cf58b2013-01-24 11:01:33 -08004038
Tejun Heobc3a1af2013-03-13 19:47:39 -07004039 mutex_lock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004040 spin_lock_irq(&pool->lock);
4041
4042 /*
Tejun Heobc3a1af2013-03-13 19:47:39 -07004043 * We've blocked all manager operations. Make all workers
Tejun Heo94cf58b2013-01-24 11:01:33 -08004044 * unbound and set DISASSOCIATED. Before this, all workers
4045 * except for the ones which are still executing works from
4046 * before the last CPU down must be on the cpu. After
4047 * this, they may become diasporas.
4048 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07004049 for_each_pool_worker(worker, wi, pool)
Tejun Heoc9e7cf22013-01-24 11:01:33 -08004050 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004051
Tejun Heo24647572013-01-24 11:01:33 -08004052 pool->flags |= POOL_DISASSOCIATED;
Tejun Heof2d5a0e2012-07-17 12:39:26 -07004053
Tejun Heo94cf58b2013-01-24 11:01:33 -08004054 spin_unlock_irq(&pool->lock);
Tejun Heobc3a1af2013-03-13 19:47:39 -07004055 mutex_unlock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004056 }
Tejun Heoe22bee72010-06-29 10:07:14 +02004057
4058 /*
Tejun Heo628c78e2012-07-17 12:39:27 -07004059 * Call schedule() so that we cross rq->lock and thus can guarantee
4060 * sched callbacks see the %WORKER_UNBOUND flag. This is necessary
4061 * as scheduler callbacks may be invoked from other cpus.
4062 */
4063 schedule();
4064
4065 /*
4066 * Sched callbacks are disabled now. Zap nr_running. After this,
4067 * nr_running stays zero and need_more_worker() and keep_working()
Tejun Heo38db41d2013-01-24 11:01:34 -08004068 * are always true as long as the worklist is not empty. Pools on
4069 * @cpu now behave as unbound (in terms of concurrency management)
4070 * pools which are served by workers tied to the CPU.
Tejun Heo628c78e2012-07-17 12:39:27 -07004071 *
4072 * On return from this function, the current worker would trigger
4073 * unbound chain execution of pending work items if other workers
4074 * didn't already.
Tejun Heoe22bee72010-06-29 10:07:14 +02004075 */
Tejun Heof02ae732013-03-12 11:30:03 -07004076 for_each_cpu_worker_pool(pool, cpu)
Tejun Heoe19e3972013-01-24 11:39:44 -08004077 atomic_set(&pool->nr_running, 0);
Tejun Heodb7bccf2010-06-29 10:07:12 +02004078}
4079
Tejun Heobd7c0892013-03-19 13:45:21 -07004080/**
4081 * rebind_workers - rebind all workers of a pool to the associated CPU
4082 * @pool: pool of interest
4083 *
Tejun Heoa9ab7752013-03-19 13:45:21 -07004084 * @pool->cpu is coming online. Rebind all workers to the CPU.
Tejun Heobd7c0892013-03-19 13:45:21 -07004085 */
4086static void rebind_workers(struct worker_pool *pool)
4087{
Tejun Heoa9ab7752013-03-19 13:45:21 -07004088 struct worker *worker;
4089 int wi;
Tejun Heobd7c0892013-03-19 13:45:21 -07004090
4091 lockdep_assert_held(&pool->manager_mutex);
Tejun Heobd7c0892013-03-19 13:45:21 -07004092
Tejun Heoa9ab7752013-03-19 13:45:21 -07004093 /*
4094 * Restore CPU affinity of all workers. As all idle workers should
4095 * be on the run-queue of the associated CPU before any local
4096 * wake-ups for concurrency management happen, restore CPU affinty
4097 * of all workers first and then clear UNBOUND. As we're called
4098 * from CPU_ONLINE, the following shouldn't fail.
4099 */
4100 for_each_pool_worker(worker, wi, pool)
4101 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
4102 pool->attrs->cpumask) < 0);
4103
4104 spin_lock_irq(&pool->lock);
4105
4106 for_each_pool_worker(worker, wi, pool) {
4107 unsigned int worker_flags = worker->flags;
Tejun Heobd7c0892013-03-19 13:45:21 -07004108
4109 /*
Tejun Heoa9ab7752013-03-19 13:45:21 -07004110 * A bound idle worker should actually be on the runqueue
4111 * of the associated CPU for local wake-ups targeting it to
4112 * work. Kick all idle workers so that they migrate to the
4113 * associated CPU. Doing this in the same loop as
4114 * replacing UNBOUND with REBOUND is safe as no worker will
4115 * be bound before @pool->lock is released.
Tejun Heobd7c0892013-03-19 13:45:21 -07004116 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07004117 if (worker_flags & WORKER_IDLE)
4118 wake_up_process(worker->task);
4119
4120 /*
4121 * We want to clear UNBOUND but can't directly call
4122 * worker_clr_flags() or adjust nr_running. Atomically
4123 * replace UNBOUND with another NOT_RUNNING flag REBOUND.
4124 * @worker will clear REBOUND using worker_clr_flags() when
4125 * it initiates the next execution cycle thus restoring
4126 * concurrency management. Note that when or whether
4127 * @worker clears REBOUND doesn't affect correctness.
4128 *
4129 * ACCESS_ONCE() is necessary because @worker->flags may be
4130 * tested without holding any lock in
4131 * wq_worker_waking_up(). Without it, NOT_RUNNING test may
4132 * fail incorrectly leading to premature concurrency
4133 * management operations.
4134 */
4135 WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND));
4136 worker_flags |= WORKER_REBOUND;
4137 worker_flags &= ~WORKER_UNBOUND;
4138 ACCESS_ONCE(worker->flags) = worker_flags;
Tejun Heobd7c0892013-03-19 13:45:21 -07004139 }
4140
Tejun Heoa9ab7752013-03-19 13:45:21 -07004141 spin_unlock_irq(&pool->lock);
Tejun Heobd7c0892013-03-19 13:45:21 -07004142}
4143
Tejun Heo7dbc7252013-03-19 13:45:21 -07004144/**
4145 * restore_unbound_workers_cpumask - restore cpumask of unbound workers
4146 * @pool: unbound pool of interest
4147 * @cpu: the CPU which is coming up
4148 *
4149 * An unbound pool may end up with a cpumask which doesn't have any online
4150 * CPUs. When a worker of such pool get scheduled, the scheduler resets
4151 * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any
4152 * online CPU before, cpus_allowed of all its workers should be restored.
4153 */
4154static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu)
4155{
4156 static cpumask_t cpumask;
4157 struct worker *worker;
4158 int wi;
4159
4160 lockdep_assert_held(&pool->manager_mutex);
4161
4162 /* is @cpu allowed for @pool? */
4163 if (!cpumask_test_cpu(cpu, pool->attrs->cpumask))
4164 return;
4165
4166 /* is @cpu the only online CPU? */
4167 cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask);
4168 if (cpumask_weight(&cpumask) != 1)
4169 return;
4170
4171 /* as we're called from CPU_ONLINE, the following shouldn't fail */
4172 for_each_pool_worker(worker, wi, pool)
4173 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
4174 pool->attrs->cpumask) < 0);
4175}
4176
Tejun Heo8db25e72012-07-17 12:39:28 -07004177/*
4178 * Workqueues should be brought up before normal priority CPU notifiers.
4179 * This will be registered high priority CPU notifier.
4180 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07004181static int __cpuinit workqueue_cpu_up_callback(struct notifier_block *nfb,
Tejun Heo8db25e72012-07-17 12:39:28 -07004182 unsigned long action,
4183 void *hcpu)
Oleg Nesterov3af244332007-05-09 02:34:09 -07004184{
Tejun Heod84ff052013-03-12 11:29:59 -07004185 int cpu = (unsigned long)hcpu;
Tejun Heo4ce62e92012-07-13 22:16:44 -07004186 struct worker_pool *pool;
Tejun Heo7dbc7252013-03-19 13:45:21 -07004187 int pi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188
Tejun Heo8db25e72012-07-17 12:39:28 -07004189 switch (action & ~CPU_TASKS_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 case CPU_UP_PREPARE:
Tejun Heof02ae732013-03-12 11:30:03 -07004191 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo3ce63372012-07-17 12:39:27 -07004192 if (pool->nr_workers)
4193 continue;
Tejun Heoebf44d12013-03-13 19:47:39 -07004194 if (create_and_start_worker(pool) < 0)
Tejun Heo3ce63372012-07-17 12:39:27 -07004195 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02004197 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07004198
Tejun Heo65758202012-07-17 12:39:26 -07004199 case CPU_DOWN_FAILED:
4200 case CPU_ONLINE:
Tejun Heo7dbc7252013-03-19 13:45:21 -07004201 mutex_lock(&wq_mutex);
4202
4203 for_each_pool(pool, pi) {
Tejun Heobc3a1af2013-03-13 19:47:39 -07004204 mutex_lock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004205
Tejun Heo7dbc7252013-03-19 13:45:21 -07004206 if (pool->cpu == cpu) {
4207 spin_lock_irq(&pool->lock);
4208 pool->flags &= ~POOL_DISASSOCIATED;
4209 spin_unlock_irq(&pool->lock);
Tejun Heoa9ab7752013-03-19 13:45:21 -07004210
Tejun Heo7dbc7252013-03-19 13:45:21 -07004211 rebind_workers(pool);
4212 } else if (pool->cpu < 0) {
4213 restore_unbound_workers_cpumask(pool, cpu);
4214 }
Tejun Heo94cf58b2013-01-24 11:01:33 -08004215
Tejun Heobc3a1af2013-03-13 19:47:39 -07004216 mutex_unlock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004217 }
Tejun Heo7dbc7252013-03-19 13:45:21 -07004218
4219 mutex_unlock(&wq_mutex);
Tejun Heo8db25e72012-07-17 12:39:28 -07004220 break;
Tejun Heo65758202012-07-17 12:39:26 -07004221 }
4222 return NOTIFY_OK;
4223}
4224
4225/*
4226 * Workqueues should be brought down after normal priority CPU notifiers.
4227 * This will be registered as low priority CPU notifier.
4228 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07004229static int __cpuinit workqueue_cpu_down_callback(struct notifier_block *nfb,
Tejun Heo65758202012-07-17 12:39:26 -07004230 unsigned long action,
4231 void *hcpu)
4232{
Tejun Heod84ff052013-03-12 11:29:59 -07004233 int cpu = (unsigned long)hcpu;
Tejun Heo8db25e72012-07-17 12:39:28 -07004234 struct work_struct unbind_work;
4235
Tejun Heo65758202012-07-17 12:39:26 -07004236 switch (action & ~CPU_TASKS_FROZEN) {
4237 case CPU_DOWN_PREPARE:
Tejun Heo8db25e72012-07-17 12:39:28 -07004238 /* unbinding should happen on the local CPU */
Tejun Heo706026c2013-01-24 11:01:34 -08004239 INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn);
Joonsoo Kim7635d2f2012-08-15 23:25:41 +09004240 queue_work_on(cpu, system_highpri_wq, &unbind_work);
Tejun Heo8db25e72012-07-17 12:39:28 -07004241 flush_work(&unbind_work);
4242 break;
Tejun Heo65758202012-07-17 12:39:26 -07004243 }
4244 return NOTIFY_OK;
4245}
4246
Rusty Russell2d3854a2008-11-05 13:39:10 +11004247#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08004248
Rusty Russell2d3854a2008-11-05 13:39:10 +11004249struct work_for_cpu {
Tejun Heoed48ece2012-09-18 12:48:43 -07004250 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11004251 long (*fn)(void *);
4252 void *arg;
4253 long ret;
4254};
4255
Tejun Heoed48ece2012-09-18 12:48:43 -07004256static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11004257{
Tejun Heoed48ece2012-09-18 12:48:43 -07004258 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
4259
Rusty Russell2d3854a2008-11-05 13:39:10 +11004260 wfc->ret = wfc->fn(wfc->arg);
4261}
4262
4263/**
4264 * work_on_cpu - run a function in user context on a particular cpu
4265 * @cpu: the cpu to run on
4266 * @fn: the function to run
4267 * @arg: the function arg
4268 *
Rusty Russell31ad9082009-01-16 15:31:15 -08004269 * This will return the value @fn returns.
4270 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b44003e2009-04-09 09:50:37 -06004271 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11004272 */
Tejun Heod84ff052013-03-12 11:29:59 -07004273long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
Rusty Russell2d3854a2008-11-05 13:39:10 +11004274{
Tejun Heoed48ece2012-09-18 12:48:43 -07004275 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11004276
Tejun Heoed48ece2012-09-18 12:48:43 -07004277 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
4278 schedule_work_on(cpu, &wfc.work);
4279 flush_work(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11004280 return wfc.ret;
4281}
4282EXPORT_SYMBOL_GPL(work_on_cpu);
4283#endif /* CONFIG_SMP */
4284
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004285#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10304286
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004287/**
4288 * freeze_workqueues_begin - begin freezing workqueues
4289 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01004290 * Start freezing workqueues. After this function returns, all freezable
Tejun Heoc5aa87b2013-03-13 16:51:36 -07004291 * workqueues will queue new works to their delayed_works list instead of
Tejun Heo706026c2013-01-24 11:01:34 -08004292 * pool->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004293 *
4294 * CONTEXT:
Tejun Heo794b18b2013-03-13 19:47:40 -07004295 * Grabs and releases wq_mutex, pwq_lock and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004296 */
4297void freeze_workqueues_begin(void)
4298{
Tejun Heo17116962013-03-12 11:29:58 -07004299 struct worker_pool *pool;
Tejun Heo24b8a842013-03-12 11:29:58 -07004300 struct workqueue_struct *wq;
4301 struct pool_workqueue *pwq;
Tejun Heo611c92a2013-03-13 16:51:36 -07004302 int pi;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004303
Tejun Heo5bcab332013-03-13 19:47:40 -07004304 mutex_lock(&wq_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004305
Tejun Heo6183c002013-03-12 11:29:57 -07004306 WARN_ON_ONCE(workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004307 workqueue_freezing = true;
4308
Tejun Heo24b8a842013-03-12 11:29:58 -07004309 /* set FREEZING */
Tejun Heo611c92a2013-03-13 16:51:36 -07004310 for_each_pool(pool, pi) {
Tejun Heo5bcab332013-03-13 19:47:40 -07004311 spin_lock_irq(&pool->lock);
Tejun Heo17116962013-03-12 11:29:58 -07004312 WARN_ON_ONCE(pool->flags & POOL_FREEZING);
4313 pool->flags |= POOL_FREEZING;
Tejun Heo5bcab332013-03-13 19:47:40 -07004314 spin_unlock_irq(&pool->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004315 }
4316
Tejun Heo24b8a842013-03-12 11:29:58 -07004317 /* suppress further executions by setting max_active to zero */
Tejun Heo794b18b2013-03-13 19:47:40 -07004318 spin_lock_irq(&pwq_lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004319 list_for_each_entry(wq, &workqueues, list) {
Tejun Heo699ce092013-03-13 16:51:35 -07004320 for_each_pwq(pwq, wq)
4321 pwq_adjust_max_active(pwq);
Tejun Heo24b8a842013-03-12 11:29:58 -07004322 }
Tejun Heo794b18b2013-03-13 19:47:40 -07004323 spin_unlock_irq(&pwq_lock);
Tejun Heo5bcab332013-03-13 19:47:40 -07004324
4325 mutex_unlock(&wq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004327
4328/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01004329 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004330 *
4331 * Check whether freezing is complete. This function must be called
4332 * between freeze_workqueues_begin() and thaw_workqueues().
4333 *
4334 * CONTEXT:
Tejun Heo5bcab332013-03-13 19:47:40 -07004335 * Grabs and releases wq_mutex.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004336 *
4337 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01004338 * %true if some freezable workqueues are still busy. %false if freezing
4339 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004340 */
4341bool freeze_workqueues_busy(void)
4342{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004343 bool busy = false;
Tejun Heo24b8a842013-03-12 11:29:58 -07004344 struct workqueue_struct *wq;
4345 struct pool_workqueue *pwq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004346
Tejun Heo5bcab332013-03-13 19:47:40 -07004347 mutex_lock(&wq_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004348
Tejun Heo6183c002013-03-12 11:29:57 -07004349 WARN_ON_ONCE(!workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004350
Tejun Heo24b8a842013-03-12 11:29:58 -07004351 list_for_each_entry(wq, &workqueues, list) {
4352 if (!(wq->flags & WQ_FREEZABLE))
4353 continue;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004354 /*
4355 * nr_active is monotonically decreasing. It's safe
4356 * to peek without lock.
4357 */
Lai Jiangshan88109452013-03-20 03:28:10 +08004358 rcu_read_lock_sched();
Tejun Heo24b8a842013-03-12 11:29:58 -07004359 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07004360 WARN_ON_ONCE(pwq->nr_active < 0);
Tejun Heo112202d2013-02-13 19:29:12 -08004361 if (pwq->nr_active) {
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004362 busy = true;
Lai Jiangshan88109452013-03-20 03:28:10 +08004363 rcu_read_unlock_sched();
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004364 goto out_unlock;
4365 }
4366 }
Lai Jiangshan88109452013-03-20 03:28:10 +08004367 rcu_read_unlock_sched();
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004368 }
4369out_unlock:
Tejun Heo5bcab332013-03-13 19:47:40 -07004370 mutex_unlock(&wq_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004371 return busy;
4372}
4373
4374/**
4375 * thaw_workqueues - thaw workqueues
4376 *
4377 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo706026c2013-01-24 11:01:34 -08004378 * frozen works are transferred to their respective pool worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004379 *
4380 * CONTEXT:
Tejun Heo794b18b2013-03-13 19:47:40 -07004381 * Grabs and releases wq_mutex, pwq_lock and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004382 */
4383void thaw_workqueues(void)
4384{
Tejun Heo24b8a842013-03-12 11:29:58 -07004385 struct workqueue_struct *wq;
4386 struct pool_workqueue *pwq;
4387 struct worker_pool *pool;
Tejun Heo611c92a2013-03-13 16:51:36 -07004388 int pi;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004389
Tejun Heo5bcab332013-03-13 19:47:40 -07004390 mutex_lock(&wq_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004391
4392 if (!workqueue_freezing)
4393 goto out_unlock;
4394
Tejun Heo24b8a842013-03-12 11:29:58 -07004395 /* clear FREEZING */
Tejun Heo611c92a2013-03-13 16:51:36 -07004396 for_each_pool(pool, pi) {
Tejun Heo5bcab332013-03-13 19:47:40 -07004397 spin_lock_irq(&pool->lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004398 WARN_ON_ONCE(!(pool->flags & POOL_FREEZING));
4399 pool->flags &= ~POOL_FREEZING;
Tejun Heo5bcab332013-03-13 19:47:40 -07004400 spin_unlock_irq(&pool->lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004401 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02004402
Tejun Heo24b8a842013-03-12 11:29:58 -07004403 /* restore max_active and repopulate worklist */
Tejun Heo794b18b2013-03-13 19:47:40 -07004404 spin_lock_irq(&pwq_lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004405 list_for_each_entry(wq, &workqueues, list) {
Tejun Heo699ce092013-03-13 16:51:35 -07004406 for_each_pwq(pwq, wq)
4407 pwq_adjust_max_active(pwq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004408 }
Tejun Heo794b18b2013-03-13 19:47:40 -07004409 spin_unlock_irq(&pwq_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004410
4411 workqueue_freezing = false;
4412out_unlock:
Tejun Heo5bcab332013-03-13 19:47:40 -07004413 mutex_unlock(&wq_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004414}
4415#endif /* CONFIG_FREEZER */
4416
Suresh Siddha6ee05782010-07-30 14:57:37 -07004417static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418{
Tejun Heo7a4e3442013-03-12 11:30:00 -07004419 int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL };
4420 int i, cpu;
Tejun Heoc34056a2010-06-29 10:07:11 +02004421
Tejun Heo7c3eed52013-01-24 11:01:33 -08004422 /* make sure we have enough bits for OFFQ pool ID */
4423 BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) <
Lai Jiangshan6be19582013-02-06 18:04:53 -08004424 WORK_CPU_END * NR_STD_WORKER_POOLS);
Tejun Heob5490072012-08-03 10:30:46 -07004425
Tejun Heoe904e6c2013-03-12 11:29:57 -07004426 WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long));
4427
4428 pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC);
4429
Tejun Heo65758202012-07-17 12:39:26 -07004430 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
Lai Jiangshana5b4e572012-09-18 09:59:23 -07004431 hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02004432
Tejun Heo706026c2013-01-24 11:01:34 -08004433 /* initialize CPU pools */
Tejun Heo29c91e92013-03-12 11:30:03 -07004434 for_each_possible_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07004435 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02004436
Tejun Heo7a4e3442013-03-12 11:30:00 -07004437 i = 0;
Tejun Heof02ae732013-03-12 11:30:03 -07004438 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo7a4e3442013-03-12 11:30:00 -07004439 BUG_ON(init_worker_pool(pool));
Tejun Heoec22ca52013-01-24 11:01:33 -08004440 pool->cpu = cpu;
Tejun Heo29c91e92013-03-12 11:30:03 -07004441 cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu));
Tejun Heo7a4e3442013-03-12 11:30:00 -07004442 pool->attrs->nice = std_nice[i++];
4443
Tejun Heo9daf9e62013-01-24 11:01:33 -08004444 /* alloc pool ID */
Tejun Heo5bcab332013-03-13 19:47:40 -07004445 mutex_lock(&wq_mutex);
Tejun Heo9daf9e62013-01-24 11:01:33 -08004446 BUG_ON(worker_pool_assign_id(pool));
Tejun Heo5bcab332013-03-13 19:47:40 -07004447 mutex_unlock(&wq_mutex);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004448 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02004449 }
4450
Tejun Heoe22bee72010-06-29 10:07:14 +02004451 /* create the initial worker */
Tejun Heo29c91e92013-03-12 11:30:03 -07004452 for_each_online_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07004453 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02004454
Tejun Heof02ae732013-03-12 11:30:03 -07004455 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo29c91e92013-03-12 11:30:03 -07004456 pool->flags &= ~POOL_DISASSOCIATED;
Tejun Heoebf44d12013-03-13 19:47:39 -07004457 BUG_ON(create_and_start_worker(pool) < 0);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004458 }
Tejun Heoe22bee72010-06-29 10:07:14 +02004459 }
4460
Tejun Heo29c91e92013-03-12 11:30:03 -07004461 /* create default unbound wq attrs */
4462 for (i = 0; i < NR_STD_WORKER_POOLS; i++) {
4463 struct workqueue_attrs *attrs;
4464
4465 BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL)));
4466
4467 attrs->nice = std_nice[i];
4468 cpumask_setall(attrs->cpumask);
4469
4470 unbound_std_wq_attrs[i] = attrs;
4471 }
4472
Tejun Heod320c032010-06-29 10:07:14 +02004473 system_wq = alloc_workqueue("events", 0, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09004474 system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
Tejun Heod320c032010-06-29 10:07:14 +02004475 system_long_wq = alloc_workqueue("events_long", 0, 0);
Tejun Heof3421792010-07-02 10:03:51 +02004476 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
4477 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01004478 system_freezable_wq = alloc_workqueue("events_freezable",
4479 WQ_FREEZABLE, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09004480 BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
Tejun Heoae930e02012-08-20 14:51:23 -07004481 !system_unbound_wq || !system_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07004482 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483}
Suresh Siddha6ee05782010-07-30 14:57:37 -07004484early_initcall(init_workqueues);