blob: d9a4aeb844d5cec82defb98260a60acf8264b8d2 [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 Heobce90382013-04-01 11:23:32 -070047#include <linux/nodemask.h>
Tejun Heoe22bee72010-06-29 10:07:14 +020048
Tejun Heoea138442013-01-18 14:05:55 -080049#include "workqueue_internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Tejun Heoc8e55f32010-06-29 10:07:12 +020051enum {
Tejun Heobc2ae0f2012-07-17 12:39:27 -070052 /*
Tejun Heo24647572013-01-24 11:01:33 -080053 * worker_pool flags
Tejun Heobc2ae0f2012-07-17 12:39:27 -070054 *
Tejun Heo24647572013-01-24 11:01:33 -080055 * A bound pool is either associated or disassociated with its CPU.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070056 * While associated (!DISASSOCIATED), all workers are bound to the
57 * CPU and none has %WORKER_UNBOUND set and concurrency management
58 * is in effect.
59 *
60 * While DISASSOCIATED, the cpu may be offline and all workers have
61 * %WORKER_UNBOUND set and concurrency management disabled, and may
Tejun Heo24647572013-01-24 11:01:33 -080062 * be executing on any CPU. The pool behaves as an unbound one.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070063 *
Tejun Heobc3a1af2013-03-13 19:47:39 -070064 * Note that DISASSOCIATED should be flipped only while holding
65 * manager_mutex to avoid changing binding state while
Tejun Heo24647572013-01-24 11:01:33 -080066 * create_worker() is in progress.
Tejun Heobc2ae0f2012-07-17 12:39:27 -070067 */
Tejun Heo11ebea52012-07-12 14:46:37 -070068 POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
Tejun Heo24647572013-01-24 11:01:33 -080069 POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */
Tejun Heo35b6bb62013-01-24 11:01:33 -080070 POOL_FREEZING = 1 << 3, /* freeze in progress */
Tejun Heodb7bccf2010-06-29 10:07:12 +020071
Tejun Heoc8e55f32010-06-29 10:07:12 +020072 /* worker flags */
73 WORKER_STARTED = 1 << 0, /* started */
74 WORKER_DIE = 1 << 1, /* die die die */
75 WORKER_IDLE = 1 << 2, /* is idle */
Tejun Heoe22bee72010-06-29 10:07:14 +020076 WORKER_PREP = 1 << 3, /* preparing to run works */
Tejun Heofb0e7be2010-06-29 10:07:15 +020077 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
Tejun Heof3421792010-07-02 10:03:51 +020078 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
Tejun Heoa9ab7752013-03-19 13:45:21 -070079 WORKER_REBOUND = 1 << 8, /* worker was rebound */
Tejun Heoe22bee72010-06-29 10:07:14 +020080
Tejun Heoa9ab7752013-03-19 13:45:21 -070081 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_CPU_INTENSIVE |
82 WORKER_UNBOUND | WORKER_REBOUND,
Tejun Heodb7bccf2010-06-29 10:07:12 +020083
Tejun Heoe34cdddb2013-01-24 11:01:33 -080084 NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */
Tejun Heo4ce62e92012-07-13 22:16:44 -070085
Tejun Heo29c91e92013-03-12 11:30:03 -070086 UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */
Tejun Heoc8e55f32010-06-29 10:07:12 +020087 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
Tejun Heodb7bccf2010-06-29 10:07:12 +020088
Tejun Heoe22bee72010-06-29 10:07:14 +020089 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
90 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
91
Tejun Heo3233cdb2011-02-16 18:10:19 +010092 MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2,
93 /* call for help after 10ms
94 (min two ticks) */
Tejun Heoe22bee72010-06-29 10:07:14 +020095 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
96 CREATE_COOLDOWN = HZ, /* time to breath after fail */
Tejun Heoe22bee72010-06-29 10:07:14 +020097
98 /*
99 * Rescue workers are used only on emergencies and shared by
100 * all cpus. Give -20.
101 */
102 RESCUER_NICE_LEVEL = -20,
Tejun Heo32704762012-07-13 22:16:45 -0700103 HIGHPRI_NICE_LEVEL = -20,
Tejun Heoecf68812013-04-01 11:23:34 -0700104
105 WQ_NAME_LEN = 24,
Tejun Heoc8e55f32010-06-29 10:07:12 +0200106};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108/*
Tejun Heo4690c4a2010-06-29 10:07:10 +0200109 * Structure fields follow one of the following exclusion rules.
110 *
Tejun Heoe41e7042010-08-24 14:22:47 +0200111 * I: Modifiable by initialization/destruction paths and read-only for
112 * everyone else.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200113 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200114 * P: Preemption protected. Disabling preemption is enough and should
115 * only be modified and accessed from the local cpu.
116 *
Tejun Heod565ed62013-01-24 11:01:33 -0800117 * L: pool->lock protected. Access with pool->lock held.
Tejun Heo4690c4a2010-06-29 10:07:10 +0200118 *
Tejun Heod565ed62013-01-24 11:01:33 -0800119 * X: During normal operation, modification requires pool->lock and should
120 * be done only from local cpu. Either disabling preemption on local
121 * cpu or grabbing pool->lock is enough for read access. If
122 * POOL_DISASSOCIATED is set, it's identical to L.
Tejun Heoe22bee72010-06-29 10:07:14 +0200123 *
Tejun Heo822d8402013-03-19 13:45:21 -0700124 * MG: pool->manager_mutex and pool->lock protected. Writes require both
125 * locks. Reads can happen under either lock.
126 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700127 * PL: wq_pool_mutex protected.
Tejun Heo76af4d92013-03-12 11:30:00 -0700128 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700129 * PR: wq_pool_mutex protected for writes. Sched-RCU protected for reads.
Tejun Heo5bcab332013-03-13 19:47:40 -0700130 *
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700131 * WQ: wq->mutex protected.
132 *
Lai Jiangshanb5927602013-03-25 16:57:19 -0700133 * WR: wq->mutex protected for writes. Sched-RCU 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 Heof3f90ad2013-04-01 11:23:34 -0700143 int node; /* I: the associated node ID */
Tejun Heo9daf9e62013-01-24 11:01:33 -0800144 int id; /* I: pool ID */
Tejun Heo11ebea52012-07-12 14:46:37 -0700145 unsigned int flags; /* X: flags */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700146
147 struct list_head worklist; /* L: list of pending works */
148 int nr_workers; /* L: total number of workers */
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700149
150 /* nr_idle includes the ones off idle_list for rebinding */
Tejun Heobd7bdd42012-07-12 14:46:37 -0700151 int nr_idle; /* L: currently idle ones */
152
153 struct list_head idle_list; /* X: list of idle workers */
154 struct timer_list idle_timer; /* L: worker idle timeout */
155 struct timer_list mayday_timer; /* L: SOS timer for workers */
156
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700157 /* a workers is either on busy_hash or idle_list, or the manager */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800158 DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER);
159 /* L: hash of busy workers */
160
Tejun Heobc3a1af2013-03-13 19:47:39 -0700161 /* see manage_workers() for details on the two manager mutexes */
Tejun Heo34a06bd2013-03-12 11:30:00 -0700162 struct mutex manager_arb; /* manager arbitration */
Tejun Heobc3a1af2013-03-13 19:47:39 -0700163 struct mutex manager_mutex; /* manager exclusion */
Tejun Heo822d8402013-03-19 13:45:21 -0700164 struct idr worker_idr; /* MG: worker IDs and iteration */
Tejun Heoe19e3972013-01-24 11:39:44 -0800165
Tejun Heo7a4e3442013-03-12 11:30:00 -0700166 struct workqueue_attrs *attrs; /* I: worker attributes */
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700167 struct hlist_node hash_node; /* PL: unbound_pool_hash node */
168 int refcnt; /* PL: refcnt for unbound pools */
Tejun Heo7a4e3442013-03-12 11:30:00 -0700169
Tejun Heoe19e3972013-01-24 11:39:44 -0800170 /*
171 * The current concurrency level. As it's likely to be accessed
172 * from other CPUs during try_to_wake_up(), put it in a separate
173 * cacheline.
174 */
175 atomic_t nr_running ____cacheline_aligned_in_smp;
Tejun Heo29c91e92013-03-12 11:30:03 -0700176
177 /*
178 * Destruction of pool is sched-RCU protected to allow dereferences
179 * from get_work_pool().
180 */
181 struct rcu_head rcu;
Tejun Heo8b03ae32010-06-29 10:07:12 +0200182} ____cacheline_aligned_in_smp;
183
184/*
Tejun Heo112202d2013-02-13 19:29:12 -0800185 * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS
186 * of work_struct->data are used for flags and the remaining high bits
187 * point to the pwq; thus, pwqs need to be aligned at two's power of the
188 * number of flag bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 */
Tejun Heo112202d2013-02-13 19:29:12 -0800190struct pool_workqueue {
Tejun Heobd7bdd42012-07-12 14:46:37 -0700191 struct worker_pool *pool; /* I: the associated pool */
Tejun Heo4690c4a2010-06-29 10:07:10 +0200192 struct workqueue_struct *wq; /* I: the owning workqueue */
Tejun Heo73f53c42010-06-29 10:07:11 +0200193 int work_color; /* L: current color */
194 int flush_color; /* L: flushing color */
Tejun Heo8864b4e2013-03-12 11:30:04 -0700195 int refcnt; /* L: reference count */
Tejun Heo73f53c42010-06-29 10:07:11 +0200196 int nr_in_flight[WORK_NR_COLORS];
197 /* L: nr of in_flight works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200198 int nr_active; /* L: nr of active works */
Tejun Heoa0a1a5f2010-06-29 10:07:12 +0200199 int max_active; /* L: max active works */
Tejun Heo1e19ffc2010-06-29 10:07:12 +0200200 struct list_head delayed_works; /* L: delayed works */
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700201 struct list_head pwqs_node; /* WR: node on wq->pwqs */
Tejun Heo2e109a22013-03-13 19:47:40 -0700202 struct list_head mayday_node; /* MD: node on wq->maydays */
Tejun Heo8864b4e2013-03-12 11:30:04 -0700203
204 /*
205 * Release of unbound pwq is punted to system_wq. See put_pwq()
206 * and pwq_unbound_release_workfn() for details. pool_workqueue
207 * itself is also sched-RCU protected so that the first pwq can be
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700208 * determined without grabbing wq->mutex.
Tejun Heo8864b4e2013-03-12 11:30:04 -0700209 */
210 struct work_struct unbound_release_work;
211 struct rcu_head rcu;
Tejun Heoe904e6c2013-03-12 11:29:57 -0700212} __aligned(1 << WORK_STRUCT_FLAG_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214/*
Tejun Heo73f53c42010-06-29 10:07:11 +0200215 * Structure used to wait for workqueue flush.
216 */
217struct wq_flusher {
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700218 struct list_head list; /* WQ: list of flushers */
219 int flush_color; /* WQ: flush color waiting for */
Tejun Heo73f53c42010-06-29 10:07:11 +0200220 struct completion done; /* flush completion */
221};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Tejun Heo226223a2013-03-12 11:30:05 -0700223struct wq_device;
224
Tejun Heo73f53c42010-06-29 10:07:11 +0200225/*
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700226 * The externally visible workqueue. It relays the issued work items to
227 * the appropriate worker_pool through its pool_workqueues.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 */
229struct workqueue_struct {
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700230 struct list_head pwqs; /* WR: all pwqs of this wq */
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700231 struct list_head list; /* PL: list of all workqueues */
Tejun Heo73f53c42010-06-29 10:07:11 +0200232
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700233 struct mutex mutex; /* protects this wq */
234 int work_color; /* WQ: current work color */
235 int flush_color; /* WQ: current flush color */
Tejun Heo112202d2013-02-13 19:29:12 -0800236 atomic_t nr_pwqs_to_flush; /* flush in progress */
Lai Jiangshan3c25a552013-03-25 16:57:17 -0700237 struct wq_flusher *first_flusher; /* WQ: first flusher */
238 struct list_head flusher_queue; /* WQ: flush waiters */
239 struct list_head flusher_overflow; /* WQ: flush overflow list */
Tejun Heo73f53c42010-06-29 10:07:11 +0200240
Tejun Heo2e109a22013-03-13 19:47:40 -0700241 struct list_head maydays; /* MD: pwqs requesting rescue */
Tejun Heoe22bee72010-06-29 10:07:14 +0200242 struct worker *rescuer; /* I: rescue worker */
243
Lai Jiangshan87fc7412013-03-25 16:57:18 -0700244 int nr_drainers; /* WQ: drain in progress */
Lai Jiangshana357fc02013-03-25 16:57:19 -0700245 int saved_max_active; /* WQ: saved pwq max_active */
Tejun Heo226223a2013-03-12 11:30:05 -0700246
Tejun Heo6029a912013-04-01 11:23:34 -0700247 struct workqueue_attrs *unbound_attrs; /* WQ: only for unbound wqs */
248
Tejun Heo226223a2013-03-12 11:30:05 -0700249#ifdef CONFIG_SYSFS
250 struct wq_device *wq_dev; /* I: for sysfs interface */
251#endif
Johannes Berg4e6045f2007-10-18 23:39:55 -0700252#ifdef CONFIG_LOCKDEP
Tejun Heo4690c4a2010-06-29 10:07:10 +0200253 struct lockdep_map lockdep_map;
Johannes Berg4e6045f2007-10-18 23:39:55 -0700254#endif
Tejun Heoecf68812013-04-01 11:23:34 -0700255 char name[WQ_NAME_LEN]; /* I: workqueue name */
Tejun Heo2728fd22013-04-01 11:23:35 -0700256
257 /* hot fields used during command issue, aligned to cacheline */
258 unsigned int flags ____cacheline_aligned; /* WQ: WQ_* flags */
259 struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwqs */
Tejun Heodf2d5ae2013-04-01 11:23:35 -0700260 struct pool_workqueue __rcu *numa_pwq_tbl[]; /* FR: unbound pwqs indexed by node */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261};
262
Tejun Heoe904e6c2013-03-12 11:29:57 -0700263static struct kmem_cache *pwq_cache;
264
Tejun Heobce90382013-04-01 11:23:32 -0700265static int wq_numa_tbl_len; /* highest possible NUMA node id + 1 */
266static cpumask_var_t *wq_numa_possible_cpumask;
267 /* possible CPUs of each node */
268
269static bool wq_numa_enabled; /* unbound NUMA affinity enabled */
270
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700271static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */
Tejun Heo2e109a22013-03-13 19:47:40 -0700272static DEFINE_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */
Tejun Heo5bcab332013-03-13 19:47:40 -0700273
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700274static LIST_HEAD(workqueues); /* PL: list of all workqueues */
275static bool workqueue_freezing; /* PL: have wqs started freezing? */
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700276
277/* the per-cpu worker pools */
278static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS],
279 cpu_worker_pools);
280
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700281static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700282
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700283/* PL: hash of all unbound pools keyed by pool->attrs */
Tejun Heo29c91e92013-03-12 11:30:03 -0700284static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER);
285
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700286/* I: attributes used when instantiating standard unbound pools on demand */
Tejun Heo29c91e92013-03-12 11:30:03 -0700287static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS];
288
Tejun Heod320c032010-06-29 10:07:14 +0200289struct workqueue_struct *system_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200290EXPORT_SYMBOL_GPL(system_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300291struct workqueue_struct *system_highpri_wq __read_mostly;
Joonsoo Kim1aabe902012-08-15 23:25:39 +0900292EXPORT_SYMBOL_GPL(system_highpri_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300293struct workqueue_struct *system_long_wq __read_mostly;
Tejun Heod320c032010-06-29 10:07:14 +0200294EXPORT_SYMBOL_GPL(system_long_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300295struct workqueue_struct *system_unbound_wq __read_mostly;
Tejun Heof3421792010-07-02 10:03:51 +0200296EXPORT_SYMBOL_GPL(system_unbound_wq);
Valentin Ilie044c7822012-08-19 00:52:42 +0300297struct workqueue_struct *system_freezable_wq __read_mostly;
Tejun Heo24d51ad2011-02-21 09:52:50 +0100298EXPORT_SYMBOL_GPL(system_freezable_wq);
Tejun Heod320c032010-06-29 10:07:14 +0200299
Tejun Heo7d19c5c2013-03-13 19:47:40 -0700300static int worker_thread(void *__worker);
301static void copy_workqueue_attrs(struct workqueue_attrs *to,
302 const struct workqueue_attrs *from);
303
Tejun Heo97bd2342010-10-05 10:41:14 +0200304#define CREATE_TRACE_POINTS
305#include <trace/events/workqueue.h>
306
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700307#define assert_rcu_or_pool_mutex() \
Tejun Heo5bcab332013-03-13 19:47:40 -0700308 rcu_lockdep_assert(rcu_read_lock_sched_held() || \
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700309 lockdep_is_held(&wq_pool_mutex), \
310 "sched RCU or wq_pool_mutex should be held")
Tejun Heo5bcab332013-03-13 19:47:40 -0700311
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700312#define assert_rcu_or_wq_mutex(wq) \
Tejun Heo76af4d92013-03-12 11:30:00 -0700313 rcu_lockdep_assert(rcu_read_lock_sched_held() || \
Lai Jiangshanb5927602013-03-25 16:57:19 -0700314 lockdep_is_held(&wq->mutex), \
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700315 "sched RCU or wq->mutex should be held")
Tejun Heo76af4d92013-03-12 11:30:00 -0700316
Tejun Heo822d8402013-03-19 13:45:21 -0700317#ifdef CONFIG_LOCKDEP
318#define assert_manager_or_pool_lock(pool) \
Lai Jiangshan519e3c12013-03-20 03:28:21 +0800319 WARN_ONCE(debug_locks && \
320 !lockdep_is_held(&(pool)->manager_mutex) && \
Tejun Heo822d8402013-03-19 13:45:21 -0700321 !lockdep_is_held(&(pool)->lock), \
322 "pool->manager_mutex or ->lock should be held")
323#else
324#define assert_manager_or_pool_lock(pool) do { } while (0)
325#endif
326
Tejun Heof02ae732013-03-12 11:30:03 -0700327#define for_each_cpu_worker_pool(pool, cpu) \
328 for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \
329 (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \
Tejun Heo7a62c2c2013-03-12 11:30:03 -0700330 (pool)++)
Tejun Heo4ce62e92012-07-13 22:16:44 -0700331
Tejun Heo49e3cf42013-03-12 11:29:58 -0700332/**
Tejun Heo17116962013-03-12 11:29:58 -0700333 * for_each_pool - iterate through all worker_pools in the system
334 * @pool: iteration cursor
Tejun Heo611c92a2013-03-13 16:51:36 -0700335 * @pi: integer used for iteration
Tejun Heofa1b54e2013-03-12 11:30:00 -0700336 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700337 * This must be called either with wq_pool_mutex held or sched RCU read
338 * locked. If the pool needs to be used beyond the locking in effect, the
339 * caller is responsible for guaranteeing that the pool stays online.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700340 *
341 * The if/else clause exists only for the lockdep assertion and can be
342 * ignored.
Tejun Heo17116962013-03-12 11:29:58 -0700343 */
Tejun Heo611c92a2013-03-13 16:51:36 -0700344#define for_each_pool(pool, pi) \
345 idr_for_each_entry(&worker_pool_idr, pool, pi) \
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700346 if (({ assert_rcu_or_pool_mutex(); false; })) { } \
Tejun Heofa1b54e2013-03-12 11:30:00 -0700347 else
Tejun Heo17116962013-03-12 11:29:58 -0700348
349/**
Tejun Heo822d8402013-03-19 13:45:21 -0700350 * for_each_pool_worker - iterate through all workers of a worker_pool
351 * @worker: iteration cursor
352 * @wi: integer used for iteration
353 * @pool: worker_pool to iterate workers of
354 *
355 * This must be called with either @pool->manager_mutex or ->lock held.
356 *
357 * The if/else clause exists only for the lockdep assertion and can be
358 * ignored.
359 */
360#define for_each_pool_worker(worker, wi, pool) \
361 idr_for_each_entry(&(pool)->worker_idr, (worker), (wi)) \
362 if (({ assert_manager_or_pool_lock((pool)); false; })) { } \
363 else
364
365/**
Tejun Heo49e3cf42013-03-12 11:29:58 -0700366 * for_each_pwq - iterate through all pool_workqueues of the specified workqueue
367 * @pwq: iteration cursor
368 * @wq: the target workqueue
Tejun Heo76af4d92013-03-12 11:30:00 -0700369 *
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700370 * This must be called either with wq->mutex held or sched RCU read locked.
Tejun Heo794b18b2013-03-13 19:47:40 -0700371 * If the pwq needs to be used beyond the locking in effect, the caller is
372 * responsible for guaranteeing that the pwq stays online.
Tejun Heo76af4d92013-03-12 11:30:00 -0700373 *
374 * The if/else clause exists only for the lockdep assertion and can be
375 * ignored.
Tejun Heo49e3cf42013-03-12 11:29:58 -0700376 */
377#define for_each_pwq(pwq, wq) \
Tejun Heo76af4d92013-03-12 11:30:00 -0700378 list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node) \
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700379 if (({ assert_rcu_or_wq_mutex(wq); false; })) { } \
Tejun Heo76af4d92013-03-12 11:30:00 -0700380 else
Tejun Heof3421792010-07-02 10:03:51 +0200381
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900382#ifdef CONFIG_DEBUG_OBJECTS_WORK
383
384static struct debug_obj_descr work_debug_descr;
385
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100386static void *work_debug_hint(void *addr)
387{
388 return ((struct work_struct *) addr)->func;
389}
390
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900391/*
392 * fixup_init is called when:
393 * - an active object is initialized
394 */
395static int work_fixup_init(void *addr, enum debug_obj_state state)
396{
397 struct work_struct *work = addr;
398
399 switch (state) {
400 case ODEBUG_STATE_ACTIVE:
401 cancel_work_sync(work);
402 debug_object_init(work, &work_debug_descr);
403 return 1;
404 default:
405 return 0;
406 }
407}
408
409/*
410 * fixup_activate is called when:
411 * - an active object is activated
412 * - an unknown object is activated (might be a statically initialized object)
413 */
414static int work_fixup_activate(void *addr, enum debug_obj_state state)
415{
416 struct work_struct *work = addr;
417
418 switch (state) {
419
420 case ODEBUG_STATE_NOTAVAILABLE:
421 /*
422 * This is not really a fixup. The work struct was
423 * statically initialized. We just make sure that it
424 * is tracked in the object tracker.
425 */
Tejun Heo22df02b2010-06-29 10:07:10 +0200426 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900427 debug_object_init(work, &work_debug_descr);
428 debug_object_activate(work, &work_debug_descr);
429 return 0;
430 }
431 WARN_ON_ONCE(1);
432 return 0;
433
434 case ODEBUG_STATE_ACTIVE:
435 WARN_ON(1);
436
437 default:
438 return 0;
439 }
440}
441
442/*
443 * fixup_free is called when:
444 * - an active object is freed
445 */
446static int work_fixup_free(void *addr, enum debug_obj_state state)
447{
448 struct work_struct *work = addr;
449
450 switch (state) {
451 case ODEBUG_STATE_ACTIVE:
452 cancel_work_sync(work);
453 debug_object_free(work, &work_debug_descr);
454 return 1;
455 default:
456 return 0;
457 }
458}
459
460static struct debug_obj_descr work_debug_descr = {
461 .name = "work_struct",
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100462 .debug_hint = work_debug_hint,
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900463 .fixup_init = work_fixup_init,
464 .fixup_activate = work_fixup_activate,
465 .fixup_free = work_fixup_free,
466};
467
468static inline void debug_work_activate(struct work_struct *work)
469{
470 debug_object_activate(work, &work_debug_descr);
471}
472
473static inline void debug_work_deactivate(struct work_struct *work)
474{
475 debug_object_deactivate(work, &work_debug_descr);
476}
477
478void __init_work(struct work_struct *work, int onstack)
479{
480 if (onstack)
481 debug_object_init_on_stack(work, &work_debug_descr);
482 else
483 debug_object_init(work, &work_debug_descr);
484}
485EXPORT_SYMBOL_GPL(__init_work);
486
487void destroy_work_on_stack(struct work_struct *work)
488{
489 debug_object_free(work, &work_debug_descr);
490}
491EXPORT_SYMBOL_GPL(destroy_work_on_stack);
492
493#else
494static inline void debug_work_activate(struct work_struct *work) { }
495static inline void debug_work_deactivate(struct work_struct *work) { }
496#endif
497
Tejun Heo9daf9e62013-01-24 11:01:33 -0800498/* allocate ID and assign it to @pool */
499static int worker_pool_assign_id(struct worker_pool *pool)
500{
501 int ret;
502
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700503 lockdep_assert_held(&wq_pool_mutex);
Tejun Heo5bcab332013-03-13 19:47:40 -0700504
Tejun Heofa1b54e2013-03-12 11:30:00 -0700505 do {
506 if (!idr_pre_get(&worker_pool_idr, GFP_KERNEL))
507 return -ENOMEM;
Tejun Heofa1b54e2013-03-12 11:30:00 -0700508 ret = idr_get_new(&worker_pool_idr, pool, &pool->id);
Tejun Heofa1b54e2013-03-12 11:30:00 -0700509 } while (ret == -EAGAIN);
Tejun Heo9daf9e62013-01-24 11:01:33 -0800510
511 return ret;
512}
513
Tejun Heo76af4d92013-03-12 11:30:00 -0700514/**
515 * first_pwq - return the first pool_workqueue of the specified workqueue
516 * @wq: the target workqueue
517 *
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700518 * This must be called either with wq->mutex held or sched RCU read locked.
Tejun Heo794b18b2013-03-13 19:47:40 -0700519 * If the pwq needs to be used beyond the locking in effect, the caller is
520 * responsible for guaranteeing that the pwq stays online.
Tejun Heo76af4d92013-03-12 11:30:00 -0700521 */
Tejun Heo7fb98ea2013-03-12 11:30:00 -0700522static struct pool_workqueue *first_pwq(struct workqueue_struct *wq)
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700523{
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -0700524 assert_rcu_or_wq_mutex(wq);
Tejun Heo76af4d92013-03-12 11:30:00 -0700525 return list_first_or_null_rcu(&wq->pwqs, struct pool_workqueue,
526 pwqs_node);
Oleg Nesterova848e3b2007-05-09 02:34:17 -0700527}
528
Tejun Heodf2d5ae2013-04-01 11:23:35 -0700529/**
530 * unbound_pwq_by_node - return the unbound pool_workqueue for the given node
531 * @wq: the target workqueue
532 * @node: the node ID
533 *
534 * This must be called either with pwq_lock held or sched RCU read locked.
535 * If the pwq needs to be used beyond the locking in effect, the caller is
536 * responsible for guaranteeing that the pwq stays online.
537 */
538static struct pool_workqueue *unbound_pwq_by_node(struct workqueue_struct *wq,
539 int node)
540{
541 assert_rcu_or_wq_mutex(wq);
542 return rcu_dereference_raw(wq->numa_pwq_tbl[node]);
543}
544
Tejun Heo73f53c42010-06-29 10:07:11 +0200545static unsigned int work_color_to_flags(int color)
546{
547 return color << WORK_STRUCT_COLOR_SHIFT;
548}
549
550static int get_work_color(struct work_struct *work)
551{
552 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
553 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
554}
555
556static int work_next_color(int color)
557{
558 return (color + 1) % WORK_NR_COLORS;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -0700559}
560
David Howells4594bf12006-12-07 11:33:26 +0000561/*
Tejun Heo112202d2013-02-13 19:29:12 -0800562 * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data
563 * contain the pointer to the queued pwq. Once execution starts, the flag
Tejun Heo7c3eed52013-01-24 11:01:33 -0800564 * is cleared and the high bits contain OFFQ flags and pool ID.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200565 *
Tejun Heo112202d2013-02-13 19:29:12 -0800566 * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling()
567 * and clear_work_data() can be used to set the pwq, pool or clear
Tejun Heobbb68df2012-08-03 10:30:46 -0700568 * work->data. These functions should only be called while the work is
569 * owned - ie. while the PENDING bit is set.
Tejun Heo7a22ad72010-06-29 10:07:13 +0200570 *
Tejun Heo112202d2013-02-13 19:29:12 -0800571 * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq
Tejun Heo7c3eed52013-01-24 11:01:33 -0800572 * corresponding to a work. Pool is available once the work has been
Tejun Heo112202d2013-02-13 19:29:12 -0800573 * queued anywhere after initialization until it is sync canceled. pwq is
Tejun Heo7c3eed52013-01-24 11:01:33 -0800574 * available only while the work item is queued.
Tejun Heobbb68df2012-08-03 10:30:46 -0700575 *
576 * %WORK_OFFQ_CANCELING is used to mark a work item which is being
577 * canceled. While being canceled, a work item may have its PENDING set
578 * but stay off timer and worklist for arbitrarily long and nobody should
579 * try to steal the PENDING bit.
David Howells4594bf12006-12-07 11:33:26 +0000580 */
Tejun Heo7a22ad72010-06-29 10:07:13 +0200581static inline void set_work_data(struct work_struct *work, unsigned long data,
582 unsigned long flags)
David Howells365970a2006-11-22 14:54:49 +0000583{
Tejun Heo6183c002013-03-12 11:29:57 -0700584 WARN_ON_ONCE(!work_pending(work));
Tejun Heo7a22ad72010-06-29 10:07:13 +0200585 atomic_long_set(&work->data, data | flags | work_static(work));
David Howells365970a2006-11-22 14:54:49 +0000586}
David Howells365970a2006-11-22 14:54:49 +0000587
Tejun Heo112202d2013-02-13 19:29:12 -0800588static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq,
Tejun Heo7a22ad72010-06-29 10:07:13 +0200589 unsigned long extra_flags)
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200590{
Tejun Heo112202d2013-02-13 19:29:12 -0800591 set_work_data(work, (unsigned long)pwq,
592 WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags);
Oleg Nesterov4d707b92010-04-23 17:40:40 +0200593}
594
Lai Jiangshan4468a002013-02-06 18:04:53 -0800595static void set_work_pool_and_keep_pending(struct work_struct *work,
596 int pool_id)
597{
598 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT,
599 WORK_STRUCT_PENDING);
600}
601
Tejun Heo7c3eed52013-01-24 11:01:33 -0800602static void set_work_pool_and_clear_pending(struct work_struct *work,
603 int pool_id)
David Howells365970a2006-11-22 14:54:49 +0000604{
Tejun Heo23657bb2012-08-13 17:08:19 -0700605 /*
606 * The following wmb is paired with the implied mb in
607 * test_and_set_bit(PENDING) and ensures all updates to @work made
608 * here are visible to and precede any updates by the next PENDING
609 * owner.
610 */
611 smp_wmb();
Tejun Heo7c3eed52013-01-24 11:01:33 -0800612 set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200613}
614
615static void clear_work_data(struct work_struct *work)
616{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800617 smp_wmb(); /* see set_work_pool_and_clear_pending() */
618 set_work_data(work, WORK_STRUCT_NO_POOL, 0);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200619}
620
Tejun Heo112202d2013-02-13 19:29:12 -0800621static struct pool_workqueue *get_work_pwq(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200622{
Tejun Heoe1201532010-07-22 14:14:25 +0200623 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7a22ad72010-06-29 10:07:13 +0200624
Tejun Heo112202d2013-02-13 19:29:12 -0800625 if (data & WORK_STRUCT_PWQ)
Tejun Heoe1201532010-07-22 14:14:25 +0200626 return (void *)(data & WORK_STRUCT_WQ_DATA_MASK);
627 else
628 return NULL;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200629}
630
Tejun Heo7c3eed52013-01-24 11:01:33 -0800631/**
632 * get_work_pool - return the worker_pool a given work was associated with
633 * @work: the work item of interest
634 *
635 * Return the worker_pool @work was last associated with. %NULL if none.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700636 *
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700637 * Pools are created and destroyed under wq_pool_mutex, and allows read
638 * access under sched-RCU read lock. As such, this function should be
639 * called under wq_pool_mutex or with preemption disabled.
Tejun Heofa1b54e2013-03-12 11:30:00 -0700640 *
641 * All fields of the returned pool are accessible as long as the above
642 * mentioned locking is in effect. If the returned pool needs to be used
643 * beyond the critical section, the caller is responsible for ensuring the
644 * returned pool is and stays online.
Tejun Heo7c3eed52013-01-24 11:01:33 -0800645 */
646static struct worker_pool *get_work_pool(struct work_struct *work)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200647{
Tejun Heoe1201532010-07-22 14:14:25 +0200648 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800649 int pool_id;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200650
Lai Jiangshan68e13a62013-03-25 16:57:17 -0700651 assert_rcu_or_pool_mutex();
Tejun Heofa1b54e2013-03-12 11:30:00 -0700652
Tejun Heo112202d2013-02-13 19:29:12 -0800653 if (data & WORK_STRUCT_PWQ)
654 return ((struct pool_workqueue *)
Tejun Heo7c3eed52013-01-24 11:01:33 -0800655 (data & WORK_STRUCT_WQ_DATA_MASK))->pool;
Tejun Heo7a22ad72010-06-29 10:07:13 +0200656
Tejun Heo7c3eed52013-01-24 11:01:33 -0800657 pool_id = data >> WORK_OFFQ_POOL_SHIFT;
658 if (pool_id == WORK_OFFQ_POOL_NONE)
Tejun Heo7a22ad72010-06-29 10:07:13 +0200659 return NULL;
660
Tejun Heofa1b54e2013-03-12 11:30:00 -0700661 return idr_find(&worker_pool_idr, pool_id);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800662}
663
664/**
665 * get_work_pool_id - return the worker pool ID a given work is associated with
666 * @work: the work item of interest
667 *
668 * Return the worker_pool ID @work was last associated with.
669 * %WORK_OFFQ_POOL_NONE if none.
670 */
671static int get_work_pool_id(struct work_struct *work)
672{
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800673 unsigned long data = atomic_long_read(&work->data);
Tejun Heo7c3eed52013-01-24 11:01:33 -0800674
Tejun Heo112202d2013-02-13 19:29:12 -0800675 if (data & WORK_STRUCT_PWQ)
676 return ((struct pool_workqueue *)
Lai Jiangshan54d5b7d2013-02-07 13:14:20 -0800677 (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id;
678
679 return data >> WORK_OFFQ_POOL_SHIFT;
Tejun Heo7c3eed52013-01-24 11:01:33 -0800680}
681
Tejun Heobbb68df2012-08-03 10:30:46 -0700682static void mark_work_canceling(struct work_struct *work)
683{
Tejun Heo7c3eed52013-01-24 11:01:33 -0800684 unsigned long pool_id = get_work_pool_id(work);
Tejun Heobbb68df2012-08-03 10:30:46 -0700685
Tejun Heo7c3eed52013-01-24 11:01:33 -0800686 pool_id <<= WORK_OFFQ_POOL_SHIFT;
687 set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700688}
689
690static bool work_is_canceling(struct work_struct *work)
691{
692 unsigned long data = atomic_long_read(&work->data);
693
Tejun Heo112202d2013-02-13 19:29:12 -0800694 return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING);
Tejun Heobbb68df2012-08-03 10:30:46 -0700695}
696
David Howells365970a2006-11-22 14:54:49 +0000697/*
Tejun Heo32704762012-07-13 22:16:45 -0700698 * Policy functions. These define the policies on how the global worker
699 * pools are managed. Unless noted otherwise, these functions assume that
Tejun Heod565ed62013-01-24 11:01:33 -0800700 * they're being called with pool->lock held.
David Howells365970a2006-11-22 14:54:49 +0000701 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200702
Tejun Heo63d95a92012-07-12 14:46:37 -0700703static bool __need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000704{
Tejun Heoe19e3972013-01-24 11:39:44 -0800705 return !atomic_read(&pool->nr_running);
David Howells365970a2006-11-22 14:54:49 +0000706}
707
Tejun Heoe22bee72010-06-29 10:07:14 +0200708/*
709 * Need to wake up a worker? Called from anything but currently
710 * running workers.
Tejun Heo974271c2012-07-12 14:46:37 -0700711 *
712 * Note that, because unbound workers never contribute to nr_running, this
Tejun Heo706026c2013-01-24 11:01:34 -0800713 * function will always return %true for unbound pools as long as the
Tejun Heo974271c2012-07-12 14:46:37 -0700714 * worklist isn't empty.
Tejun Heoe22bee72010-06-29 10:07:14 +0200715 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700716static bool need_more_worker(struct worker_pool *pool)
David Howells365970a2006-11-22 14:54:49 +0000717{
Tejun Heo63d95a92012-07-12 14:46:37 -0700718 return !list_empty(&pool->worklist) && __need_more_worker(pool);
David Howells365970a2006-11-22 14:54:49 +0000719}
720
Tejun Heoe22bee72010-06-29 10:07:14 +0200721/* Can I start working? Called from busy but !running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700722static bool may_start_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200723{
Tejun Heo63d95a92012-07-12 14:46:37 -0700724 return pool->nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200725}
726
727/* Do I need to keep working? Called from currently running workers. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700728static bool keep_working(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200729{
Tejun Heoe19e3972013-01-24 11:39:44 -0800730 return !list_empty(&pool->worklist) &&
731 atomic_read(&pool->nr_running) <= 1;
Tejun Heoe22bee72010-06-29 10:07:14 +0200732}
733
734/* Do we need a new worker? Called from manager. */
Tejun Heo63d95a92012-07-12 14:46:37 -0700735static bool need_to_create_worker(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200736{
Tejun Heo63d95a92012-07-12 14:46:37 -0700737 return need_more_worker(pool) && !may_start_working(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200738}
739
740/* Do I need to be the manager? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700741static bool need_to_manage_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200742{
Tejun Heo63d95a92012-07-12 14:46:37 -0700743 return need_to_create_worker(pool) ||
Tejun Heo11ebea52012-07-12 14:46:37 -0700744 (pool->flags & POOL_MANAGE_WORKERS);
Tejun Heoe22bee72010-06-29 10:07:14 +0200745}
746
747/* Do we have too many workers and should some go away? */
Tejun Heo63d95a92012-07-12 14:46:37 -0700748static bool too_many_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +0200749{
Tejun Heo34a06bd2013-03-12 11:30:00 -0700750 bool managing = mutex_is_locked(&pool->manager_arb);
Tejun Heo63d95a92012-07-12 14:46:37 -0700751 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
752 int nr_busy = pool->nr_workers - nr_idle;
Tejun Heoe22bee72010-06-29 10:07:14 +0200753
Lai Jiangshanea1abd62012-09-18 09:59:22 -0700754 /*
755 * nr_idle and idle_list may disagree if idle rebinding is in
756 * progress. Never return %true if idle_list is empty.
757 */
758 if (list_empty(&pool->idle_list))
759 return false;
760
Tejun Heoe22bee72010-06-29 10:07:14 +0200761 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
762}
763
764/*
765 * Wake up functions.
766 */
767
Tejun Heo7e116292010-06-29 10:07:13 +0200768/* Return the first worker. Safe with preemption disabled */
Tejun Heo63d95a92012-07-12 14:46:37 -0700769static struct worker *first_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200770{
Tejun Heo63d95a92012-07-12 14:46:37 -0700771 if (unlikely(list_empty(&pool->idle_list)))
Tejun Heo7e116292010-06-29 10:07:13 +0200772 return NULL;
773
Tejun Heo63d95a92012-07-12 14:46:37 -0700774 return list_first_entry(&pool->idle_list, struct worker, entry);
Tejun Heo7e116292010-06-29 10:07:13 +0200775}
776
777/**
778 * wake_up_worker - wake up an idle worker
Tejun Heo63d95a92012-07-12 14:46:37 -0700779 * @pool: worker pool to wake worker from
Tejun Heo7e116292010-06-29 10:07:13 +0200780 *
Tejun Heo63d95a92012-07-12 14:46:37 -0700781 * Wake up the first idle worker of @pool.
Tejun Heo7e116292010-06-29 10:07:13 +0200782 *
783 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800784 * spin_lock_irq(pool->lock).
Tejun Heo7e116292010-06-29 10:07:13 +0200785 */
Tejun Heo63d95a92012-07-12 14:46:37 -0700786static void wake_up_worker(struct worker_pool *pool)
Tejun Heo7e116292010-06-29 10:07:13 +0200787{
Tejun Heo63d95a92012-07-12 14:46:37 -0700788 struct worker *worker = first_worker(pool);
Tejun Heo7e116292010-06-29 10:07:13 +0200789
790 if (likely(worker))
791 wake_up_process(worker->task);
792}
793
Tejun Heo4690c4a2010-06-29 10:07:10 +0200794/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200795 * wq_worker_waking_up - a worker is waking up
796 * @task: task waking up
797 * @cpu: CPU @task is waking up to
798 *
799 * This function is called during try_to_wake_up() when a worker is
800 * being awoken.
801 *
802 * CONTEXT:
803 * spin_lock_irq(rq->lock)
804 */
Tejun Heod84ff052013-03-12 11:29:59 -0700805void wq_worker_waking_up(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200806{
807 struct worker *worker = kthread_data(task);
808
Joonsoo Kim36576002012-10-26 23:03:49 +0900809 if (!(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoec22ca52013-01-24 11:01:33 -0800810 WARN_ON_ONCE(worker->pool->cpu != cpu);
Tejun Heoe19e3972013-01-24 11:39:44 -0800811 atomic_inc(&worker->pool->nr_running);
Joonsoo Kim36576002012-10-26 23:03:49 +0900812 }
Tejun Heoe22bee72010-06-29 10:07:14 +0200813}
814
815/**
816 * wq_worker_sleeping - a worker is going to sleep
817 * @task: task going to sleep
818 * @cpu: CPU in question, must be the current CPU number
819 *
820 * This function is called during schedule() when a busy worker is
821 * going to sleep. Worker on the same cpu can be woken up by
822 * returning pointer to its task.
823 *
824 * CONTEXT:
825 * spin_lock_irq(rq->lock)
826 *
827 * RETURNS:
828 * Worker task on @cpu to wake up, %NULL if none.
829 */
Tejun Heod84ff052013-03-12 11:29:59 -0700830struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
Tejun Heoe22bee72010-06-29 10:07:14 +0200831{
832 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
Tejun Heo111c2252013-01-17 17:16:24 -0800833 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200834
Tejun Heo111c2252013-01-17 17:16:24 -0800835 /*
836 * Rescuers, which may not have all the fields set up like normal
837 * workers, also reach here, let's not access anything before
838 * checking NOT_RUNNING.
839 */
Steven Rostedt2d646722010-12-03 23:12:33 -0500840 if (worker->flags & WORKER_NOT_RUNNING)
Tejun Heoe22bee72010-06-29 10:07:14 +0200841 return NULL;
842
Tejun Heo111c2252013-01-17 17:16:24 -0800843 pool = worker->pool;
Tejun Heo111c2252013-01-17 17:16:24 -0800844
Tejun Heoe22bee72010-06-29 10:07:14 +0200845 /* this can only happen on the local cpu */
Tejun Heo6183c002013-03-12 11:29:57 -0700846 if (WARN_ON_ONCE(cpu != raw_smp_processor_id()))
847 return NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +0200848
849 /*
850 * The counterpart of the following dec_and_test, implied mb,
851 * worklist not empty test sequence is in insert_work().
852 * Please read comment there.
853 *
Tejun Heo628c78e2012-07-17 12:39:27 -0700854 * NOT_RUNNING is clear. This means that we're bound to and
855 * running on the local cpu w/ rq lock held and preemption
856 * disabled, which in turn means that none else could be
Tejun Heod565ed62013-01-24 11:01:33 -0800857 * manipulating idle_list, so dereferencing idle_list without pool
Tejun Heo628c78e2012-07-17 12:39:27 -0700858 * lock is safe.
Tejun Heoe22bee72010-06-29 10:07:14 +0200859 */
Tejun Heoe19e3972013-01-24 11:39:44 -0800860 if (atomic_dec_and_test(&pool->nr_running) &&
861 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700862 to_wakeup = first_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200863 return to_wakeup ? to_wakeup->task : NULL;
864}
865
866/**
867 * worker_set_flags - set worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200868 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200869 * @flags: flags to set
870 * @wakeup: wakeup an idle worker if necessary
871 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200872 * Set @flags in @worker->flags and adjust nr_running accordingly. If
873 * nr_running becomes zero and @wakeup is %true, an idle worker is
874 * woken up.
Tejun Heod302f012010-06-29 10:07:13 +0200875 *
Tejun Heocb444762010-07-02 10:03:50 +0200876 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800877 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200878 */
879static inline void worker_set_flags(struct worker *worker, unsigned int flags,
880 bool wakeup)
881{
Tejun Heobd7bdd42012-07-12 14:46:37 -0700882 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200883
Tejun Heocb444762010-07-02 10:03:50 +0200884 WARN_ON_ONCE(worker->task != current);
885
Tejun Heoe22bee72010-06-29 10:07:14 +0200886 /*
887 * If transitioning into NOT_RUNNING, adjust nr_running and
888 * wake up an idle worker as necessary if requested by
889 * @wakeup.
890 */
891 if ((flags & WORKER_NOT_RUNNING) &&
892 !(worker->flags & WORKER_NOT_RUNNING)) {
Tejun Heoe22bee72010-06-29 10:07:14 +0200893 if (wakeup) {
Tejun Heoe19e3972013-01-24 11:39:44 -0800894 if (atomic_dec_and_test(&pool->nr_running) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -0700895 !list_empty(&pool->worklist))
Tejun Heo63d95a92012-07-12 14:46:37 -0700896 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +0200897 } else
Tejun Heoe19e3972013-01-24 11:39:44 -0800898 atomic_dec(&pool->nr_running);
Tejun Heoe22bee72010-06-29 10:07:14 +0200899 }
900
Tejun Heod302f012010-06-29 10:07:13 +0200901 worker->flags |= flags;
902}
903
904/**
Tejun Heoe22bee72010-06-29 10:07:14 +0200905 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
Tejun Heocb444762010-07-02 10:03:50 +0200906 * @worker: self
Tejun Heod302f012010-06-29 10:07:13 +0200907 * @flags: flags to clear
908 *
Tejun Heoe22bee72010-06-29 10:07:14 +0200909 * Clear @flags in @worker->flags and adjust nr_running accordingly.
Tejun Heod302f012010-06-29 10:07:13 +0200910 *
Tejun Heocb444762010-07-02 10:03:50 +0200911 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800912 * spin_lock_irq(pool->lock)
Tejun Heod302f012010-06-29 10:07:13 +0200913 */
914static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
915{
Tejun Heo63d95a92012-07-12 14:46:37 -0700916 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +0200917 unsigned int oflags = worker->flags;
918
Tejun Heocb444762010-07-02 10:03:50 +0200919 WARN_ON_ONCE(worker->task != current);
920
Tejun Heod302f012010-06-29 10:07:13 +0200921 worker->flags &= ~flags;
Tejun Heoe22bee72010-06-29 10:07:14 +0200922
Tejun Heo42c025f2011-01-11 15:58:49 +0100923 /*
924 * If transitioning out of NOT_RUNNING, increment nr_running. Note
925 * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask
926 * of multiple flags, not a single flag.
927 */
Tejun Heoe22bee72010-06-29 10:07:14 +0200928 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
929 if (!(worker->flags & WORKER_NOT_RUNNING))
Tejun Heoe19e3972013-01-24 11:39:44 -0800930 atomic_inc(&pool->nr_running);
Tejun Heod302f012010-06-29 10:07:13 +0200931}
932
933/**
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200934 * find_worker_executing_work - find worker which is executing a work
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800935 * @pool: pool of interest
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200936 * @work: work to find worker for
937 *
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800938 * Find a worker which is executing @work on @pool by searching
939 * @pool->busy_hash which is keyed by the address of @work. For a worker
Tejun Heoa2c1c572012-12-18 10:35:02 -0800940 * to match, its current execution should match the address of @work and
941 * its work function. This is to avoid unwanted dependency between
942 * unrelated work executions through a work item being recycled while still
943 * being executed.
944 *
945 * This is a bit tricky. A work item may be freed once its execution
946 * starts and nothing prevents the freed area from being recycled for
947 * another work item. If the same work item address ends up being reused
948 * before the original execution finishes, workqueue will identify the
949 * recycled work item as currently executing and make it wait until the
950 * current execution finishes, introducing an unwanted dependency.
951 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -0700952 * This function checks the work item address and work function to avoid
953 * false positives. Note that this isn't complete as one may construct a
954 * work function which can introduce dependency onto itself through a
955 * recycled work item. Well, if somebody wants to shoot oneself in the
956 * foot that badly, there's only so much we can do, and if such deadlock
957 * actually occurs, it should be easy to locate the culprit work function.
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200958 *
959 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800960 * spin_lock_irq(pool->lock).
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200961 *
962 * RETURNS:
963 * Pointer to worker which is executing @work if found, NULL
964 * otherwise.
965 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -0800966static struct worker *find_worker_executing_work(struct worker_pool *pool,
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200967 struct work_struct *work)
968{
Sasha Levin42f85702012-12-17 10:01:23 -0500969 struct worker *worker;
Sasha Levin42f85702012-12-17 10:01:23 -0500970
Sasha Levinb67bfe02013-02-27 17:06:00 -0800971 hash_for_each_possible(pool->busy_hash, worker, hentry,
Tejun Heoa2c1c572012-12-18 10:35:02 -0800972 (unsigned long)work)
973 if (worker->current_work == work &&
974 worker->current_func == work->func)
Sasha Levin42f85702012-12-17 10:01:23 -0500975 return worker;
976
977 return NULL;
Tejun Heo8cca0ee2010-06-29 10:07:13 +0200978}
979
980/**
Tejun Heobf4ede02012-08-03 10:30:46 -0700981 * move_linked_works - move linked works to a list
982 * @work: start of series of works to be scheduled
983 * @head: target list to append @work to
984 * @nextp: out paramter for nested worklist walking
985 *
986 * Schedule linked works starting from @work to @head. Work series to
987 * be scheduled starts at @work and includes any consecutive work with
988 * WORK_STRUCT_LINKED set in its predecessor.
989 *
990 * If @nextp is not NULL, it's updated to point to the next work of
991 * the last scheduled work. This allows move_linked_works() to be
992 * nested inside outer list_for_each_entry_safe().
993 *
994 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -0800995 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -0700996 */
997static void move_linked_works(struct work_struct *work, struct list_head *head,
998 struct work_struct **nextp)
999{
1000 struct work_struct *n;
1001
1002 /*
1003 * Linked worklist will always end before the end of the list,
1004 * use NULL for list head.
1005 */
1006 list_for_each_entry_safe_from(work, n, NULL, entry) {
1007 list_move_tail(&work->entry, head);
1008 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1009 break;
1010 }
1011
1012 /*
1013 * If we're already inside safe list traversal and have moved
1014 * multiple works to the scheduled queue, the next position
1015 * needs to be updated.
1016 */
1017 if (nextp)
1018 *nextp = n;
1019}
1020
Tejun Heo8864b4e2013-03-12 11:30:04 -07001021/**
1022 * get_pwq - get an extra reference on the specified pool_workqueue
1023 * @pwq: pool_workqueue to get
1024 *
1025 * Obtain an extra reference on @pwq. The caller should guarantee that
1026 * @pwq has positive refcnt and be holding the matching pool->lock.
1027 */
1028static void get_pwq(struct pool_workqueue *pwq)
1029{
1030 lockdep_assert_held(&pwq->pool->lock);
1031 WARN_ON_ONCE(pwq->refcnt <= 0);
1032 pwq->refcnt++;
1033}
1034
1035/**
1036 * put_pwq - put a pool_workqueue reference
1037 * @pwq: pool_workqueue to put
1038 *
1039 * Drop a reference of @pwq. If its refcnt reaches zero, schedule its
1040 * destruction. The caller should be holding the matching pool->lock.
1041 */
1042static void put_pwq(struct pool_workqueue *pwq)
1043{
1044 lockdep_assert_held(&pwq->pool->lock);
1045 if (likely(--pwq->refcnt))
1046 return;
1047 if (WARN_ON_ONCE(!(pwq->wq->flags & WQ_UNBOUND)))
1048 return;
1049 /*
1050 * @pwq can't be released under pool->lock, bounce to
1051 * pwq_unbound_release_workfn(). This never recurses on the same
1052 * pool->lock as this path is taken only for unbound workqueues and
1053 * the release work item is scheduled on a per-cpu workqueue. To
1054 * avoid lockdep warning, unbound pool->locks are given lockdep
1055 * subclass of 1 in get_unbound_pool().
1056 */
1057 schedule_work(&pwq->unbound_release_work);
1058}
1059
Tejun Heodce90d42013-04-01 11:23:35 -07001060/**
1061 * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock
1062 * @pwq: pool_workqueue to put (can be %NULL)
1063 *
1064 * put_pwq() with locking. This function also allows %NULL @pwq.
1065 */
1066static void put_pwq_unlocked(struct pool_workqueue *pwq)
1067{
1068 if (pwq) {
1069 /*
1070 * As both pwqs and pools are sched-RCU protected, the
1071 * following lock operations are safe.
1072 */
1073 spin_lock_irq(&pwq->pool->lock);
1074 put_pwq(pwq);
1075 spin_unlock_irq(&pwq->pool->lock);
1076 }
1077}
1078
Tejun Heo112202d2013-02-13 19:29:12 -08001079static void pwq_activate_delayed_work(struct work_struct *work)
Tejun Heobf4ede02012-08-03 10:30:46 -07001080{
Tejun Heo112202d2013-02-13 19:29:12 -08001081 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobf4ede02012-08-03 10:30:46 -07001082
1083 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001084 move_linked_works(work, &pwq->pool->worklist, NULL);
Tejun Heobf4ede02012-08-03 10:30:46 -07001085 __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));
Tejun Heo112202d2013-02-13 19:29:12 -08001086 pwq->nr_active++;
Tejun Heobf4ede02012-08-03 10:30:46 -07001087}
1088
Tejun Heo112202d2013-02-13 19:29:12 -08001089static void pwq_activate_first_delayed(struct pool_workqueue *pwq)
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001090{
Tejun Heo112202d2013-02-13 19:29:12 -08001091 struct work_struct *work = list_first_entry(&pwq->delayed_works,
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001092 struct work_struct, entry);
1093
Tejun Heo112202d2013-02-13 19:29:12 -08001094 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001095}
1096
Tejun Heobf4ede02012-08-03 10:30:46 -07001097/**
Tejun Heo112202d2013-02-13 19:29:12 -08001098 * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight
1099 * @pwq: pwq of interest
Tejun Heobf4ede02012-08-03 10:30:46 -07001100 * @color: color of work which left the queue
Tejun Heobf4ede02012-08-03 10:30:46 -07001101 *
1102 * A work either has completed or is removed from pending queue,
Tejun Heo112202d2013-02-13 19:29:12 -08001103 * decrement nr_in_flight of its pwq and handle workqueue flushing.
Tejun Heobf4ede02012-08-03 10:30:46 -07001104 *
1105 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001106 * spin_lock_irq(pool->lock).
Tejun Heobf4ede02012-08-03 10:30:46 -07001107 */
Tejun Heo112202d2013-02-13 19:29:12 -08001108static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color)
Tejun Heobf4ede02012-08-03 10:30:46 -07001109{
Tejun Heo8864b4e2013-03-12 11:30:04 -07001110 /* uncolored work items don't participate in flushing or nr_active */
Tejun Heobf4ede02012-08-03 10:30:46 -07001111 if (color == WORK_NO_COLOR)
Tejun Heo8864b4e2013-03-12 11:30:04 -07001112 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001113
Tejun Heo112202d2013-02-13 19:29:12 -08001114 pwq->nr_in_flight[color]--;
Tejun Heobf4ede02012-08-03 10:30:46 -07001115
Tejun Heo112202d2013-02-13 19:29:12 -08001116 pwq->nr_active--;
1117 if (!list_empty(&pwq->delayed_works)) {
Lai Jiangshanb3f9f402012-09-18 10:40:00 -07001118 /* one down, submit a delayed one */
Tejun Heo112202d2013-02-13 19:29:12 -08001119 if (pwq->nr_active < pwq->max_active)
1120 pwq_activate_first_delayed(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001121 }
1122
1123 /* is flush in progress and are we at the flushing tip? */
Tejun Heo112202d2013-02-13 19:29:12 -08001124 if (likely(pwq->flush_color != color))
Tejun Heo8864b4e2013-03-12 11:30:04 -07001125 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001126
1127 /* are there still in-flight works? */
Tejun Heo112202d2013-02-13 19:29:12 -08001128 if (pwq->nr_in_flight[color])
Tejun Heo8864b4e2013-03-12 11:30:04 -07001129 goto out_put;
Tejun Heobf4ede02012-08-03 10:30:46 -07001130
Tejun Heo112202d2013-02-13 19:29:12 -08001131 /* this pwq is done, clear flush_color */
1132 pwq->flush_color = -1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001133
1134 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001135 * If this was the last pwq, wake up the first flusher. It
Tejun Heobf4ede02012-08-03 10:30:46 -07001136 * will handle the rest.
1137 */
Tejun Heo112202d2013-02-13 19:29:12 -08001138 if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush))
1139 complete(&pwq->wq->first_flusher->done);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001140out_put:
1141 put_pwq(pwq);
Tejun Heobf4ede02012-08-03 10:30:46 -07001142}
1143
Tejun Heo36e227d2012-08-03 10:30:46 -07001144/**
Tejun Heobbb68df2012-08-03 10:30:46 -07001145 * try_to_grab_pending - steal work item from worklist and disable irq
Tejun Heo36e227d2012-08-03 10:30:46 -07001146 * @work: work item to steal
1147 * @is_dwork: @work is a delayed_work
Tejun Heobbb68df2012-08-03 10:30:46 -07001148 * @flags: place to store irq state
Tejun Heo36e227d2012-08-03 10:30:46 -07001149 *
1150 * Try to grab PENDING bit of @work. This function can handle @work in any
1151 * stable state - idle, on timer or on worklist. Return values are
1152 *
1153 * 1 if @work was pending and we successfully stole PENDING
1154 * 0 if @work was idle and we claimed PENDING
1155 * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry
Tejun Heobbb68df2012-08-03 10:30:46 -07001156 * -ENOENT if someone else is canceling @work, this state may persist
1157 * for arbitrarily long
Tejun Heo36e227d2012-08-03 10:30:46 -07001158 *
Tejun Heobbb68df2012-08-03 10:30:46 -07001159 * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001160 * interrupted while holding PENDING and @work off queue, irq must be
1161 * disabled on entry. This, combined with delayed_work->timer being
1162 * irqsafe, ensures that we return -EAGAIN for finite short period of time.
Tejun Heobbb68df2012-08-03 10:30:46 -07001163 *
1164 * On successful return, >= 0, irq is disabled and the caller is
1165 * responsible for releasing it using local_irq_restore(*@flags).
1166 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001167 * This function is safe to call from any context including IRQ handler.
Tejun Heobf4ede02012-08-03 10:30:46 -07001168 */
Tejun Heobbb68df2012-08-03 10:30:46 -07001169static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
1170 unsigned long *flags)
Tejun Heobf4ede02012-08-03 10:30:46 -07001171{
Tejun Heod565ed62013-01-24 11:01:33 -08001172 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08001173 struct pool_workqueue *pwq;
Tejun Heobf4ede02012-08-03 10:30:46 -07001174
Tejun Heobbb68df2012-08-03 10:30:46 -07001175 local_irq_save(*flags);
1176
Tejun Heo36e227d2012-08-03 10:30:46 -07001177 /* try to steal the timer if it exists */
1178 if (is_dwork) {
1179 struct delayed_work *dwork = to_delayed_work(work);
1180
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001181 /*
1182 * dwork->timer is irqsafe. If del_timer() fails, it's
1183 * guaranteed that the timer is not queued anywhere and not
1184 * running on the local CPU.
1185 */
Tejun Heo36e227d2012-08-03 10:30:46 -07001186 if (likely(del_timer(&dwork->timer)))
1187 return 1;
1188 }
1189
1190 /* try to claim PENDING the normal way */
Tejun Heobf4ede02012-08-03 10:30:46 -07001191 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1192 return 0;
1193
1194 /*
1195 * The queueing is in progress, or it is already queued. Try to
1196 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1197 */
Tejun Heod565ed62013-01-24 11:01:33 -08001198 pool = get_work_pool(work);
1199 if (!pool)
Tejun Heobbb68df2012-08-03 10:30:46 -07001200 goto fail;
Tejun Heobf4ede02012-08-03 10:30:46 -07001201
Tejun Heod565ed62013-01-24 11:01:33 -08001202 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001203 /*
Tejun Heo112202d2013-02-13 19:29:12 -08001204 * work->data is guaranteed to point to pwq only while the work
1205 * item is queued on pwq->wq, and both updating work->data to point
1206 * to pwq on queueing and to pool on dequeueing are done under
1207 * pwq->pool->lock. This in turn guarantees that, if work->data
1208 * points to pwq which is associated with a locked pool, the work
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08001209 * item is currently queued on that pool.
1210 */
Tejun Heo112202d2013-02-13 19:29:12 -08001211 pwq = get_work_pwq(work);
1212 if (pwq && pwq->pool == pool) {
Tejun Heo16062832013-02-06 18:04:53 -08001213 debug_work_deactivate(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001214
Tejun Heo16062832013-02-06 18:04:53 -08001215 /*
1216 * A delayed work item cannot be grabbed directly because
1217 * it might have linked NO_COLOR work items which, if left
Tejun Heo112202d2013-02-13 19:29:12 -08001218 * on the delayed_list, will confuse pwq->nr_active
Tejun Heo16062832013-02-06 18:04:53 -08001219 * management later on and cause stall. Make sure the work
1220 * item is activated before grabbing.
1221 */
1222 if (*work_data_bits(work) & WORK_STRUCT_DELAYED)
Tejun Heo112202d2013-02-13 19:29:12 -08001223 pwq_activate_delayed_work(work);
Lai Jiangshan3aa62492012-09-18 10:40:00 -07001224
Tejun Heo16062832013-02-06 18:04:53 -08001225 list_del_init(&work->entry);
Tejun Heo112202d2013-02-13 19:29:12 -08001226 pwq_dec_nr_in_flight(get_work_pwq(work), get_work_color(work));
Tejun Heo36e227d2012-08-03 10:30:46 -07001227
Tejun Heo112202d2013-02-13 19:29:12 -08001228 /* work->data points to pwq iff queued, point to pool */
Tejun Heo16062832013-02-06 18:04:53 -08001229 set_work_pool_and_keep_pending(work, pool->id);
Lai Jiangshan4468a002013-02-06 18:04:53 -08001230
Tejun Heo16062832013-02-06 18:04:53 -08001231 spin_unlock(&pool->lock);
1232 return 1;
Tejun Heobf4ede02012-08-03 10:30:46 -07001233 }
Tejun Heod565ed62013-01-24 11:01:33 -08001234 spin_unlock(&pool->lock);
Tejun Heobbb68df2012-08-03 10:30:46 -07001235fail:
1236 local_irq_restore(*flags);
1237 if (work_is_canceling(work))
1238 return -ENOENT;
1239 cpu_relax();
Tejun Heo36e227d2012-08-03 10:30:46 -07001240 return -EAGAIN;
Tejun Heobf4ede02012-08-03 10:30:46 -07001241}
1242
1243/**
Tejun Heo706026c2013-01-24 11:01:34 -08001244 * insert_work - insert a work into a pool
Tejun Heo112202d2013-02-13 19:29:12 -08001245 * @pwq: pwq @work belongs to
Tejun Heo4690c4a2010-06-29 10:07:10 +02001246 * @work: work to insert
1247 * @head: insertion point
1248 * @extra_flags: extra WORK_STRUCT_* flags to set
1249 *
Tejun Heo112202d2013-02-13 19:29:12 -08001250 * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to
Tejun Heo706026c2013-01-24 11:01:34 -08001251 * work_struct flags.
Tejun Heo4690c4a2010-06-29 10:07:10 +02001252 *
1253 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001254 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02001255 */
Tejun Heo112202d2013-02-13 19:29:12 -08001256static void insert_work(struct pool_workqueue *pwq, struct work_struct *work,
1257 struct list_head *head, unsigned int extra_flags)
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001258{
Tejun Heo112202d2013-02-13 19:29:12 -08001259 struct worker_pool *pool = pwq->pool;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01001260
Tejun Heo4690c4a2010-06-29 10:07:10 +02001261 /* we own @work, set data and link */
Tejun Heo112202d2013-02-13 19:29:12 -08001262 set_work_pwq(work, pwq, extra_flags);
Oleg Nesterov1a4d9b02008-07-25 01:47:47 -07001263 list_add_tail(&work->entry, head);
Tejun Heo8864b4e2013-03-12 11:30:04 -07001264 get_pwq(pwq);
Tejun Heoe22bee72010-06-29 10:07:14 +02001265
1266 /*
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001267 * Ensure either wq_worker_sleeping() sees the above
1268 * list_add_tail() or we see zero nr_running to avoid workers lying
1269 * around lazily while there are works to be processed.
Tejun Heoe22bee72010-06-29 10:07:14 +02001270 */
1271 smp_mb();
1272
Tejun Heo63d95a92012-07-12 14:46:37 -07001273 if (__need_more_worker(pool))
1274 wake_up_worker(pool);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07001275}
1276
Tejun Heoc8efcc22010-12-20 19:32:04 +01001277/*
1278 * Test whether @work is being queued from another work executing on the
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001279 * same workqueue.
Tejun Heoc8efcc22010-12-20 19:32:04 +01001280 */
1281static bool is_chained_work(struct workqueue_struct *wq)
1282{
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001283 struct worker *worker;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001284
Tejun Heo8d03ecf2013-02-13 19:29:10 -08001285 worker = current_wq_worker();
1286 /*
1287 * Return %true iff I'm a worker execuing a work item on @wq. If
1288 * I'm @worker, it's safe to dereference it without locking.
1289 */
Tejun Heo112202d2013-02-13 19:29:12 -08001290 return worker && worker->current_pwq->wq == wq;
Tejun Heoc8efcc22010-12-20 19:32:04 +01001291}
1292
Tejun Heod84ff052013-03-12 11:29:59 -07001293static void __queue_work(int cpu, struct workqueue_struct *wq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 struct work_struct *work)
1295{
Tejun Heo112202d2013-02-13 19:29:12 -08001296 struct pool_workqueue *pwq;
Tejun Heoc9178082013-03-12 11:30:04 -07001297 struct worker_pool *last_pool;
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001298 struct list_head *worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001299 unsigned int work_flags;
Joonsoo Kimb75cac92012-08-15 23:25:37 +09001300 unsigned int req_cpu = cpu;
Tejun Heo8930cab2012-08-03 10:30:45 -07001301
1302 /*
1303 * While a work item is PENDING && off queue, a task trying to
1304 * steal the PENDING will busy-loop waiting for it to either get
1305 * queued or lose PENDING. Grabbing PENDING and queueing should
1306 * happen with IRQ disabled.
1307 */
1308 WARN_ON_ONCE(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09001310 debug_work_activate(work);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001311
Tejun Heoc8efcc22010-12-20 19:32:04 +01001312 /* if dying, only works from the same workqueue are allowed */
Tejun Heo618b01e2013-03-12 11:30:04 -07001313 if (unlikely(wq->flags & __WQ_DRAINING) &&
Tejun Heoc8efcc22010-12-20 19:32:04 +01001314 WARN_ON_ONCE(!is_chained_work(wq)))
Tejun Heoe41e7042010-08-24 14:22:47 +02001315 return;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001316retry:
Tejun Heodf2d5ae2013-04-01 11:23:35 -07001317 if (req_cpu == WORK_CPU_UNBOUND)
1318 cpu = raw_smp_processor_id();
1319
Tejun Heoc9178082013-03-12 11:30:04 -07001320 /* pwq which will be used unless @work is executing elsewhere */
Tejun Heodf2d5ae2013-04-01 11:23:35 -07001321 if (!(wq->flags & WQ_UNBOUND))
Tejun Heo7fb98ea2013-03-12 11:30:00 -07001322 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heodf2d5ae2013-04-01 11:23:35 -07001323 else
1324 pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));
Tejun Heodbf25762012-08-20 14:51:23 -07001325
Tejun Heoc9178082013-03-12 11:30:04 -07001326 /*
1327 * If @work was previously on a different pool, it might still be
1328 * running there, in which case the work needs to be queued on that
1329 * pool to guarantee non-reentrancy.
1330 */
1331 last_pool = get_work_pool(work);
1332 if (last_pool && last_pool != pwq->pool) {
1333 struct worker *worker;
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001334
Tejun Heoc9178082013-03-12 11:30:04 -07001335 spin_lock(&last_pool->lock);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001336
Tejun Heoc9178082013-03-12 11:30:04 -07001337 worker = find_worker_executing_work(last_pool, work);
Tejun Heo18aa9ef2010-06-29 10:07:13 +02001338
Tejun Heoc9178082013-03-12 11:30:04 -07001339 if (worker && worker->current_pwq->wq == wq) {
1340 pwq = worker->current_pwq;
Tejun Heo8930cab2012-08-03 10:30:45 -07001341 } else {
Tejun Heoc9178082013-03-12 11:30:04 -07001342 /* meh... not running there, queue here */
1343 spin_unlock(&last_pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08001344 spin_lock(&pwq->pool->lock);
Tejun Heo8930cab2012-08-03 10:30:45 -07001345 }
Tejun Heof3421792010-07-02 10:03:51 +02001346 } else {
Tejun Heo112202d2013-02-13 19:29:12 -08001347 spin_lock(&pwq->pool->lock);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001348 }
1349
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001350 /*
1351 * pwq is determined and locked. For unbound pools, we could have
1352 * raced with pwq release and it could already be dead. If its
1353 * refcnt is zero, repeat pwq selection. Note that pwqs never die
Tejun Heodf2d5ae2013-04-01 11:23:35 -07001354 * without another pwq replacing it in the numa_pwq_tbl or while
1355 * work items are executing on it, so the retrying is guaranteed to
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07001356 * make forward-progress.
1357 */
1358 if (unlikely(!pwq->refcnt)) {
1359 if (wq->flags & WQ_UNBOUND) {
1360 spin_unlock(&pwq->pool->lock);
1361 cpu_relax();
1362 goto retry;
1363 }
1364 /* oops */
1365 WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt",
1366 wq->name, cpu);
1367 }
1368
Tejun Heo112202d2013-02-13 19:29:12 -08001369 /* pwq determined, queue */
1370 trace_workqueue_queue_work(req_cpu, pwq, work);
Tejun Heo502ca9d2010-06-29 10:07:13 +02001371
Dan Carpenterf5b25522012-04-13 22:06:58 +03001372 if (WARN_ON(!list_empty(&work->entry))) {
Tejun Heo112202d2013-02-13 19:29:12 -08001373 spin_unlock(&pwq->pool->lock);
Dan Carpenterf5b25522012-04-13 22:06:58 +03001374 return;
1375 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001376
Tejun Heo112202d2013-02-13 19:29:12 -08001377 pwq->nr_in_flight[pwq->work_color]++;
1378 work_flags = work_color_to_flags(pwq->work_color);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001379
Tejun Heo112202d2013-02-13 19:29:12 -08001380 if (likely(pwq->nr_active < pwq->max_active)) {
Tejun Heocdadf002010-10-05 10:49:55 +02001381 trace_workqueue_activate_work(work);
Tejun Heo112202d2013-02-13 19:29:12 -08001382 pwq->nr_active++;
1383 worklist = &pwq->pool->worklist;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001384 } else {
1385 work_flags |= WORK_STRUCT_DELAYED;
Tejun Heo112202d2013-02-13 19:29:12 -08001386 worklist = &pwq->delayed_works;
Tejun Heo8a2e8e5d2010-08-25 10:33:56 +02001387 }
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001388
Tejun Heo112202d2013-02-13 19:29:12 -08001389 insert_work(pwq, work, worklist, work_flags);
Tejun Heo1e19ffc2010-06-29 10:07:12 +02001390
Tejun Heo112202d2013-02-13 19:29:12 -08001391 spin_unlock(&pwq->pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392}
1393
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001394/**
Zhang Ruic1a220e2008-07-23 21:28:39 -07001395 * queue_work_on - queue work on specific cpu
1396 * @cpu: CPU number to execute work on
1397 * @wq: workqueue to use
1398 * @work: work to queue
1399 *
Tejun Heod4283e92012-08-03 10:30:44 -07001400 * Returns %false if @work was already on a queue, %true otherwise.
Zhang Ruic1a220e2008-07-23 21:28:39 -07001401 *
1402 * We queue the work to a specific CPU, the caller must ensure it
1403 * can't go away.
1404 */
Tejun Heod4283e92012-08-03 10:30:44 -07001405bool queue_work_on(int cpu, struct workqueue_struct *wq,
1406 struct work_struct *work)
Zhang Ruic1a220e2008-07-23 21:28:39 -07001407{
Tejun Heod4283e92012-08-03 10:30:44 -07001408 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001409 unsigned long flags;
1410
1411 local_irq_save(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001412
Tejun Heo22df02b2010-06-29 10:07:10 +02001413 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo4690c4a2010-06-29 10:07:10 +02001414 __queue_work(cpu, wq, work);
Tejun Heod4283e92012-08-03 10:30:44 -07001415 ret = true;
Zhang Ruic1a220e2008-07-23 21:28:39 -07001416 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001417
1418 local_irq_restore(flags);
Zhang Ruic1a220e2008-07-23 21:28:39 -07001419 return ret;
1420}
1421EXPORT_SYMBOL_GPL(queue_work_on);
1422
Tejun Heod8e794d2012-08-03 10:30:45 -07001423void delayed_work_timer_fn(unsigned long __data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
David Howells52bad642006-11-22 14:54:01 +00001425 struct delayed_work *dwork = (struct delayed_work *)__data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001427 /* should have been called from irqsafe timer with irq already off */
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001428 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
Konstantin Khlebnikov1438ade52013-01-24 16:36:31 +04001430EXPORT_SYMBOL(delayed_work_timer_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001432static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,
1433 struct delayed_work *dwork, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001435 struct timer_list *timer = &dwork->timer;
1436 struct work_struct *work = &dwork->work;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001438 WARN_ON_ONCE(timer->function != delayed_work_timer_fn ||
1439 timer->data != (unsigned long)dwork);
Tejun Heofc4b5142012-12-04 07:40:39 -08001440 WARN_ON_ONCE(timer_pending(timer));
1441 WARN_ON_ONCE(!list_empty(&work->entry));
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001442
Tejun Heo8852aac2012-12-01 16:23:42 -08001443 /*
1444 * If @delay is 0, queue @dwork->work immediately. This is for
1445 * both optimization and correctness. The earliest @timer can
1446 * expire is on the closest next tick and delayed_work users depend
1447 * on that there's no such delay when @delay is 0.
1448 */
1449 if (!delay) {
1450 __queue_work(cpu, wq, &dwork->work);
1451 return;
1452 }
1453
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001454 timer_stats_timer_set_start_info(&dwork->timer);
1455
Lai Jiangshan60c057b2013-02-06 18:04:53 -08001456 dwork->wq = wq;
Tejun Heo12650572012-08-08 09:38:42 -07001457 dwork->cpu = cpu;
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001458 timer->expires = jiffies + delay;
1459
1460 if (unlikely(cpu != WORK_CPU_UNBOUND))
1461 add_timer_on(timer, cpu);
1462 else
1463 add_timer(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001466/**
1467 * queue_delayed_work_on - queue work on specific CPU after delay
1468 * @cpu: CPU number to execute work on
1469 * @wq: workqueue to use
Randy Dunlapaf9997e2006-12-22 01:06:52 -08001470 * @dwork: work to queue
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001471 * @delay: number of jiffies to wait before queueing
1472 *
Tejun Heo715f1302012-08-03 10:30:46 -07001473 * Returns %false if @work was already on a queue, %true otherwise. If
1474 * @delay is zero and @dwork is idle, it will be scheduled for immediate
1475 * execution.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07001476 */
Tejun Heod4283e92012-08-03 10:30:44 -07001477bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
1478 struct delayed_work *dwork, unsigned long delay)
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001479{
David Howells52bad642006-11-22 14:54:01 +00001480 struct work_struct *work = &dwork->work;
Tejun Heod4283e92012-08-03 10:30:44 -07001481 bool ret = false;
Tejun Heo8930cab2012-08-03 10:30:45 -07001482 unsigned long flags;
1483
1484 /* read the comment in __queue_work() */
1485 local_irq_save(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001486
Tejun Heo22df02b2010-06-29 10:07:10 +02001487 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
Tejun Heo7beb2ed2012-08-03 10:30:46 -07001488 __queue_delayed_work(cpu, wq, dwork, delay);
Tejun Heod4283e92012-08-03 10:30:44 -07001489 ret = true;
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001490 }
Tejun Heo8930cab2012-08-03 10:30:45 -07001491
1492 local_irq_restore(flags);
Venkatesh Pallipadi7a6bc1c2006-06-28 13:50:33 -07001493 return ret;
1494}
Dave Jonesae90dd52006-06-30 01:40:45 -04001495EXPORT_SYMBOL_GPL(queue_delayed_work_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Tejun Heoc8e55f32010-06-29 10:07:12 +02001497/**
Tejun Heo8376fe22012-08-03 10:30:47 -07001498 * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
1499 * @cpu: CPU number to execute work on
1500 * @wq: workqueue to use
1501 * @dwork: work to queue
1502 * @delay: number of jiffies to wait before queueing
1503 *
1504 * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise,
1505 * modify @dwork's timer so that it expires after @delay. If @delay is
1506 * zero, @work is guaranteed to be scheduled immediately regardless of its
1507 * current state.
1508 *
1509 * Returns %false if @dwork was idle and queued, %true if @dwork was
1510 * pending and its timer was modified.
1511 *
Tejun Heoe0aecdd2012-08-21 13:18:24 -07001512 * This function is safe to call from any context including IRQ handler.
Tejun Heo8376fe22012-08-03 10:30:47 -07001513 * See try_to_grab_pending() for details.
1514 */
1515bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
1516 struct delayed_work *dwork, unsigned long delay)
1517{
1518 unsigned long flags;
1519 int ret;
1520
1521 do {
1522 ret = try_to_grab_pending(&dwork->work, true, &flags);
1523 } while (unlikely(ret == -EAGAIN));
1524
1525 if (likely(ret >= 0)) {
1526 __queue_delayed_work(cpu, wq, dwork, delay);
1527 local_irq_restore(flags);
1528 }
1529
1530 /* -ENOENT from try_to_grab_pending() becomes %true */
1531 return ret;
1532}
1533EXPORT_SYMBOL_GPL(mod_delayed_work_on);
1534
1535/**
Tejun Heoc8e55f32010-06-29 10:07:12 +02001536 * worker_enter_idle - enter idle state
1537 * @worker: worker which is entering idle state
1538 *
1539 * @worker is entering idle state. Update stats and idle timer if
1540 * necessary.
1541 *
1542 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001543 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001544 */
1545static void worker_enter_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001547 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Tejun Heo6183c002013-03-12 11:29:57 -07001549 if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) ||
1550 WARN_ON_ONCE(!list_empty(&worker->entry) &&
1551 (worker->hentry.next || worker->hentry.pprev)))
1552 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Tejun Heocb444762010-07-02 10:03:50 +02001554 /* can't use worker_set_flags(), also called from start_worker() */
1555 worker->flags |= WORKER_IDLE;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001556 pool->nr_idle++;
Tejun Heoe22bee72010-06-29 10:07:14 +02001557 worker->last_active = jiffies;
Peter Zijlstrad5abe662006-12-06 20:37:26 -08001558
Tejun Heoc8e55f32010-06-29 10:07:12 +02001559 /* idle_list is LIFO */
Tejun Heobd7bdd42012-07-12 14:46:37 -07001560 list_add(&worker->entry, &pool->idle_list);
Tejun Heodb7bccf2010-06-29 10:07:12 +02001561
Tejun Heo628c78e2012-07-17 12:39:27 -07001562 if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
1563 mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
Tejun Heocb444762010-07-02 10:03:50 +02001564
Tejun Heo544ecf32012-05-14 15:04:50 -07001565 /*
Tejun Heo706026c2013-01-24 11:01:34 -08001566 * Sanity check nr_running. Because wq_unbind_fn() releases
Tejun Heod565ed62013-01-24 11:01:33 -08001567 * pool->lock between setting %WORKER_UNBOUND and zapping
Tejun Heo628c78e2012-07-17 12:39:27 -07001568 * nr_running, the warning may trigger spuriously. Check iff
1569 * unbind is not in progress.
Tejun Heo544ecf32012-05-14 15:04:50 -07001570 */
Tejun Heo24647572013-01-24 11:01:33 -08001571 WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heobd7bdd42012-07-12 14:46:37 -07001572 pool->nr_workers == pool->nr_idle &&
Tejun Heoe19e3972013-01-24 11:39:44 -08001573 atomic_read(&pool->nr_running));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574}
1575
Tejun Heoc8e55f32010-06-29 10:07:12 +02001576/**
1577 * worker_leave_idle - leave idle state
1578 * @worker: worker which is leaving idle state
1579 *
1580 * @worker is leaving idle state. Update stats.
1581 *
1582 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001583 * spin_lock_irq(pool->lock).
Tejun Heoc8e55f32010-06-29 10:07:12 +02001584 */
1585static void worker_leave_idle(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001587 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Tejun Heo6183c002013-03-12 11:29:57 -07001589 if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE)))
1590 return;
Tejun Heod302f012010-06-29 10:07:13 +02001591 worker_clr_flags(worker, WORKER_IDLE);
Tejun Heobd7bdd42012-07-12 14:46:37 -07001592 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001593 list_del_init(&worker->entry);
1594}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Tejun Heoe22bee72010-06-29 10:07:14 +02001596/**
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001597 * worker_maybe_bind_and_lock - try to bind %current to worker_pool and lock it
1598 * @pool: target worker_pool
1599 *
1600 * Bind %current to the cpu of @pool if it is associated and lock @pool.
Tejun Heoe22bee72010-06-29 10:07:14 +02001601 *
1602 * Works which are scheduled while the cpu is online must at least be
1603 * scheduled to a worker which is bound to the cpu so that if they are
1604 * flushed from cpu callbacks while cpu is going down, they are
1605 * guaranteed to execute on the cpu.
1606 *
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001607 * This function is to be used by unbound workers and rescuers to bind
Tejun Heoe22bee72010-06-29 10:07:14 +02001608 * themselves to the target cpu and may race with cpu going down or
1609 * coming online. kthread_bind() can't be used because it may put the
1610 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
Tejun Heo706026c2013-01-24 11:01:34 -08001611 * verbatim as it's best effort and blocking and pool may be
Tejun Heoe22bee72010-06-29 10:07:14 +02001612 * [dis]associated in the meantime.
1613 *
Tejun Heo706026c2013-01-24 11:01:34 -08001614 * This function tries set_cpus_allowed() and locks pool and verifies the
Tejun Heo24647572013-01-24 11:01:33 -08001615 * binding against %POOL_DISASSOCIATED which is set during
Tejun Heof2d5a0e2012-07-17 12:39:26 -07001616 * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker
1617 * enters idle state or fetches works without dropping lock, it can
1618 * guarantee the scheduling requirement described in the first paragraph.
Tejun Heoe22bee72010-06-29 10:07:14 +02001619 *
1620 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001621 * Might sleep. Called without any lock but returns with pool->lock
Tejun Heoe22bee72010-06-29 10:07:14 +02001622 * held.
1623 *
1624 * RETURNS:
Tejun Heo706026c2013-01-24 11:01:34 -08001625 * %true if the associated pool is online (@worker is successfully
Tejun Heoe22bee72010-06-29 10:07:14 +02001626 * bound), %false if offline.
1627 */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08001628static bool worker_maybe_bind_and_lock(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001629__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001630{
Tejun Heoe22bee72010-06-29 10:07:14 +02001631 while (true) {
1632 /*
1633 * The following call may fail, succeed or succeed
1634 * without actually migrating the task to the cpu if
1635 * it races with cpu hotunplug operation. Verify
Tejun Heo24647572013-01-24 11:01:33 -08001636 * against POOL_DISASSOCIATED.
Tejun Heoe22bee72010-06-29 10:07:14 +02001637 */
Tejun Heo24647572013-01-24 11:01:33 -08001638 if (!(pool->flags & POOL_DISASSOCIATED))
Tejun Heo7a4e3442013-03-12 11:30:00 -07001639 set_cpus_allowed_ptr(current, pool->attrs->cpumask);
Oleg Nesterov85f41862007-05-09 02:34:20 -07001640
Tejun Heod565ed62013-01-24 11:01:33 -08001641 spin_lock_irq(&pool->lock);
Tejun Heo24647572013-01-24 11:01:33 -08001642 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heoe22bee72010-06-29 10:07:14 +02001643 return false;
Lai Jiangshanf5faa072013-02-19 12:17:02 -08001644 if (task_cpu(current) == pool->cpu &&
Tejun Heo7a4e3442013-03-12 11:30:00 -07001645 cpumask_equal(&current->cpus_allowed, pool->attrs->cpumask))
Tejun Heoe22bee72010-06-29 10:07:14 +02001646 return true;
Tejun Heod565ed62013-01-24 11:01:33 -08001647 spin_unlock_irq(&pool->lock);
Oleg Nesterov3af244332007-05-09 02:34:09 -07001648
Tejun Heo5035b202011-04-29 18:08:37 +02001649 /*
1650 * We've raced with CPU hot[un]plug. Give it a breather
1651 * and retry migration. cond_resched() is required here;
1652 * otherwise, we might deadlock against cpu_stop trying to
1653 * bring down the CPU on non-preemptive kernel.
1654 */
Tejun Heoe22bee72010-06-29 10:07:14 +02001655 cpu_relax();
Tejun Heo5035b202011-04-29 18:08:37 +02001656 cond_resched();
Tejun Heoe22bee72010-06-29 10:07:14 +02001657 }
1658}
1659
Tejun Heoc34056a2010-06-29 10:07:11 +02001660static struct worker *alloc_worker(void)
1661{
1662 struct worker *worker;
1663
1664 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001665 if (worker) {
1666 INIT_LIST_HEAD(&worker->entry);
Tejun Heoaffee4b2010-06-29 10:07:12 +02001667 INIT_LIST_HEAD(&worker->scheduled);
Tejun Heoe22bee72010-06-29 10:07:14 +02001668 /* on creation a worker is in !idle && prep state */
1669 worker->flags = WORKER_PREP;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001670 }
Tejun Heoc34056a2010-06-29 10:07:11 +02001671 return worker;
1672}
1673
1674/**
1675 * create_worker - create a new workqueue worker
Tejun Heo63d95a92012-07-12 14:46:37 -07001676 * @pool: pool the new worker will belong to
Tejun Heoc34056a2010-06-29 10:07:11 +02001677 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001678 * Create a new worker which is bound to @pool. The returned worker
Tejun Heoc34056a2010-06-29 10:07:11 +02001679 * can be started by calling start_worker() or destroyed using
1680 * destroy_worker().
1681 *
1682 * CONTEXT:
1683 * Might sleep. Does GFP_KERNEL allocations.
1684 *
1685 * RETURNS:
1686 * Pointer to the newly created worker.
1687 */
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001688static struct worker *create_worker(struct worker_pool *pool)
Tejun Heoc34056a2010-06-29 10:07:11 +02001689{
Tejun Heoc34056a2010-06-29 10:07:11 +02001690 struct worker *worker = NULL;
Tejun Heof3421792010-07-02 10:03:51 +02001691 int id = -1;
Tejun Heoe3c916a2013-04-01 11:23:32 -07001692 char id_buf[16];
Tejun Heoc34056a2010-06-29 10:07:11 +02001693
Tejun Heocd549682013-03-13 19:47:39 -07001694 lockdep_assert_held(&pool->manager_mutex);
1695
Tejun Heo822d8402013-03-19 13:45:21 -07001696 /*
1697 * ID is needed to determine kthread name. Allocate ID first
1698 * without installing the pointer.
1699 */
1700 idr_preload(GFP_KERNEL);
Tejun Heod565ed62013-01-24 11:01:33 -08001701 spin_lock_irq(&pool->lock);
Tejun Heo822d8402013-03-19 13:45:21 -07001702
1703 id = idr_alloc(&pool->worker_idr, NULL, 0, 0, GFP_NOWAIT);
1704
Tejun Heod565ed62013-01-24 11:01:33 -08001705 spin_unlock_irq(&pool->lock);
Tejun Heo822d8402013-03-19 13:45:21 -07001706 idr_preload_end();
1707 if (id < 0)
1708 goto fail;
Tejun Heoc34056a2010-06-29 10:07:11 +02001709
1710 worker = alloc_worker();
1711 if (!worker)
1712 goto fail;
1713
Tejun Heobd7bdd42012-07-12 14:46:37 -07001714 worker->pool = pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001715 worker->id = id;
1716
Tejun Heo29c91e92013-03-12 11:30:03 -07001717 if (pool->cpu >= 0)
Tejun Heoe3c916a2013-04-01 11:23:32 -07001718 snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id,
1719 pool->attrs->nice < 0 ? "H" : "");
Tejun Heof3421792010-07-02 10:03:51 +02001720 else
Tejun Heoe3c916a2013-04-01 11:23:32 -07001721 snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id);
1722
Tejun Heof3f90ad2013-04-01 11:23:34 -07001723 worker->task = kthread_create_on_node(worker_thread, worker, pool->node,
Tejun Heoe3c916a2013-04-01 11:23:32 -07001724 "kworker/%s", id_buf);
Tejun Heoc34056a2010-06-29 10:07:11 +02001725 if (IS_ERR(worker->task))
1726 goto fail;
1727
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001728 /*
1729 * set_cpus_allowed_ptr() will fail if the cpumask doesn't have any
1730 * online CPUs. It'll be re-applied when any of the CPUs come up.
1731 */
Tejun Heo7a4e3442013-03-12 11:30:00 -07001732 set_user_nice(worker->task, pool->attrs->nice);
1733 set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask);
Tejun Heo32704762012-07-13 22:16:45 -07001734
Tejun Heo14a40ff2013-03-19 13:45:20 -07001735 /* prevent userland from meddling with cpumask of workqueue workers */
1736 worker->task->flags |= PF_NO_SETAFFINITY;
Tejun Heo7a4e3442013-03-12 11:30:00 -07001737
1738 /*
1739 * The caller is responsible for ensuring %POOL_DISASSOCIATED
1740 * remains stable across this function. See the comments above the
1741 * flag definition for details.
1742 */
1743 if (pool->flags & POOL_DISASSOCIATED)
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001744 worker->flags |= WORKER_UNBOUND;
Oleg Nesterov3af244332007-05-09 02:34:09 -07001745
Tejun Heo822d8402013-03-19 13:45:21 -07001746 /* successful, commit the pointer to idr */
1747 spin_lock_irq(&pool->lock);
1748 idr_replace(&pool->worker_idr, worker, worker->id);
1749 spin_unlock_irq(&pool->lock);
1750
Tejun Heoc34056a2010-06-29 10:07:11 +02001751 return worker;
Tejun Heo822d8402013-03-19 13:45:21 -07001752
Tejun Heoc34056a2010-06-29 10:07:11 +02001753fail:
1754 if (id >= 0) {
Tejun Heod565ed62013-01-24 11:01:33 -08001755 spin_lock_irq(&pool->lock);
Tejun Heo822d8402013-03-19 13:45:21 -07001756 idr_remove(&pool->worker_idr, id);
Tejun Heod565ed62013-01-24 11:01:33 -08001757 spin_unlock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001758 }
1759 kfree(worker);
1760 return NULL;
1761}
1762
1763/**
1764 * start_worker - start a newly created worker
1765 * @worker: worker to start
1766 *
Tejun Heo706026c2013-01-24 11:01:34 -08001767 * Make the pool aware of @worker and start it.
Tejun Heoc34056a2010-06-29 10:07:11 +02001768 *
1769 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001770 * spin_lock_irq(pool->lock).
Tejun Heoc34056a2010-06-29 10:07:11 +02001771 */
1772static void start_worker(struct worker *worker)
1773{
Tejun Heocb444762010-07-02 10:03:50 +02001774 worker->flags |= WORKER_STARTED;
Tejun Heobd7bdd42012-07-12 14:46:37 -07001775 worker->pool->nr_workers++;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001776 worker_enter_idle(worker);
Tejun Heoc34056a2010-06-29 10:07:11 +02001777 wake_up_process(worker->task);
1778}
1779
1780/**
Tejun Heoebf44d12013-03-13 19:47:39 -07001781 * create_and_start_worker - create and start a worker for a pool
1782 * @pool: the target pool
1783 *
Tejun Heocd549682013-03-13 19:47:39 -07001784 * Grab the managership of @pool and create and start a new worker for it.
Tejun Heoebf44d12013-03-13 19:47:39 -07001785 */
1786static int create_and_start_worker(struct worker_pool *pool)
1787{
1788 struct worker *worker;
1789
Tejun Heocd549682013-03-13 19:47:39 -07001790 mutex_lock(&pool->manager_mutex);
1791
Tejun Heoebf44d12013-03-13 19:47:39 -07001792 worker = create_worker(pool);
1793 if (worker) {
1794 spin_lock_irq(&pool->lock);
1795 start_worker(worker);
1796 spin_unlock_irq(&pool->lock);
1797 }
1798
Tejun Heocd549682013-03-13 19:47:39 -07001799 mutex_unlock(&pool->manager_mutex);
1800
Tejun Heoebf44d12013-03-13 19:47:39 -07001801 return worker ? 0 : -ENOMEM;
1802}
1803
1804/**
Tejun Heoc34056a2010-06-29 10:07:11 +02001805 * destroy_worker - destroy a workqueue worker
1806 * @worker: worker to be destroyed
1807 *
Tejun Heo706026c2013-01-24 11:01:34 -08001808 * Destroy @worker and adjust @pool stats accordingly.
Tejun Heoc8e55f32010-06-29 10:07:12 +02001809 *
1810 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08001811 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoc34056a2010-06-29 10:07:11 +02001812 */
1813static void destroy_worker(struct worker *worker)
1814{
Tejun Heobd7bdd42012-07-12 14:46:37 -07001815 struct worker_pool *pool = worker->pool;
Tejun Heoc34056a2010-06-29 10:07:11 +02001816
Tejun Heocd549682013-03-13 19:47:39 -07001817 lockdep_assert_held(&pool->manager_mutex);
1818 lockdep_assert_held(&pool->lock);
1819
Tejun Heoc34056a2010-06-29 10:07:11 +02001820 /* sanity check frenzy */
Tejun Heo6183c002013-03-12 11:29:57 -07001821 if (WARN_ON(worker->current_work) ||
1822 WARN_ON(!list_empty(&worker->scheduled)))
1823 return;
Tejun Heoc34056a2010-06-29 10:07:11 +02001824
Tejun Heoc8e55f32010-06-29 10:07:12 +02001825 if (worker->flags & WORKER_STARTED)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001826 pool->nr_workers--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001827 if (worker->flags & WORKER_IDLE)
Tejun Heobd7bdd42012-07-12 14:46:37 -07001828 pool->nr_idle--;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001829
1830 list_del_init(&worker->entry);
Tejun Heocb444762010-07-02 10:03:50 +02001831 worker->flags |= WORKER_DIE;
Tejun Heoc8e55f32010-06-29 10:07:12 +02001832
Tejun Heo822d8402013-03-19 13:45:21 -07001833 idr_remove(&pool->worker_idr, worker->id);
1834
Tejun Heod565ed62013-01-24 11:01:33 -08001835 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02001836
Tejun Heoc34056a2010-06-29 10:07:11 +02001837 kthread_stop(worker->task);
1838 kfree(worker);
1839
Tejun Heod565ed62013-01-24 11:01:33 -08001840 spin_lock_irq(&pool->lock);
Tejun Heoc34056a2010-06-29 10:07:11 +02001841}
1842
Tejun Heo63d95a92012-07-12 14:46:37 -07001843static void idle_worker_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001844{
Tejun Heo63d95a92012-07-12 14:46:37 -07001845 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001846
Tejun Heod565ed62013-01-24 11:01:33 -08001847 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001848
Tejun Heo63d95a92012-07-12 14:46:37 -07001849 if (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001850 struct worker *worker;
1851 unsigned long expires;
1852
1853 /* idle_list is kept in LIFO order, check the last one */
Tejun Heo63d95a92012-07-12 14:46:37 -07001854 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001855 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1856
1857 if (time_before(jiffies, expires))
Tejun Heo63d95a92012-07-12 14:46:37 -07001858 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02001859 else {
1860 /* it's been idle for too long, wake up manager */
Tejun Heo11ebea52012-07-12 14:46:37 -07001861 pool->flags |= POOL_MANAGE_WORKERS;
Tejun Heo63d95a92012-07-12 14:46:37 -07001862 wake_up_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001863 }
1864 }
1865
Tejun Heod565ed62013-01-24 11:01:33 -08001866 spin_unlock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001867}
1868
Tejun Heo493a1722013-03-12 11:29:59 -07001869static void send_mayday(struct work_struct *work)
Tejun Heoe22bee72010-06-29 10:07:14 +02001870{
Tejun Heo112202d2013-02-13 19:29:12 -08001871 struct pool_workqueue *pwq = get_work_pwq(work);
1872 struct workqueue_struct *wq = pwq->wq;
Tejun Heo493a1722013-03-12 11:29:59 -07001873
Tejun Heo2e109a22013-03-13 19:47:40 -07001874 lockdep_assert_held(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001875
Tejun Heo493008a2013-03-12 11:30:03 -07001876 if (!wq->rescuer)
Tejun Heo493a1722013-03-12 11:29:59 -07001877 return;
Tejun Heoe22bee72010-06-29 10:07:14 +02001878
1879 /* mayday mayday mayday */
Tejun Heo493a1722013-03-12 11:29:59 -07001880 if (list_empty(&pwq->mayday_node)) {
1881 list_add_tail(&pwq->mayday_node, &wq->maydays);
Tejun Heoe22bee72010-06-29 10:07:14 +02001882 wake_up_process(wq->rescuer->task);
Tejun Heo493a1722013-03-12 11:29:59 -07001883 }
Tejun Heoe22bee72010-06-29 10:07:14 +02001884}
1885
Tejun Heo706026c2013-01-24 11:01:34 -08001886static void pool_mayday_timeout(unsigned long __pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001887{
Tejun Heo63d95a92012-07-12 14:46:37 -07001888 struct worker_pool *pool = (void *)__pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02001889 struct work_struct *work;
1890
Tejun Heo2e109a22013-03-13 19:47:40 -07001891 spin_lock_irq(&wq_mayday_lock); /* for wq->maydays */
Tejun Heo493a1722013-03-12 11:29:59 -07001892 spin_lock(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001893
Tejun Heo63d95a92012-07-12 14:46:37 -07001894 if (need_to_create_worker(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001895 /*
1896 * We've been trying to create a new worker but
1897 * haven't been successful. We might be hitting an
1898 * allocation deadlock. Send distress signals to
1899 * rescuers.
1900 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001901 list_for_each_entry(work, &pool->worklist, entry)
Tejun Heoe22bee72010-06-29 10:07:14 +02001902 send_mayday(work);
1903 }
1904
Tejun Heo493a1722013-03-12 11:29:59 -07001905 spin_unlock(&pool->lock);
Tejun Heo2e109a22013-03-13 19:47:40 -07001906 spin_unlock_irq(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001907
Tejun Heo63d95a92012-07-12 14:46:37 -07001908 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
Tejun Heoe22bee72010-06-29 10:07:14 +02001909}
1910
1911/**
1912 * maybe_create_worker - create a new worker if necessary
Tejun Heo63d95a92012-07-12 14:46:37 -07001913 * @pool: pool to create a new worker for
Tejun Heoe22bee72010-06-29 10:07:14 +02001914 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001915 * Create a new worker for @pool if necessary. @pool is guaranteed to
Tejun Heoe22bee72010-06-29 10:07:14 +02001916 * have at least one idle worker on return from this function. If
1917 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
Tejun Heo63d95a92012-07-12 14:46:37 -07001918 * sent to all rescuers with works scheduled on @pool to resolve
Tejun Heoe22bee72010-06-29 10:07:14 +02001919 * possible allocation deadlock.
1920 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001921 * On return, need_to_create_worker() is guaranteed to be %false and
1922 * may_start_working() %true.
Tejun Heoe22bee72010-06-29 10:07:14 +02001923 *
1924 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001925 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001926 * multiple times. Does GFP_KERNEL allocations. Called only from
1927 * manager.
1928 *
1929 * RETURNS:
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001930 * %false if no action was taken and pool->lock stayed locked, %true
Tejun Heoe22bee72010-06-29 10:07:14 +02001931 * otherwise.
1932 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001933static bool maybe_create_worker(struct worker_pool *pool)
Tejun Heod565ed62013-01-24 11:01:33 -08001934__releases(&pool->lock)
1935__acquires(&pool->lock)
Tejun Heoe22bee72010-06-29 10:07:14 +02001936{
Tejun Heo63d95a92012-07-12 14:46:37 -07001937 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001938 return false;
1939restart:
Tejun Heod565ed62013-01-24 11:01:33 -08001940 spin_unlock_irq(&pool->lock);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001941
Tejun Heoe22bee72010-06-29 10:07:14 +02001942 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
Tejun Heo63d95a92012-07-12 14:46:37 -07001943 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
Tejun Heoe22bee72010-06-29 10:07:14 +02001944
1945 while (true) {
1946 struct worker *worker;
1947
Tejun Heobc2ae0f2012-07-17 12:39:27 -07001948 worker = create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02001949 if (worker) {
Tejun Heo63d95a92012-07-12 14:46:37 -07001950 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001951 spin_lock_irq(&pool->lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02001952 start_worker(worker);
Tejun Heo6183c002013-03-12 11:29:57 -07001953 if (WARN_ON_ONCE(need_to_create_worker(pool)))
1954 goto restart;
Tejun Heoe22bee72010-06-29 10:07:14 +02001955 return true;
1956 }
1957
Tejun Heo63d95a92012-07-12 14:46:37 -07001958 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001959 break;
1960
Tejun Heoe22bee72010-06-29 10:07:14 +02001961 __set_current_state(TASK_INTERRUPTIBLE);
1962 schedule_timeout(CREATE_COOLDOWN);
Tejun Heo9f9c2362010-07-14 11:31:20 +02001963
Tejun Heo63d95a92012-07-12 14:46:37 -07001964 if (!need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001965 break;
1966 }
1967
Tejun Heo63d95a92012-07-12 14:46:37 -07001968 del_timer_sync(&pool->mayday_timer);
Tejun Heod565ed62013-01-24 11:01:33 -08001969 spin_lock_irq(&pool->lock);
Tejun Heo63d95a92012-07-12 14:46:37 -07001970 if (need_to_create_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02001971 goto restart;
1972 return true;
1973}
1974
1975/**
1976 * maybe_destroy_worker - destroy workers which have been idle for a while
Tejun Heo63d95a92012-07-12 14:46:37 -07001977 * @pool: pool to destroy workers for
Tejun Heoe22bee72010-06-29 10:07:14 +02001978 *
Tejun Heo63d95a92012-07-12 14:46:37 -07001979 * Destroy @pool workers which have been idle for longer than
Tejun Heoe22bee72010-06-29 10:07:14 +02001980 * IDLE_WORKER_TIMEOUT.
1981 *
1982 * LOCKING:
Tejun Heod565ed62013-01-24 11:01:33 -08001983 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02001984 * multiple times. Called only from manager.
1985 *
1986 * RETURNS:
Tejun Heoc5aa87b2013-03-13 16:51:36 -07001987 * %false if no action was taken and pool->lock stayed locked, %true
Tejun Heoe22bee72010-06-29 10:07:14 +02001988 * otherwise.
1989 */
Tejun Heo63d95a92012-07-12 14:46:37 -07001990static bool maybe_destroy_workers(struct worker_pool *pool)
Tejun Heoe22bee72010-06-29 10:07:14 +02001991{
1992 bool ret = false;
1993
Tejun Heo63d95a92012-07-12 14:46:37 -07001994 while (too_many_workers(pool)) {
Tejun Heoe22bee72010-06-29 10:07:14 +02001995 struct worker *worker;
1996 unsigned long expires;
1997
Tejun Heo63d95a92012-07-12 14:46:37 -07001998 worker = list_entry(pool->idle_list.prev, struct worker, entry);
Tejun Heoe22bee72010-06-29 10:07:14 +02001999 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
2000
2001 if (time_before(jiffies, expires)) {
Tejun Heo63d95a92012-07-12 14:46:37 -07002002 mod_timer(&pool->idle_timer, expires);
Tejun Heoe22bee72010-06-29 10:07:14 +02002003 break;
2004 }
2005
2006 destroy_worker(worker);
2007 ret = true;
2008 }
2009
2010 return ret;
2011}
2012
2013/**
2014 * manage_workers - manage worker pool
2015 * @worker: self
2016 *
Tejun Heo706026c2013-01-24 11:01:34 -08002017 * Assume the manager role and manage the worker pool @worker belongs
Tejun Heoe22bee72010-06-29 10:07:14 +02002018 * to. At any given time, there can be only zero or one manager per
Tejun Heo706026c2013-01-24 11:01:34 -08002019 * pool. The exclusion is handled automatically by this function.
Tejun Heoe22bee72010-06-29 10:07:14 +02002020 *
2021 * The caller can safely start processing works on false return. On
2022 * true return, it's guaranteed that need_to_create_worker() is false
2023 * and may_start_working() is true.
2024 *
2025 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002026 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoe22bee72010-06-29 10:07:14 +02002027 * multiple times. Does GFP_KERNEL allocations.
2028 *
2029 * RETURNS:
Tejun Heod565ed62013-01-24 11:01:33 -08002030 * spin_lock_irq(pool->lock) which may be released and regrabbed
2031 * multiple times. Does GFP_KERNEL allocations.
Tejun Heoe22bee72010-06-29 10:07:14 +02002032 */
2033static bool manage_workers(struct worker *worker)
2034{
Tejun Heo63d95a92012-07-12 14:46:37 -07002035 struct worker_pool *pool = worker->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002036 bool ret = false;
2037
Tejun Heobc3a1af2013-03-13 19:47:39 -07002038 /*
2039 * Managership is governed by two mutexes - manager_arb and
2040 * manager_mutex. manager_arb handles arbitration of manager role.
2041 * Anyone who successfully grabs manager_arb wins the arbitration
2042 * and becomes the manager. mutex_trylock() on pool->manager_arb
2043 * failure while holding pool->lock reliably indicates that someone
2044 * else is managing the pool and the worker which failed trylock
2045 * can proceed to executing work items. This means that anyone
2046 * grabbing manager_arb is responsible for actually performing
2047 * manager duties. If manager_arb is grabbed and released without
2048 * actual management, the pool may stall indefinitely.
2049 *
2050 * manager_mutex is used for exclusion of actual management
2051 * operations. The holder of manager_mutex can be sure that none
2052 * of management operations, including creation and destruction of
2053 * workers, won't take place until the mutex is released. Because
2054 * manager_mutex doesn't interfere with manager role arbitration,
2055 * it is guaranteed that the pool's management, while may be
2056 * delayed, won't be disturbed by someone else grabbing
2057 * manager_mutex.
2058 */
Tejun Heo34a06bd2013-03-12 11:30:00 -07002059 if (!mutex_trylock(&pool->manager_arb))
Tejun Heoe22bee72010-06-29 10:07:14 +02002060 return ret;
2061
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002062 /*
Tejun Heobc3a1af2013-03-13 19:47:39 -07002063 * With manager arbitration won, manager_mutex would be free in
2064 * most cases. trylock first without dropping @pool->lock.
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002065 */
Tejun Heobc3a1af2013-03-13 19:47:39 -07002066 if (unlikely(!mutex_trylock(&pool->manager_mutex))) {
Tejun Heod565ed62013-01-24 11:01:33 -08002067 spin_unlock_irq(&pool->lock);
Tejun Heobc3a1af2013-03-13 19:47:39 -07002068 mutex_lock(&pool->manager_mutex);
Lai Jiangshanee378aa2012-09-10 10:03:44 -07002069 ret = true;
2070 }
2071
Tejun Heo11ebea52012-07-12 14:46:37 -07002072 pool->flags &= ~POOL_MANAGE_WORKERS;
Tejun Heoe22bee72010-06-29 10:07:14 +02002073
2074 /*
2075 * Destroy and then create so that may_start_working() is true
2076 * on return.
2077 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002078 ret |= maybe_destroy_workers(pool);
2079 ret |= maybe_create_worker(pool);
Tejun Heoe22bee72010-06-29 10:07:14 +02002080
Tejun Heobc3a1af2013-03-13 19:47:39 -07002081 mutex_unlock(&pool->manager_mutex);
Tejun Heo34a06bd2013-03-12 11:30:00 -07002082 mutex_unlock(&pool->manager_arb);
Tejun Heoe22bee72010-06-29 10:07:14 +02002083 return ret;
2084}
2085
Tejun Heoa62428c2010-06-29 10:07:10 +02002086/**
2087 * process_one_work - process single work
Tejun Heoc34056a2010-06-29 10:07:11 +02002088 * @worker: self
Tejun Heoa62428c2010-06-29 10:07:10 +02002089 * @work: work to process
2090 *
2091 * Process @work. This function contains all the logics necessary to
2092 * process a single work including synchronization against and
2093 * interaction with other workers on the same cpu, queueing and
2094 * flushing. As long as context requirement is met, any worker can
2095 * call this function to process a work.
2096 *
2097 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002098 * spin_lock_irq(pool->lock) which is released and regrabbed.
Tejun Heoa62428c2010-06-29 10:07:10 +02002099 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002100static void process_one_work(struct worker *worker, struct work_struct *work)
Tejun Heod565ed62013-01-24 11:01:33 -08002101__releases(&pool->lock)
2102__acquires(&pool->lock)
Tejun Heoa62428c2010-06-29 10:07:10 +02002103{
Tejun Heo112202d2013-02-13 19:29:12 -08002104 struct pool_workqueue *pwq = get_work_pwq(work);
Tejun Heobd7bdd42012-07-12 14:46:37 -07002105 struct worker_pool *pool = worker->pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002106 bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE;
Tejun Heo73f53c42010-06-29 10:07:11 +02002107 int work_color;
Tejun Heo7e116292010-06-29 10:07:13 +02002108 struct worker *collision;
Tejun Heoa62428c2010-06-29 10:07:10 +02002109#ifdef CONFIG_LOCKDEP
2110 /*
2111 * It is permissible to free the struct work_struct from
2112 * inside the function that is called from it, this we need to
2113 * take into account for lockdep too. To avoid bogus "held
2114 * lock freed" warnings as well as problems when looking into
2115 * work->lockdep_map, make a copy and use that here.
2116 */
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -07002117 struct lockdep_map lockdep_map;
2118
2119 lockdep_copy_map(&lockdep_map, &work->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002120#endif
Tejun Heo6fec10a2012-07-22 10:16:34 -07002121 /*
2122 * Ensure we're on the correct CPU. DISASSOCIATED test is
2123 * necessary to avoid spurious warnings from rescuers servicing the
Tejun Heo24647572013-01-24 11:01:33 -08002124 * unbound or a disassociated pool.
Tejun Heo6fec10a2012-07-22 10:16:34 -07002125 */
Lai Jiangshan5f7dabf2012-09-18 09:59:23 -07002126 WARN_ON_ONCE(!(worker->flags & WORKER_UNBOUND) &&
Tejun Heo24647572013-01-24 11:01:33 -08002127 !(pool->flags & POOL_DISASSOCIATED) &&
Tejun Heoec22ca52013-01-24 11:01:33 -08002128 raw_smp_processor_id() != pool->cpu);
Tejun Heo25511a42012-07-17 12:39:27 -07002129
Tejun Heo7e116292010-06-29 10:07:13 +02002130 /*
2131 * A single work shouldn't be executed concurrently by
2132 * multiple workers on a single cpu. Check whether anyone is
2133 * already processing the work. If so, defer the work to the
2134 * currently executing one.
2135 */
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002136 collision = find_worker_executing_work(pool, work);
Tejun Heo7e116292010-06-29 10:07:13 +02002137 if (unlikely(collision)) {
2138 move_linked_works(work, &collision->scheduled, NULL);
2139 return;
2140 }
2141
Tejun Heo8930cab2012-08-03 10:30:45 -07002142 /* claim and dequeue */
Tejun Heoa62428c2010-06-29 10:07:10 +02002143 debug_work_deactivate(work);
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002144 hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work);
Tejun Heoc34056a2010-06-29 10:07:11 +02002145 worker->current_work = work;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002146 worker->current_func = work->func;
Tejun Heo112202d2013-02-13 19:29:12 -08002147 worker->current_pwq = pwq;
Tejun Heo73f53c42010-06-29 10:07:11 +02002148 work_color = get_work_color(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002149
Tejun Heoa62428c2010-06-29 10:07:10 +02002150 list_del_init(&work->entry);
2151
Tejun Heo649027d2010-06-29 10:07:14 +02002152 /*
Tejun Heofb0e7be2010-06-29 10:07:15 +02002153 * CPU intensive works don't participate in concurrency
2154 * management. They're the scheduler's responsibility.
2155 */
2156 if (unlikely(cpu_intensive))
2157 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
2158
Tejun Heo974271c2012-07-12 14:46:37 -07002159 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002160 * Unbound pool isn't concurrency managed and work items should be
Tejun Heo974271c2012-07-12 14:46:37 -07002161 * executed ASAP. Wake up another worker if necessary.
2162 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002163 if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool))
2164 wake_up_worker(pool);
Tejun Heo974271c2012-07-12 14:46:37 -07002165
Tejun Heo8930cab2012-08-03 10:30:45 -07002166 /*
Tejun Heo7c3eed52013-01-24 11:01:33 -08002167 * Record the last pool and clear PENDING which should be the last
Tejun Heod565ed62013-01-24 11:01:33 -08002168 * update to @work. Also, do this inside @pool->lock so that
Tejun Heo23657bb2012-08-13 17:08:19 -07002169 * PENDING and queued state changes happen together while IRQ is
2170 * disabled.
Tejun Heo8930cab2012-08-03 10:30:45 -07002171 */
Tejun Heo7c3eed52013-01-24 11:01:33 -08002172 set_work_pool_and_clear_pending(work, pool->id);
Tejun Heoa62428c2010-06-29 10:07:10 +02002173
Tejun Heod565ed62013-01-24 11:01:33 -08002174 spin_unlock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002175
Tejun Heo112202d2013-02-13 19:29:12 -08002176 lock_map_acquire_read(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002177 lock_map_acquire(&lockdep_map);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002178 trace_workqueue_execute_start(work);
Tejun Heoa2c1c572012-12-18 10:35:02 -08002179 worker->current_func(work);
Arjan van de Vene36c8862010-08-21 13:07:26 -07002180 /*
2181 * While we must be careful to not use "work" after this, the trace
2182 * point will only record its address.
2183 */
2184 trace_workqueue_execute_end(work);
Tejun Heoa62428c2010-06-29 10:07:10 +02002185 lock_map_release(&lockdep_map);
Tejun Heo112202d2013-02-13 19:29:12 -08002186 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoa62428c2010-06-29 10:07:10 +02002187
2188 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
Valentin Ilie044c7822012-08-19 00:52:42 +03002189 pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n"
2190 " last function: %pf\n",
Tejun Heoa2c1c572012-12-18 10:35:02 -08002191 current->comm, preempt_count(), task_pid_nr(current),
2192 worker->current_func);
Tejun Heoa62428c2010-06-29 10:07:10 +02002193 debug_show_held_locks(current);
2194 dump_stack();
2195 }
2196
Tejun Heod565ed62013-01-24 11:01:33 -08002197 spin_lock_irq(&pool->lock);
Tejun Heoa62428c2010-06-29 10:07:10 +02002198
Tejun Heofb0e7be2010-06-29 10:07:15 +02002199 /* clear cpu intensive status */
2200 if (unlikely(cpu_intensive))
2201 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
2202
Tejun Heoa62428c2010-06-29 10:07:10 +02002203 /* we're done with it, release */
Sasha Levin42f85702012-12-17 10:01:23 -05002204 hash_del(&worker->hentry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002205 worker->current_work = NULL;
Tejun Heoa2c1c572012-12-18 10:35:02 -08002206 worker->current_func = NULL;
Tejun Heo112202d2013-02-13 19:29:12 -08002207 worker->current_pwq = NULL;
2208 pwq_dec_nr_in_flight(pwq, work_color);
Tejun Heoa62428c2010-06-29 10:07:10 +02002209}
2210
Tejun Heoaffee4b2010-06-29 10:07:12 +02002211/**
2212 * process_scheduled_works - process scheduled works
2213 * @worker: self
2214 *
2215 * Process all scheduled works. Please note that the scheduled list
2216 * may change while processing a work, so this function repeatedly
2217 * fetches a work from the top and executes it.
2218 *
2219 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002220 * spin_lock_irq(pool->lock) which may be released and regrabbed
Tejun Heoaffee4b2010-06-29 10:07:12 +02002221 * multiple times.
2222 */
2223static void process_scheduled_works(struct worker *worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002225 while (!list_empty(&worker->scheduled)) {
2226 struct work_struct *work = list_first_entry(&worker->scheduled,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 struct work_struct, entry);
Tejun Heoc34056a2010-06-29 10:07:11 +02002228 process_one_work(worker, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230}
2231
Tejun Heo4690c4a2010-06-29 10:07:10 +02002232/**
2233 * worker_thread - the worker thread function
Tejun Heoc34056a2010-06-29 10:07:11 +02002234 * @__worker: self
Tejun Heo4690c4a2010-06-29 10:07:10 +02002235 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002236 * The worker thread function. All workers belong to a worker_pool -
2237 * either a per-cpu one or dynamic unbound one. These workers process all
2238 * work items regardless of their specific target workqueue. The only
2239 * exception is work items which belong to workqueues with a rescuer which
2240 * will be explained in rescuer_thread().
Tejun Heo4690c4a2010-06-29 10:07:10 +02002241 */
Tejun Heoc34056a2010-06-29 10:07:11 +02002242static int worker_thread(void *__worker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243{
Tejun Heoc34056a2010-06-29 10:07:11 +02002244 struct worker *worker = __worker;
Tejun Heobd7bdd42012-07-12 14:46:37 -07002245 struct worker_pool *pool = worker->pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
Tejun Heoe22bee72010-06-29 10:07:14 +02002247 /* tell the scheduler that this is a workqueue worker */
2248 worker->task->flags |= PF_WQ_WORKER;
Tejun Heoc8e55f32010-06-29 10:07:12 +02002249woke_up:
Tejun Heod565ed62013-01-24 11:01:33 -08002250 spin_lock_irq(&pool->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
Tejun Heoa9ab7752013-03-19 13:45:21 -07002252 /* am I supposed to die? */
2253 if (unlikely(worker->flags & WORKER_DIE)) {
Tejun Heod565ed62013-01-24 11:01:33 -08002254 spin_unlock_irq(&pool->lock);
Tejun Heoa9ab7752013-03-19 13:45:21 -07002255 WARN_ON_ONCE(!list_empty(&worker->entry));
2256 worker->task->flags &= ~PF_WQ_WORKER;
2257 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 }
2259
Tejun Heoc8e55f32010-06-29 10:07:12 +02002260 worker_leave_idle(worker);
Tejun Heodb7bccf2010-06-29 10:07:12 +02002261recheck:
Tejun Heoe22bee72010-06-29 10:07:14 +02002262 /* no more worker necessary? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002263 if (!need_more_worker(pool))
Tejun Heoe22bee72010-06-29 10:07:14 +02002264 goto sleep;
2265
2266 /* do we need to manage? */
Tejun Heo63d95a92012-07-12 14:46:37 -07002267 if (unlikely(!may_start_working(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002268 goto recheck;
2269
Tejun Heoc8e55f32010-06-29 10:07:12 +02002270 /*
2271 * ->scheduled list can only be filled while a worker is
2272 * preparing to process a work or actually processing it.
2273 * Make sure nobody diddled with it while I was sleeping.
2274 */
Tejun Heo6183c002013-03-12 11:29:57 -07002275 WARN_ON_ONCE(!list_empty(&worker->scheduled));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002276
Tejun Heoe22bee72010-06-29 10:07:14 +02002277 /*
Tejun Heoa9ab7752013-03-19 13:45:21 -07002278 * Finish PREP stage. We're guaranteed to have at least one idle
2279 * worker or that someone else has already assumed the manager
2280 * role. This is where @worker starts participating in concurrency
2281 * management if applicable and concurrency management is restored
2282 * after being rebound. See rebind_workers() for details.
Tejun Heoe22bee72010-06-29 10:07:14 +02002283 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07002284 worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND);
Tejun Heoe22bee72010-06-29 10:07:14 +02002285
2286 do {
Tejun Heoc8e55f32010-06-29 10:07:12 +02002287 struct work_struct *work =
Tejun Heobd7bdd42012-07-12 14:46:37 -07002288 list_first_entry(&pool->worklist,
Tejun Heoc8e55f32010-06-29 10:07:12 +02002289 struct work_struct, entry);
2290
2291 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
2292 /* optimization path, not strictly necessary */
2293 process_one_work(worker, work);
2294 if (unlikely(!list_empty(&worker->scheduled)))
2295 process_scheduled_works(worker);
2296 } else {
2297 move_linked_works(work, &worker->scheduled, NULL);
2298 process_scheduled_works(worker);
2299 }
Tejun Heo63d95a92012-07-12 14:46:37 -07002300 } while (keep_working(pool));
Tejun Heoc8e55f32010-06-29 10:07:12 +02002301
Tejun Heoe22bee72010-06-29 10:07:14 +02002302 worker_set_flags(worker, WORKER_PREP, false);
Tejun Heod313dd82010-07-02 10:03:51 +02002303sleep:
Tejun Heo63d95a92012-07-12 14:46:37 -07002304 if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker))
Tejun Heoe22bee72010-06-29 10:07:14 +02002305 goto recheck;
Tejun Heod313dd82010-07-02 10:03:51 +02002306
Tejun Heoc8e55f32010-06-29 10:07:12 +02002307 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002308 * pool->lock is held and there's no work to process and no need to
2309 * manage, sleep. Workers are woken up only while holding
2310 * pool->lock or from local cpu, so setting the current state
2311 * before releasing pool->lock is enough to prevent losing any
2312 * event.
Tejun Heoc8e55f32010-06-29 10:07:12 +02002313 */
2314 worker_enter_idle(worker);
2315 __set_current_state(TASK_INTERRUPTIBLE);
Tejun Heod565ed62013-01-24 11:01:33 -08002316 spin_unlock_irq(&pool->lock);
Tejun Heoc8e55f32010-06-29 10:07:12 +02002317 schedule();
2318 goto woke_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319}
2320
Tejun Heoe22bee72010-06-29 10:07:14 +02002321/**
2322 * rescuer_thread - the rescuer thread function
Tejun Heo111c2252013-01-17 17:16:24 -08002323 * @__rescuer: self
Tejun Heoe22bee72010-06-29 10:07:14 +02002324 *
2325 * Workqueue rescuer thread function. There's one rescuer for each
Tejun Heo493008a2013-03-12 11:30:03 -07002326 * workqueue which has WQ_MEM_RECLAIM set.
Tejun Heoe22bee72010-06-29 10:07:14 +02002327 *
Tejun Heo706026c2013-01-24 11:01:34 -08002328 * Regular work processing on a pool may block trying to create a new
Tejun Heoe22bee72010-06-29 10:07:14 +02002329 * worker which uses GFP_KERNEL allocation which has slight chance of
2330 * developing into deadlock if some works currently on the same queue
2331 * need to be processed to satisfy the GFP_KERNEL allocation. This is
2332 * the problem rescuer solves.
2333 *
Tejun Heo706026c2013-01-24 11:01:34 -08002334 * When such condition is possible, the pool summons rescuers of all
2335 * workqueues which have works queued on the pool and let them process
Tejun Heoe22bee72010-06-29 10:07:14 +02002336 * those works so that forward progress can be guaranteed.
2337 *
2338 * This should happen rarely.
2339 */
Tejun Heo111c2252013-01-17 17:16:24 -08002340static int rescuer_thread(void *__rescuer)
Tejun Heoe22bee72010-06-29 10:07:14 +02002341{
Tejun Heo111c2252013-01-17 17:16:24 -08002342 struct worker *rescuer = __rescuer;
2343 struct workqueue_struct *wq = rescuer->rescue_wq;
Tejun Heoe22bee72010-06-29 10:07:14 +02002344 struct list_head *scheduled = &rescuer->scheduled;
Tejun Heoe22bee72010-06-29 10:07:14 +02002345
2346 set_user_nice(current, RESCUER_NICE_LEVEL);
Tejun Heo111c2252013-01-17 17:16:24 -08002347
2348 /*
2349 * Mark rescuer as worker too. As WORKER_PREP is never cleared, it
2350 * doesn't participate in concurrency management.
2351 */
2352 rescuer->task->flags |= PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002353repeat:
2354 set_current_state(TASK_INTERRUPTIBLE);
2355
Mike Galbraith412d32e2012-11-28 07:17:18 +01002356 if (kthread_should_stop()) {
2357 __set_current_state(TASK_RUNNING);
Tejun Heo111c2252013-01-17 17:16:24 -08002358 rescuer->task->flags &= ~PF_WQ_WORKER;
Tejun Heoe22bee72010-06-29 10:07:14 +02002359 return 0;
Mike Galbraith412d32e2012-11-28 07:17:18 +01002360 }
Tejun Heoe22bee72010-06-29 10:07:14 +02002361
Tejun Heo493a1722013-03-12 11:29:59 -07002362 /* see whether any pwq is asking for help */
Tejun Heo2e109a22013-03-13 19:47:40 -07002363 spin_lock_irq(&wq_mayday_lock);
Tejun Heo493a1722013-03-12 11:29:59 -07002364
2365 while (!list_empty(&wq->maydays)) {
2366 struct pool_workqueue *pwq = list_first_entry(&wq->maydays,
2367 struct pool_workqueue, mayday_node);
Tejun Heo112202d2013-02-13 19:29:12 -08002368 struct worker_pool *pool = pwq->pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002369 struct work_struct *work, *n;
2370
2371 __set_current_state(TASK_RUNNING);
Tejun Heo493a1722013-03-12 11:29:59 -07002372 list_del_init(&pwq->mayday_node);
2373
Tejun Heo2e109a22013-03-13 19:47:40 -07002374 spin_unlock_irq(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002375
2376 /* migrate to the target cpu if possible */
Lai Jiangshanf36dc672013-02-19 12:17:02 -08002377 worker_maybe_bind_and_lock(pool);
Lai Jiangshanb3104102013-02-19 12:17:02 -08002378 rescuer->pool = pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02002379
2380 /*
2381 * Slurp in all works issued via this workqueue and
2382 * process'em.
2383 */
Tejun Heo6183c002013-03-12 11:29:57 -07002384 WARN_ON_ONCE(!list_empty(&rescuer->scheduled));
Tejun Heobd7bdd42012-07-12 14:46:37 -07002385 list_for_each_entry_safe(work, n, &pool->worklist, entry)
Tejun Heo112202d2013-02-13 19:29:12 -08002386 if (get_work_pwq(work) == pwq)
Tejun Heoe22bee72010-06-29 10:07:14 +02002387 move_linked_works(work, scheduled, &n);
2388
2389 process_scheduled_works(rescuer);
Tejun Heo75769582011-02-14 14:04:46 +01002390
2391 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002392 * Leave this pool. If keep_working() is %true, notify a
Tejun Heo75769582011-02-14 14:04:46 +01002393 * regular worker; otherwise, we end up with 0 concurrency
2394 * and stalling the execution.
2395 */
Tejun Heo63d95a92012-07-12 14:46:37 -07002396 if (keep_working(pool))
2397 wake_up_worker(pool);
Tejun Heo75769582011-02-14 14:04:46 +01002398
Lai Jiangshanb3104102013-02-19 12:17:02 -08002399 rescuer->pool = NULL;
Tejun Heo493a1722013-03-12 11:29:59 -07002400 spin_unlock(&pool->lock);
Tejun Heo2e109a22013-03-13 19:47:40 -07002401 spin_lock(&wq_mayday_lock);
Tejun Heoe22bee72010-06-29 10:07:14 +02002402 }
2403
Tejun Heo2e109a22013-03-13 19:47:40 -07002404 spin_unlock_irq(&wq_mayday_lock);
Tejun Heo493a1722013-03-12 11:29:59 -07002405
Tejun Heo111c2252013-01-17 17:16:24 -08002406 /* rescuers should never participate in concurrency management */
2407 WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
Tejun Heoe22bee72010-06-29 10:07:14 +02002408 schedule();
2409 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410}
2411
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002412struct wq_barrier {
2413 struct work_struct work;
2414 struct completion done;
2415};
2416
2417static void wq_barrier_func(struct work_struct *work)
2418{
2419 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2420 complete(&barr->done);
2421}
2422
Tejun Heo4690c4a2010-06-29 10:07:10 +02002423/**
2424 * insert_wq_barrier - insert a barrier work
Tejun Heo112202d2013-02-13 19:29:12 -08002425 * @pwq: pwq to insert barrier into
Tejun Heo4690c4a2010-06-29 10:07:10 +02002426 * @barr: wq_barrier to insert
Tejun Heoaffee4b2010-06-29 10:07:12 +02002427 * @target: target work to attach @barr to
2428 * @worker: worker currently executing @target, NULL if @target is not executing
Tejun Heo4690c4a2010-06-29 10:07:10 +02002429 *
Tejun Heoaffee4b2010-06-29 10:07:12 +02002430 * @barr is linked to @target such that @barr is completed only after
2431 * @target finishes execution. Please note that the ordering
2432 * guarantee is observed only with respect to @target and on the local
2433 * cpu.
2434 *
2435 * Currently, a queued barrier can't be canceled. This is because
2436 * try_to_grab_pending() can't determine whether the work to be
2437 * grabbed is at the head of the queue and thus can't clear LINKED
2438 * flag of the previous work while there must be a valid next work
2439 * after a work with LINKED flag set.
2440 *
2441 * Note that when @worker is non-NULL, @target may be modified
Tejun Heo112202d2013-02-13 19:29:12 -08002442 * underneath us, so we can't reliably determine pwq from @target.
Tejun Heo4690c4a2010-06-29 10:07:10 +02002443 *
2444 * CONTEXT:
Tejun Heod565ed62013-01-24 11:01:33 -08002445 * spin_lock_irq(pool->lock).
Tejun Heo4690c4a2010-06-29 10:07:10 +02002446 */
Tejun Heo112202d2013-02-13 19:29:12 -08002447static void insert_wq_barrier(struct pool_workqueue *pwq,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002448 struct wq_barrier *barr,
2449 struct work_struct *target, struct worker *worker)
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002450{
Tejun Heoaffee4b2010-06-29 10:07:12 +02002451 struct list_head *head;
2452 unsigned int linked = 0;
2453
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002454 /*
Tejun Heod565ed62013-01-24 11:01:33 -08002455 * debugobject calls are safe here even with pool->lock locked
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002456 * as we know for sure that this will not trigger any of the
2457 * checks and call back into the fixup functions where we
2458 * might deadlock.
2459 */
Andrew Mortonca1cab32010-10-26 14:22:34 -07002460 INIT_WORK_ONSTACK(&barr->work, wq_barrier_func);
Tejun Heo22df02b2010-06-29 10:07:10 +02002461 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002462 init_completion(&barr->done);
Oleg Nesterov83c22522007-05-09 02:33:54 -07002463
Tejun Heoaffee4b2010-06-29 10:07:12 +02002464 /*
2465 * If @target is currently being executed, schedule the
2466 * barrier to the worker; otherwise, put it after @target.
2467 */
2468 if (worker)
2469 head = worker->scheduled.next;
2470 else {
2471 unsigned long *bits = work_data_bits(target);
2472
2473 head = target->entry.next;
2474 /* there can already be other linked works, inherit and set */
2475 linked = *bits & WORK_STRUCT_LINKED;
2476 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2477 }
2478
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002479 debug_work_activate(&barr->work);
Tejun Heo112202d2013-02-13 19:29:12 -08002480 insert_work(pwq, &barr->work, head,
Tejun Heoaffee4b2010-06-29 10:07:12 +02002481 work_color_to_flags(WORK_NO_COLOR) | linked);
Oleg Nesterovfc2e4d72007-05-09 02:33:51 -07002482}
2483
Tejun Heo73f53c42010-06-29 10:07:11 +02002484/**
Tejun Heo112202d2013-02-13 19:29:12 -08002485 * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing
Tejun Heo73f53c42010-06-29 10:07:11 +02002486 * @wq: workqueue being flushed
2487 * @flush_color: new flush color, < 0 for no-op
2488 * @work_color: new work color, < 0 for no-op
2489 *
Tejun Heo112202d2013-02-13 19:29:12 -08002490 * Prepare pwqs for workqueue flushing.
Tejun Heo73f53c42010-06-29 10:07:11 +02002491 *
Tejun Heo112202d2013-02-13 19:29:12 -08002492 * If @flush_color is non-negative, flush_color on all pwqs should be
2493 * -1. If no pwq has in-flight commands at the specified color, all
2494 * pwq->flush_color's stay at -1 and %false is returned. If any pwq
2495 * has in flight commands, its pwq->flush_color is set to
2496 * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq
Tejun Heo73f53c42010-06-29 10:07:11 +02002497 * wakeup logic is armed and %true is returned.
2498 *
2499 * The caller should have initialized @wq->first_flusher prior to
2500 * calling this function with non-negative @flush_color. If
2501 * @flush_color is negative, no flush color update is done and %false
2502 * is returned.
2503 *
Tejun Heo112202d2013-02-13 19:29:12 -08002504 * If @work_color is non-negative, all pwqs should have the same
Tejun Heo73f53c42010-06-29 10:07:11 +02002505 * work_color which is previous to @work_color and all will be
2506 * advanced to @work_color.
2507 *
2508 * CONTEXT:
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002509 * mutex_lock(wq->mutex).
Tejun Heo73f53c42010-06-29 10:07:11 +02002510 *
2511 * RETURNS:
2512 * %true if @flush_color >= 0 and there's something to flush. %false
2513 * otherwise.
2514 */
Tejun Heo112202d2013-02-13 19:29:12 -08002515static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
Tejun Heo73f53c42010-06-29 10:07:11 +02002516 int flush_color, int work_color)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517{
Tejun Heo73f53c42010-06-29 10:07:11 +02002518 bool wait = false;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002519 struct pool_workqueue *pwq;
Oleg Nesterov14441962007-05-23 13:57:57 -07002520
Tejun Heo73f53c42010-06-29 10:07:11 +02002521 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002522 WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush));
Tejun Heo112202d2013-02-13 19:29:12 -08002523 atomic_set(&wq->nr_pwqs_to_flush, 1);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +09002524 }
Oleg Nesterov14441962007-05-23 13:57:57 -07002525
Tejun Heo49e3cf42013-03-12 11:29:58 -07002526 for_each_pwq(pwq, wq) {
Tejun Heo112202d2013-02-13 19:29:12 -08002527 struct worker_pool *pool = pwq->pool;
Tejun Heo73f53c42010-06-29 10:07:11 +02002528
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002529 spin_lock_irq(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002530
2531 if (flush_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002532 WARN_ON_ONCE(pwq->flush_color != -1);
Tejun Heo73f53c42010-06-29 10:07:11 +02002533
Tejun Heo112202d2013-02-13 19:29:12 -08002534 if (pwq->nr_in_flight[flush_color]) {
2535 pwq->flush_color = flush_color;
2536 atomic_inc(&wq->nr_pwqs_to_flush);
Tejun Heo73f53c42010-06-29 10:07:11 +02002537 wait = true;
2538 }
2539 }
2540
2541 if (work_color >= 0) {
Tejun Heo6183c002013-03-12 11:29:57 -07002542 WARN_ON_ONCE(work_color != work_next_color(pwq->work_color));
Tejun Heo112202d2013-02-13 19:29:12 -08002543 pwq->work_color = work_color;
Tejun Heo73f53c42010-06-29 10:07:11 +02002544 }
2545
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002546 spin_unlock_irq(&pool->lock);
Tejun Heo73f53c42010-06-29 10:07:11 +02002547 }
2548
Tejun Heo112202d2013-02-13 19:29:12 -08002549 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush))
Tejun Heo73f53c42010-06-29 10:07:11 +02002550 complete(&wq->first_flusher->done);
2551
2552 return wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553}
2554
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002555/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 * flush_workqueue - ensure that any scheduled work has run to completion.
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002557 * @wq: workqueue to flush
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 *
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002559 * This function sleeps until all work items which were queued on entry
2560 * have finished execution, but it is not livelocked by new incoming ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002562void flush_workqueue(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563{
Tejun Heo73f53c42010-06-29 10:07:11 +02002564 struct wq_flusher this_flusher = {
2565 .list = LIST_HEAD_INIT(this_flusher.list),
2566 .flush_color = -1,
2567 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2568 };
2569 int next_color;
Oleg Nesterovb1f4ec12007-05-09 02:34:12 -07002570
Ingo Molnar3295f0e2008-08-11 10:30:30 +02002571 lock_map_acquire(&wq->lockdep_map);
2572 lock_map_release(&wq->lockdep_map);
Tejun Heo73f53c42010-06-29 10:07:11 +02002573
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002574 mutex_lock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002575
2576 /*
2577 * Start-to-wait phase
2578 */
2579 next_color = work_next_color(wq->work_color);
2580
2581 if (next_color != wq->flush_color) {
2582 /*
2583 * Color space is not full. The current work_color
2584 * becomes our flush_color and work_color is advanced
2585 * by one.
2586 */
Tejun Heo6183c002013-03-12 11:29:57 -07002587 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow));
Tejun Heo73f53c42010-06-29 10:07:11 +02002588 this_flusher.flush_color = wq->work_color;
2589 wq->work_color = next_color;
2590
2591 if (!wq->first_flusher) {
2592 /* no flush in progress, become the first flusher */
Tejun Heo6183c002013-03-12 11:29:57 -07002593 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002594
2595 wq->first_flusher = &this_flusher;
2596
Tejun Heo112202d2013-02-13 19:29:12 -08002597 if (!flush_workqueue_prep_pwqs(wq, wq->flush_color,
Tejun Heo73f53c42010-06-29 10:07:11 +02002598 wq->work_color)) {
2599 /* nothing to flush, done */
2600 wq->flush_color = next_color;
2601 wq->first_flusher = NULL;
2602 goto out_unlock;
2603 }
2604 } else {
2605 /* wait in queue */
Tejun Heo6183c002013-03-12 11:29:57 -07002606 WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002607 list_add_tail(&this_flusher.list, &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002608 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002609 }
2610 } else {
2611 /*
2612 * Oops, color space is full, wait on overflow queue.
2613 * The next flush completion will assign us
2614 * flush_color and transfer to flusher_queue.
2615 */
2616 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2617 }
2618
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002619 mutex_unlock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002620
2621 wait_for_completion(&this_flusher.done);
2622
2623 /*
2624 * Wake-up-and-cascade phase
2625 *
2626 * First flushers are responsible for cascading flushes and
2627 * handling overflow. Non-first flushers can simply return.
2628 */
2629 if (wq->first_flusher != &this_flusher)
2630 return;
2631
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002632 mutex_lock(&wq->mutex);
Tejun Heo73f53c42010-06-29 10:07:11 +02002633
Tejun Heo4ce48b32010-07-02 10:03:51 +02002634 /* we might have raced, check again with mutex held */
2635 if (wq->first_flusher != &this_flusher)
2636 goto out_unlock;
2637
Tejun Heo73f53c42010-06-29 10:07:11 +02002638 wq->first_flusher = NULL;
2639
Tejun Heo6183c002013-03-12 11:29:57 -07002640 WARN_ON_ONCE(!list_empty(&this_flusher.list));
2641 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002642
2643 while (true) {
2644 struct wq_flusher *next, *tmp;
2645
2646 /* complete all the flushers sharing the current flush color */
2647 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2648 if (next->flush_color != wq->flush_color)
2649 break;
2650 list_del_init(&next->list);
2651 complete(&next->done);
2652 }
2653
Tejun Heo6183c002013-03-12 11:29:57 -07002654 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) &&
2655 wq->flush_color != work_next_color(wq->work_color));
Tejun Heo73f53c42010-06-29 10:07:11 +02002656
2657 /* this flush_color is finished, advance by one */
2658 wq->flush_color = work_next_color(wq->flush_color);
2659
2660 /* one color has been freed, handle overflow queue */
2661 if (!list_empty(&wq->flusher_overflow)) {
2662 /*
2663 * Assign the same color to all overflowed
2664 * flushers, advance work_color and append to
2665 * flusher_queue. This is the start-to-wait
2666 * phase for these overflowed flushers.
2667 */
2668 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2669 tmp->flush_color = wq->work_color;
2670
2671 wq->work_color = work_next_color(wq->work_color);
2672
2673 list_splice_tail_init(&wq->flusher_overflow,
2674 &wq->flusher_queue);
Tejun Heo112202d2013-02-13 19:29:12 -08002675 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002676 }
2677
2678 if (list_empty(&wq->flusher_queue)) {
Tejun Heo6183c002013-03-12 11:29:57 -07002679 WARN_ON_ONCE(wq->flush_color != wq->work_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002680 break;
2681 }
2682
2683 /*
2684 * Need to flush more colors. Make the next flusher
Tejun Heo112202d2013-02-13 19:29:12 -08002685 * the new first flusher and arm pwqs.
Tejun Heo73f53c42010-06-29 10:07:11 +02002686 */
Tejun Heo6183c002013-03-12 11:29:57 -07002687 WARN_ON_ONCE(wq->flush_color == wq->work_color);
2688 WARN_ON_ONCE(wq->flush_color != next->flush_color);
Tejun Heo73f53c42010-06-29 10:07:11 +02002689
2690 list_del_init(&next->list);
2691 wq->first_flusher = next;
2692
Tejun Heo112202d2013-02-13 19:29:12 -08002693 if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1))
Tejun Heo73f53c42010-06-29 10:07:11 +02002694 break;
2695
2696 /*
2697 * Meh... this color is already done, clear first
2698 * flusher and repeat cascading.
2699 */
2700 wq->first_flusher = NULL;
2701 }
2702
2703out_unlock:
Lai Jiangshan3c25a552013-03-25 16:57:17 -07002704 mutex_unlock(&wq->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705}
Dave Jonesae90dd52006-06-30 01:40:45 -04002706EXPORT_SYMBOL_GPL(flush_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002708/**
2709 * drain_workqueue - drain a workqueue
2710 * @wq: workqueue to drain
2711 *
2712 * Wait until the workqueue becomes empty. While draining is in progress,
2713 * only chain queueing is allowed. IOW, only currently pending or running
2714 * work items on @wq can queue further work items on it. @wq is flushed
2715 * repeatedly until it becomes empty. The number of flushing is detemined
2716 * by the depth of chaining and should be relatively short. Whine if it
2717 * takes too long.
2718 */
2719void drain_workqueue(struct workqueue_struct *wq)
2720{
2721 unsigned int flush_cnt = 0;
Tejun Heo49e3cf42013-03-12 11:29:58 -07002722 struct pool_workqueue *pwq;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002723
2724 /*
2725 * __queue_work() needs to test whether there are drainers, is much
2726 * hotter than drain_workqueue() and already looks at @wq->flags.
Tejun Heo618b01e2013-03-12 11:30:04 -07002727 * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers.
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002728 */
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002729 mutex_lock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002730 if (!wq->nr_drainers++)
Tejun Heo618b01e2013-03-12 11:30:04 -07002731 wq->flags |= __WQ_DRAINING;
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002732 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002733reflush:
2734 flush_workqueue(wq);
2735
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002736 mutex_lock(&wq->mutex);
Tejun Heo76af4d92013-03-12 11:30:00 -07002737
Tejun Heo49e3cf42013-03-12 11:29:58 -07002738 for_each_pwq(pwq, wq) {
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002739 bool drained;
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002740
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002741 spin_lock_irq(&pwq->pool->lock);
Tejun Heo112202d2013-02-13 19:29:12 -08002742 drained = !pwq->nr_active && list_empty(&pwq->delayed_works);
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002743 spin_unlock_irq(&pwq->pool->lock);
Thomas Tuttlefa2563e2011-09-14 16:22:28 -07002744
2745 if (drained)
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002746 continue;
2747
2748 if (++flush_cnt == 10 ||
2749 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
Tejun Heoc5aa87b2013-03-13 16:51:36 -07002750 pr_warn("workqueue %s: drain_workqueue() isn't complete after %u tries\n",
Valentin Ilie044c7822012-08-19 00:52:42 +03002751 wq->name, flush_cnt);
Tejun Heo76af4d92013-03-12 11:30:00 -07002752
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07002753 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002754 goto reflush;
2755 }
2756
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002757 if (!--wq->nr_drainers)
Tejun Heo618b01e2013-03-12 11:30:04 -07002758 wq->flags &= ~__WQ_DRAINING;
Lai Jiangshan87fc7412013-03-25 16:57:18 -07002759 mutex_unlock(&wq->mutex);
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02002760}
2761EXPORT_SYMBOL_GPL(drain_workqueue);
2762
Tejun Heo606a5022012-08-20 14:51:23 -07002763static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
Tejun Heobaf59022010-09-16 10:42:16 +02002764{
2765 struct worker *worker = NULL;
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002766 struct worker_pool *pool;
Tejun Heo112202d2013-02-13 19:29:12 -08002767 struct pool_workqueue *pwq;
Tejun Heobaf59022010-09-16 10:42:16 +02002768
2769 might_sleep();
Tejun Heobaf59022010-09-16 10:42:16 +02002770
Tejun Heofa1b54e2013-03-12 11:30:00 -07002771 local_irq_disable();
2772 pool = get_work_pool(work);
2773 if (!pool) {
2774 local_irq_enable();
2775 return false;
2776 }
2777
2778 spin_lock(&pool->lock);
Lai Jiangshan0b3dae62013-02-06 18:04:53 -08002779 /* see the comment in try_to_grab_pending() with the same code */
Tejun Heo112202d2013-02-13 19:29:12 -08002780 pwq = get_work_pwq(work);
2781 if (pwq) {
2782 if (unlikely(pwq->pool != pool))
Tejun Heobaf59022010-09-16 10:42:16 +02002783 goto already_gone;
Tejun Heo606a5022012-08-20 14:51:23 -07002784 } else {
Tejun Heoc9e7cf22013-01-24 11:01:33 -08002785 worker = find_worker_executing_work(pool, work);
Tejun Heobaf59022010-09-16 10:42:16 +02002786 if (!worker)
2787 goto already_gone;
Tejun Heo112202d2013-02-13 19:29:12 -08002788 pwq = worker->current_pwq;
Tejun Heo606a5022012-08-20 14:51:23 -07002789 }
Tejun Heobaf59022010-09-16 10:42:16 +02002790
Tejun Heo112202d2013-02-13 19:29:12 -08002791 insert_wq_barrier(pwq, barr, work, worker);
Tejun Heod565ed62013-01-24 11:01:33 -08002792 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002793
Tejun Heoe1594892011-01-09 23:32:15 +01002794 /*
2795 * If @max_active is 1 or rescuer is in use, flushing another work
2796 * item on the same workqueue may lead to deadlock. Make sure the
2797 * flusher is not running on the same workqueue by verifying write
2798 * access.
2799 */
Tejun Heo493008a2013-03-12 11:30:03 -07002800 if (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)
Tejun Heo112202d2013-02-13 19:29:12 -08002801 lock_map_acquire(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002802 else
Tejun Heo112202d2013-02-13 19:29:12 -08002803 lock_map_acquire_read(&pwq->wq->lockdep_map);
2804 lock_map_release(&pwq->wq->lockdep_map);
Tejun Heoe1594892011-01-09 23:32:15 +01002805
Tejun Heobaf59022010-09-16 10:42:16 +02002806 return true;
2807already_gone:
Tejun Heod565ed62013-01-24 11:01:33 -08002808 spin_unlock_irq(&pool->lock);
Tejun Heobaf59022010-09-16 10:42:16 +02002809 return false;
2810}
2811
Oleg Nesterovdb700892008-07-25 01:47:49 -07002812/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002813 * flush_work - wait for a work to finish executing the last queueing instance
2814 * @work: the work to flush
Oleg Nesterovdb700892008-07-25 01:47:49 -07002815 *
Tejun Heo606a5022012-08-20 14:51:23 -07002816 * Wait until @work has finished execution. @work is guaranteed to be idle
2817 * on return if it hasn't been requeued since flush started.
Tejun Heo401a8d02010-09-16 10:36:00 +02002818 *
2819 * RETURNS:
2820 * %true if flush_work() waited for the work to finish execution,
2821 * %false if it was already idle.
Oleg Nesterovdb700892008-07-25 01:47:49 -07002822 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002823bool flush_work(struct work_struct *work)
Oleg Nesterovdb700892008-07-25 01:47:49 -07002824{
Oleg Nesterovdb700892008-07-25 01:47:49 -07002825 struct wq_barrier barr;
2826
Stephen Boyd0976dfc2012-04-20 17:28:50 -07002827 lock_map_acquire(&work->lockdep_map);
2828 lock_map_release(&work->lockdep_map);
2829
Tejun Heo606a5022012-08-20 14:51:23 -07002830 if (start_flush_work(work, &barr)) {
Tejun Heobaf59022010-09-16 10:42:16 +02002831 wait_for_completion(&barr.done);
2832 destroy_work_on_stack(&barr.work);
2833 return true;
Tejun Heo606a5022012-08-20 14:51:23 -07002834 } else {
Tejun Heobaf59022010-09-16 10:42:16 +02002835 return false;
Tejun Heo606a5022012-08-20 14:51:23 -07002836 }
Oleg Nesterovdb700892008-07-25 01:47:49 -07002837}
2838EXPORT_SYMBOL_GPL(flush_work);
2839
Tejun Heo36e227d2012-08-03 10:30:46 -07002840static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
Tejun Heo401a8d02010-09-16 10:36:00 +02002841{
Tejun Heobbb68df2012-08-03 10:30:46 -07002842 unsigned long flags;
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002843 int ret;
2844
2845 do {
Tejun Heobbb68df2012-08-03 10:30:46 -07002846 ret = try_to_grab_pending(work, is_dwork, &flags);
2847 /*
2848 * If someone else is canceling, wait for the same event it
2849 * would be waiting for before retrying.
2850 */
2851 if (unlikely(ret == -ENOENT))
Tejun Heo606a5022012-08-20 14:51:23 -07002852 flush_work(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002853 } while (unlikely(ret < 0));
2854
Tejun Heobbb68df2012-08-03 10:30:46 -07002855 /* tell other tasks trying to grab @work to back off */
2856 mark_work_canceling(work);
2857 local_irq_restore(flags);
2858
Tejun Heo606a5022012-08-20 14:51:23 -07002859 flush_work(work);
Tejun Heo7a22ad72010-06-29 10:07:13 +02002860 clear_work_data(work);
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002861 return ret;
2862}
2863
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002864/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002865 * cancel_work_sync - cancel a work and wait for it to finish
2866 * @work: the work to cancel
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002867 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002868 * Cancel @work and wait for its execution to finish. This function
2869 * can be used even if the work re-queues itself or migrates to
2870 * another workqueue. On return from this function, @work is
2871 * guaranteed to be not pending or executing on any CPU.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002872 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002873 * cancel_work_sync(&delayed_work->work) must not be used for
2874 * delayed_work's. Use cancel_delayed_work_sync() instead.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002875 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002876 * The caller must ensure that the workqueue on which @work was last
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002877 * queued can't be destroyed before this function returns.
Tejun Heo401a8d02010-09-16 10:36:00 +02002878 *
2879 * RETURNS:
2880 * %true if @work was pending, %false otherwise.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002881 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002882bool cancel_work_sync(struct work_struct *work)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002883{
Tejun Heo36e227d2012-08-03 10:30:46 -07002884 return __cancel_work_timer(work, false);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002885}
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002886EXPORT_SYMBOL_GPL(cancel_work_sync);
Oleg Nesterovb89deed2007-05-09 02:33:52 -07002887
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002888/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002889 * flush_delayed_work - wait for a dwork to finish executing the last queueing
2890 * @dwork: the delayed work to flush
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002891 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002892 * Delayed timer is cancelled and the pending work is queued for
2893 * immediate execution. Like flush_work(), this function only
2894 * considers the last queueing instance of @dwork.
Oleg Nesterov1f1f6422007-07-15 23:41:44 -07002895 *
Tejun Heo401a8d02010-09-16 10:36:00 +02002896 * RETURNS:
2897 * %true if flush_work() waited for the work to finish execution,
2898 * %false if it was already idle.
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002899 */
Tejun Heo401a8d02010-09-16 10:36:00 +02002900bool flush_delayed_work(struct delayed_work *dwork)
2901{
Tejun Heo8930cab2012-08-03 10:30:45 -07002902 local_irq_disable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002903 if (del_timer_sync(&dwork->timer))
Lai Jiangshan60c057b2013-02-06 18:04:53 -08002904 __queue_work(dwork->cpu, dwork->wq, &dwork->work);
Tejun Heo8930cab2012-08-03 10:30:45 -07002905 local_irq_enable();
Tejun Heo401a8d02010-09-16 10:36:00 +02002906 return flush_work(&dwork->work);
2907}
2908EXPORT_SYMBOL(flush_delayed_work);
2909
2910/**
Tejun Heo57b30ae2012-08-21 13:18:24 -07002911 * cancel_delayed_work - cancel a delayed work
2912 * @dwork: delayed_work to cancel
Tejun Heo09383492010-09-16 10:48:29 +02002913 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002914 * Kill off a pending delayed_work. Returns %true if @dwork was pending
2915 * and canceled; %false if wasn't pending. Note that the work callback
2916 * function may still be running on return, unless it returns %true and the
2917 * work doesn't re-arm itself. Explicitly flush or use
2918 * cancel_delayed_work_sync() to wait on it.
Tejun Heo09383492010-09-16 10:48:29 +02002919 *
Tejun Heo57b30ae2012-08-21 13:18:24 -07002920 * This function is safe to call from any context including IRQ handler.
Tejun Heo09383492010-09-16 10:48:29 +02002921 */
Tejun Heo57b30ae2012-08-21 13:18:24 -07002922bool cancel_delayed_work(struct delayed_work *dwork)
Tejun Heo09383492010-09-16 10:48:29 +02002923{
Tejun Heo57b30ae2012-08-21 13:18:24 -07002924 unsigned long flags;
2925 int ret;
2926
2927 do {
2928 ret = try_to_grab_pending(&dwork->work, true, &flags);
2929 } while (unlikely(ret == -EAGAIN));
2930
2931 if (unlikely(ret < 0))
2932 return false;
2933
Tejun Heo7c3eed52013-01-24 11:01:33 -08002934 set_work_pool_and_clear_pending(&dwork->work,
2935 get_work_pool_id(&dwork->work));
Tejun Heo57b30ae2012-08-21 13:18:24 -07002936 local_irq_restore(flags);
Dan Magenheimerc0158ca2012-10-18 16:31:37 -07002937 return ret;
Tejun Heo09383492010-09-16 10:48:29 +02002938}
Tejun Heo57b30ae2012-08-21 13:18:24 -07002939EXPORT_SYMBOL(cancel_delayed_work);
Tejun Heo09383492010-09-16 10:48:29 +02002940
2941/**
Tejun Heo401a8d02010-09-16 10:36:00 +02002942 * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish
2943 * @dwork: the delayed work cancel
2944 *
2945 * This is cancel_work_sync() for delayed works.
2946 *
2947 * RETURNS:
2948 * %true if @dwork was pending, %false otherwise.
2949 */
2950bool cancel_delayed_work_sync(struct delayed_work *dwork)
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002951{
Tejun Heo36e227d2012-08-03 10:30:46 -07002952 return __cancel_work_timer(&dwork->work, true);
Oleg Nesterov6e84d642007-05-09 02:34:46 -07002953}
Oleg Nesterovf5a421a2007-07-15 23:41:44 -07002954EXPORT_SYMBOL(cancel_delayed_work_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
Rolf Eike Beer0fcb78c2006-07-30 03:03:42 -07002956/**
Tejun Heo31ddd872010-10-19 11:14:49 +02002957 * schedule_on_each_cpu - execute a function synchronously on each online CPU
Andrew Mortonb6136772006-06-25 05:47:49 -07002958 * @func: the function to call
Andrew Mortonb6136772006-06-25 05:47:49 -07002959 *
Tejun Heo31ddd872010-10-19 11:14:49 +02002960 * schedule_on_each_cpu() executes @func on each online CPU using the
2961 * system workqueue and blocks until all CPUs have completed.
Andrew Mortonb6136772006-06-25 05:47:49 -07002962 * schedule_on_each_cpu() is very slow.
Tejun Heo31ddd872010-10-19 11:14:49 +02002963 *
2964 * RETURNS:
2965 * 0 on success, -errno on failure.
Andrew Mortonb6136772006-06-25 05:47:49 -07002966 */
David Howells65f27f32006-11-22 14:55:48 +00002967int schedule_on_each_cpu(work_func_t func)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002968{
2969 int cpu;
Namhyung Kim38f51562010-08-08 14:24:09 +02002970 struct work_struct __percpu *works;
Christoph Lameter15316ba2006-01-08 01:00:43 -08002971
Andrew Mortonb6136772006-06-25 05:47:49 -07002972 works = alloc_percpu(struct work_struct);
2973 if (!works)
Christoph Lameter15316ba2006-01-08 01:00:43 -08002974 return -ENOMEM;
Andrew Mortonb6136772006-06-25 05:47:49 -07002975
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002976 get_online_cpus();
Tejun Heo93981802009-11-17 14:06:20 -08002977
Christoph Lameter15316ba2006-01-08 01:00:43 -08002978 for_each_online_cpu(cpu) {
Ingo Molnar9bfb1832006-12-18 20:05:09 +01002979 struct work_struct *work = per_cpu_ptr(works, cpu);
2980
2981 INIT_WORK(work, func);
Tejun Heob71ab8c2010-06-29 10:07:14 +02002982 schedule_work_on(cpu, work);
Andi Kleen65a64462009-10-14 06:22:47 +02002983 }
Tejun Heo93981802009-11-17 14:06:20 -08002984
2985 for_each_online_cpu(cpu)
2986 flush_work(per_cpu_ptr(works, cpu));
2987
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002988 put_online_cpus();
Andrew Mortonb6136772006-06-25 05:47:49 -07002989 free_percpu(works);
Christoph Lameter15316ba2006-01-08 01:00:43 -08002990 return 0;
2991}
2992
Alan Sterneef6a7d2010-02-12 17:39:21 +09002993/**
2994 * flush_scheduled_work - ensure that any scheduled work has run to completion.
2995 *
2996 * Forces execution of the kernel-global workqueue and blocks until its
2997 * completion.
2998 *
2999 * Think twice before calling this function! It's very easy to get into
3000 * trouble if you don't take great care. Either of the following situations
3001 * will lead to deadlock:
3002 *
3003 * One of the work items currently on the workqueue needs to acquire
3004 * a lock held by your code or its caller.
3005 *
3006 * Your code is running in the context of a work routine.
3007 *
3008 * They will be detected by lockdep when they occur, but the first might not
3009 * occur very often. It depends on what work items are on the workqueue and
3010 * what locks they need, which you have no control over.
3011 *
3012 * In most situations flushing the entire workqueue is overkill; you merely
3013 * need to know that a particular work item isn't queued and isn't running.
3014 * In such cases you should use cancel_delayed_work_sync() or
3015 * cancel_work_sync() instead.
3016 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017void flush_scheduled_work(void)
3018{
Tejun Heod320c032010-06-29 10:07:14 +02003019 flush_workqueue(system_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020}
Dave Jonesae90dd52006-06-30 01:40:45 -04003021EXPORT_SYMBOL(flush_scheduled_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
3023/**
James Bottomley1fa44ec2006-02-23 12:43:43 -06003024 * execute_in_process_context - reliably execute the routine with user context
3025 * @fn: the function to execute
James Bottomley1fa44ec2006-02-23 12:43:43 -06003026 * @ew: guaranteed storage for the execute work structure (must
3027 * be available when the work executes)
3028 *
3029 * Executes the function immediately if process context is available,
3030 * otherwise schedules the function for delayed execution.
3031 *
3032 * Returns: 0 - function was executed
3033 * 1 - function was scheduled for execution
3034 */
David Howells65f27f32006-11-22 14:55:48 +00003035int execute_in_process_context(work_func_t fn, struct execute_work *ew)
James Bottomley1fa44ec2006-02-23 12:43:43 -06003036{
3037 if (!in_interrupt()) {
David Howells65f27f32006-11-22 14:55:48 +00003038 fn(&ew->work);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003039 return 0;
3040 }
3041
David Howells65f27f32006-11-22 14:55:48 +00003042 INIT_WORK(&ew->work, fn);
James Bottomley1fa44ec2006-02-23 12:43:43 -06003043 schedule_work(&ew->work);
3044
3045 return 1;
3046}
3047EXPORT_SYMBOL_GPL(execute_in_process_context);
3048
Tejun Heo226223a2013-03-12 11:30:05 -07003049#ifdef CONFIG_SYSFS
3050/*
3051 * Workqueues with WQ_SYSFS flag set is visible to userland via
3052 * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the
3053 * following attributes.
3054 *
3055 * per_cpu RO bool : whether the workqueue is per-cpu or unbound
3056 * max_active RW int : maximum number of in-flight work items
3057 *
3058 * Unbound workqueues have the following extra attributes.
3059 *
3060 * id RO int : the associated pool ID
3061 * nice RW int : nice value of the workers
3062 * cpumask RW mask : bitmask of allowed CPUs for the workers
3063 */
3064struct wq_device {
3065 struct workqueue_struct *wq;
3066 struct device dev;
3067};
3068
3069static struct workqueue_struct *dev_to_wq(struct device *dev)
3070{
3071 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
3072
3073 return wq_dev->wq;
3074}
3075
3076static ssize_t wq_per_cpu_show(struct device *dev,
3077 struct device_attribute *attr, char *buf)
3078{
3079 struct workqueue_struct *wq = dev_to_wq(dev);
3080
3081 return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND));
3082}
3083
3084static ssize_t wq_max_active_show(struct device *dev,
3085 struct device_attribute *attr, char *buf)
3086{
3087 struct workqueue_struct *wq = dev_to_wq(dev);
3088
3089 return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active);
3090}
3091
3092static ssize_t wq_max_active_store(struct device *dev,
3093 struct device_attribute *attr,
3094 const char *buf, size_t count)
3095{
3096 struct workqueue_struct *wq = dev_to_wq(dev);
3097 int val;
3098
3099 if (sscanf(buf, "%d", &val) != 1 || val <= 0)
3100 return -EINVAL;
3101
3102 workqueue_set_max_active(wq, val);
3103 return count;
3104}
3105
3106static struct device_attribute wq_sysfs_attrs[] = {
3107 __ATTR(per_cpu, 0444, wq_per_cpu_show, NULL),
3108 __ATTR(max_active, 0644, wq_max_active_show, wq_max_active_store),
3109 __ATTR_NULL,
3110};
3111
3112static ssize_t wq_pool_id_show(struct device *dev,
3113 struct device_attribute *attr, char *buf)
3114{
3115 struct workqueue_struct *wq = dev_to_wq(dev);
3116 struct worker_pool *pool;
3117 int written;
3118
3119 rcu_read_lock_sched();
3120 pool = first_pwq(wq)->pool;
3121 written = scnprintf(buf, PAGE_SIZE, "%d\n", pool->id);
3122 rcu_read_unlock_sched();
3123
3124 return written;
3125}
3126
3127static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr,
3128 char *buf)
3129{
3130 struct workqueue_struct *wq = dev_to_wq(dev);
3131 int written;
3132
Tejun Heo6029a912013-04-01 11:23:34 -07003133 mutex_lock(&wq->mutex);
3134 written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice);
3135 mutex_unlock(&wq->mutex);
Tejun Heo226223a2013-03-12 11:30:05 -07003136
3137 return written;
3138}
3139
3140/* prepare workqueue_attrs for sysfs store operations */
3141static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq)
3142{
3143 struct workqueue_attrs *attrs;
3144
3145 attrs = alloc_workqueue_attrs(GFP_KERNEL);
3146 if (!attrs)
3147 return NULL;
3148
Tejun Heo6029a912013-04-01 11:23:34 -07003149 mutex_lock(&wq->mutex);
3150 copy_workqueue_attrs(attrs, wq->unbound_attrs);
3151 mutex_unlock(&wq->mutex);
Tejun Heo226223a2013-03-12 11:30:05 -07003152 return attrs;
3153}
3154
3155static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr,
3156 const char *buf, size_t count)
3157{
3158 struct workqueue_struct *wq = dev_to_wq(dev);
3159 struct workqueue_attrs *attrs;
3160 int ret;
3161
3162 attrs = wq_sysfs_prep_attrs(wq);
3163 if (!attrs)
3164 return -ENOMEM;
3165
3166 if (sscanf(buf, "%d", &attrs->nice) == 1 &&
3167 attrs->nice >= -20 && attrs->nice <= 19)
3168 ret = apply_workqueue_attrs(wq, attrs);
3169 else
3170 ret = -EINVAL;
3171
3172 free_workqueue_attrs(attrs);
3173 return ret ?: count;
3174}
3175
3176static ssize_t wq_cpumask_show(struct device *dev,
3177 struct device_attribute *attr, char *buf)
3178{
3179 struct workqueue_struct *wq = dev_to_wq(dev);
3180 int written;
3181
Tejun Heo6029a912013-04-01 11:23:34 -07003182 mutex_lock(&wq->mutex);
3183 written = cpumask_scnprintf(buf, PAGE_SIZE, wq->unbound_attrs->cpumask);
3184 mutex_unlock(&wq->mutex);
Tejun Heo226223a2013-03-12 11:30:05 -07003185
3186 written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
3187 return written;
3188}
3189
3190static ssize_t wq_cpumask_store(struct device *dev,
3191 struct device_attribute *attr,
3192 const char *buf, size_t count)
3193{
3194 struct workqueue_struct *wq = dev_to_wq(dev);
3195 struct workqueue_attrs *attrs;
3196 int ret;
3197
3198 attrs = wq_sysfs_prep_attrs(wq);
3199 if (!attrs)
3200 return -ENOMEM;
3201
3202 ret = cpumask_parse(buf, attrs->cpumask);
3203 if (!ret)
3204 ret = apply_workqueue_attrs(wq, attrs);
3205
3206 free_workqueue_attrs(attrs);
3207 return ret ?: count;
3208}
3209
3210static struct device_attribute wq_sysfs_unbound_attrs[] = {
3211 __ATTR(pool_id, 0444, wq_pool_id_show, NULL),
3212 __ATTR(nice, 0644, wq_nice_show, wq_nice_store),
3213 __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store),
3214 __ATTR_NULL,
3215};
3216
3217static struct bus_type wq_subsys = {
3218 .name = "workqueue",
3219 .dev_attrs = wq_sysfs_attrs,
3220};
3221
3222static int __init wq_sysfs_init(void)
3223{
3224 return subsys_virtual_register(&wq_subsys, NULL);
3225}
3226core_initcall(wq_sysfs_init);
3227
3228static void wq_device_release(struct device *dev)
3229{
3230 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
3231
3232 kfree(wq_dev);
3233}
3234
3235/**
3236 * workqueue_sysfs_register - make a workqueue visible in sysfs
3237 * @wq: the workqueue to register
3238 *
3239 * Expose @wq in sysfs under /sys/bus/workqueue/devices.
3240 * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set
3241 * which is the preferred method.
3242 *
3243 * Workqueue user should use this function directly iff it wants to apply
3244 * workqueue_attrs before making the workqueue visible in sysfs; otherwise,
3245 * apply_workqueue_attrs() may race against userland updating the
3246 * attributes.
3247 *
3248 * Returns 0 on success, -errno on failure.
3249 */
3250int workqueue_sysfs_register(struct workqueue_struct *wq)
3251{
3252 struct wq_device *wq_dev;
3253 int ret;
3254
3255 /*
3256 * Adjusting max_active or creating new pwqs by applyting
3257 * attributes breaks ordering guarantee. Disallow exposing ordered
3258 * workqueues.
3259 */
3260 if (WARN_ON(wq->flags & __WQ_ORDERED))
3261 return -EINVAL;
3262
3263 wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL);
3264 if (!wq_dev)
3265 return -ENOMEM;
3266
3267 wq_dev->wq = wq;
3268 wq_dev->dev.bus = &wq_subsys;
3269 wq_dev->dev.init_name = wq->name;
3270 wq_dev->dev.release = wq_device_release;
3271
3272 /*
3273 * unbound_attrs are created separately. Suppress uevent until
3274 * everything is ready.
3275 */
3276 dev_set_uevent_suppress(&wq_dev->dev, true);
3277
3278 ret = device_register(&wq_dev->dev);
3279 if (ret) {
3280 kfree(wq_dev);
3281 wq->wq_dev = NULL;
3282 return ret;
3283 }
3284
3285 if (wq->flags & WQ_UNBOUND) {
3286 struct device_attribute *attr;
3287
3288 for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) {
3289 ret = device_create_file(&wq_dev->dev, attr);
3290 if (ret) {
3291 device_unregister(&wq_dev->dev);
3292 wq->wq_dev = NULL;
3293 return ret;
3294 }
3295 }
3296 }
3297
3298 kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD);
3299 return 0;
3300}
3301
3302/**
3303 * workqueue_sysfs_unregister - undo workqueue_sysfs_register()
3304 * @wq: the workqueue to unregister
3305 *
3306 * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister.
3307 */
3308static void workqueue_sysfs_unregister(struct workqueue_struct *wq)
3309{
3310 struct wq_device *wq_dev = wq->wq_dev;
3311
3312 if (!wq->wq_dev)
3313 return;
3314
3315 wq->wq_dev = NULL;
3316 device_unregister(&wq_dev->dev);
3317}
3318#else /* CONFIG_SYSFS */
3319static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { }
3320#endif /* CONFIG_SYSFS */
3321
Tejun Heo7a4e3442013-03-12 11:30:00 -07003322/**
3323 * free_workqueue_attrs - free a workqueue_attrs
3324 * @attrs: workqueue_attrs to free
3325 *
3326 * Undo alloc_workqueue_attrs().
3327 */
3328void free_workqueue_attrs(struct workqueue_attrs *attrs)
3329{
3330 if (attrs) {
3331 free_cpumask_var(attrs->cpumask);
3332 kfree(attrs);
3333 }
3334}
3335
3336/**
3337 * alloc_workqueue_attrs - allocate a workqueue_attrs
3338 * @gfp_mask: allocation mask to use
3339 *
3340 * Allocate a new workqueue_attrs, initialize with default settings and
3341 * return it. Returns NULL on failure.
3342 */
3343struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask)
3344{
3345 struct workqueue_attrs *attrs;
3346
3347 attrs = kzalloc(sizeof(*attrs), gfp_mask);
3348 if (!attrs)
3349 goto fail;
3350 if (!alloc_cpumask_var(&attrs->cpumask, gfp_mask))
3351 goto fail;
3352
Tejun Heo13e2e552013-04-01 11:23:31 -07003353 cpumask_copy(attrs->cpumask, cpu_possible_mask);
Tejun Heo7a4e3442013-03-12 11:30:00 -07003354 return attrs;
3355fail:
3356 free_workqueue_attrs(attrs);
3357 return NULL;
3358}
3359
Tejun Heo29c91e92013-03-12 11:30:03 -07003360static void copy_workqueue_attrs(struct workqueue_attrs *to,
3361 const struct workqueue_attrs *from)
3362{
3363 to->nice = from->nice;
3364 cpumask_copy(to->cpumask, from->cpumask);
3365}
3366
Tejun Heo29c91e92013-03-12 11:30:03 -07003367/* hash value of the content of @attr */
3368static u32 wqattrs_hash(const struct workqueue_attrs *attrs)
3369{
3370 u32 hash = 0;
3371
3372 hash = jhash_1word(attrs->nice, hash);
Tejun Heo13e2e552013-04-01 11:23:31 -07003373 hash = jhash(cpumask_bits(attrs->cpumask),
3374 BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash);
Tejun Heo29c91e92013-03-12 11:30:03 -07003375 return hash;
3376}
3377
3378/* content equality test */
3379static bool wqattrs_equal(const struct workqueue_attrs *a,
3380 const struct workqueue_attrs *b)
3381{
3382 if (a->nice != b->nice)
3383 return false;
3384 if (!cpumask_equal(a->cpumask, b->cpumask))
3385 return false;
3386 return true;
3387}
3388
Tejun Heo7a4e3442013-03-12 11:30:00 -07003389/**
3390 * init_worker_pool - initialize a newly zalloc'd worker_pool
3391 * @pool: worker_pool to initialize
3392 *
3393 * Initiailize a newly zalloc'd @pool. It also allocates @pool->attrs.
Tejun Heo29c91e92013-03-12 11:30:03 -07003394 * Returns 0 on success, -errno on failure. Even on failure, all fields
3395 * inside @pool proper are initialized and put_unbound_pool() can be called
3396 * on @pool safely to release it.
Tejun Heo7a4e3442013-03-12 11:30:00 -07003397 */
3398static int init_worker_pool(struct worker_pool *pool)
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003399{
3400 spin_lock_init(&pool->lock);
Tejun Heo29c91e92013-03-12 11:30:03 -07003401 pool->id = -1;
3402 pool->cpu = -1;
Tejun Heof3f90ad2013-04-01 11:23:34 -07003403 pool->node = NUMA_NO_NODE;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003404 pool->flags |= POOL_DISASSOCIATED;
3405 INIT_LIST_HEAD(&pool->worklist);
3406 INIT_LIST_HEAD(&pool->idle_list);
3407 hash_init(pool->busy_hash);
3408
3409 init_timer_deferrable(&pool->idle_timer);
3410 pool->idle_timer.function = idle_worker_timeout;
3411 pool->idle_timer.data = (unsigned long)pool;
3412
3413 setup_timer(&pool->mayday_timer, pool_mayday_timeout,
3414 (unsigned long)pool);
3415
3416 mutex_init(&pool->manager_arb);
Tejun Heobc3a1af2013-03-13 19:47:39 -07003417 mutex_init(&pool->manager_mutex);
Tejun Heo822d8402013-03-19 13:45:21 -07003418 idr_init(&pool->worker_idr);
Tejun Heo7a4e3442013-03-12 11:30:00 -07003419
Tejun Heo29c91e92013-03-12 11:30:03 -07003420 INIT_HLIST_NODE(&pool->hash_node);
3421 pool->refcnt = 1;
3422
3423 /* shouldn't fail above this point */
Tejun Heo7a4e3442013-03-12 11:30:00 -07003424 pool->attrs = alloc_workqueue_attrs(GFP_KERNEL);
3425 if (!pool->attrs)
3426 return -ENOMEM;
3427 return 0;
Tejun Heo4e1a1f92013-03-12 11:30:00 -07003428}
3429
Tejun Heo29c91e92013-03-12 11:30:03 -07003430static void rcu_free_pool(struct rcu_head *rcu)
3431{
3432 struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu);
3433
Tejun Heo822d8402013-03-19 13:45:21 -07003434 idr_destroy(&pool->worker_idr);
Tejun Heo29c91e92013-03-12 11:30:03 -07003435 free_workqueue_attrs(pool->attrs);
3436 kfree(pool);
3437}
3438
3439/**
3440 * put_unbound_pool - put a worker_pool
3441 * @pool: worker_pool to put
3442 *
3443 * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003444 * safe manner. get_unbound_pool() calls this function on its failure path
3445 * and this function should be able to release pools which went through,
3446 * successfully or not, init_worker_pool().
Tejun Heoa892cac2013-04-01 11:23:32 -07003447 *
3448 * Should be called with wq_pool_mutex held.
Tejun Heo29c91e92013-03-12 11:30:03 -07003449 */
3450static void put_unbound_pool(struct worker_pool *pool)
3451{
3452 struct worker *worker;
3453
Tejun Heoa892cac2013-04-01 11:23:32 -07003454 lockdep_assert_held(&wq_pool_mutex);
3455
3456 if (--pool->refcnt)
Tejun Heo29c91e92013-03-12 11:30:03 -07003457 return;
Tejun Heo29c91e92013-03-12 11:30:03 -07003458
3459 /* sanity checks */
3460 if (WARN_ON(!(pool->flags & POOL_DISASSOCIATED)) ||
Tejun Heoa892cac2013-04-01 11:23:32 -07003461 WARN_ON(!list_empty(&pool->worklist)))
Tejun Heo29c91e92013-03-12 11:30:03 -07003462 return;
Tejun Heo29c91e92013-03-12 11:30:03 -07003463
3464 /* release id and unhash */
3465 if (pool->id >= 0)
3466 idr_remove(&worker_pool_idr, pool->id);
3467 hash_del(&pool->hash_node);
3468
Tejun Heoc5aa87b2013-03-13 16:51:36 -07003469 /*
3470 * Become the manager and destroy all workers. Grabbing
3471 * manager_arb prevents @pool's workers from blocking on
3472 * manager_mutex.
3473 */
Tejun Heo29c91e92013-03-12 11:30:03 -07003474 mutex_lock(&pool->manager_arb);
Tejun Heocd549682013-03-13 19:47:39 -07003475 mutex_lock(&pool->manager_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003476 spin_lock_irq(&pool->lock);
3477
3478 while ((worker = first_worker(pool)))
3479 destroy_worker(worker);
3480 WARN_ON(pool->nr_workers || pool->nr_idle);
3481
3482 spin_unlock_irq(&pool->lock);
Tejun Heocd549682013-03-13 19:47:39 -07003483 mutex_unlock(&pool->manager_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003484 mutex_unlock(&pool->manager_arb);
3485
3486 /* shut down the timers */
3487 del_timer_sync(&pool->idle_timer);
3488 del_timer_sync(&pool->mayday_timer);
3489
3490 /* sched-RCU protected to allow dereferences from get_work_pool() */
3491 call_rcu_sched(&pool->rcu, rcu_free_pool);
3492}
3493
3494/**
3495 * get_unbound_pool - get a worker_pool with the specified attributes
3496 * @attrs: the attributes of the worker_pool to get
3497 *
3498 * Obtain a worker_pool which has the same attributes as @attrs, bump the
3499 * reference count and return it. If there already is a matching
3500 * worker_pool, it will be used; otherwise, this function attempts to
3501 * create a new one. On failure, returns NULL.
Tejun Heoa892cac2013-04-01 11:23:32 -07003502 *
3503 * Should be called with wq_pool_mutex held.
Tejun Heo29c91e92013-03-12 11:30:03 -07003504 */
3505static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs)
3506{
Tejun Heo29c91e92013-03-12 11:30:03 -07003507 u32 hash = wqattrs_hash(attrs);
3508 struct worker_pool *pool;
Tejun Heof3f90ad2013-04-01 11:23:34 -07003509 int node;
Tejun Heo29c91e92013-03-12 11:30:03 -07003510
Tejun Heoa892cac2013-04-01 11:23:32 -07003511 lockdep_assert_held(&wq_pool_mutex);
Tejun Heo29c91e92013-03-12 11:30:03 -07003512
3513 /* do we already have a matching pool? */
Tejun Heo29c91e92013-03-12 11:30:03 -07003514 hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) {
3515 if (wqattrs_equal(pool->attrs, attrs)) {
3516 pool->refcnt++;
3517 goto out_unlock;
3518 }
3519 }
Tejun Heo29c91e92013-03-12 11:30:03 -07003520
3521 /* nope, create a new one */
3522 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
3523 if (!pool || init_worker_pool(pool) < 0)
3524 goto fail;
3525
Lai Jiangshan12ee4fc2013-03-20 03:28:01 +08003526 if (workqueue_freezing)
3527 pool->flags |= POOL_FREEZING;
3528
Tejun Heo8864b4e2013-03-12 11:30:04 -07003529 lockdep_set_subclass(&pool->lock, 1); /* see put_pwq() */
Tejun Heo29c91e92013-03-12 11:30:03 -07003530 copy_workqueue_attrs(pool->attrs, attrs);
3531
Tejun Heof3f90ad2013-04-01 11:23:34 -07003532 /* if cpumask is contained inside a NUMA node, we belong to that node */
3533 if (wq_numa_enabled) {
3534 for_each_node(node) {
3535 if (cpumask_subset(pool->attrs->cpumask,
3536 wq_numa_possible_cpumask[node])) {
3537 pool->node = node;
3538 break;
3539 }
3540 }
3541 }
3542
Tejun Heo29c91e92013-03-12 11:30:03 -07003543 if (worker_pool_assign_id(pool) < 0)
3544 goto fail;
3545
3546 /* create and start the initial worker */
Tejun Heoebf44d12013-03-13 19:47:39 -07003547 if (create_and_start_worker(pool) < 0)
Tejun Heo29c91e92013-03-12 11:30:03 -07003548 goto fail;
3549
Tejun Heo29c91e92013-03-12 11:30:03 -07003550 /* install */
Tejun Heo29c91e92013-03-12 11:30:03 -07003551 hash_add(unbound_pool_hash, &pool->hash_node, hash);
3552out_unlock:
Tejun Heo29c91e92013-03-12 11:30:03 -07003553 return pool;
3554fail:
Tejun Heo29c91e92013-03-12 11:30:03 -07003555 if (pool)
3556 put_unbound_pool(pool);
3557 return NULL;
3558}
3559
Tejun Heo8864b4e2013-03-12 11:30:04 -07003560static void rcu_free_pwq(struct rcu_head *rcu)
3561{
3562 kmem_cache_free(pwq_cache,
3563 container_of(rcu, struct pool_workqueue, rcu));
3564}
3565
3566/*
3567 * Scheduled on system_wq by put_pwq() when an unbound pwq hits zero refcnt
3568 * and needs to be destroyed.
3569 */
3570static void pwq_unbound_release_workfn(struct work_struct *work)
3571{
3572 struct pool_workqueue *pwq = container_of(work, struct pool_workqueue,
3573 unbound_release_work);
3574 struct workqueue_struct *wq = pwq->wq;
3575 struct worker_pool *pool = pwq->pool;
Tejun Heobc0caf02013-04-01 11:23:31 -07003576 bool is_last;
Tejun Heo8864b4e2013-03-12 11:30:04 -07003577
3578 if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)))
3579 return;
3580
Tejun Heo75ccf592013-03-12 11:30:04 -07003581 /*
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003582 * Unlink @pwq. Synchronization against wq->mutex isn't strictly
Tejun Heo75ccf592013-03-12 11:30:04 -07003583 * necessary on release but do it anyway. It's easier to verify
3584 * and consistent with the linking path.
3585 */
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003586 mutex_lock(&wq->mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003587 list_del_rcu(&pwq->pwqs_node);
Tejun Heobc0caf02013-04-01 11:23:31 -07003588 is_last = list_empty(&wq->pwqs);
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003589 mutex_unlock(&wq->mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003590
Tejun Heoa892cac2013-04-01 11:23:32 -07003591 mutex_lock(&wq_pool_mutex);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003592 put_unbound_pool(pool);
Tejun Heoa892cac2013-04-01 11:23:32 -07003593 mutex_unlock(&wq_pool_mutex);
3594
Tejun Heo8864b4e2013-03-12 11:30:04 -07003595 call_rcu_sched(&pwq->rcu, rcu_free_pwq);
3596
3597 /*
3598 * If we're the last pwq going away, @wq is already dead and no one
3599 * is gonna access it anymore. Free it.
3600 */
Tejun Heo6029a912013-04-01 11:23:34 -07003601 if (is_last) {
3602 free_workqueue_attrs(wq->unbound_attrs);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003603 kfree(wq);
Tejun Heo6029a912013-04-01 11:23:34 -07003604 }
Tejun Heo8864b4e2013-03-12 11:30:04 -07003605}
3606
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003607/**
Tejun Heo699ce092013-03-13 16:51:35 -07003608 * pwq_adjust_max_active - update a pwq's max_active to the current setting
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003609 * @pwq: target pool_workqueue
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003610 *
Tejun Heo699ce092013-03-13 16:51:35 -07003611 * If @pwq isn't freezing, set @pwq->max_active to the associated
3612 * workqueue's saved_max_active and activate delayed work items
3613 * accordingly. If @pwq is freezing, clear @pwq->max_active to zero.
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003614 */
Tejun Heo699ce092013-03-13 16:51:35 -07003615static void pwq_adjust_max_active(struct pool_workqueue *pwq)
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003616{
Tejun Heo699ce092013-03-13 16:51:35 -07003617 struct workqueue_struct *wq = pwq->wq;
3618 bool freezable = wq->flags & WQ_FREEZABLE;
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003619
Tejun Heo699ce092013-03-13 16:51:35 -07003620 /* for @wq->saved_max_active */
Lai Jiangshana357fc02013-03-25 16:57:19 -07003621 lockdep_assert_held(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07003622
3623 /* fast exit for non-freezable wqs */
3624 if (!freezable && pwq->max_active == wq->saved_max_active)
3625 return;
3626
Lai Jiangshana357fc02013-03-25 16:57:19 -07003627 spin_lock_irq(&pwq->pool->lock);
Tejun Heo699ce092013-03-13 16:51:35 -07003628
3629 if (!freezable || !(pwq->pool->flags & POOL_FREEZING)) {
3630 pwq->max_active = wq->saved_max_active;
3631
3632 while (!list_empty(&pwq->delayed_works) &&
3633 pwq->nr_active < pwq->max_active)
3634 pwq_activate_first_delayed(pwq);
Lai Jiangshan951a0782013-03-20 10:52:30 -07003635
3636 /*
3637 * Need to kick a worker after thawed or an unbound wq's
3638 * max_active is bumped. It's a slow path. Do it always.
3639 */
3640 wake_up_worker(pwq->pool);
Tejun Heo699ce092013-03-13 16:51:35 -07003641 } else {
3642 pwq->max_active = 0;
3643 }
3644
Lai Jiangshana357fc02013-03-25 16:57:19 -07003645 spin_unlock_irq(&pwq->pool->lock);
Tejun Heo0fbd95a2013-03-13 16:51:35 -07003646}
3647
Tejun Heoe50aba92013-04-01 11:23:35 -07003648/* initialize newly alloced @pwq which is associated with @wq and @pool */
Tejun Heof147f292013-04-01 11:23:35 -07003649static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq,
3650 struct worker_pool *pool)
Tejun Heod2c1d402013-03-12 11:30:04 -07003651{
3652 BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK);
3653
Tejun Heoe50aba92013-04-01 11:23:35 -07003654 memset(pwq, 0, sizeof(*pwq));
3655
Tejun Heod2c1d402013-03-12 11:30:04 -07003656 pwq->pool = pool;
3657 pwq->wq = wq;
3658 pwq->flush_color = -1;
Tejun Heo8864b4e2013-03-12 11:30:04 -07003659 pwq->refcnt = 1;
Tejun Heod2c1d402013-03-12 11:30:04 -07003660 INIT_LIST_HEAD(&pwq->delayed_works);
Tejun Heo1befcf32013-04-01 11:23:35 -07003661 INIT_LIST_HEAD(&pwq->pwqs_node);
Tejun Heod2c1d402013-03-12 11:30:04 -07003662 INIT_LIST_HEAD(&pwq->mayday_node);
Tejun Heo8864b4e2013-03-12 11:30:04 -07003663 INIT_WORK(&pwq->unbound_release_work, pwq_unbound_release_workfn);
Tejun Heof147f292013-04-01 11:23:35 -07003664}
Tejun Heod2c1d402013-03-12 11:30:04 -07003665
Tejun Heof147f292013-04-01 11:23:35 -07003666/* sync @pwq with the current state of its associated wq and link it */
Tejun Heo1befcf32013-04-01 11:23:35 -07003667static void link_pwq(struct pool_workqueue *pwq)
Tejun Heof147f292013-04-01 11:23:35 -07003668{
3669 struct workqueue_struct *wq = pwq->wq;
3670
3671 lockdep_assert_held(&wq->mutex);
Tejun Heo75ccf592013-03-12 11:30:04 -07003672
Tejun Heo1befcf32013-04-01 11:23:35 -07003673 /* may be called multiple times, ignore if already linked */
3674 if (!list_empty(&pwq->pwqs_node))
3675 return;
3676
Tejun Heo983ca252013-03-13 16:51:35 -07003677 /*
3678 * Set the matching work_color. This is synchronized with
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003679 * wq->mutex to avoid confusing flush_workqueue().
Tejun Heo983ca252013-03-13 16:51:35 -07003680 */
Tejun Heo75ccf592013-03-12 11:30:04 -07003681 pwq->work_color = wq->work_color;
Tejun Heo983ca252013-03-13 16:51:35 -07003682
3683 /* sync max_active to the current setting */
3684 pwq_adjust_max_active(pwq);
3685
3686 /* link in @pwq */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003687 list_add_rcu(&pwq->pwqs_node, &wq->pwqs);
Tejun Heof147f292013-04-01 11:23:35 -07003688}
Lai Jiangshana357fc02013-03-25 16:57:19 -07003689
Tejun Heof147f292013-04-01 11:23:35 -07003690/* obtain a pool matching @attr and create a pwq associating the pool and @wq */
3691static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq,
3692 const struct workqueue_attrs *attrs)
3693{
3694 struct worker_pool *pool;
3695 struct pool_workqueue *pwq;
3696
3697 lockdep_assert_held(&wq_pool_mutex);
3698
3699 pool = get_unbound_pool(attrs);
3700 if (!pool)
3701 return NULL;
3702
Tejun Heoe50aba92013-04-01 11:23:35 -07003703 pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node);
Tejun Heof147f292013-04-01 11:23:35 -07003704 if (!pwq) {
3705 put_unbound_pool(pool);
3706 return NULL;
Tejun Heodf2d5ae2013-04-01 11:23:35 -07003707 }
Tejun Heo6029a912013-04-01 11:23:34 -07003708
Tejun Heof147f292013-04-01 11:23:35 -07003709 init_pwq(pwq, wq, pool);
3710 return pwq;
Tejun Heod2c1d402013-03-12 11:30:04 -07003711}
3712
Tejun Heo1befcf32013-04-01 11:23:35 -07003713/* install @pwq into @wq's numa_pwq_tbl[] for @node and return the old pwq */
3714static struct pool_workqueue *numa_pwq_tbl_install(struct workqueue_struct *wq,
3715 int node,
3716 struct pool_workqueue *pwq)
3717{
3718 struct pool_workqueue *old_pwq;
3719
3720 lockdep_assert_held(&wq->mutex);
3721
3722 /* link_pwq() can handle duplicate calls */
3723 link_pwq(pwq);
3724
3725 old_pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]);
3726 rcu_assign_pointer(wq->numa_pwq_tbl[node], pwq);
3727 return old_pwq;
3728}
3729
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003730/**
3731 * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue
3732 * @wq: the target workqueue
3733 * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs()
3734 *
3735 * Apply @attrs to an unbound workqueue @wq. If @attrs doesn't match the
3736 * current attributes, a new pwq is created and made the first pwq which
3737 * will serve all new work items. Older pwqs are released as in-flight
3738 * work items finish. Note that a work item which repeatedly requeues
3739 * itself back-to-back will stay on its current pwq.
3740 *
3741 * Performs GFP_KERNEL allocations. Returns 0 on success and -errno on
3742 * failure.
3743 */
3744int apply_workqueue_attrs(struct workqueue_struct *wq,
3745 const struct workqueue_attrs *attrs)
3746{
Tejun Heo13e2e552013-04-01 11:23:31 -07003747 struct workqueue_attrs *new_attrs;
Tejun Heo1befcf32013-04-01 11:23:35 -07003748 struct pool_workqueue *pwq, *last_pwq = NULL;
Tejun Heof147f292013-04-01 11:23:35 -07003749 int node, ret;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003750
Tejun Heo8719dce2013-03-12 11:30:04 -07003751 /* only unbound workqueues can change attributes */
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003752 if (WARN_ON(!(wq->flags & WQ_UNBOUND)))
3753 return -EINVAL;
3754
Tejun Heo8719dce2013-03-12 11:30:04 -07003755 /* creating multiple pwqs breaks ordering guarantee */
3756 if (WARN_ON((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs)))
3757 return -EINVAL;
3758
Tejun Heo13e2e552013-04-01 11:23:31 -07003759 /* make a copy of @attrs and sanitize it */
3760 new_attrs = alloc_workqueue_attrs(GFP_KERNEL);
3761 if (!new_attrs)
3762 goto enomem;
3763
3764 copy_workqueue_attrs(new_attrs, attrs);
3765 cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask);
3766
Tejun Heoa892cac2013-04-01 11:23:32 -07003767 mutex_lock(&wq_pool_mutex);
Tejun Heof147f292013-04-01 11:23:35 -07003768 pwq = alloc_unbound_pwq(wq, new_attrs);
Tejun Heoa892cac2013-04-01 11:23:32 -07003769 mutex_unlock(&wq_pool_mutex);
Tejun Heof147f292013-04-01 11:23:35 -07003770 if (!pwq)
3771 goto enomem;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003772
Tejun Heof147f292013-04-01 11:23:35 -07003773 mutex_lock(&wq->mutex);
3774
Tejun Heof147f292013-04-01 11:23:35 -07003775 copy_workqueue_attrs(wq->unbound_attrs, new_attrs);
3776 for_each_node(node)
Tejun Heo1befcf32013-04-01 11:23:35 -07003777 last_pwq = numa_pwq_tbl_install(wq, node, pwq);
Tejun Heof147f292013-04-01 11:23:35 -07003778
3779 mutex_unlock(&wq->mutex);
3780
Tejun Heodce90d42013-04-01 11:23:35 -07003781 put_pwq_unlocked(last_pwq);
Tejun Heo48621252013-04-01 11:23:31 -07003782 ret = 0;
3783 /* fall through */
3784out_free:
3785 free_workqueue_attrs(new_attrs);
3786 return ret;
Tejun Heo13e2e552013-04-01 11:23:31 -07003787
3788enomem:
Tejun Heo48621252013-04-01 11:23:31 -07003789 ret = -ENOMEM;
3790 goto out_free;
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003791}
3792
Tejun Heo30cdf242013-03-12 11:29:57 -07003793static int alloc_and_link_pwqs(struct workqueue_struct *wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003795 bool highpri = wq->flags & WQ_HIGHPRI;
Tejun Heo30cdf242013-03-12 11:29:57 -07003796 int cpu;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01003797
Tejun Heo30cdf242013-03-12 11:29:57 -07003798 if (!(wq->flags & WQ_UNBOUND)) {
Tejun Heo420c0dd2013-03-12 11:29:59 -07003799 wq->cpu_pwqs = alloc_percpu(struct pool_workqueue);
3800 if (!wq->cpu_pwqs)
Tejun Heo30cdf242013-03-12 11:29:57 -07003801 return -ENOMEM;
3802
3803 for_each_possible_cpu(cpu) {
Tejun Heo7fb98ea2013-03-12 11:30:00 -07003804 struct pool_workqueue *pwq =
3805 per_cpu_ptr(wq->cpu_pwqs, cpu);
Tejun Heo7a62c2c2013-03-12 11:30:03 -07003806 struct worker_pool *cpu_pools =
Tejun Heof02ae732013-03-12 11:30:03 -07003807 per_cpu(cpu_worker_pools, cpu);
Tejun Heo30cdf242013-03-12 11:29:57 -07003808
Tejun Heof147f292013-04-01 11:23:35 -07003809 init_pwq(pwq, wq, &cpu_pools[highpri]);
3810
3811 mutex_lock(&wq->mutex);
Tejun Heo1befcf32013-04-01 11:23:35 -07003812 link_pwq(pwq);
Tejun Heof147f292013-04-01 11:23:35 -07003813 mutex_unlock(&wq->mutex);
Tejun Heo30cdf242013-03-12 11:29:57 -07003814 }
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003815 return 0;
Tejun Heo30cdf242013-03-12 11:29:57 -07003816 } else {
Tejun Heo9e8cd2f2013-03-12 11:30:04 -07003817 return apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]);
Tejun Heo30cdf242013-03-12 11:29:57 -07003818 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003819}
3820
Tejun Heof3421792010-07-02 10:03:51 +02003821static int wq_clamp_max_active(int max_active, unsigned int flags,
3822 const char *name)
Tejun Heob71ab8c2010-06-29 10:07:14 +02003823{
Tejun Heof3421792010-07-02 10:03:51 +02003824 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
3825
3826 if (max_active < 1 || max_active > lim)
Valentin Ilie044c7822012-08-19 00:52:42 +03003827 pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n",
3828 max_active, name, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003829
Tejun Heof3421792010-07-02 10:03:51 +02003830 return clamp_val(max_active, 1, lim);
Tejun Heob71ab8c2010-06-29 10:07:14 +02003831}
3832
Tejun Heob196be82012-01-10 15:11:35 -08003833struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
Tejun Heod320c032010-06-29 10:07:14 +02003834 unsigned int flags,
3835 int max_active,
3836 struct lock_class_key *key,
Tejun Heob196be82012-01-10 15:11:35 -08003837 const char *lock_name, ...)
Oleg Nesterov3af244332007-05-09 02:34:09 -07003838{
Tejun Heodf2d5ae2013-04-01 11:23:35 -07003839 size_t tbl_size = 0;
Tejun Heoecf68812013-04-01 11:23:34 -07003840 va_list args;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003841 struct workqueue_struct *wq;
Tejun Heo49e3cf42013-03-12 11:29:58 -07003842 struct pool_workqueue *pwq;
Tejun Heob196be82012-01-10 15:11:35 -08003843
Tejun Heoecf68812013-04-01 11:23:34 -07003844 /* allocate wq and format name */
Tejun Heodf2d5ae2013-04-01 11:23:35 -07003845 if (flags & WQ_UNBOUND)
3846 tbl_size = wq_numa_tbl_len * sizeof(wq->numa_pwq_tbl[0]);
3847
3848 wq = kzalloc(sizeof(*wq) + tbl_size, GFP_KERNEL);
Tejun Heob196be82012-01-10 15:11:35 -08003849 if (!wq)
Tejun Heod2c1d402013-03-12 11:30:04 -07003850 return NULL;
Tejun Heob196be82012-01-10 15:11:35 -08003851
Tejun Heo6029a912013-04-01 11:23:34 -07003852 if (flags & WQ_UNBOUND) {
3853 wq->unbound_attrs = alloc_workqueue_attrs(GFP_KERNEL);
3854 if (!wq->unbound_attrs)
3855 goto err_free_wq;
3856 }
3857
Tejun Heoecf68812013-04-01 11:23:34 -07003858 va_start(args, lock_name);
3859 vsnprintf(wq->name, sizeof(wq->name), fmt, args);
Tejun Heob196be82012-01-10 15:11:35 -08003860 va_end(args);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003861
Tejun Heod320c032010-06-29 10:07:14 +02003862 max_active = max_active ?: WQ_DFL_ACTIVE;
Tejun Heob196be82012-01-10 15:11:35 -08003863 max_active = wq_clamp_max_active(max_active, flags, wq->name);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003864
Tejun Heob196be82012-01-10 15:11:35 -08003865 /* init wq */
Tejun Heo97e37d72010-06-29 10:07:10 +02003866 wq->flags = flags;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003867 wq->saved_max_active = max_active;
Lai Jiangshan3c25a552013-03-25 16:57:17 -07003868 mutex_init(&wq->mutex);
Tejun Heo112202d2013-02-13 19:29:12 -08003869 atomic_set(&wq->nr_pwqs_to_flush, 0);
Tejun Heo30cdf242013-03-12 11:29:57 -07003870 INIT_LIST_HEAD(&wq->pwqs);
Tejun Heo73f53c42010-06-29 10:07:11 +02003871 INIT_LIST_HEAD(&wq->flusher_queue);
3872 INIT_LIST_HEAD(&wq->flusher_overflow);
Tejun Heo493a1722013-03-12 11:29:59 -07003873 INIT_LIST_HEAD(&wq->maydays);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003874
Johannes Bergeb13ba82008-01-16 09:51:58 +01003875 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
Oleg Nesterovcce1a162007-05-09 02:34:13 -07003876 INIT_LIST_HEAD(&wq->list);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003877
Tejun Heo30cdf242013-03-12 11:29:57 -07003878 if (alloc_and_link_pwqs(wq) < 0)
Tejun Heod2c1d402013-03-12 11:30:04 -07003879 goto err_free_wq;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003880
Tejun Heo493008a2013-03-12 11:30:03 -07003881 /*
3882 * Workqueues which may be used during memory reclaim should
3883 * have a rescuer to guarantee forward progress.
3884 */
3885 if (flags & WQ_MEM_RECLAIM) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003886 struct worker *rescuer;
3887
Tejun Heod2c1d402013-03-12 11:30:04 -07003888 rescuer = alloc_worker();
Tejun Heoe22bee72010-06-29 10:07:14 +02003889 if (!rescuer)
Tejun Heod2c1d402013-03-12 11:30:04 -07003890 goto err_destroy;
Tejun Heoe22bee72010-06-29 10:07:14 +02003891
Tejun Heo111c2252013-01-17 17:16:24 -08003892 rescuer->rescue_wq = wq;
3893 rescuer->task = kthread_create(rescuer_thread, rescuer, "%s",
Tejun Heob196be82012-01-10 15:11:35 -08003894 wq->name);
Tejun Heod2c1d402013-03-12 11:30:04 -07003895 if (IS_ERR(rescuer->task)) {
3896 kfree(rescuer);
3897 goto err_destroy;
3898 }
Tejun Heoe22bee72010-06-29 10:07:14 +02003899
Tejun Heod2c1d402013-03-12 11:30:04 -07003900 wq->rescuer = rescuer;
Tejun Heo14a40ff2013-03-19 13:45:20 -07003901 rescuer->task->flags |= PF_NO_SETAFFINITY;
Tejun Heoe22bee72010-06-29 10:07:14 +02003902 wake_up_process(rescuer->task);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003903 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07003904
Tejun Heo226223a2013-03-12 11:30:05 -07003905 if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq))
3906 goto err_destroy;
3907
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003908 /*
Lai Jiangshan68e13a62013-03-25 16:57:17 -07003909 * wq_pool_mutex protects global freeze state and workqueues list.
3910 * Grab it, adjust max_active and add the new @wq to workqueues
3911 * list.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003912 */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07003913 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003914
Lai Jiangshana357fc02013-03-25 16:57:19 -07003915 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07003916 for_each_pwq(pwq, wq)
3917 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07003918 mutex_unlock(&wq->mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003919
Tejun Heo15376632010-06-29 10:07:11 +02003920 list_add(&wq->list, &workqueues);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003921
Lai Jiangshan68e13a62013-03-25 16:57:17 -07003922 mutex_unlock(&wq_pool_mutex);
Tejun Heo15376632010-06-29 10:07:11 +02003923
Oleg Nesterov3af244332007-05-09 02:34:09 -07003924 return wq;
Tejun Heod2c1d402013-03-12 11:30:04 -07003925
3926err_free_wq:
Tejun Heo6029a912013-04-01 11:23:34 -07003927 free_workqueue_attrs(wq->unbound_attrs);
Tejun Heod2c1d402013-03-12 11:30:04 -07003928 kfree(wq);
3929 return NULL;
3930err_destroy:
3931 destroy_workqueue(wq);
Tejun Heo4690c4a2010-06-29 10:07:10 +02003932 return NULL;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003933}
Tejun Heod320c032010-06-29 10:07:14 +02003934EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003935
3936/**
3937 * destroy_workqueue - safely terminate a workqueue
3938 * @wq: target workqueue
3939 *
3940 * Safely destroy a workqueue. All work currently pending will be done first.
3941 */
3942void destroy_workqueue(struct workqueue_struct *wq)
3943{
Tejun Heo49e3cf42013-03-12 11:29:58 -07003944 struct pool_workqueue *pwq;
Oleg Nesterov3af244332007-05-09 02:34:09 -07003945
Tejun Heo9c5a2ba2011-04-05 18:01:44 +02003946 /* drain it before proceeding with destruction */
3947 drain_workqueue(wq);
Tejun Heoc8efcc22010-12-20 19:32:04 +01003948
Tejun Heo6183c002013-03-12 11:29:57 -07003949 /* sanity checks */
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07003950 mutex_lock(&wq->mutex);
Tejun Heo49e3cf42013-03-12 11:29:58 -07003951 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07003952 int i;
3953
Tejun Heo76af4d92013-03-12 11:30:00 -07003954 for (i = 0; i < WORK_NR_COLORS; i++) {
3955 if (WARN_ON(pwq->nr_in_flight[i])) {
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07003956 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07003957 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07003958 }
3959 }
3960
Tejun Heo8864b4e2013-03-12 11:30:04 -07003961 if (WARN_ON(pwq->refcnt > 1) ||
3962 WARN_ON(pwq->nr_active) ||
Tejun Heo76af4d92013-03-12 11:30:00 -07003963 WARN_ON(!list_empty(&pwq->delayed_works))) {
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07003964 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07003965 return;
Tejun Heo76af4d92013-03-12 11:30:00 -07003966 }
Tejun Heo6183c002013-03-12 11:29:57 -07003967 }
Lai Jiangshanb09f4fd2013-03-25 16:57:18 -07003968 mutex_unlock(&wq->mutex);
Tejun Heo6183c002013-03-12 11:29:57 -07003969
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02003970 /*
3971 * wq list is used to freeze wq, remove from list after
3972 * flushing is complete in case freeze races us.
3973 */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07003974 mutex_lock(&wq_pool_mutex);
Tejun Heod2c1d402013-03-12 11:30:04 -07003975 list_del_init(&wq->list);
Lai Jiangshan68e13a62013-03-25 16:57:17 -07003976 mutex_unlock(&wq_pool_mutex);
Oleg Nesterov3af244332007-05-09 02:34:09 -07003977
Tejun Heo226223a2013-03-12 11:30:05 -07003978 workqueue_sysfs_unregister(wq);
3979
Tejun Heo493008a2013-03-12 11:30:03 -07003980 if (wq->rescuer) {
Tejun Heoe22bee72010-06-29 10:07:14 +02003981 kthread_stop(wq->rescuer->task);
Xiaotian Feng8d9df9f2010-08-16 09:54:28 +02003982 kfree(wq->rescuer);
Tejun Heo493008a2013-03-12 11:30:03 -07003983 wq->rescuer = NULL;
Tejun Heoe22bee72010-06-29 10:07:14 +02003984 }
3985
Tejun Heo8864b4e2013-03-12 11:30:04 -07003986 if (!(wq->flags & WQ_UNBOUND)) {
3987 /*
3988 * The base ref is never dropped on per-cpu pwqs. Directly
3989 * free the pwqs and wq.
3990 */
3991 free_percpu(wq->cpu_pwqs);
3992 kfree(wq);
3993 } else {
3994 /*
3995 * We're the sole accessor of @wq at this point. Directly
Tejun Heodce90d42013-04-01 11:23:35 -07003996 * access the first pwq and put the base ref. @wq will be
3997 * freed when the last pwq is released.
Tejun Heo8864b4e2013-03-12 11:30:04 -07003998 */
Tejun Heo29c91e92013-03-12 11:30:03 -07003999 pwq = list_first_entry(&wq->pwqs, struct pool_workqueue,
4000 pwqs_node);
Tejun Heodce90d42013-04-01 11:23:35 -07004001 put_pwq_unlocked(pwq);
Tejun Heo29c91e92013-03-12 11:30:03 -07004002 }
Oleg Nesterov3af244332007-05-09 02:34:09 -07004003}
4004EXPORT_SYMBOL_GPL(destroy_workqueue);
4005
Tejun Heodcd989c2010-06-29 10:07:14 +02004006/**
4007 * workqueue_set_max_active - adjust max_active of a workqueue
4008 * @wq: target workqueue
4009 * @max_active: new max_active value.
4010 *
4011 * Set max_active of @wq to @max_active.
4012 *
4013 * CONTEXT:
4014 * Don't call from IRQ context.
4015 */
4016void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
4017{
Tejun Heo49e3cf42013-03-12 11:29:58 -07004018 struct pool_workqueue *pwq;
Tejun Heodcd989c2010-06-29 10:07:14 +02004019
Tejun Heo8719dce2013-03-12 11:30:04 -07004020 /* disallow meddling with max_active for ordered workqueues */
4021 if (WARN_ON(wq->flags & __WQ_ORDERED))
4022 return;
4023
Tejun Heof3421792010-07-02 10:03:51 +02004024 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
Tejun Heodcd989c2010-06-29 10:07:14 +02004025
Lai Jiangshana357fc02013-03-25 16:57:19 -07004026 mutex_lock(&wq->mutex);
Tejun Heodcd989c2010-06-29 10:07:14 +02004027
4028 wq->saved_max_active = max_active;
4029
Tejun Heo699ce092013-03-13 16:51:35 -07004030 for_each_pwq(pwq, wq)
4031 pwq_adjust_max_active(pwq);
Tejun Heodcd989c2010-06-29 10:07:14 +02004032
Lai Jiangshana357fc02013-03-25 16:57:19 -07004033 mutex_unlock(&wq->mutex);
Tejun Heodcd989c2010-06-29 10:07:14 +02004034}
4035EXPORT_SYMBOL_GPL(workqueue_set_max_active);
4036
4037/**
Tejun Heoe6267612013-03-12 17:41:37 -07004038 * current_is_workqueue_rescuer - is %current workqueue rescuer?
4039 *
4040 * Determine whether %current is a workqueue rescuer. Can be used from
4041 * work functions to determine whether it's being run off the rescuer task.
4042 */
4043bool current_is_workqueue_rescuer(void)
4044{
4045 struct worker *worker = current_wq_worker();
4046
Lai Jiangshan6a092df2013-03-20 03:28:03 +08004047 return worker && worker->rescue_wq;
Tejun Heoe6267612013-03-12 17:41:37 -07004048}
4049
4050/**
Tejun Heodcd989c2010-06-29 10:07:14 +02004051 * workqueue_congested - test whether a workqueue is congested
4052 * @cpu: CPU in question
4053 * @wq: target workqueue
4054 *
4055 * Test whether @wq's cpu workqueue for @cpu is congested. There is
4056 * no synchronization around this function and the test result is
4057 * unreliable and only useful as advisory hints or for debugging.
4058 *
4059 * RETURNS:
4060 * %true if congested, %false otherwise.
4061 */
Tejun Heod84ff052013-03-12 11:29:59 -07004062bool workqueue_congested(int cpu, struct workqueue_struct *wq)
Tejun Heodcd989c2010-06-29 10:07:14 +02004063{
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004064 struct pool_workqueue *pwq;
Tejun Heo76af4d92013-03-12 11:30:00 -07004065 bool ret;
4066
Lai Jiangshan88109452013-03-20 03:28:10 +08004067 rcu_read_lock_sched();
Tejun Heo7fb98ea2013-03-12 11:30:00 -07004068
4069 if (!(wq->flags & WQ_UNBOUND))
4070 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
4071 else
Tejun Heodf2d5ae2013-04-01 11:23:35 -07004072 pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));
Tejun Heodcd989c2010-06-29 10:07:14 +02004073
Tejun Heo76af4d92013-03-12 11:30:00 -07004074 ret = !list_empty(&pwq->delayed_works);
Lai Jiangshan88109452013-03-20 03:28:10 +08004075 rcu_read_unlock_sched();
Tejun Heo76af4d92013-03-12 11:30:00 -07004076
4077 return ret;
Tejun Heodcd989c2010-06-29 10:07:14 +02004078}
4079EXPORT_SYMBOL_GPL(workqueue_congested);
4080
4081/**
Tejun Heodcd989c2010-06-29 10:07:14 +02004082 * work_busy - test whether a work is currently pending or running
4083 * @work: the work to be tested
4084 *
4085 * Test whether @work is currently pending or running. There is no
4086 * synchronization around this function and the test result is
4087 * unreliable and only useful as advisory hints or for debugging.
Tejun Heodcd989c2010-06-29 10:07:14 +02004088 *
4089 * RETURNS:
4090 * OR'd bitmask of WORK_BUSY_* bits.
4091 */
4092unsigned int work_busy(struct work_struct *work)
4093{
Tejun Heofa1b54e2013-03-12 11:30:00 -07004094 struct worker_pool *pool;
Tejun Heodcd989c2010-06-29 10:07:14 +02004095 unsigned long flags;
4096 unsigned int ret = 0;
4097
Tejun Heodcd989c2010-06-29 10:07:14 +02004098 if (work_pending(work))
4099 ret |= WORK_BUSY_PENDING;
Tejun Heodcd989c2010-06-29 10:07:14 +02004100
Tejun Heofa1b54e2013-03-12 11:30:00 -07004101 local_irq_save(flags);
4102 pool = get_work_pool(work);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004103 if (pool) {
Tejun Heofa1b54e2013-03-12 11:30:00 -07004104 spin_lock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004105 if (find_worker_executing_work(pool, work))
4106 ret |= WORK_BUSY_RUNNING;
Tejun Heofa1b54e2013-03-12 11:30:00 -07004107 spin_unlock(&pool->lock);
Lai Jiangshan038366c2013-02-06 18:04:53 -08004108 }
Tejun Heofa1b54e2013-03-12 11:30:00 -07004109 local_irq_restore(flags);
Tejun Heodcd989c2010-06-29 10:07:14 +02004110
4111 return ret;
4112}
4113EXPORT_SYMBOL_GPL(work_busy);
4114
Tejun Heodb7bccf2010-06-29 10:07:12 +02004115/*
4116 * CPU hotplug.
4117 *
Tejun Heoe22bee72010-06-29 10:07:14 +02004118 * There are two challenges in supporting CPU hotplug. Firstly, there
Tejun Heo112202d2013-02-13 19:29:12 -08004119 * are a lot of assumptions on strong associations among work, pwq and
Tejun Heo706026c2013-01-24 11:01:34 -08004120 * pool which make migrating pending and scheduled works very
Tejun Heoe22bee72010-06-29 10:07:14 +02004121 * difficult to implement without impacting hot paths. Secondly,
Tejun Heo94cf58b2013-01-24 11:01:33 -08004122 * worker pools serve mix of short, long and very long running works making
Tejun Heoe22bee72010-06-29 10:07:14 +02004123 * blocked draining impractical.
4124 *
Tejun Heo24647572013-01-24 11:01:33 -08004125 * This is solved by allowing the pools to be disassociated from the CPU
Tejun Heo628c78e2012-07-17 12:39:27 -07004126 * running as an unbound one and allowing it to be reattached later if the
4127 * cpu comes back online.
Tejun Heodb7bccf2010-06-29 10:07:12 +02004128 */
4129
Tejun Heo706026c2013-01-24 11:01:34 -08004130static void wq_unbind_fn(struct work_struct *work)
Tejun Heodb7bccf2010-06-29 10:07:12 +02004131{
Tejun Heo38db41d2013-01-24 11:01:34 -08004132 int cpu = smp_processor_id();
Tejun Heo4ce62e92012-07-13 22:16:44 -07004133 struct worker_pool *pool;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004134 struct worker *worker;
Tejun Heoa9ab7752013-03-19 13:45:21 -07004135 int wi;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004136
Tejun Heof02ae732013-03-12 11:30:03 -07004137 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo6183c002013-03-12 11:29:57 -07004138 WARN_ON_ONCE(cpu != smp_processor_id());
Tejun Heo94cf58b2013-01-24 11:01:33 -08004139
Tejun Heobc3a1af2013-03-13 19:47:39 -07004140 mutex_lock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004141 spin_lock_irq(&pool->lock);
4142
4143 /*
Tejun Heobc3a1af2013-03-13 19:47:39 -07004144 * We've blocked all manager operations. Make all workers
Tejun Heo94cf58b2013-01-24 11:01:33 -08004145 * unbound and set DISASSOCIATED. Before this, all workers
4146 * except for the ones which are still executing works from
4147 * before the last CPU down must be on the cpu. After
4148 * this, they may become diasporas.
4149 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07004150 for_each_pool_worker(worker, wi, pool)
Tejun Heoc9e7cf22013-01-24 11:01:33 -08004151 worker->flags |= WORKER_UNBOUND;
Tejun Heodb7bccf2010-06-29 10:07:12 +02004152
Tejun Heo24647572013-01-24 11:01:33 -08004153 pool->flags |= POOL_DISASSOCIATED;
Tejun Heof2d5a0e2012-07-17 12:39:26 -07004154
Tejun Heo94cf58b2013-01-24 11:01:33 -08004155 spin_unlock_irq(&pool->lock);
Tejun Heobc3a1af2013-03-13 19:47:39 -07004156 mutex_unlock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004157 }
Tejun Heoe22bee72010-06-29 10:07:14 +02004158
4159 /*
Tejun Heo628c78e2012-07-17 12:39:27 -07004160 * Call schedule() so that we cross rq->lock and thus can guarantee
4161 * sched callbacks see the %WORKER_UNBOUND flag. This is necessary
4162 * as scheduler callbacks may be invoked from other cpus.
4163 */
4164 schedule();
4165
4166 /*
4167 * Sched callbacks are disabled now. Zap nr_running. After this,
4168 * nr_running stays zero and need_more_worker() and keep_working()
Tejun Heo38db41d2013-01-24 11:01:34 -08004169 * are always true as long as the worklist is not empty. Pools on
4170 * @cpu now behave as unbound (in terms of concurrency management)
4171 * pools which are served by workers tied to the CPU.
Tejun Heo628c78e2012-07-17 12:39:27 -07004172 *
4173 * On return from this function, the current worker would trigger
4174 * unbound chain execution of pending work items if other workers
4175 * didn't already.
Tejun Heoe22bee72010-06-29 10:07:14 +02004176 */
Tejun Heof02ae732013-03-12 11:30:03 -07004177 for_each_cpu_worker_pool(pool, cpu)
Tejun Heoe19e3972013-01-24 11:39:44 -08004178 atomic_set(&pool->nr_running, 0);
Tejun Heodb7bccf2010-06-29 10:07:12 +02004179}
4180
Tejun Heobd7c0892013-03-19 13:45:21 -07004181/**
4182 * rebind_workers - rebind all workers of a pool to the associated CPU
4183 * @pool: pool of interest
4184 *
Tejun Heoa9ab7752013-03-19 13:45:21 -07004185 * @pool->cpu is coming online. Rebind all workers to the CPU.
Tejun Heobd7c0892013-03-19 13:45:21 -07004186 */
4187static void rebind_workers(struct worker_pool *pool)
4188{
Tejun Heoa9ab7752013-03-19 13:45:21 -07004189 struct worker *worker;
4190 int wi;
Tejun Heobd7c0892013-03-19 13:45:21 -07004191
4192 lockdep_assert_held(&pool->manager_mutex);
Tejun Heobd7c0892013-03-19 13:45:21 -07004193
Tejun Heoa9ab7752013-03-19 13:45:21 -07004194 /*
4195 * Restore CPU affinity of all workers. As all idle workers should
4196 * be on the run-queue of the associated CPU before any local
4197 * wake-ups for concurrency management happen, restore CPU affinty
4198 * of all workers first and then clear UNBOUND. As we're called
4199 * from CPU_ONLINE, the following shouldn't fail.
4200 */
4201 for_each_pool_worker(worker, wi, pool)
4202 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
4203 pool->attrs->cpumask) < 0);
4204
4205 spin_lock_irq(&pool->lock);
4206
4207 for_each_pool_worker(worker, wi, pool) {
4208 unsigned int worker_flags = worker->flags;
Tejun Heobd7c0892013-03-19 13:45:21 -07004209
4210 /*
Tejun Heoa9ab7752013-03-19 13:45:21 -07004211 * A bound idle worker should actually be on the runqueue
4212 * of the associated CPU for local wake-ups targeting it to
4213 * work. Kick all idle workers so that they migrate to the
4214 * associated CPU. Doing this in the same loop as
4215 * replacing UNBOUND with REBOUND is safe as no worker will
4216 * be bound before @pool->lock is released.
Tejun Heobd7c0892013-03-19 13:45:21 -07004217 */
Tejun Heoa9ab7752013-03-19 13:45:21 -07004218 if (worker_flags & WORKER_IDLE)
4219 wake_up_process(worker->task);
4220
4221 /*
4222 * We want to clear UNBOUND but can't directly call
4223 * worker_clr_flags() or adjust nr_running. Atomically
4224 * replace UNBOUND with another NOT_RUNNING flag REBOUND.
4225 * @worker will clear REBOUND using worker_clr_flags() when
4226 * it initiates the next execution cycle thus restoring
4227 * concurrency management. Note that when or whether
4228 * @worker clears REBOUND doesn't affect correctness.
4229 *
4230 * ACCESS_ONCE() is necessary because @worker->flags may be
4231 * tested without holding any lock in
4232 * wq_worker_waking_up(). Without it, NOT_RUNNING test may
4233 * fail incorrectly leading to premature concurrency
4234 * management operations.
4235 */
4236 WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND));
4237 worker_flags |= WORKER_REBOUND;
4238 worker_flags &= ~WORKER_UNBOUND;
4239 ACCESS_ONCE(worker->flags) = worker_flags;
Tejun Heobd7c0892013-03-19 13:45:21 -07004240 }
4241
Tejun Heoa9ab7752013-03-19 13:45:21 -07004242 spin_unlock_irq(&pool->lock);
Tejun Heobd7c0892013-03-19 13:45:21 -07004243}
4244
Tejun Heo7dbc7252013-03-19 13:45:21 -07004245/**
4246 * restore_unbound_workers_cpumask - restore cpumask of unbound workers
4247 * @pool: unbound pool of interest
4248 * @cpu: the CPU which is coming up
4249 *
4250 * An unbound pool may end up with a cpumask which doesn't have any online
4251 * CPUs. When a worker of such pool get scheduled, the scheduler resets
4252 * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any
4253 * online CPU before, cpus_allowed of all its workers should be restored.
4254 */
4255static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu)
4256{
4257 static cpumask_t cpumask;
4258 struct worker *worker;
4259 int wi;
4260
4261 lockdep_assert_held(&pool->manager_mutex);
4262
4263 /* is @cpu allowed for @pool? */
4264 if (!cpumask_test_cpu(cpu, pool->attrs->cpumask))
4265 return;
4266
4267 /* is @cpu the only online CPU? */
4268 cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask);
4269 if (cpumask_weight(&cpumask) != 1)
4270 return;
4271
4272 /* as we're called from CPU_ONLINE, the following shouldn't fail */
4273 for_each_pool_worker(worker, wi, pool)
4274 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
4275 pool->attrs->cpumask) < 0);
4276}
4277
Tejun Heo8db25e72012-07-17 12:39:28 -07004278/*
4279 * Workqueues should be brought up before normal priority CPU notifiers.
4280 * This will be registered high priority CPU notifier.
4281 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07004282static int __cpuinit workqueue_cpu_up_callback(struct notifier_block *nfb,
Tejun Heo8db25e72012-07-17 12:39:28 -07004283 unsigned long action,
4284 void *hcpu)
Oleg Nesterov3af244332007-05-09 02:34:09 -07004285{
Tejun Heod84ff052013-03-12 11:29:59 -07004286 int cpu = (unsigned long)hcpu;
Tejun Heo4ce62e92012-07-13 22:16:44 -07004287 struct worker_pool *pool;
Tejun Heo7dbc7252013-03-19 13:45:21 -07004288 int pi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289
Tejun Heo8db25e72012-07-17 12:39:28 -07004290 switch (action & ~CPU_TASKS_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 case CPU_UP_PREPARE:
Tejun Heof02ae732013-03-12 11:30:03 -07004292 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo3ce63372012-07-17 12:39:27 -07004293 if (pool->nr_workers)
4294 continue;
Tejun Heoebf44d12013-03-13 19:47:39 -07004295 if (create_and_start_worker(pool) < 0)
Tejun Heo3ce63372012-07-17 12:39:27 -07004296 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 }
Tejun Heodb7bccf2010-06-29 10:07:12 +02004298 break;
Oleg Nesterov00dfcaf2008-04-29 01:00:27 -07004299
Tejun Heo65758202012-07-17 12:39:26 -07004300 case CPU_DOWN_FAILED:
4301 case CPU_ONLINE:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004302 mutex_lock(&wq_pool_mutex);
Tejun Heo7dbc7252013-03-19 13:45:21 -07004303
4304 for_each_pool(pool, pi) {
Tejun Heobc3a1af2013-03-13 19:47:39 -07004305 mutex_lock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004306
Tejun Heo7dbc7252013-03-19 13:45:21 -07004307 if (pool->cpu == cpu) {
4308 spin_lock_irq(&pool->lock);
4309 pool->flags &= ~POOL_DISASSOCIATED;
4310 spin_unlock_irq(&pool->lock);
Tejun Heoa9ab7752013-03-19 13:45:21 -07004311
Tejun Heo7dbc7252013-03-19 13:45:21 -07004312 rebind_workers(pool);
4313 } else if (pool->cpu < 0) {
4314 restore_unbound_workers_cpumask(pool, cpu);
4315 }
Tejun Heo94cf58b2013-01-24 11:01:33 -08004316
Tejun Heobc3a1af2013-03-13 19:47:39 -07004317 mutex_unlock(&pool->manager_mutex);
Tejun Heo94cf58b2013-01-24 11:01:33 -08004318 }
Tejun Heo7dbc7252013-03-19 13:45:21 -07004319
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004320 mutex_unlock(&wq_pool_mutex);
Tejun Heo8db25e72012-07-17 12:39:28 -07004321 break;
Tejun Heo65758202012-07-17 12:39:26 -07004322 }
4323 return NOTIFY_OK;
4324}
4325
4326/*
4327 * Workqueues should be brought down after normal priority CPU notifiers.
4328 * This will be registered as low priority CPU notifier.
4329 */
Lai Jiangshan9fdf9b72012-09-18 09:59:23 -07004330static int __cpuinit workqueue_cpu_down_callback(struct notifier_block *nfb,
Tejun Heo65758202012-07-17 12:39:26 -07004331 unsigned long action,
4332 void *hcpu)
4333{
Tejun Heod84ff052013-03-12 11:29:59 -07004334 int cpu = (unsigned long)hcpu;
Tejun Heo8db25e72012-07-17 12:39:28 -07004335 struct work_struct unbind_work;
4336
Tejun Heo65758202012-07-17 12:39:26 -07004337 switch (action & ~CPU_TASKS_FROZEN) {
4338 case CPU_DOWN_PREPARE:
Tejun Heo8db25e72012-07-17 12:39:28 -07004339 /* unbinding should happen on the local CPU */
Tejun Heo706026c2013-01-24 11:01:34 -08004340 INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn);
Joonsoo Kim7635d2f2012-08-15 23:25:41 +09004341 queue_work_on(cpu, system_highpri_wq, &unbind_work);
Tejun Heo8db25e72012-07-17 12:39:28 -07004342 flush_work(&unbind_work);
4343 break;
Tejun Heo65758202012-07-17 12:39:26 -07004344 }
4345 return NOTIFY_OK;
4346}
4347
Rusty Russell2d3854a2008-11-05 13:39:10 +11004348#ifdef CONFIG_SMP
Rusty Russell8ccad402009-01-16 15:31:15 -08004349
Rusty Russell2d3854a2008-11-05 13:39:10 +11004350struct work_for_cpu {
Tejun Heoed48ece2012-09-18 12:48:43 -07004351 struct work_struct work;
Rusty Russell2d3854a2008-11-05 13:39:10 +11004352 long (*fn)(void *);
4353 void *arg;
4354 long ret;
4355};
4356
Tejun Heoed48ece2012-09-18 12:48:43 -07004357static void work_for_cpu_fn(struct work_struct *work)
Rusty Russell2d3854a2008-11-05 13:39:10 +11004358{
Tejun Heoed48ece2012-09-18 12:48:43 -07004359 struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work);
4360
Rusty Russell2d3854a2008-11-05 13:39:10 +11004361 wfc->ret = wfc->fn(wfc->arg);
4362}
4363
4364/**
4365 * work_on_cpu - run a function in user context on a particular cpu
4366 * @cpu: the cpu to run on
4367 * @fn: the function to run
4368 * @arg: the function arg
4369 *
Rusty Russell31ad9082009-01-16 15:31:15 -08004370 * This will return the value @fn returns.
4371 * It is up to the caller to ensure that the cpu doesn't go offline.
Andrew Morton6b440032009-04-09 09:50:37 -06004372 * The caller must not hold any locks which would prevent @fn from completing.
Rusty Russell2d3854a2008-11-05 13:39:10 +11004373 */
Tejun Heod84ff052013-03-12 11:29:59 -07004374long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
Rusty Russell2d3854a2008-11-05 13:39:10 +11004375{
Tejun Heoed48ece2012-09-18 12:48:43 -07004376 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
Rusty Russell2d3854a2008-11-05 13:39:10 +11004377
Tejun Heoed48ece2012-09-18 12:48:43 -07004378 INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn);
4379 schedule_work_on(cpu, &wfc.work);
4380 flush_work(&wfc.work);
Rusty Russell2d3854a2008-11-05 13:39:10 +11004381 return wfc.ret;
4382}
4383EXPORT_SYMBOL_GPL(work_on_cpu);
4384#endif /* CONFIG_SMP */
4385
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004386#ifdef CONFIG_FREEZER
Rusty Russelle7577c52009-01-01 10:12:25 +10304387
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004388/**
4389 * freeze_workqueues_begin - begin freezing workqueues
4390 *
Tejun Heo58a69cb2011-02-16 09:25:31 +01004391 * Start freezing workqueues. After this function returns, all freezable
Tejun Heoc5aa87b2013-03-13 16:51:36 -07004392 * workqueues will queue new works to their delayed_works list instead of
Tejun Heo706026c2013-01-24 11:01:34 -08004393 * pool->worklist.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004394 *
4395 * CONTEXT:
Lai Jiangshana357fc02013-03-25 16:57:19 -07004396 * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004397 */
4398void freeze_workqueues_begin(void)
4399{
Tejun Heo17116962013-03-12 11:29:58 -07004400 struct worker_pool *pool;
Tejun Heo24b8a842013-03-12 11:29:58 -07004401 struct workqueue_struct *wq;
4402 struct pool_workqueue *pwq;
Tejun Heo611c92a2013-03-13 16:51:36 -07004403 int pi;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004404
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004405 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004406
Tejun Heo6183c002013-03-12 11:29:57 -07004407 WARN_ON_ONCE(workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004408 workqueue_freezing = true;
4409
Tejun Heo24b8a842013-03-12 11:29:58 -07004410 /* set FREEZING */
Tejun Heo611c92a2013-03-13 16:51:36 -07004411 for_each_pool(pool, pi) {
Tejun Heo5bcab332013-03-13 19:47:40 -07004412 spin_lock_irq(&pool->lock);
Tejun Heo17116962013-03-12 11:29:58 -07004413 WARN_ON_ONCE(pool->flags & POOL_FREEZING);
4414 pool->flags |= POOL_FREEZING;
Tejun Heo5bcab332013-03-13 19:47:40 -07004415 spin_unlock_irq(&pool->lock);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004416 }
4417
Tejun Heo24b8a842013-03-12 11:29:58 -07004418 list_for_each_entry(wq, &workqueues, list) {
Lai Jiangshana357fc02013-03-25 16:57:19 -07004419 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07004420 for_each_pwq(pwq, wq)
4421 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07004422 mutex_unlock(&wq->mutex);
Tejun Heo24b8a842013-03-12 11:29:58 -07004423 }
Tejun Heo5bcab332013-03-13 19:47:40 -07004424
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004425 mutex_unlock(&wq_pool_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426}
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004427
4428/**
Tejun Heo58a69cb2011-02-16 09:25:31 +01004429 * freeze_workqueues_busy - are freezable workqueues still busy?
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004430 *
4431 * Check whether freezing is complete. This function must be called
4432 * between freeze_workqueues_begin() and thaw_workqueues().
4433 *
4434 * CONTEXT:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004435 * Grabs and releases wq_pool_mutex.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004436 *
4437 * RETURNS:
Tejun Heo58a69cb2011-02-16 09:25:31 +01004438 * %true if some freezable workqueues are still busy. %false if freezing
4439 * is complete.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004440 */
4441bool freeze_workqueues_busy(void)
4442{
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004443 bool busy = false;
Tejun Heo24b8a842013-03-12 11:29:58 -07004444 struct workqueue_struct *wq;
4445 struct pool_workqueue *pwq;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004446
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004447 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004448
Tejun Heo6183c002013-03-12 11:29:57 -07004449 WARN_ON_ONCE(!workqueue_freezing);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004450
Tejun Heo24b8a842013-03-12 11:29:58 -07004451 list_for_each_entry(wq, &workqueues, list) {
4452 if (!(wq->flags & WQ_FREEZABLE))
4453 continue;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004454 /*
4455 * nr_active is monotonically decreasing. It's safe
4456 * to peek without lock.
4457 */
Lai Jiangshan88109452013-03-20 03:28:10 +08004458 rcu_read_lock_sched();
Tejun Heo24b8a842013-03-12 11:29:58 -07004459 for_each_pwq(pwq, wq) {
Tejun Heo6183c002013-03-12 11:29:57 -07004460 WARN_ON_ONCE(pwq->nr_active < 0);
Tejun Heo112202d2013-02-13 19:29:12 -08004461 if (pwq->nr_active) {
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004462 busy = true;
Lai Jiangshan88109452013-03-20 03:28:10 +08004463 rcu_read_unlock_sched();
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004464 goto out_unlock;
4465 }
4466 }
Lai Jiangshan88109452013-03-20 03:28:10 +08004467 rcu_read_unlock_sched();
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004468 }
4469out_unlock:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004470 mutex_unlock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004471 return busy;
4472}
4473
4474/**
4475 * thaw_workqueues - thaw workqueues
4476 *
4477 * Thaw workqueues. Normal queueing is restored and all collected
Tejun Heo706026c2013-01-24 11:01:34 -08004478 * frozen works are transferred to their respective pool worklists.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004479 *
4480 * CONTEXT:
Lai Jiangshana357fc02013-03-25 16:57:19 -07004481 * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's.
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004482 */
4483void thaw_workqueues(void)
4484{
Tejun Heo24b8a842013-03-12 11:29:58 -07004485 struct workqueue_struct *wq;
4486 struct pool_workqueue *pwq;
4487 struct worker_pool *pool;
Tejun Heo611c92a2013-03-13 16:51:36 -07004488 int pi;
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004489
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004490 mutex_lock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004491
4492 if (!workqueue_freezing)
4493 goto out_unlock;
4494
Tejun Heo24b8a842013-03-12 11:29:58 -07004495 /* clear FREEZING */
Tejun Heo611c92a2013-03-13 16:51:36 -07004496 for_each_pool(pool, pi) {
Tejun Heo5bcab332013-03-13 19:47:40 -07004497 spin_lock_irq(&pool->lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004498 WARN_ON_ONCE(!(pool->flags & POOL_FREEZING));
4499 pool->flags &= ~POOL_FREEZING;
Tejun Heo5bcab332013-03-13 19:47:40 -07004500 spin_unlock_irq(&pool->lock);
Tejun Heo24b8a842013-03-12 11:29:58 -07004501 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02004502
Tejun Heo24b8a842013-03-12 11:29:58 -07004503 /* restore max_active and repopulate worklist */
4504 list_for_each_entry(wq, &workqueues, list) {
Lai Jiangshana357fc02013-03-25 16:57:19 -07004505 mutex_lock(&wq->mutex);
Tejun Heo699ce092013-03-13 16:51:35 -07004506 for_each_pwq(pwq, wq)
4507 pwq_adjust_max_active(pwq);
Lai Jiangshana357fc02013-03-25 16:57:19 -07004508 mutex_unlock(&wq->mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004509 }
4510
4511 workqueue_freezing = false;
4512out_unlock:
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004513 mutex_unlock(&wq_pool_mutex);
Tejun Heoa0a1a5f2010-06-29 10:07:12 +02004514}
4515#endif /* CONFIG_FREEZER */
4516
Tejun Heobce90382013-04-01 11:23:32 -07004517static void __init wq_numa_init(void)
4518{
4519 cpumask_var_t *tbl;
4520 int node, cpu;
4521
4522 /* determine NUMA pwq table len - highest node id + 1 */
4523 for_each_node(node)
4524 wq_numa_tbl_len = max(wq_numa_tbl_len, node + 1);
4525
4526 if (num_possible_nodes() <= 1)
4527 return;
4528
4529 /*
4530 * We want masks of possible CPUs of each node which isn't readily
4531 * available. Build one from cpu_to_node() which should have been
4532 * fully initialized by now.
4533 */
4534 tbl = kzalloc(wq_numa_tbl_len * sizeof(tbl[0]), GFP_KERNEL);
4535 BUG_ON(!tbl);
4536
4537 for_each_node(node)
4538 BUG_ON(!alloc_cpumask_var_node(&tbl[node], GFP_KERNEL, node));
4539
4540 for_each_possible_cpu(cpu) {
4541 node = cpu_to_node(cpu);
4542 if (WARN_ON(node == NUMA_NO_NODE)) {
4543 pr_warn("workqueue: NUMA node mapping not available for cpu%d, disabling NUMA support\n", cpu);
4544 /* happens iff arch is bonkers, let's just proceed */
4545 return;
4546 }
4547 cpumask_set_cpu(cpu, tbl[node]);
4548 }
4549
4550 wq_numa_possible_cpumask = tbl;
4551 wq_numa_enabled = true;
4552}
4553
Suresh Siddha6ee05782010-07-30 14:57:37 -07004554static int __init init_workqueues(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555{
Tejun Heo7a4e3442013-03-12 11:30:00 -07004556 int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL };
4557 int i, cpu;
Tejun Heoc34056a2010-06-29 10:07:11 +02004558
Tejun Heo7c3eed52013-01-24 11:01:33 -08004559 /* make sure we have enough bits for OFFQ pool ID */
4560 BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) <
Lai Jiangshan6be19582013-02-06 18:04:53 -08004561 WORK_CPU_END * NR_STD_WORKER_POOLS);
Tejun Heob5490072012-08-03 10:30:46 -07004562
Tejun Heoe904e6c2013-03-12 11:29:57 -07004563 WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long));
4564
4565 pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC);
4566
Tejun Heo65758202012-07-17 12:39:26 -07004567 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
Lai Jiangshana5b4e572012-09-18 09:59:23 -07004568 hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
Tejun Heo8b03ae32010-06-29 10:07:12 +02004569
Tejun Heobce90382013-04-01 11:23:32 -07004570 wq_numa_init();
4571
Tejun Heo706026c2013-01-24 11:01:34 -08004572 /* initialize CPU pools */
Tejun Heo29c91e92013-03-12 11:30:03 -07004573 for_each_possible_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07004574 struct worker_pool *pool;
Tejun Heo8b03ae32010-06-29 10:07:12 +02004575
Tejun Heo7a4e3442013-03-12 11:30:00 -07004576 i = 0;
Tejun Heof02ae732013-03-12 11:30:03 -07004577 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo7a4e3442013-03-12 11:30:00 -07004578 BUG_ON(init_worker_pool(pool));
Tejun Heoec22ca52013-01-24 11:01:33 -08004579 pool->cpu = cpu;
Tejun Heo29c91e92013-03-12 11:30:03 -07004580 cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu));
Tejun Heo7a4e3442013-03-12 11:30:00 -07004581 pool->attrs->nice = std_nice[i++];
Tejun Heof3f90ad2013-04-01 11:23:34 -07004582 pool->node = cpu_to_node(cpu);
Tejun Heo7a4e3442013-03-12 11:30:00 -07004583
Tejun Heo9daf9e62013-01-24 11:01:33 -08004584 /* alloc pool ID */
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004585 mutex_lock(&wq_pool_mutex);
Tejun Heo9daf9e62013-01-24 11:01:33 -08004586 BUG_ON(worker_pool_assign_id(pool));
Lai Jiangshan68e13a62013-03-25 16:57:17 -07004587 mutex_unlock(&wq_pool_mutex);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004588 }
Tejun Heo8b03ae32010-06-29 10:07:12 +02004589 }
4590
Tejun Heoe22bee72010-06-29 10:07:14 +02004591 /* create the initial worker */
Tejun Heo29c91e92013-03-12 11:30:03 -07004592 for_each_online_cpu(cpu) {
Tejun Heo4ce62e92012-07-13 22:16:44 -07004593 struct worker_pool *pool;
Tejun Heoe22bee72010-06-29 10:07:14 +02004594
Tejun Heof02ae732013-03-12 11:30:03 -07004595 for_each_cpu_worker_pool(pool, cpu) {
Tejun Heo29c91e92013-03-12 11:30:03 -07004596 pool->flags &= ~POOL_DISASSOCIATED;
Tejun Heoebf44d12013-03-13 19:47:39 -07004597 BUG_ON(create_and_start_worker(pool) < 0);
Tejun Heo4ce62e92012-07-13 22:16:44 -07004598 }
Tejun Heoe22bee72010-06-29 10:07:14 +02004599 }
4600
Tejun Heo29c91e92013-03-12 11:30:03 -07004601 /* create default unbound wq attrs */
4602 for (i = 0; i < NR_STD_WORKER_POOLS; i++) {
4603 struct workqueue_attrs *attrs;
4604
4605 BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL)));
Tejun Heo29c91e92013-03-12 11:30:03 -07004606 attrs->nice = std_nice[i];
Tejun Heo29c91e92013-03-12 11:30:03 -07004607 unbound_std_wq_attrs[i] = attrs;
4608 }
4609
Tejun Heod320c032010-06-29 10:07:14 +02004610 system_wq = alloc_workqueue("events", 0, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09004611 system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);
Tejun Heod320c032010-06-29 10:07:14 +02004612 system_long_wq = alloc_workqueue("events_long", 0, 0);
Tejun Heof3421792010-07-02 10:03:51 +02004613 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
4614 WQ_UNBOUND_MAX_ACTIVE);
Tejun Heo24d51ad2011-02-21 09:52:50 +01004615 system_freezable_wq = alloc_workqueue("events_freezable",
4616 WQ_FREEZABLE, 0);
Joonsoo Kim1aabe902012-08-15 23:25:39 +09004617 BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq ||
Tejun Heoae930e02012-08-20 14:51:23 -07004618 !system_unbound_wq || !system_freezable_wq);
Suresh Siddha6ee05782010-07-30 14:57:37 -07004619 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620}
Suresh Siddha6ee05782010-07-30 14:57:37 -07004621early_initcall(init_workqueues);