blob: 40f4017285a0a3dad5abeb14537dad1cd98d5d45 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tejun Heoc54fce62010-09-10 16:51:36 +02002 * kernel/workqueue.c - generic async execution with shared worker pool
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Tejun Heoc54fce62010-09-10 16:51:36 +02004 * Copyright (C) 2002 Ingo Molnar
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Tejun Heoc54fce62010-09-10 16:51:36 +02006 * Derived from the taskqueue/keventd code by:
7 * David Woodhouse <dwmw2@infradead.org>
8 * Andrew Morton
9 * Kai Petzke <wpp@marie.physik.tu-berlin.de>
10 * Theodore Ts'o <tytso@mit.edu>
Christoph Lameter89ada672005-10-30 15:01:59 -080011 *
Christoph Lametercde53532008-07-04 09:59:22 -070012 * Made to use alloc_percpu by Christoph Lameter.
Tejun Heoc54fce62010-09-10 16:51:36 +020013 *
14 * Copyright (C) 2010 SUSE Linux Products GmbH
15 * Copyright (C) 2010 Tejun Heo <tj@kernel.org>
16 *
17 * This is the generic async execution mechanism. Work items as are
18 * executed in process context. The worker pool is shared and
19 * automatically managed. There is one worker pool for each CPU and
20 * one extra for works which are better served by workers which are
21 * not bound to any specific CPU.
22 *
23 * Please read Documentation/workqueue.txt for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 */
25
Paul Gortmaker9984de12011-05-23 14:51:41 -040026#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/kernel.h>
28#include <linux/sched.h>
29#include <linux/init.h>
30#include <linux/signal.h>
31#include <linux/completion.h>
32#include <linux/workqueue.h>
33#include <linux/slab.h>
34#include <linux/cpu.h>
35#include <linux/notifier.h>
36#include <linux/kthread.h>
James Bottomley1fa44ec2006-02-23 12:43:43 -060037#include <linux/hardirq.h>
Christoph Lameter46934022006-10-11 01:21:26 -070038#include <linux/mempolicy.h>
Rafael J. Wysocki341a5952006-12-06 20:34:49 -080039#include <linux/freezer.h>
Peter Zijlstrad5abe662006-12-06 20:37:26 -080040#include <linux/kallsyms.h>
41#include <linux/debug_locks.h>
Johannes Berg4e6045f2007-10-18 23:39:55 -070042#include <linux/lockdep.h>
Tejun Heoc34056a2010-06-29 10:07:11 +020043#include <linux/idr.h>
Tejun Heo29c91e92013-03-12 11:30:03 -070044#include <linux/jhash.h>
Sasha Levin42f85702012-12-17 10:01:23 -050045#include <linux/hashtable.h>
Tejun Heo76af4d92013-03-12 11:30:00 -070046#include <linux/rculist.h>
Tejun Heoe22bee72010-06-29 10:07:14 +020047
Tejun Heoea138442013-01-18 14:05:55 -080048#include "workqueue_internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Tejun Heoc8e55f32010-06-29 10:07:12 +020050enum {
Tejun Heobc2ae0f2012-07-17 12:39:27 -070051 /*
Tejun Heo24647572013-01-24 11:01:33 -080052 * worker_pool flags
Tejun Heobc2ae0f2012-07-17 12:39:27 -070053 *
Tejun Heo24647572013-01-24 11:01:33 -080054 * A bound pool is either associated or disassociated with its CPU.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070055 * While associated (!DISASSOCIATED), all workers are bound to the
56 * CPU and none has %WORKER_UNBOUND set and concurrency management
57 * is in effect.
58 *
59 * While DISASSOCIATED, the cpu may be offline and all workers have
60 * %WORKER_UNBOUND set and concurrency management disabled, and may
Tejun Heo24647572013-01-24 11:01:33 -080061 * be executing on any CPU. The pool behaves as an unbound one.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070062 *
Tejun Heobc3a1af2013-03-13 19:47:39 -070063 * Note that DISASSOCIATED should be flipped only while holding
64 * manager_mutex to avoid changing binding state while
Tejun Heo24647572013-01-24 11:01:33 -080065 * create_worker() is in progress.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070066 */
Tejun Heo11ebea52012-07-12 14:46:37 -070067 POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
Tejun Heo24647572013-01-24 11:01:33 -080068 POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */
Tejun Heo35b6bb62013-01-24 11:01:33 -080069 POOL_FREEZING = 1 << 3, /* freeze in progress */
Tejun Heodb7bccf2010-06-29 10:07:12 +020070
Tejun Heoc8e55f32010-06-29 10:07:12 +020071 /* worker flags */
72 WORKER_STARTED = 1 << 0, /* started */
73 WORKER_DIE = 1 << 1, /* die die die */
74 WORKER_IDLE = 1 << 2, /* is idle */
Tejun Heoe22bee72010-06-29 10:07:14 +020075 WORKER_PREP = 1 << 3, /* preparing to run works */
Tejun Heofb0e7be2010-06-29 10:07:15 +020076 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
Tejun Heof3421792010-07-02 10:03:51 +020077 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
Tejun Heoa9ab7752013-03-19 13:45:21 -070078 WORKER_REBOUND = 1 << 8, /* worker was rebound */
Tejun Heoe22bee72010-06-29 10:07:14 +020079
Tejun Heoa9ab7752013-03-19 13:45:21 -070080 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_CPU_INTENSIVE |
81 WORKER_UNBOUND | WORKER_REBOUND,
Tejun Heodb7bccf2010-06-29 10:07:12 +020082
Tejun Heoe34cdddb2013-01-24 11:01:33 -080083 NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */
Tejun Heo4ce62e92012-07-13 22:16:44 -070084
Tejun Heo29c91e92013-03-12 11:30:03 -070085 UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */
Tejun Heoc8e55f32010-06-29 10:07:12 +020086 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
Tejun Heodb7bccf2010-06-29 10:07:12 +020087
Tejun Heoe22bee72010-06-29 10:07:14 +020088 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
89 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
90
Tejun Heo3233cdb2011-02-16 18:10:19 +010091 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
92 /* call for help after 10ms
93 (min two ticks) */
Tejun Heoe22bee72010-06-29 10:07:14 +020094 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
95 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heoe22bee72010-06-29 10:07:14 +020096
97 /*
98 * Rescue workers are used only on emergencies and shared by
99 * all cpus. Give -20.
100 */
101 RESCUER_NICE_LEVEL = -20,
Tejun Heo32704762012-07-13 22:16:45 -0700102 HIGHPRI_NICE_LEVEL = -20,
Tejun Heoc8e55f32010-06-29 10:07:12 +0200103};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105/*
Tejun Heo4690c4a2010-06-29 10:07:10 +0200106 * Structure fields follow one of the following exclusion rules.
107 *
Tejun Heoe41e7042010-08-24 14:22:47 +0200108 * I: Modifiable by initialization/destruction paths and read-only for
109 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200110 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200111 * P: Preemption protected. Disabling preemption is enough and should
112 * only be modified and accessed from the local cpu.
113 *
Tejun Heod565ed62013-01-24 11:01:33 -0800114 * L: pool->lock protected. Access with pool->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200115 *
Tejun Heod565ed62013-01-24 11:01:33 -0800116 * X: During normal operation, modification requires pool->lock and should
117 * be done only from local cpu. Either disabling preemption on local
118 * cpu or grabbing pool->lock is enough for read access. If
119 * POOL_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200120 *
Tejun 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) \
308 WARN_ONCE(!lockdep_is_held(&(pool)->manager_mutex) && \
309 !lockdep_is_held(&(pool)->lock), \
310 "pool->manager_mutex or ->lock should be held")
311#else
312#define assert_manager_or_pool_lock(pool) do { } while (0)
313#endif
314
Tejun Heof02ae732013-03-12 11:30:03 -0700315#define for_each_cpu_worker_pool(pool, cpu) \
316 for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \
317 (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \
Tejun Heo7a62c2c2013-03-12 11:30:03 -0700318 (pool)++)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700319
Tejun Heo49e3cf42013-03-12 11:29:58 -0700320/**
Tejun Heo17116962013-03-12 11:29:58 -0700321 * for_each_pool - iterate through all worker_pools in the system
322 * @pool: iteration cursor
Tejun Heo611c92a2013-03-13 16:51:36 -0700323 * @pi: integer used for iteration
Tejun Heofa1b54e2013-03-12 11:30:00 -0700324 *
Tejun Heo5bcab332013-03-13 19:47:40 -0700325 * This must be called either with wq_mutex held or sched RCU read locked.
326 * If the pool needs to be used beyond the locking in effect, the caller is
327 * responsible for guaranteeing that the pool stays online.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700328 *
329 * The if/else clause exists only for the lockdep assertion and can be
330 * ignored.
Tejun Heo17116962013-03-12 11:29:58 -0700331 */
Tejun Heo611c92a2013-03-13 16:51:36 -0700332#define for_each_pool(pool, pi) \
333 idr_for_each_entry(&worker_pool_idr, pool, pi) \
Tejun Heo5bcab332013-03-13 19:47:40 -0700334 if (({ assert_rcu_or_wq_mutex(); false; })) { } \
Tejun Heofa1b54e2013-03-12 11:30:00 -0700335 else
Tejun Heo17116962013-03-12 11:29:58 -0700336
337/**
Tejun Heo822d8402013-03-19 13:45:21 -0700338 * for_each_pool_worker - iterate through all workers of a worker_pool
339 * @worker: iteration cursor
340 * @wi: integer used for iteration
341 * @pool: worker_pool to iterate workers of
342 *
343 * This must be called with either @pool->manager_mutex or ->lock held.
344 *
345 * The if/else clause exists only for the lockdep assertion and can be
346 * ignored.
347 */
348#define for_each_pool_worker(worker, wi, pool) \
349 idr_for_each_entry(&(pool)->worker_idr, (worker), (wi)) \
350 if (({ assert_manager_or_pool_lock((pool)); false; })) { } \
351 else
352
353/**
Tejun Heo49e3cf42013-03-12 11:29:58 -0700354 * for_each_pwq - iterate through all pool_workqueues of the specified workqueue
355 * @pwq: iteration cursor
356 * @wq: the target workqueue
Tejun Heo76af4d92013-03-12 11:30:00 -0700357 *
Tejun Heo794b18b2013-03-13 19:47:40 -0700358 * This must be called either with pwq_lock held or sched RCU read locked.
359 * If the pwq needs to be used beyond the locking in effect, the caller is
360 * responsible for guaranteeing that the pwq stays online.
Tejun Heo76af4d92013-03-12 11:30:00 -0700361 *
362 * The if/else clause exists only for the lockdep assertion and can be
363 * ignored.
Tejun Heo49e3cf42013-03-12 11:29:58 -0700364 */
365#define for_each_pwq(pwq, wq) \
Tejun Heo76af4d92013-03-12 11:30:00 -0700366 list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node) \
Tejun Heo794b18b2013-03-13 19:47:40 -0700367 if (({ assert_rcu_or_pwq_lock(); false; })) { } \
Tejun Heo76af4d92013-03-12 11:30:00 -0700368 else
Tejun Heof3421792010-07-02 10:03:51 +0200369
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900370#ifdef CONFIG_DEBUG_OBJECTS_WORK
371
372static struct debug_obj_descr work_debug_descr;
373
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100374static void *work_debug_hint(void *addr)
375{
376 return ((struct work_struct *) addr)->func;
377}
378
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900379/*
380 * fixup_init is called when:
381 * - an active object is initialized
382 */
383static int work_fixup_init(void *addr, enum debug_obj_state state)
384{
385 struct work_struct *work = addr;
386
387 switch (state) {
388 case ODEBUG_STATE_ACTIVE:
389 cancel_work_sync(work);
390 debug_object_init(work, &work_debug_descr);
391 return 1;
392 default:
393 return 0;
394 }
395}
396
397/*
398 * fixup_activate is called when:
399 * - an active object is activated
400 * - an unknown object is activated (might be a statically initialized object)
401 */
402static int work_fixup_activate(void *addr, enum debug_obj_state state)
403{
404 struct work_struct *work = addr;
405
406 switch (state) {
407
408 case ODEBUG_STATE_NOTAVAILABLE:
409 /*
410 * This is not really a fixup. The work struct was
411 * statically initialized. We just make sure that it
412 * is tracked in the object tracker.
413 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200414 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900415 debug_object_init(work, &work_debug_descr);
416 debug_object_activate(work, &work_debug_descr);
417 return 0;
418 }
419 WARN_ON_ONCE(1);
420 return 0;
421
422 case ODEBUG_STATE_ACTIVE:
423 WARN_ON(1);
424
425 default:
426 return 0;
427 }
428}
429
430/*
431 * fixup_free is called when:
432 * - an active object is freed
433 */
434static int work_fixup_free(void *addr, enum debug_obj_state state)
435{
436 struct work_struct *work = addr;
437
438 switch (state) {
439 case ODEBUG_STATE_ACTIVE:
440 cancel_work_sync(work);
441 debug_object_free(work, &work_debug_descr);
442 return 1;
443 default:
444 return 0;
445 }
446}
447
448static struct debug_obj_descr work_debug_descr = {
449 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100450 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900451 .fixup_init = work_fixup_init,
452 .fixup_activate = work_fixup_activate,
453 .fixup_free = work_fixup_free,
454};
455
456static inline void debug_work_activate(struct work_struct *work)
457{
458 debug_object_activate(work, &work_debug_descr);
459}
460
461static inline void debug_work_deactivate(struct work_struct *work)
462{
463 debug_object_deactivate(work, &work_debug_descr);
464}
465
466void __init_work(struct work_struct *work, int onstack)
467{
468 if (onstack)
469 debug_object_init_on_stack(work, &work_debug_descr);
470 else
471 debug_object_init(work, &work_debug_descr);
472}
473EXPORT_SYMBOL_GPL(__init_work);
474
475void destroy_work_on_stack(struct work_struct *work)
476{
477 debug_object_free(work, &work_debug_descr);
478}
479EXPORT_SYMBOL_GPL(destroy_work_on_stack);
480
481#else
482static inline void debug_work_activate(struct work_struct *work) { }
483static inline void debug_work_deactivate(struct work_struct *work) { }
484#endif
485
Tejun Heo9daf9e62013-01-24 11:01:33 -0800486/* allocate ID and assign it to @pool */
487static int worker_pool_assign_id(struct worker_pool *pool)
488{
489 int ret;
490
Tejun Heo5bcab332013-03-13 19:47:40 -0700491 lockdep_assert_held(&wq_mutex);
492
Tejun Heofa1b54e2013-03-12 11:30:00 -0700493 do {
494 if (!idr_pre_get(&worker_pool_idr, GFP_KERNEL))
495 return -ENOMEM;
Tejun Heofa1b54e2013-03-12 11:30:00 -0700496 ret = idr_get_new(&worker_pool_idr, pool, &pool->id);
Tejun Heofa1b54e2013-03-12 11:30:00 -0700497 } while (ret == -EAGAIN);
Tejun Heo9daf9e62013-01-24 11:01:33 -0800498
499 return ret;
500}
501
Tejun Heo76af4d92013-03-12 11:30:00 -0700502/**
503 * first_pwq - return the first pool_workqueue of the specified workqueue
504 * @wq: the target workqueue
505 *
Tejun Heo794b18b2013-03-13 19:47:40 -0700506 * This must be called either with pwq_lock held or sched RCU read locked.
507 * If the pwq needs to be used beyond the locking in effect, the caller is
508 * responsible for guaranteeing that the pwq stays online.
Tejun Heo76af4d92013-03-12 11:30:00 -0700509 */
Tejun Heo7fb98ea2013-03-12 11:30:00 -0700510static struct pool_workqueue *first_pwq(struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700511{
Tejun Heo794b18b2013-03-13 19:47:40 -0700512 assert_rcu_or_pwq_lock();
Tejun Heo76af4d92013-03-12 11:30:00 -0700513 return list_first_or_null_rcu(&wq->pwqs, struct pool_workqueue,
514 pwqs_node);
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700515}
516
Tejun Heo73f53c42010-06-29 10:07:11 +0200517static unsigned int work_color_to_flags(int color)
518{
519 return color << WORK_STRUCT_COLOR_SHIFT;
520}
521
522static int get_work_color(struct work_struct *work)
523{
524 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
525 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
526}
527
528static int work_next_color(int color)
529{
530 return (color + 1) % WORK_NR_COLORS;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700531}
532
David Howells4594bf12006-12-07 11:33:26 +0000533/*
Tejun Heo112202d2013-02-13 19:29:12 -0800534 * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data
535 * contain the pointer to the queued pwq. Once execution starts, the flag
Tejun Heo7c3eed52013-01-24 11:01:33 -0800536 * is cleared and the high bits contain OFFQ flags and pool ID.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200537 *
Tejun Heo112202d2013-02-13 19:29:12 -0800538 * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling()
539 * and clear_work_data() can be used to set the pwq, pool or clear
Tejun Heobbb68df2012-08-03 10:30:46 -0700540 * work->data. These functions should only be called while the work is
541 * owned - ie. while the PENDING bit is set.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200542 *
Tejun Heo112202d2013-02-13 19:29:12 -0800543 * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq
Tejun Heo7c3eed52013-01-24 11:01:33 -0800544 * corresponding to a work. Pool is available once the work has been
Tejun Heo112202d2013-02-13 19:29:12 -0800545 * queued anywhere after initialization until it is sync canceled. pwq is
Tejun Heo7c3eed52013-01-24 11:01:33 -0800546 * available only while the work item is queued.
Tejun Heobbb68df2012-08-03 10:30:46 -0700547 *
548 * %WORK_OFFQ_CANCELING is used to mark a work item which is being
549 * canceled. While being canceled, a work item may have its PENDING set
550 * but stay off timer and worklist for arbitrarily long and nobody should
551 * try to steal the PENDING bit.
David Howells4594bf12006-12-07 11:33:26 +0000552 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200553static inline void set_work_data(struct work_struct *work, unsigned long data,
554 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000555{
Tejun Heo6183c002013-03-12 11:29:57 -0700556 WARN_ON_ONCE(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200557 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000558}
David Howells365970a2006-11-22 14:54:49 +0000559
Tejun Heo112202d2013-02-13 19:29:12 -0800560static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq,
Tejun Heo7a22ad72010-06-29 10:07:13 +0200561 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200562{
Tejun Heo112202d2013-02-13 19:29:12 -0800563 set_work_data(work, (unsigned long)pwq,
564 WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200565}
566
Lai Jiangshan4468a002013-02-06 18:04:53 -0800567static void set_work_pool_and_keep_pending(struct work_struct *work,
568 int pool_id)
569{
570 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT,
571 WORK_STRUCT_PENDING);
572}
573
Tejun Heo7c3eed52013-01-24 11:01:33 -0800574static void set_work_pool_and_clear_pending(struct work_struct *work,
575 int pool_id)
David Howells365970a2006-11-22 14:54:49 +0000576{
Tejun Heo23657bb2012-08-13 17:08:19 -0700577 /*
578 * The following wmb is paired with the implied mb in
579 * test_and_set_bit(PENDING) and ensures all updates to @work made
580 * here are visible to and precede any updates by the next PENDING
581 * owner.
582 */
583 smp_wmb();
Tejun Heo7c3eed52013-01-24 11:01:33 -0800584 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200585}
586
587static void clear_work_data(struct work_struct *work)
588{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800589 smp_wmb(); /* see set_work_pool_and_clear_pending() */
590 set_work_data(work, WORK_STRUCT_NO_POOL, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200591}
592
Tejun Heo112202d2013-02-13 19:29:12 -0800593static struct pool_workqueue *get_work_pwq(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200594{
Tejun Heoe1201532010-07-22 14:14:25 +0200595 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200596
Tejun Heo112202d2013-02-13 19:29:12 -0800597 if (data & WORK_STRUCT_PWQ)
Tejun Heoe1201532010-07-22 14:14:25 +0200598 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
599 else
600 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200601}
602
Tejun Heo7c3eed52013-01-24 11:01:33 -0800603/**
604 * get_work_pool - return the worker_pool a given work was associated with
605 * @work: the work item of interest
606 *
607 * Return the worker_pool @work was last associated with. %NULL if none.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700608 *
Tejun Heo5bcab332013-03-13 19:47:40 -0700609 * Pools are created and destroyed under wq_mutex, and allows read access
610 * under sched-RCU read lock. As such, this function should be called
611 * under wq_mutex or with preemption disabled.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700612 *
613 * All fields of the returned pool are accessible as long as the above
614 * mentioned locking is in effect. If the returned pool needs to be used
615 * beyond the critical section, the caller is responsible for ensuring the
616 * returned pool is and stays online.
Tejun Heo7c3eed52013-01-24 11:01:33 -0800617 */
618static struct worker_pool *get_work_pool(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200619{
Tejun Heoe1201532010-07-22 14:14:25 +0200620 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800621 int pool_id;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200622
Tejun Heo5bcab332013-03-13 19:47:40 -0700623 assert_rcu_or_wq_mutex();
Tejun Heofa1b54e2013-03-12 11:30:00 -0700624
Tejun Heo112202d2013-02-13 19:29:12 -0800625 if (data & WORK_STRUCT_PWQ)
626 return ((struct pool_workqueue *)
Tejun Heo7c3eed52013-01-24 11:01:33 -0800627 (data & WORK_STRUCT_WQ_DATA_MASK))->pool;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200628
Tejun Heo7c3eed52013-01-24 11:01:33 -0800629 pool_id = data >> WORK_OFFQ_POOL_SHIFT;
630 if (pool_id == WORK_OFFQ_POOL_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200631 return NULL;
632
Tejun Heofa1b54e2013-03-12 11:30:00 -0700633 return idr_find(&worker_pool_idr, pool_id);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800634}
635
636/**
637 * get_work_pool_id - return the worker pool ID a given work is associated with
638 * @work: the work item of interest
639 *
640 * Return the worker_pool ID @work was last associated with.
641 * %WORK_OFFQ_POOL_NONE if none.
642 */
643static int get_work_pool_id(struct work_struct *work)
644{
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800645 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800646
Tejun Heo112202d2013-02-13 19:29:12 -0800647 if (data & WORK_STRUCT_PWQ)
648 return ((struct pool_workqueue *)
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800649 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id;
650
651 return data >> WORK_OFFQ_POOL_SHIFT;
Tejun Heo7c3eed52013-01-24 11:01:33 -0800652}
653
Tejun Heobbb68df2012-08-03 10:30:46 -0700654static void mark_work_canceling(struct work_struct *work)
655{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800656 unsigned long pool_id = get_work_pool_id(work);
Tejun Heobbb68df2012-08-03 10:30:46 -0700657
Tejun Heo7c3eed52013-01-24 11:01:33 -0800658 pool_id <<= WORK_OFFQ_POOL_SHIFT;
659 set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700660}
661
662static bool work_is_canceling(struct work_struct *work)
663{
664 unsigned long data = atomic_long_read(&work->data);
665
Tejun Heo112202d2013-02-13 19:29:12 -0800666 return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700667}
668
David Howells365970a2006-11-22 14:54:49 +0000669/*
Tejun Heo32704762012-07-13 22:16:45 -0700670 * Policy functions. These define the policies on how the global worker
671 * pools are managed. Unless noted otherwise, these functions assume that
Tejun Heod565ed62013-01-24 11:01:33 -0800672 * they're being called with pool->lock held.
David Howells365970a2006-11-22 14:54:49 +0000673 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200674
Tejun Heo63d95a92012-07-12 14:46:37 -0700675static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000676{
Tejun Heoe19e3972013-01-24 11:39:44 -0800677 return !atomic_read(&pool->nr_running);
David Howells365970a2006-11-22 14:54:49 +0000678}
679
Tejun Heoe22bee72010-06-29 10:07:14 +0200680/*
681 * Need to wake up a worker? Called from anything but currently
682 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700683 *
684 * Note that, because unbound workers never contribute to nr_running, this
Tejun Heo706026c2013-01-24 11:01:34 -0800685 * function will always return %true for unbound pools as long as the
Tejun Heo974271c2012-07-12 14:46:37 -0700686 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200687 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700688static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000689{
Tejun Heo63d95a92012-07-12 14:46:37 -0700690 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000691}
692
Tejun Heoe22bee72010-06-29 10:07:14 +0200693/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700694static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200695{
Tejun Heo63d95a92012-07-12 14:46:37 -0700696 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200697}
698
699/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700700static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200701{
Tejun Heoe19e3972013-01-24 11:39:44 -0800702 return !list_empty(&pool->worklist) &&
703 atomic_read(&pool->nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200704}
705
706/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700707static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200708{
Tejun Heo63d95a92012-07-12 14:46:37 -0700709 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200710}
711
712/* Do I need to be the manager? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700713static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200714{
Tejun Heo63d95a92012-07-12 14:46:37 -0700715 return need_to_create_worker(pool) ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700716 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200717}
718
719/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700720static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200721{
Tejun Heo34a06bd2013-03-12 11:30:00 -0700722 bool managing = mutex_is_locked(&pool->manager_arb);
Tejun Heo63d95a92012-07-12 14:46:37 -0700723 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
724 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200725
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700726 /*
727 * nr_idle and idle_list may disagree if idle rebinding is in
728 * progress. Never return %true if idle_list is empty.
729 */
730 if (list_empty(&pool->idle_list))
731 return false;
732
Tejun Heoe22bee72010-06-29 10:07:14 +0200733 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
734}
735
736/*
737 * Wake up functions.
738 */
739
Tejun Heo7e116292010-06-29 10:07:13 +0200740/* Return the first worker. Safe with preemption disabled */
Tejun Heo63d95a92012-07-12 14:46:37 -0700741static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200742{
Tejun Heo63d95a92012-07-12 14:46:37 -0700743 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200744 return NULL;
745
Tejun Heo63d95a92012-07-12 14:46:37 -0700746 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200747}
748
749/**
750 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700751 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200752 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700753 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200754 *
755 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800756 * spin_lock_irq(pool->lock).
Tejun Heo7e116292010-06-29 10:07:13 +0200757 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700758static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200759{
Tejun Heo63d95a92012-07-12 14:46:37 -0700760 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200761
762 if (likely(worker))
763 wake_up_process(worker->task);
764}
765
Tejun Heo4690c4a2010-06-29 10:07:10 +0200766/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200767 * wq_worker_waking_up - a worker is waking up
768 * @task: task waking up
769 * @cpu: CPU @task is waking up to
770 *
771 * This function is called during try_to_wake_up() when a worker is
772 * being awoken.
773 *
774 * CONTEXT:
775 * spin_lock_irq(rq->lock)
776 */
Tejun Heod84ff052013-03-12 11:29:59 -0700777void wq_worker_waking_up(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200778{
779 struct worker *worker = kthread_data(task);
780
Joonsoo Kim36576002012-10-26 23:03:49 +0900781 if (!(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoec22ca52013-01-24 11:01:33 -0800782 WARN_ON_ONCE(worker->pool->cpu != cpu);
Tejun Heoe19e3972013-01-24 11:39:44 -0800783 atomic_inc(&worker->pool->nr_running);
Joonsoo Kim36576002012-10-26 23:03:49 +0900784 }
Tejun Heoe22bee72010-06-29 10:07:14 +0200785}
786
787/**
788 * wq_worker_sleeping - a worker is going to sleep
789 * @task: task going to sleep
790 * @cpu: CPU in question, must be the current CPU number
791 *
792 * This function is called during schedule() when a busy worker is
793 * going to sleep. Worker on the same cpu can be woken up by
794 * returning pointer to its task.
795 *
796 * CONTEXT:
797 * spin_lock_irq(rq->lock)
798 *
799 * RETURNS:
800 * Worker task on @cpu to wake up, %NULL if none.
801 */
Tejun Heod84ff052013-03-12 11:29:59 -0700802struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200803{
804 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heo111c2252013-01-17 17:16:24 -0800805 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200806
Tejun Heo111c2252013-01-17 17:16:24 -0800807 /*
808 * Rescuers, which may not have all the fields set up like normal
809 * workers, also reach here, let's not access anything before
810 * checking NOT_RUNNING.
811 */
Steven Rostedt2d646722010-12-03 23:12:33 -0500812 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200813 return NULL;
814
Tejun Heo111c2252013-01-17 17:16:24 -0800815 pool = worker->pool;
Tejun Heo111c2252013-01-17 17:16:24 -0800816
Tejun Heoe22bee72010-06-29 10:07:14 +0200817 /* this can only happen on the local cpu */
Tejun Heo6183c002013-03-12 11:29:57 -0700818 if (WARN_ON_ONCE(cpu != raw_smp_processor_id()))
819 return NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +0200820
821 /*
822 * The counterpart of the following dec_and_test, implied mb,
823 * worklist not empty test sequence is in insert_work().
824 * Please read comment there.
825 *
Tejun Heo628c78e2012-07-17 12:39:27 -0700826 * NOT_RUNNING is clear. This means that we're bound to and
827 * running on the local cpu w/ rq lock held and preemption
828 * disabled, which in turn means that none else could be
Tejun Heod565ed62013-01-24 11:01:33 -0800829 * manipulating idle_list, so dereferencing idle_list without pool
Tejun Heo628c78e2012-07-17 12:39:27 -0700830 * lock is safe.
Tejun Heoe22bee72010-06-29 10:07:14 +0200831 */
Tejun Heoe19e3972013-01-24 11:39:44 -0800832 if (atomic_dec_and_test(&pool->nr_running) &&
833 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700834 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200835 return to_wakeup ? to_wakeup->task : NULL;
836}
837
838/**
839 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200840 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200841 * @flags: flags to set
842 * @wakeup: wakeup an idle worker if necessary
843 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200844 * Set @flags in @worker->flags and adjust nr_running accordingly. If
845 * nr_running becomes zero and @wakeup is %true, an idle worker is
846 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200847 *
Tejun Heocb444762010-07-02 10:03:50 +0200848 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800849 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200850 */
851static inline void worker_set_flags(struct worker *worker, unsigned int flags,
852 bool wakeup)
853{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700854 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200855
Tejun Heocb444762010-07-02 10:03:50 +0200856 WARN_ON_ONCE(worker->task != current);
857
Tejun Heoe22bee72010-06-29 10:07:14 +0200858 /*
859 * If transitioning into NOT_RUNNING, adjust nr_running and
860 * wake up an idle worker as necessary if requested by
861 * @wakeup.
862 */
863 if ((flags & WORKER_NOT_RUNNING) &&
864 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoe22bee72010-06-29 10:07:14 +0200865 if (wakeup) {
Tejun Heoe19e3972013-01-24 11:39:44 -0800866 if (atomic_dec_and_test(&pool->nr_running) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -0700867 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700868 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200869 } else
Tejun Heoe19e3972013-01-24 11:39:44 -0800870 atomic_dec(&pool->nr_running);
Tejun Heoe22bee72010-06-29 10:07:14 +0200871 }
872
Tejun Heod302f012010-06-29 10:07:13 +0200873 worker->flags |= flags;
874}
875
876/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200877 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200878 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200879 * @flags: flags to clear
880 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200881 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200882 *
Tejun Heocb444762010-07-02 10:03:50 +0200883 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800884 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200885 */
886static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
887{
Tejun Heo63d95a92012-07-12 14:46:37 -0700888 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200889 unsigned int oflags = worker->flags;
890
Tejun Heocb444762010-07-02 10:03:50 +0200891 WARN_ON_ONCE(worker->task != current);
892
Tejun Heod302f012010-06-29 10:07:13 +0200893 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200894
Tejun Heo42c025f2011-01-11 15:58:49 +0100895 /*
896 * If transitioning out of NOT_RUNNING, increment nr_running. Note
897 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
898 * of multiple flags, not a single flag.
899 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200900 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
901 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heoe19e3972013-01-24 11:39:44 -0800902 atomic_inc(&pool->nr_running);
Tejun Heod302f012010-06-29 10:07:13 +0200903}
904
905/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200906 * find_worker_executing_work - find worker which is executing a work
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800907 * @pool: pool of interest
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200908 * @work: work to find worker for
909 *
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800910 * Find a worker which is executing @work on @pool by searching
911 * @pool->busy_hash which is keyed by the address of @work. For a worker
Tejun Heoa2c1c572012-12-18 10:35:02 -0800912 * to match, its current execution should match the address of @work and
913 * its work function. This is to avoid unwanted dependency between
914 * unrelated work executions through a work item being recycled while still
915 * being executed.
916 *
917 * This is a bit tricky. A work item may be freed once its execution
918 * starts and nothing prevents the freed area from being recycled for
919 * another work item. If the same work item address ends up being reused
920 * before the original execution finishes, workqueue will identify the
921 * recycled work item as currently executing and make it wait until the
922 * current execution finishes, introducing an unwanted dependency.
923 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700924 * This function checks the work item address and work function to avoid
925 * false positives. Note that this isn't complete as one may construct a
926 * work function which can introduce dependency onto itself through a
927 * recycled work item. Well, if somebody wants to shoot oneself in the
928 * foot that badly, there's only so much we can do, and if such deadlock
929 * actually occurs, it should be easy to locate the culprit work function.
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200930 *
931 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800932 * spin_lock_irq(pool->lock).
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200933 *
934 * RETURNS:
935 * Pointer to worker which is executing @work if found, NULL
936 * otherwise.
937 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800938static struct worker *find_worker_executing_work(struct worker_pool *pool,
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200939 struct work_struct *work)
940{
Sasha Levin42f85702012-12-17 10:01:23 -0500941 struct worker *worker;
Sasha Levin42f85702012-12-17 10:01:23 -0500942
Sasha Levinb67bfe02013-02-27 17:06:00 -0800943 hash_for_each_possible(pool->busy_hash, worker, hentry,
Tejun Heoa2c1c572012-12-18 10:35:02 -0800944 (unsigned long)work)
945 if (worker->current_work == work &&
946 worker->current_func == work->func)
Sasha Levin42f85702012-12-17 10:01:23 -0500947 return worker;
948
949 return NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200950}
951
952/**
Tejun Heobf4ede02012-08-03 10:30:46 -0700953 * move_linked_works - move linked works to a list
954 * @work: start of series of works to be scheduled
955 * @head: target list to append @work to
956 * @nextp: out paramter for nested worklist walking
957 *
958 * Schedule linked works starting from @work to @head. Work series to
959 * be scheduled starts at @work and includes any consecutive work with
960 * WORK_STRUCT_LINKED set in its predecessor.
961 *
962 * If @nextp is not NULL, it's updated to point to the next work of
963 * the last scheduled work. This allows move_linked_works() to be
964 * nested inside outer list_for_each_entry_safe().
965 *
966 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800967 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -0700968 */
969static void move_linked_works(struct work_struct *work, struct list_head *head,
970 struct work_struct **nextp)
971{
972 struct work_struct *n;
973
974 /*
975 * Linked worklist will always end before the end of the list,
976 * use NULL for list head.
977 */
978 list_for_each_entry_safe_from(work, n, NULL, entry) {
979 list_move_tail(&work->entry, head);
980 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
981 break;
982 }
983
984 /*
985 * If we're already inside safe list traversal and have moved
986 * multiple works to the scheduled queue, the next position
987 * needs to be updated.
988 */
989 if (nextp)
990 *nextp = n;
991}
992
Tejun Heo8864b4e2013-03-12 11:30:04 -0700993/**
994 * get_pwq - get an extra reference on the specified pool_workqueue
995 * @pwq: pool_workqueue to get
996 *
997 * Obtain an extra reference on @pwq. The caller should guarantee that
998 * @pwq has positive refcnt and be holding the matching pool->lock.
999 */
1000static void get_pwq(struct pool_workqueue *pwq)
1001{
1002 lockdep_assert_held(&pwq->pool->lock);
1003 WARN_ON_ONCE(pwq->refcnt <= 0);
1004 pwq->refcnt++;
1005}
1006
1007/**
1008 * put_pwq - put a pool_workqueue reference
1009 * @pwq: pool_workqueue to put
1010 *
1011 * Drop a reference of @pwq. If its refcnt reaches zero, schedule its
1012 * destruction. The caller should be holding the matching pool->lock.
1013 */
1014static void put_pwq(struct pool_workqueue *pwq)
1015{
1016 lockdep_assert_held(&pwq->pool->lock);
1017 if (likely(--pwq->refcnt))
1018 return;
1019 if (WARN_ON_ONCE(!(pwq->wq->flags & WQ_UNBOUND)))
1020 return;
1021 /*
1022 * @pwq can't be released under pool->lock, bounce to
1023 * pwq_unbound_release_workfn(). This never recurses on the same
1024 * pool->lock as this path is taken only for unbound workqueues and
1025 * the release work item is scheduled on a per-cpu workqueue. To
1026 * avoid lockdep warning, unbound pool->locks are given lockdep
1027 * subclass of 1 in get_unbound_pool().
1028 */
1029 schedule_work(&pwq->unbound_release_work);
1030}
1031
Tejun Heo112202d2013-02-13 19:29:12 -08001032static void pwq_activate_delayed_work(struct work_struct *work)
Tejun Heobf4ede02012-08-03 10:30:46 -07001033{
Tejun Heo112202d2013-02-13 19:29:12 -08001034 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobf4ede02012-08-03 10:30:46 -07001035
1036 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001037 move_linked_works(work, &pwq->pool->worklist, NULL);
Tejun Heobf4ede02012-08-03 10:30:46 -07001038 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo112202d2013-02-13 19:29:12 -08001039 pwq->nr_active++;
Tejun Heobf4ede02012-08-03 10:30:46 -07001040}
1041
Tejun Heo112202d2013-02-13 19:29:12 -08001042static void pwq_activate_first_delayed(struct pool_workqueue *pwq)
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001043{
Tejun Heo112202d2013-02-13 19:29:12 -08001044 struct work_struct *work = list_first_entry(&pwq->delayed_works,
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001045 struct work_struct, entry);
1046
Tejun Heo112202d2013-02-13 19:29:12 -08001047 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001048}
1049
Tejun Heobf4ede02012-08-03 10:30:46 -07001050/**
Tejun Heo112202d2013-02-13 19:29:12 -08001051 * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight
1052 * @pwq: pwq of interest
Tejun Heobf4ede02012-08-03 10:30:46 -07001053 * @color: color of work which left the queue
Tejun Heobf4ede02012-08-03 10:30:46 -07001054 *
1055 * A work either has completed or is removed from pending queue,
Tejun Heo112202d2013-02-13 19:29:12 -08001056 * decrement nr_in_flight of its pwq and handle workqueue flushing.
Tejun Heobf4ede02012-08-03 10:30:46 -07001057 *
1058 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001059 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -07001060 */
Tejun Heo112202d2013-02-13 19:29:12 -08001061static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color)
Tejun Heobf4ede02012-08-03 10:30:46 -07001062{
Tejun Heo8864b4e2013-03-12 11:30:04 -07001063 /* uncolored work items don't participate in flushing or nr_active */
Tejun Heobf4ede02012-08-03 10:30:46 -07001064 if (color == WORK_NO_COLOR)
Tejun Heo8864b4e2013-03-12 11:30:04 -07001065 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001066
Tejun Heo112202d2013-02-13 19:29:12 -08001067 pwq->nr_in_flight[color]--;
Tejun Heobf4ede02012-08-03 10:30:46 -07001068
Tejun Heo112202d2013-02-13 19:29:12 -08001069 pwq->nr_active--;
1070 if (!list_empty(&pwq->delayed_works)) {
Lai Jiangshanb3f9f402012-09-18 10:40:00 -07001071 /* one down, submit a delayed one */
Tejun Heo112202d2013-02-13 19:29:12 -08001072 if (pwq->nr_active < pwq->max_active)
1073 pwq_activate_first_delayed(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001074 }
1075
1076 /* is flush in progress and are we at the flushing tip? */
Tejun Heo112202d2013-02-13 19:29:12 -08001077 if (likely(pwq->flush_color != color))
Tejun Heo8864b4e2013-03-12 11:30:04 -07001078 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001079
1080 /* are there still in-flight works? */
Tejun Heo112202d2013-02-13 19:29:12 -08001081 if (pwq->nr_in_flight[color])
Tejun Heo8864b4e2013-03-12 11:30:04 -07001082 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001083
Tejun Heo112202d2013-02-13 19:29:12 -08001084 /* this pwq is done, clear flush_color */
1085 pwq->flush_color = -1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001086
1087 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001088 * If this was the last pwq, wake up the first flusher. It
Tejun Heobf4ede02012-08-03 10:30:46 -07001089 * will handle the rest.
1090 */
Tejun Heo112202d2013-02-13 19:29:12 -08001091 if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush))
1092 complete(&pwq->wq->first_flusher->done);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001093out_put:
1094 put_pwq(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001095}
1096
Tejun Heo36e227d2012-08-03 10:30:46 -07001097/**
Tejun Heobbb68df2012-08-03 10:30:46 -07001098 * try_to_grab_pending - steal work item from worklist and disable irq
Tejun Heo36e227d2012-08-03 10:30:46 -07001099 * @work: work item to steal
1100 * @is_dwork: @work is a delayed_work
Tejun Heobbb68df2012-08-03 10:30:46 -07001101 * @flags: place to store irq state
Tejun Heo36e227d2012-08-03 10:30:46 -07001102 *
1103 * Try to grab PENDING bit of @work. This function can handle @work in any
1104 * stable state - idle, on timer or on worklist. Return values are
1105 *
1106 * 1 if @work was pending and we successfully stole PENDING
1107 * 0 if @work was idle and we claimed PENDING
1108 * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry
Tejun Heobbb68df2012-08-03 10:30:46 -07001109 * -ENOENT if someone else is canceling @work, this state may persist
1110 * for arbitrarily long
Tejun Heo36e227d2012-08-03 10:30:46 -07001111 *
Tejun Heobbb68df2012-08-03 10:30:46 -07001112 * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001113 * interrupted while holding PENDING and @work off queue, irq must be
1114 * disabled on entry. This, combined with delayed_work->timer being
1115 * irqsafe, ensures that we return -EAGAIN for finite short period of time.
Tejun Heobbb68df2012-08-03 10:30:46 -07001116 *
1117 * On successful return, >= 0, irq is disabled and the caller is
1118 * responsible for releasing it using local_irq_restore(*@flags).
1119 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001120 * This function is safe to call from any context including IRQ handler.
Tejun Heobf4ede02012-08-03 10:30:46 -07001121 */
Tejun Heobbb68df2012-08-03 10:30:46 -07001122static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
1123 unsigned long *flags)
Tejun Heobf4ede02012-08-03 10:30:46 -07001124{
Tejun Heod565ed62013-01-24 11:01:33 -08001125 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08001126 struct pool_workqueue *pwq;
Tejun Heobf4ede02012-08-03 10:30:46 -07001127
Tejun Heobbb68df2012-08-03 10:30:46 -07001128 local_irq_save(*flags);
1129
Tejun Heo36e227d2012-08-03 10:30:46 -07001130 /* try to steal the timer if it exists */
1131 if (is_dwork) {
1132 struct delayed_work *dwork = to_delayed_work(work);
1133
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001134 /*
1135 * dwork->timer is irqsafe. If del_timer() fails, it's
1136 * guaranteed that the timer is not queued anywhere and not
1137 * running on the local CPU.
1138 */
Tejun Heo36e227d2012-08-03 10:30:46 -07001139 if (likely(del_timer(&dwork->timer)))
1140 return 1;
1141 }
1142
1143 /* try to claim PENDING the normal way */
Tejun Heobf4ede02012-08-03 10:30:46 -07001144 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1145 return 0;
1146
1147 /*
1148 * The queueing is in progress, or it is already queued. Try to
1149 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1150 */
Tejun Heod565ed62013-01-24 11:01:33 -08001151 pool = get_work_pool(work);
1152 if (!pool)
Tejun Heobbb68df2012-08-03 10:30:46 -07001153 goto fail;
Tejun Heobf4ede02012-08-03 10:30:46 -07001154
Tejun Heod565ed62013-01-24 11:01:33 -08001155 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001156 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001157 * work->data is guaranteed to point to pwq only while the work
1158 * item is queued on pwq->wq, and both updating work->data to point
1159 * to pwq on queueing and to pool on dequeueing are done under
1160 * pwq->pool->lock. This in turn guarantees that, if work->data
1161 * points to pwq which is associated with a locked pool, the work
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001162 * item is currently queued on that pool.
1163 */
Tejun Heo112202d2013-02-13 19:29:12 -08001164 pwq = get_work_pwq(work);
1165 if (pwq && pwq->pool == pool) {
Tejun Heo16062832013-02-06 18:04:53 -08001166 debug_work_deactivate(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001167
Tejun Heo16062832013-02-06 18:04:53 -08001168 /*
1169 * A delayed work item cannot be grabbed directly because
1170 * it might have linked NO_COLOR work items which, if left
Tejun Heo112202d2013-02-13 19:29:12 -08001171 * on the delayed_list, will confuse pwq->nr_active
Tejun Heo16062832013-02-06 18:04:53 -08001172 * management later on and cause stall. Make sure the work
1173 * item is activated before grabbing.
1174 */
1175 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
Tejun Heo112202d2013-02-13 19:29:12 -08001176 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001177
Tejun Heo16062832013-02-06 18:04:53 -08001178 list_del_init(&work->entry);
Tejun Heo112202d2013-02-13 19:29:12 -08001179 pwq_dec_nr_in_flight(get_work_pwq(work), get_work_color(work));
Tejun Heo36e227d2012-08-03 10:30:46 -07001180
Tejun Heo112202d2013-02-13 19:29:12 -08001181 /* work->data points to pwq iff queued, point to pool */
Tejun Heo16062832013-02-06 18:04:53 -08001182 set_work_pool_and_keep_pending(work, pool->id);
Lai Jiangshan4468a002013-02-06 18:04:53 -08001183
Tejun Heo16062832013-02-06 18:04:53 -08001184 spin_unlock(&pool->lock);
1185 return 1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001186 }
Tejun Heod565ed62013-01-24 11:01:33 -08001187 spin_unlock(&pool->lock);
Tejun Heobbb68df2012-08-03 10:30:46 -07001188fail:
1189 local_irq_restore(*flags);
1190 if (work_is_canceling(work))
1191 return -ENOENT;
1192 cpu_relax();
Tejun Heo36e227d2012-08-03 10:30:46 -07001193 return -EAGAIN;
Tejun Heobf4ede02012-08-03 10:30:46 -07001194}
1195
1196/**
Tejun Heo706026c2013-01-24 11:01:34 -08001197 * insert_work - insert a work into a pool
Tejun Heo112202d2013-02-13 19:29:12 -08001198 * @pwq: pwq @work belongs to
Tejun Heo4690c4a2010-06-29 10:07:10 +02001199 * @work: work to insert
1200 * @head: insertion point
1201 * @extra_flags: extra WORK_STRUCT_* flags to set
1202 *
Tejun Heo112202d2013-02-13 19:29:12 -08001203 * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to
Tejun Heo706026c2013-01-24 11:01:34 -08001204 * work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +02001205 *
1206 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001207 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02001208 */
Tejun Heo112202d2013-02-13 19:29:12 -08001209static void insert_work(struct pool_workqueue *pwq, struct work_struct *work,
1210 struct list_head *head, unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001211{
Tejun Heo112202d2013-02-13 19:29:12 -08001212 struct worker_pool *pool = pwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01001213
Tejun Heo4690c4a2010-06-29 10:07:10 +02001214 /* we own @work, set data and link */
Tejun Heo112202d2013-02-13 19:29:12 -08001215 set_work_pwq(work, pwq, extra_flags);
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -07001216 list_add_tail(&work->entry, head);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001217 get_pwq(pwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02001218
1219 /*
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001220 * Ensure either wq_worker_sleeping() sees the above
1221 * list_add_tail() or we see zero nr_running to avoid workers lying
1222 * around lazily while there are works to be processed.
Tejun Heoe22bee72010-06-29 10:07:14 +02001223 */
1224 smp_mb();
1225
Tejun Heo63d95a92012-07-12 14:46:37 -07001226 if (__need_more_worker(pool))
1227 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001228}
1229
Tejun Heoc8efcc22010-12-20 19:32:04 +01001230/*
1231 * Test whether @work is being queued from another work executing on the
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001232 * same workqueue.
Tejun Heoc8efcc22010-12-20 19:32:04 +01001233 */
1234static bool is_chained_work(struct workqueue_struct *wq)
1235{
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001236 struct worker *worker;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001237
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001238 worker = current_wq_worker();
1239 /*
1240 * Return %true iff I'm a worker execuing a work item on @wq. If
1241 * I'm @worker, it's safe to dereference it without locking.
1242 */
Tejun Heo112202d2013-02-13 19:29:12 -08001243 return worker && worker->current_pwq->wq == wq;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001244}
1245
Tejun Heod84ff052013-03-12 11:29:59 -07001246static void __queue_work(int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 struct work_struct *work)
1248{
Tejun Heo112202d2013-02-13 19:29:12 -08001249 struct pool_workqueue *pwq;
Tejun Heoc9178082013-03-12 11:30:04 -07001250 struct worker_pool *last_pool;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001251 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001252 unsigned int work_flags;
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001253 unsigned int req_cpu = cpu;
Tejun Heo8930cab2012-08-03 10:30:45 -07001254
1255 /*
1256 * While a work item is PENDING && off queue, a task trying to
1257 * steal the PENDING will busy-loop waiting for it to either get
1258 * queued or lose PENDING. Grabbing PENDING and queueing should
1259 * happen with IRQ disabled.
1260 */
1261 WARN_ON_ONCE(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001263 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001264
Tejun Heoc8efcc22010-12-20 19:32:04 +01001265 /* if dying, only works from the same workqueue are allowed */
Tejun Heo618b01e2013-03-12 11:30:04 -07001266 if (unlikely(wq->flags & __WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001267 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001268 return;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001269retry:
Tejun Heoc9178082013-03-12 11:30:04 -07001270 /* pwq which will be used unless @work is executing elsewhere */
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001271 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo57469822012-08-03 10:30:45 -07001272 if (cpu == WORK_CPU_UNBOUND)
Tejun Heoc7fc77f2010-07-02 10:03:51 +02001273 cpu = raw_smp_processor_id();
Tejun Heo7fb98ea2013-03-12 11:30:00 -07001274 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heoc9178082013-03-12 11:30:04 -07001275 } else {
1276 pwq = first_pwq(wq);
1277 }
Tejun Heodbf25762012-08-20 14:51:23 -07001278
Tejun Heoc9178082013-03-12 11:30:04 -07001279 /*
1280 * If @work was previously on a different pool, it might still be
1281 * running there, in which case the work needs to be queued on that
1282 * pool to guarantee non-reentrancy.
1283 */
1284 last_pool = get_work_pool(work);
1285 if (last_pool && last_pool != pwq->pool) {
1286 struct worker *worker;
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001287
Tejun Heoc9178082013-03-12 11:30:04 -07001288 spin_lock(&last_pool->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001289
Tejun Heoc9178082013-03-12 11:30:04 -07001290 worker = find_worker_executing_work(last_pool, work);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001291
Tejun Heoc9178082013-03-12 11:30:04 -07001292 if (worker && worker->current_pwq->wq == wq) {
1293 pwq = worker->current_pwq;
Tejun Heo8930cab2012-08-03 10:30:45 -07001294 } else {
Tejun Heoc9178082013-03-12 11:30:04 -07001295 /* meh... not running there, queue here */
1296 spin_unlock(&last_pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08001297 spin_lock(&pwq->pool->lock);
Tejun Heo8930cab2012-08-03 10:30:45 -07001298 }
Tejun Heof3421792010-07-02 10:03:51 +02001299 } else {
Tejun Heo112202d2013-02-13 19:29:12 -08001300 spin_lock(&pwq->pool->lock);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001301 }
1302
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001303 /*
1304 * pwq is determined and locked. For unbound pools, we could have
1305 * raced with pwq release and it could already be dead. If its
1306 * refcnt is zero, repeat pwq selection. Note that pwqs never die
1307 * without another pwq replacing it as the first pwq or while a
1308 * work item is executing on it, so the retying is guaranteed to
1309 * make forward-progress.
1310 */
1311 if (unlikely(!pwq->refcnt)) {
1312 if (wq->flags & WQ_UNBOUND) {
1313 spin_unlock(&pwq->pool->lock);
1314 cpu_relax();
1315 goto retry;
1316 }
1317 /* oops */
1318 WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt",
1319 wq->name, cpu);
1320 }
1321
Tejun Heo112202d2013-02-13 19:29:12 -08001322 /* pwq determined, queue */
1323 trace_workqueue_queue_work(req_cpu, pwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001324
Dan Carpenterf5b25522012-04-13 22:06:58 +03001325 if (WARN_ON(!list_empty(&work->entry))) {
Tejun Heo112202d2013-02-13 19:29:12 -08001326 spin_unlock(&pwq->pool->lock);
Dan Carpenterf5b25522012-04-13 22:06:58 +03001327 return;
1328 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001329
Tejun Heo112202d2013-02-13 19:29:12 -08001330 pwq->nr_in_flight[pwq->work_color]++;
1331 work_flags = work_color_to_flags(pwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001332
Tejun Heo112202d2013-02-13 19:29:12 -08001333 if (likely(pwq->nr_active < pwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001334 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001335 pwq->nr_active++;
1336 worklist = &pwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001337 } else {
1338 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo112202d2013-02-13 19:29:12 -08001339 worklist = &pwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001340 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001341
Tejun Heo112202d2013-02-13 19:29:12 -08001342 insert_work(pwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001343
Tejun Heo112202d2013-02-13 19:29:12 -08001344 spin_unlock(&pwq->pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345}
1346
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001347/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07001348 * queue_work_on - queue work on specific cpu
1349 * @cpu: CPU number to execute work on
1350 * @wq: workqueue to use
1351 * @work: work to queue
1352 *
Tejun Heod4283e92012-08-03 10:30:44 -07001353 * Returns %false if @work was already on a queue, %true otherwise.
Zhang Ruic1a220e2008-07-23 21:28:39 -07001354 *
1355 * We queue the work to a specific CPU, the caller must ensure it
1356 * can't go away.
1357 */
Tejun Heod4283e92012-08-03 10:30:44 -07001358bool queue_work_on(int cpu, struct workqueue_struct *wq,
1359 struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07001360{
Tejun Heod4283e92012-08-03 10:30:44 -07001361 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001362 unsigned long flags;
1363
1364 local_irq_save(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001365
Tejun Heo22df02b2010-06-29 10:07:10 +02001366 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001367 __queue_work(cpu, wq, work);
Tejun Heod4283e92012-08-03 10:30:44 -07001368 ret = true;
Zhang Ruic1a220e2008-07-23 21:28:39 -07001369 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001370
1371 local_irq_restore(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001372 return ret;
1373}
1374EXPORT_SYMBOL_GPL(queue_work_on);
1375
Tejun Heod8e794d2012-08-03 10:30:45 -07001376void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
David Howells52bad642006-11-22 14:54:01 +00001378 struct delayed_work *dwork = (struct delayed_work *)__data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001380 /* should have been called from irqsafe timer with irq already off */
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001381 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382}
Konstantin Khlebnikov1438ade52013-01-24 16:36:31 +04001383EXPORT_SYMBOL(delayed_work_timer_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001385static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
1386 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001388 struct timer_list *timer = &dwork->timer;
1389 struct work_struct *work = &dwork->work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001391 WARN_ON_ONCE(timer->function != delayed_work_timer_fn ||
1392 timer->data != (unsigned long)dwork);
Tejun Heofc4b5142012-12-04 07:40:39 -08001393 WARN_ON_ONCE(timer_pending(timer));
1394 WARN_ON_ONCE(!list_empty(&work->entry));
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001395
Tejun Heo8852aac2012-12-01 16:23:42 -08001396 /*
1397 * If @delay is 0, queue @dwork->work immediately. This is for
1398 * both optimization and correctness. The earliest @timer can
1399 * expire is on the closest next tick and delayed_work users depend
1400 * on that there's no such delay when @delay is 0.
1401 */
1402 if (!delay) {
1403 __queue_work(cpu, wq, &dwork->work);
1404 return;
1405 }
1406
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001407 timer_stats_timer_set_start_info(&dwork->timer);
1408
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001409 dwork->wq = wq;
Tejun Heo12650572012-08-08 09:38:42 -07001410 dwork->cpu = cpu;
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001411 timer->expires = jiffies + delay;
1412
1413 if (unlikely(cpu != WORK_CPU_UNBOUND))
1414 add_timer_on(timer, cpu);
1415 else
1416 add_timer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417}
1418
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001419/**
1420 * queue_delayed_work_on - queue work on specific CPU after delay
1421 * @cpu: CPU number to execute work on
1422 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001423 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001424 * @delay: number of jiffies to wait before queueing
1425 *
Tejun Heo715f1302012-08-03 10:30:46 -07001426 * Returns %false if @work was already on a queue, %true otherwise. If
1427 * @delay is zero and @dwork is idle, it will be scheduled for immediate
1428 * execution.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001429 */
Tejun Heod4283e92012-08-03 10:30:44 -07001430bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
1431 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001432{
David Howells52bad642006-11-22 14:54:01 +00001433 struct work_struct *work = &dwork->work;
Tejun Heod4283e92012-08-03 10:30:44 -07001434 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001435 unsigned long flags;
1436
1437 /* read the comment in __queue_work() */
1438 local_irq_save(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001439
Tejun Heo22df02b2010-06-29 10:07:10 +02001440 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001441 __queue_delayed_work(cpu, wq, dwork, delay);
Tejun Heod4283e92012-08-03 10:30:44 -07001442 ret = true;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001443 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001444
1445 local_irq_restore(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001446 return ret;
1447}
Dave Jonesae90dd52006-06-30 01:40:45 -04001448EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Tejun Heoc8e55f32010-06-29 10:07:12 +02001450/**
Tejun Heo8376fe22012-08-03 10:30:47 -07001451 * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
1452 * @cpu: CPU number to execute work on
1453 * @wq: workqueue to use
1454 * @dwork: work to queue
1455 * @delay: number of jiffies to wait before queueing
1456 *
1457 * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
1458 * modify @dwork's timer so that it expires after @delay. If @delay is
1459 * zero, @work is guaranteed to be scheduled immediately regardless of its
1460 * current state.
1461 *
1462 * Returns %false if @dwork was idle and queued, %true if @dwork was
1463 * pending and its timer was modified.
1464 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001465 * This function is safe to call from any context including IRQ handler.
Tejun Heo8376fe22012-08-03 10:30:47 -07001466 * See try_to_grab_pending() for details.
1467 */
1468bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
1469 struct delayed_work *dwork, unsigned long delay)
1470{
1471 unsigned long flags;
1472 int ret;
1473
1474 do {
1475 ret = try_to_grab_pending(&dwork->work, true, &flags);
1476 } while (unlikely(ret == -EAGAIN));
1477
1478 if (likely(ret >= 0)) {
1479 __queue_delayed_work(cpu, wq, dwork, delay);
1480 local_irq_restore(flags);
1481 }
1482
1483 /* -ENOENT from try_to_grab_pending() becomes %true */
1484 return ret;
1485}
1486EXPORT_SYMBOL_GPL(mod_delayed_work_on);
1487
1488/**
Tejun Heoc8e55f32010-06-29 10:07:12 +02001489 * worker_enter_idle - enter idle state
1490 * @worker: worker which is entering idle state
1491 *
1492 * @worker is entering idle state. Update stats and idle timer if
1493 * necessary.
1494 *
1495 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001496 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001497 */
1498static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001500 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Tejun Heo6183c002013-03-12 11:29:57 -07001502 if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) ||
1503 WARN_ON_ONCE(!list_empty(&worker->entry) &&
1504 (worker->hentry.next || worker->hentry.pprev)))
1505 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Tejun Heocb444762010-07-02 10:03:50 +02001507 /* can't use worker_set_flags(), also called from start_worker() */
1508 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001509 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001510 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001511
Tejun Heoc8e55f32010-06-29 10:07:12 +02001512 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001513 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001514
Tejun Heo628c78e2012-07-17 12:39:27 -07001515 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1516 mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
Tejun Heocb444762010-07-02 10:03:50 +02001517
Tejun Heo544ecf32012-05-14 15:04:50 -07001518 /*
Tejun Heo706026c2013-01-24 11:01:34 -08001519 * Sanity check nr_running. Because wq_unbind_fn() releases
Tejun Heod565ed62013-01-24 11:01:33 -08001520 * pool->lock between setting %WORKER_UNBOUND and zapping
Tejun Heo628c78e2012-07-17 12:39:27 -07001521 * nr_running, the warning may trigger spuriously. Check iff
1522 * unbind is not in progress.
Tejun Heo544ecf32012-05-14 15:04:50 -07001523 */
Tejun Heo24647572013-01-24 11:01:33 -08001524 WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001525 pool->nr_workers == pool->nr_idle &&
Tejun Heoe19e3972013-01-24 11:39:44 -08001526 atomic_read(&pool->nr_running));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527}
1528
Tejun Heoc8e55f32010-06-29 10:07:12 +02001529/**
1530 * worker_leave_idle - leave idle state
1531 * @worker: worker which is leaving idle state
1532 *
1533 * @worker is leaving idle state. Update stats.
1534 *
1535 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001536 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001537 */
1538static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001540 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Tejun Heo6183c002013-03-12 11:29:57 -07001542 if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE)))
1543 return;
Tejun Heod302f012010-06-29 10:07:13 +02001544 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001545 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001546 list_del_init(&worker->entry);
1547}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Tejun Heoe22bee72010-06-29 10:07:14 +02001549/**
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001550 * worker_maybe_bind_and_lock - try to bind %current to worker_pool and lock it
1551 * @pool: target worker_pool
1552 *
1553 * Bind %current to the cpu of @pool if it is associated and lock @pool.
Tejun Heoe22bee72010-06-29 10:07:14 +02001554 *
1555 * Works which are scheduled while the cpu is online must at least be
1556 * scheduled to a worker which is bound to the cpu so that if they are
1557 * flushed from cpu callbacks while cpu is going down, they are
1558 * guaranteed to execute on the cpu.
1559 *
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001560 * This function is to be used by unbound workers and rescuers to bind
Tejun Heoe22bee72010-06-29 10:07:14 +02001561 * themselves to the target cpu and may race with cpu going down or
1562 * coming online. kthread_bind() can't be used because it may put the
1563 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
Tejun Heo706026c2013-01-24 11:01:34 -08001564 * verbatim as it's best effort and blocking and pool may be
Tejun Heoe22bee72010-06-29 10:07:14 +02001565 * [dis]associated in the meantime.
1566 *
Tejun Heo706026c2013-01-24 11:01:34 -08001567 * This function tries set_cpus_allowed() and locks pool and verifies the
Tejun Heo24647572013-01-24 11:01:33 -08001568 * binding against %POOL_DISASSOCIATED which is set during
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001569 * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1570 * enters idle state or fetches works without dropping lock, it can
1571 * guarantee the scheduling requirement described in the first paragraph.
Tejun Heoe22bee72010-06-29 10:07:14 +02001572 *
1573 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001574 * Might sleep. Called without any lock but returns with pool->lock
Tejun Heoe22bee72010-06-29 10:07:14 +02001575 * held.
1576 *
1577 * RETURNS:
Tejun Heo706026c2013-01-24 11:01:34 -08001578 * %true if the associated pool is online (@worker is successfully
Tejun Heoe22bee72010-06-29 10:07:14 +02001579 * bound), %false if offline.
1580 */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001581static bool worker_maybe_bind_and_lock(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001582__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001583{
Tejun Heoe22bee72010-06-29 10:07:14 +02001584 while (true) {
1585 /*
1586 * The following call may fail, succeed or succeed
1587 * without actually migrating the task to the cpu if
1588 * it races with cpu hotunplug operation. Verify
Tejun Heo24647572013-01-24 11:01:33 -08001589 * against POOL_DISASSOCIATED.
Tejun Heoe22bee72010-06-29 10:07:14 +02001590 */
Tejun Heo24647572013-01-24 11:01:33 -08001591 if (!(pool->flags & POOL_DISASSOCIATED))
Tejun Heo7a4e3442013-03-12 11:30:00 -07001592 set_cpus_allowed_ptr(current, pool->attrs->cpumask);
Oleg Nesterov85f41862007-05-09 02:34:20 -07001593
Tejun Heod565ed62013-01-24 11:01:33 -08001594 spin_lock_irq(&pool->lock);
Tejun Heo24647572013-01-24 11:01:33 -08001595 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heoe22bee72010-06-29 10:07:14 +02001596 return false;
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001597 if (task_cpu(current) == pool->cpu &&
Tejun Heo7a4e3442013-03-12 11:30:00 -07001598 cpumask_equal(&current->cpus_allowed, pool->attrs->cpumask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001599 return true;
Tejun Heod565ed62013-01-24 11:01:33 -08001600 spin_unlock_irq(&pool->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001601
Tejun Heo5035b202011-04-29 18:08:37 +02001602 /*
1603 * We've raced with CPU hot[un]plug. Give it a breather
1604 * and retry migration. cond_resched() is required here;
1605 * otherwise, we might deadlock against cpu_stop trying to
1606 * bring down the CPU on non-preemptive kernel.
1607 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001608 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001609 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001610 }
1611}
1612
Tejun Heoc34056a2010-06-29 10:07:11 +02001613static struct worker *alloc_worker(void)
1614{
1615 struct worker *worker;
1616
1617 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001618 if (worker) {
1619 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001620 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heoe22bee72010-06-29 10:07:14 +02001621 /* on creation a worker is in !idle && prep state */
1622 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001623 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001624 return worker;
1625}
1626
1627/**
1628 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001629 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001630 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001631 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001632 * can be started by calling start_worker() or destroyed using
1633 * destroy_worker().
1634 *
1635 * CONTEXT:
1636 * Might sleep. Does GFP_KERNEL allocations.
1637 *
1638 * RETURNS:
1639 * Pointer to the newly created worker.
1640 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001641static struct worker *create_worker(struct worker_pool *pool)
Tejun Heoc34056a2010-06-29 10:07:11 +02001642{
Tejun Heo7a4e3442013-03-12 11:30:00 -07001643 const char *pri = pool->attrs->nice < 0 ? "H" : "";
Tejun Heoc34056a2010-06-29 10:07:11 +02001644 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001645 int id = -1;
Tejun Heoc34056a2010-06-29 10:07:11 +02001646
Tejun Heocd549682013-03-13 19:47:39 -07001647 lockdep_assert_held(&pool->manager_mutex);
1648
Tejun Heo822d8402013-03-19 13:45:21 -07001649 /*
1650 * ID is needed to determine kthread name. Allocate ID first
1651 * without installing the pointer.
1652 */
1653 idr_preload(GFP_KERNEL);
Tejun Heod565ed62013-01-24 11:01:33 -08001654 spin_lock_irq(&pool->lock);
Tejun Heo822d8402013-03-19 13:45:21 -07001655
1656 id = idr_alloc(&pool->worker_idr, NULL, 0, 0, GFP_NOWAIT);
1657
Tejun Heod565ed62013-01-24 11:01:33 -08001658 spin_unlock_irq(&pool->lock);
Tejun Heo822d8402013-03-19 13:45:21 -07001659 idr_preload_end();
1660 if (id < 0)
1661 goto fail;
Tejun Heoc34056a2010-06-29 10:07:11 +02001662
1663 worker = alloc_worker();
1664 if (!worker)
1665 goto fail;
1666
Tejun Heobd7bdd42012-07-12 14:46:37 -07001667 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001668 worker->id = id;
1669
Tejun Heo29c91e92013-03-12 11:30:03 -07001670 if (pool->cpu >= 0)
Eric Dumazet94dcf292011-03-22 16:30:45 -07001671 worker->task = kthread_create_on_node(worker_thread,
Tejun Heoec22ca52013-01-24 11:01:33 -08001672 worker, cpu_to_node(pool->cpu),
Tejun Heod84ff052013-03-12 11:29:59 -07001673 "kworker/%d:%d%s", pool->cpu, id, pri);
Tejun Heof3421792010-07-02 10:03:51 +02001674 else
1675 worker->task = kthread_create(worker_thread, worker,
Tejun Heoac6104c2013-03-12 11:30:03 -07001676 "kworker/u%d:%d%s",
1677 pool->id, id, pri);
Tejun Heoc34056a2010-06-29 10:07:11 +02001678 if (IS_ERR(worker->task))
1679 goto fail;
1680
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001681 /*
1682 * set_cpus_allowed_ptr() will fail if the cpumask doesn't have any
1683 * online CPUs. It'll be re-applied when any of the CPUs come up.
1684 */
Tejun Heo7a4e3442013-03-12 11:30:00 -07001685 set_user_nice(worker->task, pool->attrs->nice);
1686 set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask);
Tejun Heo32704762012-07-13 22:16:45 -07001687
Tejun Heo14a40ff2013-03-19 13:45:20 -07001688 /* prevent userland from meddling with cpumask of workqueue workers */
1689 worker->task->flags |= PF_NO_SETAFFINITY;
Tejun Heo7a4e3442013-03-12 11:30:00 -07001690
1691 /*
1692 * The caller is responsible for ensuring %POOL_DISASSOCIATED
1693 * remains stable across this function. See the comments above the
1694 * flag definition for details.
1695 */
1696 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001697 worker->flags |= WORKER_UNBOUND;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001698
Tejun Heo822d8402013-03-19 13:45:21 -07001699 /* successful, commit the pointer to idr */
1700 spin_lock_irq(&pool->lock);
1701 idr_replace(&pool->worker_idr, worker, worker->id);
1702 spin_unlock_irq(&pool->lock);
1703
Tejun Heoc34056a2010-06-29 10:07:11 +02001704 return worker;
Tejun Heo822d8402013-03-19 13:45:21 -07001705
Tejun Heoc34056a2010-06-29 10:07:11 +02001706fail:
1707 if (id >= 0) {
Tejun Heod565ed62013-01-24 11:01:33 -08001708 spin_lock_irq(&pool->lock);
Tejun Heo822d8402013-03-19 13:45:21 -07001709 idr_remove(&pool->worker_idr, id);
Tejun Heod565ed62013-01-24 11:01:33 -08001710 spin_unlock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001711 }
1712 kfree(worker);
1713 return NULL;
1714}
1715
1716/**
1717 * start_worker - start a newly created worker
1718 * @worker: worker to start
1719 *
Tejun Heo706026c2013-01-24 11:01:34 -08001720 * Make the pool aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001721 *
1722 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001723 * spin_lock_irq(pool->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001724 */
1725static void start_worker(struct worker *worker)
1726{
Tejun Heocb444762010-07-02 10:03:50 +02001727 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001728 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001729 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001730 wake_up_process(worker->task);
1731}
1732
1733/**
Tejun Heoebf44d12013-03-13 19:47:39 -07001734 * create_and_start_worker - create and start a worker for a pool
1735 * @pool: the target pool
1736 *
Tejun Heocd549682013-03-13 19:47:39 -07001737 * Grab the managership of @pool and create and start a new worker for it.
Tejun Heoebf44d12013-03-13 19:47:39 -07001738 */
1739static int create_and_start_worker(struct worker_pool *pool)
1740{
1741 struct worker *worker;
1742
Tejun Heocd549682013-03-13 19:47:39 -07001743 mutex_lock(&pool->manager_mutex);
1744
Tejun Heoebf44d12013-03-13 19:47:39 -07001745 worker = create_worker(pool);
1746 if (worker) {
1747 spin_lock_irq(&pool->lock);
1748 start_worker(worker);
1749 spin_unlock_irq(&pool->lock);
1750 }
1751
Tejun Heocd549682013-03-13 19:47:39 -07001752 mutex_unlock(&pool->manager_mutex);
1753
Tejun Heoebf44d12013-03-13 19:47:39 -07001754 return worker ? 0 : -ENOMEM;
1755}
1756
1757/**
Tejun Heoc34056a2010-06-29 10:07:11 +02001758 * destroy_worker - destroy a workqueue worker
1759 * @worker: worker to be destroyed
1760 *
Tejun Heo706026c2013-01-24 11:01:34 -08001761 * Destroy @worker and adjust @pool stats accordingly.
Tejun Heoc8e55f32010-06-29 10:07:12 +02001762 *
1763 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001764 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001765 */
1766static void destroy_worker(struct worker *worker)
1767{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001768 struct worker_pool *pool = worker->pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001769
Tejun Heocd549682013-03-13 19:47:39 -07001770 lockdep_assert_held(&pool->manager_mutex);
1771 lockdep_assert_held(&pool->lock);
1772
Tejun Heoc34056a2010-06-29 10:07:11 +02001773 /* sanity check frenzy */
Tejun Heo6183c002013-03-12 11:29:57 -07001774 if (WARN_ON(worker->current_work) ||
1775 WARN_ON(!list_empty(&worker->scheduled)))
1776 return;
Tejun Heoc34056a2010-06-29 10:07:11 +02001777
Tejun Heoc8e55f32010-06-29 10:07:12 +02001778 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001779 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001780 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001781 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001782
1783 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001784 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001785
Tejun Heo822d8402013-03-19 13:45:21 -07001786 idr_remove(&pool->worker_idr, worker->id);
1787
Tejun Heod565ed62013-01-24 11:01:33 -08001788 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001789
Tejun Heoc34056a2010-06-29 10:07:11 +02001790 kthread_stop(worker->task);
1791 kfree(worker);
1792
Tejun Heod565ed62013-01-24 11:01:33 -08001793 spin_lock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001794}
1795
Tejun Heo63d95a92012-07-12 14:46:37 -07001796static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001797{
Tejun Heo63d95a92012-07-12 14:46:37 -07001798 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001799
Tejun Heod565ed62013-01-24 11:01:33 -08001800 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001801
Tejun Heo63d95a92012-07-12 14:46:37 -07001802 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001803 struct worker *worker;
1804 unsigned long expires;
1805
1806 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001807 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001808 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1809
1810 if (time_before(jiffies, expires))
Tejun Heo63d95a92012-07-12 14:46:37 -07001811 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001812 else {
1813 /* it's been idle for too long, wake up manager */
Tejun Heo11ebea52012-07-12 14:46:37 -07001814 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -07001815 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001816 }
1817 }
1818
Tejun Heod565ed62013-01-24 11:01:33 -08001819 spin_unlock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001820}
1821
Tejun Heo493a1722013-03-12 11:29:59 -07001822static void send_mayday(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001823{
Tejun Heo112202d2013-02-13 19:29:12 -08001824 struct pool_workqueue *pwq = get_work_pwq(work);
1825 struct workqueue_struct *wq = pwq->wq;
Tejun Heo493a1722013-03-12 11:29:59 -07001826
Tejun Heo2e109a22013-03-13 19:47:40 -07001827 lockdep_assert_held(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001828
Tejun Heo493008a2013-03-12 11:30:03 -07001829 if (!wq->rescuer)
Tejun Heo493a1722013-03-12 11:29:59 -07001830 return;
Tejun Heoe22bee72010-06-29 10:07:14 +02001831
1832 /* mayday mayday mayday */
Tejun Heo493a1722013-03-12 11:29:59 -07001833 if (list_empty(&pwq->mayday_node)) {
1834 list_add_tail(&pwq->mayday_node, &wq->maydays);
Tejun Heoe22bee72010-06-29 10:07:14 +02001835 wake_up_process(wq->rescuer->task);
Tejun Heo493a1722013-03-12 11:29:59 -07001836 }
Tejun Heoe22bee72010-06-29 10:07:14 +02001837}
1838
Tejun Heo706026c2013-01-24 11:01:34 -08001839static void pool_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001840{
Tejun Heo63d95a92012-07-12 14:46:37 -07001841 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001842 struct work_struct *work;
1843
Tejun Heo2e109a22013-03-13 19:47:40 -07001844 spin_lock_irq(&wq_mayday_lock); /* for wq->maydays */
Tejun Heo493a1722013-03-12 11:29:59 -07001845 spin_lock(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001846
Tejun Heo63d95a92012-07-12 14:46:37 -07001847 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001848 /*
1849 * We've been trying to create a new worker but
1850 * haven't been successful. We might be hitting an
1851 * allocation deadlock. Send distress signals to
1852 * rescuers.
1853 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001854 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001855 send_mayday(work);
1856 }
1857
Tejun Heo493a1722013-03-12 11:29:59 -07001858 spin_unlock(&pool->lock);
Tejun Heo2e109a22013-03-13 19:47:40 -07001859 spin_unlock_irq(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001860
Tejun Heo63d95a92012-07-12 14:46:37 -07001861 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001862}
1863
1864/**
1865 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001866 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001867 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001868 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001869 * have at least one idle worker on return from this function. If
1870 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001871 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001872 * possible allocation deadlock.
1873 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001874 * On return, need_to_create_worker() is guaranteed to be %false and
1875 * may_start_working() %true.
Tejun Heoe22bee72010-06-29 10:07:14 +02001876 *
1877 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001878 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001879 * multiple times. Does GFP_KERNEL allocations. Called only from
1880 * manager.
1881 *
1882 * RETURNS:
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001883 * %false if no action was taken and pool->lock stayed locked, %true
Tejun Heoe22bee72010-06-29 10:07:14 +02001884 * otherwise.
1885 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001886static bool maybe_create_worker(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001887__releases(&pool->lock)
1888__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001889{
Tejun Heo63d95a92012-07-12 14:46:37 -07001890 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001891 return false;
1892restart:
Tejun Heod565ed62013-01-24 11:01:33 -08001893 spin_unlock_irq(&pool->lock);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001894
Tejun Heoe22bee72010-06-29 10:07:14 +02001895 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07001896 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001897
1898 while (true) {
1899 struct worker *worker;
1900
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001901 worker = create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001902 if (worker) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001903 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001904 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001905 start_worker(worker);
Tejun Heo6183c002013-03-12 11:29:57 -07001906 if (WARN_ON_ONCE(need_to_create_worker(pool)))
1907 goto restart;
Tejun Heoe22bee72010-06-29 10:07:14 +02001908 return true;
1909 }
1910
Tejun Heo63d95a92012-07-12 14:46:37 -07001911 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001912 break;
1913
Tejun Heoe22bee72010-06-29 10:07:14 +02001914 __set_current_state(TASK_INTERRUPTIBLE);
1915 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001916
Tejun Heo63d95a92012-07-12 14:46:37 -07001917 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001918 break;
1919 }
1920
Tejun Heo63d95a92012-07-12 14:46:37 -07001921 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001922 spin_lock_irq(&pool->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07001923 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001924 goto restart;
1925 return true;
1926}
1927
1928/**
1929 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo63d95a92012-07-12 14:46:37 -07001930 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001931 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001932 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02001933 * IDLE_WORKER_TIMEOUT.
1934 *
1935 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001936 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001937 * multiple times. Called only from manager.
1938 *
1939 * RETURNS:
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001940 * %false if no action was taken and pool->lock stayed locked, %true
Tejun Heoe22bee72010-06-29 10:07:14 +02001941 * otherwise.
1942 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001943static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001944{
1945 bool ret = false;
1946
Tejun Heo63d95a92012-07-12 14:46:37 -07001947 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001948 struct worker *worker;
1949 unsigned long expires;
1950
Tejun Heo63d95a92012-07-12 14:46:37 -07001951 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001952 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1953
1954 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001955 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001956 break;
1957 }
1958
1959 destroy_worker(worker);
1960 ret = true;
1961 }
1962
1963 return ret;
1964}
1965
1966/**
1967 * manage_workers - manage worker pool
1968 * @worker: self
1969 *
Tejun Heo706026c2013-01-24 11:01:34 -08001970 * Assume the manager role and manage the worker pool @worker belongs
Tejun Heoe22bee72010-06-29 10:07:14 +02001971 * to. At any given time, there can be only zero or one manager per
Tejun Heo706026c2013-01-24 11:01:34 -08001972 * pool. The exclusion is handled automatically by this function.
Tejun Heoe22bee72010-06-29 10:07:14 +02001973 *
1974 * The caller can safely start processing works on false return. On
1975 * true return, it's guaranteed that need_to_create_worker() is false
1976 * and may_start_working() is true.
1977 *
1978 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001979 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001980 * multiple times. Does GFP_KERNEL allocations.
1981 *
1982 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08001983 * spin_lock_irq(pool->lock) which may be released and regrabbed
1984 * multiple times. Does GFP_KERNEL allocations.
Tejun Heoe22bee72010-06-29 10:07:14 +02001985 */
1986static bool manage_workers(struct worker *worker)
1987{
Tejun Heo63d95a92012-07-12 14:46:37 -07001988 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001989 bool ret = false;
1990
Tejun Heobc3a1af2013-03-13 19:47:39 -07001991 /*
1992 * Managership is governed by two mutexes - manager_arb and
1993 * manager_mutex. manager_arb handles arbitration of manager role.
1994 * Anyone who successfully grabs manager_arb wins the arbitration
1995 * and becomes the manager. mutex_trylock() on pool->manager_arb
1996 * failure while holding pool->lock reliably indicates that someone
1997 * else is managing the pool and the worker which failed trylock
1998 * can proceed to executing work items. This means that anyone
1999 * grabbing manager_arb is responsible for actually performing
2000 * manager duties. If manager_arb is grabbed and released without
2001 * actual management, the pool may stall indefinitely.
2002 *
2003 * manager_mutex is used for exclusion of actual management
2004 * operations. The holder of manager_mutex can be sure that none
2005 * of management operations, including creation and destruction of
2006 * workers, won't take place until the mutex is released. Because
2007 * manager_mutex doesn't interfere with manager role arbitration,
2008 * it is guaranteed that the pool's management, while may be
2009 * delayed, won't be disturbed by someone else grabbing
2010 * manager_mutex.
2011 */
Tejun Heo34a06bd2013-03-12 11:30:00 -07002012 if (!mutex_trylock(&pool->manager_arb))
Tejun Heoe22bee72010-06-29 10:07:14 +02002013 return ret;
2014
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002015 /*
Tejun Heobc3a1af2013-03-13 19:47:39 -07002016 * With manager arbitration won, manager_mutex would be free in
2017 * most cases. trylock first without dropping @pool->lock.
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002018 */
Tejun Heobc3a1af2013-03-13 19:47:39 -07002019 if (unlikely(!mutex_trylock(&pool->manager_mutex))) {
Tejun Heod565ed62013-01-24 11:01:33 -08002020 spin_unlock_irq(&pool->lock);
Tejun Heobc3a1af2013-03-13 19:47:39 -07002021 mutex_lock(&pool->manager_mutex);
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002022 ret = true;
2023 }
2024
Tejun Heo11ebea52012-07-12 14:46:37 -07002025 pool->flags &= ~POOL_MANAGE_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02002026
2027 /*
2028 * Destroy and then create so that may_start_working() is true
2029 * on return.
2030 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002031 ret |= maybe_destroy_workers(pool);
2032 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002033
Tejun Heobc3a1af2013-03-13 19:47:39 -07002034 mutex_unlock(&pool->manager_mutex);
Tejun Heo34a06bd2013-03-12 11:30:00 -07002035 mutex_unlock(&pool->manager_arb);
Tejun Heoe22bee72010-06-29 10:07:14 +02002036 return ret;
2037}
2038
Tejun Heoa62428c2010-06-29 10:07:10 +02002039/**
2040 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02002041 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02002042 * @work: work to process
2043 *
2044 * Process @work. This function contains all the logics necessary to
2045 * process a single work including synchronization against and
2046 * interaction with other workers on the same cpu, queueing and
2047 * flushing. As long as context requirement is met, any worker can
2048 * call this function to process a work.
2049 *
2050 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002051 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02002052 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002053static void process_one_work(struct worker *worker, struct work_struct *work)
Tejun Heod565ed62013-01-24 11:01:33 -08002054__releases(&pool->lock)
2055__acquires(&pool->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02002056{
Tejun Heo112202d2013-02-13 19:29:12 -08002057 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002058 struct worker_pool *pool = worker->pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002059 bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02002060 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02002061 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02002062#ifdef CONFIG_LOCKDEP
2063 /*
2064 * It is permissible to free the struct work_struct from
2065 * inside the function that is called from it, this we need to
2066 * take into account for lockdep too. To avoid bogus "held
2067 * lock freed" warnings as well as problems when looking into
2068 * work->lockdep_map, make a copy and use that here.
2069 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07002070 struct lockdep_map lockdep_map;
2071
2072 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002073#endif
Tejun Heo6fec10a2012-07-22 10:16:34 -07002074 /*
2075 * Ensure we're on the correct CPU. DISASSOCIATED test is
2076 * necessary to avoid spurious warnings from rescuers servicing the
Tejun Heo24647572013-01-24 11:01:33 -08002077 * unbound or a disassociated pool.
Tejun Heo6fec10a2012-07-22 10:16:34 -07002078 */
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002079 WARN_ON_ONCE(!(worker->flags & WORKER_UNBOUND) &&
Tejun Heo24647572013-01-24 11:01:33 -08002080 !(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heoec22ca52013-01-24 11:01:33 -08002081 raw_smp_processor_id() != pool->cpu);
Tejun Heo25511a42012-07-17 12:39:27 -07002082
Tejun Heo7e116292010-06-29 10:07:13 +02002083 /*
2084 * A single work shouldn't be executed concurrently by
2085 * multiple workers on a single cpu. Check whether anyone is
2086 * already processing the work. If so, defer the work to the
2087 * currently executing one.
2088 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002089 collision = find_worker_executing_work(pool, work);
Tejun Heo7e116292010-06-29 10:07:13 +02002090 if (unlikely(collision)) {
2091 move_linked_works(work, &collision->scheduled, NULL);
2092 return;
2093 }
2094
Tejun Heo8930cab2012-08-03 10:30:45 -07002095 /* claim and dequeue */
Tejun Heoa62428c2010-06-29 10:07:10 +02002096 debug_work_deactivate(work);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002097 hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work);
Tejun Heoc34056a2010-06-29 10:07:11 +02002098 worker->current_work = work;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002099 worker->current_func = work->func;
Tejun Heo112202d2013-02-13 19:29:12 -08002100 worker->current_pwq = pwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002101 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002102
Tejun Heoa62428c2010-06-29 10:07:10 +02002103 list_del_init(&work->entry);
2104
Tejun Heo649027d2010-06-29 10:07:14 +02002105 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02002106 * CPU intensive works don't participate in concurrency
2107 * management. They're the scheduler's responsibility.
2108 */
2109 if (unlikely(cpu_intensive))
2110 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
2111
Tejun Heo974271c2012-07-12 14:46:37 -07002112 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002113 * Unbound pool isn't concurrency managed and work items should be
Tejun Heo974271c2012-07-12 14:46:37 -07002114 * executed ASAP. Wake up another worker if necessary.
2115 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002116 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
2117 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07002118
Tejun Heo8930cab2012-08-03 10:30:45 -07002119 /*
Tejun Heo7c3eed52013-01-24 11:01:33 -08002120 * Record the last pool and clear PENDING which should be the last
Tejun Heod565ed62013-01-24 11:01:33 -08002121 * update to @work. Also, do this inside @pool->lock so that
Tejun Heo23657bb2012-08-13 17:08:19 -07002122 * PENDING and queued state changes happen together while IRQ is
2123 * disabled.
Tejun Heo8930cab2012-08-03 10:30:45 -07002124 */
Tejun Heo7c3eed52013-01-24 11:01:33 -08002125 set_work_pool_and_clear_pending(work, pool->id);
Tejun Heoa62428c2010-06-29 10:07:10 +02002126
Tejun Heod565ed62013-01-24 11:01:33 -08002127 spin_unlock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002128
Tejun Heo112202d2013-02-13 19:29:12 -08002129 lock_map_acquire_read(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002130 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002131 trace_workqueue_execute_start(work);
Tejun Heoa2c1c572012-12-18 10:35:02 -08002132 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002133 /*
2134 * While we must be careful to not use "work" after this, the trace
2135 * point will only record its address.
2136 */
2137 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002138 lock_map_release(&lockdep_map);
Tejun Heo112202d2013-02-13 19:29:12 -08002139 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002140
2141 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Valentin Ilie044c7822012-08-19 00:52:42 +03002142 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
2143 " last function: %pf\n",
Tejun Heoa2c1c572012-12-18 10:35:02 -08002144 current->comm, preempt_count(), task_pid_nr(current),
2145 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02002146 debug_show_held_locks(current);
2147 dump_stack();
2148 }
2149
Tejun Heod565ed62013-01-24 11:01:33 -08002150 spin_lock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002151
Tejun Heofb0e7be2010-06-29 10:07:15 +02002152 /* clear cpu intensive status */
2153 if (unlikely(cpu_intensive))
2154 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2155
Tejun Heoa62428c2010-06-29 10:07:10 +02002156 /* we're done with it, release */
Sasha Levin42f85702012-12-17 10:01:23 -05002157 hash_del(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002158 worker->current_work = NULL;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002159 worker->current_func = NULL;
Tejun Heo112202d2013-02-13 19:29:12 -08002160 worker->current_pwq = NULL;
2161 pwq_dec_nr_in_flight(pwq, work_color);
Tejun Heoa62428c2010-06-29 10:07:10 +02002162}
2163
Tejun Heoaffee4b2010-06-29 10:07:12 +02002164/**
2165 * process_scheduled_works - process scheduled works
2166 * @worker: self
2167 *
2168 * Process all scheduled works. Please note that the scheduled list
2169 * may change while processing a work, so this function repeatedly
2170 * fetches a work from the top and executes it.
2171 *
2172 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002173 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02002174 * multiple times.
2175 */
2176static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002178 while (!list_empty(&worker->scheduled)) {
2179 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002181 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183}
2184
Tejun Heo4690c4a2010-06-29 10:07:10 +02002185/**
2186 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02002187 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02002188 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002189 * The worker thread function. All workers belong to a worker_pool -
2190 * either a per-cpu one or dynamic unbound one. These workers process all
2191 * work items regardless of their specific target workqueue. The only
2192 * exception is work items which belong to workqueues with a rescuer which
2193 * will be explained in rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02002194 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002195static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196{
Tejun Heoc34056a2010-06-29 10:07:11 +02002197 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07002198 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Tejun Heoe22bee72010-06-29 10:07:14 +02002200 /* tell the scheduler that this is a workqueue worker */
2201 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002202woke_up:
Tejun Heod565ed62013-01-24 11:01:33 -08002203 spin_lock_irq(&pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Tejun Heoa9ab7752013-03-19 13:45:21 -07002205 /* am I supposed to die? */
2206 if (unlikely(worker->flags & WORKER_DIE)) {
Tejun Heod565ed62013-01-24 11:01:33 -08002207 spin_unlock_irq(&pool->lock);
Tejun Heoa9ab7752013-03-19 13:45:21 -07002208 WARN_ON_ONCE(!list_empty(&worker->entry));
2209 worker->task->flags &= ~PF_WQ_WORKER;
2210 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 }
2212
Tejun Heoc8e55f32010-06-29 10:07:12 +02002213 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002214recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002215 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002216 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002217 goto sleep;
2218
2219 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002220 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002221 goto recheck;
2222
Tejun Heoc8e55f32010-06-29 10:07:12 +02002223 /*
2224 * ->scheduled list can only be filled while a worker is
2225 * preparing to process a work or actually processing it.
2226 * Make sure nobody diddled with it while I was sleeping.
2227 */
Tejun Heo6183c002013-03-12 11:29:57 -07002228 WARN_ON_ONCE(!list_empty(&worker->scheduled));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002229
Tejun Heoe22bee72010-06-29 10:07:14 +02002230 /*
Tejun Heoa9ab7752013-03-19 13:45:21 -07002231 * Finish PREP stage. We're guaranteed to have at least one idle
2232 * worker or that someone else has already assumed the manager
2233 * role. This is where @worker starts participating in concurrency
2234 * management if applicable and concurrency management is restored
2235 * after being rebound. See rebind_workers() for details.
Tejun Heoe22bee72010-06-29 10:07:14 +02002236 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07002237 worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND);
Tejun Heoe22bee72010-06-29 10:07:14 +02002238
2239 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002240 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002241 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002242 struct work_struct, entry);
2243
2244 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2245 /* optimization path, not strictly necessary */
2246 process_one_work(worker, work);
2247 if (unlikely(!list_empty(&worker->scheduled)))
2248 process_scheduled_works(worker);
2249 } else {
2250 move_linked_works(work, &worker->scheduled, NULL);
2251 process_scheduled_works(worker);
2252 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002253 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002254
Tejun Heoe22bee72010-06-29 10:07:14 +02002255 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002256sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07002257 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002258 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002259
Tejun Heoc8e55f32010-06-29 10:07:12 +02002260 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002261 * pool->lock is held and there's no work to process and no need to
2262 * manage, sleep. Workers are woken up only while holding
2263 * pool->lock or from local cpu, so setting the current state
2264 * before releasing pool->lock is enough to prevent losing any
2265 * event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002266 */
2267 worker_enter_idle(worker);
2268 __set_current_state(TASK_INTERRUPTIBLE);
Tejun Heod565ed62013-01-24 11:01:33 -08002269 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02002270 schedule();
2271 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272}
2273
Tejun Heoe22bee72010-06-29 10:07:14 +02002274/**
2275 * rescuer_thread - the rescuer thread function
Tejun Heo111c2252013-01-17 17:16:24 -08002276 * @__rescuer: self
Tejun Heoe22bee72010-06-29 10:07:14 +02002277 *
2278 * Workqueue rescuer thread function. There's one rescuer for each
Tejun Heo493008a2013-03-12 11:30:03 -07002279 * workqueue which has WQ_MEM_RECLAIM set.
Tejun Heoe22bee72010-06-29 10:07:14 +02002280 *
Tejun Heo706026c2013-01-24 11:01:34 -08002281 * Regular work processing on a pool may block trying to create a new
Tejun Heoe22bee72010-06-29 10:07:14 +02002282 * worker which uses GFP_KERNEL allocation which has slight chance of
2283 * developing into deadlock if some works currently on the same queue
2284 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2285 * the problem rescuer solves.
2286 *
Tejun Heo706026c2013-01-24 11:01:34 -08002287 * When such condition is possible, the pool summons rescuers of all
2288 * workqueues which have works queued on the pool and let them process
Tejun Heoe22bee72010-06-29 10:07:14 +02002289 * those works so that forward progress can be guaranteed.
2290 *
2291 * This should happen rarely.
2292 */
Tejun Heo111c2252013-01-17 17:16:24 -08002293static int rescuer_thread(void *__rescuer)
Tejun Heoe22bee72010-06-29 10:07:14 +02002294{
Tejun Heo111c2252013-01-17 17:16:24 -08002295 struct worker *rescuer = __rescuer;
2296 struct workqueue_struct *wq = rescuer->rescue_wq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002297 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heoe22bee72010-06-29 10:07:14 +02002298
2299 set_user_nice(current, RESCUER_NICE_LEVEL);
Tejun Heo111c2252013-01-17 17:16:24 -08002300
2301 /*
2302 * Mark rescuer as worker too. As WORKER_PREP is never cleared, it
2303 * doesn't participate in concurrency management.
2304 */
2305 rescuer->task->flags |= PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002306repeat:
2307 set_current_state(TASK_INTERRUPTIBLE);
2308
Mike Galbraith412d32e2012-11-28 07:17:18 +01002309 if (kthread_should_stop()) {
2310 __set_current_state(TASK_RUNNING);
Tejun Heo111c2252013-01-17 17:16:24 -08002311 rescuer->task->flags &= ~PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002312 return 0;
Mike Galbraith412d32e2012-11-28 07:17:18 +01002313 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002314
Tejun Heo493a1722013-03-12 11:29:59 -07002315 /* see whether any pwq is asking for help */
Tejun Heo2e109a22013-03-13 19:47:40 -07002316 spin_lock_irq(&wq_mayday_lock);
Tejun Heo493a1722013-03-12 11:29:59 -07002317
2318 while (!list_empty(&wq->maydays)) {
2319 struct pool_workqueue *pwq = list_first_entry(&wq->maydays,
2320 struct pool_workqueue, mayday_node);
Tejun Heo112202d2013-02-13 19:29:12 -08002321 struct worker_pool *pool = pwq->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002322 struct work_struct *work, *n;
2323
2324 __set_current_state(TASK_RUNNING);
Tejun Heo493a1722013-03-12 11:29:59 -07002325 list_del_init(&pwq->mayday_node);
2326
Tejun Heo2e109a22013-03-13 19:47:40 -07002327 spin_unlock_irq(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002328
2329 /* migrate to the target cpu if possible */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08002330 worker_maybe_bind_and_lock(pool);
Lai Jiangshanb3104102013-02-19 12:17:02 -08002331 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002332
2333 /*
2334 * Slurp in all works issued via this workqueue and
2335 * process'em.
2336 */
Tejun Heo6183c002013-03-12 11:29:57 -07002337 WARN_ON_ONCE(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002338 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heo112202d2013-02-13 19:29:12 -08002339 if (get_work_pwq(work) == pwq)
Tejun Heoe22bee72010-06-29 10:07:14 +02002340 move_linked_works(work, scheduled, &n);
2341
2342 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002343
2344 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002345 * Leave this pool. If keep_working() is %true, notify a
Tejun Heo75769582011-02-14 14:04:46 +01002346 * regular worker; otherwise, we end up with 0 concurrency
2347 * and stalling the execution.
2348 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002349 if (keep_working(pool))
2350 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002351
Lai Jiangshanb3104102013-02-19 12:17:02 -08002352 rescuer->pool = NULL;
Tejun Heo493a1722013-03-12 11:29:59 -07002353 spin_unlock(&pool->lock);
Tejun Heo2e109a22013-03-13 19:47:40 -07002354 spin_lock(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002355 }
2356
Tejun Heo2e109a22013-03-13 19:47:40 -07002357 spin_unlock_irq(&wq_mayday_lock);
Tejun Heo493a1722013-03-12 11:29:59 -07002358
Tejun Heo111c2252013-01-17 17:16:24 -08002359 /* rescuers should never participate in concurrency management */
2360 WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
Tejun Heoe22bee72010-06-29 10:07:14 +02002361 schedule();
2362 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363}
2364
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002365struct wq_barrier {
2366 struct work_struct work;
2367 struct completion done;
2368};
2369
2370static void wq_barrier_func(struct work_struct *work)
2371{
2372 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2373 complete(&barr->done);
2374}
2375
Tejun Heo4690c4a2010-06-29 10:07:10 +02002376/**
2377 * insert_wq_barrier - insert a barrier work
Tejun Heo112202d2013-02-13 19:29:12 -08002378 * @pwq: pwq to insert barrier into
Tejun Heo4690c4a2010-06-29 10:07:10 +02002379 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002380 * @target: target work to attach @barr to
2381 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002382 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002383 * @barr is linked to @target such that @barr is completed only after
2384 * @target finishes execution. Please note that the ordering
2385 * guarantee is observed only with respect to @target and on the local
2386 * cpu.
2387 *
2388 * Currently, a queued barrier can't be canceled. This is because
2389 * try_to_grab_pending() can't determine whether the work to be
2390 * grabbed is at the head of the queue and thus can't clear LINKED
2391 * flag of the previous work while there must be a valid next work
2392 * after a work with LINKED flag set.
2393 *
2394 * Note that when @worker is non-NULL, @target may be modified
Tejun Heo112202d2013-02-13 19:29:12 -08002395 * underneath us, so we can't reliably determine pwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002396 *
2397 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002398 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002399 */
Tejun Heo112202d2013-02-13 19:29:12 -08002400static void insert_wq_barrier(struct pool_workqueue *pwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002401 struct wq_barrier *barr,
2402 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002403{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002404 struct list_head *head;
2405 unsigned int linked = 0;
2406
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002407 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002408 * debugobject calls are safe here even with pool->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002409 * as we know for sure that this will not trigger any of the
2410 * checks and call back into the fixup functions where we
2411 * might deadlock.
2412 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002413 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002414 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002415 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002416
Tejun Heoaffee4b2010-06-29 10:07:12 +02002417 /*
2418 * If @target is currently being executed, schedule the
2419 * barrier to the worker; otherwise, put it after @target.
2420 */
2421 if (worker)
2422 head = worker->scheduled.next;
2423 else {
2424 unsigned long *bits = work_data_bits(target);
2425
2426 head = target->entry.next;
2427 /* there can already be other linked works, inherit and set */
2428 linked = *bits & WORK_STRUCT_LINKED;
2429 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2430 }
2431
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002432 debug_work_activate(&barr->work);
Tejun Heo112202d2013-02-13 19:29:12 -08002433 insert_work(pwq, &barr->work, head,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002434 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002435}
2436
Tejun Heo73f53c42010-06-29 10:07:11 +02002437/**
Tejun Heo112202d2013-02-13 19:29:12 -08002438 * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing
Tejun Heo73f53c42010-06-29 10:07:11 +02002439 * @wq: workqueue being flushed
2440 * @flush_color: new flush color, < 0 for no-op
2441 * @work_color: new work color, < 0 for no-op
2442 *
Tejun Heo112202d2013-02-13 19:29:12 -08002443 * Prepare pwqs for workqueue flushing.
Tejun Heo73f53c42010-06-29 10:07:11 +02002444 *
Tejun Heo112202d2013-02-13 19:29:12 -08002445 * If @flush_color is non-negative, flush_color on all pwqs should be
2446 * -1. If no pwq has in-flight commands at the specified color, all
2447 * pwq->flush_color's stay at -1 and %false is returned. If any pwq
2448 * has in flight commands, its pwq->flush_color is set to
2449 * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq
Tejun Heo73f53c42010-06-29 10:07:11 +02002450 * wakeup logic is armed and %true is returned.
2451 *
2452 * The caller should have initialized @wq->first_flusher prior to
2453 * calling this function with non-negative @flush_color. If
2454 * @flush_color is negative, no flush color update is done and %false
2455 * is returned.
2456 *
Tejun Heo112202d2013-02-13 19:29:12 -08002457 * If @work_color is non-negative, all pwqs should have the same
Tejun Heo73f53c42010-06-29 10:07:11 +02002458 * work_color which is previous to @work_color and all will be
2459 * advanced to @work_color.
2460 *
2461 * CONTEXT:
2462 * mutex_lock(wq->flush_mutex).
2463 *
2464 * RETURNS:
2465 * %true if @flush_color >= 0 and there's something to flush. %false
2466 * otherwise.
2467 */
Tejun Heo112202d2013-02-13 19:29:12 -08002468static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
Tejun Heo73f53c42010-06-29 10:07:11 +02002469 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
Tejun Heo73f53c42010-06-29 10:07:11 +02002471 bool wait = false;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002472 struct pool_workqueue *pwq;
Oleg Nesterov14441962007-05-23 13:57:57 -07002473
Tejun Heo73f53c42010-06-29 10:07:11 +02002474 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002475 WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush));
Tejun Heo112202d2013-02-13 19:29:12 -08002476 atomic_set(&wq->nr_pwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002477 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002478
Tejun Heo76af4d92013-03-12 11:30:00 -07002479 local_irq_disable();
2480
Tejun Heo49e3cf42013-03-12 11:29:58 -07002481 for_each_pwq(pwq, wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08002482 struct worker_pool *pool = pwq->pool;
Tejun Heo73f53c42010-06-29 10:07:11 +02002483
Tejun Heo76af4d92013-03-12 11:30:00 -07002484 spin_lock(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002485
2486 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002487 WARN_ON_ONCE(pwq->flush_color != -1);
Tejun Heo73f53c42010-06-29 10:07:11 +02002488
Tejun Heo112202d2013-02-13 19:29:12 -08002489 if (pwq->nr_in_flight[flush_color]) {
2490 pwq->flush_color = flush_color;
2491 atomic_inc(&wq->nr_pwqs_to_flush);
Tejun Heo73f53c42010-06-29 10:07:11 +02002492 wait = true;
2493 }
2494 }
2495
2496 if (work_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002497 WARN_ON_ONCE(work_color != work_next_color(pwq->work_color));
Tejun Heo112202d2013-02-13 19:29:12 -08002498 pwq->work_color = work_color;
Tejun Heo73f53c42010-06-29 10:07:11 +02002499 }
2500
Tejun Heo76af4d92013-03-12 11:30:00 -07002501 spin_unlock(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002502 }
2503
Tejun Heo76af4d92013-03-12 11:30:00 -07002504 local_irq_enable();
2505
Tejun Heo112202d2013-02-13 19:29:12 -08002506 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush))
Tejun Heo73f53c42010-06-29 10:07:11 +02002507 complete(&wq->first_flusher->done);
2508
2509 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510}
2511
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002512/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002514 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002516 * This function sleeps until all work items which were queued on entry
2517 * have finished execution, but it is not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002519void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520{
Tejun Heo73f53c42010-06-29 10:07:11 +02002521 struct wq_flusher this_flusher = {
2522 .list = LIST_HEAD_INIT(this_flusher.list),
2523 .flush_color = -1,
2524 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2525 };
2526 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002527
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002528 lock_map_acquire(&wq->lockdep_map);
2529 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002530
2531 mutex_lock(&wq->flush_mutex);
2532
2533 /*
2534 * Start-to-wait phase
2535 */
2536 next_color = work_next_color(wq->work_color);
2537
2538 if (next_color != wq->flush_color) {
2539 /*
2540 * Color space is not full. The current work_color
2541 * becomes our flush_color and work_color is advanced
2542 * by one.
2543 */
Tejun Heo6183c002013-03-12 11:29:57 -07002544 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow));
Tejun Heo73f53c42010-06-29 10:07:11 +02002545 this_flusher.flush_color = wq->work_color;
2546 wq->work_color = next_color;
2547
2548 if (!wq->first_flusher) {
2549 /* no flush in progress, become the first flusher */
Tejun Heo6183c002013-03-12 11:29:57 -07002550 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002551
2552 wq->first_flusher = &this_flusher;
2553
Tejun Heo112202d2013-02-13 19:29:12 -08002554 if (!flush_workqueue_prep_pwqs(wq, wq->flush_color,
Tejun Heo73f53c42010-06-29 10:07:11 +02002555 wq->work_color)) {
2556 /* nothing to flush, done */
2557 wq->flush_color = next_color;
2558 wq->first_flusher = NULL;
2559 goto out_unlock;
2560 }
2561 } else {
2562 /* wait in queue */
Tejun Heo6183c002013-03-12 11:29:57 -07002563 WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002564 list_add_tail(&this_flusher.list, &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002565 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002566 }
2567 } else {
2568 /*
2569 * Oops, color space is full, wait on overflow queue.
2570 * The next flush completion will assign us
2571 * flush_color and transfer to flusher_queue.
2572 */
2573 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2574 }
2575
2576 mutex_unlock(&wq->flush_mutex);
2577
2578 wait_for_completion(&this_flusher.done);
2579
2580 /*
2581 * Wake-up-and-cascade phase
2582 *
2583 * First flushers are responsible for cascading flushes and
2584 * handling overflow. Non-first flushers can simply return.
2585 */
2586 if (wq->first_flusher != &this_flusher)
2587 return;
2588
2589 mutex_lock(&wq->flush_mutex);
2590
Tejun Heo4ce48b32010-07-02 10:03:51 +02002591 /* we might have raced, check again with mutex held */
2592 if (wq->first_flusher != &this_flusher)
2593 goto out_unlock;
2594
Tejun Heo73f53c42010-06-29 10:07:11 +02002595 wq->first_flusher = NULL;
2596
Tejun Heo6183c002013-03-12 11:29:57 -07002597 WARN_ON_ONCE(!list_empty(&this_flusher.list));
2598 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002599
2600 while (true) {
2601 struct wq_flusher *next, *tmp;
2602
2603 /* complete all the flushers sharing the current flush color */
2604 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2605 if (next->flush_color != wq->flush_color)
2606 break;
2607 list_del_init(&next->list);
2608 complete(&next->done);
2609 }
2610
Tejun Heo6183c002013-03-12 11:29:57 -07002611 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) &&
2612 wq->flush_color != work_next_color(wq->work_color));
Tejun Heo73f53c42010-06-29 10:07:11 +02002613
2614 /* this flush_color is finished, advance by one */
2615 wq->flush_color = work_next_color(wq->flush_color);
2616
2617 /* one color has been freed, handle overflow queue */
2618 if (!list_empty(&wq->flusher_overflow)) {
2619 /*
2620 * Assign the same color to all overflowed
2621 * flushers, advance work_color and append to
2622 * flusher_queue. This is the start-to-wait
2623 * phase for these overflowed flushers.
2624 */
2625 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2626 tmp->flush_color = wq->work_color;
2627
2628 wq->work_color = work_next_color(wq->work_color);
2629
2630 list_splice_tail_init(&wq->flusher_overflow,
2631 &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002632 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002633 }
2634
2635 if (list_empty(&wq->flusher_queue)) {
Tejun Heo6183c002013-03-12 11:29:57 -07002636 WARN_ON_ONCE(wq->flush_color != wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002637 break;
2638 }
2639
2640 /*
2641 * Need to flush more colors. Make the next flusher
Tejun Heo112202d2013-02-13 19:29:12 -08002642 * the new first flusher and arm pwqs.
Tejun Heo73f53c42010-06-29 10:07:11 +02002643 */
Tejun Heo6183c002013-03-12 11:29:57 -07002644 WARN_ON_ONCE(wq->flush_color == wq->work_color);
2645 WARN_ON_ONCE(wq->flush_color != next->flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002646
2647 list_del_init(&next->list);
2648 wq->first_flusher = next;
2649
Tejun Heo112202d2013-02-13 19:29:12 -08002650 if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1))
Tejun Heo73f53c42010-06-29 10:07:11 +02002651 break;
2652
2653 /*
2654 * Meh... this color is already done, clear first
2655 * flusher and repeat cascading.
2656 */
2657 wq->first_flusher = NULL;
2658 }
2659
2660out_unlock:
2661 mutex_unlock(&wq->flush_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662}
Dave Jonesae90dd52006-06-30 01:40:45 -04002663EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002665/**
2666 * drain_workqueue - drain a workqueue
2667 * @wq: workqueue to drain
2668 *
2669 * Wait until the workqueue becomes empty. While draining is in progress,
2670 * only chain queueing is allowed. IOW, only currently pending or running
2671 * work items on @wq can queue further work items on it. @wq is flushed
2672 * repeatedly until it becomes empty. The number of flushing is detemined
2673 * by the depth of chaining and should be relatively short. Whine if it
2674 * takes too long.
2675 */
2676void drain_workqueue(struct workqueue_struct *wq)
2677{
2678 unsigned int flush_cnt = 0;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002679 struct pool_workqueue *pwq;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002680
2681 /*
2682 * __queue_work() needs to test whether there are drainers, is much
2683 * hotter than drain_workqueue() and already looks at @wq->flags.
Tejun Heo618b01e2013-03-12 11:30:04 -07002684 * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers.
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002685 */
Tejun Heo5bcab332013-03-13 19:47:40 -07002686 mutex_lock(&wq_mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002687 if (!wq->nr_drainers++)
Tejun Heo618b01e2013-03-12 11:30:04 -07002688 wq->flags |= __WQ_DRAINING;
Tejun Heo5bcab332013-03-13 19:47:40 -07002689 mutex_unlock(&wq_mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002690reflush:
2691 flush_workqueue(wq);
2692
Tejun Heo76af4d92013-03-12 11:30:00 -07002693 local_irq_disable();
2694
Tejun Heo49e3cf42013-03-12 11:29:58 -07002695 for_each_pwq(pwq, wq) {
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002696 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002697
Tejun Heo76af4d92013-03-12 11:30:00 -07002698 spin_lock(&pwq->pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08002699 drained = !pwq->nr_active && list_empty(&pwq->delayed_works);
Tejun Heo76af4d92013-03-12 11:30:00 -07002700 spin_unlock(&pwq->pool->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002701
2702 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002703 continue;
2704
2705 if (++flush_cnt == 10 ||
2706 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002707 pr_warn("workqueue %s: drain_workqueue() isn't complete after %u tries\n",
Valentin Ilie044c7822012-08-19 00:52:42 +03002708 wq->name, flush_cnt);
Tejun Heo76af4d92013-03-12 11:30:00 -07002709
2710 local_irq_enable();
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002711 goto reflush;
2712 }
2713
Tejun Heo5bcab332013-03-13 19:47:40 -07002714 local_irq_enable();
2715
2716 mutex_lock(&wq_mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002717 if (!--wq->nr_drainers)
Tejun Heo618b01e2013-03-12 11:30:04 -07002718 wq->flags &= ~__WQ_DRAINING;
Tejun Heo5bcab332013-03-13 19:47:40 -07002719 mutex_unlock(&wq_mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002720}
2721EXPORT_SYMBOL_GPL(drain_workqueue);
2722
Tejun Heo606a5022012-08-20 14:51:23 -07002723static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
Tejun Heobaf59022010-09-16 10:42:16 +02002724{
2725 struct worker *worker = NULL;
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002726 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002727 struct pool_workqueue *pwq;
Tejun Heobaf59022010-09-16 10:42:16 +02002728
2729 might_sleep();
Tejun Heobaf59022010-09-16 10:42:16 +02002730
Tejun Heofa1b54e2013-03-12 11:30:00 -07002731 local_irq_disable();
2732 pool = get_work_pool(work);
2733 if (!pool) {
2734 local_irq_enable();
2735 return false;
2736 }
2737
2738 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08002739 /* see the comment in try_to_grab_pending() with the same code */
Tejun Heo112202d2013-02-13 19:29:12 -08002740 pwq = get_work_pwq(work);
2741 if (pwq) {
2742 if (unlikely(pwq->pool != pool))
Tejun Heobaf59022010-09-16 10:42:16 +02002743 goto already_gone;
Tejun Heo606a5022012-08-20 14:51:23 -07002744 } else {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002745 worker = find_worker_executing_work(pool, work);
Tejun Heobaf59022010-09-16 10:42:16 +02002746 if (!worker)
2747 goto already_gone;
Tejun Heo112202d2013-02-13 19:29:12 -08002748 pwq = worker->current_pwq;
Tejun Heo606a5022012-08-20 14:51:23 -07002749 }
Tejun Heobaf59022010-09-16 10:42:16 +02002750
Tejun Heo112202d2013-02-13 19:29:12 -08002751 insert_wq_barrier(pwq, barr, work, worker);
Tejun Heod565ed62013-01-24 11:01:33 -08002752 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002753
Tejun Heoe1594892011-01-09 23:32:15 +01002754 /*
2755 * If @max_active is 1 or rescuer is in use, flushing another work
2756 * item on the same workqueue may lead to deadlock. Make sure the
2757 * flusher is not running on the same workqueue by verifying write
2758 * access.
2759 */
Tejun Heo493008a2013-03-12 11:30:03 -07002760 if (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)
Tejun Heo112202d2013-02-13 19:29:12 -08002761 lock_map_acquire(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002762 else
Tejun Heo112202d2013-02-13 19:29:12 -08002763 lock_map_acquire_read(&pwq->wq->lockdep_map);
2764 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002765
Tejun Heobaf59022010-09-16 10:42:16 +02002766 return true;
2767already_gone:
Tejun Heod565ed62013-01-24 11:01:33 -08002768 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002769 return false;
2770}
2771
Oleg Nesterovdb700892008-07-25 01:47:49 -07002772/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002773 * flush_work - wait for a work to finish executing the last queueing instance
2774 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002775 *
Tejun Heo606a5022012-08-20 14:51:23 -07002776 * Wait until @work has finished execution. @work is guaranteed to be idle
2777 * on return if it hasn't been requeued since flush started.
Tejun Heo401a8d02010-09-16 10:36:00 +02002778 *
2779 * RETURNS:
2780 * %true if flush_work() waited for the work to finish execution,
2781 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002782 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002783bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002784{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002785 struct wq_barrier barr;
2786
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002787 lock_map_acquire(&work->lockdep_map);
2788 lock_map_release(&work->lockdep_map);
2789
Tejun Heo606a5022012-08-20 14:51:23 -07002790 if (start_flush_work(work, &barr)) {
Tejun Heobaf59022010-09-16 10:42:16 +02002791 wait_for_completion(&barr.done);
2792 destroy_work_on_stack(&barr.work);
2793 return true;
Tejun Heo606a5022012-08-20 14:51:23 -07002794 } else {
Tejun Heobaf59022010-09-16 10:42:16 +02002795 return false;
Tejun Heo606a5022012-08-20 14:51:23 -07002796 }
Oleg Nesterovdb700892008-07-25 01:47:49 -07002797}
2798EXPORT_SYMBOL_GPL(flush_work);
2799
Tejun Heo36e227d2012-08-03 10:30:46 -07002800static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
Tejun Heo401a8d02010-09-16 10:36:00 +02002801{
Tejun Heobbb68df2012-08-03 10:30:46 -07002802 unsigned long flags;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002803 int ret;
2804
2805 do {
Tejun Heobbb68df2012-08-03 10:30:46 -07002806 ret = try_to_grab_pending(work, is_dwork, &flags);
2807 /*
2808 * If someone else is canceling, wait for the same event it
2809 * would be waiting for before retrying.
2810 */
2811 if (unlikely(ret == -ENOENT))
Tejun Heo606a5022012-08-20 14:51:23 -07002812 flush_work(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002813 } while (unlikely(ret < 0));
2814
Tejun Heobbb68df2012-08-03 10:30:46 -07002815 /* tell other tasks trying to grab @work to back off */
2816 mark_work_canceling(work);
2817 local_irq_restore(flags);
2818
Tejun Heo606a5022012-08-20 14:51:23 -07002819 flush_work(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002820 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002821 return ret;
2822}
2823
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002824/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002825 * cancel_work_sync - cancel a work and wait for it to finish
2826 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002827 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002828 * Cancel @work and wait for its execution to finish. This function
2829 * can be used even if the work re-queues itself or migrates to
2830 * another workqueue. On return from this function, @work is
2831 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002832 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002833 * cancel_work_sync(&delayed_work->work) must not be used for
2834 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002835 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002836 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002837 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002838 *
2839 * RETURNS:
2840 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002841 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002842bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002843{
Tejun Heo36e227d2012-08-03 10:30:46 -07002844 return __cancel_work_timer(work, false);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002845}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002846EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002847
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002848/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002849 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2850 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002851 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002852 * Delayed timer is cancelled and the pending work is queued for
2853 * immediate execution. Like flush_work(), this function only
2854 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002855 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002856 * RETURNS:
2857 * %true if flush_work() waited for the work to finish execution,
2858 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002859 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002860bool flush_delayed_work(struct delayed_work *dwork)
2861{
Tejun Heo8930cab2012-08-03 10:30:45 -07002862 local_irq_disable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002863 if (del_timer_sync(&dwork->timer))
Lai Jiangshan60c057b2013-02-06 18:04:53 -08002864 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Tejun Heo8930cab2012-08-03 10:30:45 -07002865 local_irq_enable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002866 return flush_work(&dwork->work);
2867}
2868EXPORT_SYMBOL(flush_delayed_work);
2869
2870/**
Tejun Heo57b30ae2012-08-21 13:18:24 -07002871 * cancel_delayed_work - cancel a delayed work
2872 * @dwork: delayed_work to cancel
Tejun Heo09383492010-09-16 10:48:29 +02002873 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002874 * Kill off a pending delayed_work. Returns %true if @dwork was pending
2875 * and canceled; %false if wasn't pending. Note that the work callback
2876 * function may still be running on return, unless it returns %true and the
2877 * work doesn't re-arm itself. Explicitly flush or use
2878 * cancel_delayed_work_sync() to wait on it.
Tejun Heo09383492010-09-16 10:48:29 +02002879 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002880 * This function is safe to call from any context including IRQ handler.
Tejun Heo09383492010-09-16 10:48:29 +02002881 */
Tejun Heo57b30ae2012-08-21 13:18:24 -07002882bool cancel_delayed_work(struct delayed_work *dwork)
Tejun Heo09383492010-09-16 10:48:29 +02002883{
Tejun Heo57b30ae2012-08-21 13:18:24 -07002884 unsigned long flags;
2885 int ret;
2886
2887 do {
2888 ret = try_to_grab_pending(&dwork->work, true, &flags);
2889 } while (unlikely(ret == -EAGAIN));
2890
2891 if (unlikely(ret < 0))
2892 return false;
2893
Tejun Heo7c3eed52013-01-24 11:01:33 -08002894 set_work_pool_and_clear_pending(&dwork->work,
2895 get_work_pool_id(&dwork->work));
Tejun Heo57b30ae2012-08-21 13:18:24 -07002896 local_irq_restore(flags);
Dan Magenheimerc0158ca2012-10-18 16:31:37 -07002897 return ret;
Tejun Heo09383492010-09-16 10:48:29 +02002898}
Tejun Heo57b30ae2012-08-21 13:18:24 -07002899EXPORT_SYMBOL(cancel_delayed_work);
Tejun Heo09383492010-09-16 10:48:29 +02002900
2901/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002902 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2903 * @dwork: the delayed work cancel
2904 *
2905 * This is cancel_work_sync() for delayed works.
2906 *
2907 * RETURNS:
2908 * %true if @dwork was pending, %false otherwise.
2909 */
2910bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002911{
Tejun Heo36e227d2012-08-03 10:30:46 -07002912 return __cancel_work_timer(&dwork->work, true);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002913}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002914EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002916/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002917 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002918 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002919 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002920 * schedule_on_each_cpu() executes @func on each online CPU using the
2921 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002922 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002923 *
2924 * RETURNS:
2925 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002926 */
David Howells65f27f32006-11-22 14:55:48 +00002927int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002928{
2929 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002930 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002931
Andrew Mortonb6136772006-06-25 05:47:49 -07002932 works = alloc_percpu(struct work_struct);
2933 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002934 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002935
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002936 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002937
Christoph Lameter15316ba2006-01-08 01:00:43 -08002938 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002939 struct work_struct *work = per_cpu_ptr(works, cpu);
2940
2941 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002942 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002943 }
Tejun Heo93981802009-11-17 14:06:20 -08002944
2945 for_each_online_cpu(cpu)
2946 flush_work(per_cpu_ptr(works, cpu));
2947
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002948 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002949 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002950 return 0;
2951}
2952
Alan Sterneef6a7d2010-02-12 17:39:21 +09002953/**
2954 * flush_scheduled_work - ensure that any scheduled work has run to completion.
2955 *
2956 * Forces execution of the kernel-global workqueue and blocks until its
2957 * completion.
2958 *
2959 * Think twice before calling this function! It's very easy to get into
2960 * trouble if you don't take great care. Either of the following situations
2961 * will lead to deadlock:
2962 *
2963 * One of the work items currently on the workqueue needs to acquire
2964 * a lock held by your code or its caller.
2965 *
2966 * Your code is running in the context of a work routine.
2967 *
2968 * They will be detected by lockdep when they occur, but the first might not
2969 * occur very often. It depends on what work items are on the workqueue and
2970 * what locks they need, which you have no control over.
2971 *
2972 * In most situations flushing the entire workqueue is overkill; you merely
2973 * need to know that a particular work item isn't queued and isn't running.
2974 * In such cases you should use cancel_delayed_work_sync() or
2975 * cancel_work_sync() instead.
2976 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977void flush_scheduled_work(void)
2978{
Tejun Heod320c032010-06-29 10:07:14 +02002979 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980}
Dave Jonesae90dd52006-06-30 01:40:45 -04002981EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
2983/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06002984 * execute_in_process_context - reliably execute the routine with user context
2985 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06002986 * @ew: guaranteed storage for the execute work structure (must
2987 * be available when the work executes)
2988 *
2989 * Executes the function immediately if process context is available,
2990 * otherwise schedules the function for delayed execution.
2991 *
2992 * Returns: 0 - function was executed
2993 * 1 - function was scheduled for execution
2994 */
David Howells65f27f32006-11-22 14:55:48 +00002995int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06002996{
2997 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00002998 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06002999 return 0;
3000 }
3001
David Howells65f27f32006-11-22 14:55:48 +00003002 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003003 schedule_work(&ew->work);
3004
3005 return 1;
3006}
3007EXPORT_SYMBOL_GPL(execute_in_process_context);
3008
Tejun Heo226223a2013-03-12 11:30:05 -07003009#ifdef CONFIG_SYSFS
3010/*
3011 * Workqueues with WQ_SYSFS flag set is visible to userland via
3012 * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the
3013 * following attributes.
3014 *
3015 * per_cpu RO bool : whether the workqueue is per-cpu or unbound
3016 * max_active RW int : maximum number of in-flight work items
3017 *
3018 * Unbound workqueues have the following extra attributes.
3019 *
3020 * id RO int : the associated pool ID
3021 * nice RW int : nice value of the workers
3022 * cpumask RW mask : bitmask of allowed CPUs for the workers
3023 */
3024struct wq_device {
3025 struct workqueue_struct *wq;
3026 struct device dev;
3027};
3028
3029static struct workqueue_struct *dev_to_wq(struct device *dev)
3030{
3031 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
3032
3033 return wq_dev->wq;
3034}
3035
3036static ssize_t wq_per_cpu_show(struct device *dev,
3037 struct device_attribute *attr, char *buf)
3038{
3039 struct workqueue_struct *wq = dev_to_wq(dev);
3040
3041 return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND));
3042}
3043
3044static ssize_t wq_max_active_show(struct device *dev,
3045 struct device_attribute *attr, char *buf)
3046{
3047 struct workqueue_struct *wq = dev_to_wq(dev);
3048
3049 return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active);
3050}
3051
3052static ssize_t wq_max_active_store(struct device *dev,
3053 struct device_attribute *attr,
3054 const char *buf, size_t count)
3055{
3056 struct workqueue_struct *wq = dev_to_wq(dev);
3057 int val;
3058
3059 if (sscanf(buf, "%d", &val) != 1 || val <= 0)
3060 return -EINVAL;
3061
3062 workqueue_set_max_active(wq, val);
3063 return count;
3064}
3065
3066static struct device_attribute wq_sysfs_attrs[] = {
3067 __ATTR(per_cpu, 0444, wq_per_cpu_show, NULL),
3068 __ATTR(max_active, 0644, wq_max_active_show, wq_max_active_store),
3069 __ATTR_NULL,
3070};
3071
3072static ssize_t wq_pool_id_show(struct device *dev,
3073 struct device_attribute *attr, char *buf)
3074{
3075 struct workqueue_struct *wq = dev_to_wq(dev);
3076 struct worker_pool *pool;
3077 int written;
3078
3079 rcu_read_lock_sched();
3080 pool = first_pwq(wq)->pool;
3081 written = scnprintf(buf, PAGE_SIZE, "%d\n", pool->id);
3082 rcu_read_unlock_sched();
3083
3084 return written;
3085}
3086
3087static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr,
3088 char *buf)
3089{
3090 struct workqueue_struct *wq = dev_to_wq(dev);
3091 int written;
3092
3093 rcu_read_lock_sched();
3094 written = scnprintf(buf, PAGE_SIZE, "%d\n",
3095 first_pwq(wq)->pool->attrs->nice);
3096 rcu_read_unlock_sched();
3097
3098 return written;
3099}
3100
3101/* prepare workqueue_attrs for sysfs store operations */
3102static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq)
3103{
3104 struct workqueue_attrs *attrs;
3105
3106 attrs = alloc_workqueue_attrs(GFP_KERNEL);
3107 if (!attrs)
3108 return NULL;
3109
3110 rcu_read_lock_sched();
3111 copy_workqueue_attrs(attrs, first_pwq(wq)->pool->attrs);
3112 rcu_read_unlock_sched();
3113 return attrs;
3114}
3115
3116static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr,
3117 const char *buf, size_t count)
3118{
3119 struct workqueue_struct *wq = dev_to_wq(dev);
3120 struct workqueue_attrs *attrs;
3121 int ret;
3122
3123 attrs = wq_sysfs_prep_attrs(wq);
3124 if (!attrs)
3125 return -ENOMEM;
3126
3127 if (sscanf(buf, "%d", &attrs->nice) == 1 &&
3128 attrs->nice >= -20 && attrs->nice <= 19)
3129 ret = apply_workqueue_attrs(wq, attrs);
3130 else
3131 ret = -EINVAL;
3132
3133 free_workqueue_attrs(attrs);
3134 return ret ?: count;
3135}
3136
3137static ssize_t wq_cpumask_show(struct device *dev,
3138 struct device_attribute *attr, char *buf)
3139{
3140 struct workqueue_struct *wq = dev_to_wq(dev);
3141 int written;
3142
3143 rcu_read_lock_sched();
3144 written = cpumask_scnprintf(buf, PAGE_SIZE,
3145 first_pwq(wq)->pool->attrs->cpumask);
3146 rcu_read_unlock_sched();
3147
3148 written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
3149 return written;
3150}
3151
3152static ssize_t wq_cpumask_store(struct device *dev,
3153 struct device_attribute *attr,
3154 const char *buf, size_t count)
3155{
3156 struct workqueue_struct *wq = dev_to_wq(dev);
3157 struct workqueue_attrs *attrs;
3158 int ret;
3159
3160 attrs = wq_sysfs_prep_attrs(wq);
3161 if (!attrs)
3162 return -ENOMEM;
3163
3164 ret = cpumask_parse(buf, attrs->cpumask);
3165 if (!ret)
3166 ret = apply_workqueue_attrs(wq, attrs);
3167
3168 free_workqueue_attrs(attrs);
3169 return ret ?: count;
3170}
3171
3172static struct device_attribute wq_sysfs_unbound_attrs[] = {
3173 __ATTR(pool_id, 0444, wq_pool_id_show, NULL),
3174 __ATTR(nice, 0644, wq_nice_show, wq_nice_store),
3175 __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store),
3176 __ATTR_NULL,
3177};
3178
3179static struct bus_type wq_subsys = {
3180 .name = "workqueue",
3181 .dev_attrs = wq_sysfs_attrs,
3182};
3183
3184static int __init wq_sysfs_init(void)
3185{
3186 return subsys_virtual_register(&wq_subsys, NULL);
3187}
3188core_initcall(wq_sysfs_init);
3189
3190static void wq_device_release(struct device *dev)
3191{
3192 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
3193
3194 kfree(wq_dev);
3195}
3196
3197/**
3198 * workqueue_sysfs_register - make a workqueue visible in sysfs
3199 * @wq: the workqueue to register
3200 *
3201 * Expose @wq in sysfs under /sys/bus/workqueue/devices.
3202 * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set
3203 * which is the preferred method.
3204 *
3205 * Workqueue user should use this function directly iff it wants to apply
3206 * workqueue_attrs before making the workqueue visible in sysfs; otherwise,
3207 * apply_workqueue_attrs() may race against userland updating the
3208 * attributes.
3209 *
3210 * Returns 0 on success, -errno on failure.
3211 */
3212int workqueue_sysfs_register(struct workqueue_struct *wq)
3213{
3214 struct wq_device *wq_dev;
3215 int ret;
3216
3217 /*
3218 * Adjusting max_active or creating new pwqs by applyting
3219 * attributes breaks ordering guarantee. Disallow exposing ordered
3220 * workqueues.
3221 */
3222 if (WARN_ON(wq->flags & __WQ_ORDERED))
3223 return -EINVAL;
3224
3225 wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL);
3226 if (!wq_dev)
3227 return -ENOMEM;
3228
3229 wq_dev->wq = wq;
3230 wq_dev->dev.bus = &wq_subsys;
3231 wq_dev->dev.init_name = wq->name;
3232 wq_dev->dev.release = wq_device_release;
3233
3234 /*
3235 * unbound_attrs are created separately. Suppress uevent until
3236 * everything is ready.
3237 */
3238 dev_set_uevent_suppress(&wq_dev->dev, true);
3239
3240 ret = device_register(&wq_dev->dev);
3241 if (ret) {
3242 kfree(wq_dev);
3243 wq->wq_dev = NULL;
3244 return ret;
3245 }
3246
3247 if (wq->flags & WQ_UNBOUND) {
3248 struct device_attribute *attr;
3249
3250 for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) {
3251 ret = device_create_file(&wq_dev->dev, attr);
3252 if (ret) {
3253 device_unregister(&wq_dev->dev);
3254 wq->wq_dev = NULL;
3255 return ret;
3256 }
3257 }
3258 }
3259
3260 kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD);
3261 return 0;
3262}
3263
3264/**
3265 * workqueue_sysfs_unregister - undo workqueue_sysfs_register()
3266 * @wq: the workqueue to unregister
3267 *
3268 * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister.
3269 */
3270static void workqueue_sysfs_unregister(struct workqueue_struct *wq)
3271{
3272 struct wq_device *wq_dev = wq->wq_dev;
3273
3274 if (!wq->wq_dev)
3275 return;
3276
3277 wq->wq_dev = NULL;
3278 device_unregister(&wq_dev->dev);
3279}
3280#else /* CONFIG_SYSFS */
3281static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { }
3282#endif /* CONFIG_SYSFS */
3283
Tejun Heo7a4e3442013-03-12 11:30:00 -07003284/**
3285 * free_workqueue_attrs - free a workqueue_attrs
3286 * @attrs: workqueue_attrs to free
3287 *
3288 * Undo alloc_workqueue_attrs().
3289 */
3290void free_workqueue_attrs(struct workqueue_attrs *attrs)
3291{
3292 if (attrs) {
3293 free_cpumask_var(attrs->cpumask);
3294 kfree(attrs);
3295 }
3296}
3297
3298/**
3299 * alloc_workqueue_attrs - allocate a workqueue_attrs
3300 * @gfp_mask: allocation mask to use
3301 *
3302 * Allocate a new workqueue_attrs, initialize with default settings and
3303 * return it. Returns NULL on failure.
3304 */
3305struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask)
3306{
3307 struct workqueue_attrs *attrs;
3308
3309 attrs = kzalloc(sizeof(*attrs), gfp_mask);
3310 if (!attrs)
3311 goto fail;
3312 if (!alloc_cpumask_var(&attrs->cpumask, gfp_mask))
3313 goto fail;
3314
3315 cpumask_setall(attrs->cpumask);
3316 return attrs;
3317fail:
3318 free_workqueue_attrs(attrs);
3319 return NULL;
3320}
3321
Tejun Heo29c91e92013-03-12 11:30:03 -07003322static void copy_workqueue_attrs(struct workqueue_attrs *to,
3323 const struct workqueue_attrs *from)
3324{
3325 to->nice = from->nice;
3326 cpumask_copy(to->cpumask, from->cpumask);
3327}
3328
3329/*
3330 * Hacky implementation of jhash of bitmaps which only considers the
3331 * specified number of bits. We probably want a proper implementation in
3332 * include/linux/jhash.h.
3333 */
3334static u32 jhash_bitmap(const unsigned long *bitmap, int bits, u32 hash)
3335{
3336 int nr_longs = bits / BITS_PER_LONG;
3337 int nr_leftover = bits % BITS_PER_LONG;
3338 unsigned long leftover = 0;
3339
3340 if (nr_longs)
3341 hash = jhash(bitmap, nr_longs * sizeof(long), hash);
3342 if (nr_leftover) {
3343 bitmap_copy(&leftover, bitmap + nr_longs, nr_leftover);
3344 hash = jhash(&leftover, sizeof(long), hash);
3345 }
3346 return hash;
3347}
3348
3349/* hash value of the content of @attr */
3350static u32 wqattrs_hash(const struct workqueue_attrs *attrs)
3351{
3352 u32 hash = 0;
3353
3354 hash = jhash_1word(attrs->nice, hash);
3355 hash = jhash_bitmap(cpumask_bits(attrs->cpumask), nr_cpu_ids, hash);
3356 return hash;
3357}
3358
3359/* content equality test */
3360static bool wqattrs_equal(const struct workqueue_attrs *a,
3361 const struct workqueue_attrs *b)
3362{
3363 if (a->nice != b->nice)
3364 return false;
3365 if (!cpumask_equal(a->cpumask, b->cpumask))
3366 return false;
3367 return true;
3368}
3369
Tejun Heo7a4e3442013-03-12 11:30:00 -07003370/**
3371 * init_worker_pool - initialize a newly zalloc'd worker_pool
3372 * @pool: worker_pool to initialize
3373 *
3374 * Initiailize a newly zalloc'd @pool. It also allocates @pool->attrs.
Tejun Heo29c91e92013-03-12 11:30:03 -07003375 * Returns 0 on success, -errno on failure. Even on failure, all fields
3376 * inside @pool proper are initialized and put_unbound_pool() can be called
3377 * on @pool safely to release it.
Tejun Heo7a4e3442013-03-12 11:30:00 -07003378 */
3379static int init_worker_pool(struct worker_pool *pool)
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003380{
3381 spin_lock_init(&pool->lock);
Tejun Heo29c91e92013-03-12 11:30:03 -07003382 pool->id = -1;
3383 pool->cpu = -1;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003384 pool->flags |= POOL_DISASSOCIATED;
3385 INIT_LIST_HEAD(&pool->worklist);
3386 INIT_LIST_HEAD(&pool->idle_list);
3387 hash_init(pool->busy_hash);
3388
3389 init_timer_deferrable(&pool->idle_timer);
3390 pool->idle_timer.function = idle_worker_timeout;
3391 pool->idle_timer.data = (unsigned long)pool;
3392
3393 setup_timer(&pool->mayday_timer, pool_mayday_timeout,
3394 (unsigned long)pool);
3395
3396 mutex_init(&pool->manager_arb);
Tejun Heobc3a1af2013-03-13 19:47:39 -07003397 mutex_init(&pool->manager_mutex);
Tejun Heo822d8402013-03-19 13:45:21 -07003398 idr_init(&pool->worker_idr);
Tejun Heo7a4e3442013-03-12 11:30:00 -07003399
Tejun Heo29c91e92013-03-12 11:30:03 -07003400 INIT_HLIST_NODE(&pool->hash_node);
3401 pool->refcnt = 1;
3402
3403 /* shouldn't fail above this point */
Tejun Heo7a4e3442013-03-12 11:30:00 -07003404 pool->attrs = alloc_workqueue_attrs(GFP_KERNEL);
3405 if (!pool->attrs)
3406 return -ENOMEM;
3407 return 0;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003408}
3409
Tejun Heo29c91e92013-03-12 11:30:03 -07003410static void rcu_free_pool(struct rcu_head *rcu)
3411{
3412 struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu);
3413
Tejun Heo822d8402013-03-19 13:45:21 -07003414 idr_destroy(&pool->worker_idr);
Tejun Heo29c91e92013-03-12 11:30:03 -07003415 free_workqueue_attrs(pool->attrs);
3416 kfree(pool);
3417}
3418
3419/**
3420 * put_unbound_pool - put a worker_pool
3421 * @pool: worker_pool to put
3422 *
3423 * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003424 * safe manner. get_unbound_pool() calls this function on its failure path
3425 * and this function should be able to release pools which went through,
3426 * successfully or not, init_worker_pool().
Tejun Heo29c91e92013-03-12 11:30:03 -07003427 */
3428static void put_unbound_pool(struct worker_pool *pool)
3429{
3430 struct worker *worker;
3431
Tejun Heo5bcab332013-03-13 19:47:40 -07003432 mutex_lock(&wq_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003433 if (--pool->refcnt) {
Tejun Heo5bcab332013-03-13 19:47:40 -07003434 mutex_unlock(&wq_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003435 return;
3436 }
3437
3438 /* sanity checks */
3439 if (WARN_ON(!(pool->flags & POOL_DISASSOCIATED)) ||
3440 WARN_ON(!list_empty(&pool->worklist))) {
Tejun Heo5bcab332013-03-13 19:47:40 -07003441 mutex_unlock(&wq_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003442 return;
3443 }
3444
3445 /* release id and unhash */
3446 if (pool->id >= 0)
3447 idr_remove(&worker_pool_idr, pool->id);
3448 hash_del(&pool->hash_node);
3449
Tejun Heo5bcab332013-03-13 19:47:40 -07003450 mutex_unlock(&wq_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003451
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003452 /*
3453 * Become the manager and destroy all workers. Grabbing
3454 * manager_arb prevents @pool's workers from blocking on
3455 * manager_mutex.
3456 */
Tejun Heo29c91e92013-03-12 11:30:03 -07003457 mutex_lock(&pool->manager_arb);
Tejun Heocd549682013-03-13 19:47:39 -07003458 mutex_lock(&pool->manager_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003459 spin_lock_irq(&pool->lock);
3460
3461 while ((worker = first_worker(pool)))
3462 destroy_worker(worker);
3463 WARN_ON(pool->nr_workers || pool->nr_idle);
3464
3465 spin_unlock_irq(&pool->lock);
Tejun Heocd549682013-03-13 19:47:39 -07003466 mutex_unlock(&pool->manager_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003467 mutex_unlock(&pool->manager_arb);
3468
3469 /* shut down the timers */
3470 del_timer_sync(&pool->idle_timer);
3471 del_timer_sync(&pool->mayday_timer);
3472
3473 /* sched-RCU protected to allow dereferences from get_work_pool() */
3474 call_rcu_sched(&pool->rcu, rcu_free_pool);
3475}
3476
3477/**
3478 * get_unbound_pool - get a worker_pool with the specified attributes
3479 * @attrs: the attributes of the worker_pool to get
3480 *
3481 * Obtain a worker_pool which has the same attributes as @attrs, bump the
3482 * reference count and return it. If there already is a matching
3483 * worker_pool, it will be used; otherwise, this function attempts to
3484 * create a new one. On failure, returns NULL.
3485 */
3486static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs)
3487{
Tejun Heo29c91e92013-03-12 11:30:03 -07003488 u32 hash = wqattrs_hash(attrs);
3489 struct worker_pool *pool;
Tejun Heo29c91e92013-03-12 11:30:03 -07003490
Tejun Heo5bcab332013-03-13 19:47:40 -07003491 mutex_lock(&wq_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003492
3493 /* do we already have a matching pool? */
Tejun Heo29c91e92013-03-12 11:30:03 -07003494 hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) {
3495 if (wqattrs_equal(pool->attrs, attrs)) {
3496 pool->refcnt++;
3497 goto out_unlock;
3498 }
3499 }
Tejun Heo29c91e92013-03-12 11:30:03 -07003500
3501 /* nope, create a new one */
3502 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
3503 if (!pool || init_worker_pool(pool) < 0)
3504 goto fail;
3505
Lai Jiangshan12ee4fc2013-03-20 03:28:01 +08003506 if (workqueue_freezing)
3507 pool->flags |= POOL_FREEZING;
3508
Tejun Heo8864b4e2013-03-12 11:30:04 -07003509 lockdep_set_subclass(&pool->lock, 1); /* see put_pwq() */
Tejun Heo29c91e92013-03-12 11:30:03 -07003510 copy_workqueue_attrs(pool->attrs, attrs);
3511
3512 if (worker_pool_assign_id(pool) < 0)
3513 goto fail;
3514
3515 /* create and start the initial worker */
Tejun Heoebf44d12013-03-13 19:47:39 -07003516 if (create_and_start_worker(pool) < 0)
Tejun Heo29c91e92013-03-12 11:30:03 -07003517 goto fail;
3518
Tejun Heo29c91e92013-03-12 11:30:03 -07003519 /* install */
Tejun Heo29c91e92013-03-12 11:30:03 -07003520 hash_add(unbound_pool_hash, &pool->hash_node, hash);
3521out_unlock:
Tejun Heo5bcab332013-03-13 19:47:40 -07003522 mutex_unlock(&wq_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003523 return pool;
3524fail:
Tejun Heo5bcab332013-03-13 19:47:40 -07003525 mutex_unlock(&wq_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003526 if (pool)
3527 put_unbound_pool(pool);
3528 return NULL;
3529}
3530
Tejun Heo8864b4e2013-03-12 11:30:04 -07003531static void rcu_free_pwq(struct rcu_head *rcu)
3532{
3533 kmem_cache_free(pwq_cache,
3534 container_of(rcu, struct pool_workqueue, rcu));
3535}
3536
3537/*
3538 * Scheduled on system_wq by put_pwq() when an unbound pwq hits zero refcnt
3539 * and needs to be destroyed.
3540 */
3541static void pwq_unbound_release_workfn(struct work_struct *work)
3542{
3543 struct pool_workqueue *pwq = container_of(work, struct pool_workqueue,
3544 unbound_release_work);
3545 struct workqueue_struct *wq = pwq->wq;
3546 struct worker_pool *pool = pwq->pool;
3547
3548 if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)))
3549 return;
3550
Tejun Heo75ccf592013-03-12 11:30:04 -07003551 /*
3552 * Unlink @pwq. Synchronization against flush_mutex isn't strictly
3553 * necessary on release but do it anyway. It's easier to verify
3554 * and consistent with the linking path.
3555 */
3556 mutex_lock(&wq->flush_mutex);
Tejun Heo794b18b2013-03-13 19:47:40 -07003557 spin_lock_irq(&pwq_lock);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003558 list_del_rcu(&pwq->pwqs_node);
Tejun Heo794b18b2013-03-13 19:47:40 -07003559 spin_unlock_irq(&pwq_lock);
Tejun Heo75ccf592013-03-12 11:30:04 -07003560 mutex_unlock(&wq->flush_mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003561
3562 put_unbound_pool(pool);
3563 call_rcu_sched(&pwq->rcu, rcu_free_pwq);
3564
3565 /*
3566 * If we're the last pwq going away, @wq is already dead and no one
3567 * is gonna access it anymore. Free it.
3568 */
3569 if (list_empty(&wq->pwqs))
3570 kfree(wq);
3571}
3572
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003573/**
Tejun Heo699ce092013-03-13 16:51:35 -07003574 * pwq_adjust_max_active - update a pwq's max_active to the current setting
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003575 * @pwq: target pool_workqueue
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003576 *
Tejun Heo699ce092013-03-13 16:51:35 -07003577 * If @pwq isn't freezing, set @pwq->max_active to the associated
3578 * workqueue's saved_max_active and activate delayed work items
3579 * accordingly. If @pwq is freezing, clear @pwq->max_active to zero.
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003580 */
Tejun Heo699ce092013-03-13 16:51:35 -07003581static void pwq_adjust_max_active(struct pool_workqueue *pwq)
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003582{
Tejun Heo699ce092013-03-13 16:51:35 -07003583 struct workqueue_struct *wq = pwq->wq;
3584 bool freezable = wq->flags & WQ_FREEZABLE;
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003585
Tejun Heo699ce092013-03-13 16:51:35 -07003586 /* for @wq->saved_max_active */
Tejun Heo794b18b2013-03-13 19:47:40 -07003587 lockdep_assert_held(&pwq_lock);
Tejun Heo699ce092013-03-13 16:51:35 -07003588
3589 /* fast exit for non-freezable wqs */
3590 if (!freezable && pwq->max_active == wq->saved_max_active)
3591 return;
3592
3593 spin_lock(&pwq->pool->lock);
3594
3595 if (!freezable || !(pwq->pool->flags & POOL_FREEZING)) {
3596 pwq->max_active = wq->saved_max_active;
3597
3598 while (!list_empty(&pwq->delayed_works) &&
3599 pwq->nr_active < pwq->max_active)
3600 pwq_activate_first_delayed(pwq);
3601 } else {
3602 pwq->max_active = 0;
3603 }
3604
3605 spin_unlock(&pwq->pool->lock);
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003606}
3607
Tejun Heod2c1d402013-03-12 11:30:04 -07003608static void init_and_link_pwq(struct pool_workqueue *pwq,
3609 struct workqueue_struct *wq,
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003610 struct worker_pool *pool,
3611 struct pool_workqueue **p_last_pwq)
Tejun Heod2c1d402013-03-12 11:30:04 -07003612{
3613 BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK);
3614
3615 pwq->pool = pool;
3616 pwq->wq = wq;
3617 pwq->flush_color = -1;
Tejun Heo8864b4e2013-03-12 11:30:04 -07003618 pwq->refcnt = 1;
Tejun Heod2c1d402013-03-12 11:30:04 -07003619 INIT_LIST_HEAD(&pwq->delayed_works);
3620 INIT_LIST_HEAD(&pwq->mayday_node);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003621 INIT_WORK(&pwq->unbound_release_work, pwq_unbound_release_workfn);
Tejun Heod2c1d402013-03-12 11:30:04 -07003622
Tejun Heo75ccf592013-03-12 11:30:04 -07003623 mutex_lock(&wq->flush_mutex);
Tejun Heo794b18b2013-03-13 19:47:40 -07003624 spin_lock_irq(&pwq_lock);
Tejun Heo75ccf592013-03-12 11:30:04 -07003625
Tejun Heo983ca252013-03-13 16:51:35 -07003626 /*
3627 * Set the matching work_color. This is synchronized with
3628 * flush_mutex to avoid confusing flush_workqueue().
3629 */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003630 if (p_last_pwq)
3631 *p_last_pwq = first_pwq(wq);
Tejun Heo75ccf592013-03-12 11:30:04 -07003632 pwq->work_color = wq->work_color;
Tejun Heo983ca252013-03-13 16:51:35 -07003633
3634 /* sync max_active to the current setting */
3635 pwq_adjust_max_active(pwq);
3636
3637 /* link in @pwq */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003638 list_add_rcu(&pwq->pwqs_node, &wq->pwqs);
Tejun Heo75ccf592013-03-12 11:30:04 -07003639
Tejun Heo794b18b2013-03-13 19:47:40 -07003640 spin_unlock_irq(&pwq_lock);
Tejun Heo75ccf592013-03-12 11:30:04 -07003641 mutex_unlock(&wq->flush_mutex);
Tejun Heod2c1d402013-03-12 11:30:04 -07003642}
3643
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003644/**
3645 * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue
3646 * @wq: the target workqueue
3647 * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs()
3648 *
3649 * Apply @attrs to an unbound workqueue @wq. If @attrs doesn't match the
3650 * current attributes, a new pwq is created and made the first pwq which
3651 * will serve all new work items. Older pwqs are released as in-flight
3652 * work items finish. Note that a work item which repeatedly requeues
3653 * itself back-to-back will stay on its current pwq.
3654 *
3655 * Performs GFP_KERNEL allocations. Returns 0 on success and -errno on
3656 * failure.
3657 */
3658int apply_workqueue_attrs(struct workqueue_struct *wq,
3659 const struct workqueue_attrs *attrs)
3660{
3661 struct pool_workqueue *pwq, *last_pwq;
3662 struct worker_pool *pool;
3663
Tejun Heo8719dce2013-03-12 11:30:04 -07003664 /* only unbound workqueues can change attributes */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003665 if (WARN_ON(!(wq->flags & WQ_UNBOUND)))
3666 return -EINVAL;
3667
Tejun Heo8719dce2013-03-12 11:30:04 -07003668 /* creating multiple pwqs breaks ordering guarantee */
3669 if (WARN_ON((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs)))
3670 return -EINVAL;
3671
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003672 pwq = kmem_cache_zalloc(pwq_cache, GFP_KERNEL);
3673 if (!pwq)
3674 return -ENOMEM;
3675
3676 pool = get_unbound_pool(attrs);
3677 if (!pool) {
3678 kmem_cache_free(pwq_cache, pwq);
3679 return -ENOMEM;
3680 }
3681
3682 init_and_link_pwq(pwq, wq, pool, &last_pwq);
3683 if (last_pwq) {
3684 spin_lock_irq(&last_pwq->pool->lock);
3685 put_pwq(last_pwq);
3686 spin_unlock_irq(&last_pwq->pool->lock);
3687 }
3688
3689 return 0;
3690}
3691
Tejun Heo30cdf242013-03-12 11:29:57 -07003692static int alloc_and_link_pwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003694 bool highpri = wq->flags & WQ_HIGHPRI;
Tejun Heo30cdf242013-03-12 11:29:57 -07003695 int cpu;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01003696
Tejun Heo30cdf242013-03-12 11:29:57 -07003697 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo420c0dd2013-03-12 11:29:59 -07003698 wq->cpu_pwqs = alloc_percpu(struct pool_workqueue);
3699 if (!wq->cpu_pwqs)
Tejun Heo30cdf242013-03-12 11:29:57 -07003700 return -ENOMEM;
3701
3702 for_each_possible_cpu(cpu) {
Tejun Heo7fb98ea2013-03-12 11:30:00 -07003703 struct pool_workqueue *pwq =
3704 per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heo7a62c2c2013-03-12 11:30:03 -07003705 struct worker_pool *cpu_pools =
Tejun Heof02ae732013-03-12 11:30:03 -07003706 per_cpu(cpu_worker_pools, cpu);
Tejun Heo30cdf242013-03-12 11:29:57 -07003707
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003708 init_and_link_pwq(pwq, wq, &cpu_pools[highpri], NULL);
Tejun Heo30cdf242013-03-12 11:29:57 -07003709 }
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003710 return 0;
Tejun Heo30cdf242013-03-12 11:29:57 -07003711 } else {
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003712 return apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]);
Tejun Heo30cdf242013-03-12 11:29:57 -07003713 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003714}
3715
Tejun Heof3421792010-07-02 10:03:51 +02003716static int wq_clamp_max_active(int max_active, unsigned int flags,
3717 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003718{
Tejun Heof3421792010-07-02 10:03:51 +02003719 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3720
3721 if (max_active < 1 || max_active > lim)
Valentin Ilie044c7822012-08-19 00:52:42 +03003722 pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
3723 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003724
Tejun Heof3421792010-07-02 10:03:51 +02003725 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003726}
3727
Tejun Heob196be82012-01-10 15:11:35 -08003728struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02003729 unsigned int flags,
3730 int max_active,
3731 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08003732 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003733{
Tejun Heob196be82012-01-10 15:11:35 -08003734 va_list args, args1;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003735 struct workqueue_struct *wq;
Tejun Heo49e3cf42013-03-12 11:29:58 -07003736 struct pool_workqueue *pwq;
Tejun Heob196be82012-01-10 15:11:35 -08003737 size_t namelen;
3738
3739 /* determine namelen, allocate wq and format name */
3740 va_start(args, lock_name);
3741 va_copy(args1, args);
3742 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3743
3744 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);
3745 if (!wq)
Tejun Heod2c1d402013-03-12 11:30:04 -07003746 return NULL;
Tejun Heob196be82012-01-10 15:11:35 -08003747
3748 vsnprintf(wq->name, namelen, fmt, args1);
3749 va_end(args);
3750 va_end(args1);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003751
Tejun Heod320c032010-06-29 10:07:14 +02003752 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003753 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003754
Tejun Heob196be82012-01-10 15:11:35 -08003755 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003756 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003757 wq->saved_max_active = max_active;
Tejun Heo73f53c42010-06-29 10:07:11 +02003758 mutex_init(&wq->flush_mutex);
Tejun Heo112202d2013-02-13 19:29:12 -08003759 atomic_set(&wq->nr_pwqs_to_flush, 0);
Tejun Heo30cdf242013-03-12 11:29:57 -07003760 INIT_LIST_HEAD(&wq->pwqs);
Tejun Heo73f53c42010-06-29 10:07:11 +02003761 INIT_LIST_HEAD(&wq->flusher_queue);
3762 INIT_LIST_HEAD(&wq->flusher_overflow);
Tejun Heo493a1722013-03-12 11:29:59 -07003763 INIT_LIST_HEAD(&wq->maydays);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003764
Johannes Bergeb13ba82008-01-16 09:51:58 +01003765 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003766 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003767
Tejun Heo30cdf242013-03-12 11:29:57 -07003768 if (alloc_and_link_pwqs(wq) < 0)
Tejun Heod2c1d402013-03-12 11:30:04 -07003769 goto err_free_wq;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003770
Tejun Heo493008a2013-03-12 11:30:03 -07003771 /*
3772 * Workqueues which may be used during memory reclaim should
3773 * have a rescuer to guarantee forward progress.
3774 */
3775 if (flags & WQ_MEM_RECLAIM) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003776 struct worker *rescuer;
3777
Tejun Heod2c1d402013-03-12 11:30:04 -07003778 rescuer = alloc_worker();
Tejun Heoe22bee72010-06-29 10:07:14 +02003779 if (!rescuer)
Tejun Heod2c1d402013-03-12 11:30:04 -07003780 goto err_destroy;
Tejun Heoe22bee72010-06-29 10:07:14 +02003781
Tejun Heo111c2252013-01-17 17:16:24 -08003782 rescuer->rescue_wq = wq;
3783 rescuer->task = kthread_create(rescuer_thread, rescuer, "%s",
Tejun Heob196be82012-01-10 15:11:35 -08003784 wq->name);
Tejun Heod2c1d402013-03-12 11:30:04 -07003785 if (IS_ERR(rescuer->task)) {
3786 kfree(rescuer);
3787 goto err_destroy;
3788 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003789
Tejun Heod2c1d402013-03-12 11:30:04 -07003790 wq->rescuer = rescuer;
Tejun Heo14a40ff2013-03-19 13:45:20 -07003791 rescuer->task->flags |= PF_NO_SETAFFINITY;
Tejun Heoe22bee72010-06-29 10:07:14 +02003792 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003793 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003794
Tejun Heo226223a2013-03-12 11:30:05 -07003795 if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq))
3796 goto err_destroy;
3797
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003798 /*
Tejun Heo5bcab332013-03-13 19:47:40 -07003799 * wq_mutex protects global freeze state and workqueues list. Grab
3800 * it, adjust max_active and add the new @wq to workqueues list.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003801 */
Tejun Heo5bcab332013-03-13 19:47:40 -07003802 mutex_lock(&wq_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003803
Tejun Heo794b18b2013-03-13 19:47:40 -07003804 spin_lock_irq(&pwq_lock);
Tejun Heo699ce092013-03-13 16:51:35 -07003805 for_each_pwq(pwq, wq)
3806 pwq_adjust_max_active(pwq);
Tejun Heo794b18b2013-03-13 19:47:40 -07003807 spin_unlock_irq(&pwq_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003808
Tejun Heo15376632010-06-29 10:07:11 +02003809 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003810
Tejun Heo5bcab332013-03-13 19:47:40 -07003811 mutex_unlock(&wq_mutex);
Tejun Heo15376632010-06-29 10:07:11 +02003812
Oleg Nesterov3af244332007-05-09 02:34:09 -07003813 return wq;
Tejun Heod2c1d402013-03-12 11:30:04 -07003814
3815err_free_wq:
3816 kfree(wq);
3817 return NULL;
3818err_destroy:
3819 destroy_workqueue(wq);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003820 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003821}
Tejun Heod320c032010-06-29 10:07:14 +02003822EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003823
3824/**
3825 * destroy_workqueue - safely terminate a workqueue
3826 * @wq: target workqueue
3827 *
3828 * Safely destroy a workqueue. All work currently pending will be done first.
3829 */
3830void destroy_workqueue(struct workqueue_struct *wq)
3831{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003832 struct pool_workqueue *pwq;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003833
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003834 /* drain it before proceeding with destruction */
3835 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003836
Tejun Heo6183c002013-03-12 11:29:57 -07003837 /* sanity checks */
Tejun Heo794b18b2013-03-13 19:47:40 -07003838 spin_lock_irq(&pwq_lock);
Tejun Heo49e3cf42013-03-12 11:29:58 -07003839 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07003840 int i;
3841
Tejun Heo76af4d92013-03-12 11:30:00 -07003842 for (i = 0; i < WORK_NR_COLORS; i++) {
3843 if (WARN_ON(pwq->nr_in_flight[i])) {
Tejun Heo794b18b2013-03-13 19:47:40 -07003844 spin_unlock_irq(&pwq_lock);
Tejun Heo6183c002013-03-12 11:29:57 -07003845 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07003846 }
3847 }
3848
Tejun Heo8864b4e2013-03-12 11:30:04 -07003849 if (WARN_ON(pwq->refcnt > 1) ||
3850 WARN_ON(pwq->nr_active) ||
Tejun Heo76af4d92013-03-12 11:30:00 -07003851 WARN_ON(!list_empty(&pwq->delayed_works))) {
Tejun Heo794b18b2013-03-13 19:47:40 -07003852 spin_unlock_irq(&pwq_lock);
Tejun Heo6183c002013-03-12 11:29:57 -07003853 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07003854 }
Tejun Heo6183c002013-03-12 11:29:57 -07003855 }
Tejun Heo794b18b2013-03-13 19:47:40 -07003856 spin_unlock_irq(&pwq_lock);
Tejun Heo6183c002013-03-12 11:29:57 -07003857
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003858 /*
3859 * wq list is used to freeze wq, remove from list after
3860 * flushing is complete in case freeze races us.
3861 */
Tejun Heo5bcab332013-03-13 19:47:40 -07003862 mutex_lock(&wq_mutex);
Tejun Heod2c1d402013-03-12 11:30:04 -07003863 list_del_init(&wq->list);
Tejun Heo5bcab332013-03-13 19:47:40 -07003864 mutex_unlock(&wq_mutex);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003865
Tejun Heo226223a2013-03-12 11:30:05 -07003866 workqueue_sysfs_unregister(wq);
3867
Tejun Heo493008a2013-03-12 11:30:03 -07003868 if (wq->rescuer) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003869 kthread_stop(wq->rescuer->task);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003870 kfree(wq->rescuer);
Tejun Heo493008a2013-03-12 11:30:03 -07003871 wq->rescuer = NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +02003872 }
3873
Tejun Heo8864b4e2013-03-12 11:30:04 -07003874 if (!(wq->flags & WQ_UNBOUND)) {
3875 /*
3876 * The base ref is never dropped on per-cpu pwqs. Directly
3877 * free the pwqs and wq.
3878 */
3879 free_percpu(wq->cpu_pwqs);
3880 kfree(wq);
3881 } else {
3882 /*
3883 * We're the sole accessor of @wq at this point. Directly
3884 * access the first pwq and put the base ref. As both pwqs
3885 * and pools are sched-RCU protected, the lock operations
3886 * are safe. @wq will be freed when the last pwq is
3887 * released.
3888 */
Tejun Heo29c91e92013-03-12 11:30:03 -07003889 pwq = list_first_entry(&wq->pwqs, struct pool_workqueue,
3890 pwqs_node);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003891 spin_lock_irq(&pwq->pool->lock);
3892 put_pwq(pwq);
3893 spin_unlock_irq(&pwq->pool->lock);
Tejun Heo29c91e92013-03-12 11:30:03 -07003894 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003895}
3896EXPORT_SYMBOL_GPL(destroy_workqueue);
3897
Tejun Heodcd989c2010-06-29 10:07:14 +02003898/**
3899 * workqueue_set_max_active - adjust max_active of a workqueue
3900 * @wq: target workqueue
3901 * @max_active: new max_active value.
3902 *
3903 * Set max_active of @wq to @max_active.
3904 *
3905 * CONTEXT:
3906 * Don't call from IRQ context.
3907 */
3908void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3909{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003910 struct pool_workqueue *pwq;
Tejun Heodcd989c2010-06-29 10:07:14 +02003911
Tejun Heo8719dce2013-03-12 11:30:04 -07003912 /* disallow meddling with max_active for ordered workqueues */
3913 if (WARN_ON(wq->flags & __WQ_ORDERED))
3914 return;
3915
Tejun Heof3421792010-07-02 10:03:51 +02003916 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02003917
Tejun Heo794b18b2013-03-13 19:47:40 -07003918 spin_lock_irq(&pwq_lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02003919
3920 wq->saved_max_active = max_active;
3921
Tejun Heo699ce092013-03-13 16:51:35 -07003922 for_each_pwq(pwq, wq)
3923 pwq_adjust_max_active(pwq);
Tejun Heodcd989c2010-06-29 10:07:14 +02003924
Tejun Heo794b18b2013-03-13 19:47:40 -07003925 spin_unlock_irq(&pwq_lock);
Tejun Heodcd989c2010-06-29 10:07:14 +02003926}
3927EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3928
3929/**
Tejun Heoe62676162013-03-12 17:41:37 -07003930 * current_is_workqueue_rescuer - is %current workqueue rescuer?
3931 *
3932 * Determine whether %current is a workqueue rescuer. Can be used from
3933 * work functions to determine whether it's being run off the rescuer task.
3934 */
3935bool current_is_workqueue_rescuer(void)
3936{
3937 struct worker *worker = current_wq_worker();
3938
3939 return worker && worker == worker->current_pwq->wq->rescuer;
3940}
3941
3942/**
Tejun Heodcd989c2010-06-29 10:07:14 +02003943 * workqueue_congested - test whether a workqueue is congested
3944 * @cpu: CPU in question
3945 * @wq: target workqueue
3946 *
3947 * Test whether @wq's cpu workqueue for @cpu is congested. There is
3948 * no synchronization around this function and the test result is
3949 * unreliable and only useful as advisory hints or for debugging.
3950 *
3951 * RETURNS:
3952 * %true if congested, %false otherwise.
3953 */
Tejun Heod84ff052013-03-12 11:29:59 -07003954bool workqueue_congested(int cpu, struct workqueue_struct *wq)
Tejun Heodcd989c2010-06-29 10:07:14 +02003955{
Tejun Heo7fb98ea2013-03-12 11:30:00 -07003956 struct pool_workqueue *pwq;
Tejun Heo76af4d92013-03-12 11:30:00 -07003957 bool ret;
3958
3959 preempt_disable();
Tejun Heo7fb98ea2013-03-12 11:30:00 -07003960
3961 if (!(wq->flags & WQ_UNBOUND))
3962 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
3963 else
3964 pwq = first_pwq(wq);
Tejun Heodcd989c2010-06-29 10:07:14 +02003965
Tejun Heo76af4d92013-03-12 11:30:00 -07003966 ret = !list_empty(&pwq->delayed_works);
3967 preempt_enable();
3968
3969 return ret;
Tejun Heodcd989c2010-06-29 10:07:14 +02003970}
3971EXPORT_SYMBOL_GPL(workqueue_congested);
3972
3973/**
Tejun Heodcd989c2010-06-29 10:07:14 +02003974 * work_busy - test whether a work is currently pending or running
3975 * @work: the work to be tested
3976 *
3977 * Test whether @work is currently pending or running. There is no
3978 * synchronization around this function and the test result is
3979 * unreliable and only useful as advisory hints or for debugging.
Tejun Heodcd989c2010-06-29 10:07:14 +02003980 *
3981 * RETURNS:
3982 * OR'd bitmask of WORK_BUSY_* bits.
3983 */
3984unsigned int work_busy(struct work_struct *work)
3985{
Tejun Heofa1b54e2013-03-12 11:30:00 -07003986 struct worker_pool *pool;
Tejun Heodcd989c2010-06-29 10:07:14 +02003987 unsigned long flags;
3988 unsigned int ret = 0;
3989
Tejun Heodcd989c2010-06-29 10:07:14 +02003990 if (work_pending(work))
3991 ret |= WORK_BUSY_PENDING;
Tejun Heodcd989c2010-06-29 10:07:14 +02003992
Tejun Heofa1b54e2013-03-12 11:30:00 -07003993 local_irq_save(flags);
3994 pool = get_work_pool(work);
Lai Jiangshan038366c2013-02-06 18:04:53 -08003995 if (pool) {
Tejun Heofa1b54e2013-03-12 11:30:00 -07003996 spin_lock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08003997 if (find_worker_executing_work(pool, work))
3998 ret |= WORK_BUSY_RUNNING;
Tejun Heofa1b54e2013-03-12 11:30:00 -07003999 spin_unlock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004000 }
Tejun Heofa1b54e2013-03-12 11:30:00 -07004001 local_irq_restore(flags);
Tejun Heodcd989c2010-06-29 10:07:14 +02004002
4003 return ret;
4004}
4005EXPORT_SYMBOL_GPL(work_busy);
4006
Tejun Heodb7bccf2010-06-29 10:07:12 +02004007/*
4008 * CPU hotplug.
4009 *
Tejun Heoe22bee72010-06-29 10:07:14 +02004010 * There are two challenges in supporting CPU hotplug. Firstly, there
Tejun Heo112202d2013-02-13 19:29:12 -08004011 * are a lot of assumptions on strong associations among work, pwq and
Tejun Heo706026c2013-01-24 11:01:34 -08004012 * pool which make migrating pending and scheduled works very
Tejun Heoe22bee72010-06-29 10:07:14 +02004013 * difficult to implement without impacting hot paths. Secondly,
Tejun Heo94cf58b2013-01-24 11:01:33 -08004014 * worker pools serve mix of short, long and very long running works making
Tejun Heoe22bee72010-06-29 10:07:14 +02004015 * blocked draining impractical.
4016 *
Tejun Heo24647572013-01-24 11:01:33 -08004017 * This is solved by allowing the pools to be disassociated from the CPU
Tejun Heo628c78e2012-07-17 12:39:27 -07004018 * running as an unbound one and allowing it to be reattached later if the
4019 * cpu comes back online.
Tejun Heodb7bccf2010-06-29 10:07:12 +02004020 */
4021
Tejun Heo706026c2013-01-24 11:01:34 -08004022static void wq_unbind_fn(struct work_struct *work)
Tejun Heodb7bccf2010-06-29 10:07:12 +02004023{
Tejun Heo38db41d2013-01-24 11:01:34 -08004024 int cpu = smp_processor_id();
Tejun Heo4ce62e92012-07-13 22:16:44 -07004025 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004026 struct worker *worker;
Tejun Heoa9ab7752013-03-19 13:45:21 -07004027 int wi;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004028
Tejun Heof02ae732013-03-12 11:30:03 -07004029 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo6183c002013-03-12 11:29:57 -07004030 WARN_ON_ONCE(cpu != smp_processor_id());
Tejun Heo94cf58b2013-01-24 11:01:33 -08004031
Tejun Heobc3a1af2013-03-13 19:47:39 -07004032 mutex_lock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004033 spin_lock_irq(&pool->lock);
4034
4035 /*
Tejun Heobc3a1af2013-03-13 19:47:39 -07004036 * We've blocked all manager operations. Make all workers
Tejun Heo94cf58b2013-01-24 11:01:33 -08004037 * unbound and set DISASSOCIATED. Before this, all workers
4038 * except for the ones which are still executing works from
4039 * before the last CPU down must be on the cpu. After
4040 * this, they may become diasporas.
4041 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07004042 for_each_pool_worker(worker, wi, pool)
Tejun Heoc9e7cf22013-01-24 11:01:33 -08004043 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004044
Tejun Heo24647572013-01-24 11:01:33 -08004045 pool->flags |= POOL_DISASSOCIATED;
Tejun Heof2d5a0e2012-07-17 12:39:26 -07004046
Tejun Heo94cf58b2013-01-24 11:01:33 -08004047 spin_unlock_irq(&pool->lock);
Tejun Heobc3a1af2013-03-13 19:47:39 -07004048 mutex_unlock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004049 }
Tejun Heoe22bee72010-06-29 10:07:14 +02004050
4051 /*
Tejun Heo628c78e2012-07-17 12:39:27 -07004052 * Call schedule() so that we cross rq->lock and thus can guarantee
4053 * sched callbacks see the %WORKER_UNBOUND flag. This is necessary
4054 * as scheduler callbacks may be invoked from other cpus.
4055 */
4056 schedule();
4057
4058 /*
4059 * Sched callbacks are disabled now. Zap nr_running. After this,
4060 * nr_running stays zero and need_more_worker() and keep_working()
Tejun Heo38db41d2013-01-24 11:01:34 -08004061 * are always true as long as the worklist is not empty. Pools on
4062 * @cpu now behave as unbound (in terms of concurrency management)
4063 * pools which are served by workers tied to the CPU.
Tejun Heo628c78e2012-07-17 12:39:27 -07004064 *
4065 * On return from this function, the current worker would trigger
4066 * unbound chain execution of pending work items if other workers
4067 * didn't already.
Tejun Heoe22bee72010-06-29 10:07:14 +02004068 */
Tejun Heof02ae732013-03-12 11:30:03 -07004069 for_each_cpu_worker_pool(pool, cpu)
Tejun Heoe19e3972013-01-24 11:39:44 -08004070 atomic_set(&pool->nr_running, 0);
Tejun Heodb7bccf2010-06-29 10:07:12 +02004071}
4072
Tejun Heobd7c0892013-03-19 13:45:21 -07004073/**
4074 * rebind_workers - rebind all workers of a pool to the associated CPU
4075 * @pool: pool of interest
4076 *
Tejun Heoa9ab7752013-03-19 13:45:21 -07004077 * @pool->cpu is coming online. Rebind all workers to the CPU.
Tejun Heobd7c0892013-03-19 13:45:21 -07004078 */
4079static void rebind_workers(struct worker_pool *pool)
4080{
Tejun Heoa9ab7752013-03-19 13:45:21 -07004081 struct worker *worker;
4082 int wi;
Tejun Heobd7c0892013-03-19 13:45:21 -07004083
4084 lockdep_assert_held(&pool->manager_mutex);
Tejun Heobd7c0892013-03-19 13:45:21 -07004085
Tejun Heoa9ab7752013-03-19 13:45:21 -07004086 /*
4087 * Restore CPU affinity of all workers. As all idle workers should
4088 * be on the run-queue of the associated CPU before any local
4089 * wake-ups for concurrency management happen, restore CPU affinty
4090 * of all workers first and then clear UNBOUND. As we're called
4091 * from CPU_ONLINE, the following shouldn't fail.
4092 */
4093 for_each_pool_worker(worker, wi, pool)
4094 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
4095 pool->attrs->cpumask) < 0);
4096
4097 spin_lock_irq(&pool->lock);
4098
4099 for_each_pool_worker(worker, wi, pool) {
4100 unsigned int worker_flags = worker->flags;
Tejun Heobd7c0892013-03-19 13:45:21 -07004101
4102 /*
Tejun Heoa9ab7752013-03-19 13:45:21 -07004103 * A bound idle worker should actually be on the runqueue
4104 * of the associated CPU for local wake-ups targeting it to
4105 * work. Kick all idle workers so that they migrate to the
4106 * associated CPU. Doing this in the same loop as
4107 * replacing UNBOUND with REBOUND is safe as no worker will
4108 * be bound before @pool->lock is released.
Tejun Heobd7c0892013-03-19 13:45:21 -07004109 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07004110 if (worker_flags & WORKER_IDLE)
4111 wake_up_process(worker->task);
4112
4113 /*
4114 * We want to clear UNBOUND but can't directly call
4115 * worker_clr_flags() or adjust nr_running. Atomically
4116 * replace UNBOUND with another NOT_RUNNING flag REBOUND.
4117 * @worker will clear REBOUND using worker_clr_flags() when
4118 * it initiates the next execution cycle thus restoring
4119 * concurrency management. Note that when or whether
4120 * @worker clears REBOUND doesn't affect correctness.
4121 *
4122 * ACCESS_ONCE() is necessary because @worker->flags may be
4123 * tested without holding any lock in
4124 * wq_worker_waking_up(). Without it, NOT_RUNNING test may
4125 * fail incorrectly leading to premature concurrency
4126 * management operations.
4127 */
4128 WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND));
4129 worker_flags |= WORKER_REBOUND;
4130 worker_flags &= ~WORKER_UNBOUND;
4131 ACCESS_ONCE(worker->flags) = worker_flags;
Tejun Heobd7c0892013-03-19 13:45:21 -07004132 }
4133
Tejun Heoa9ab7752013-03-19 13:45:21 -07004134 spin_unlock_irq(&pool->lock);
Tejun Heobd7c0892013-03-19 13:45:21 -07004135}
4136
Tejun Heo7dbc7252013-03-19 13:45:21 -07004137/**
4138 * restore_unbound_workers_cpumask - restore cpumask of unbound workers
4139 * @pool: unbound pool of interest
4140 * @cpu: the CPU which is coming up
4141 *
4142 * An unbound pool may end up with a cpumask which doesn't have any online
4143 * CPUs. When a worker of such pool get scheduled, the scheduler resets
4144 * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any
4145 * online CPU before, cpus_allowed of all its workers should be restored.
4146 */
4147static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu)
4148{
4149 static cpumask_t cpumask;
4150 struct worker *worker;
4151 int wi;
4152
4153 lockdep_assert_held(&pool->manager_mutex);
4154
4155 /* is @cpu allowed for @pool? */
4156 if (!cpumask_test_cpu(cpu, pool->attrs->cpumask))
4157 return;
4158
4159 /* is @cpu the only online CPU? */
4160 cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask);
4161 if (cpumask_weight(&cpumask) != 1)
4162 return;
4163
4164 /* as we're called from CPU_ONLINE, the following shouldn't fail */
4165 for_each_pool_worker(worker, wi, pool)
4166 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
4167 pool->attrs->cpumask) < 0);
4168}
4169
Tejun Heo8db25e72012-07-17 12:39:28 -07004170/*
4171 * Workqueues should be brought up before normal priority CPU notifiers.
4172 * This will be registered high priority CPU notifier.
4173 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07004174static int __cpuinit workqueue_cpu_up_callback(struct notifier_block *nfb,
Tejun Heo8db25e72012-07-17 12:39:28 -07004175 unsigned long action,
4176 void *hcpu)
Oleg Nesterov3af244332007-05-09 02:34:09 -07004177{
Tejun Heod84ff052013-03-12 11:29:59 -07004178 int cpu = (unsigned long)hcpu;
Tejun Heo4ce62e92012-07-13 22:16:44 -07004179 struct worker_pool *pool;
Tejun Heo7dbc7252013-03-19 13:45:21 -07004180 int pi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181
Tejun Heo8db25e72012-07-17 12:39:28 -07004182 switch (action & ~CPU_TASKS_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 case CPU_UP_PREPARE:
Tejun Heof02ae732013-03-12 11:30:03 -07004184 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo3ce63372012-07-17 12:39:27 -07004185 if (pool->nr_workers)
4186 continue;
Tejun Heoebf44d12013-03-13 19:47:39 -07004187 if (create_and_start_worker(pool) < 0)
Tejun Heo3ce63372012-07-17 12:39:27 -07004188 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02004190 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07004191
Tejun Heo65758202012-07-17 12:39:26 -07004192 case CPU_DOWN_FAILED:
4193 case CPU_ONLINE:
Tejun Heo7dbc7252013-03-19 13:45:21 -07004194 mutex_lock(&wq_mutex);
4195
4196 for_each_pool(pool, pi) {
Tejun Heobc3a1af2013-03-13 19:47:39 -07004197 mutex_lock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004198
Tejun Heo7dbc7252013-03-19 13:45:21 -07004199 if (pool->cpu == cpu) {
4200 spin_lock_irq(&pool->lock);
4201 pool->flags &= ~POOL_DISASSOCIATED;
4202 spin_unlock_irq(&pool->lock);
Tejun Heoa9ab7752013-03-19 13:45:21 -07004203
Tejun Heo7dbc7252013-03-19 13:45:21 -07004204 rebind_workers(pool);
4205 } else if (pool->cpu < 0) {
4206 restore_unbound_workers_cpumask(pool, cpu);
4207 }
Tejun Heo94cf58b2013-01-24 11:01:33 -08004208
Tejun Heobc3a1af2013-03-13 19:47:39 -07004209 mutex_unlock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004210 }
Tejun Heo7dbc7252013-03-19 13:45:21 -07004211
4212 mutex_unlock(&wq_mutex);
Tejun Heo8db25e72012-07-17 12:39:28 -07004213 break;
Tejun Heo65758202012-07-17 12:39:26 -07004214 }
4215 return NOTIFY_OK;
4216}
4217
4218/*
4219 * Workqueues should be brought down after normal priority CPU notifiers.
4220 * This will be registered as low priority CPU notifier.
4221 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07004222static int __cpuinit workqueue_cpu_down_callback(struct notifier_block *nfb,
Tejun Heo65758202012-07-17 12:39:26 -07004223 unsigned long action,
4224 void *hcpu)
4225{
Tejun Heod84ff052013-03-12 11:29:59 -07004226 int cpu = (unsigned long)hcpu;
Tejun Heo8db25e72012-07-17 12:39:28 -07004227 struct work_struct unbind_work;
4228
Tejun Heo65758202012-07-17 12:39:26 -07004229 switch (action & ~CPU_TASKS_FROZEN) {
4230 case CPU_DOWN_PREPARE:
Tejun Heo8db25e72012-07-17 12:39:28 -07004231 /* unbinding should happen on the local CPU */
Tejun Heo706026c2013-01-24 11:01:34 -08004232 INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn);
Joonsoo Kim7635d2f2012-08-15 23:25:41 +09004233 queue_work_on(cpu, system_highpri_wq, &unbind_work);
Tejun Heo8db25e72012-07-17 12:39:28 -07004234 flush_work(&unbind_work);
4235 break;
Tejun Heo65758202012-07-17 12:39:26 -07004236 }
4237 return NOTIFY_OK;
4238}
4239
Rusty Russell2d3854a2008-11-05 13:39:10 +11004240#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08004241
Rusty Russell2d3854a2008-11-05 13:39:10 +11004242struct work_for_cpu {
Tejun Heoed48ece2012-09-18 12:48:43 -07004243 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11004244 long (*fn)(void *);
4245 void *arg;
4246 long ret;
4247};
4248
Tejun Heoed48ece2012-09-18 12:48:43 -07004249static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11004250{
Tejun Heoed48ece2012-09-18 12:48:43 -07004251 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
4252
Rusty Russell2d3854a2008-11-05 13:39:10 +11004253 wfc->ret = wfc->fn(wfc->arg);
4254}
4255
4256/**
4257 * work_on_cpu - run a function in user context on a particular cpu
4258 * @cpu: the cpu to run on
4259 * @fn: the function to run
4260 * @arg: the function arg
4261 *
Rusty Russell31ad9082009-01-16 15:31:15 -08004262 * This will return the value @fn returns.
4263 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06004264 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11004265 */
Tejun Heod84ff052013-03-12 11:29:59 -07004266long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
Rusty Russell2d3854a2008-11-05 13:39:10 +11004267{
Tejun Heoed48ece2012-09-18 12:48:43 -07004268 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11004269
Tejun Heoed48ece2012-09-18 12:48:43 -07004270 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
4271 schedule_work_on(cpu, &wfc.work);
4272 flush_work(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11004273 return wfc.ret;
4274}
4275EXPORT_SYMBOL_GPL(work_on_cpu);
4276#endif /* CONFIG_SMP */
4277
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004278#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10304279
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004280/**
4281 * freeze_workqueues_begin - begin freezing workqueues
4282 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01004283 * Start freezing workqueues. After this function returns, all freezable
Tejun Heoc5aa87b2013-03-13 16:51:36 -07004284 * workqueues will queue new works to their delayed_works list instead of
Tejun Heo706026c2013-01-24 11:01:34 -08004285 * pool->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004286 *
4287 * CONTEXT:
Tejun Heo794b18b2013-03-13 19:47:40 -07004288 * Grabs and releases wq_mutex, pwq_lock and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004289 */
4290void freeze_workqueues_begin(void)
4291{
Tejun Heo17116962013-03-12 11:29:58 -07004292 struct worker_pool *pool;
Tejun Heo24b8a842013-03-12 11:29:58 -07004293 struct workqueue_struct *wq;
4294 struct pool_workqueue *pwq;
Tejun Heo611c92a2013-03-13 16:51:36 -07004295 int pi;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004296
Tejun Heo5bcab332013-03-13 19:47:40 -07004297 mutex_lock(&wq_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004298
Tejun Heo6183c002013-03-12 11:29:57 -07004299 WARN_ON_ONCE(workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004300 workqueue_freezing = true;
4301
Tejun Heo24b8a842013-03-12 11:29:58 -07004302 /* set FREEZING */
Tejun Heo611c92a2013-03-13 16:51:36 -07004303 for_each_pool(pool, pi) {
Tejun Heo5bcab332013-03-13 19:47:40 -07004304 spin_lock_irq(&pool->lock);
Tejun Heo17116962013-03-12 11:29:58 -07004305 WARN_ON_ONCE(pool->flags & POOL_FREEZING);
4306 pool->flags |= POOL_FREEZING;
Tejun Heo5bcab332013-03-13 19:47:40 -07004307 spin_unlock_irq(&pool->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004308 }
4309
Tejun Heo24b8a842013-03-12 11:29:58 -07004310 /* suppress further executions by setting max_active to zero */
Tejun Heo794b18b2013-03-13 19:47:40 -07004311 spin_lock_irq(&pwq_lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004312 list_for_each_entry(wq, &workqueues, list) {
Tejun Heo699ce092013-03-13 16:51:35 -07004313 for_each_pwq(pwq, wq)
4314 pwq_adjust_max_active(pwq);
Tejun Heo24b8a842013-03-12 11:29:58 -07004315 }
Tejun Heo794b18b2013-03-13 19:47:40 -07004316 spin_unlock_irq(&pwq_lock);
Tejun Heo5bcab332013-03-13 19:47:40 -07004317
4318 mutex_unlock(&wq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004320
4321/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01004322 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004323 *
4324 * Check whether freezing is complete. This function must be called
4325 * between freeze_workqueues_begin() and thaw_workqueues().
4326 *
4327 * CONTEXT:
Tejun Heo5bcab332013-03-13 19:47:40 -07004328 * Grabs and releases wq_mutex.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004329 *
4330 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01004331 * %true if some freezable workqueues are still busy. %false if freezing
4332 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004333 */
4334bool freeze_workqueues_busy(void)
4335{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004336 bool busy = false;
Tejun Heo24b8a842013-03-12 11:29:58 -07004337 struct workqueue_struct *wq;
4338 struct pool_workqueue *pwq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004339
Tejun Heo5bcab332013-03-13 19:47:40 -07004340 mutex_lock(&wq_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004341
Tejun Heo6183c002013-03-12 11:29:57 -07004342 WARN_ON_ONCE(!workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004343
Tejun Heo24b8a842013-03-12 11:29:58 -07004344 list_for_each_entry(wq, &workqueues, list) {
4345 if (!(wq->flags & WQ_FREEZABLE))
4346 continue;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004347 /*
4348 * nr_active is monotonically decreasing. It's safe
4349 * to peek without lock.
4350 */
Tejun Heo5bcab332013-03-13 19:47:40 -07004351 preempt_disable();
Tejun Heo24b8a842013-03-12 11:29:58 -07004352 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07004353 WARN_ON_ONCE(pwq->nr_active < 0);
Tejun Heo112202d2013-02-13 19:29:12 -08004354 if (pwq->nr_active) {
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004355 busy = true;
Tejun Heo5bcab332013-03-13 19:47:40 -07004356 preempt_enable();
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004357 goto out_unlock;
4358 }
4359 }
Tejun Heo5bcab332013-03-13 19:47:40 -07004360 preempt_enable();
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004361 }
4362out_unlock:
Tejun Heo5bcab332013-03-13 19:47:40 -07004363 mutex_unlock(&wq_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004364 return busy;
4365}
4366
4367/**
4368 * thaw_workqueues - thaw workqueues
4369 *
4370 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo706026c2013-01-24 11:01:34 -08004371 * frozen works are transferred to their respective pool worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004372 *
4373 * CONTEXT:
Tejun Heo794b18b2013-03-13 19:47:40 -07004374 * Grabs and releases wq_mutex, pwq_lock and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004375 */
4376void thaw_workqueues(void)
4377{
Tejun Heo24b8a842013-03-12 11:29:58 -07004378 struct workqueue_struct *wq;
4379 struct pool_workqueue *pwq;
4380 struct worker_pool *pool;
Tejun Heo611c92a2013-03-13 16:51:36 -07004381 int pi;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004382
Tejun Heo5bcab332013-03-13 19:47:40 -07004383 mutex_lock(&wq_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004384
4385 if (!workqueue_freezing)
4386 goto out_unlock;
4387
Tejun Heo24b8a842013-03-12 11:29:58 -07004388 /* clear FREEZING */
Tejun Heo611c92a2013-03-13 16:51:36 -07004389 for_each_pool(pool, pi) {
Tejun Heo5bcab332013-03-13 19:47:40 -07004390 spin_lock_irq(&pool->lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004391 WARN_ON_ONCE(!(pool->flags & POOL_FREEZING));
4392 pool->flags &= ~POOL_FREEZING;
Tejun Heo5bcab332013-03-13 19:47:40 -07004393 spin_unlock_irq(&pool->lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004394 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02004395
Tejun Heo24b8a842013-03-12 11:29:58 -07004396 /* restore max_active and repopulate worklist */
Tejun Heo794b18b2013-03-13 19:47:40 -07004397 spin_lock_irq(&pwq_lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004398 list_for_each_entry(wq, &workqueues, list) {
Tejun Heo699ce092013-03-13 16:51:35 -07004399 for_each_pwq(pwq, wq)
4400 pwq_adjust_max_active(pwq);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004401 }
Tejun Heo794b18b2013-03-13 19:47:40 -07004402 spin_unlock_irq(&pwq_lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004403
Tejun Heo24b8a842013-03-12 11:29:58 -07004404 /* kick workers */
Tejun Heo611c92a2013-03-13 16:51:36 -07004405 for_each_pool(pool, pi) {
Tejun Heo5bcab332013-03-13 19:47:40 -07004406 spin_lock_irq(&pool->lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004407 wake_up_worker(pool);
Tejun Heo5bcab332013-03-13 19:47:40 -07004408 spin_unlock_irq(&pool->lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004409 }
4410
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004411 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);